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.
41 #include "libavutil/intreadwrite.h"
43 #define BITSTREAM_READER_LE
45 #include "bytestream.h"
50 #define PALETTE_COUNT 256
52 typedef struct IpvideoContext {
54 AVCodecContext *avctx;
56 AVFrame *second_last_frame;
58 const unsigned char *decoding_map;
59 int decoding_map_size;
62 GetByteContext stream_ptr, mv_ptr;
63 unsigned char *pixel_ptr;
66 int upper_motion_limit_offset;
71 static int copy_from(IpvideoContext *s, AVFrame *src, AVFrame *dst, int delta_x, int delta_y)
73 int current_offset = s->pixel_ptr - dst->data[0];
74 int motion_offset = current_offset + delta_y * dst->linesize[0]
75 + delta_x * (1 + s->is_16bpp);
76 if (motion_offset < 0) {
77 av_log(s->avctx, AV_LOG_ERROR, "motion offset < 0 (%d)\n", motion_offset);
78 return AVERROR_INVALIDDATA;
79 } else if (motion_offset > s->upper_motion_limit_offset) {
80 av_log(s->avctx, AV_LOG_ERROR, "motion offset above limit (%d >= %d)\n",
81 motion_offset, s->upper_motion_limit_offset);
82 return AVERROR_INVALIDDATA;
85 av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
86 return AVERROR(EINVAL);
88 s->hdsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
93 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s, AVFrame *frame)
95 return copy_from(s, s->last_frame, frame, 0, 0);
98 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s, AVFrame *frame)
100 return copy_from(s, s->second_last_frame, frame, 0, 0);
103 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s, AVFrame *frame)
108 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
110 B = bytestream2_get_byte(&s->stream_ptr);
112 B = bytestream2_get_byte(&s->mv_ptr);
119 x = -14 + ((B - 56) % 29);
120 y = 8 + ((B - 56) / 29);
123 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
124 return copy_from(s, s->second_last_frame, frame, x, y);
127 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s, AVFrame *frame)
132 /* copy 8x8 block from current frame from an up/left block */
134 /* need 1 more byte for motion */
136 B = bytestream2_get_byte(&s->stream_ptr);
138 B = bytestream2_get_byte(&s->mv_ptr);
145 x = -(-14 + ((B - 56) % 29));
146 y = -( 8 + ((B - 56) / 29));
149 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
150 return copy_from(s, frame, frame, x, y);
153 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s, AVFrame *frame)
156 unsigned char B, BL, BH;
158 /* copy a block from the previous frame; need 1 more byte */
160 B = bytestream2_get_byte(&s->stream_ptr);
162 B = bytestream2_get_byte(&s->mv_ptr);
166 BH = (B >> 4) & 0x0F;
170 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
171 return copy_from(s, s->last_frame, frame, x, y);
174 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s, AVFrame *frame)
178 /* copy a block from the previous frame using an expanded range;
179 * need 2 more bytes */
180 x = bytestream2_get_byte(&s->stream_ptr);
181 y = bytestream2_get_byte(&s->stream_ptr);
183 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
184 return copy_from(s, s->last_frame, frame, x, y);
187 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s, AVFrame *frame)
189 /* mystery opcode? skip multiple blocks? */
190 av_log(s->avctx, AV_LOG_ERROR, "Help! Mystery opcode 0x6 seen\n");
196 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s, AVFrame *frame)
202 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
203 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x7\n");
204 return AVERROR_INVALIDDATA;
207 /* 2-color encoding */
208 P[0] = bytestream2_get_byte(&s->stream_ptr);
209 P[1] = bytestream2_get_byte(&s->stream_ptr);
213 /* need 8 more bytes from the stream */
214 for (y = 0; y < 8; y++) {
215 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
216 for (; flags != 1; flags >>= 1)
217 *s->pixel_ptr++ = P[flags & 1];
218 s->pixel_ptr += s->line_inc;
223 /* need 2 more bytes from the stream */
224 flags = bytestream2_get_le16(&s->stream_ptr);
225 for (y = 0; y < 8; y += 2) {
226 for (x = 0; x < 8; x += 2, flags >>= 1) {
228 s->pixel_ptr[x + 1 ] =
229 s->pixel_ptr[x + s->stride] =
230 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
232 s->pixel_ptr += s->stride * 2;
240 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s, AVFrame *frame)
244 unsigned int flags = 0;
246 if (bytestream2_get_bytes_left(&s->stream_ptr) < 12) {
247 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x8\n");
248 return AVERROR_INVALIDDATA;
251 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
252 * either top and bottom or left and right halves */
253 P[0] = bytestream2_get_byte(&s->stream_ptr);
254 P[1] = bytestream2_get_byte(&s->stream_ptr);
257 for (y = 0; y < 16; y++) {
258 // new values for each 4x4 block
261 P[0] = bytestream2_get_byte(&s->stream_ptr);
262 P[1] = bytestream2_get_byte(&s->stream_ptr);
264 flags = bytestream2_get_le16(&s->stream_ptr);
267 for (x = 0; x < 4; x++, flags >>= 1)
268 *s->pixel_ptr++ = P[flags & 1];
269 s->pixel_ptr += s->stride - 4;
270 // switch to right half
271 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
275 flags = bytestream2_get_le32(&s->stream_ptr);
276 P[2] = bytestream2_get_byte(&s->stream_ptr);
277 P[3] = bytestream2_get_byte(&s->stream_ptr);
281 /* vertical split; left & right halves are 2-color encoded */
283 for (y = 0; y < 16; y++) {
284 for (x = 0; x < 4; x++, flags >>= 1)
285 *s->pixel_ptr++ = P[flags & 1];
286 s->pixel_ptr += s->stride - 4;
287 // switch to right half
289 s->pixel_ptr -= 8 * s->stride - 4;
292 flags = bytestream2_get_le32(&s->stream_ptr);
298 /* horizontal split; top & bottom halves are 2-color encoded */
300 for (y = 0; y < 8; y++) {
304 flags = bytestream2_get_le32(&s->stream_ptr);
307 for (x = 0; x < 8; x++, flags >>= 1)
308 *s->pixel_ptr++ = P[flags & 1];
309 s->pixel_ptr += s->line_inc;
318 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s, AVFrame *frame)
323 if (bytestream2_get_bytes_left(&s->stream_ptr) < 8) {
324 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x9\n");
325 return AVERROR_INVALIDDATA;
328 /* 4-color encoding */
329 bytestream2_get_buffer(&s->stream_ptr, P, 4);
334 /* 1 of 4 colors for each pixel, need 16 more bytes */
335 for (y = 0; y < 8; y++) {
336 /* get the next set of 8 2-bit flags */
337 int flags = bytestream2_get_le16(&s->stream_ptr);
338 for (x = 0; x < 8; x++, flags >>= 2)
339 *s->pixel_ptr++ = P[flags & 0x03];
340 s->pixel_ptr += s->line_inc;
346 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
347 flags = bytestream2_get_le32(&s->stream_ptr);
349 for (y = 0; y < 8; y += 2) {
350 for (x = 0; x < 8; x += 2, flags >>= 2) {
352 s->pixel_ptr[x + 1 ] =
353 s->pixel_ptr[x + s->stride] =
354 s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
356 s->pixel_ptr += s->stride * 2;
363 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
364 flags = bytestream2_get_le64(&s->stream_ptr);
366 for (y = 0; y < 8; y++) {
367 for (x = 0; x < 8; x += 2, flags >>= 2) {
369 s->pixel_ptr[x + 1] = P[flags & 0x03];
371 s->pixel_ptr += s->stride;
374 for (y = 0; y < 8; y += 2) {
375 for (x = 0; x < 8; x++, flags >>= 2) {
377 s->pixel_ptr[x + s->stride] = P[flags & 0x03];
379 s->pixel_ptr += s->stride * 2;
388 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s, AVFrame *frame)
394 if (bytestream2_get_bytes_left(&s->stream_ptr) < 16) {
395 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xA\n");
396 return AVERROR_INVALIDDATA;
399 bytestream2_get_buffer(&s->stream_ptr, P, 4);
401 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
402 * either top and bottom or left and right halves */
405 /* 4-color encoding for each quadrant; need 32 bytes */
406 for (y = 0; y < 16; y++) {
407 // new values for each 4x4 block
409 if (y) bytestream2_get_buffer(&s->stream_ptr, P, 4);
410 flags = bytestream2_get_le32(&s->stream_ptr);
413 for (x = 0; x < 4; x++, flags >>= 2)
414 *s->pixel_ptr++ = P[flags & 0x03];
416 s->pixel_ptr += s->stride - 4;
417 // switch to right half
418 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
424 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
426 bytestream2_get_buffer(&s->stream_ptr, P + 4, 4);
429 /* 4-color encoding for either left and right or top and bottom
432 for (y = 0; y < 16; y++) {
433 for (x = 0; x < 4; x++, flags >>= 2)
434 *s->pixel_ptr++ = P[flags & 0x03];
437 s->pixel_ptr += s->stride - 4;
438 // switch to right half
439 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
440 } else if (y & 1) s->pixel_ptr += s->line_inc;
442 // load values for second half
445 flags = bytestream2_get_le64(&s->stream_ptr);
454 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s, AVFrame *frame)
458 /* 64-color encoding (each pixel in block is a different color) */
459 for (y = 0; y < 8; y++) {
460 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
461 s->pixel_ptr += s->stride;
468 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s, AVFrame *frame)
472 /* 16-color block encoding: each 2x2 block is a different color */
473 for (y = 0; y < 8; y += 2) {
474 for (x = 0; x < 8; x += 2) {
476 s->pixel_ptr[x + 1 ] =
477 s->pixel_ptr[x + s->stride] =
478 s->pixel_ptr[x + 1 + s->stride] = bytestream2_get_byte(&s->stream_ptr);
480 s->pixel_ptr += s->stride * 2;
487 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s, AVFrame *frame)
492 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
493 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xD\n");
494 return AVERROR_INVALIDDATA;
497 /* 4-color block encoding: each 4x4 block is a different color */
498 for (y = 0; y < 8; y++) {
500 P[0] = bytestream2_get_byte(&s->stream_ptr);
501 P[1] = bytestream2_get_byte(&s->stream_ptr);
503 memset(s->pixel_ptr, P[0], 4);
504 memset(s->pixel_ptr + 4, P[1], 4);
505 s->pixel_ptr += s->stride;
512 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s, AVFrame *frame)
517 /* 1-color encoding: the whole block is 1 solid color */
518 pix = bytestream2_get_byte(&s->stream_ptr);
520 for (y = 0; y < 8; y++) {
521 memset(s->pixel_ptr, pix, 8);
522 s->pixel_ptr += s->stride;
529 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s, AVFrame *frame)
532 unsigned char sample[2];
534 /* dithered encoding */
535 sample[0] = bytestream2_get_byte(&s->stream_ptr);
536 sample[1] = bytestream2_get_byte(&s->stream_ptr);
538 for (y = 0; y < 8; y++) {
539 for (x = 0; x < 8; x += 2) {
540 *s->pixel_ptr++ = sample[ y & 1 ];
541 *s->pixel_ptr++ = sample[!(y & 1)];
543 s->pixel_ptr += s->line_inc;
550 static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s, AVFrame *frame)
554 /* copy a block from the second last frame using an expanded range */
555 x = bytestream2_get_byte(&s->stream_ptr);
556 y = bytestream2_get_byte(&s->stream_ptr);
558 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
559 return copy_from(s, s->second_last_frame, frame, x, y);
562 static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s, AVFrame *frame)
567 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
569 /* 2-color encoding */
570 P[0] = bytestream2_get_le16(&s->stream_ptr);
571 P[1] = bytestream2_get_le16(&s->stream_ptr);
573 if (!(P[0] & 0x8000)) {
575 for (y = 0; y < 8; y++) {
576 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
577 for (; flags != 1; flags >>= 1)
578 *pixel_ptr++ = P[flags & 1];
579 pixel_ptr += s->line_inc;
584 flags = bytestream2_get_le16(&s->stream_ptr);
585 for (y = 0; y < 8; y += 2) {
586 for (x = 0; x < 8; x += 2, flags >>= 1) {
589 pixel_ptr[x + s->stride] =
590 pixel_ptr[x + 1 + s->stride] = P[flags & 1];
592 pixel_ptr += s->stride * 2;
599 static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s, AVFrame *frame)
603 unsigned int flags = 0;
604 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
606 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
607 * either top and bottom or left and right halves */
608 P[0] = bytestream2_get_le16(&s->stream_ptr);
609 P[1] = bytestream2_get_le16(&s->stream_ptr);
611 if (!(P[0] & 0x8000)) {
613 for (y = 0; y < 16; y++) {
614 // new values for each 4x4 block
617 P[0] = bytestream2_get_le16(&s->stream_ptr);
618 P[1] = bytestream2_get_le16(&s->stream_ptr);
620 flags = bytestream2_get_le16(&s->stream_ptr);
623 for (x = 0; x < 4; x++, flags >>= 1)
624 *pixel_ptr++ = P[flags & 1];
625 pixel_ptr += s->stride - 4;
626 // switch to right half
627 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
632 flags = bytestream2_get_le32(&s->stream_ptr);
633 P[2] = bytestream2_get_le16(&s->stream_ptr);
634 P[3] = bytestream2_get_le16(&s->stream_ptr);
636 if (!(P[2] & 0x8000)) {
638 /* vertical split; left & right halves are 2-color encoded */
640 for (y = 0; y < 16; y++) {
641 for (x = 0; x < 4; x++, flags >>= 1)
642 *pixel_ptr++ = P[flags & 1];
643 pixel_ptr += s->stride - 4;
644 // switch to right half
646 pixel_ptr -= 8 * s->stride - 4;
649 flags = bytestream2_get_le32(&s->stream_ptr);
655 /* horizontal split; top & bottom halves are 2-color encoded */
657 for (y = 0; y < 8; y++) {
661 flags = bytestream2_get_le32(&s->stream_ptr);
664 for (x = 0; x < 8; x++, flags >>= 1)
665 *pixel_ptr++ = P[flags & 1];
666 pixel_ptr += s->line_inc;
675 static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s, AVFrame *frame)
679 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
681 /* 4-color encoding */
682 for (x = 0; x < 4; x++)
683 P[x] = bytestream2_get_le16(&s->stream_ptr);
685 if (!(P[0] & 0x8000)) {
686 if (!(P[2] & 0x8000)) {
688 /* 1 of 4 colors for each pixel */
689 for (y = 0; y < 8; y++) {
690 /* get the next set of 8 2-bit flags */
691 int flags = bytestream2_get_le16(&s->stream_ptr);
692 for (x = 0; x < 8; x++, flags >>= 2)
693 *pixel_ptr++ = P[flags & 0x03];
694 pixel_ptr += s->line_inc;
700 /* 1 of 4 colors for each 2x2 block */
701 flags = bytestream2_get_le32(&s->stream_ptr);
703 for (y = 0; y < 8; y += 2) {
704 for (x = 0; x < 8; x += 2, flags >>= 2) {
707 pixel_ptr[x + s->stride] =
708 pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
710 pixel_ptr += s->stride * 2;
717 /* 1 of 4 colors for each 2x1 or 1x2 block */
718 flags = bytestream2_get_le64(&s->stream_ptr);
719 if (!(P[2] & 0x8000)) {
720 for (y = 0; y < 8; y++) {
721 for (x = 0; x < 8; x += 2, flags >>= 2) {
723 pixel_ptr[x + 1] = P[flags & 0x03];
725 pixel_ptr += s->stride;
728 for (y = 0; y < 8; y += 2) {
729 for (x = 0; x < 8; x++, flags >>= 2) {
731 pixel_ptr[x + s->stride] = P[flags & 0x03];
733 pixel_ptr += s->stride * 2;
742 static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s, AVFrame *frame)
747 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
749 for (x = 0; x < 4; x++)
750 P[x] = bytestream2_get_le16(&s->stream_ptr);
752 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
753 * either top and bottom or left and right halves */
754 if (!(P[0] & 0x8000)) {
756 /* 4-color encoding for each quadrant */
757 for (y = 0; y < 16; y++) {
758 // new values for each 4x4 block
761 for (x = 0; x < 4; x++)
762 P[x] = bytestream2_get_le16(&s->stream_ptr);
763 flags = bytestream2_get_le32(&s->stream_ptr);
766 for (x = 0; x < 4; x++, flags >>= 2)
767 *pixel_ptr++ = P[flags & 0x03];
769 pixel_ptr += s->stride - 4;
770 // switch to right half
771 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
777 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
779 for (x = 4; x < 8; x++)
780 P[x] = bytestream2_get_le16(&s->stream_ptr);
781 vert = !(P[4] & 0x8000);
783 /* 4-color encoding for either left and right or top and bottom
786 for (y = 0; y < 16; y++) {
787 for (x = 0; x < 4; x++, flags >>= 2)
788 *pixel_ptr++ = P[flags & 0x03];
791 pixel_ptr += s->stride - 4;
792 // switch to right half
793 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
794 } else if (y & 1) pixel_ptr += s->line_inc;
796 // load values for second half
799 flags = bytestream2_get_le64(&s->stream_ptr);
808 static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s, AVFrame *frame)
811 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
813 /* 64-color encoding (each pixel in block is a different color) */
814 for (y = 0; y < 8; y++) {
815 for (x = 0; x < 8; x++)
816 pixel_ptr[x] = bytestream2_get_le16(&s->stream_ptr);
817 pixel_ptr += s->stride;
824 static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s, AVFrame *frame)
827 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
829 /* 16-color block encoding: each 2x2 block is a different color */
830 for (y = 0; y < 8; y += 2) {
831 for (x = 0; x < 8; x += 2) {
834 pixel_ptr[x + s->stride] =
835 pixel_ptr[x + 1 + s->stride] = bytestream2_get_le16(&s->stream_ptr);
837 pixel_ptr += s->stride * 2;
844 static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s, AVFrame *frame)
848 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
850 /* 4-color block encoding: each 4x4 block is a different color */
851 for (y = 0; y < 8; y++) {
853 P[0] = bytestream2_get_le16(&s->stream_ptr);
854 P[1] = bytestream2_get_le16(&s->stream_ptr);
856 for (x = 0; x < 8; x++)
857 pixel_ptr[x] = P[x >> 2];
858 pixel_ptr += s->stride;
865 static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s, AVFrame *frame)
869 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
871 /* 1-color encoding: the whole block is 1 solid color */
872 pix = bytestream2_get_le16(&s->stream_ptr);
874 for (y = 0; y < 8; y++) {
875 for (x = 0; x < 8; x++)
877 pixel_ptr += s->stride;
884 static int (* const ipvideo_decode_block[])(IpvideoContext *s, AVFrame *frame) = {
885 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
886 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
887 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
888 ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
889 ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
890 ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
891 ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
892 ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
895 static int (* const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame) = {
896 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
897 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
898 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
899 ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
900 ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
901 ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
902 ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
903 ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
906 static void ipvideo_decode_opcodes(IpvideoContext *s, AVFrame *frame)
909 unsigned char opcode;
913 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
915 /* this is PAL8, so make the palette available */
916 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
918 s->stride = frame->linesize[0];
920 s->stride = frame->linesize[0] >> 1;
921 s->mv_ptr = s->stream_ptr;
922 bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
924 s->line_inc = s->stride - 8;
925 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
926 + (s->avctx->width - 8) * (1 + s->is_16bpp);
928 init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
929 for (y = 0; y < s->avctx->height; y += 8) {
930 for (x = 0; x < s->avctx->width; x += 8) {
931 if (get_bits_left(&gb) < 4)
933 opcode = get_bits(&gb, 4);
936 " block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
937 x, y, opcode, bytestream2_tell(&s->stream_ptr));
940 s->pixel_ptr = frame->data[0] + x
941 + y*frame->linesize[0];
942 ret = ipvideo_decode_block[opcode](s, frame);
944 s->pixel_ptr = frame->data[0] + x*2
945 + y*frame->linesize[0];
946 ret = ipvideo_decode_block16[opcode](s, frame);
949 av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
950 s->avctx->frame_number, x, y);
955 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
956 av_log(s->avctx, AV_LOG_DEBUG,
957 "decode finished with %d bytes left over\n",
958 bytestream2_get_bytes_left(&s->stream_ptr));
962 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
964 IpvideoContext *s = avctx->priv_data;
968 s->is_16bpp = avctx->bits_per_coded_sample == 16;
969 avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
971 ff_hpeldsp_init(&s->hdsp, avctx->flags);
973 s->last_frame = av_frame_alloc();
974 s->second_last_frame = av_frame_alloc();
975 if (!s->last_frame || !s->second_last_frame) {
976 av_frame_free(&s->last_frame);
977 av_frame_free(&s->second_last_frame);
978 return AVERROR(ENOMEM);
984 static int ipvideo_decode_frame(AVCodecContext *avctx,
985 void *data, int *got_frame,
988 const uint8_t *buf = avpkt->data;
989 int buf_size = avpkt->size;
990 IpvideoContext *s = avctx->priv_data;
991 AVFrame *frame = data;
994 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
995 av_frame_unref(s->last_frame);
996 av_frame_unref(s->second_last_frame);
1000 return AVERROR_INVALIDDATA;
1002 /* decoding map contains 4 bits of information per 8x8 block */
1003 s->decoding_map_size = AV_RL16(avpkt->data);
1005 /* compressed buffer needs to be large enough to at least hold an entire
1007 if (buf_size < s->decoding_map_size + 2)
1011 s->decoding_map = buf + 2;
1012 bytestream2_init(&s->stream_ptr, buf + 2 + s->decoding_map_size,
1013 buf_size - s->decoding_map_size);
1015 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1020 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
1021 if (pal && size == AVPALETTE_SIZE) {
1022 frame->palette_has_changed = 1;
1023 memcpy(s->pal, pal, AVPALETTE_SIZE);
1025 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
1029 ipvideo_decode_opcodes(s, frame);
1033 /* shuffle frames */
1034 av_frame_unref(s->second_last_frame);
1035 FFSWAP(AVFrame*, s->second_last_frame, s->last_frame);
1036 if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
1039 /* report that the buffer was completely consumed */
1043 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1045 IpvideoContext *s = avctx->priv_data;
1047 av_frame_free(&s->last_frame);
1048 av_frame_free(&s->second_last_frame);
1053 AVCodec ff_interplay_video_decoder = {
1054 .name = "interplayvideo",
1055 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
1056 .type = AVMEDIA_TYPE_VIDEO,
1057 .id = AV_CODEC_ID_INTERPLAY_VIDEO,
1058 .priv_data_size = sizeof(IpvideoContext),
1059 .init = ipvideo_decode_init,
1060 .close = ipvideo_decode_end,
1061 .decode = ipvideo_decode_frame,
1062 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,