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 #include "bytestream.h"
45 #define BITSTREAM_READER_LE
49 #define PALETTE_COUNT 256
51 typedef struct IpvideoContext {
53 AVCodecContext *avctx;
55 AVFrame *second_last_frame;
57 const unsigned char *decoding_map;
58 int decoding_map_size;
61 GetByteContext stream_ptr, mv_ptr;
62 unsigned char *pixel_ptr;
65 int upper_motion_limit_offset;
70 static int copy_from(IpvideoContext *s, AVFrame *src, AVFrame *dst, int delta_x, int delta_y)
72 int current_offset = s->pixel_ptr - dst->data[0];
73 int motion_offset = current_offset + delta_y * dst->linesize[0]
74 + delta_x * (1 + s->is_16bpp);
75 if (motion_offset < 0) {
76 av_log(s->avctx, AV_LOG_ERROR, "motion offset < 0 (%d)\n", motion_offset);
77 return AVERROR_INVALIDDATA;
78 } else if (motion_offset > s->upper_motion_limit_offset) {
79 av_log(s->avctx, AV_LOG_ERROR, "motion offset above limit (%d >= %d)\n",
80 motion_offset, s->upper_motion_limit_offset);
81 return AVERROR_INVALIDDATA;
84 av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
85 return AVERROR(EINVAL);
87 s->hdsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
92 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s, AVFrame *frame)
94 return copy_from(s, s->last_frame, frame, 0, 0);
97 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s, AVFrame *frame)
99 return copy_from(s, s->second_last_frame, frame, 0, 0);
102 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s, AVFrame *frame)
107 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
109 B = bytestream2_get_byte(&s->stream_ptr);
111 B = bytestream2_get_byte(&s->mv_ptr);
118 x = -14 + ((B - 56) % 29);
119 y = 8 + ((B - 56) / 29);
122 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
123 return copy_from(s, s->second_last_frame, frame, x, y);
126 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s, AVFrame *frame)
131 /* copy 8x8 block from current frame from an up/left block */
133 /* need 1 more byte for motion */
135 B = bytestream2_get_byte(&s->stream_ptr);
137 B = bytestream2_get_byte(&s->mv_ptr);
144 x = -(-14 + ((B - 56) % 29));
145 y = -( 8 + ((B - 56) / 29));
148 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
149 return copy_from(s, frame, frame, x, y);
152 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s, AVFrame *frame)
155 unsigned char B, BL, BH;
157 /* copy a block from the previous frame; need 1 more byte */
159 B = bytestream2_get_byte(&s->stream_ptr);
161 B = bytestream2_get_byte(&s->mv_ptr);
165 BH = (B >> 4) & 0x0F;
169 ff_tlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
170 return copy_from(s, s->last_frame, frame, x, y);
173 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s, AVFrame *frame)
177 /* copy a block from the previous frame using an expanded range;
178 * need 2 more bytes */
179 x = bytestream2_get_byte(&s->stream_ptr);
180 y = bytestream2_get_byte(&s->stream_ptr);
182 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
183 return copy_from(s, s->last_frame, frame, x, y);
186 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s, AVFrame *frame)
188 /* mystery opcode? skip multiple blocks? */
189 av_log(s->avctx, AV_LOG_ERROR, "Help! Mystery opcode 0x6 seen\n");
195 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s, AVFrame *frame)
201 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
202 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x7\n");
203 return AVERROR_INVALIDDATA;
206 /* 2-color encoding */
207 P[0] = bytestream2_get_byte(&s->stream_ptr);
208 P[1] = bytestream2_get_byte(&s->stream_ptr);
212 /* need 8 more bytes from the stream */
213 for (y = 0; y < 8; y++) {
214 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
215 for (; flags != 1; flags >>= 1)
216 *s->pixel_ptr++ = P[flags & 1];
217 s->pixel_ptr += s->line_inc;
222 /* need 2 more bytes from the stream */
223 flags = bytestream2_get_le16(&s->stream_ptr);
224 for (y = 0; y < 8; y += 2) {
225 for (x = 0; x < 8; x += 2, flags >>= 1) {
227 s->pixel_ptr[x + 1 ] =
228 s->pixel_ptr[x + s->stride] =
229 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
231 s->pixel_ptr += s->stride * 2;
239 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s, AVFrame *frame)
243 unsigned int flags = 0;
245 if (bytestream2_get_bytes_left(&s->stream_ptr) < 12) {
246 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x8\n");
247 return AVERROR_INVALIDDATA;
250 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
251 * either top and bottom or left and right halves */
252 P[0] = bytestream2_get_byte(&s->stream_ptr);
253 P[1] = bytestream2_get_byte(&s->stream_ptr);
256 for (y = 0; y < 16; y++) {
257 // new values for each 4x4 block
260 P[0] = bytestream2_get_byte(&s->stream_ptr);
261 P[1] = bytestream2_get_byte(&s->stream_ptr);
263 flags = bytestream2_get_le16(&s->stream_ptr);
266 for (x = 0; x < 4; x++, flags >>= 1)
267 *s->pixel_ptr++ = P[flags & 1];
268 s->pixel_ptr += s->stride - 4;
269 // switch to right half
270 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
274 flags = bytestream2_get_le32(&s->stream_ptr);
275 P[2] = bytestream2_get_byte(&s->stream_ptr);
276 P[3] = bytestream2_get_byte(&s->stream_ptr);
280 /* vertical split; left & right halves are 2-color encoded */
282 for (y = 0; y < 16; y++) {
283 for (x = 0; x < 4; x++, flags >>= 1)
284 *s->pixel_ptr++ = P[flags & 1];
285 s->pixel_ptr += s->stride - 4;
286 // switch to right half
288 s->pixel_ptr -= 8 * s->stride - 4;
291 flags = bytestream2_get_le32(&s->stream_ptr);
297 /* horizontal split; top & bottom halves are 2-color encoded */
299 for (y = 0; y < 8; y++) {
303 flags = bytestream2_get_le32(&s->stream_ptr);
306 for (x = 0; x < 8; x++, flags >>= 1)
307 *s->pixel_ptr++ = P[flags & 1];
308 s->pixel_ptr += s->line_inc;
317 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s, AVFrame *frame)
322 if (bytestream2_get_bytes_left(&s->stream_ptr) < 8) {
323 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0x9\n");
324 return AVERROR_INVALIDDATA;
327 /* 4-color encoding */
328 bytestream2_get_buffer(&s->stream_ptr, P, 4);
333 /* 1 of 4 colors for each pixel, need 16 more bytes */
334 for (y = 0; y < 8; y++) {
335 /* get the next set of 8 2-bit flags */
336 int flags = bytestream2_get_le16(&s->stream_ptr);
337 for (x = 0; x < 8; x++, flags >>= 2)
338 *s->pixel_ptr++ = P[flags & 0x03];
339 s->pixel_ptr += s->line_inc;
345 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
346 flags = bytestream2_get_le32(&s->stream_ptr);
348 for (y = 0; y < 8; y += 2) {
349 for (x = 0; x < 8; x += 2, flags >>= 2) {
351 s->pixel_ptr[x + 1 ] =
352 s->pixel_ptr[x + s->stride] =
353 s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
355 s->pixel_ptr += s->stride * 2;
362 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
363 flags = bytestream2_get_le64(&s->stream_ptr);
365 for (y = 0; y < 8; y++) {
366 for (x = 0; x < 8; x += 2, flags >>= 2) {
368 s->pixel_ptr[x + 1] = P[flags & 0x03];
370 s->pixel_ptr += s->stride;
373 for (y = 0; y < 8; y += 2) {
374 for (x = 0; x < 8; x++, flags >>= 2) {
376 s->pixel_ptr[x + s->stride] = P[flags & 0x03];
378 s->pixel_ptr += s->stride * 2;
387 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s, AVFrame *frame)
393 if (bytestream2_get_bytes_left(&s->stream_ptr) < 16) {
394 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xA\n");
395 return AVERROR_INVALIDDATA;
398 bytestream2_get_buffer(&s->stream_ptr, P, 4);
400 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
401 * either top and bottom or left and right halves */
404 /* 4-color encoding for each quadrant; need 32 bytes */
405 for (y = 0; y < 16; y++) {
406 // new values for each 4x4 block
408 if (y) bytestream2_get_buffer(&s->stream_ptr, P, 4);
409 flags = bytestream2_get_le32(&s->stream_ptr);
412 for (x = 0; x < 4; x++, flags >>= 2)
413 *s->pixel_ptr++ = P[flags & 0x03];
415 s->pixel_ptr += s->stride - 4;
416 // switch to right half
417 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
423 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
425 bytestream2_get_buffer(&s->stream_ptr, P + 4, 4);
428 /* 4-color encoding for either left and right or top and bottom
431 for (y = 0; y < 16; y++) {
432 for (x = 0; x < 4; x++, flags >>= 2)
433 *s->pixel_ptr++ = P[flags & 0x03];
436 s->pixel_ptr += s->stride - 4;
437 // switch to right half
438 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
439 } else if (y & 1) s->pixel_ptr += s->line_inc;
441 // load values for second half
444 flags = bytestream2_get_le64(&s->stream_ptr);
453 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s, AVFrame *frame)
457 /* 64-color encoding (each pixel in block is a different color) */
458 for (y = 0; y < 8; y++) {
459 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
460 s->pixel_ptr += s->stride;
467 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s, AVFrame *frame)
471 /* 16-color block encoding: each 2x2 block is a different color */
472 for (y = 0; y < 8; y += 2) {
473 for (x = 0; x < 8; x += 2) {
475 s->pixel_ptr[x + 1 ] =
476 s->pixel_ptr[x + s->stride] =
477 s->pixel_ptr[x + 1 + s->stride] = bytestream2_get_byte(&s->stream_ptr);
479 s->pixel_ptr += s->stride * 2;
486 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s, AVFrame *frame)
491 if (bytestream2_get_bytes_left(&s->stream_ptr) < 4) {
492 av_log(s->avctx, AV_LOG_ERROR, "too little data for opcode 0xD\n");
493 return AVERROR_INVALIDDATA;
496 /* 4-color block encoding: each 4x4 block is a different color */
497 for (y = 0; y < 8; y++) {
499 P[0] = bytestream2_get_byte(&s->stream_ptr);
500 P[1] = bytestream2_get_byte(&s->stream_ptr);
502 memset(s->pixel_ptr, P[0], 4);
503 memset(s->pixel_ptr + 4, P[1], 4);
504 s->pixel_ptr += s->stride;
511 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s, AVFrame *frame)
516 /* 1-color encoding: the whole block is 1 solid color */
517 pix = bytestream2_get_byte(&s->stream_ptr);
519 for (y = 0; y < 8; y++) {
520 memset(s->pixel_ptr, pix, 8);
521 s->pixel_ptr += s->stride;
528 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s, AVFrame *frame)
531 unsigned char sample[2];
533 /* dithered encoding */
534 sample[0] = bytestream2_get_byte(&s->stream_ptr);
535 sample[1] = bytestream2_get_byte(&s->stream_ptr);
537 for (y = 0; y < 8; y++) {
538 for (x = 0; x < 8; x += 2) {
539 *s->pixel_ptr++ = sample[ y & 1 ];
540 *s->pixel_ptr++ = sample[!(y & 1)];
542 s->pixel_ptr += s->line_inc;
549 static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s, AVFrame *frame)
553 /* copy a block from the second last frame using an expanded range */
554 x = bytestream2_get_byte(&s->stream_ptr);
555 y = bytestream2_get_byte(&s->stream_ptr);
557 ff_tlog(s->avctx, "motion bytes = %d, %d\n", x, y);
558 return copy_from(s, s->second_last_frame, frame, x, y);
561 static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s, AVFrame *frame)
566 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
568 /* 2-color encoding */
569 P[0] = bytestream2_get_le16(&s->stream_ptr);
570 P[1] = bytestream2_get_le16(&s->stream_ptr);
572 if (!(P[0] & 0x8000)) {
574 for (y = 0; y < 8; y++) {
575 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
576 for (; flags != 1; flags >>= 1)
577 *pixel_ptr++ = P[flags & 1];
578 pixel_ptr += s->line_inc;
583 flags = bytestream2_get_le16(&s->stream_ptr);
584 for (y = 0; y < 8; y += 2) {
585 for (x = 0; x < 8; x += 2, flags >>= 1) {
588 pixel_ptr[x + s->stride] =
589 pixel_ptr[x + 1 + s->stride] = P[flags & 1];
591 pixel_ptr += s->stride * 2;
598 static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s, AVFrame *frame)
602 unsigned int flags = 0;
603 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
605 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
606 * either top and bottom or left and right halves */
607 P[0] = bytestream2_get_le16(&s->stream_ptr);
608 P[1] = bytestream2_get_le16(&s->stream_ptr);
610 if (!(P[0] & 0x8000)) {
612 for (y = 0; y < 16; y++) {
613 // new values for each 4x4 block
616 P[0] = bytestream2_get_le16(&s->stream_ptr);
617 P[1] = bytestream2_get_le16(&s->stream_ptr);
619 flags = bytestream2_get_le16(&s->stream_ptr);
622 for (x = 0; x < 4; x++, flags >>= 1)
623 *pixel_ptr++ = P[flags & 1];
624 pixel_ptr += s->stride - 4;
625 // switch to right half
626 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
631 flags = bytestream2_get_le32(&s->stream_ptr);
632 P[2] = bytestream2_get_le16(&s->stream_ptr);
633 P[3] = bytestream2_get_le16(&s->stream_ptr);
635 if (!(P[2] & 0x8000)) {
637 /* vertical split; left & right halves are 2-color encoded */
639 for (y = 0; y < 16; y++) {
640 for (x = 0; x < 4; x++, flags >>= 1)
641 *pixel_ptr++ = P[flags & 1];
642 pixel_ptr += s->stride - 4;
643 // switch to right half
645 pixel_ptr -= 8 * s->stride - 4;
648 flags = bytestream2_get_le32(&s->stream_ptr);
654 /* horizontal split; top & bottom halves are 2-color encoded */
656 for (y = 0; y < 8; y++) {
660 flags = bytestream2_get_le32(&s->stream_ptr);
663 for (x = 0; x < 8; x++, flags >>= 1)
664 *pixel_ptr++ = P[flags & 1];
665 pixel_ptr += s->line_inc;
674 static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s, AVFrame *frame)
678 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
680 /* 4-color encoding */
681 for (x = 0; x < 4; x++)
682 P[x] = bytestream2_get_le16(&s->stream_ptr);
684 if (!(P[0] & 0x8000)) {
685 if (!(P[2] & 0x8000)) {
687 /* 1 of 4 colors for each pixel */
688 for (y = 0; y < 8; y++) {
689 /* get the next set of 8 2-bit flags */
690 int flags = bytestream2_get_le16(&s->stream_ptr);
691 for (x = 0; x < 8; x++, flags >>= 2)
692 *pixel_ptr++ = P[flags & 0x03];
693 pixel_ptr += s->line_inc;
699 /* 1 of 4 colors for each 2x2 block */
700 flags = bytestream2_get_le32(&s->stream_ptr);
702 for (y = 0; y < 8; y += 2) {
703 for (x = 0; x < 8; x += 2, flags >>= 2) {
706 pixel_ptr[x + s->stride] =
707 pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
709 pixel_ptr += s->stride * 2;
716 /* 1 of 4 colors for each 2x1 or 1x2 block */
717 flags = bytestream2_get_le64(&s->stream_ptr);
718 if (!(P[2] & 0x8000)) {
719 for (y = 0; y < 8; y++) {
720 for (x = 0; x < 8; x += 2, flags >>= 2) {
722 pixel_ptr[x + 1] = P[flags & 0x03];
724 pixel_ptr += s->stride;
727 for (y = 0; y < 8; y += 2) {
728 for (x = 0; x < 8; x++, flags >>= 2) {
730 pixel_ptr[x + s->stride] = P[flags & 0x03];
732 pixel_ptr += s->stride * 2;
741 static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s, AVFrame *frame)
746 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
748 for (x = 0; x < 4; x++)
749 P[x] = bytestream2_get_le16(&s->stream_ptr);
751 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
752 * either top and bottom or left and right halves */
753 if (!(P[0] & 0x8000)) {
755 /* 4-color encoding for each quadrant */
756 for (y = 0; y < 16; y++) {
757 // new values for each 4x4 block
760 for (x = 0; x < 4; x++)
761 P[x] = bytestream2_get_le16(&s->stream_ptr);
762 flags = bytestream2_get_le32(&s->stream_ptr);
765 for (x = 0; x < 4; x++, flags >>= 2)
766 *pixel_ptr++ = P[flags & 0x03];
768 pixel_ptr += s->stride - 4;
769 // switch to right half
770 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
776 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
778 for (x = 4; x < 8; x++)
779 P[x] = bytestream2_get_le16(&s->stream_ptr);
780 vert = !(P[4] & 0x8000);
782 /* 4-color encoding for either left and right or top and bottom
785 for (y = 0; y < 16; y++) {
786 for (x = 0; x < 4; x++, flags >>= 2)
787 *pixel_ptr++ = P[flags & 0x03];
790 pixel_ptr += s->stride - 4;
791 // switch to right half
792 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
793 } else if (y & 1) pixel_ptr += s->line_inc;
795 // load values for second half
798 flags = bytestream2_get_le64(&s->stream_ptr);
807 static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s, AVFrame *frame)
810 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
812 /* 64-color encoding (each pixel in block is a different color) */
813 for (y = 0; y < 8; y++) {
814 for (x = 0; x < 8; x++)
815 pixel_ptr[x] = bytestream2_get_le16(&s->stream_ptr);
816 pixel_ptr += s->stride;
823 static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s, AVFrame *frame)
826 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
828 /* 16-color block encoding: each 2x2 block is a different color */
829 for (y = 0; y < 8; y += 2) {
830 for (x = 0; x < 8; x += 2) {
833 pixel_ptr[x + s->stride] =
834 pixel_ptr[x + 1 + s->stride] = bytestream2_get_le16(&s->stream_ptr);
836 pixel_ptr += s->stride * 2;
843 static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s, AVFrame *frame)
847 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
849 /* 4-color block encoding: each 4x4 block is a different color */
850 for (y = 0; y < 8; y++) {
852 P[0] = bytestream2_get_le16(&s->stream_ptr);
853 P[1] = bytestream2_get_le16(&s->stream_ptr);
855 for (x = 0; x < 8; x++)
856 pixel_ptr[x] = P[x >> 2];
857 pixel_ptr += s->stride;
864 static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s, AVFrame *frame)
868 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
870 /* 1-color encoding: the whole block is 1 solid color */
871 pix = bytestream2_get_le16(&s->stream_ptr);
873 for (y = 0; y < 8; y++) {
874 for (x = 0; x < 8; x++)
876 pixel_ptr += s->stride;
883 static int (* const ipvideo_decode_block[])(IpvideoContext *s, AVFrame *frame) = {
884 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
885 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
886 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
887 ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
888 ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
889 ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
890 ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
891 ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
894 static int (* const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame) = {
895 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
896 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
897 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
898 ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
899 ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
900 ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
901 ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
902 ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
905 static void ipvideo_decode_opcodes(IpvideoContext *s, AVFrame *frame)
908 unsigned char opcode;
912 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
914 /* this is PAL8, so make the palette available */
915 memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
917 s->stride = frame->linesize[0];
919 s->stride = frame->linesize[0] >> 1;
920 s->mv_ptr = s->stream_ptr;
921 bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
923 s->line_inc = s->stride - 8;
924 s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
925 + (s->avctx->width - 8) * (1 + s->is_16bpp);
927 init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
928 for (y = 0; y < s->avctx->height; y += 8) {
929 for (x = 0; x < s->avctx->width; x += 8) {
930 opcode = get_bits(&gb, 4);
933 " block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
934 x, y, opcode, bytestream2_tell(&s->stream_ptr));
937 s->pixel_ptr = frame->data[0] + x
938 + y*frame->linesize[0];
939 ret = ipvideo_decode_block[opcode](s, frame);
941 s->pixel_ptr = frame->data[0] + x*2
942 + y*frame->linesize[0];
943 ret = ipvideo_decode_block16[opcode](s, frame);
946 av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
947 s->avctx->frame_number, x, y);
952 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
953 av_log(s->avctx, AV_LOG_DEBUG,
954 "decode finished with %d bytes left over\n",
955 bytestream2_get_bytes_left(&s->stream_ptr));
959 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
961 IpvideoContext *s = avctx->priv_data;
965 s->is_16bpp = avctx->bits_per_coded_sample == 16;
966 avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
968 ff_hpeldsp_init(&s->hdsp, avctx->flags);
970 s->last_frame = av_frame_alloc();
971 s->second_last_frame = av_frame_alloc();
972 if (!s->last_frame || !s->second_last_frame) {
973 av_frame_free(&s->last_frame);
974 av_frame_free(&s->second_last_frame);
975 return AVERROR(ENOMEM);
981 static int ipvideo_decode_frame(AVCodecContext *avctx,
982 void *data, int *got_frame,
985 const uint8_t *buf = avpkt->data;
986 int buf_size = avpkt->size;
987 IpvideoContext *s = avctx->priv_data;
988 AVFrame *frame = data;
992 return AVERROR_INVALIDDATA;
994 /* decoding map contains 4 bits of information per 8x8 block */
995 s->decoding_map_size = AV_RL16(avpkt->data);
997 /* compressed buffer needs to be large enough to at least hold an entire
999 if (buf_size < s->decoding_map_size + 2)
1002 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
1003 av_frame_unref(s->last_frame);
1004 av_frame_unref(s->second_last_frame);
1007 s->decoding_map = buf + 2;
1008 bytestream2_init(&s->stream_ptr, buf + 2 + s->decoding_map_size,
1009 buf_size - s->decoding_map_size);
1011 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1015 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
1017 frame->palette_has_changed = 1;
1018 memcpy(s->pal, pal, AVPALETTE_SIZE);
1022 ipvideo_decode_opcodes(s, frame);
1026 /* shuffle frames */
1027 av_frame_unref(s->second_last_frame);
1028 FFSWAP(AVFrame*, s->second_last_frame, s->last_frame);
1029 if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
1032 /* report that the buffer was completely consumed */
1036 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1038 IpvideoContext *s = avctx->priv_data;
1040 av_frame_free(&s->last_frame);
1041 av_frame_free(&s->second_last_frame);
1046 AVCodec ff_interplay_video_decoder = {
1047 .name = "interplayvideo",
1048 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
1049 .type = AVMEDIA_TYPE_VIDEO,
1050 .id = AV_CODEC_ID_INTERPLAY_VIDEO,
1051 .priv_data_size = sizeof(IpvideoContext),
1052 .init = ipvideo_decode_init,
1053 .close = ipvideo_decode_end,
1054 .decode = ipvideo_decode_frame,
1055 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,