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"
51 #define PALETTE_COUNT 256
53 typedef struct IpvideoContext {
55 AVCodecContext *avctx;
57 AVFrame *second_last_frame;
61 AVFrame *cur_decode_frame;
62 AVFrame *prev_decode_frame;
64 const unsigned char *decoding_map;
65 int decoding_map_size;
66 const unsigned char *skip_map;
70 GetByteContext stream_ptr, mv_ptr;
71 unsigned char *pixel_ptr;
74 int upper_motion_limit_offset;
79 static int copy_from(IpvideoContext *s, AVFrame *src, AVFrame *dst, int delta_x, int delta_y)
81 int width = dst->width;
82 int current_offset = s->pixel_ptr - dst->data[0];
83 int x = (current_offset % dst->linesize[0]) / (1 + s->is_16bpp);
84 int y = current_offset / dst->linesize[0];
85 int dx = delta_x + x - ((delta_x + x >= width) - (delta_x + x < 0)) * width;
86 int dy = delta_y + y + (delta_x + x >= width) - (delta_x + x < 0);
87 int motion_offset = dy * src->linesize[0] + dx * (1 + s->is_16bpp);
89 if (motion_offset < 0) {
90 av_log(s->avctx, AV_LOG_ERROR, "motion offset < 0 (%d)\n", motion_offset);
91 return AVERROR_INVALIDDATA;
92 } else if (motion_offset > s->upper_motion_limit_offset) {
93 av_log(s->avctx, AV_LOG_ERROR, "motion offset above limit (%d >= %d)\n",
94 motion_offset, s->upper_motion_limit_offset);
95 return AVERROR_INVALIDDATA;
98 av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
99 return AVERROR(EINVAL);
101 s->hdsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
102 dst->linesize[0], 8);
106 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s, AVFrame *frame)
108 return copy_from(s, s->last_frame, frame, 0, 0);
111 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s, AVFrame *frame)
113 return copy_from(s, s->second_last_frame, frame, 0, 0);
116 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s, AVFrame *frame)
121 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
123 B = bytestream2_get_byte(&s->stream_ptr);
125 B = bytestream2_get_byte(&s->mv_ptr);
132 x = -14 + ((B - 56) % 29);
133 y = 8 + ((B - 56) / 29);
136 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
137 return copy_from(s, s->second_last_frame, frame, x, y);
140 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s, AVFrame *frame)
145 /* copy 8x8 block from current frame from an up/left block */
147 /* need 1 more byte for motion */
149 B = bytestream2_get_byte(&s->stream_ptr);
151 B = bytestream2_get_byte(&s->mv_ptr);
158 x = -(-14 + ((B - 56) % 29));
159 y = -( 8 + ((B - 56) / 29));
162 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
163 return copy_from(s, frame, frame, x, y);
166 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s, AVFrame *frame)
169 unsigned char B, BL, BH;
171 /* copy a block from the previous frame; need 1 more byte */
173 B = bytestream2_get_byte(&s->stream_ptr);
175 B = bytestream2_get_byte(&s->mv_ptr);
179 BH = (B >> 4) & 0x0F;
183 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
184 return copy_from(s, s->last_frame, frame, x, y);
187 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s, AVFrame *frame)
191 /* copy a block from the previous frame using an expanded range;
192 * need 2 more bytes */
193 x = bytestream2_get_byte(&s->stream_ptr);
194 y = bytestream2_get_byte(&s->stream_ptr);
196 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
197 return copy_from(s, s->last_frame, frame, x, y);
200 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s, AVFrame *frame)
202 /* mystery opcode? skip multiple blocks? */
203 av_log(s->avctx, AV_LOG_ERROR, "Help! Mystery opcode 0x6 seen\n");
209 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s, AVFrame *frame)
215 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
216 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x7\n");
217 return AVERROR_INVALIDDATA;
220 /* 2-color encoding */
221 P[0] = bytestream2_get_byte(&s->stream_ptr);
222 P[1] = bytestream2_get_byte(&s->stream_ptr);
226 /* need 8 more bytes from the stream */
227 for (y = 0; y < 8; y++) {
228 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
229 for (; flags != 1; flags >>= 1)
230 *s->pixel_ptr++ = P[flags & 1];
231 s->pixel_ptr += s->line_inc;
236 /* need 2 more bytes from the stream */
237 flags = bytestream2_get_le16(&s->stream_ptr);
238 for (y = 0; y < 8; y += 2) {
239 for (x = 0; x < 8; x += 2, flags >>= 1) {
241 s->pixel_ptr[x + 1 ] =
242 s->pixel_ptr[x + s->stride] =
243 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
245 s->pixel_ptr += s->stride * 2;
253 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s, AVFrame *frame)
257 unsigned int flags = 0;
259 if (bytestream2_get_bytes_left(&s->stream_ptr) < 12) {
260 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x8\n");
261 return AVERROR_INVALIDDATA;
264 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
265 * either top and bottom or left and right halves */
266 P[0] = bytestream2_get_byte(&s->stream_ptr);
267 P[1] = bytestream2_get_byte(&s->stream_ptr);
270 for (y = 0; y < 16; y++) {
271 // new values for each 4x4 block
274 P[0] = bytestream2_get_byte(&s->stream_ptr);
275 P[1] = bytestream2_get_byte(&s->stream_ptr);
277 flags = bytestream2_get_le16(&s->stream_ptr);
280 for (x = 0; x < 4; x++, flags >>= 1)
281 *s->pixel_ptr++ = P[flags & 1];
282 s->pixel_ptr += s->stride - 4;
283 // switch to right half
284 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
288 flags = bytestream2_get_le32(&s->stream_ptr);
289 P[2] = bytestream2_get_byte(&s->stream_ptr);
290 P[3] = bytestream2_get_byte(&s->stream_ptr);
294 /* vertical split; left & right halves are 2-color encoded */
296 for (y = 0; y < 16; y++) {
297 for (x = 0; x < 4; x++, flags >>= 1)
298 *s->pixel_ptr++ = P[flags & 1];
299 s->pixel_ptr += s->stride - 4;
300 // switch to right half
302 s->pixel_ptr -= 8 * s->stride - 4;
305 flags = bytestream2_get_le32(&s->stream_ptr);
311 /* horizontal split; top & bottom halves are 2-color encoded */
313 for (y = 0; y < 8; y++) {
317 flags = bytestream2_get_le32(&s->stream_ptr);
320 for (x = 0; x < 8; x++, flags >>= 1)
321 *s->pixel_ptr++ = P[flags & 1];
322 s->pixel_ptr += s->line_inc;
331 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s, AVFrame *frame)
336 if (bytestream2_get_bytes_left(&s->stream_ptr) < 8) {
337 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x9\n");
338 return AVERROR_INVALIDDATA;
341 /* 4-color encoding */
342 bytestream2_get_buffer(&s->stream_ptr, P, 4);
347 /* 1 of 4 colors for each pixel, need 16 more bytes */
348 for (y = 0; y < 8; y++) {
349 /* get the next set of 8 2-bit flags */
350 int flags = bytestream2_get_le16(&s->stream_ptr);
351 for (x = 0; x < 8; x++, flags >>= 2)
352 *s->pixel_ptr++ = P[flags & 0x03];
353 s->pixel_ptr += s->line_inc;
359 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
360 flags = bytestream2_get_le32(&s->stream_ptr);
362 for (y = 0; y < 8; y += 2) {
363 for (x = 0; x < 8; x += 2, flags >>= 2) {
365 s->pixel_ptr[x + 1 ] =
366 s->pixel_ptr[x + s->stride] =
367 s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
369 s->pixel_ptr += s->stride * 2;
376 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
377 flags = bytestream2_get_le64(&s->stream_ptr);
379 for (y = 0; y < 8; y++) {
380 for (x = 0; x < 8; x += 2, flags >>= 2) {
382 s->pixel_ptr[x + 1] = P[flags & 0x03];
384 s->pixel_ptr += s->stride;
387 for (y = 0; y < 8; y += 2) {
388 for (x = 0; x < 8; x++, flags >>= 2) {
390 s->pixel_ptr[x + s->stride] = P[flags & 0x03];
392 s->pixel_ptr += s->stride * 2;
401 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s, AVFrame *frame)
407 if (bytestream2_get_bytes_left(&s->stream_ptr) < 16) {
408 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xA\n");
409 return AVERROR_INVALIDDATA;
412 bytestream2_get_buffer(&s->stream_ptr, P, 4);
414 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
415 * either top and bottom or left and right halves */
418 /* 4-color encoding for each quadrant; need 32 bytes */
419 for (y = 0; y < 16; y++) {
420 // new values for each 4x4 block
422 if (y) bytestream2_get_buffer(&s->stream_ptr, P, 4);
423 flags = bytestream2_get_le32(&s->stream_ptr);
426 for (x = 0; x < 4; x++, flags >>= 2)
427 *s->pixel_ptr++ = P[flags & 0x03];
429 s->pixel_ptr += s->stride - 4;
430 // switch to right half
431 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
437 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
439 bytestream2_get_buffer(&s->stream_ptr, P + 4, 4);
442 /* 4-color encoding for either left and right or top and bottom
445 for (y = 0; y < 16; y++) {
446 for (x = 0; x < 4; x++, flags >>= 2)
447 *s->pixel_ptr++ = P[flags & 0x03];
450 s->pixel_ptr += s->stride - 4;
451 // switch to right half
452 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
453 } else if (y & 1) s->pixel_ptr += s->line_inc;
455 // load values for second half
458 flags = bytestream2_get_le64(&s->stream_ptr);
467 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s, AVFrame *frame)
471 /* 64-color encoding (each pixel in block is a different color) */
472 for (y = 0; y < 8; y++) {
473 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
474 s->pixel_ptr += s->stride;
481 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s, AVFrame *frame)
485 /* 16-color block encoding: each 2x2 block is a different color */
486 for (y = 0; y < 8; y += 2) {
487 for (x = 0; x < 8; x += 2) {
489 s->pixel_ptr[x + 1 ] =
490 s->pixel_ptr[x + s->stride] =
491 s->pixel_ptr[x + 1 + s->stride] = bytestream2_get_byte(&s->stream_ptr);
493 s->pixel_ptr += s->stride * 2;
500 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s, AVFrame *frame)
505 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
506 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xD\n");
507 return AVERROR_INVALIDDATA;
510 /* 4-color block encoding: each 4x4 block is a different color */
511 for (y = 0; y < 8; y++) {
513 P[0] = bytestream2_get_byte(&s->stream_ptr);
514 P[1] = bytestream2_get_byte(&s->stream_ptr);
516 memset(s->pixel_ptr, P[0], 4);
517 memset(s->pixel_ptr + 4, P[1], 4);
518 s->pixel_ptr += s->stride;
525 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s, AVFrame *frame)
530 /* 1-color encoding: the whole block is 1 solid color */
531 pix = bytestream2_get_byte(&s->stream_ptr);
533 for (y = 0; y < 8; y++) {
534 memset(s->pixel_ptr, pix, 8);
535 s->pixel_ptr += s->stride;
542 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s, AVFrame *frame)
545 unsigned char sample[2];
547 /* dithered encoding */
548 sample[0] = bytestream2_get_byte(&s->stream_ptr);
549 sample[1] = bytestream2_get_byte(&s->stream_ptr);
551 for (y = 0; y < 8; y++) {
552 for (x = 0; x < 8; x += 2) {
553 *s->pixel_ptr++ = sample[ y & 1 ];
554 *s->pixel_ptr++ = sample[!(y & 1)];
556 s->pixel_ptr += s->line_inc;
563 static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s, AVFrame *frame)
567 /* copy a block from the second last frame using an expanded range */
568 x = bytestream2_get_byte(&s->stream_ptr);
569 y = bytestream2_get_byte(&s->stream_ptr);
571 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
572 return copy_from(s, s->second_last_frame, frame, x, y);
575 static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s, AVFrame *frame)
580 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
582 /* 2-color encoding */
583 P[0] = bytestream2_get_le16(&s->stream_ptr);
584 P[1] = bytestream2_get_le16(&s->stream_ptr);
586 if (!(P[0] & 0x8000)) {
588 for (y = 0; y < 8; y++) {
589 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
590 for (; flags != 1; flags >>= 1)
591 *pixel_ptr++ = P[flags & 1];
592 pixel_ptr += s->line_inc;
597 flags = bytestream2_get_le16(&s->stream_ptr);
598 for (y = 0; y < 8; y += 2) {
599 for (x = 0; x < 8; x += 2, flags >>= 1) {
602 pixel_ptr[x + s->stride] =
603 pixel_ptr[x + 1 + s->stride] = P[flags & 1];
605 pixel_ptr += s->stride * 2;
612 static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s, AVFrame *frame)
616 unsigned int flags = 0;
617 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
619 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
620 * either top and bottom or left and right halves */
621 P[0] = bytestream2_get_le16(&s->stream_ptr);
622 P[1] = bytestream2_get_le16(&s->stream_ptr);
624 if (!(P[0] & 0x8000)) {
626 for (y = 0; y < 16; y++) {
627 // new values for each 4x4 block
630 P[0] = bytestream2_get_le16(&s->stream_ptr);
631 P[1] = bytestream2_get_le16(&s->stream_ptr);
633 flags = bytestream2_get_le16(&s->stream_ptr);
636 for (x = 0; x < 4; x++, flags >>= 1)
637 *pixel_ptr++ = P[flags & 1];
638 pixel_ptr += s->stride - 4;
639 // switch to right half
640 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
645 flags = bytestream2_get_le32(&s->stream_ptr);
646 P[2] = bytestream2_get_le16(&s->stream_ptr);
647 P[3] = bytestream2_get_le16(&s->stream_ptr);
649 if (!(P[2] & 0x8000)) {
651 /* vertical split; left & right halves are 2-color encoded */
653 for (y = 0; y < 16; y++) {
654 for (x = 0; x < 4; x++, flags >>= 1)
655 *pixel_ptr++ = P[flags & 1];
656 pixel_ptr += s->stride - 4;
657 // switch to right half
659 pixel_ptr -= 8 * s->stride - 4;
662 flags = bytestream2_get_le32(&s->stream_ptr);
668 /* horizontal split; top & bottom halves are 2-color encoded */
670 for (y = 0; y < 8; y++) {
674 flags = bytestream2_get_le32(&s->stream_ptr);
677 for (x = 0; x < 8; x++, flags >>= 1)
678 *pixel_ptr++ = P[flags & 1];
679 pixel_ptr += s->line_inc;
688 static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s, AVFrame *frame)
692 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
694 /* 4-color encoding */
695 for (x = 0; x < 4; x++)
696 P[x] = bytestream2_get_le16(&s->stream_ptr);
698 if (!(P[0] & 0x8000)) {
699 if (!(P[2] & 0x8000)) {
701 /* 1 of 4 colors for each pixel */
702 for (y = 0; y < 8; y++) {
703 /* get the next set of 8 2-bit flags */
704 int flags = bytestream2_get_le16(&s->stream_ptr);
705 for (x = 0; x < 8; x++, flags >>= 2)
706 *pixel_ptr++ = P[flags & 0x03];
707 pixel_ptr += s->line_inc;
713 /* 1 of 4 colors for each 2x2 block */
714 flags = bytestream2_get_le32(&s->stream_ptr);
716 for (y = 0; y < 8; y += 2) {
717 for (x = 0; x < 8; x += 2, flags >>= 2) {
720 pixel_ptr[x + s->stride] =
721 pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
723 pixel_ptr += s->stride * 2;
730 /* 1 of 4 colors for each 2x1 or 1x2 block */
731 flags = bytestream2_get_le64(&s->stream_ptr);
732 if (!(P[2] & 0x8000)) {
733 for (y = 0; y < 8; y++) {
734 for (x = 0; x < 8; x += 2, flags >>= 2) {
736 pixel_ptr[x + 1] = P[flags & 0x03];
738 pixel_ptr += s->stride;
741 for (y = 0; y < 8; y += 2) {
742 for (x = 0; x < 8; x++, flags >>= 2) {
744 pixel_ptr[x + s->stride] = P[flags & 0x03];
746 pixel_ptr += s->stride * 2;
755 static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s, AVFrame *frame)
760 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
762 for (x = 0; x < 4; x++)
763 P[x] = bytestream2_get_le16(&s->stream_ptr);
765 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
766 * either top and bottom or left and right halves */
767 if (!(P[0] & 0x8000)) {
769 /* 4-color encoding for each quadrant */
770 for (y = 0; y < 16; y++) {
771 // new values for each 4x4 block
774 for (x = 0; x < 4; x++)
775 P[x] = bytestream2_get_le16(&s->stream_ptr);
776 flags = bytestream2_get_le32(&s->stream_ptr);
779 for (x = 0; x < 4; x++, flags >>= 2)
780 *pixel_ptr++ = P[flags & 0x03];
782 pixel_ptr += s->stride - 4;
783 // switch to right half
784 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
790 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
792 for (x = 4; x < 8; x++)
793 P[x] = bytestream2_get_le16(&s->stream_ptr);
794 vert = !(P[4] & 0x8000);
796 /* 4-color encoding for either left and right or top and bottom
799 for (y = 0; y < 16; y++) {
800 for (x = 0; x < 4; x++, flags >>= 2)
801 *pixel_ptr++ = P[flags & 0x03];
804 pixel_ptr += s->stride - 4;
805 // switch to right half
806 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
807 } else if (y & 1) pixel_ptr += s->line_inc;
809 // load values for second half
812 flags = bytestream2_get_le64(&s->stream_ptr);
821 static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s, AVFrame *frame)
824 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
826 /* 64-color encoding (each pixel in block is a different color) */
827 for (y = 0; y < 8; y++) {
828 for (x = 0; x < 8; x++)
829 pixel_ptr[x] = bytestream2_get_le16(&s->stream_ptr);
830 pixel_ptr += s->stride;
837 static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s, AVFrame *frame)
840 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
842 /* 16-color block encoding: each 2x2 block is a different color */
843 for (y = 0; y < 8; y += 2) {
844 for (x = 0; x < 8; x += 2) {
847 pixel_ptr[x + s->stride] =
848 pixel_ptr[x + 1 + s->stride] = bytestream2_get_le16(&s->stream_ptr);
850 pixel_ptr += s->stride * 2;
857 static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s, AVFrame *frame)
861 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
863 /* 4-color block encoding: each 4x4 block is a different color */
864 for (y = 0; y < 8; y++) {
866 P[0] = bytestream2_get_le16(&s->stream_ptr);
867 P[1] = bytestream2_get_le16(&s->stream_ptr);
869 for (x = 0; x < 8; x++)
870 pixel_ptr[x] = P[x >> 2];
871 pixel_ptr += s->stride;
878 static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s, AVFrame *frame)
882 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
884 /* 1-color encoding: the whole block is 1 solid color */
885 pix = bytestream2_get_le16(&s->stream_ptr);
887 for (y = 0; y < 8; y++) {
888 for (x = 0; x < 8; x++)
890 pixel_ptr += s->stride;
897 static int (* const ipvideo_decode_block[])(IpvideoContext *s, AVFrame *frame) = {
898 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
899 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
900 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
901 ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
902 ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
903 ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
904 ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
905 ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
908 static int (* const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame) = {
909 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
910 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
911 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
912 ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
913 ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
914 ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
915 ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
916 ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
919 static void ipvideo_format_06_firstpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
924 for (line = 0; line < 8; ++line) {
925 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
926 s->pixel_ptr += s->stride;
929 /* Don't try to copy second_last_frame data on the first frames */
930 if (s->avctx->frame_number > 2)
931 copy_from(s, s->second_last_frame, frame, 0, 0);
935 static void ipvideo_format_06_secondpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
940 off_x = ((uint16_t)opcode - 0xC000) % frame->width;
941 off_y = ((uint16_t)opcode - 0xC000) / frame->width;
942 copy_from(s, s->last_frame, frame, off_x, off_y);
943 } else if (opcode > 0) {
944 off_x = ((uint16_t)opcode - 0x4000) % frame->width;
945 off_y = ((uint16_t)opcode - 0x4000) / frame->width;
946 copy_from(s, frame, frame, off_x, off_y);
950 static void (* const ipvideo_format_06_passes[])(IpvideoContext *s, AVFrame *frame, int16_t op) = {
951 ipvideo_format_06_firstpass, ipvideo_format_06_secondpass,
954 static void ipvideo_decode_format_06_opcodes(IpvideoContext *s, AVFrame *frame)
958 GetByteContext decoding_map_ptr;
960 /* this is PAL8, so make the palette available */
961 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
962 s->stride = frame->linesize[0];
964 s->line_inc = s->stride - 8;
965 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
966 + (s->avctx->width - 8) * (1 + s->is_16bpp);
968 bytestream2_init(&decoding_map_ptr, s->decoding_map, s->decoding_map_size);
970 for (pass = 0; pass < 2; ++pass) {
971 bytestream2_seek(&decoding_map_ptr, 0, SEEK_SET);
972 for (y = 0; y < s->avctx->height; y += 8) {
973 for (x = 0; x < s->avctx->width; x += 8) {
974 opcode = bytestream2_get_le16(&decoding_map_ptr);
977 " block @ (%3d, %3d): opcode 0x%X, data ptr offset %d\n",
978 x, y, opcode, bytestream2_tell(&s->stream_ptr));
980 s->pixel_ptr = frame->data[0] + x + y * frame->linesize[0];
981 ipvideo_format_06_passes[pass](s, frame, opcode);
986 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
987 av_log(s->avctx, AV_LOG_DEBUG,
988 "decode finished with %d bytes left over\n",
989 bytestream2_get_bytes_left(&s->stream_ptr));
993 static void ipvideo_format_10_firstpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
998 for (line = 0; line < 8; ++line) {
999 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
1000 s->pixel_ptr += s->stride;
1005 static void ipvideo_format_10_secondpass(IpvideoContext *s, AVFrame *frame, int16_t opcode)
1010 off_x = ((uint16_t)opcode - 0xC000) % s->cur_decode_frame->width;
1011 off_y = ((uint16_t)opcode - 0xC000) / s->cur_decode_frame->width;
1012 copy_from(s, s->prev_decode_frame, s->cur_decode_frame, off_x, off_y);
1013 } else if (opcode > 0) {
1014 off_x = ((uint16_t)opcode - 0x4000) % s->cur_decode_frame->width;
1015 off_y = ((uint16_t)opcode - 0x4000) / s->cur_decode_frame->width;
1016 copy_from(s, s->cur_decode_frame, s->cur_decode_frame, off_x, off_y);
1020 static void (* const ipvideo_format_10_passes[])(IpvideoContext *s, AVFrame *frame, int16_t op) = {
1021 ipvideo_format_10_firstpass, ipvideo_format_10_secondpass,
1024 static void ipvideo_decode_format_10_opcodes(IpvideoContext *s, AVFrame *frame)
1026 int pass, x, y, changed_block;
1027 int16_t opcode, skip;
1028 GetByteContext decoding_map_ptr;
1029 GetByteContext skip_map_ptr;
1031 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
1033 /* this is PAL8, so make the palette available */
1034 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
1035 s->stride = frame->linesize[0];
1037 s->line_inc = s->stride - 8;
1038 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
1039 + (s->avctx->width - 8) * (1 + s->is_16bpp);
1041 bytestream2_init(&decoding_map_ptr, s->decoding_map, s->decoding_map_size);
1042 bytestream2_init(&skip_map_ptr, s->skip_map, s->skip_map_size);
1044 for (pass = 0; pass < 2; ++pass) {
1045 bytestream2_seek(&decoding_map_ptr, 0, SEEK_SET);
1046 bytestream2_seek(&skip_map_ptr, 0, SEEK_SET);
1047 skip = bytestream2_get_le16(&skip_map_ptr);
1049 for (y = 0; y < s->avctx->height; y += 8) {
1050 for (x = 0; x < s->avctx->width; x += 8) {
1051 s->pixel_ptr = s->cur_decode_frame->data[0] + x + y * s->cur_decode_frame->linesize[0];
1054 if (skip != -0x8000 && skip) {
1055 opcode = bytestream2_get_le16(&decoding_map_ptr);
1056 ipvideo_format_10_passes[pass](s, frame, opcode);
1059 if (bytestream2_get_bytes_left(&skip_map_ptr) < 2)
1061 skip = bytestream2_get_le16(&skip_map_ptr);
1068 bytestream2_seek(&skip_map_ptr, 0, SEEK_SET);
1069 skip = bytestream2_get_le16(&skip_map_ptr);
1070 for (y = 0; y < s->avctx->height; y += 8) {
1071 for (x = 0; x < s->avctx->width; x += 8) {
1073 s->pixel_ptr = frame->data[0] + x + y*frame->linesize[0];
1076 if (skip != -0x8000 && skip) {
1080 if (bytestream2_get_bytes_left(&skip_map_ptr) < 2)
1082 skip = bytestream2_get_le16(&skip_map_ptr);
1085 if (changed_block) {
1086 copy_from(s, s->cur_decode_frame, frame, 0, 0);
1088 /* Don't try to copy last_frame data on the first frame */
1089 if (s->avctx->frame_number)
1090 copy_from(s, s->last_frame, frame, 0, 0);
1096 FFSWAP(AVFrame*, s->prev_decode_frame, s->cur_decode_frame);
1098 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
1099 av_log(s->avctx, AV_LOG_DEBUG,
1100 "decode finished with %d bytes left over\n",
1101 bytestream2_get_bytes_left(&s->stream_ptr));
1105 static void ipvideo_decode_format_11_opcodes(IpvideoContext *s, AVFrame *frame)
1108 unsigned char opcode;
1112 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
1114 /* this is PAL8, so make the palette available */
1115 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
1117 s->stride = frame->linesize[0];
1119 s->stride = frame->linesize[0] >> 1;
1120 s->mv_ptr = s->stream_ptr;
1121 bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
1123 s->line_inc = s->stride - 8;
1124 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
1125 + (s->avctx->width - 8) * (1 + s->is_16bpp);
1127 init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
1128 for (y = 0; y < s->avctx->height; y += 8) {
1129 for (x = 0; x < s->avctx->width; x += 8) {
1130 if (get_bits_left(&gb) < 4)
1132 opcode = get_bits(&gb, 4);
1135 " block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
1136 x, y, opcode, bytestream2_tell(&s->stream_ptr));
1139 s->pixel_ptr = frame->data[0] + x
1140 + y*frame->linesize[0];
1141 ret = ipvideo_decode_block[opcode](s, frame);
1143 s->pixel_ptr = frame->data[0] + x*2
1144 + y*frame->linesize[0];
1145 ret = ipvideo_decode_block16[opcode](s, frame);
1148 av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
1149 s->avctx->frame_number, x, y);
1154 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
1155 av_log(s->avctx, AV_LOG_DEBUG,
1156 "decode finished with %d bytes left over\n",
1157 bytestream2_get_bytes_left(&s->stream_ptr));
1161 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
1163 IpvideoContext *s = avctx->priv_data;
1167 s->is_16bpp = avctx->bits_per_coded_sample == 16;
1168 avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
1170 ff_hpeldsp_init(&s->hdsp, avctx->flags);
1172 s->last_frame = av_frame_alloc();
1173 s->second_last_frame = av_frame_alloc();
1174 s->cur_decode_frame = av_frame_alloc();
1175 s->prev_decode_frame = av_frame_alloc();
1176 if (!s->last_frame || !s->second_last_frame ||
1177 !s->cur_decode_frame || !s->prev_decode_frame) {
1178 return AVERROR(ENOMEM);
1181 s->cur_decode_frame->width = avctx->width;
1182 s->prev_decode_frame->width = avctx->width;
1183 s->cur_decode_frame->height = avctx->height;
1184 s->prev_decode_frame->height = avctx->height;
1185 s->cur_decode_frame->format = avctx->pix_fmt;
1186 s->prev_decode_frame->format = avctx->pix_fmt;
1191 static int ipvideo_decode_frame(AVCodecContext *avctx,
1192 void *data, int *got_frame,
1195 const uint8_t *buf = avpkt->data;
1196 int buf_size = avpkt->size;
1197 IpvideoContext *s = avctx->priv_data;
1198 AVFrame *frame = data;
1202 int video_data_size;
1204 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
1205 av_frame_unref(s->last_frame);
1206 av_frame_unref(s->second_last_frame);
1207 av_frame_unref(s->cur_decode_frame);
1208 av_frame_unref(s->prev_decode_frame);
1211 if (!s->cur_decode_frame->data[0]) {
1212 ret = ff_get_buffer(avctx, s->cur_decode_frame, 0);
1216 ret = ff_get_buffer(avctx, s->prev_decode_frame, 0);
1218 av_frame_unref(s->cur_decode_frame);
1224 return AVERROR_INVALIDDATA;
1226 frame_format = AV_RL8(buf);
1227 send_buffer = AV_RL8(buf + 1);
1228 video_data_size = AV_RL16(buf + 2);
1229 s->decoding_map_size = AV_RL16(buf + 4);
1230 s->skip_map_size = AV_RL16(buf + 6);
1232 switch (frame_format) {
1234 if (s->decoding_map_size) {
1235 av_log(avctx, AV_LOG_ERROR, "Decoding map for format 0x06\n");
1236 return AVERROR_INVALIDDATA;
1239 if (s->skip_map_size) {
1240 av_log(avctx, AV_LOG_ERROR, "Skip map for format 0x06\n");
1241 return AVERROR_INVALIDDATA;
1245 av_log(avctx, AV_LOG_ERROR, "Video format 0x06 does not support 16bpp movies\n");
1246 return AVERROR_INVALIDDATA;
1249 /* Decoding map for 0x06 frame format is at the top of pixeldata */
1250 s->decoding_map_size = ((s->avctx->width / 8) * (s->avctx->height / 8)) * 2;
1251 s->decoding_map = buf + 8 + 14; /* 14 bits of op data */
1252 video_data_size -= s->decoding_map_size + 14;
1253 if (video_data_size <= 0 || s->decoding_map_size == 0)
1254 return AVERROR_INVALIDDATA;
1256 if (buf_size < 8 + s->decoding_map_size + 14 + video_data_size)
1257 return AVERROR_INVALIDDATA;
1259 bytestream2_init(&s->stream_ptr, buf + 8 + s->decoding_map_size + 14, video_data_size);
1264 if (! s->decoding_map_size) {
1265 av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x10\n");
1266 return AVERROR_INVALIDDATA;
1269 if (! s->skip_map_size) {
1270 av_log(avctx, AV_LOG_ERROR, "Empty skip map for format 0x10\n");
1271 return AVERROR_INVALIDDATA;
1275 av_log(avctx, AV_LOG_ERROR, "Video format 0x10 does not support 16bpp movies\n");
1276 return AVERROR_INVALIDDATA;
1279 if (buf_size < 8 + video_data_size + s->decoding_map_size + s->skip_map_size)
1280 return AVERROR_INVALIDDATA;
1282 bytestream2_init(&s->stream_ptr, buf + 8, video_data_size);
1283 s->decoding_map = buf + 8 + video_data_size;
1284 s->skip_map = buf + 8 + video_data_size + s->decoding_map_size;
1289 if (! s->decoding_map_size) {
1290 av_log(avctx, AV_LOG_ERROR, "Empty decoding map for format 0x11\n");
1291 return AVERROR_INVALIDDATA;
1294 if (s->skip_map_size) {
1295 av_log(avctx, AV_LOG_ERROR, "Skip map for format 0x11\n");
1296 return AVERROR_INVALIDDATA;
1299 if (buf_size < 8 + video_data_size + s->decoding_map_size)
1300 return AVERROR_INVALIDDATA;
1302 bytestream2_init(&s->stream_ptr, buf + 8, video_data_size);
1303 s->decoding_map = buf + 8 + video_data_size;
1308 av_log(avctx, AV_LOG_ERROR, "Frame type 0x%02X unsupported\n", frame_format);
1311 /* ensure we can't overread the packet */
1312 if (buf_size < 8 + s->decoding_map_size + video_data_size + s->skip_map_size) {
1313 av_log(avctx, AV_LOG_ERROR, "Invalid IP packet size\n");
1314 return AVERROR_INVALIDDATA;
1317 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1321 frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
1324 switch (frame_format) {
1326 ipvideo_decode_format_06_opcodes(s, frame);
1329 ipvideo_decode_format_10_opcodes(s, frame);
1332 ipvideo_decode_format_11_opcodes(s, frame);
1336 *got_frame = send_buffer;
1338 /* shuffle frames */
1339 av_frame_unref(s->second_last_frame);
1340 FFSWAP(AVFrame*, s->second_last_frame, s->last_frame);
1341 if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
1344 /* report that the buffer was completely consumed */
1348 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1350 IpvideoContext *s = avctx->priv_data;
1352 av_frame_free(&s->last_frame);
1353 av_frame_free(&s->second_last_frame);
1354 av_frame_free(&s->cur_decode_frame);
1355 av_frame_free(&s->prev_decode_frame);
1360 const AVCodec ff_interplay_video_decoder = {
1361 .name = "interplayvideo",
1362 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
1363 .type = AVMEDIA_TYPE_VIDEO,
1364 .id = AV_CODEC_ID_INTERPLAY_VIDEO,
1365 .priv_data_size = sizeof(IpvideoContext),
1366 .init = ipvideo_decode_init,
1367 .close = ipvideo_decode_end,
1368 .decode = ipvideo_decode_frame,
1369 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,
1370 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,