Debian Codegraph @

src/libs/zmbv/zmbv.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include <zlib.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24
25 #include "zmbv.h"
26
27 #define DBZV_VERSION_HIGH 0
28 #define DBZV_VERSION_LOW 1
29
30 #define COMPRESSION_NONE 0
31 #define COMPRESSION_ZLIB 1
32
33 #define MAX_VECTOR 16
34
35 #define Mask_KeyFrame 0x01
36 #define Mask_DeltaPalette 0x02
37
38 zmbv_format_t BPPFormat( int bpp ) {
39 switch (bpp) {
40 case 8:
41 return ZMBV_FORMAT_8BPP;
42 case 15:
43 return ZMBV_FORMAT_15BPP;
44 case 16:
45 return ZMBV_FORMAT_16BPP;
46 case 32:
47 return ZMBV_FORMAT_32BPP;
48 }
49 return ZMBV_FORMAT_NONE;
50 }
51 int VideoCodec::NeededSize( int _width, int _height, zmbv_format_t _format) {
52 int f;
53 switch (_format) {
54 case ZMBV_FORMAT_8BPP:f = 1;break;
55 case ZMBV_FORMAT_15BPP:f = 2;break;
56 case ZMBV_FORMAT_16BPP:f = 2;break;
57 case ZMBV_FORMAT_32BPP:f = 4;break;
58 default:
59 return -1;
60 }
61 f = f*_width*_height + 2*(1+(_width/8)) * (1+(_height/8))+1024;
62 return f + f/1000;
63 }
64
65 bool VideoCodec::SetupBuffers(zmbv_format_t _format, int blockwidth, int blockheight) {
66 FreeBuffers();
67 palsize = 0;
68 switch (_format) {
69 case ZMBV_FORMAT_8BPP:
70 pixelsize = 1;
71 palsize = 256;
72 break;
73 case ZMBV_FORMAT_15BPP:
74 pixelsize = 2;
75 break;
76 case ZMBV_FORMAT_16BPP:
77 pixelsize = 2;
78 break;
79 case ZMBV_FORMAT_32BPP:
80 pixelsize = 4;
81 break;
82 default:
83 return false;
84 };
85 bufsize = (height+2*MAX_VECTOR)*pitch*pixelsize+2048;
86
87 buf1 = new unsigned char[bufsize];
88 buf2 = new unsigned char[bufsize];
89 work = new unsigned char[bufsize];
90
91 int xblocks = (width/blockwidth);
92 int xleft = width % blockwidth;
93 if (xleft) xblocks++;
94 int yblocks = (height/blockheight);
95 int yleft = height % blockheight;
96 if (yleft) yblocks++;
97 blockcount=yblocks*xblocks;
98 blocks=new FrameBlock[blockcount];
99
100 if (!buf1 || !buf2 || !work || !blocks) {
101 FreeBuffers();
102 return false;
103 }
104 int y,x,i;
105 i=0;
106 for (y=0;y<yblocks;y++) {
107 for (x=0;x<xblocks;x++) {
108 blocks[i].start=((y*blockheight)+MAX_VECTOR)*pitch+
109 (x*blockwidth)+MAX_VECTOR;
110 if (xleft && x==(xblocks-1)) {
111 blocks[i].dx=xleft;
112 } else {
113 blocks[i].dx=blockwidth;
114 }
115 if (yleft && y==(yblocks-1)) {
116 blocks[i].dy=yleft;
117 } else {
118 blocks[i].dy=blockheight;
119 }
120 i++;
121 }
122 }
123
124 memset(buf1,0,bufsize);
125 memset(buf2,0,bufsize);
126 memset(work,0,bufsize);
127 oldframe=buf1;
128 newframe=buf2;
129 format = _format;
130 return true;
131 }
132
133 void VideoCodec::CreateVectorTable(void) {
134 int x,y,s;
135 VectorCount=1;
136
137 VectorTable[0].x=VectorTable[0].y=0;
138 for (s=1;s<=10;s++) {
139 for (y=0-s;y<=0+s;y++) for (x=0-s;x<=0+s;x++) {
140 if (abs(x)==s || abs(y)==s) {
141 VectorTable[VectorCount].x=x;
142 VectorTable[VectorCount].y=y;
143 VectorCount++;
144 }
145 }
146 }
147 }
148
149 template<class P>
150 INLINE int VideoCodec::PossibleBlock(int vx,int vy,FrameBlock * block) {
151 int ret=0;
152 P * pold=((P*)oldframe)+block->start+(vy*pitch)+vx;
153 P * pnew=((P*)newframe)+block->start;;
154 for (int y=0;y<block->dy;y+=4) {
155 for (int x=0;x<block->dx;x+=4) {
156 int test=0-((pold[x]-pnew[x])&0x00ffffff);
157 ret-=(test>>31);
158 }
159 pold+=pitch*4;
160 pnew+=pitch*4;
161 }
162 return ret;
163 }
164
165 template<class P>
166 INLINE int VideoCodec::CompareBlock(int vx,int vy,FrameBlock * block) {
167 int ret=0;
168 P * pold=((P*)oldframe)+block->start+(vy*pitch)+vx;
169 P * pnew=((P*)newframe)+block->start;;
170 for (int y=0;y<block->dy;y++) {
171 for (int x=0;x<block->dx;x++) {
172 int test=0-((pold[x]-pnew[x])&0x00ffffff);
173 ret-=(test>>31);
174 }
175 pold+=pitch;
176 pnew+=pitch;
177 }
178 return ret;
179 }
180
181 template<class P>
182 INLINE void VideoCodec::AddXorBlock(int vx,int vy,FrameBlock * block) {
183 P * pold=((P*)oldframe)+block->start+(vy*pitch)+vx;
184 P * pnew=((P*)newframe)+block->start;
185 for (int y=0;y<block->dy;y++) {
186 for (int x=0;x<block->dx;x++) {
187 *((P*)&work[workUsed])=pnew[x] ^ pold[x];
188 workUsed+=sizeof(P);
189 }
190 pold+=pitch;
191 pnew+=pitch;
192 }
193 }
194
195 template<class P>
196 void VideoCodec::AddXorFrame(void) {
197 int written=0;
198 int lastvector=0;
199 signed char * vectors=(signed char*)&work[workUsed];
200 /* Align the following xor data on 4 byte boundary*/
201 workUsed=(workUsed + blockcount*2 +3) & ~3;
202 int totalx=0;
203 int totaly=0;
204 for (int b=0;b<blockcount;b++) {
205 FrameBlock * block=&blocks[b];
206 int bestvx = 0;
207 int bestvy = 0;
208 int bestchange=CompareBlock<P>(0,0, block);
209 int possibles=64;
210 for (int v=0;v<VectorCount && possibles;v++) {
211 if (bestchange<4) break;
212 int vx = VectorTable[v].x;
213 int vy = VectorTable[v].y;
214 if (PossibleBlock<P>(vx, vy, block) < 4) {
215 possibles--;
216 // if (!possibles) Msg("Ran out of possibles, at %d of %d best %d\n",v,VectorCount,bestchange);
217 int testchange=CompareBlock<P>(vx,vy, block);
218 if (testchange<bestchange) {
219 bestchange=testchange;
220 bestvx = vx;
221 bestvy = vy;
222 }
223 }
224 }
225 vectors[b*2+0]=(bestvx << 1);
226 vectors[b*2+1]=(bestvy << 1);
227 if (bestchange) {
228 vectors[b*2+0]|=1;
229 AddXorBlock<P>(bestvx, bestvy, block);
230 }
231 }
232 }
233
234 bool VideoCodec::SetupCompress( int _width, int _height ) {
235 width = _width;
236 height = _height;
237 pitch = _width + 2*MAX_VECTOR;
238 format = ZMBV_FORMAT_NONE;
239 if (deflateInit (&zstream, 4) != Z_OK)
240 return false;
241 return true;
242 }
243
244 bool VideoCodec::SetupDecompress( int _width, int _height) {
245 width = _width;
246 height = _height;
247 pitch = _width + 2*MAX_VECTOR;
248 format = ZMBV_FORMAT_NONE;
249 if (inflateInit (&zstream) != Z_OK)
250 return false;
251 return true;
252 }
253
254 bool VideoCodec::PrepareCompressFrame(int flags, zmbv_format_t _format, char * pal, void *writeBuf, int writeSize) {
255 int i;
256 unsigned char *firstByte;
257
258 if (_format != format) {
259 if (!SetupBuffers( _format, 16, 16))
260 return false;
261 flags|=1; //Force a keyframe
262 }
263 /* replace oldframe with new frame */
264 unsigned char *copyFrame = newframe;
265 newframe = oldframe;
266 oldframe = copyFrame;
267
268 compress.linesDone = 0;
269 compress.writeSize = writeSize;
270 compress.writeDone = 1;
271 compress.writeBuf = (unsigned char *)writeBuf;
272 /* Set a pointer to the first byte which will contain info about this frame */
273 firstByte = compress.writeBuf;
274 *firstByte = 0;
275 //Reset the work buffer
276 workUsed = 0;workPos = 0;
277 if (flags & 1) {
278 /* Make a keyframe */
279 *firstByte |= Mask_KeyFrame;
280 KeyframeHeader * header = (KeyframeHeader *)(compress.writeBuf + compress.writeDone);
281 header->high_version = DBZV_VERSION_HIGH;
282 header->low_version = DBZV_VERSION_LOW;
283 header->compression = COMPRESSION_ZLIB;
284 header->format = format;
285 header->blockwidth = 16;
286 header->blockheight = 16;
287 compress.writeDone += sizeof(KeyframeHeader);
288 /* Copy the new frame directly over */
289 if (palsize) {
290 if (pal)
291 memcpy(&palette, pal, sizeof(palette));
292 else
293 memset(&palette,0, sizeof(palette));
294 /* keyframes get the full palette */
295 for (i=0;i<palsize;i++) {
296 work[workUsed++] = palette[i*4+0];
297 work[workUsed++] = palette[i*4+1];
298 work[workUsed++] = palette[i*4+2];
299 }
300 }
301 /* Restart deflate */
302 deflateReset(&zstream);
303 } else {
304 if (palsize && pal && memcmp(pal, palette, palsize * 4)) {
305 *firstByte |= Mask_DeltaPalette;
306 for(i=0;i<palsize;i++) {
307 work[workUsed++]=palette[i*4+0] ^ pal[i*4+0];
308 work[workUsed++]=palette[i*4+1] ^ pal[i*4+1];
309 work[workUsed++]=palette[i*4+2] ^ pal[i*4+2];
310 }
311 memcpy(&palette,pal, palsize * 4);
312 }
313 }
314 return true;
315 }
316
317 void VideoCodec::CompressLines(int lineCount, void *lineData[]) {
318 int linePitch = pitch * pixelsize;
319 int lineWidth = width * pixelsize;
320 int i = 0;
321 unsigned char *destStart = newframe + pixelsize*(MAX_VECTOR+(compress.linesDone+MAX_VECTOR)*pitch);
322 while ( i < lineCount && (compress.linesDone < height)) {
323 memcpy(destStart, lineData[i], lineWidth );
324 destStart += linePitch;
325 i++;compress.linesDone++;
326 }
327 }
328
329 int VideoCodec::FinishCompressFrame( void ) {
330 unsigned char firstByte = *compress.writeBuf;
331 if (firstByte & Mask_KeyFrame) {
332 int i;
333 /* Add the full frame data */
334 unsigned char * readFrame = newframe + pixelsize*(MAX_VECTOR+MAX_VECTOR*pitch);
335 for (i=0;i<height;i++) {
336 memcpy(&work[workUsed], readFrame, width*pixelsize);
337 readFrame += pitch*pixelsize;
338 workUsed += width*pixelsize;
339 }
340 } else {
341 /* Add the delta frame data */
342 switch (format) {
343 case ZMBV_FORMAT_8BPP:
344 AddXorFrame<char>();
345 break;
346 case ZMBV_FORMAT_15BPP:
347 case ZMBV_FORMAT_16BPP:
348 AddXorFrame<short>();
349 break;
350 case ZMBV_FORMAT_32BPP:
351 AddXorFrame<long>();
352 break;
353 }
354 }
355 /* Create the actual frame with compression */
356 zstream.next_in = (Bytef *)work;
357 zstream.avail_in = workUsed;
358 zstream.total_in = 0;
359
360 zstream.next_out = (Bytef *)(compress.writeBuf + compress.writeDone);
361 zstream.avail_out = compress.writeSize - compress.writeDone;
362 zstream.total_out = 0;
363 int res = deflate(&zstream, Z_SYNC_FLUSH);
364 return compress.writeDone + zstream.total_out;
365 }
366
367 template<class P>
368 INLINE void VideoCodec::UnXorBlock(int vx,int vy,FrameBlock * block) {
369 P * pold=((P*)oldframe)+block->start+(vy*pitch)+vx;
370 P * pnew=((P*)newframe)+block->start;
371 for (int y=0;y<block->dy;y++) {
372 for (int x=0;x<block->dx;x++) {
373 pnew[x]=pold[x]^*((P*)&work[workPos]);
374 workPos+=sizeof(P);
375 }
376 pold+=pitch;
377 pnew+=pitch;
378 }
379 }
380
381 template<class P>
382 INLINE void VideoCodec::CopyBlock(int vx,int vy,FrameBlock * block) {
383 P * pold=((P*)oldframe)+block->start+(vy*pitch)+vx;
384 P * pnew=((P*)newframe)+block->start;
385 for (int y=0;y<block->dy;y++) {
386 for (int x=0;x<block->dx;x++) {
387 pnew[x]=pold[x];
388 }
389 pold+=pitch;
390 pnew+=pitch;
391 }
392 }
393
394 template<class P>
395 void VideoCodec::UnXorFrame(void) {
396 signed char * vectors=(signed char *)&work[workPos];
397 workPos=(workPos + blockcount*2 + 3) & ~3;
398 for (int b=0;b<blockcount;b++) {
399 FrameBlock * block=&blocks[b];
400 int delta = vectors[b*2+0] & 1;
401 int vx = vectors[b*2+0] >> 1;
402 int vy = vectors[b*2+1] >> 1;
403 if (delta) UnXorBlock<P>(vx,vy,block);
404 else CopyBlock<P>(vx,vy,block);
405 }
406 }
407
408 bool VideoCodec::DecompressFrame(void * framedata, int size) {
409 unsigned char *data=(unsigned char *)framedata;
410 unsigned char tag;int i;
411
412 tag = *data++;
413 if (--size<=0)
414 return false;
415 if (tag & Mask_KeyFrame) {
416 KeyframeHeader * header = (KeyframeHeader *)data;
417 size -= sizeof(KeyframeHeader);data += sizeof(KeyframeHeader);
418 if (size<=0)
419 return false;
420 if (header->low_version != DBZV_VERSION_LOW || header->high_version != DBZV_VERSION_HIGH)
421 return false;
422 if (format != (zmbv_format_t)header->format && !SetupBuffers((zmbv_format_t)header->format, header->blockwidth, header->blockheight))
423 return false;
424 inflateReset(&zstream);
425 }
426 zstream.next_in = (Bytef *)data;
427 zstream.avail_in = size;
428 zstream.total_in = 0;
429
430 zstream.next_out = (Bytef *)work;
431 zstream.avail_out = bufsize;
432 zstream.total_out = 0;
433 int res = inflate(&zstream, Z_FINISH);
434 workUsed= zstream.total_out;
435 workPos = 0;
436 if (tag & Mask_KeyFrame) {
437 if (palsize) {
438 for (i=0;i<palsize;i++) {
439 palette[i*4+0] = work[workPos++];
440 palette[i*4+1] = work[workPos++];
441 palette[i*4+2] = work[workPos++];
442 }
443 }
444 newframe = buf1;
445 oldframe = buf2;
446 unsigned char * writeframe = newframe + pixelsize*(MAX_VECTOR+MAX_VECTOR*pitch);
447 for (i=0;i<height;i++) {
448 memcpy(writeframe,&work[workPos],width*pixelsize);
449 writeframe+=pitch*pixelsize;
450 workPos+=width*pixelsize;
451 }
452 } else {
453 data = oldframe;
454 oldframe = newframe;
455 newframe = data;
456 if (tag & Mask_DeltaPalette) {
457 for (i=0;i<palsize;i++) {
458 palette[i*4+0] ^= work[workPos++];
459 palette[i*4+1] ^= work[workPos++];
460 palette[i*4+2] ^= work[workPos++];
461 }
462 }
463 switch (format) {
464 case ZMBV_FORMAT_8BPP:
465 UnXorFrame<char>();
466 break;
467 case ZMBV_FORMAT_15BPP:
468 case ZMBV_FORMAT_16BPP:
469 UnXorFrame<short>();
470 break;
471 case ZMBV_FORMAT_32BPP:
472 UnXorFrame<long>();
473 break;
474 }
475 }
476 return true;
477 }
478
479 void VideoCodec::Output_UpsideDown_24(void *output) {
480 int i;
481 unsigned char *r;
482 unsigned char *w = (unsigned char *)output;
483 int pad = width & 3;
484
485 for (i=height-1;i>=0;i--) {
486 r = newframe + pixelsize*(MAX_VECTOR+(i+MAX_VECTOR)*pitch);
487 switch (format) {
488 case ZMBV_FORMAT_8BPP:
489 for (int j=0;j<width;j++) {
490 int c=r[j];
491 *w++=palette[c*4+2];
492 *w++=palette[c*4+1];
493 *w++=palette[c*4+0];
494 }
495 break;
496 case ZMBV_FORMAT_15BPP:
497 for (int j=0;j<width;j++) {
498 unsigned short c = *(unsigned short *)&r[j*2];
499 *w++ = (unsigned char)(((c & 0x001f) * 0x21) >> 2);
500 *w++ = (unsigned char)(((c & 0x03e0) * 0x21) >> 7);
501 *w++ = (unsigned char)(((c & 0x7c00) * 0x21) >> 12);
502 }
503 break;
504 case ZMBV_FORMAT_16BPP:
505 for (int j=0;j<width;j++) {
506 unsigned short c = *(unsigned short *)&r[j*2];
507 *w++ = (unsigned char)(((c & 0x001f) * 0x21) >> 2);
508 *w++ = (unsigned char)(((c & 0x07e0) * 0x41) >> 9);
509 *w++ = (unsigned char)(((c & 0xf800) * 0x21) >> 13);
510 }
511 break;
512 case ZMBV_FORMAT_32BPP:
513 for (int j=0;j<width;j++) {
514 *w++ = r[j*4+0];
515 *w++ = r[j*4+1];
516 *w++ = r[j*4+2];
517 }
518 break;
519 }
520
521 // Maintain 32-bit alignment for scanlines.
522 w += pad;
523 }
524 }
525
526 void VideoCodec::FreeBuffers(void) {
527 if (blocks) {
528 delete[] blocks;blocks=0;
529 }
530 if (buf1) {
531 delete[] buf1;buf1=0;
532 }
533 if (buf2) {
534 delete[] buf2;buf2=0;
535 }
536 if (work) {
537 delete[] work;work=0;
538 }
539 }
540
541
542 VideoCodec::VideoCodec() {
543 CreateVectorTable();
544 blocks = 0;
545 buf1 = 0;
546 buf2 = 0;
547 work = 0;
548 memset( &zstream, 0, sizeof(zstream));
549 }