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, short 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, short opcode)
934 off_x = ((unsigned short)opcode - 0xC000) % frame->linesize[0];
935 off_y = ((unsigned short)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 = ((unsigned short)opcode - 0x4000) % frame->linesize[0];
939 off_y = ((unsigned short)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, short 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, short 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, short opcode)
1004 off_x = ((unsigned short)opcode - 0xC000) % s->cur_decode_frame->linesize[0];
1005 off_y = ((unsigned short)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 = ((unsigned short)opcode - 0x4000) % s->cur_decode_frame->linesize[0];
1009 off_y = ((unsigned short)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, short 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;
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 skip = bytestream2_get_le16(&skip_map_ptr);
1060 bytestream2_seek(&skip_map_ptr, 0, SEEK_SET);
1061 skip = bytestream2_get_le16(&skip_map_ptr);
1062 for (y = 0; y < s->avctx->height; y += 8) {
1063 for (x = 0; x < s->avctx->width; x += 8) {
1065 s->pixel_ptr = frame->data[0] + x + y*frame->linesize[0];
1068 if (skip != -0x8000 && skip) {
1072 skip = bytestream2_get_le16(&skip_map_ptr);
1075 if (changed_block) {
1076 copy_from(s, s->cur_decode_frame, frame, 0, 0);
1078 /* Don't try to copy last_frame data on the first frame */
1079 if (s->avctx->frame_number)
1080 copy_from(s, s->last_frame, frame, 0, 0);
1086 FFSWAP(AVFrame*, s->prev_decode_frame, s->cur_decode_frame);
1088 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
1089 av_log(s->avctx, AV_LOG_DEBUG,
1090 "decode finished with %d bytes left over\n",
1091 bytestream2_get_bytes_left(&s->stream_ptr));
1095 static void ipvideo_decode_format_11_opcodes(IpvideoContext *s, AVFrame *frame)
1098 unsigned char opcode;
1102 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
1104 /* this is PAL8, so make the palette available */
1105 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
1107 s->stride = frame->linesize[0];
1109 s->stride = frame->linesize[0] >> 1;
1110 s->mv_ptr = s->stream_ptr;
1111 bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
1113 s->line_inc = s->stride - 8;
1114 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
1115 + (s->avctx->width - 8) * (1 + s->is_16bpp);
1117 init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
1118 for (y = 0; y < s->avctx->height; y += 8) {
1119 for (x = 0; x < s->avctx->width; x += 8) {
1120 if (get_bits_left(&gb) < 4)
1122 opcode = get_bits(&gb, 4);
1125 " block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
1126 x, y, opcode, bytestream2_tell(&s->stream_ptr));
1129 s->pixel_ptr = frame->data[0] + x
1130 + y*frame->linesize[0];
1131 ret = ipvideo_decode_block[opcode](s, frame);
1133 s->pixel_ptr = frame->data[0] + x*2
1134 + y*frame->linesize[0];
1135 ret = ipvideo_decode_block16[opcode](s, frame);
1138 av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
1139 s->avctx->frame_number, x, y);
1144 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
1145 av_log(s->avctx, AV_LOG_DEBUG,
1146 "decode finished with %d bytes left over\n",
1147 bytestream2_get_bytes_left(&s->stream_ptr));
1151 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
1153 IpvideoContext *s = avctx->priv_data;
1157 s->is_16bpp = avctx->bits_per_coded_sample == 16;
1158 avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
1160 ff_hpeldsp_init(&s->hdsp, avctx->flags);
1162 s->last_frame = av_frame_alloc();
1163 s->second_last_frame = av_frame_alloc();
1164 s->cur_decode_frame = av_frame_alloc();
1165 s->prev_decode_frame = av_frame_alloc();
1166 if (!s->last_frame || !s->second_last_frame ||
1167 !s->cur_decode_frame || !s->prev_decode_frame) {
1168 av_frame_free(&s->last_frame);
1169 av_frame_free(&s->second_last_frame);
1170 av_frame_free(&s->cur_decode_frame);
1171 av_frame_free(&s->prev_decode_frame);
1172 return AVERROR(ENOMEM);
1175 s->cur_decode_frame->width = avctx->width;
1176 s->prev_decode_frame->width = avctx->width;
1177 s->cur_decode_frame->height = avctx->height;
1178 s->prev_decode_frame->height = avctx->height;
1179 s->cur_decode_frame->format = avctx->pix_fmt;
1180 s->prev_decode_frame->format = avctx->pix_fmt;
1182 ff_get_buffer(avctx, s->cur_decode_frame, 0);
1183 ff_get_buffer(avctx, s->prev_decode_frame, 0);
1188 static int ipvideo_decode_frame(AVCodecContext *avctx,
1189 void *data, int *got_frame,
1192 const uint8_t *buf = avpkt->data;
1193 int buf_size = avpkt->size;
1194 IpvideoContext *s = avctx->priv_data;
1195 AVFrame *frame = data;
1199 int video_data_size;
1201 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
1202 av_frame_unref(s->last_frame);
1203 av_frame_unref(s->second_last_frame);
1207 return AVERROR_INVALIDDATA;
1209 frame_format = AV_RL8(buf);
1210 send_buffer = AV_RL8(buf + 1);
1211 video_data_size = AV_RL16(buf + 2);
1212 s->decoding_map_size = AV_RL16(buf + 4);
1213 s->skip_map_size = AV_RL16(buf + 6);
1215 switch(frame_format) {
1217 if (s->decoding_map_size) {
1218 av_log(avctx, AV_LOG_ERROR, "Decoding map for format 0x06\n");
1219 return AVERROR_INVALIDDATA;
1222 if (s->skip_map_size) {
1223 av_log(avctx, AV_LOG_ERROR, "Skip map for format 0x06\n");
1224 return AVERROR_INVALIDDATA;
1228 av_log(avctx, AV_LOG_ERROR, "Video format 0x06 does not support 16bpp movies\n");
1229 return AVERROR_INVALIDDATA;
1232 /* Decoding map for 0x06 frame format is at the top of pixeldata */
1233 s->decoding_map_size = ((s->avctx->width / 8) * (s->avctx->height / 8)) * 2;
1234 s->decoding_map = buf + 8 + 14; /* 14 bits of op data */
1235 video_data_size -= s->decoding_map_size + 14;
1237 if (buf_size < 8 + s->decoding_map_size + 14 + video_data_size)
1238 return AVERROR_INVALIDDATA;
1240 bytestream2_init(&s->stream_ptr, buf + 8 + s->decoding_map_size + 14, video_data_size);
1245 if (! s->decoding_map_size) {
1246 av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x10\n");
1247 return AVERROR_INVALIDDATA;
1250 if (! s->skip_map_size) {
1251 av_log(avctx, AV_LOG_ERROR, "Empty skip map for format 0x10\n");
1252 return AVERROR_INVALIDDATA;
1256 av_log(avctx, AV_LOG_ERROR, "Video format 0x10 does not support 16bpp movies\n");
1257 return AVERROR_INVALIDDATA;
1260 if (buf_size < 8 + video_data_size + s->decoding_map_size + s->skip_map_size)
1261 return AVERROR_INVALIDDATA;
1263 bytestream2_init(&s->stream_ptr, buf + 8, video_data_size);
1264 s->decoding_map = buf + 8 + video_data_size;
1265 s->skip_map = buf + 8 + video_data_size + s->decoding_map_size;
1270 if (! s->decoding_map_size) {
1271 av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x11\n");
1272 return AVERROR_INVALIDDATA;
1275 if (s->skip_map_size) {
1276 av_log(avctx, AV_LOG_ERROR, "Skip map for format 0x11\n");
1277 return AVERROR_INVALIDDATA;
1280 if (buf_size < 8 + video_data_size + s->decoding_map_size)
1281 return AVERROR_INVALIDDATA;
1283 bytestream2_init(&s->stream_ptr, buf + 8, video_data_size);
1284 s->decoding_map = buf + 8 + video_data_size;
1289 av_log(avctx, AV_LOG_ERROR, "Frame type 0x%02X unsupported\n", frame_format);
1292 /* ensure we can't overread the packet */
1293 if (buf_size < 8 + s->decoding_map_size + video_data_size + s->skip_map_size) {
1294 av_log(avctx, AV_LOG_ERROR, "Invalid IP packet size\n");
1295 return AVERROR_INVALIDDATA;
1298 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1303 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
1304 if (pal && size == AVPALETTE_SIZE) {
1305 frame->palette_has_changed = 1;
1306 memcpy(s->pal, pal, AVPALETTE_SIZE);
1308 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
1312 switch(frame_format) {
1314 ipvideo_decode_format_06_opcodes(s, frame);
1317 ipvideo_decode_format_10_opcodes(s, frame);
1320 ipvideo_decode_format_11_opcodes(s, frame);
1324 *got_frame = send_buffer;
1326 /* shuffle frames */
1327 av_frame_unref(s->second_last_frame);
1328 FFSWAP(AVFrame*, s->second_last_frame, s->last_frame);
1329 if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
1332 /* report that the buffer was completely consumed */
1336 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1338 IpvideoContext *s = avctx->priv_data;
1340 av_frame_free(&s->last_frame);
1341 av_frame_free(&s->second_last_frame);
1342 av_frame_free(&s->cur_decode_frame);
1343 av_frame_free(&s->prev_decode_frame);
1348 AVCodec ff_interplay_video_decoder = {
1349 .name = "interplayvideo",
1350 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
1351 .type = AVMEDIA_TYPE_VIDEO,
1352 .id = AV_CODEC_ID_INTERPLAY_VIDEO,
1353 .priv_data_size = sizeof(IpvideoContext),
1354 .init = ipvideo_decode_init,
1355 .close = ipvideo_decode_end,
1356 .decode = ipvideo_decode_frame,
1357 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,