2 * Interplay MVE Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * @file interplayvideo.c
23 * Interplay MVE Video Decoder by Mike Melanson (melanson@pcisys.net)
24 * For more information about the Interplay MVE format, visit:
25 * http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
26 * This code is written in such a way that the identifiers match up
27 * with the encoding descriptions in the document.
29 * This decoder presently only supports a PAL8 output colorspace.
31 * An Interplay video frame consists of 2 parts: The decoding map and
32 * the video data. A demuxer must load these 2 parts together in a single
33 * buffer before sending it through the stream to this decoder.
45 #define PALETTE_COUNT 256
47 /* debugging support */
48 #define DEBUG_INTERPLAY 0
50 #define debug_interplay printf
52 static inline void debug_interplay(const char *format
, ...) { }
55 typedef struct IpvideoContext
{
57 AVCodecContext
*avctx
;
59 AVFrame second_last_frame
;
61 AVFrame current_frame
;
62 unsigned char *decoding_map
;
63 int decoding_map_size
;
68 unsigned char palette
[PALETTE_COUNT
* 4];
70 unsigned char *stream_ptr
;
71 unsigned char *stream_end
;
72 unsigned char *pixel_ptr
;
75 int upper_motion_limit_offset
;
79 #define CHECK_STREAM_PTR(n) \
80 if ((s->stream_ptr + n) > s->stream_end) { \
81 printf ("Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
82 s->stream_ptr + n, s->stream_end); \
86 static void ipvideo_new_palette(IpvideoContext
*s
, unsigned char *palette
) {
89 unsigned char r
, g
, b
;
90 unsigned int *palette32
;
92 palette32
= (unsigned int *)s
->palette
;
93 for (i
= 0; i
< PALETTE_COUNT
; i
++) {
97 palette32
[i
] = (r
<< 16) | (g
<< 8) | (b
);
101 #define COPY_FROM_CURRENT() \
102 motion_offset = current_offset; \
103 motion_offset += y * s->stride; \
104 motion_offset += x; \
105 if (motion_offset < 0) { \
106 printf (" Interplay video: motion offset < 0 (%d)\n", motion_offset); \
108 } else if (motion_offset > s->upper_motion_limit_offset) { \
109 printf (" Interplay video: motion offset above limit (%d >= %d)\n", \
110 motion_offset, s->upper_motion_limit_offset); \
113 s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
114 s->current_frame.data[0] + motion_offset, s->stride, 8);
116 #define COPY_FROM_PREVIOUS() \
117 motion_offset = current_offset; \
118 motion_offset += y * s->stride; \
119 motion_offset += x; \
120 if (motion_offset < 0) { \
121 printf (" Interplay video: motion offset < 0 (%d)\n", motion_offset); \
123 } else if (motion_offset > s->upper_motion_limit_offset) { \
124 printf (" Interplay video: motion offset above limit (%d >= %d)\n", \
125 motion_offset, s->upper_motion_limit_offset); \
128 s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
129 s->last_frame.data[0] + motion_offset, s->stride, 8);
131 #define COPY_FROM_SECOND_LAST() \
132 motion_offset = current_offset; \
133 motion_offset += y * s->stride; \
134 motion_offset += x; \
135 if (motion_offset < 0) { \
136 printf (" Interplay video: motion offset < 0 (%d)\n", motion_offset); \
138 } else if (motion_offset > s->upper_motion_limit_offset) { \
139 printf (" Interplay video: motion offset above limit (%d >= %d)\n", \
140 motion_offset, s->upper_motion_limit_offset); \
143 s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
144 s->second_last_frame.data[0] + motion_offset, s->stride, 8);
146 static int ipvideo_decode_block_opcode_0x0(IpvideoContext
*s
)
150 int current_offset
= s
->pixel_ptr
- s
->current_frame
.data
[0];
152 /* copy a block from the previous frame */
154 COPY_FROM_PREVIOUS();
160 static int ipvideo_decode_block_opcode_0x1(IpvideoContext
*s
)
164 int current_offset
= s
->pixel_ptr
- s
->current_frame
.data
[0];
166 /* copy block from 2 frames ago */
168 COPY_FROM_SECOND_LAST();
174 static int ipvideo_decode_block_opcode_0x2(IpvideoContext
*s
)
179 int current_offset
= s
->pixel_ptr
- s
->current_frame
.data
[0];
181 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
183 B
= *s
->stream_ptr
++;
189 x
= -14 + ((B
- 56) % 29);
190 y
= 8 + ((B
- 56) / 29);
193 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B
, x
, y
);
194 COPY_FROM_SECOND_LAST();
200 static int ipvideo_decode_block_opcode_0x3(IpvideoContext
*s
)
205 int current_offset
= s
->pixel_ptr
- s
->current_frame
.data
[0];
207 /* copy 8x8 block from current frame from an up/left block */
209 /* need 1 more byte for motion */
211 B
= *s
->stream_ptr
++;
217 x
= -(-14 + ((B
- 56) % 29));
218 y
= -( 8 + ((B
- 56) / 29));
221 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B
, x
, y
);
228 static int ipvideo_decode_block_opcode_0x4(IpvideoContext
*s
)
231 unsigned char B
, BL
, BH
;
233 int current_offset
= s
->pixel_ptr
- s
->current_frame
.data
[0];
235 /* copy a block from the previous frame; need 1 more byte */
238 B
= *s
->stream_ptr
++;
240 BH
= (B
>> 4) & 0x0F;
244 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B
, x
, y
);
245 COPY_FROM_PREVIOUS();
251 static int ipvideo_decode_block_opcode_0x5(IpvideoContext
*s
)
255 int current_offset
= s
->pixel_ptr
- s
->current_frame
.data
[0];
257 /* copy a block from the previous frame using an expanded range;
258 * need 2 more bytes */
261 x
= *s
->stream_ptr
++;
262 y
= *s
->stream_ptr
++;
264 debug_interplay (" motion bytes = %d, %d\n", x
, y
);
265 COPY_FROM_PREVIOUS();
271 static int ipvideo_decode_block_opcode_0x6(IpvideoContext
*s
)
273 /* mystery opcode? skip multiple blocks? */
274 printf (" Interplay video: Help! Mystery opcode 0x6 seen\n");
280 static int ipvideo_decode_block_opcode_0x7(IpvideoContext
*s
)
283 unsigned char P0
, P1
;
288 /* 2-color encoding */
291 P0
= *s
->stream_ptr
++;
292 P1
= *s
->stream_ptr
++;
296 /* need 8 more bytes from the stream */
298 for (y
= 0; y
< 8; y
++)
299 B
[y
] = *s
->stream_ptr
++;
301 for (y
= 0; y
< 8; y
++) {
303 for (x
= 0x01; x
<= 0x80; x
<<= 1) {
305 *s
->pixel_ptr
++ = P1
;
307 *s
->pixel_ptr
++ = P0
;
309 s
->pixel_ptr
+= s
->line_inc
;
314 /* need 2 more bytes from the stream */
316 B
[0] = *s
->stream_ptr
++;
317 B
[1] = *s
->stream_ptr
++;
319 flags
= (B
[1] << 8) | B
[0];
321 for (y
= 0; y
< 8; y
+= 2) {
322 for (x
= 0; x
< 8; x
+= 2, bitmask
<<= 1) {
323 if (flags
& bitmask
) {
324 *(s
->pixel_ptr
+ x
) = P1
;
325 *(s
->pixel_ptr
+ x
+ 1) = P1
;
326 *(s
->pixel_ptr
+ s
->stride
+ x
) = P1
;
327 *(s
->pixel_ptr
+ s
->stride
+ x
+ 1) = P1
;
329 *(s
->pixel_ptr
+ x
) = P0
;
330 *(s
->pixel_ptr
+ x
+ 1) = P0
;
331 *(s
->pixel_ptr
+ s
->stride
+ x
) = P0
;
332 *(s
->pixel_ptr
+ s
->stride
+ x
+ 1) = P0
;
335 s
->pixel_ptr
+= s
->stride
* 2;
343 static int ipvideo_decode_block_opcode_0x8(IpvideoContext
*s
)
348 unsigned int flags
= 0;
349 unsigned int bitmask
= 0;
350 unsigned char P0
= 0, P1
= 0;
353 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
354 * either top and bottom or left and right halves */
357 P
[0] = *s
->stream_ptr
++;
358 P
[1] = *s
->stream_ptr
++;
362 /* need 12 more bytes */
363 CHECK_STREAM_PTR(12);
364 B
[0] = *s
->stream_ptr
++; B
[1] = *s
->stream_ptr
++;
365 P
[2] = *s
->stream_ptr
++; P
[3] = *s
->stream_ptr
++;
366 B
[2] = *s
->stream_ptr
++; B
[3] = *s
->stream_ptr
++;
367 P
[4] = *s
->stream_ptr
++; P
[5] = *s
->stream_ptr
++;
368 B
[4] = *s
->stream_ptr
++; B
[5] = *s
->stream_ptr
++;
369 P
[6] = *s
->stream_ptr
++; P
[7] = *s
->stream_ptr
++;
370 B
[6] = *s
->stream_ptr
++; B
[7] = *s
->stream_ptr
++;
372 for (y
= 0; y
< 8; y
++) {
374 /* time to reload flags? */
377 ((B
[0] & 0xF0) << 4) | ((B
[4] & 0xF0) << 8) |
378 ((B
[0] & 0x0F) ) | ((B
[4] & 0x0F) << 4) |
379 ((B
[1] & 0xF0) << 20) | ((B
[5] & 0xF0) << 24) |
380 ((B
[1] & 0x0F) << 16) | ((B
[5] & 0x0F) << 20);
381 bitmask
= 0x00000001;
382 lower_half
= 0; /* still on top half */
385 ((B
[2] & 0xF0) << 4) | ((B
[6] & 0xF0) << 8) |
386 ((B
[2] & 0x0F) ) | ((B
[6] & 0x0F) << 4) |
387 ((B
[3] & 0xF0) << 20) | ((B
[7] & 0xF0) << 24) |
388 ((B
[3] & 0x0F) << 16) | ((B
[7] & 0x0F) << 20);
389 bitmask
= 0x00000001;
393 for (x
= 0; x
< 8; x
++, bitmask
<<= 1) {
394 /* get the pixel values ready for this quadrant */
396 P0
= P
[lower_half
+ 0];
397 P1
= P
[lower_half
+ 1];
399 P0
= P
[lower_half
+ 4];
400 P1
= P
[lower_half
+ 5];
404 *s
->pixel_ptr
++ = P1
;
406 *s
->pixel_ptr
++ = P0
;
408 s
->pixel_ptr
+= s
->line_inc
;
413 /* need 10 more bytes */
414 CHECK_STREAM_PTR(10);
415 B
[0] = *s
->stream_ptr
++; B
[1] = *s
->stream_ptr
++;
416 B
[2] = *s
->stream_ptr
++; B
[3] = *s
->stream_ptr
++;
417 P
[2] = *s
->stream_ptr
++; P
[3] = *s
->stream_ptr
++;
418 B
[4] = *s
->stream_ptr
++; B
[5] = *s
->stream_ptr
++;
419 B
[6] = *s
->stream_ptr
++; B
[7] = *s
->stream_ptr
++;
423 /* vertical split; left & right halves are 2-color encoded */
425 for (y
= 0; y
< 8; y
++) {
427 /* time to reload flags? */
430 ((B
[0] & 0xF0) << 4) | ((B
[4] & 0xF0) << 8) |
431 ((B
[0] & 0x0F) ) | ((B
[4] & 0x0F) << 4) |
432 ((B
[1] & 0xF0) << 20) | ((B
[5] & 0xF0) << 24) |
433 ((B
[1] & 0x0F) << 16) | ((B
[5] & 0x0F) << 20);
434 bitmask
= 0x00000001;
437 ((B
[2] & 0xF0) << 4) | ((B
[6] & 0xF0) << 8) |
438 ((B
[2] & 0x0F) ) | ((B
[6] & 0x0F) << 4) |
439 ((B
[3] & 0xF0) << 20) | ((B
[7] & 0xF0) << 24) |
440 ((B
[3] & 0x0F) << 16) | ((B
[7] & 0x0F) << 20);
441 bitmask
= 0x00000001;
444 for (x
= 0; x
< 8; x
++, bitmask
<<= 1) {
445 /* get the pixel values ready for this half */
455 *s
->pixel_ptr
++ = P1
;
457 *s
->pixel_ptr
++ = P0
;
459 s
->pixel_ptr
+= s
->line_inc
;
464 /* horizontal split; top & bottom halves are 2-color encoded */
466 for (y
= 0; y
< 8; y
++) {
477 for (bitmask
= 0x01; bitmask
<= 0x80; bitmask
<<= 1) {
480 *s
->pixel_ptr
++ = P1
;
482 *s
->pixel_ptr
++ = P0
;
484 s
->pixel_ptr
+= s
->line_inc
;
493 static int ipvideo_decode_block_opcode_0x9(IpvideoContext
*s
)
498 unsigned int flags
= 0;
502 /* 4-color encoding */
505 for (y
= 0; y
< 4; y
++)
506 P
[y
] = *s
->stream_ptr
++;
508 if ((P
[0] <= P
[1]) && (P
[2] <= P
[3])) {
510 /* 1 of 4 colors for each pixel, need 16 more bytes */
511 CHECK_STREAM_PTR(16);
513 for (y
= 0; y
< 8; y
++) {
514 /* get the next set of 8 2-bit flags */
515 flags
= (s
->stream_ptr
[1] << 8) | s
->stream_ptr
[0];
517 for (x
= 0, shifter
= 0; x
< 8; x
++, shifter
+= 2) {
518 *s
->pixel_ptr
++ = P
[(flags
>> shifter
) & 0x03];
520 s
->pixel_ptr
+= s
->line_inc
;
523 } else if ((P
[0] <= P
[1]) && (P
[2] > P
[3])) {
525 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
528 B
[0] = *s
->stream_ptr
++;
529 B
[1] = *s
->stream_ptr
++;
530 B
[2] = *s
->stream_ptr
++;
531 B
[3] = *s
->stream_ptr
++;
532 flags
= (B
[3] << 24) | (B
[2] << 16) | (B
[1] << 8) | B
[0];
535 for (y
= 0; y
< 8; y
+= 2) {
536 for (x
= 0; x
< 8; x
+= 2, shifter
+= 2) {
537 pix
= P
[(flags
>> shifter
) & 0x03];
538 *(s
->pixel_ptr
+ x
) = pix
;
539 *(s
->pixel_ptr
+ x
+ 1) = pix
;
540 *(s
->pixel_ptr
+ s
->stride
+ x
) = pix
;
541 *(s
->pixel_ptr
+ s
->stride
+ x
+ 1) = pix
;
543 s
->pixel_ptr
+= s
->stride
* 2;
546 } else if ((P
[0] > P
[1]) && (P
[2] <= P
[3])) {
548 /* 1 of 4 colors for each 2x1 block, need 8 more bytes */
551 for (y
= 0; y
< 8; y
++) {
552 /* time to reload flags? */
553 if ((y
== 0) || (y
== 4)) {
554 B
[0] = *s
->stream_ptr
++;
555 B
[1] = *s
->stream_ptr
++;
556 B
[2] = *s
->stream_ptr
++;
557 B
[3] = *s
->stream_ptr
++;
558 flags
= (B
[3] << 24) | (B
[2] << 16) | (B
[1] << 8) | B
[0];
561 for (x
= 0; x
< 8; x
+= 2, shifter
+= 2) {
562 pix
= P
[(flags
>> shifter
) & 0x03];
563 *(s
->pixel_ptr
+ x
) = pix
;
564 *(s
->pixel_ptr
+ x
+ 1) = pix
;
566 s
->pixel_ptr
+= s
->stride
;
571 /* 1 of 4 colors for each 1x2 block, need 8 more bytes */
574 for (y
= 0; y
< 8; y
+= 2) {
575 /* time to reload flags? */
576 if ((y
== 0) || (y
== 4)) {
577 B
[0] = *s
->stream_ptr
++;
578 B
[1] = *s
->stream_ptr
++;
579 B
[2] = *s
->stream_ptr
++;
580 B
[3] = *s
->stream_ptr
++;
581 flags
= (B
[3] << 24) | (B
[2] << 16) | (B
[1] << 8) | B
[0];
584 for (x
= 0; x
< 8; x
++, shifter
+= 2) {
585 pix
= P
[(flags
>> shifter
) & 0x03];
586 *(s
->pixel_ptr
+ x
) = pix
;
587 *(s
->pixel_ptr
+ s
->stride
+ x
) = pix
;
589 s
->pixel_ptr
+= s
->stride
* 2;
597 static int ipvideo_decode_block_opcode_0xA(IpvideoContext
*s
)
608 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
609 * either top and bottom or left and right halves */
612 for (y
= 0; y
< 4; y
++)
613 P
[y
] = *s
->stream_ptr
++;
617 /* 4-color encoding for each quadrant; need 28 more bytes */
618 CHECK_STREAM_PTR(28);
620 for (y
= 0; y
< 4; y
++)
621 B
[y
] = *s
->stream_ptr
++;
622 for (y
= 4; y
< 16; y
+= 4) {
623 for (x
= y
; x
< y
+ 4; x
++)
624 P
[x
] = *s
->stream_ptr
++;
625 for (x
= y
; x
< y
+ 4; x
++)
626 B
[x
] = *s
->stream_ptr
++;
629 for (y
= 0; y
< 8; y
++) {
631 lower_half
= (y
>= 4) ?
4 : 0;
632 flags
= (B
[y
+ 8] << 8) | B
[y
];
634 for (x
= 0, shifter
= 0; x
< 8; x
++, shifter
+= 2) {
635 split
= (x
>= 4) ?
8 : 0;
636 index
= split
+ lower_half
+ ((flags
>> shifter
) & 0x03);
637 *s
->pixel_ptr
++ = P
[index
];
640 s
->pixel_ptr
+= s
->line_inc
;
645 /* 4-color encoding for either left and right or top and bottom
646 * halves; need 20 more bytes */
647 CHECK_STREAM_PTR(20);
649 for (y
= 0; y
< 8; y
++)
650 B
[y
] = *s
->stream_ptr
++;
651 for (y
= 4; y
< 8; y
++)
652 P
[y
] = *s
->stream_ptr
++;
653 for (y
= 8; y
< 16; y
++)
654 B
[y
] = *s
->stream_ptr
++;
658 /* block is divided into left and right halves */
659 for (y
= 0; y
< 8; y
++) {
661 flags
= (B
[y
+ 8] << 8) | B
[y
];
664 for (x
= 0, shifter
= 0; x
< 8; x
++, shifter
+= 2) {
667 *s
->pixel_ptr
++ = P
[split
+ ((flags
>> shifter
) & 0x03)];
670 s
->pixel_ptr
+= s
->line_inc
;
675 /* block is divided into top and bottom halves */
677 for (y
= 0; y
< 8; y
++) {
679 flags
= (B
[y
* 2 + 1] << 8) | B
[y
* 2];
683 for (x
= 0, shifter
= 0; x
< 8; x
++, shifter
+= 2)
684 *s
->pixel_ptr
++ = P
[split
+ ((flags
>> shifter
) & 0x03)];
686 s
->pixel_ptr
+= s
->line_inc
;
695 static int ipvideo_decode_block_opcode_0xB(IpvideoContext
*s
)
699 /* 64-color encoding (each pixel in block is a different color) */
700 CHECK_STREAM_PTR(64);
702 for (y
= 0; y
< 8; y
++) {
703 for (x
= 0; x
< 8; x
++) {
704 *s
->pixel_ptr
++ = *s
->stream_ptr
++;
706 s
->pixel_ptr
+= s
->line_inc
;
713 static int ipvideo_decode_block_opcode_0xC(IpvideoContext
*s
)
718 /* 16-color block encoding: each 2x2 block is a different color */
719 CHECK_STREAM_PTR(16);
721 for (y
= 0; y
< 8; y
+= 2) {
722 for (x
= 0; x
< 8; x
+= 2) {
723 pix
= *s
->stream_ptr
++;
724 *(s
->pixel_ptr
+ x
) = pix
;
725 *(s
->pixel_ptr
+ x
+ 1) = pix
;
726 *(s
->pixel_ptr
+ s
->stride
+ x
) = pix
;
727 *(s
->pixel_ptr
+ s
->stride
+ x
+ 1) = pix
;
729 s
->pixel_ptr
+= s
->stride
* 2;
736 static int ipvideo_decode_block_opcode_0xD(IpvideoContext
*s
)
740 unsigned char index
= 0;
742 /* 4-color block encoding: each 4x4 block is a different color */
745 for (y
= 0; y
< 4; y
++)
746 P
[y
] = *s
->stream_ptr
++;
748 for (y
= 0; y
< 8; y
++) {
754 for (x
= 0; x
< 8; x
++) {
757 *s
->pixel_ptr
++ = P
[index
];
759 s
->pixel_ptr
+= s
->line_inc
;
766 static int ipvideo_decode_block_opcode_0xE(IpvideoContext
*s
)
771 /* 1-color encoding: the whole block is 1 solid color */
773 pix
= *s
->stream_ptr
++;
775 for (y
= 0; y
< 8; y
++) {
776 for (x
= 0; x
< 8; x
++) {
777 *s
->pixel_ptr
++ = pix
;
779 s
->pixel_ptr
+= s
->line_inc
;
786 static int ipvideo_decode_block_opcode_0xF(IpvideoContext
*s
)
789 unsigned char sample0
, sample1
;
791 /* dithered encoding */
793 sample0
= *s
->stream_ptr
++;
794 sample1
= *s
->stream_ptr
++;
796 for (y
= 0; y
< 8; y
++) {
797 for (x
= 0; x
< 8; x
+= 2) {
799 *s
->pixel_ptr
++ = sample1
;
800 *s
->pixel_ptr
++ = sample0
;
802 *s
->pixel_ptr
++ = sample0
;
803 *s
->pixel_ptr
++ = sample1
;
806 s
->pixel_ptr
+= s
->line_inc
;
813 static int (*ipvideo_decode_block
[16])(IpvideoContext
*s
);
815 static void ipvideo_decode_opcodes(IpvideoContext
*s
)
819 unsigned char opcode
;
822 static int frame
= 0;
824 debug_interplay("------------------ frame %d\n", frame
);
827 for (x
= 0; x
< 16; x
++)
830 /* this is PAL8, so make the palette available */
831 memcpy(s
->current_frame
.data
[1], s
->palette
, PALETTE_COUNT
* 4);
833 s
->stride
= s
->current_frame
.linesize
[0];
834 s
->stream_ptr
= s
->buf
+ 14; /* data starts 14 bytes in */
835 s
->stream_end
= s
->buf
+ s
->size
;
836 s
->line_inc
= s
->stride
- 8;
837 s
->upper_motion_limit_offset
= (s
->avctx
->height
- 8) * s
->stride
838 + s
->avctx
->width
- 8;
841 for (y
= 0; y
< (s
->stride
* s
->avctx
->height
); y
+= s
->stride
* 8) {
842 for (x
= y
; x
< y
+ s
->avctx
->width
; x
+= 8) {
843 /* bottom nibble first, then top nibble (which makes it
844 * hard to use a GetBitcontext) */
846 opcode
= s
->decoding_map
[index
>> 1] >> 4;
848 opcode
= s
->decoding_map
[index
>> 1] & 0xF;
851 debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
852 x
- y
, y
/ s
->stride
, opcode
, s
->stream_ptr
);
853 code_counts
[opcode
]++;
855 s
->pixel_ptr
= s
->current_frame
.data
[0] + x
;
856 ret
= ipvideo_decode_block
[opcode
](s
);
858 printf(" Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
859 frame
, x
- y
, y
/ s
->stride
);
864 if ((s
->stream_ptr
!= s
->stream_end
) &&
865 (s
->stream_ptr
+ 1 != s
->stream_end
)) {
866 printf (" Interplay video: decode finished with %d bytes left over\n",
867 s
->stream_end
- s
->stream_ptr
);
871 static int ipvideo_decode_init(AVCodecContext
*avctx
)
873 IpvideoContext
*s
= avctx
->priv_data
;
877 if (s
->avctx
->extradata_size
!= sizeof(AVPaletteControl
)) {
878 printf (" Interplay video: expected extradata_size of %d\n",
879 (int)sizeof(AVPaletteControl
));
883 avctx
->pix_fmt
= PIX_FMT_PAL8
;
884 avctx
->has_b_frames
= 0;
885 dsputil_init(&s
->dsp
, avctx
);
887 /* decoding map contains 4 bits of information per 8x8 block */
888 s
->decoding_map_size
= avctx
->width
* avctx
->height
/ (8 * 8 * 2);
890 /* assign block decode functions */
891 ipvideo_decode_block
[0x0] = ipvideo_decode_block_opcode_0x0
;
892 ipvideo_decode_block
[0x1] = ipvideo_decode_block_opcode_0x1
;
893 ipvideo_decode_block
[0x2] = ipvideo_decode_block_opcode_0x2
;
894 ipvideo_decode_block
[0x3] = ipvideo_decode_block_opcode_0x3
;
895 ipvideo_decode_block
[0x4] = ipvideo_decode_block_opcode_0x4
;
896 ipvideo_decode_block
[0x5] = ipvideo_decode_block_opcode_0x5
;
897 ipvideo_decode_block
[0x6] = ipvideo_decode_block_opcode_0x6
;
898 ipvideo_decode_block
[0x7] = ipvideo_decode_block_opcode_0x7
;
899 ipvideo_decode_block
[0x8] = ipvideo_decode_block_opcode_0x8
;
900 ipvideo_decode_block
[0x9] = ipvideo_decode_block_opcode_0x9
;
901 ipvideo_decode_block
[0xA] = ipvideo_decode_block_opcode_0xA
;
902 ipvideo_decode_block
[0xB] = ipvideo_decode_block_opcode_0xB
;
903 ipvideo_decode_block
[0xC] = ipvideo_decode_block_opcode_0xC
;
904 ipvideo_decode_block
[0xD] = ipvideo_decode_block_opcode_0xD
;
905 ipvideo_decode_block
[0xE] = ipvideo_decode_block_opcode_0xE
;
906 ipvideo_decode_block
[0xF] = ipvideo_decode_block_opcode_0xF
;
908 s
->current_frame
.data
[0] = s
->last_frame
.data
[0] =
909 s
->second_last_frame
.data
[0] = NULL
;
914 static int ipvideo_decode_frame(AVCodecContext
*avctx
,
915 void *data
, int *data_size
,
916 uint8_t *buf
, int buf_size
)
918 IpvideoContext
*s
= avctx
->priv_data
;
919 AVPaletteControl
*palette_control
= (AVPaletteControl
*)avctx
->extradata
;
921 if (palette_control
->palette_changed
) {
922 /* load the new palette and reset the palette control */
923 ipvideo_new_palette(s
, palette_control
->palette
);
924 palette_control
->palette_changed
= 0;
927 s
->decoding_map
= buf
;
928 s
->buf
= buf
+ s
->decoding_map_size
;
929 s
->size
= buf_size
- s
->decoding_map_size
;
931 s
->current_frame
.reference
= 3;
932 if (avctx
->get_buffer(avctx
, &s
->current_frame
)) {
933 printf (" Interplay Video: get_buffer() failed\n");
937 ipvideo_decode_opcodes(s
);
939 *data_size
= sizeof(AVFrame
);
940 *(AVFrame
*)data
= s
->current_frame
;
943 if (s
->second_last_frame
.data
[0])
944 avctx
->release_buffer(avctx
, &s
->second_last_frame
);
945 s
->second_last_frame
= s
->last_frame
;
946 s
->last_frame
= s
->current_frame
;
947 s
->current_frame
.data
[0] = NULL
; /* catch any access attempts */
949 /* report that the buffer was completely consumed */
953 static int ipvideo_decode_end(AVCodecContext
*avctx
)
955 IpvideoContext
*s
= avctx
->priv_data
;
957 /* release the last frame */
958 if (s
->last_frame
.data
[0])
959 avctx
->release_buffer(avctx
, &s
->last_frame
);
960 if (s
->second_last_frame
.data
[0])
961 avctx
->release_buffer(avctx
, &s
->second_last_frame
);
966 AVCodec interplay_video_decoder
= {
969 CODEC_ID_INTERPLAY_VIDEO
,
970 sizeof(IpvideoContext
),
974 ipvideo_decode_frame
,