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 current_offset = s->pixel_ptr - dst->data[0];
81 int motion_offset = current_offset + delta_y * dst->linesize[0]
82 + delta_x * (1 + s->is_16bpp);
83 if (motion_offset < 0) {
84 av_log(s->avctx, AV_LOG_ERROR, "motion offset < 0 (%d)\n", motion_offset);
85 return AVERROR_INVALIDDATA;
86 } else if (motion_offset > s->upper_motion_limit_offset) {
87 av_log(s->avctx, AV_LOG_ERROR, "motion offset above limit (%d >= %d)\n",
88 motion_offset, s->upper_motion_limit_offset);
89 return AVERROR_INVALIDDATA;
92 av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
93 return AVERROR(EINVAL);
95 s->hdsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
100 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s, AVFrame *frame)
102 return copy_from(s, s->last_frame, frame, 0, 0);
105 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s, AVFrame *frame)
107 return copy_from(s, s->second_last_frame, frame, 0, 0);
110 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s, AVFrame *frame)
115 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
117 B = bytestream2_get_byte(&s->stream_ptr);
119 B = bytestream2_get_byte(&s->mv_ptr);
126 x = -14 + ((B - 56) % 29);
127 y = 8 + ((B - 56) / 29);
130 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
131 return copy_from(s, s->second_last_frame, frame, x, y);
134 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s, AVFrame *frame)
139 /* copy 8x8 block from current frame from an up/left block */
141 /* need 1 more byte for motion */
143 B = bytestream2_get_byte(&s->stream_ptr);
145 B = bytestream2_get_byte(&s->mv_ptr);
152 x = -(-14 + ((B - 56) % 29));
153 y = -( 8 + ((B - 56) / 29));
156 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
157 return copy_from(s, frame, frame, x, y);
160 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s, AVFrame *frame)
163 unsigned char B, BL, BH;
165 /* copy a block from the previous frame; need 1 more byte */
167 B = bytestream2_get_byte(&s->stream_ptr);
169 B = bytestream2_get_byte(&s->mv_ptr);
173 BH = (B >> 4) & 0x0F;
177 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
178 return copy_from(s, s->last_frame, frame, x, y);
181 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s, AVFrame *frame)
185 /* copy a block from the previous frame using an expanded range;
186 * need 2 more bytes */
187 x = bytestream2_get_byte(&s->stream_ptr);
188 y = bytestream2_get_byte(&s->stream_ptr);
190 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
191 return copy_from(s, s->last_frame, frame, x, y);
194 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s, AVFrame *frame)
196 /* mystery opcode? skip multiple blocks? */
197 av_log(s->avctx, AV_LOG_ERROR, "Help! Mystery opcode 0x6 seen\n");
203 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s, AVFrame *frame)
209 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
210 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x7\n");
211 return AVERROR_INVALIDDATA;
214 /* 2-color encoding */
215 P[0] = bytestream2_get_byte(&s->stream_ptr);
216 P[1] = bytestream2_get_byte(&s->stream_ptr);
220 /* need 8 more bytes from the stream */
221 for (y = 0; y < 8; y++) {
222 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
223 for (; flags != 1; flags >>= 1)
224 *s->pixel_ptr++ = P[flags & 1];
225 s->pixel_ptr += s->line_inc;
230 /* need 2 more bytes from the stream */
231 flags = bytestream2_get_le16(&s->stream_ptr);
232 for (y = 0; y < 8; y += 2) {
233 for (x = 0; x < 8; x += 2, flags >>= 1) {
235 s->pixel_ptr[x + 1 ] =
236 s->pixel_ptr[x + s->stride] =
237 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
239 s->pixel_ptr += s->stride * 2;
247 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s, AVFrame *frame)
251 unsigned int flags = 0;
253 if (bytestream2_get_bytes_left(&s->stream_ptr) < 12) {
254 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x8\n");
255 return AVERROR_INVALIDDATA;
258 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
259 * either top and bottom or left and right halves */
260 P[0] = bytestream2_get_byte(&s->stream_ptr);
261 P[1] = bytestream2_get_byte(&s->stream_ptr);
264 for (y = 0; y < 16; y++) {
265 // new values for each 4x4 block
268 P[0] = bytestream2_get_byte(&s->stream_ptr);
269 P[1] = bytestream2_get_byte(&s->stream_ptr);
271 flags = bytestream2_get_le16(&s->stream_ptr);
274 for (x = 0; x < 4; x++, flags >>= 1)
275 *s->pixel_ptr++ = P[flags & 1];
276 s->pixel_ptr += s->stride - 4;
277 // switch to right half
278 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
282 flags = bytestream2_get_le32(&s->stream_ptr);
283 P[2] = bytestream2_get_byte(&s->stream_ptr);
284 P[3] = bytestream2_get_byte(&s->stream_ptr);
288 /* vertical split; left & right halves are 2-color encoded */
290 for (y = 0; y < 16; y++) {
291 for (x = 0; x < 4; x++, flags >>= 1)
292 *s->pixel_ptr++ = P[flags & 1];
293 s->pixel_ptr += s->stride - 4;
294 // switch to right half
296 s->pixel_ptr -= 8 * s->stride - 4;
299 flags = bytestream2_get_le32(&s->stream_ptr);
305 /* horizontal split; top & bottom halves are 2-color encoded */
307 for (y = 0; y < 8; y++) {
311 flags = bytestream2_get_le32(&s->stream_ptr);
314 for (x = 0; x < 8; x++, flags >>= 1)
315 *s->pixel_ptr++ = P[flags & 1];
316 s->pixel_ptr += s->line_inc;
325 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s, AVFrame *frame)
330 if (bytestream2_get_bytes_left(&s->stream_ptr) < 8) {
331 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x9\n");
332 return AVERROR_INVALIDDATA;
335 /* 4-color encoding */
336 bytestream2_get_buffer(&s->stream_ptr, P, 4);
341 /* 1 of 4 colors for each pixel, need 16 more bytes */
342 for (y = 0; y < 8; y++) {
343 /* get the next set of 8 2-bit flags */
344 int flags = bytestream2_get_le16(&s->stream_ptr);
345 for (x = 0; x < 8; x++, flags >>= 2)
346 *s->pixel_ptr++ = P[flags & 0x03];
347 s->pixel_ptr += s->line_inc;
353 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
354 flags = bytestream2_get_le32(&s->stream_ptr);
356 for (y = 0; y < 8; y += 2) {
357 for (x = 0; x < 8; x += 2, flags >>= 2) {
359 s->pixel_ptr[x + 1 ] =
360 s->pixel_ptr[x + s->stride] =
361 s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
363 s->pixel_ptr += s->stride * 2;
370 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
371 flags = bytestream2_get_le64(&s->stream_ptr);
373 for (y = 0; y < 8; y++) {
374 for (x = 0; x < 8; x += 2, flags >>= 2) {
376 s->pixel_ptr[x + 1] = P[flags & 0x03];
378 s->pixel_ptr += s->stride;
381 for (y = 0; y < 8; y += 2) {
382 for (x = 0; x < 8; x++, flags >>= 2) {
384 s->pixel_ptr[x + s->stride] = P[flags & 0x03];
386 s->pixel_ptr += s->stride * 2;
395 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s, AVFrame *frame)
401 if (bytestream2_get_bytes_left(&s->stream_ptr) < 16) {
402 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xA\n");
403 return AVERROR_INVALIDDATA;
406 bytestream2_get_buffer(&s->stream_ptr, P, 4);
408 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
409 * either top and bottom or left and right halves */
412 /* 4-color encoding for each quadrant; need 32 bytes */
413 for (y = 0; y < 16; y++) {
414 // new values for each 4x4 block
416 if (y) bytestream2_get_buffer(&s->stream_ptr, P, 4);
417 flags = bytestream2_get_le32(&s->stream_ptr);
420 for (x = 0; x < 4; x++, flags >>= 2)
421 *s->pixel_ptr++ = P[flags & 0x03];
423 s->pixel_ptr += s->stride - 4;
424 // switch to right half
425 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
431 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
433 bytestream2_get_buffer(&s->stream_ptr, P + 4, 4);
436 /* 4-color encoding for either left and right or top and bottom
439 for (y = 0; y < 16; y++) {
440 for (x = 0; x < 4; x++, flags >>= 2)
441 *s->pixel_ptr++ = P[flags & 0x03];
444 s->pixel_ptr += s->stride - 4;
445 // switch to right half
446 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
447 } else if (y & 1) s->pixel_ptr += s->line_inc;
449 // load values for second half
452 flags = bytestream2_get_le64(&s->stream_ptr);
461 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s, AVFrame *frame)
465 /* 64-color encoding (each pixel in block is a different color) */
466 for (y = 0; y < 8; y++) {
467 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
468 s->pixel_ptr += s->stride;
475 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s, AVFrame *frame)
479 /* 16-color block encoding: each 2x2 block is a different color */
480 for (y = 0; y < 8; y += 2) {
481 for (x = 0; x < 8; x += 2) {
483 s->pixel_ptr[x + 1 ] =
484 s->pixel_ptr[x + s->stride] =
485 s->pixel_ptr[x + 1 + s->stride] = bytestream2_get_byte(&s->stream_ptr);
487 s->pixel_ptr += s->stride * 2;
494 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s, AVFrame *frame)
499 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
500 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xD\n");
501 return AVERROR_INVALIDDATA;
504 /* 4-color block encoding: each 4x4 block is a different color */
505 for (y = 0; y < 8; y++) {
507 P[0] = bytestream2_get_byte(&s->stream_ptr);
508 P[1] = bytestream2_get_byte(&s->stream_ptr);
510 memset(s->pixel_ptr, P[0], 4);
511 memset(s->pixel_ptr + 4, P[1], 4);
512 s->pixel_ptr += s->stride;
519 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s, AVFrame *frame)
524 /* 1-color encoding: the whole block is 1 solid color */
525 pix = bytestream2_get_byte(&s->stream_ptr);
527 for (y = 0; y < 8; y++) {
528 memset(s->pixel_ptr, pix, 8);
529 s->pixel_ptr += s->stride;
536 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s, AVFrame *frame)
539 unsigned char sample[2];
541 /* dithered encoding */
542 sample[0] = bytestream2_get_byte(&s->stream_ptr);
543 sample[1] = bytestream2_get_byte(&s->stream_ptr);
545 for (y = 0; y < 8; y++) {
546 for (x = 0; x < 8; x += 2) {
547 *s->pixel_ptr++ = sample[ y & 1 ];
548 *s->pixel_ptr++ = sample[!(y & 1)];
550 s->pixel_ptr += s->line_inc;
557 static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s, AVFrame *frame)
561 /* copy a block from the second last frame using an expanded range */
562 x = bytestream2_get_byte(&s->stream_ptr);
563 y = bytestream2_get_byte(&s->stream_ptr);
565 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
566 return copy_from(s, s->second_last_frame, frame, x, y);
569 static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s, AVFrame *frame)
574 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
576 /* 2-color encoding */
577 P[0] = bytestream2_get_le16(&s->stream_ptr);
578 P[1] = bytestream2_get_le16(&s->stream_ptr);
580 if (!(P[0] & 0x8000)) {
582 for (y = 0; y < 8; y++) {
583 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
584 for (; flags != 1; flags >>= 1)
585 *pixel_ptr++ = P[flags & 1];
586 pixel_ptr += s->line_inc;
591 flags = bytestream2_get_le16(&s->stream_ptr);
592 for (y = 0; y < 8; y += 2) {
593 for (x = 0; x < 8; x += 2, flags >>= 1) {
596 pixel_ptr[x + s->stride] =
597 pixel_ptr[x + 1 + s->stride] = P[flags & 1];
599 pixel_ptr += s->stride * 2;
606 static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s, AVFrame *frame)
610 unsigned int flags = 0;
611 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
613 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
614 * either top and bottom or left and right halves */
615 P[0] = bytestream2_get_le16(&s->stream_ptr);
616 P[1] = bytestream2_get_le16(&s->stream_ptr);
618 if (!(P[0] & 0x8000)) {
620 for (y = 0; y < 16; y++) {
621 // new values for each 4x4 block
624 P[0] = bytestream2_get_le16(&s->stream_ptr);
625 P[1] = bytestream2_get_le16(&s->stream_ptr);
627 flags = bytestream2_get_le16(&s->stream_ptr);
630 for (x = 0; x < 4; x++, flags >>= 1)
631 *pixel_ptr++ = P[flags & 1];
632 pixel_ptr += s->stride - 4;
633 // switch to right half
634 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
639 flags = bytestream2_get_le32(&s->stream_ptr);
640 P[2] = bytestream2_get_le16(&s->stream_ptr);
641 P[3] = bytestream2_get_le16(&s->stream_ptr);
643 if (!(P[2] & 0x8000)) {
645 /* vertical split; left & right halves are 2-color encoded */
647 for (y = 0; y < 16; y++) {
648 for (x = 0; x < 4; x++, flags >>= 1)
649 *pixel_ptr++ = P[flags & 1];
650 pixel_ptr += s->stride - 4;
651 // switch to right half
653 pixel_ptr -= 8 * s->stride - 4;
656 flags = bytestream2_get_le32(&s->stream_ptr);
662 /* horizontal split; top & bottom halves are 2-color encoded */
664 for (y = 0; y < 8; y++) {
668 flags = bytestream2_get_le32(&s->stream_ptr);
671 for (x = 0; x < 8; x++, flags >>= 1)
672 *pixel_ptr++ = P[flags & 1];
673 pixel_ptr += s->line_inc;
682 static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s, AVFrame *frame)
686 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
688 /* 4-color encoding */
689 for (x = 0; x < 4; x++)
690 P[x] = bytestream2_get_le16(&s->stream_ptr);
692 if (!(P[0] & 0x8000)) {
693 if (!(P[2] & 0x8000)) {
695 /* 1 of 4 colors for each pixel */
696 for (y = 0; y < 8; y++) {
697 /* get the next set of 8 2-bit flags */
698 int flags = bytestream2_get_le16(&s->stream_ptr);
699 for (x = 0; x < 8; x++, flags >>= 2)
700 *pixel_ptr++ = P[flags & 0x03];
701 pixel_ptr += s->line_inc;
707 /* 1 of 4 colors for each 2x2 block */
708 flags = bytestream2_get_le32(&s->stream_ptr);
710 for (y = 0; y < 8; y += 2) {
711 for (x = 0; x < 8; x += 2, flags >>= 2) {
714 pixel_ptr[x + s->stride] =
715 pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
717 pixel_ptr += s->stride * 2;
724 /* 1 of 4 colors for each 2x1 or 1x2 block */
725 flags = bytestream2_get_le64(&s->stream_ptr);
726 if (!(P[2] & 0x8000)) {
727 for (y = 0; y < 8; y++) {
728 for (x = 0; x < 8; x += 2, flags >>= 2) {
730 pixel_ptr[x + 1] = P[flags & 0x03];
732 pixel_ptr += s->stride;
735 for (y = 0; y < 8; y += 2) {
736 for (x = 0; x < 8; x++, flags >>= 2) {
738 pixel_ptr[x + s->stride] = P[flags & 0x03];
740 pixel_ptr += s->stride * 2;
749 static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s, AVFrame *frame)
754 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
756 for (x = 0; x < 4; x++)
757 P[x] = bytestream2_get_le16(&s->stream_ptr);
759 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
760 * either top and bottom or left and right halves */
761 if (!(P[0] & 0x8000)) {
763 /* 4-color encoding for each quadrant */
764 for (y = 0; y < 16; y++) {
765 // new values for each 4x4 block
768 for (x = 0; x < 4; x++)
769 P[x] = bytestream2_get_le16(&s->stream_ptr);
770 flags = bytestream2_get_le32(&s->stream_ptr);
773 for (x = 0; x < 4; x++, flags >>= 2)
774 *pixel_ptr++ = P[flags & 0x03];
776 pixel_ptr += s->stride - 4;
777 // switch to right half
778 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
784 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
786 for (x = 4; x < 8; x++)
787 P[x] = bytestream2_get_le16(&s->stream_ptr);
788 vert = !(P[4] & 0x8000);
790 /* 4-color encoding for either left and right or top and bottom
793 for (y = 0; y < 16; y++) {
794 for (x = 0; x < 4; x++, flags >>= 2)
795 *pixel_ptr++ = P[flags & 0x03];
798 pixel_ptr += s->stride - 4;
799 // switch to right half
800 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
801 } else if (y & 1) pixel_ptr += s->line_inc;
803 // load values for second half
806 flags = bytestream2_get_le64(&s->stream_ptr);
815 static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s, AVFrame *frame)
818 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
820 /* 64-color encoding (each pixel in block is a different color) */
821 for (y = 0; y < 8; y++) {
822 for (x = 0; x < 8; x++)
823 pixel_ptr[x] = bytestream2_get_le16(&s->stream_ptr);
824 pixel_ptr += s->stride;
831 static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s, AVFrame *frame)
834 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
836 /* 16-color block encoding: each 2x2 block is a different color */
837 for (y = 0; y < 8; y += 2) {
838 for (x = 0; x < 8; x += 2) {
841 pixel_ptr[x + s->stride] =
842 pixel_ptr[x + 1 + s->stride] = bytestream2_get_le16(&s->stream_ptr);
844 pixel_ptr += s->stride * 2;
851 static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s, AVFrame *frame)
855 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
857 /* 4-color block encoding: each 4x4 block is a different color */
858 for (y = 0; y < 8; y++) {
860 P[0] = bytestream2_get_le16(&s->stream_ptr);
861 P[1] = bytestream2_get_le16(&s->stream_ptr);
863 for (x = 0; x < 8; x++)
864 pixel_ptr[x] = P[x >> 2];
865 pixel_ptr += s->stride;
872 static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s, AVFrame *frame)
876 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
878 /* 1-color encoding: the whole block is 1 solid color */
879 pix = bytestream2_get_le16(&s->stream_ptr);
881 for (y = 0; y < 8; y++) {
882 for (x = 0; x < 8; x++)
884 pixel_ptr += s->stride;
891 static int (* const ipvideo_decode_block[])(IpvideoContext *s, AVFrame *frame) = {
892 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
893 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
894 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
895 ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
896 ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
897 ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
898 ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
899 ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
902 static int (* const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame) = {
903 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
904 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
905 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
906 ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
907 ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
908 ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
909 ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
910 ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
913 static void ipvideo_format_06_firstpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
918 for (line = 0; line < 8; ++line) {
919 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
920 s->pixel_ptr += s->stride;
923 /* Don't try to copy second_last_frame data on the first frames */
924 if (s->avctx->frame_number > 2)
925 copy_from(s, s->second_last_frame, frame, 0, 0);
929 static void ipvideo_format_06_secondpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
934 off_x = ((uint16_t)opcode - 0xC000) % frame->linesize[0];
935 off_y = ((uint16_t)opcode - 0xC000) / frame->linesize[0];
936 copy_from(s, s->last_frame, frame, off_x, off_y);
937 } else if (opcode > 0) {
938 off_x = ((uint16_t)opcode - 0x4000) % frame->linesize[0];
939 off_y = ((uint16_t)opcode - 0x4000) / frame->linesize[0];
940 copy_from(s, frame, frame, off_x, off_y);
944 static void (* const ipvideo_format_06_passes[])(IpvideoContext *s, AVFrame *frame, int16_t op) = {
945 ipvideo_format_06_firstpass, ipvideo_format_06_secondpass,
948 static void ipvideo_decode_format_06_opcodes(IpvideoContext *s, AVFrame *frame)
952 GetByteContext decoding_map_ptr;
954 /* this is PAL8, so make the palette available */
955 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
956 s->stride = frame->linesize[0];
958 s->line_inc = s->stride - 8;
959 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
960 + (s->avctx->width - 8) * (1 + s->is_16bpp);
962 bytestream2_init(&decoding_map_ptr, s->decoding_map, s->decoding_map_size);
964 for (pass = 0; pass < 2; ++pass) {
965 bytestream2_seek(&decoding_map_ptr, 0, SEEK_SET);
966 for (y = 0; y < s->avctx->height; y += 8) {
967 for (x = 0; x < s->avctx->width; x += 8) {
968 opcode = bytestream2_get_le16(&decoding_map_ptr);
971 " block @ (%3d, %3d): opcode 0x%X, data ptr offset %d\n",
972 x, y, opcode, bytestream2_tell(&s->stream_ptr));
974 s->pixel_ptr = frame->data[0] + x + y * frame->linesize[0];
975 ipvideo_format_06_passes[pass](s, frame, opcode);
980 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
981 av_log(s->avctx, AV_LOG_DEBUG,
982 "decode finished with %d bytes left over\n",
983 bytestream2_get_bytes_left(&s->stream_ptr));
987 static void ipvideo_format_10_firstpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
992 for (line = 0; line < 8; ++line) {
993 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
994 s->pixel_ptr += s->stride;
999 static void ipvideo_format_10_secondpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
1004 off_x = ((uint16_t)opcode - 0xC000) % s->cur_decode_frame->linesize[0];
1005 off_y = ((uint16_t)opcode - 0xC000) / s->cur_decode_frame->linesize[0];
1006 copy_from(s, s->prev_decode_frame, s->cur_decode_frame, off_x, off_y);
1007 } else if (opcode > 0) {
1008 off_x = ((uint16_t)opcode - 0x4000) % s->cur_decode_frame->linesize[0];
1009 off_y = ((uint16_t)opcode - 0x4000) / s->cur_decode_frame->linesize[0];
1010 copy_from(s, s->cur_decode_frame, s->cur_decode_frame, off_x, off_y);
1014 static void (* const ipvideo_format_10_passes[])(IpvideoContext *s, AVFrame *frame, int16_t op) = {
1015 ipvideo_format_10_firstpass, ipvideo_format_10_secondpass,
1018 static void ipvideo_decode_format_10_opcodes(IpvideoContext *s, AVFrame *frame)
1020 int pass, x, y, changed_block;
1021 int16_t opcode, skip;
1022 GetByteContext decoding_map_ptr;
1023 GetByteContext skip_map_ptr;
1025 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
1027 /* this is PAL8, so make the palette available */
1028 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
1029 s->stride = frame->linesize[0];
1031 s->line_inc = s->stride - 8;
1032 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
1033 + (s->avctx->width - 8) * (1 + s->is_16bpp);
1035 bytestream2_init(&decoding_map_ptr, s->decoding_map, s->decoding_map_size);
1036 bytestream2_init(&skip_map_ptr, s->skip_map, s->skip_map_size);
1038 for (pass = 0; pass < 2; ++pass) {
1039 bytestream2_seek(&decoding_map_ptr, 0, SEEK_SET);
1040 bytestream2_seek(&skip_map_ptr, 0, SEEK_SET);
1041 skip = bytestream2_get_le16(&skip_map_ptr);
1043 for (y = 0; y < s->avctx->height; y += 8) {
1044 for (x = 0; x < s->avctx->width; x += 8) {
1045 s->pixel_ptr = s->cur_decode_frame->data[0] + x + y * s->cur_decode_frame->linesize[0];
1048 if (skip != -0x8000 && skip) {
1049 opcode = bytestream2_get_le16(&decoding_map_ptr);
1050 ipvideo_format_10_passes[pass](s, frame, opcode);
1053 if (bytestream2_get_bytes_left(&skip_map_ptr) < 2)
1055 skip = bytestream2_get_le16(&skip_map_ptr);
1062 bytestream2_seek(&skip_map_ptr, 0, SEEK_SET);
1063 skip = bytestream2_get_le16(&skip_map_ptr);
1064 for (y = 0; y < s->avctx->height; y += 8) {
1065 for (x = 0; x < s->avctx->width; x += 8) {
1067 s->pixel_ptr = frame->data[0] + x + y*frame->linesize[0];
1070 if (skip != -0x8000 && skip) {
1074 if (bytestream2_get_bytes_left(&skip_map_ptr) < 2)
1076 skip = bytestream2_get_le16(&skip_map_ptr);
1079 if (changed_block) {
1080 copy_from(s, s->cur_decode_frame, frame, 0, 0);
1082 /* Don't try to copy last_frame data on the first frame */
1083 if (s->avctx->frame_number)
1084 copy_from(s, s->last_frame, frame, 0, 0);
1090 FFSWAP(AVFrame*, s->prev_decode_frame, s->cur_decode_frame);
1092 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
1093 av_log(s->avctx, AV_LOG_DEBUG,
1094 "decode finished with %d bytes left over\n",
1095 bytestream2_get_bytes_left(&s->stream_ptr));
1099 static void ipvideo_decode_format_11_opcodes(IpvideoContext *s, AVFrame *frame)
1102 unsigned char opcode;
1106 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
1108 /* this is PAL8, so make the palette available */
1109 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
1111 s->stride = frame->linesize[0];
1113 s->stride = frame->linesize[0] >> 1;
1114 s->mv_ptr = s->stream_ptr;
1115 bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
1117 s->line_inc = s->stride - 8;
1118 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
1119 + (s->avctx->width - 8) * (1 + s->is_16bpp);
1121 init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
1122 for (y = 0; y < s->avctx->height; y += 8) {
1123 for (x = 0; x < s->avctx->width; x += 8) {
1124 if (get_bits_left(&gb) < 4)
1126 opcode = get_bits(&gb, 4);
1129 " block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
1130 x, y, opcode, bytestream2_tell(&s->stream_ptr));
1133 s->pixel_ptr = frame->data[0] + x
1134 + y*frame->linesize[0];
1135 ret = ipvideo_decode_block[opcode](s, frame);
1137 s->pixel_ptr = frame->data[0] + x*2
1138 + y*frame->linesize[0];
1139 ret = ipvideo_decode_block16[opcode](s, frame);
1142 av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
1143 s->avctx->frame_number, x, y);
1148 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
1149 av_log(s->avctx, AV_LOG_DEBUG,
1150 "decode finished with %d bytes left over\n",
1151 bytestream2_get_bytes_left(&s->stream_ptr));
1155 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
1157 IpvideoContext *s = avctx->priv_data;
1162 s->is_16bpp = avctx->bits_per_coded_sample == 16;
1163 avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
1165 ff_hpeldsp_init(&s->hdsp, avctx->flags);
1167 s->last_frame = av_frame_alloc();
1168 s->second_last_frame = av_frame_alloc();
1169 s->cur_decode_frame = av_frame_alloc();
1170 s->prev_decode_frame = av_frame_alloc();
1171 if (!s->last_frame || !s->second_last_frame ||
1172 !s->cur_decode_frame || !s->prev_decode_frame) {
1173 ret = AVERROR(ENOMEM);
1177 s->cur_decode_frame->width = avctx->width;
1178 s->prev_decode_frame->width = avctx->width;
1179 s->cur_decode_frame->height = avctx->height;
1180 s->prev_decode_frame->height = avctx->height;
1181 s->cur_decode_frame->format = avctx->pix_fmt;
1182 s->prev_decode_frame->format = avctx->pix_fmt;
1184 ret = ff_get_buffer(avctx, s->cur_decode_frame, 0);
1188 ret = ff_get_buffer(avctx, s->prev_decode_frame, 0);
1194 av_frame_free(&s->last_frame);
1195 av_frame_free(&s->second_last_frame);
1196 av_frame_free(&s->cur_decode_frame);
1197 av_frame_free(&s->prev_decode_frame);
1201 static int ipvideo_decode_frame(AVCodecContext *avctx,
1202 void *data, int *got_frame,
1205 const uint8_t *buf = avpkt->data;
1206 int buf_size = avpkt->size;
1207 IpvideoContext *s = avctx->priv_data;
1208 AVFrame *frame = data;
1212 int video_data_size;
1214 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
1215 av_frame_unref(s->last_frame);
1216 av_frame_unref(s->second_last_frame);
1217 av_frame_unref(s->cur_decode_frame);
1218 av_frame_unref(s->prev_decode_frame);
1221 if (!s->cur_decode_frame->data[0]) {
1222 ret = ff_get_buffer(avctx, s->cur_decode_frame, 0);
1226 ret = ff_get_buffer(avctx, s->prev_decode_frame, 0);
1228 av_frame_unref(s->cur_decode_frame);
1234 return AVERROR_INVALIDDATA;
1236 frame_format = AV_RL8(buf);
1237 send_buffer = AV_RL8(buf + 1);
1238 video_data_size = AV_RL16(buf + 2);
1239 s->decoding_map_size = AV_RL16(buf + 4);
1240 s->skip_map_size = AV_RL16(buf + 6);
1242 switch(frame_format) {
1244 if (s->decoding_map_size) {
1245 av_log(avctx, AV_LOG_ERROR, "Decoding map for format 0x06\n");
1246 return AVERROR_INVALIDDATA;
1249 if (s->skip_map_size) {
1250 av_log(avctx, AV_LOG_ERROR, "Skip map for format 0x06\n");
1251 return AVERROR_INVALIDDATA;
1255 av_log(avctx, AV_LOG_ERROR, "Video format 0x06 does not support 16bpp movies\n");
1256 return AVERROR_INVALIDDATA;
1259 /* Decoding map for 0x06 frame format is at the top of pixeldata */
1260 s->decoding_map_size = ((s->avctx->width / 8) * (s->avctx->height / 8)) * 2;
1261 s->decoding_map = buf + 8 + 14; /* 14 bits of op data */
1262 video_data_size -= s->decoding_map_size + 14;
1263 if (video_data_size <= 0)
1264 return AVERROR_INVALIDDATA;
1266 if (buf_size < 8 + s->decoding_map_size + 14 + video_data_size)
1267 return AVERROR_INVALIDDATA;
1269 bytestream2_init(&s->stream_ptr, buf + 8 + s->decoding_map_size + 14, video_data_size);
1274 if (! s->decoding_map_size) {
1275 av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x10\n");
1276 return AVERROR_INVALIDDATA;
1279 if (! s->skip_map_size) {
1280 av_log(avctx, AV_LOG_ERROR, "Empty skip map for format 0x10\n");
1281 return AVERROR_INVALIDDATA;
1285 av_log(avctx, AV_LOG_ERROR, "Video format 0x10 does not support 16bpp movies\n");
1286 return AVERROR_INVALIDDATA;
1289 if (buf_size < 8 + video_data_size + s->decoding_map_size + s->skip_map_size)
1290 return AVERROR_INVALIDDATA;
1292 bytestream2_init(&s->stream_ptr, buf + 8, video_data_size);
1293 s->decoding_map = buf + 8 + video_data_size;
1294 s->skip_map = buf + 8 + video_data_size + s->decoding_map_size;
1299 if (! s->decoding_map_size) {
1300 av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x11\n");
1301 return AVERROR_INVALIDDATA;
1304 if (s->skip_map_size) {
1305 av_log(avctx, AV_LOG_ERROR, "Skip map for format 0x11\n");
1306 return AVERROR_INVALIDDATA;
1309 if (buf_size < 8 + video_data_size + s->decoding_map_size)
1310 return AVERROR_INVALIDDATA;
1312 bytestream2_init(&s->stream_ptr, buf + 8, video_data_size);
1313 s->decoding_map = buf + 8 + video_data_size;
1318 av_log(avctx, AV_LOG_ERROR, "Frame type 0x%02X unsupported\n", frame_format);
1321 /* ensure we can't overread the packet */
1322 if (buf_size < 8 + s->decoding_map_size + video_data_size + s->skip_map_size) {
1323 av_log(avctx, AV_LOG_ERROR, "Invalid IP packet size\n");
1324 return AVERROR_INVALIDDATA;
1327 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1332 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
1333 if (pal && size == AVPALETTE_SIZE) {
1334 frame->palette_has_changed = 1;
1335 memcpy(s->pal, pal, AVPALETTE_SIZE);
1337 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
1341 switch(frame_format) {
1343 ipvideo_decode_format_06_opcodes(s, frame);
1346 ipvideo_decode_format_10_opcodes(s, frame);
1349 ipvideo_decode_format_11_opcodes(s, frame);
1353 *got_frame = send_buffer;
1355 /* shuffle frames */
1356 av_frame_unref(s->second_last_frame);
1357 FFSWAP(AVFrame*, s->second_last_frame, s->last_frame);
1358 if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
1361 /* report that the buffer was completely consumed */
1365 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1367 IpvideoContext *s = avctx->priv_data;
1369 av_frame_free(&s->last_frame);
1370 av_frame_free(&s->second_last_frame);
1371 av_frame_free(&s->cur_decode_frame);
1372 av_frame_free(&s->prev_decode_frame);
1377 AVCodec ff_interplay_video_decoder = {
1378 .name = "interplayvideo",
1379 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
1380 .type = AVMEDIA_TYPE_VIDEO,
1381 .id = AV_CODEC_ID_INTERPLAY_VIDEO,
1382 .priv_data_size = sizeof(IpvideoContext),
1383 .init = ipvideo_decode_init,
1384 .close = ipvideo_decode_end,
1385 .decode = ipvideo_decode_frame,
1386 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,