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;
60 AVFrame *cur_decode_frame;
61 AVFrame *prev_decode_frame;
63 const unsigned char *decoding_map;
64 int decoding_map_size;
65 const unsigned char *skip_map;
69 GetByteContext stream_ptr, mv_ptr;
70 unsigned char *pixel_ptr;
73 int upper_motion_limit_offset;
78 static int copy_from(IpvideoContext *s, AVFrame *src, AVFrame *dst, int delta_x, int delta_y)
80 int width = dst->width;
81 int current_offset = s->pixel_ptr - dst->data[0];
82 int x = (current_offset % dst->linesize[0]) / (1 + s->is_16bpp);
83 int y = current_offset / dst->linesize[0];
84 int dx = delta_x + x - ((delta_x + x >= width) - (delta_x + x < 0)) * width;
85 int dy = delta_y + y + (delta_x + x >= width) - (delta_x + x < 0);
86 int motion_offset = dy * src->linesize[0] + dx * (1 + s->is_16bpp);
88 if (motion_offset < 0) {
89 av_log(s->avctx, AV_LOG_ERROR, "motion offset < 0 (%d)\n", motion_offset);
90 return AVERROR_INVALIDDATA;
91 } else if (motion_offset > s->upper_motion_limit_offset) {
92 av_log(s->avctx, AV_LOG_ERROR, "motion offset above limit (%d >= %d)\n",
93 motion_offset, s->upper_motion_limit_offset);
94 return AVERROR_INVALIDDATA;
97 av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
98 return AVERROR(EINVAL);
100 s->hdsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
101 dst->linesize[0], 8);
105 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s, AVFrame *frame)
107 return copy_from(s, s->last_frame, frame, 0, 0);
110 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s, AVFrame *frame)
112 return copy_from(s, s->second_last_frame, frame, 0, 0);
115 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s, AVFrame *frame)
120 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
122 B = bytestream2_get_byte(&s->stream_ptr);
124 B = bytestream2_get_byte(&s->mv_ptr);
131 x = -14 + ((B - 56) % 29);
132 y = 8 + ((B - 56) / 29);
135 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
136 return copy_from(s, s->second_last_frame, frame, x, y);
139 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s, AVFrame *frame)
144 /* copy 8x8 block from current frame from an up/left block */
146 /* need 1 more byte for motion */
148 B = bytestream2_get_byte(&s->stream_ptr);
150 B = bytestream2_get_byte(&s->mv_ptr);
157 x = -(-14 + ((B - 56) % 29));
158 y = -( 8 + ((B - 56) / 29));
161 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
162 return copy_from(s, frame, frame, x, y);
165 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s, AVFrame *frame)
168 unsigned char B, BL, BH;
170 /* copy a block from the previous frame; need 1 more byte */
172 B = bytestream2_get_byte(&s->stream_ptr);
174 B = bytestream2_get_byte(&s->mv_ptr);
178 BH = (B >> 4) & 0x0F;
182 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
183 return copy_from(s, s->last_frame, frame, x, y);
186 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s, AVFrame *frame)
190 /* copy a block from the previous frame using an expanded range;
191 * need 2 more bytes */
192 x = bytestream2_get_byte(&s->stream_ptr);
193 y = bytestream2_get_byte(&s->stream_ptr);
195 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
196 return copy_from(s, s->last_frame, frame, x, y);
199 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s, AVFrame *frame)
201 /* mystery opcode? skip multiple blocks? */
202 av_log(s->avctx, AV_LOG_ERROR, "Help! Mystery opcode 0x6 seen\n");
208 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s, AVFrame *frame)
214 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
215 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x7\n");
216 return AVERROR_INVALIDDATA;
219 /* 2-color encoding */
220 P[0] = bytestream2_get_byte(&s->stream_ptr);
221 P[1] = bytestream2_get_byte(&s->stream_ptr);
225 /* need 8 more bytes from the stream */
226 for (y = 0; y < 8; y++) {
227 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
228 for (; flags != 1; flags >>= 1)
229 *s->pixel_ptr++ = P[flags & 1];
230 s->pixel_ptr += s->line_inc;
235 /* need 2 more bytes from the stream */
236 flags = bytestream2_get_le16(&s->stream_ptr);
237 for (y = 0; y < 8; y += 2) {
238 for (x = 0; x < 8; x += 2, flags >>= 1) {
240 s->pixel_ptr[x + 1 ] =
241 s->pixel_ptr[x + s->stride] =
242 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
244 s->pixel_ptr += s->stride * 2;
252 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s, AVFrame *frame)
256 unsigned int flags = 0;
258 if (bytestream2_get_bytes_left(&s->stream_ptr) < 12) {
259 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x8\n");
260 return AVERROR_INVALIDDATA;
263 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
264 * either top and bottom or left and right halves */
265 P[0] = bytestream2_get_byte(&s->stream_ptr);
266 P[1] = bytestream2_get_byte(&s->stream_ptr);
269 for (y = 0; y < 16; y++) {
270 // new values for each 4x4 block
273 P[0] = bytestream2_get_byte(&s->stream_ptr);
274 P[1] = bytestream2_get_byte(&s->stream_ptr);
276 flags = bytestream2_get_le16(&s->stream_ptr);
279 for (x = 0; x < 4; x++, flags >>= 1)
280 *s->pixel_ptr++ = P[flags & 1];
281 s->pixel_ptr += s->stride - 4;
282 // switch to right half
283 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
287 flags = bytestream2_get_le32(&s->stream_ptr);
288 P[2] = bytestream2_get_byte(&s->stream_ptr);
289 P[3] = bytestream2_get_byte(&s->stream_ptr);
293 /* vertical split; left & right halves are 2-color encoded */
295 for (y = 0; y < 16; y++) {
296 for (x = 0; x < 4; x++, flags >>= 1)
297 *s->pixel_ptr++ = P[flags & 1];
298 s->pixel_ptr += s->stride - 4;
299 // switch to right half
301 s->pixel_ptr -= 8 * s->stride - 4;
304 flags = bytestream2_get_le32(&s->stream_ptr);
310 /* horizontal split; top & bottom halves are 2-color encoded */
312 for (y = 0; y < 8; y++) {
316 flags = bytestream2_get_le32(&s->stream_ptr);
319 for (x = 0; x < 8; x++, flags >>= 1)
320 *s->pixel_ptr++ = P[flags & 1];
321 s->pixel_ptr += s->line_inc;
330 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s, AVFrame *frame)
335 if (bytestream2_get_bytes_left(&s->stream_ptr) < 8) {
336 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x9\n");
337 return AVERROR_INVALIDDATA;
340 /* 4-color encoding */
341 bytestream2_get_buffer(&s->stream_ptr, P, 4);
346 /* 1 of 4 colors for each pixel, need 16 more bytes */
347 for (y = 0; y < 8; y++) {
348 /* get the next set of 8 2-bit flags */
349 int flags = bytestream2_get_le16(&s->stream_ptr);
350 for (x = 0; x < 8; x++, flags >>= 2)
351 *s->pixel_ptr++ = P[flags & 0x03];
352 s->pixel_ptr += s->line_inc;
358 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
359 flags = bytestream2_get_le32(&s->stream_ptr);
361 for (y = 0; y < 8; y += 2) {
362 for (x = 0; x < 8; x += 2, flags >>= 2) {
364 s->pixel_ptr[x + 1 ] =
365 s->pixel_ptr[x + s->stride] =
366 s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
368 s->pixel_ptr += s->stride * 2;
375 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
376 flags = bytestream2_get_le64(&s->stream_ptr);
378 for (y = 0; y < 8; y++) {
379 for (x = 0; x < 8; x += 2, flags >>= 2) {
381 s->pixel_ptr[x + 1] = P[flags & 0x03];
383 s->pixel_ptr += s->stride;
386 for (y = 0; y < 8; y += 2) {
387 for (x = 0; x < 8; x++, flags >>= 2) {
389 s->pixel_ptr[x + s->stride] = P[flags & 0x03];
391 s->pixel_ptr += s->stride * 2;
400 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s, AVFrame *frame)
406 if (bytestream2_get_bytes_left(&s->stream_ptr) < 16) {
407 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xA\n");
408 return AVERROR_INVALIDDATA;
411 bytestream2_get_buffer(&s->stream_ptr, P, 4);
413 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
414 * either top and bottom or left and right halves */
417 /* 4-color encoding for each quadrant; need 32 bytes */
418 for (y = 0; y < 16; y++) {
419 // new values for each 4x4 block
421 if (y) bytestream2_get_buffer(&s->stream_ptr, P, 4);
422 flags = bytestream2_get_le32(&s->stream_ptr);
425 for (x = 0; x < 4; x++, flags >>= 2)
426 *s->pixel_ptr++ = P[flags & 0x03];
428 s->pixel_ptr += s->stride - 4;
429 // switch to right half
430 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
436 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
438 bytestream2_get_buffer(&s->stream_ptr, P + 4, 4);
441 /* 4-color encoding for either left and right or top and bottom
444 for (y = 0; y < 16; y++) {
445 for (x = 0; x < 4; x++, flags >>= 2)
446 *s->pixel_ptr++ = P[flags & 0x03];
449 s->pixel_ptr += s->stride - 4;
450 // switch to right half
451 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
452 } else if (y & 1) s->pixel_ptr += s->line_inc;
454 // load values for second half
457 flags = bytestream2_get_le64(&s->stream_ptr);
466 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s, AVFrame *frame)
470 /* 64-color encoding (each pixel in block is a different color) */
471 for (y = 0; y < 8; y++) {
472 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
473 s->pixel_ptr += s->stride;
480 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s, AVFrame *frame)
484 /* 16-color block encoding: each 2x2 block is a different color */
485 for (y = 0; y < 8; y += 2) {
486 for (x = 0; x < 8; x += 2) {
488 s->pixel_ptr[x + 1 ] =
489 s->pixel_ptr[x + s->stride] =
490 s->pixel_ptr[x + 1 + s->stride] = bytestream2_get_byte(&s->stream_ptr);
492 s->pixel_ptr += s->stride * 2;
499 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s, AVFrame *frame)
504 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
505 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xD\n");
506 return AVERROR_INVALIDDATA;
509 /* 4-color block encoding: each 4x4 block is a different color */
510 for (y = 0; y < 8; y++) {
512 P[0] = bytestream2_get_byte(&s->stream_ptr);
513 P[1] = bytestream2_get_byte(&s->stream_ptr);
515 memset(s->pixel_ptr, P[0], 4);
516 memset(s->pixel_ptr + 4, P[1], 4);
517 s->pixel_ptr += s->stride;
524 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s, AVFrame *frame)
529 /* 1-color encoding: the whole block is 1 solid color */
530 pix = bytestream2_get_byte(&s->stream_ptr);
532 for (y = 0; y < 8; y++) {
533 memset(s->pixel_ptr, pix, 8);
534 s->pixel_ptr += s->stride;
541 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s, AVFrame *frame)
544 unsigned char sample[2];
546 /* dithered encoding */
547 sample[0] = bytestream2_get_byte(&s->stream_ptr);
548 sample[1] = bytestream2_get_byte(&s->stream_ptr);
550 for (y = 0; y < 8; y++) {
551 for (x = 0; x < 8; x += 2) {
552 *s->pixel_ptr++ = sample[ y & 1 ];
553 *s->pixel_ptr++ = sample[!(y & 1)];
555 s->pixel_ptr += s->line_inc;
562 static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s, AVFrame *frame)
566 /* copy a block from the second last frame using an expanded range */
567 x = bytestream2_get_byte(&s->stream_ptr);
568 y = bytestream2_get_byte(&s->stream_ptr);
570 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
571 return copy_from(s, s->second_last_frame, frame, x, y);
574 static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s, AVFrame *frame)
579 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
581 /* 2-color encoding */
582 P[0] = bytestream2_get_le16(&s->stream_ptr);
583 P[1] = bytestream2_get_le16(&s->stream_ptr);
585 if (!(P[0] & 0x8000)) {
587 for (y = 0; y < 8; y++) {
588 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
589 for (; flags != 1; flags >>= 1)
590 *pixel_ptr++ = P[flags & 1];
591 pixel_ptr += s->line_inc;
596 flags = bytestream2_get_le16(&s->stream_ptr);
597 for (y = 0; y < 8; y += 2) {
598 for (x = 0; x < 8; x += 2, flags >>= 1) {
601 pixel_ptr[x + s->stride] =
602 pixel_ptr[x + 1 + s->stride] = P[flags & 1];
604 pixel_ptr += s->stride * 2;
611 static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s, AVFrame *frame)
615 unsigned int flags = 0;
616 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
618 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
619 * either top and bottom or left and right halves */
620 P[0] = bytestream2_get_le16(&s->stream_ptr);
621 P[1] = bytestream2_get_le16(&s->stream_ptr);
623 if (!(P[0] & 0x8000)) {
625 for (y = 0; y < 16; y++) {
626 // new values for each 4x4 block
629 P[0] = bytestream2_get_le16(&s->stream_ptr);
630 P[1] = bytestream2_get_le16(&s->stream_ptr);
632 flags = bytestream2_get_le16(&s->stream_ptr);
635 for (x = 0; x < 4; x++, flags >>= 1)
636 *pixel_ptr++ = P[flags & 1];
637 pixel_ptr += s->stride - 4;
638 // switch to right half
639 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
644 flags = bytestream2_get_le32(&s->stream_ptr);
645 P[2] = bytestream2_get_le16(&s->stream_ptr);
646 P[3] = bytestream2_get_le16(&s->stream_ptr);
648 if (!(P[2] & 0x8000)) {
650 /* vertical split; left & right halves are 2-color encoded */
652 for (y = 0; y < 16; y++) {
653 for (x = 0; x < 4; x++, flags >>= 1)
654 *pixel_ptr++ = P[flags & 1];
655 pixel_ptr += s->stride - 4;
656 // switch to right half
658 pixel_ptr -= 8 * s->stride - 4;
661 flags = bytestream2_get_le32(&s->stream_ptr);
667 /* horizontal split; top & bottom halves are 2-color encoded */
669 for (y = 0; y < 8; y++) {
673 flags = bytestream2_get_le32(&s->stream_ptr);
676 for (x = 0; x < 8; x++, flags >>= 1)
677 *pixel_ptr++ = P[flags & 1];
678 pixel_ptr += s->line_inc;
687 static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s, AVFrame *frame)
691 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
693 /* 4-color encoding */
694 for (x = 0; x < 4; x++)
695 P[x] = bytestream2_get_le16(&s->stream_ptr);
697 if (!(P[0] & 0x8000)) {
698 if (!(P[2] & 0x8000)) {
700 /* 1 of 4 colors for each pixel */
701 for (y = 0; y < 8; y++) {
702 /* get the next set of 8 2-bit flags */
703 int flags = bytestream2_get_le16(&s->stream_ptr);
704 for (x = 0; x < 8; x++, flags >>= 2)
705 *pixel_ptr++ = P[flags & 0x03];
706 pixel_ptr += s->line_inc;
712 /* 1 of 4 colors for each 2x2 block */
713 flags = bytestream2_get_le32(&s->stream_ptr);
715 for (y = 0; y < 8; y += 2) {
716 for (x = 0; x < 8; x += 2, flags >>= 2) {
719 pixel_ptr[x + s->stride] =
720 pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
722 pixel_ptr += s->stride * 2;
729 /* 1 of 4 colors for each 2x1 or 1x2 block */
730 flags = bytestream2_get_le64(&s->stream_ptr);
731 if (!(P[2] & 0x8000)) {
732 for (y = 0; y < 8; y++) {
733 for (x = 0; x < 8; x += 2, flags >>= 2) {
735 pixel_ptr[x + 1] = P[flags & 0x03];
737 pixel_ptr += s->stride;
740 for (y = 0; y < 8; y += 2) {
741 for (x = 0; x < 8; x++, flags >>= 2) {
743 pixel_ptr[x + s->stride] = P[flags & 0x03];
745 pixel_ptr += s->stride * 2;
754 static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s, AVFrame *frame)
759 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
761 for (x = 0; x < 4; x++)
762 P[x] = bytestream2_get_le16(&s->stream_ptr);
764 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
765 * either top and bottom or left and right halves */
766 if (!(P[0] & 0x8000)) {
768 /* 4-color encoding for each quadrant */
769 for (y = 0; y < 16; y++) {
770 // new values for each 4x4 block
773 for (x = 0; x < 4; x++)
774 P[x] = bytestream2_get_le16(&s->stream_ptr);
775 flags = bytestream2_get_le32(&s->stream_ptr);
778 for (x = 0; x < 4; x++, flags >>= 2)
779 *pixel_ptr++ = P[flags & 0x03];
781 pixel_ptr += s->stride - 4;
782 // switch to right half
783 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
789 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
791 for (x = 4; x < 8; x++)
792 P[x] = bytestream2_get_le16(&s->stream_ptr);
793 vert = !(P[4] & 0x8000);
795 /* 4-color encoding for either left and right or top and bottom
798 for (y = 0; y < 16; y++) {
799 for (x = 0; x < 4; x++, flags >>= 2)
800 *pixel_ptr++ = P[flags & 0x03];
803 pixel_ptr += s->stride - 4;
804 // switch to right half
805 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
806 } else if (y & 1) pixel_ptr += s->line_inc;
808 // load values for second half
811 flags = bytestream2_get_le64(&s->stream_ptr);
820 static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s, AVFrame *frame)
823 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
825 /* 64-color encoding (each pixel in block is a different color) */
826 for (y = 0; y < 8; y++) {
827 for (x = 0; x < 8; x++)
828 pixel_ptr[x] = bytestream2_get_le16(&s->stream_ptr);
829 pixel_ptr += s->stride;
836 static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s, AVFrame *frame)
839 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
841 /* 16-color block encoding: each 2x2 block is a different color */
842 for (y = 0; y < 8; y += 2) {
843 for (x = 0; x < 8; x += 2) {
846 pixel_ptr[x + s->stride] =
847 pixel_ptr[x + 1 + s->stride] = bytestream2_get_le16(&s->stream_ptr);
849 pixel_ptr += s->stride * 2;
856 static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s, AVFrame *frame)
860 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
862 /* 4-color block encoding: each 4x4 block is a different color */
863 for (y = 0; y < 8; y++) {
865 P[0] = bytestream2_get_le16(&s->stream_ptr);
866 P[1] = bytestream2_get_le16(&s->stream_ptr);
868 for (x = 0; x < 8; x++)
869 pixel_ptr[x] = P[x >> 2];
870 pixel_ptr += s->stride;
877 static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s, AVFrame *frame)
881 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
883 /* 1-color encoding: the whole block is 1 solid color */
884 pix = bytestream2_get_le16(&s->stream_ptr);
886 for (y = 0; y < 8; y++) {
887 for (x = 0; x < 8; x++)
889 pixel_ptr += s->stride;
896 static int (* const ipvideo_decode_block[])(IpvideoContext *s, AVFrame *frame) = {
897 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
898 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
899 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
900 ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
901 ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
902 ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
903 ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
904 ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
907 static int (* const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame) = {
908 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
909 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
910 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
911 ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
912 ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
913 ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
914 ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
915 ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
918 static void ipvideo_format_06_firstpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
923 for (line = 0; line < 8; ++line) {
924 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
925 s->pixel_ptr += s->stride;
928 /* Don't try to copy second_last_frame data on the first frames */
929 if (s->avctx->frame_number > 2)
930 copy_from(s, s->second_last_frame, frame, 0, 0);
934 static void ipvideo_format_06_secondpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
939 off_x = ((uint16_t)opcode - 0xC000) % frame->width;
940 off_y = ((uint16_t)opcode - 0xC000) / frame->width;
941 copy_from(s, s->last_frame, frame, off_x, off_y);
942 } else if (opcode > 0) {
943 off_x = ((uint16_t)opcode - 0x4000) % frame->width;
944 off_y = ((uint16_t)opcode - 0x4000) / frame->width;
945 copy_from(s, frame, frame, off_x, off_y);
949 static void (* const ipvideo_format_06_passes[])(IpvideoContext *s, AVFrame *frame, int16_t op) = {
950 ipvideo_format_06_firstpass, ipvideo_format_06_secondpass,
953 static void ipvideo_decode_format_06_opcodes(IpvideoContext *s, AVFrame *frame)
957 GetByteContext decoding_map_ptr;
959 /* this is PAL8, so make the palette available */
960 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
961 s->stride = frame->linesize[0];
963 s->line_inc = s->stride - 8;
964 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
965 + (s->avctx->width - 8) * (1 + s->is_16bpp);
967 bytestream2_init(&decoding_map_ptr, s->decoding_map, s->decoding_map_size);
969 for (pass = 0; pass < 2; ++pass) {
970 bytestream2_seek(&decoding_map_ptr, 0, SEEK_SET);
971 for (y = 0; y < s->avctx->height; y += 8) {
972 for (x = 0; x < s->avctx->width; x += 8) {
973 opcode = bytestream2_get_le16(&decoding_map_ptr);
976 " block @ (%3d, %3d): opcode 0x%X, data ptr offset %d\n",
977 x, y, opcode, bytestream2_tell(&s->stream_ptr));
979 s->pixel_ptr = frame->data[0] + x + y * frame->linesize[0];
980 ipvideo_format_06_passes[pass](s, frame, opcode);
985 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
986 av_log(s->avctx, AV_LOG_DEBUG,
987 "decode finished with %d bytes left over\n",
988 bytestream2_get_bytes_left(&s->stream_ptr));
992 static void ipvideo_format_10_firstpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
997 for (line = 0; line < 8; ++line) {
998 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
999 s->pixel_ptr += s->stride;
1004 static void ipvideo_format_10_secondpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
1009 off_x = ((uint16_t)opcode - 0xC000) % s->cur_decode_frame->width;
1010 off_y = ((uint16_t)opcode - 0xC000) / s->cur_decode_frame->width;
1011 copy_from(s, s->prev_decode_frame, s->cur_decode_frame, off_x, off_y);
1012 } else if (opcode > 0) {
1013 off_x = ((uint16_t)opcode - 0x4000) % s->cur_decode_frame->width;
1014 off_y = ((uint16_t)opcode - 0x4000) / s->cur_decode_frame->width;
1015 copy_from(s, s->cur_decode_frame, s->cur_decode_frame, off_x, off_y);
1019 static void (* const ipvideo_format_10_passes[])(IpvideoContext *s, AVFrame *frame, int16_t op) = {
1020 ipvideo_format_10_firstpass, ipvideo_format_10_secondpass,
1023 static void ipvideo_decode_format_10_opcodes(IpvideoContext *s, AVFrame *frame)
1025 int pass, x, y, changed_block;
1026 int16_t opcode, skip;
1027 GetByteContext decoding_map_ptr;
1028 GetByteContext skip_map_ptr;
1030 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
1032 /* this is PAL8, so make the palette available */
1033 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
1034 s->stride = frame->linesize[0];
1036 s->line_inc = s->stride - 8;
1037 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
1038 + (s->avctx->width - 8) * (1 + s->is_16bpp);
1040 bytestream2_init(&decoding_map_ptr, s->decoding_map, s->decoding_map_size);
1041 bytestream2_init(&skip_map_ptr, s->skip_map, s->skip_map_size);
1043 for (pass = 0; pass < 2; ++pass) {
1044 bytestream2_seek(&decoding_map_ptr, 0, SEEK_SET);
1045 bytestream2_seek(&skip_map_ptr, 0, SEEK_SET);
1046 skip = bytestream2_get_le16(&skip_map_ptr);
1048 for (y = 0; y < s->avctx->height; y += 8) {
1049 for (x = 0; x < s->avctx->width; x += 8) {
1050 s->pixel_ptr = s->cur_decode_frame->data[0] + x + y * s->cur_decode_frame->linesize[0];
1053 if (skip != -0x8000 && skip) {
1054 opcode = bytestream2_get_le16(&decoding_map_ptr);
1055 ipvideo_format_10_passes[pass](s, frame, opcode);
1058 if (bytestream2_get_bytes_left(&skip_map_ptr) < 2)
1060 skip = bytestream2_get_le16(&skip_map_ptr);
1067 bytestream2_seek(&skip_map_ptr, 0, SEEK_SET);
1068 skip = bytestream2_get_le16(&skip_map_ptr);
1069 for (y = 0; y < s->avctx->height; y += 8) {
1070 for (x = 0; x < s->avctx->width; x += 8) {
1072 s->pixel_ptr = frame->data[0] + x + y*frame->linesize[0];
1075 if (skip != -0x8000 && skip) {
1079 if (bytestream2_get_bytes_left(&skip_map_ptr) < 2)
1081 skip = bytestream2_get_le16(&skip_map_ptr);
1084 if (changed_block) {
1085 copy_from(s, s->cur_decode_frame, frame, 0, 0);
1087 /* Don't try to copy last_frame data on the first frame */
1088 if (s->avctx->frame_number)
1089 copy_from(s, s->last_frame, frame, 0, 0);
1095 FFSWAP(AVFrame*, s->prev_decode_frame, s->cur_decode_frame);
1097 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
1098 av_log(s->avctx, AV_LOG_DEBUG,
1099 "decode finished with %d bytes left over\n",
1100 bytestream2_get_bytes_left(&s->stream_ptr));
1104 static void ipvideo_decode_format_11_opcodes(IpvideoContext *s, AVFrame *frame)
1107 unsigned char opcode;
1111 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
1113 /* this is PAL8, so make the palette available */
1114 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
1116 s->stride = frame->linesize[0];
1118 s->stride = frame->linesize[0] >> 1;
1119 s->mv_ptr = s->stream_ptr;
1120 bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
1122 s->line_inc = s->stride - 8;
1123 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
1124 + (s->avctx->width - 8) * (1 + s->is_16bpp);
1126 init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
1127 for (y = 0; y < s->avctx->height; y += 8) {
1128 for (x = 0; x < s->avctx->width; x += 8) {
1129 if (get_bits_left(&gb) < 4)
1131 opcode = get_bits(&gb, 4);
1134 " block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
1135 x, y, opcode, bytestream2_tell(&s->stream_ptr));
1138 s->pixel_ptr = frame->data[0] + x
1139 + y*frame->linesize[0];
1140 ret = ipvideo_decode_block[opcode](s, frame);
1142 s->pixel_ptr = frame->data[0] + x*2
1143 + y*frame->linesize[0];
1144 ret = ipvideo_decode_block16[opcode](s, frame);
1147 av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
1148 s->avctx->frame_number, x, y);
1153 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
1154 av_log(s->avctx, AV_LOG_DEBUG,
1155 "decode finished with %d bytes left over\n",
1156 bytestream2_get_bytes_left(&s->stream_ptr));
1160 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
1162 IpvideoContext *s = avctx->priv_data;
1166 s->is_16bpp = avctx->bits_per_coded_sample == 16;
1167 avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
1169 ff_hpeldsp_init(&s->hdsp, avctx->flags);
1171 s->last_frame = av_frame_alloc();
1172 s->second_last_frame = av_frame_alloc();
1173 s->cur_decode_frame = av_frame_alloc();
1174 s->prev_decode_frame = av_frame_alloc();
1175 if (!s->last_frame || !s->second_last_frame ||
1176 !s->cur_decode_frame || !s->prev_decode_frame) {
1177 return AVERROR(ENOMEM);
1180 s->cur_decode_frame->width = avctx->width;
1181 s->prev_decode_frame->width = avctx->width;
1182 s->cur_decode_frame->height = avctx->height;
1183 s->prev_decode_frame->height = avctx->height;
1184 s->cur_decode_frame->format = avctx->pix_fmt;
1185 s->prev_decode_frame->format = avctx->pix_fmt;
1190 static int ipvideo_decode_frame(AVCodecContext *avctx,
1191 void *data, int *got_frame,
1194 const uint8_t *buf = avpkt->data;
1195 int buf_size = avpkt->size;
1196 IpvideoContext *s = avctx->priv_data;
1197 AVFrame *frame = data;
1201 int video_data_size;
1203 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
1204 av_frame_unref(s->last_frame);
1205 av_frame_unref(s->second_last_frame);
1206 av_frame_unref(s->cur_decode_frame);
1207 av_frame_unref(s->prev_decode_frame);
1210 if (!s->cur_decode_frame->data[0]) {
1211 ret = ff_get_buffer(avctx, s->cur_decode_frame, 0);
1215 ret = ff_get_buffer(avctx, s->prev_decode_frame, 0);
1217 av_frame_unref(s->cur_decode_frame);
1223 return AVERROR_INVALIDDATA;
1225 frame_format = AV_RL8(buf);
1226 send_buffer = AV_RL8(buf + 1);
1227 video_data_size = AV_RL16(buf + 2);
1228 s->decoding_map_size = AV_RL16(buf + 4);
1229 s->skip_map_size = AV_RL16(buf + 6);
1231 switch (frame_format) {
1233 if (s->decoding_map_size) {
1234 av_log(avctx, AV_LOG_ERROR, "Decoding map for format 0x06\n");
1235 return AVERROR_INVALIDDATA;
1238 if (s->skip_map_size) {
1239 av_log(avctx, AV_LOG_ERROR, "Skip map for format 0x06\n");
1240 return AVERROR_INVALIDDATA;
1244 av_log(avctx, AV_LOG_ERROR, "Video format 0x06 does not support 16bpp movies\n");
1245 return AVERROR_INVALIDDATA;
1248 /* Decoding map for 0x06 frame format is at the top of pixeldata */
1249 s->decoding_map_size = ((s->avctx->width / 8) * (s->avctx->height / 8)) * 2;
1250 s->decoding_map = buf + 8 + 14; /* 14 bits of op data */
1251 video_data_size -= s->decoding_map_size + 14;
1252 if (video_data_size <= 0 || s->decoding_map_size == 0)
1253 return AVERROR_INVALIDDATA;
1255 if (buf_size < 8 + s->decoding_map_size + 14 + video_data_size)
1256 return AVERROR_INVALIDDATA;
1258 bytestream2_init(&s->stream_ptr, buf + 8 + s->decoding_map_size + 14, video_data_size);
1263 if (! s->decoding_map_size) {
1264 av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x10\n");
1265 return AVERROR_INVALIDDATA;
1268 if (! s->skip_map_size) {
1269 av_log(avctx, AV_LOG_ERROR, "Empty skip map for format 0x10\n");
1270 return AVERROR_INVALIDDATA;
1274 av_log(avctx, AV_LOG_ERROR, "Video format 0x10 does not support 16bpp movies\n");
1275 return AVERROR_INVALIDDATA;
1278 if (buf_size < 8 + video_data_size + s->decoding_map_size + s->skip_map_size)
1279 return AVERROR_INVALIDDATA;
1281 bytestream2_init(&s->stream_ptr, buf + 8, video_data_size);
1282 s->decoding_map = buf + 8 + video_data_size;
1283 s->skip_map = buf + 8 + video_data_size + s->decoding_map_size;
1288 if (! s->decoding_map_size) {
1289 av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x11\n");
1290 return AVERROR_INVALIDDATA;
1293 if (s->skip_map_size) {
1294 av_log(avctx, AV_LOG_ERROR, "Skip map for format 0x11\n");
1295 return AVERROR_INVALIDDATA;
1298 if (buf_size < 8 + video_data_size + s->decoding_map_size)
1299 return AVERROR_INVALIDDATA;
1301 bytestream2_init(&s->stream_ptr, buf + 8, video_data_size);
1302 s->decoding_map = buf + 8 + video_data_size;
1307 av_log(avctx, AV_LOG_ERROR, "Frame type 0x%02X unsupported\n", frame_format);
1310 /* ensure we can't overread the packet */
1311 if (buf_size < 8 + s->decoding_map_size + video_data_size + s->skip_map_size) {
1312 av_log(avctx, AV_LOG_ERROR, "Invalid IP packet size\n");
1313 return AVERROR_INVALIDDATA;
1316 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1321 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
1322 if (pal && size == AVPALETTE_SIZE) {
1323 frame->palette_has_changed = 1;
1324 memcpy(s->pal, pal, AVPALETTE_SIZE);
1326 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
1330 switch (frame_format) {
1332 ipvideo_decode_format_06_opcodes(s, frame);
1335 ipvideo_decode_format_10_opcodes(s, frame);
1338 ipvideo_decode_format_11_opcodes(s, frame);
1342 *got_frame = send_buffer;
1344 /* shuffle frames */
1345 av_frame_unref(s->second_last_frame);
1346 FFSWAP(AVFrame*, s->second_last_frame, s->last_frame);
1347 if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
1350 /* report that the buffer was completely consumed */
1354 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1356 IpvideoContext *s = avctx->priv_data;
1358 av_frame_free(&s->last_frame);
1359 av_frame_free(&s->second_last_frame);
1360 av_frame_free(&s->cur_decode_frame);
1361 av_frame_free(&s->prev_decode_frame);
1366 AVCodec ff_interplay_video_decoder = {
1367 .name = "interplayvideo",
1368 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
1369 .type = AVMEDIA_TYPE_VIDEO,
1370 .id = AV_CODEC_ID_INTERPLAY_VIDEO,
1371 .priv_data_size = sizeof(IpvideoContext),
1372 .init = ipvideo_decode_init,
1373 .close = ipvideo_decode_end,
1374 .decode = ipvideo_decode_frame,
1375 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,
1376 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,