Ticket #8706: DXAChanges4

File DXAChanges4, 8.9 KB (added by SF/robinwatts, 14 years ago)

Fourth version of the DXA diffs

Line 
1Index: graphics/dxa_player.cpp
2===================================================================
3--- graphics/dxa_player.cpp (revision 27747)
4+++ graphics/dxa_player.cpp (working copy)
5@@ -42,6 +42,12 @@
6 _scaledBuffer = 0;
7 _drawBuffer = 0;
8
9+ _inBuffer = 0;
10+ _inBufferSize = 0;
11+
12+ _decompBuffer = 0;
13+ _decompBufferSize = 0;
14+
15 _width = 0;
16 _height = 0;
17
18@@ -53,6 +59,12 @@
19 _frameTicks = 0;
20
21 _scaleMode = S_NONE;
22+
23+ _d_stream.zalloc = (alloc_func)0;
24+ _d_stream.zfree = (free_func)0;
25+ _d_stream.opaque = (voidpf)0;
26+
27+ _continuousCompression = false;
28 }
29
30 DXAPlayer::~DXAPlayer() {
31@@ -129,6 +141,7 @@
32 debug(2, "flags 0x0%x framesCount %d width %d height %d rate %d ticks %d", flags, _framesCount, _width, _height, _framesPerSec, _frameTicks);
33
34 _frameSize = _width * _height;
35+ _decompBufferSize = _frameSize;
36 _frameBuffer1 = (uint8 *)malloc(_frameSize);
37 _frameBuffer2 = (uint8 *)malloc(_frameSize);
38 if (!_frameBuffer1 || !_frameBuffer2)
39@@ -141,6 +154,40 @@
40 error("Error allocating scale buffer (size %d)", _frameSize);
41 }
42
43+ // Check for an extended header
44+ if (flags & 1) {
45+ uint32 size;
46+
47+ do {
48+ tag = _fd->readUint32BE();
49+ if (tag != 0) {
50+ size = _fd->readUint32BE();
51+ }
52+ switch (tag) {
53+ case 0: // No more tags
54+ break;
55+ case MKID_BE('MAXD'):
56+ assert(size == 4);
57+ _decompBufferSize = _fd->readUint32BE();
58+ break;
59+ case MKID_BE('ZCNT'):
60+ // This tag specifies that we are
61+ // using continuous compression -
62+ // i.e. that the compression context
63+ // is not restarted between frames.
64+ _continuousCompression = true;
65+ inflateInit(&_d_stream);
66+ break;
67+ default: // Unknown tag - skip it.
68+ while (size > 0) {
69+ byte dummy = _fd->readByte();
70+ size--;
71+ }
72+ break;
73+ }
74+ } while (tag != 0);
75+ }
76+
77 _frameNum = 0;
78 _frameSkipped = 0;
79
80@@ -157,6 +204,11 @@
81 free(_frameBuffer1);
82 free(_frameBuffer2);
83 free(_scaledBuffer);
84+ free(_inBuffer);
85+ free(_decompBuffer);
86+ if (_continuousCompression) {
87+ inflateEnd(&_d_stream);
88+ }
89 }
90
91 void DXAPlayer::copyFrameToBuffer(byte *dst, uint x, uint y, uint pitch) {
92@@ -175,23 +227,17 @@
93
94 void DXAPlayer::decodeZlib(byte *data, int size, int totalSize) {
95 #ifdef USE_ZLIB
96- byte *temp = (byte *)malloc(size);
97- if (temp) {
98- memcpy(temp, data, size);
99-
100- z_stream d_stream;
101- d_stream.zalloc = (alloc_func)0;
102- d_stream.zfree = (free_func)0;
103- d_stream.opaque = (voidpf)0;
104- d_stream.next_in = temp;
105- d_stream.avail_in = size;
106- d_stream.total_in = size;
107- d_stream.next_out = data;
108- d_stream.avail_out = totalSize;
109- inflateInit(&d_stream);
110- inflate(&d_stream, Z_FINISH);
111- inflateEnd(&d_stream);
112- free(temp);
113+ _d_stream.next_in = _inBuffer;
114+ _d_stream.avail_in = size;
115+ _d_stream.total_in = size;
116+ _d_stream.next_out = data;
117+ _d_stream.avail_out = totalSize;
118+ if (_continuousCompression) {
119+ inflate(&_d_stream, Z_SYNC_FLUSH);
120+ } else {
121+ inflateInit(&_d_stream);
122+ inflate(&_d_stream, Z_FINISH);
123+ inflateEnd(&_d_stream);
124 }
125 #endif
126 }
127@@ -199,20 +245,24 @@
128 #define BLOCKW 4
129 #define BLOCKH 4
130
131-void DXAPlayer::decode12(byte *data, int size, int totalSize) {
132+void DXAPlayer::decode12(int size) {
133 #ifdef USE_ZLIB
134+ if (_decompBuffer == NULL) {
135+ _decompBuffer = (byte *)malloc(_decompBufferSize);
136+ if (_decompBuffer == NULL)
137+ error("Error allocating decomp buffer (size %d)", _decompBufferSize);
138+ }
139 /* decompress the input data */
140- decodeZlib(data, size, totalSize);
141+ decodeZlib(_decompBuffer, size, _decompBufferSize);
142
143- byte *dat = data;
144- byte *frame2 = (byte *)malloc(totalSize);
145+ byte *dat = _decompBuffer;
146
147- memcpy(frame2, _frameBuffer1, totalSize);
148+ memcpy(_frameBuffer2, _frameBuffer1, _frameSize);
149
150 for (int by = 0; by < _height; by += BLOCKH) {
151 for (int bx = 0; bx < _width; bx += BLOCKW) {
152 byte type = *dat++;
153- byte *b2 = frame2 + bx + by * _width;
154+ byte *b2 = _frameBuffer1 + bx + by * _width;
155
156 switch (type) {
157 case 0:
158@@ -276,7 +326,7 @@
159 int my = mbyte & 0x07;
160 if (mbyte & 0x08)
161 my = -my;
162- byte *b1 = _frameBuffer1 + (bx+mx) + (by+my) * _width;
163+ byte *b1 = _frameBuffer2 + (bx+mx) + (by+my) * _width;
164 for (int yc = 0; yc < BLOCKH; yc++) {
165 memcpy(b2, b1, BLOCKW);
166 b1 += _width;
167@@ -291,30 +341,32 @@
168 }
169 }
170 }
171-
172- memcpy(data, frame2, totalSize);
173- free(frame2);
174 #endif
175 }
176
177-void DXAPlayer::decode13(byte *data, int size, int totalSize) {
178+void DXAPlayer::decode13(int size) {
179 #ifdef USE_ZLIB
180 uint8 *codeBuf, *dataBuf, *motBuf, *maskBuf;
181
182+ if (_decompBuffer == NULL) {
183+ _decompBuffer = (byte *)malloc(_decompBufferSize);
184+ if (_decompBuffer == NULL)
185+ error("Error allocating decomp buffer (size %d)", _decompBufferSize);
186+ }
187+
188 /* decompress the input data */
189- decodeZlib(data, size, totalSize);
190+ decodeZlib(_decompBuffer, size, _decompBufferSize);
191
192- uint8 *frame2 = (uint8*)malloc(totalSize);
193- memcpy(frame2, _frameBuffer1, totalSize);
194+ memcpy(_frameBuffer2, _frameBuffer1, _frameSize);
195
196 int codeSize = _width * _curHeight / 16;
197 int dataSize, motSize, maskSize;
198
199- dataSize = READ_BE_UINT32(&data[0]);
200- motSize = READ_BE_UINT32(&data[4]);
201- maskSize = READ_BE_UINT32(&data[8]);
202+ dataSize = READ_BE_UINT32(&_decompBuffer[0]);
203+ motSize = READ_BE_UINT32(&_decompBuffer[4]);
204+ maskSize = READ_BE_UINT32(&_decompBuffer[8]);
205
206- codeBuf = &data[12];
207+ codeBuf = &_decompBuffer[12];
208 dataBuf = &codeBuf[codeSize];
209 motBuf = &dataBuf[dataSize];
210 maskBuf = &motBuf[motSize];
211@@ -322,7 +374,7 @@
212 for (int by = 0; by < _curHeight; by += BLOCKH) {
213 for (int bx = 0; bx < _width; bx += BLOCKW) {
214 uint8 type = *codeBuf++;
215- uint8 *b2 = (uint8*)frame2 + bx + by * _width;
216+ uint8 *b2 = (uint8*)_frameBuffer1 + bx + by * _width;
217
218 switch (type) {
219 case 0:
220@@ -373,7 +425,7 @@
221 if (mbyte & 0x08)
222 my = -my;
223
224- uint8 *b1 = (uint8*)_frameBuffer1 + (bx+mx) + (by+my) * _width;
225+ uint8 *b1 = (uint8*)_frameBuffer2 + (bx+mx) + (by+my) * _width;
226 for (int yc = 0; yc < BLOCKH; yc++) {
227 memcpy(b2, b1, BLOCKW);
228 b1 += _width;
229@@ -389,7 +441,7 @@
230
231 for (int subBlock = 0; subBlock < 4; subBlock++) {
232 int sx = bx + subX[subBlock], sy = by + subY[subBlock];
233- b2 = (uint8*)frame2 + sx + sy * _width;
234+ b2 = (uint8*)_frameBuffer1 + sx + sy * _width;
235 switch (subMask & 0xC0) {
236 // 00: skip
237 case 0x00:
238@@ -417,7 +469,7 @@
239 if (mbyte & 0x08)
240 my = -my;
241
242- uint8 *b1 = (uint8*)_frameBuffer1 + (sx+mx) + (sy+my) * _width;
243+ uint8 *b1 = (uint8*)_frameBuffer2 + (sx+mx) + (sy+my) * _width;
244 for (int yc = 0; yc < BLOCKH / 2; yc++) {
245 memcpy(b2, b1, BLOCKW / 2);
246 b1 += _width;
247@@ -476,9 +528,6 @@
248 }
249 }
250 }
251-
252- memcpy(data, frame2, totalSize);
253- free(frame2);
254 #endif
255 }
256
257@@ -498,25 +547,34 @@
258 byte type = _fd->readByte();
259 uint32 size = _fd->readUint32BE();
260
261- _fd->read(_frameBuffer2, size);
262+ if ((_inBuffer == NULL) || (_inBufferSize < size)) {
263+ free(_inBuffer);
264+ _inBuffer = (byte *)malloc(size);
265+ if (_inBuffer == NULL)
266+ error("Error allocating input buffer (size %d)", size);
267+ _inBufferSize = size;
268+ }
269
270+ _fd->read(_inBuffer, size);
271+
272 switch (type) {
273 case 2:
274+ decodeZlib(_frameBuffer1, size, _frameSize);
275+ break;
276 case 3:
277 decodeZlib(_frameBuffer2, size, _frameSize);
278 break;
279 case 12:
280- decode12(_frameBuffer2, size, _frameSize);
281+ decode12(size);
282 break;
283 case 13:
284- decode13(_frameBuffer2, size, _frameSize);
285+ decode13(size);
286 break;
287 default:
288 error("decodeFrame: Unknown compression type %d", type);
289 }
290- if (type == 2 || type == 4 || type == 12 || type == 13) {
291- memcpy(_frameBuffer1, _frameBuffer2, _frameSize);
292- } else {
293+
294+ if (type != 2 && type != 12 && type != 13) {
295 for (int j = 0; j < _curHeight; ++j) {
296 for (int i = 0; i < _width; ++i) {
297 const int offs = j * _width + i;
298Index: graphics/dxa_player.h
299===================================================================
300--- graphics/dxa_player.h (revision 27747)
301+++ graphics/dxa_player.h (working copy)
302@@ -29,6 +29,10 @@
303 #include "common/scummsys.h"
304 #include "common/file.h"
305
306+#ifdef USE_ZLIB
307+#include <zlib.h>
308+#endif
309+
310 namespace Common {
311 class File;
312 }
313@@ -47,6 +51,10 @@
314 byte *_frameBuffer2;
315 byte *_scaledBuffer;
316 byte *_drawBuffer;
317+ byte *_inBuffer;
318+ uint32 _inBufferSize;
319+ byte *_decompBuffer;
320+ uint32 _decompBufferSize;
321 uint16 _width;
322 uint16 _height, _curHeight;
323 uint16 _framesCount;
324@@ -56,6 +64,10 @@
325 uint16 _frameSkipped;
326 uint32 _frameTicks;
327 ScaleMode _scaleMode;
328+ bool _continuousCompression;
329+#ifdef USE_ZLIB
330+ z_stream _d_stream;
331+#endif
332
333 public:
334 DXAPlayer();
335@@ -121,10 +133,10 @@
336 void decodeNextFrame();
337
338 void decodeZlib(byte *data, int size, int totalSize);
339- void decode12(byte *data, int size, int totalSize);
340- void decode13(byte *data, int size, int totalSize);
341+ void decode12(int size);
342+ void decode13(int size);
343 };
344-
345+
346 } // End of namespace Graphics
347
348 #endif