4 extern int g_width, g_height;
5 extern void *g_vBackBuf1, *g_vBackBuf2;
7 /* 8 bit decoding routines */
9 static void dispatchDecoder(unsigned char **pFrame, unsigned char codeType, unsigned char **pData, int *pDataRemain, int *curXb, int *curYb);
11 void decodeFrame8(unsigned char *pFrame, unsigned char *pMap, int mapRemain, unsigned char *pData, int dataRemain)
20 for (i=0; i<xb/2; i++)
22 dispatchDecoder(&pFrame, (*pMap) & 0xf, &pData, &dataRemain, &i, &j);
23 if (pFrame < (unsigned char *)g_vBackBuf1)
24 fprintf(stderr, "danger! pointing out of bounds below after dispatch decoder: %d, %d (1) [%x]\n", i, j, (*pMap) & 0xf);
25 else if (pFrame >= ((unsigned char *)g_vBackBuf1) + g_width*g_height)
26 fprintf(stderr, "danger! pointing out of bounds above after dispatch decoder: %d, %d (1) [%x]\n", i, j, (*pMap) & 0xf);
27 dispatchDecoder(&pFrame, (*pMap) >> 4, &pData, &dataRemain, &i, &j);
28 if (pFrame < (unsigned char *)g_vBackBuf1)
29 fprintf(stderr, "danger! pointing out of bounds below after dispatch decoder: %d, %d (2) [%x]\n", i, j, (*pMap) >> 4);
30 else if (pFrame >= ((unsigned char *)g_vBackBuf1) + g_width*g_height)
31 fprintf(stderr, "danger! pointing out of bounds above after dispatch decoder: %d, %d (2) [%x]\n", i, j, (*pMap) >> 4);
41 static void relClose(int i, int *x, int *y)
52 static void relFar(int i, int sign, int *x, int *y)
56 *x = sign * (8 + (i % 7));
61 *x = sign * (-14 + (i - 56) % 29);
62 *y = sign * (8 + (i - 56) / 29);
66 /* copies an 8x8 block from pSrc to pDest.
67 pDest and pSrc are both g_width bytes wide */
68 static void copyFrame(unsigned char *pDest, unsigned char *pSrc)
74 memcpy(pDest, pSrc, 8);
80 // Fill in the next eight bytes with p[0], p[1], p[2], or p[3],
81 // depending on the corresponding two-bit value in pat0 and pat1
82 static void patternRow4Pixels(unsigned char *pFrame,
83 unsigned char pat0, unsigned char pat1,
86 unsigned short mask=0x0003;
87 unsigned short shift=0;
88 unsigned short pattern = (pat1 << 8) | pat0;
92 *pFrame++ = p[(mask & pattern) >> shift];
98 // Fill in the next four 2x2 pixel blocks with p[0], p[1], p[2], or p[3],
99 // depending on the corresponding two-bit value in pat0.
100 static void patternRow4Pixels2(unsigned char *pFrame,
104 unsigned char mask=0x03;
105 unsigned char shift=0;
110 pel = p[(mask & pat0) >> shift];
113 pFrame[g_width + 0] = pel;
114 pFrame[g_width + 1] = pel;
121 // Fill in the next four 2x1 pixel blocks with p[0], p[1], p[2], or p[3],
122 // depending on the corresponding two-bit value in pat.
123 static void patternRow4Pixels2x1(unsigned char *pFrame, unsigned char pat, unsigned char *p)
125 unsigned char mask=0x03;
126 unsigned char shift=0;
131 pel = p[(mask & pat) >> shift];
140 // Fill in the next 4x4 pixel block with p[0], p[1], p[2], or p[3],
141 // depending on the corresponding two-bit value in pat0, pat1, pat2, and pat3.
142 static void patternQuadrant4Pixels(unsigned char *pFrame, unsigned char pat0, unsigned char pat1, unsigned char pat2, unsigned char pat3, unsigned char *p)
144 unsigned long mask = 0x00000003UL;
147 unsigned long pat = (pat3 << 24) | (pat2 << 16) | (pat1 << 8) | pat0;
151 pFrame[i&3] = p[(pat & mask) >> shift];
161 // fills the next 8 pixels with either p[0] or p[1], depending on pattern
162 static void patternRow2Pixels(unsigned char *pFrame, unsigned char pat, unsigned char *p)
164 unsigned char mask=0x01;
168 *pFrame++ = p[(mask & pat) ? 1 : 0];
173 // fills the next four 2 x 2 pixel boxes with either p[0] or p[1], depending on pattern
174 static void patternRow2Pixels2(unsigned char *pFrame, unsigned char pat, unsigned char *p)
177 unsigned char mask=0x1;
181 pel = p[(mask & pat) ? 1 : 0];
183 pFrame[0] = pel; // upper-left
184 pFrame[1] = pel; // upper-right
185 pFrame[g_width + 0] = pel; // lower-left
186 pFrame[g_width + 1] = pel; // lower-right
193 // fills pixels in the next 4 x 4 pixel boxes with either p[0] or p[1], depending on pat0 and pat1.
194 static void patternQuadrant2Pixels(unsigned char *pFrame, unsigned char pat0, unsigned char pat1, unsigned char *p)
197 unsigned short mask = 0x0001;
199 unsigned short pat = (pat1 << 8) | pat0;
205 pel = p[(pat & mask) ? 1 : 0];
207 pFrame[j + i * g_width] = pel;
214 static void dispatchDecoder(unsigned char **pFrame, unsigned char codeType, unsigned char **pData, int *pDataRemain, int *curXb, int *curYb)
217 unsigned char pat[16];
221 /* Data is processed in 8x8 pixel blocks.
222 There are 16 ways to encode each block.
228 /* block is copied from block in current frame */
229 copyFrame(*pFrame, *pFrame + (g_vBackBuf2 - g_vBackBuf1));
231 /* block is unchanged from two frames ago */
236 /* Block is copied from nearby (below and/or to the right) within the
237 new frame. The offset within the buffer from which to grab the
238 patch of 8 pixels is given by grabbing a byte B from the data
239 stream, which is broken into a positive x and y offset according
240 to the following mapping:
246 x = -14 + ((B - 56) % 29)
247 y = 8 + ((B - 56) / 29)
249 relFar(*(*pData)++, 1, &x, &y);
250 copyFrame(*pFrame, *pFrame + x + y*g_width);
256 /* Block is copied from nearby (above and/or to the left) within the
263 x = -(-14 + ((B - 56) % 29))
264 y = -( 8 + ((B - 56) / 29))
266 relFar(*(*pData)++, -1, &x, &y);
267 copyFrame(*pFrame, *pFrame + x + y*g_width);
273 /* Similar to 0x2 and 0x3, except this method copies from the
274 "current" frame, rather than the "new" frame, and instead of the
275 lopsided mapping they use, this one uses one which is symmetric
276 and centered around the top-left corner of the block. This uses
277 only 1 byte still, though, so the range is decreased, since we
278 have to encode all directions in a single byte. The byte we pull
279 from the data stream, I'll call B. Call the highest 4 bits of B
280 BH and the lowest 4 bytes BL. Then the offset from which to copy
286 relClose(*(*pData)++, &x, &y);
287 copyFrame(*pFrame, *pFrame + (g_vBackBuf2 - g_vBackBuf1) + x + y*g_width);
293 /* Similar to 0x4, but instead of one byte for the offset, this uses
294 two bytes to encode a larger range, the first being the x offset
295 as a signed 8-bit value, and the second being the y offset as a
298 x = (signed char)*(*pData)++;
299 y = (signed char)*(*pData)++;
300 copyFrame(*pFrame, *pFrame + (g_vBackBuf2 - g_vBackBuf1) + x + y*g_width);
306 /* I can't figure out how any file containing a block of this type
307 could still be playable, since it appears that it would leave the
308 internal bookkeeping in an inconsistent state in the BG player
309 code. Ahh, well. Perhaps it was a bug in the BG player code that
310 just didn't happen to be exposed by any of the included movies.
311 Anyway, this skips the next two blocks, doing nothing to them.
312 Note that if you've reached the end of a row, this means going on
318 if (++*curXb == (g_width >> 3))
320 *pFrame += 7*g_width;
322 if (++*curYb == (g_height >> 3))
329 /* Ok, here's where it starts to get really...interesting. This is,
330 incidentally, the part where they started using self-modifying
331 code. So, most of the following encodings are "patterned" blocks,
332 where we are given a number of pixel values and then bitmapped
333 values to specify which pixel values belong to which squares. For
334 this encoding, we are given the following in the data stream:
338 These are pixel values (i.e. 8-bit indices into the palette). If
339 P0 <= P1, we then get 8 more bytes from the data stream, one for
340 each row in the block:
342 B0 B1 B2 B3 B4 B5 B6 B7
344 For each row, the leftmost pixel is represented by the low-order
345 bit, and the rightmost by the high-order bit. Use your imagination
346 in between. If a bit is set, the pixel value is P1 and if it is
347 unset, the pixel value is P0.
349 So, for example, if we had:
351 11 22 fe 83 83 83 83 83 83 fe
353 This would represent the following layout:
355 11 22 22 22 22 22 22 22 ; fe == 11111110
356 22 22 11 11 11 11 11 22 ; 83 == 10000011
357 22 22 11 11 11 11 11 22 ; 83 == 10000011
358 22 22 11 11 11 11 11 22 ; 83 == 10000011
359 22 22 11 11 11 11 11 22 ; 83 == 10000011
360 22 22 11 11 11 11 11 22 ; 83 == 10000011
361 22 22 11 11 11 11 11 22 ; 83 == 10000011
362 11 22 22 22 22 22 22 22 ; fe == 11111110
364 If, on the other hand, P0 > P1, we get two more bytes from the
369 Each of these bytes contains two 4-bit patterns. These patterns
370 work like the patterns above with 8 bytes, except each bit
371 represents a 2x2 pixel region.
373 B0 contains the pattern for the top two rows and B1 contains
374 the pattern for the bottom two rows. Note that the low-order
375 nibble of each byte contains the pattern for the upper of the
376 two rows that that byte controls.
384 11 11 22 22 22 22 22 22 ; e == 1 1 1 0
385 11 11 22 22 22 22 22 22 ;
386 22 22 22 22 22 22 11 11 ; 7 == 0 1 1 1
387 22 22 22 22 22 22 11 11 ;
388 11 11 11 11 11 11 22 22 ; 3 == 1 0 0 0
389 11 11 11 11 11 11 22 22 ;
390 22 22 22 22 11 11 11 11 ; 8 == 0 0 1 1
391 22 22 22 22 11 11 11 11 ;
399 patternRow2Pixels(*pFrame, *(*pData)++, p);
407 patternRow2Pixels2(*pFrame, *(*pData) & 0xf, p);
408 *pFrame += 2*g_width;
409 patternRow2Pixels2(*pFrame, *(*pData)++ >> 4, p);
410 *pFrame += 2*g_width;
413 *pFrame -= (8*g_width - 8);
417 /* Ok, this one is basically like encoding 0x7, only more
418 complicated. Again, we start out by getting two bytes on the data
423 if P0 <= P1 then we get the following from the data stream:
430 P0 P1 and B0 B1 are used for the top-left corner, P2 P3 B2 B3 for
431 the bottom-left corner, P4 P5 B4 B5 for the top-right, P6 P7 B6 B7
432 for the bottom-right. (So, each codes for a 4x4 pixel array.)
433 Since we have 16 bits in B0 B1, there is one bit for each pixel in
434 the array. The convention for the bit-mapping is, again, left to
435 right and top to bottom.
437 So, basically, the top-left quarter of the block is an arbitrary
438 pattern with 2 pixels, the bottom-left a different arbitrary
439 pattern with 2 different pixels, and so on.
441 For example if the next 16 bytes were:
443 00 22 f9 9f 44 55 aa 55 11 33 cc 33 66 77 01 ef
447 22 22 22 22 | 11 11 33 33 ; f = 1111, c = 1100
448 22 00 00 22 | 11 11 33 33 ; 9 = 1001, c = 1100
449 22 00 00 22 | 33 33 11 11 ; 9 = 1001, 3 = 0011
450 22 22 22 22 | 33 33 11 11 ; f = 1111, 3 = 0011
451 ------------+------------
452 44 55 44 55 | 66 66 66 66 ; a = 1010, 0 = 0000
453 44 55 44 55 | 77 66 66 66 ; a = 1010, 1 = 0001
454 55 44 55 44 | 66 77 77 77 ; 5 = 0101, e = 1110
455 55 44 55 44 | 77 77 77 77 ; 5 = 0101, f = 1111
457 I've added a dividing line in the above to clearly delineate the
461 Now, if P0 > P1 then we get 10 more bytes from the data stream:
463 B0 B1 B2 B3 P2 P3 B4 B5 B6 B7
465 Now, if P2 <= P3, then the first six bytes [P0 P1 B0 B1 B2 B3]
466 represent the left half of the block and the latter six bytes
467 [P2 P3 B4 B5 B6 B7] represent the right half.
471 22 00 01 37 f7 31 11 66 8c e6 73 31
475 22 22 22 22 | 11 11 11 66 ; 0: 0000 | 8: 1000
476 00 22 22 22 | 11 11 66 66 ; 1: 0001 | C: 1100
477 00 00 22 22 | 11 66 66 66 ; 3: 0011 | e: 1110
478 00 00 00 22 | 11 66 11 66 ; 7: 0111 | 6: 0101
479 00 00 00 00 | 66 66 66 11 ; f: 1111 | 7: 0111
480 00 00 00 22 | 66 66 11 11 ; 7: 0111 | 3: 0011
481 00 00 22 22 | 66 66 11 11 ; 3: 0011 | 3: 0011
482 00 22 22 22 | 66 11 11 11 ; 1: 0001 | 1: 0001
485 On the other hand, if P0 > P1 and P2 > P3, then
486 [P0 P1 B0 B1 B2 B3] represent the top half of the
487 block and [P2 P3 B4 B5 B6 B7] represent the bottom half.
491 22 00 cc 66 33 19 66 11 18 24 42 81
495 22 22 00 00 22 22 00 00 ; cc: 11001100
496 22 00 00 22 22 00 00 22 ; 66: 01100110
497 00 00 22 22 00 00 22 22 ; 33: 00110011
498 00 22 22 00 00 22 22 22 ; 19: 00011001
499 -----------------------
500 66 66 66 11 11 66 66 66 ; 18: 00011000
501 66 66 11 66 66 11 66 66 ; 24: 00100100
502 66 11 66 66 66 66 11 66 ; 42: 01000010
503 11 66 66 66 66 66 66 11 ; 81: 10000001
505 if ( (*pData)[0] <= (*pData)[1])
507 // four quadrant case
512 pat[0] = *(*pData)++;
513 pat[1] = *(*pData)++;
514 patternQuadrant2Pixels(*pFrame, pat[0], pat[1], p);
516 // alternate between moving down and moving up and right
518 *pFrame += 4 - 4*g_width; // up and right
520 *pFrame += 4*g_width; // down
523 else if ( (*pData)[6] <= (*pData)[7])
533 pat[0] = *(*pData)++;
534 pat[1] = *(*pData)++;
535 patternQuadrant2Pixels(*pFrame, pat[0], pat[1], p);
538 *pFrame -= (4*g_width - 4);
540 *pFrame += 4*g_width;
553 patternRow2Pixels(*pFrame, *(*pData)++, p);
556 *pFrame -= (8*g_width - 8);
561 /* Similar to the previous 2 encodings, only more complicated. And
562 it will get worse before it gets better. No longer are we dealing
563 with patterns over two pixel values. Now we are dealing with
564 patterns over 4 pixel values with 2 bits assigned to each pixel
565 (or block of pixels).
567 So, first on the data stream are our 4 pixel values:
571 Now, if P0 <= P1 AND P2 <= P3, we get 16 bytes of pattern, each
572 2 bits representing a 1x1 pixel (00=P0, 01=P1, 10=P2, 11=P3). The
573 ordering is again left to right and top to bottom. The most
574 significant bits represent the left side at the top, and so on.
576 If P0 <= P1 AND P2 > P3, we get 4 bytes of pattern, each 2 bits
577 representing a 2x2 pixel. Ordering is left to right and top to
580 if P0 > P1 AND P2 <= P3, we get 8 bytes of pattern, each 2 bits
581 representing a 2x1 pixel (i.e. 2 pixels wide, and 1 high).
583 if P0 > P1 AND P2 > P3, we get 8 bytes of pattern, each 2 bits
584 representing a 1x2 pixel (i.e. 1 pixel wide, and 2 high).
586 if ( (*pData)[0] <= (*pData)[1])
588 if ( (*pData)[2] <= (*pData)[3])
597 pat[0] = *(*pData)++;
598 pat[1] = *(*pData)++;
599 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
603 *pFrame -= (8*g_width - 8);
612 patternRow4Pixels2(*pFrame, *(*pData)++, p);
613 *pFrame += 2*g_width;
614 patternRow4Pixels2(*pFrame, *(*pData)++, p);
615 *pFrame += 2*g_width;
616 patternRow4Pixels2(*pFrame, *(*pData)++, p);
617 *pFrame += 2*g_width;
618 patternRow4Pixels2(*pFrame, *(*pData)++, p);
619 *pFrame -= (6*g_width - 8);
624 if ( (*pData)[2] <= (*pData)[3])
634 pat[0] = *(*pData)++;
635 patternRow4Pixels2x1(*pFrame, pat[0], p);
639 *pFrame -= (8*g_width - 8);
651 pat[0] = *(*pData)++;
652 pat[1] = *(*pData)++;
653 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
655 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
659 *pFrame -= (8*g_width - 8);
665 /* Similar to the previous, only a little more complicated.
667 We are still dealing with patterns over 4 pixel values with 2 bits
668 assigned to each pixel (or block of pixels).
670 So, first on the data stream are our 4 pixel values:
674 Now, if P0 <= P1, the block is divided into 4 quadrants, ordered
675 (as with opcode 0x8) TL, BL, TR, BR. In this case the next data
676 in the data stream should be:
679 P4 P5 P6 P7 B4 B5 B6 B7
680 P8 P9 P10 P11 B8 B9 B10 B11
681 P12 P13 P14 P15 B12 B13 B14 B15
683 Each 2 bits represent a 1x1 pixel (00=P0, 01=P1, 10=P2, 11=P3).
684 The ordering is again left to right and top to bottom. The most
685 significant bits represent the right side at the top, and so on.
687 If P0 > P1 then the next data on the data stream is:
689 B0 B1 B2 B3 B4 B5 B6 B7
690 P4 P5 P6 P7 B8 B9 B10 B11 B12 B13 B14 B15
692 Now, in this case, if P4 <= P5,
693 [P0 P1 P2 P3 B0 B1 B2 B3 B4 B5 B6 B7] represent the left half of
694 the block and the other bytes represent the right half. If P4 >
695 P5, then [P0 P1 P2 P3 B0 B1 B2 B3 B4 B5 B6 B7] represent the top
696 half of the block and the other bytes represent the bottom half.
698 if ( (*pData)[0] <= (*pData)[1])
706 pat[0] = *(*pData)++;
707 pat[1] = *(*pData)++;
708 pat[2] = *(*pData)++;
709 pat[3] = *(*pData)++;
711 patternQuadrant4Pixels(*pFrame, pat[0], pat[1], pat[2], pat[3], p);
714 *pFrame -= (4*g_width - 4);
716 *pFrame += 4*g_width;
721 if ( (*pData)[12] <= (*pData)[13])
734 pat[0] = *(*pData)++;
735 pat[1] = *(*pData)++;
736 pat[2] = *(*pData)++;
737 pat[3] = *(*pData)++;
739 patternQuadrant4Pixels(*pFrame, pat[0], pat[1], pat[2], pat[3], p);
742 *pFrame -= (4*g_width - 4);
744 *pFrame += 4*g_width;
760 pat[0] = *(*pData)++;
761 pat[1] = *(*pData)++;
762 patternRow4Pixels(*pFrame, pat[0], pat[1], p);
766 *pFrame -= (8*g_width - 8);
772 /* In this encoding we get raw pixel data in the data stream -- 64
773 bytes of pixel data. 1 byte for each pixel, and in the standard
778 memcpy(*pFrame, *pData, 8);
783 *pFrame -= (8*g_width - 8);
787 /* In this encoding we get raw pixel data in the data stream -- 16
788 bytes of pixel data. 1 byte for each block of 2x2 pixels, and in
789 the standard order (l->r, t->b).
797 (*pFrame)[2*k] = (*pData)[k];
798 (*pFrame)[2*k+1] = (*pData)[k];
805 *pFrame -= (8*g_width - 8);
809 /* In this encoding we get raw pixel data in the data stream -- 4
810 bytes of pixel data. 1 byte for each block of 4x4 pixels, and in
811 the standard order (l->r, t->b).
819 (*pFrame)[k*g_width+j] = (*pData)[0];
820 (*pFrame)[k*g_width+j+4] = (*pData)[1];
823 *pFrame += 4*g_width;
827 *pFrame -= (8*g_width - 8);
831 /* This encoding represents a solid 8x8 frame. We get 1 byte of pixel
832 data from the data stream.
836 memset(*pFrame, **pData, 8);
841 *pFrame -= (8*g_width - 8);
845 /* This encoding represents a "dithered" frame, which is
846 checkerboarded with alternate pixels of two colors. We get 2
847 bytes of pixel data from the data stream, and these bytes are
850 P0 P1 P0 P1 P0 P1 P0 P1
851 P1 P0 P1 P0 P1 P0 P1 P0
853 P0 P1 P0 P1 P0 P1 P0 P1
854 P1 P0 P1 P0 P1 P0 P1 P0
860 (*pFrame)[j] = (*pData)[(i+j)&1];
866 *pFrame -= (8*g_width - 8);