2 * Interplay MVE Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Interplay MVE Video Decoder by Mike Melanson (melanson@pcisys.net)
25 * For more information about the Interplay MVE format, visit:
26 * http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
27 * This code is written in such a way that the identifiers match up
28 * with the encoding descriptions in the document.
30 * This decoder presently only supports a PAL8 output colorspace.
32 * An Interplay video frame consists of 2 parts: The decoding map and
33 * the video data. A demuxer must load these 2 parts together in a single
34 * buffer before sending it through the stream to this decoder.
42 #include "bytestream.h"
44 #define ALT_BITSTREAM_READER_LE
47 #define PALETTE_COUNT 256
49 /* debugging support */
50 #define DEBUG_INTERPLAY 0
52 #define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
54 static inline void debug_interplay(const char *format, ...) { }
57 typedef struct IpvideoContext {
59 AVCodecContext *avctx;
61 AVFrame second_last_frame;
63 AVFrame current_frame;
64 const unsigned char *decoding_map;
65 int decoding_map_size;
67 const unsigned char *buf;
71 const unsigned char *stream_ptr;
72 const unsigned char *stream_end;
73 const uint8_t *mv_ptr;
74 const uint8_t *mv_end;
75 unsigned char *pixel_ptr;
78 int upper_motion_limit_offset;
82 #define CHECK_STREAM_PTR(stream_ptr, stream_end, n) \
83 if (stream_end - stream_ptr < n) { \
84 av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
85 stream_ptr + n, stream_end); \
89 static int copy_from(IpvideoContext *s, AVFrame *src, int delta_x, int delta_y)
91 int current_offset = s->pixel_ptr - s->current_frame.data[0];
92 int motion_offset = current_offset + delta_y * s->current_frame.linesize[0]
93 + delta_x * (1 + s->is_16bpp);
94 if (motion_offset < 0) {
95 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset);
97 } else if (motion_offset > s->upper_motion_limit_offset) {
98 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n",
99 motion_offset, s->upper_motion_limit_offset);
102 s->dsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
103 s->current_frame.linesize[0], 8);
107 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
109 return copy_from(s, &s->last_frame, 0, 0);
112 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
114 return copy_from(s, &s->second_last_frame, 0, 0);
117 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
122 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
124 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
125 B = *s->stream_ptr++;
127 CHECK_STREAM_PTR(s->mv_ptr, s->mv_end, 1);
135 x = -14 + ((B - 56) % 29);
136 y = 8 + ((B - 56) / 29);
139 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
140 return copy_from(s, &s->second_last_frame, x, y);
143 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
148 /* copy 8x8 block from current frame from an up/left block */
150 /* need 1 more byte for motion */
152 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
153 B = *s->stream_ptr++;
155 CHECK_STREAM_PTR(s->mv_ptr, s->mv_end, 1);
163 x = -(-14 + ((B - 56) % 29));
164 y = -( 8 + ((B - 56) / 29));
167 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
168 return copy_from(s, &s->current_frame, x, y);
171 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
174 unsigned char B, BL, BH;
176 /* copy a block from the previous frame; need 1 more byte */
178 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
179 B = *s->stream_ptr++;
181 CHECK_STREAM_PTR(s->mv_ptr, s->mv_end, 1);
186 BH = (B >> 4) & 0x0F;
190 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
191 return copy_from(s, &s->last_frame, x, y);
194 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
198 /* copy a block from the previous frame using an expanded range;
199 * need 2 more bytes */
200 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
202 x = *s->stream_ptr++;
203 y = *s->stream_ptr++;
205 debug_interplay (" motion bytes = %d, %d\n", x, y);
206 return copy_from(s, &s->last_frame, x, y);
209 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
211 /* mystery opcode? skip multiple blocks? */
212 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: Help! Mystery opcode 0x6 seen\n");
218 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
224 /* 2-color encoding */
225 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
227 P[0] = *s->stream_ptr++;
228 P[1] = *s->stream_ptr++;
232 /* need 8 more bytes from the stream */
233 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
235 for (y = 0; y < 8; y++) {
236 flags = *s->stream_ptr++ | 0x100;
237 for (; flags != 1; flags >>= 1)
238 *s->pixel_ptr++ = P[flags & 1];
239 s->pixel_ptr += s->line_inc;
244 /* need 2 more bytes from the stream */
245 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
247 flags = bytestream_get_le16(&s->stream_ptr);
248 for (y = 0; y < 8; y += 2) {
249 for (x = 0; x < 8; x += 2, flags >>= 1) {
251 s->pixel_ptr[x + 1 ] =
252 s->pixel_ptr[x + s->stride] =
253 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
255 s->pixel_ptr += s->stride * 2;
263 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
267 unsigned int flags = 0;
269 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
270 * either top and bottom or left and right halves */
271 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
273 P[0] = *s->stream_ptr++;
274 P[1] = *s->stream_ptr++;
278 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 14);
281 for (y = 0; y < 16; y++) {
282 // new values for each 4x4 block
284 P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
285 flags = bytestream_get_le16(&s->stream_ptr);
288 for (x = 0; x < 4; x++, flags >>= 1)
289 *s->pixel_ptr++ = P[flags & 1];
290 s->pixel_ptr += s->stride - 4;
291 // switch to right half
292 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
297 /* need 10 more bytes */
298 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 10);
300 if (s->stream_ptr[4] <= s->stream_ptr[5]) {
302 flags = bytestream_get_le32(&s->stream_ptr);
304 /* vertical split; left & right halves are 2-color encoded */
306 for (y = 0; y < 16; y++) {
307 for (x = 0; x < 4; x++, flags >>= 1)
308 *s->pixel_ptr++ = P[flags & 1];
309 s->pixel_ptr += s->stride - 4;
310 // switch to right half
312 s->pixel_ptr -= 8 * s->stride - 4;
313 P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
314 flags = bytestream_get_le32(&s->stream_ptr);
320 /* horizontal split; top & bottom halves are 2-color encoded */
322 for (y = 0; y < 8; y++) {
324 P[0] = *s->stream_ptr++;
325 P[1] = *s->stream_ptr++;
327 flags = *s->stream_ptr++ | 0x100;
329 for (; flags != 1; flags >>= 1)
330 *s->pixel_ptr++ = P[flags & 1];
331 s->pixel_ptr += s->line_inc;
340 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
345 /* 4-color encoding */
346 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
348 memcpy(P, s->stream_ptr, 4);
354 /* 1 of 4 colors for each pixel, need 16 more bytes */
355 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 16);
357 for (y = 0; y < 8; y++) {
358 /* get the next set of 8 2-bit flags */
359 int flags = bytestream_get_le16(&s->stream_ptr);
360 for (x = 0; x < 8; x++, flags >>= 2)
361 *s->pixel_ptr++ = P[flags & 0x03];
362 s->pixel_ptr += s->line_inc;
368 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
369 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
371 flags = bytestream_get_le32(&s->stream_ptr);
373 for (y = 0; y < 8; y += 2) {
374 for (x = 0; x < 8; x += 2, flags >>= 2) {
376 s->pixel_ptr[x + 1 ] =
377 s->pixel_ptr[x + s->stride] =
378 s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
380 s->pixel_ptr += s->stride * 2;
387 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
388 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
390 flags = bytestream_get_le64(&s->stream_ptr);
392 for (y = 0; y < 8; y++) {
393 for (x = 0; x < 8; x += 2, flags >>= 2) {
395 s->pixel_ptr[x + 1] = P[flags & 0x03];
397 s->pixel_ptr += s->stride;
400 for (y = 0; y < 8; y += 2) {
401 for (x = 0; x < 8; x++, flags >>= 2) {
403 s->pixel_ptr[x + s->stride] = P[flags & 0x03];
405 s->pixel_ptr += s->stride * 2;
414 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
420 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
421 * either top and bottom or left and right halves */
422 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 24);
424 if (s->stream_ptr[0] <= s->stream_ptr[1]) {
426 /* 4-color encoding for each quadrant; need 32 bytes */
427 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 32);
429 for (y = 0; y < 16; y++) {
430 // new values for each 4x4 block
432 memcpy(P, s->stream_ptr, 4);
434 flags = bytestream_get_le32(&s->stream_ptr);
437 for (x = 0; x < 4; x++, flags >>= 2)
438 *s->pixel_ptr++ = P[flags & 0x03];
440 s->pixel_ptr += s->stride - 4;
441 // switch to right half
442 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
447 int vert = s->stream_ptr[12] <= s->stream_ptr[13];
450 /* 4-color encoding for either left and right or top and bottom
453 for (y = 0; y < 16; y++) {
454 // load values for each half
456 memcpy(P, s->stream_ptr, 4);
458 flags = bytestream_get_le64(&s->stream_ptr);
461 for (x = 0; x < 4; x++, flags >>= 2)
462 *s->pixel_ptr++ = P[flags & 0x03];
465 s->pixel_ptr += s->stride - 4;
466 // switch to right half
467 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
468 } else if (y & 1) s->pixel_ptr += s->line_inc;
476 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
480 /* 64-color encoding (each pixel in block is a different color) */
481 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 64);
483 for (y = 0; y < 8; y++) {
484 memcpy(s->pixel_ptr, s->stream_ptr, 8);
486 s->pixel_ptr += s->stride;
493 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
497 /* 16-color block encoding: each 2x2 block is a different color */
498 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 16);
500 for (y = 0; y < 8; y += 2) {
501 for (x = 0; x < 8; x += 2) {
503 s->pixel_ptr[x + 1 ] =
504 s->pixel_ptr[x + s->stride] =
505 s->pixel_ptr[x + 1 + s->stride] = *s->stream_ptr++;
507 s->pixel_ptr += s->stride * 2;
514 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
519 /* 4-color block encoding: each 4x4 block is a different color */
520 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
522 for (y = 0; y < 8; y++) {
524 P[0] = *s->stream_ptr++;
525 P[1] = *s->stream_ptr++;
527 memset(s->pixel_ptr, P[0], 4);
528 memset(s->pixel_ptr + 4, P[1], 4);
529 s->pixel_ptr += s->stride;
536 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
541 /* 1-color encoding: the whole block is 1 solid color */
542 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
543 pix = *s->stream_ptr++;
545 for (y = 0; y < 8; y++) {
546 memset(s->pixel_ptr, pix, 8);
547 s->pixel_ptr += s->stride;
554 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
557 unsigned char sample[2];
559 /* dithered encoding */
560 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
561 sample[0] = *s->stream_ptr++;
562 sample[1] = *s->stream_ptr++;
564 for (y = 0; y < 8; y++) {
565 for (x = 0; x < 8; x += 2) {
566 *s->pixel_ptr++ = sample[ y & 1 ];
567 *s->pixel_ptr++ = sample[!(y & 1)];
569 s->pixel_ptr += s->line_inc;
576 static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s)
580 /* copy a block from the second last frame using an expanded range */
581 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
583 x = *s->stream_ptr++;
584 y = *s->stream_ptr++;
586 debug_interplay (" motion bytes = %d, %d\n", x, y);
587 return copy_from(s, &s->second_last_frame, x, y);
590 static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s)
595 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
597 /* 2-color encoding */
598 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
600 P[0] = bytestream_get_le16(&s->stream_ptr);
601 P[1] = bytestream_get_le16(&s->stream_ptr);
603 if (!(P[0] & 0x8000)) {
605 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
607 for (y = 0; y < 8; y++) {
608 flags = *s->stream_ptr++ | 0x100;
609 for (; flags != 1; flags >>= 1)
610 *pixel_ptr++ = P[flags & 1];
611 pixel_ptr += s->line_inc;
616 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
618 flags = bytestream_get_le16(&s->stream_ptr);
619 for (y = 0; y < 8; y += 2) {
620 for (x = 0; x < 8; x += 2, flags >>= 1) {
623 pixel_ptr[x + s->stride] =
624 pixel_ptr[x + 1 + s->stride] = P[flags & 1];
626 pixel_ptr += s->stride * 2;
633 static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s)
637 unsigned int flags = 0;
638 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
640 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
641 * either top and bottom or left and right halves */
642 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
644 P[0] = bytestream_get_le16(&s->stream_ptr);
645 P[1] = bytestream_get_le16(&s->stream_ptr);
647 if (!(P[0] & 0x8000)) {
649 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 24);
652 for (y = 0; y < 16; y++) {
653 // new values for each 4x4 block
655 P[0] = bytestream_get_le16(&s->stream_ptr);
656 P[1] = bytestream_get_le16(&s->stream_ptr);
657 flags = bytestream_get_le16(&s->stream_ptr);
660 for (x = 0; x < 4; x++, flags >>= 1)
661 *pixel_ptr++ = P[flags & 1];
662 pixel_ptr += s->stride - 4;
663 // switch to right half
664 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
669 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 12);
671 if (!(AV_RL16(s->stream_ptr + 4) & 0x8000)) {
673 flags = bytestream_get_le32(&s->stream_ptr);
675 /* vertical split; left & right halves are 2-color encoded */
677 for (y = 0; y < 16; y++) {
678 for (x = 0; x < 4; x++, flags >>= 1)
679 *pixel_ptr++ = P[flags & 1];
680 pixel_ptr += s->stride - 4;
681 // switch to right half
683 pixel_ptr -= 8 * s->stride - 4;
684 P[0] = bytestream_get_le16(&s->stream_ptr);
685 P[1] = bytestream_get_le16(&s->stream_ptr);
686 flags = bytestream_get_le32(&s->stream_ptr);
692 /* horizontal split; top & bottom halves are 2-color encoded */
694 for (y = 0; y < 8; y++) {
696 P[0] = bytestream_get_le16(&s->stream_ptr);
697 P[1] = bytestream_get_le16(&s->stream_ptr);
699 flags = *s->stream_ptr++ | 0x100;
701 for (; flags != 1; flags >>= 1)
702 *pixel_ptr++ = P[flags & 1];
703 pixel_ptr += s->line_inc;
712 static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s)
716 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
718 /* 4-color encoding */
719 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
721 for (x = 0; x < 4; x++)
722 P[x] = bytestream_get_le16(&s->stream_ptr);
724 if (!(P[0] & 0x8000)) {
725 if (!(P[2] & 0x8000)) {
727 /* 1 of 4 colors for each pixel */
728 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 16);
730 for (y = 0; y < 8; y++) {
731 /* get the next set of 8 2-bit flags */
732 int flags = bytestream_get_le16(&s->stream_ptr);
733 for (x = 0; x < 8; x++, flags >>= 2)
734 *pixel_ptr++ = P[flags & 0x03];
735 pixel_ptr += s->line_inc;
741 /* 1 of 4 colors for each 2x2 block */
742 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
744 flags = bytestream_get_le32(&s->stream_ptr);
746 for (y = 0; y < 8; y += 2) {
747 for (x = 0; x < 8; x += 2, flags >>= 2) {
750 pixel_ptr[x + s->stride] =
751 pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
753 pixel_ptr += s->stride * 2;
760 /* 1 of 4 colors for each 2x1 or 1x2 block */
761 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
763 flags = bytestream_get_le64(&s->stream_ptr);
764 if (!(P[2] & 0x8000)) {
765 for (y = 0; y < 8; y++) {
766 for (x = 0; x < 8; x += 2, flags >>= 2) {
768 pixel_ptr[x + 1] = P[flags & 0x03];
770 pixel_ptr += s->stride;
773 for (y = 0; y < 8; y += 2) {
774 for (x = 0; x < 8; x++, flags >>= 2) {
776 pixel_ptr[x + s->stride] = P[flags & 0x03];
778 pixel_ptr += s->stride * 2;
787 static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s)
792 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
794 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
795 * either top and bottom or left and right halves */
796 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 24);
798 if (!(AV_RL16(s->stream_ptr) & 0x8000)) {
800 /* 4-color encoding for each quadrant */
801 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 48);
803 for (y = 0; y < 16; y++) {
804 // new values for each 4x4 block
806 for (x = 0; x < 4; x++)
807 P[x] = bytestream_get_le16(&s->stream_ptr);
808 flags = bytestream_get_le32(&s->stream_ptr);
811 for (x = 0; x < 4; x++, flags >>= 2)
812 *pixel_ptr++ = P[flags & 0x03];
814 pixel_ptr += s->stride - 4;
815 // switch to right half
816 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
821 int vert = !(AV_RL16(s->stream_ptr + 16) & 0x8000);
824 /* 4-color encoding for either left and right or top and bottom
827 for (y = 0; y < 16; y++) {
828 // load values for each half
830 for (x = 0; x < 4; x++)
831 P[x] = bytestream_get_le16(&s->stream_ptr);
832 flags = bytestream_get_le64(&s->stream_ptr);
835 for (x = 0; x < 4; x++, flags >>= 2)
836 *pixel_ptr++ = P[flags & 0x03];
839 pixel_ptr += s->stride - 4;
840 // switch to right half
841 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
842 } else if (y & 1) pixel_ptr += s->line_inc;
850 static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s)
853 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
855 /* 64-color encoding (each pixel in block is a different color) */
856 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 128);
858 for (y = 0; y < 8; y++) {
859 for (x = 0; x < 8; x++)
860 pixel_ptr[x] = bytestream_get_le16(&s->stream_ptr);
861 pixel_ptr += s->stride;
868 static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s)
871 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
873 /* 16-color block encoding: each 2x2 block is a different color */
874 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 32);
876 for (y = 0; y < 8; y += 2) {
877 for (x = 0; x < 8; x += 2) {
880 pixel_ptr[x + s->stride] =
881 pixel_ptr[x + 1 + s->stride] = bytestream_get_le16(&s->stream_ptr);
883 pixel_ptr += s->stride * 2;
890 static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s)
894 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
896 /* 4-color block encoding: each 4x4 block is a different color */
897 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
899 for (y = 0; y < 8; y++) {
901 P[0] = bytestream_get_le16(&s->stream_ptr);
902 P[1] = bytestream_get_le16(&s->stream_ptr);
904 for (x = 0; x < 8; x++)
905 pixel_ptr[x] = P[x >> 2];
906 pixel_ptr += s->stride;
913 static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s)
917 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
919 /* 1-color encoding: the whole block is 1 solid color */
920 CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
921 pix = bytestream_get_le16(&s->stream_ptr);
923 for (y = 0; y < 8; y++) {
924 for (x = 0; x < 8; x++)
926 pixel_ptr += s->stride;
933 static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
934 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
935 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
936 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
937 ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
938 ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
939 ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
940 ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
941 ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
944 static int (* const ipvideo_decode_block16[])(IpvideoContext *s) = {
945 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
946 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
947 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
948 ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
949 ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
950 ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
951 ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
952 ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
955 static void ipvideo_decode_opcodes(IpvideoContext *s)
958 unsigned char opcode;
960 static int frame = 0;
963 debug_interplay("------------------ frame %d\n", frame);
967 /* this is PAL8, so make the palette available */
968 memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
970 s->stride = s->current_frame.linesize[0];
971 s->stream_ptr = s->buf + 14; /* data starts 14 bytes in */
972 s->stream_end = s->buf + s->size;
974 s->stride = s->current_frame.linesize[0] >> 1;
975 s->stream_ptr = s->buf + 16;
977 s->mv_ptr = s->buf + 14 + AV_RL16(s->buf+14);
978 s->mv_end = s->buf + s->size;
980 s->line_inc = s->stride - 8;
981 s->upper_motion_limit_offset = (s->avctx->height - 8) * s->current_frame.linesize[0]
982 + (s->avctx->width - 8) * (1 + s->is_16bpp);
984 init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
985 for (y = 0; y < s->avctx->height; y += 8) {
986 for (x = 0; x < s->avctx->width; x += 8) {
987 opcode = get_bits(&gb, 4);
989 debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
990 x, y, opcode, s->stream_ptr);
993 s->pixel_ptr = s->current_frame.data[0] + x
994 + y*s->current_frame.linesize[0];
995 ret = ipvideo_decode_block[opcode](s);
997 s->pixel_ptr = s->current_frame.data[0] + x*2
998 + y*s->current_frame.linesize[0];
999 ret = ipvideo_decode_block16[opcode](s);
1002 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
1008 if (s->stream_end - s->stream_ptr > 1) {
1009 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
1010 s->stream_end - s->stream_ptr);
1014 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
1016 IpvideoContext *s = avctx->priv_data;
1020 s->is_16bpp = avctx->bits_per_coded_sample == 16;
1021 avctx->pix_fmt = s->is_16bpp ? PIX_FMT_RGB555 : PIX_FMT_PAL8;
1022 if (!s->is_16bpp && s->avctx->palctrl == NULL) {
1023 av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
1027 dsputil_init(&s->dsp, avctx);
1029 /* decoding map contains 4 bits of information per 8x8 block */
1030 s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
1032 s->current_frame.data[0] = s->last_frame.data[0] =
1033 s->second_last_frame.data[0] = NULL;
1038 static int ipvideo_decode_frame(AVCodecContext *avctx,
1039 void *data, int *data_size,
1042 const uint8_t *buf = avpkt->data;
1043 int buf_size = avpkt->size;
1044 IpvideoContext *s = avctx->priv_data;
1045 AVPaletteControl *palette_control = avctx->palctrl;
1047 /* compressed buffer needs to be large enough to at least hold an entire
1049 if (buf_size < s->decoding_map_size)
1052 s->decoding_map = buf;
1053 s->buf = buf + s->decoding_map_size;
1054 s->size = buf_size - s->decoding_map_size;
1056 s->current_frame.reference = 3;
1057 if (avctx->get_buffer(avctx, &s->current_frame)) {
1058 av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
1062 ipvideo_decode_opcodes(s);
1064 if (!s->is_16bpp && palette_control->palette_changed) {
1065 palette_control->palette_changed = 0;
1066 s->current_frame.palette_has_changed = 1;
1069 *data_size = sizeof(AVFrame);
1070 *(AVFrame*)data = s->current_frame;
1072 /* shuffle frames */
1073 if (s->second_last_frame.data[0])
1074 avctx->release_buffer(avctx, &s->second_last_frame);
1075 s->second_last_frame = s->last_frame;
1076 s->last_frame = s->current_frame;
1077 s->current_frame.data[0] = NULL; /* catch any access attempts */
1079 /* report that the buffer was completely consumed */
1083 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1085 IpvideoContext *s = avctx->priv_data;
1087 /* release the last frame */
1088 if (s->last_frame.data[0])
1089 avctx->release_buffer(avctx, &s->last_frame);
1090 if (s->second_last_frame.data[0])
1091 avctx->release_buffer(avctx, &s->second_last_frame);
1096 AVCodec interplay_video_decoder = {
1099 CODEC_ID_INTERPLAY_VIDEO,
1100 sizeof(IpvideoContext),
1101 ipvideo_decode_init,
1104 ipvideo_decode_frame,
1106 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),