2 * Interplay MVE Video Decoder
3 * Copyright (C) 2003 the ffmpeg project
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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.
42 #include "bytestream.h"
44 #define BITSTREAM_READER_LE
48 #define PALETTE_COUNT 256
50 typedef struct IpvideoContext {
52 AVCodecContext *avctx;
54 AVFrame second_last_frame;
56 AVFrame current_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, int delta_x, int delta_y)
72 int current_offset = s->pixel_ptr - s->current_frame.data[0];
73 int motion_offset = current_offset + delta_y * s->current_frame.linesize[0]
74 + delta_x * (1 + s->is_16bpp);
75 if (motion_offset < 0) {
76 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset);
78 } else if (motion_offset > s->upper_motion_limit_offset) {
79 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n",
80 motion_offset, s->upper_motion_limit_offset);
83 if (src->data[0] == NULL) {
84 av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
85 return AVERROR(EINVAL);
87 s->dsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
88 s->current_frame.linesize[0], 8);
92 static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
94 return copy_from(s, &s->last_frame, 0, 0);
97 static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
99 return copy_from(s, &s->second_last_frame, 0, 0);
102 static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
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 av_dlog(NULL, " motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
123 return copy_from(s, &s->second_last_frame, x, y);
126 static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
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 av_dlog(NULL, " motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
149 return copy_from(s, &s->current_frame, x, y);
152 static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
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 av_dlog(NULL, " motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
170 return copy_from(s, &s->last_frame, x, y);
173 static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
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 av_dlog(NULL, " motion bytes = %d, %d\n", x, y);
183 return copy_from(s, &s->last_frame, x, y);
186 static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
188 /* mystery opcode? skip multiple blocks? */
189 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: Help! Mystery opcode 0x6 seen\n");
195 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
201 /* 2-color encoding */
202 P[0] = bytestream2_get_byte(&s->stream_ptr);
203 P[1] = bytestream2_get_byte(&s->stream_ptr);
207 /* need 8 more bytes from the stream */
208 for (y = 0; y < 8; y++) {
209 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
210 for (; flags != 1; flags >>= 1)
211 *s->pixel_ptr++ = P[flags & 1];
212 s->pixel_ptr += s->line_inc;
217 /* need 2 more bytes from the stream */
218 flags = bytestream2_get_le16(&s->stream_ptr);
219 for (y = 0; y < 8; y += 2) {
220 for (x = 0; x < 8; x += 2, flags >>= 1) {
222 s->pixel_ptr[x + 1 ] =
223 s->pixel_ptr[x + s->stride] =
224 s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
226 s->pixel_ptr += s->stride * 2;
234 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
238 unsigned int flags = 0;
240 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
241 * either top and bottom or left and right halves */
242 P[0] = bytestream2_get_byte(&s->stream_ptr);
243 P[1] = bytestream2_get_byte(&s->stream_ptr);
246 for (y = 0; y < 16; y++) {
247 // new values for each 4x4 block
250 P[0] = bytestream2_get_byte(&s->stream_ptr);
251 P[1] = bytestream2_get_byte(&s->stream_ptr);
253 flags = bytestream2_get_le16(&s->stream_ptr);
256 for (x = 0; x < 4; x++, flags >>= 1)
257 *s->pixel_ptr++ = P[flags & 1];
258 s->pixel_ptr += s->stride - 4;
259 // switch to right half
260 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
264 flags = bytestream2_get_le32(&s->stream_ptr);
265 P[2] = bytestream2_get_byte(&s->stream_ptr);
266 P[3] = bytestream2_get_byte(&s->stream_ptr);
270 /* vertical split; left & right halves are 2-color encoded */
272 for (y = 0; y < 16; y++) {
273 for (x = 0; x < 4; x++, flags >>= 1)
274 *s->pixel_ptr++ = P[flags & 1];
275 s->pixel_ptr += s->stride - 4;
276 // switch to right half
278 s->pixel_ptr -= 8 * s->stride - 4;
281 flags = bytestream2_get_le32(&s->stream_ptr);
287 /* horizontal split; top & bottom halves are 2-color encoded */
289 for (y = 0; y < 8; y++) {
293 flags = bytestream2_get_le32(&s->stream_ptr);
296 for (x = 0; x < 8; x++, flags >>= 1)
297 *s->pixel_ptr++ = P[flags & 1];
298 s->pixel_ptr += s->line_inc;
307 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
312 /* 4-color encoding */
313 bytestream2_get_buffer(&s->stream_ptr, P, 4);
318 /* 1 of 4 colors for each pixel, need 16 more bytes */
319 for (y = 0; y < 8; y++) {
320 /* get the next set of 8 2-bit flags */
321 int flags = bytestream2_get_le16(&s->stream_ptr);
322 for (x = 0; x < 8; x++, flags >>= 2)
323 *s->pixel_ptr++ = P[flags & 0x03];
324 s->pixel_ptr += s->line_inc;
330 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
331 flags = bytestream2_get_le32(&s->stream_ptr);
333 for (y = 0; y < 8; y += 2) {
334 for (x = 0; x < 8; x += 2, flags >>= 2) {
336 s->pixel_ptr[x + 1 ] =
337 s->pixel_ptr[x + s->stride] =
338 s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
340 s->pixel_ptr += s->stride * 2;
347 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
348 flags = bytestream2_get_le64(&s->stream_ptr);
350 for (y = 0; y < 8; y++) {
351 for (x = 0; x < 8; x += 2, flags >>= 2) {
353 s->pixel_ptr[x + 1] = P[flags & 0x03];
355 s->pixel_ptr += s->stride;
358 for (y = 0; y < 8; y += 2) {
359 for (x = 0; x < 8; x++, flags >>= 2) {
361 s->pixel_ptr[x + s->stride] = P[flags & 0x03];
363 s->pixel_ptr += s->stride * 2;
372 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
378 bytestream2_get_buffer(&s->stream_ptr, P, 4);
380 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
381 * either top and bottom or left and right halves */
384 /* 4-color encoding for each quadrant; need 32 bytes */
385 for (y = 0; y < 16; y++) {
386 // new values for each 4x4 block
388 if (y) bytestream2_get_buffer(&s->stream_ptr, P, 4);
389 flags = bytestream2_get_le32(&s->stream_ptr);
392 for (x = 0; x < 4; x++, flags >>= 2)
393 *s->pixel_ptr++ = P[flags & 0x03];
395 s->pixel_ptr += s->stride - 4;
396 // switch to right half
397 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
403 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
405 bytestream2_get_buffer(&s->stream_ptr, P + 4, 4);
408 /* 4-color encoding for either left and right or top and bottom
411 for (y = 0; y < 16; y++) {
412 for (x = 0; x < 4; x++, flags >>= 2)
413 *s->pixel_ptr++ = P[flags & 0x03];
416 s->pixel_ptr += s->stride - 4;
417 // switch to right half
418 if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
419 } else if (y & 1) s->pixel_ptr += s->line_inc;
421 // load values for second half
424 flags = bytestream2_get_le64(&s->stream_ptr);
433 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
437 /* 64-color encoding (each pixel in block is a different color) */
438 for (y = 0; y < 8; y++) {
439 bytestream2_get_buffer(&s->stream_ptr, s->pixel_ptr, 8);
440 s->pixel_ptr += s->stride;
447 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
451 /* 16-color block encoding: each 2x2 block is a different color */
452 for (y = 0; y < 8; y += 2) {
453 for (x = 0; x < 8; x += 2) {
455 s->pixel_ptr[x + 1 ] =
456 s->pixel_ptr[x + s->stride] =
457 s->pixel_ptr[x + 1 + s->stride] = bytestream2_get_byte(&s->stream_ptr);
459 s->pixel_ptr += s->stride * 2;
466 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
471 /* 4-color block encoding: each 4x4 block is a different color */
472 for (y = 0; y < 8; y++) {
474 P[0] = bytestream2_get_byte(&s->stream_ptr);
475 P[1] = bytestream2_get_byte(&s->stream_ptr);
477 memset(s->pixel_ptr, P[0], 4);
478 memset(s->pixel_ptr + 4, P[1], 4);
479 s->pixel_ptr += s->stride;
486 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
491 /* 1-color encoding: the whole block is 1 solid color */
492 pix = bytestream2_get_byte(&s->stream_ptr);
494 for (y = 0; y < 8; y++) {
495 memset(s->pixel_ptr, pix, 8);
496 s->pixel_ptr += s->stride;
503 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
506 unsigned char sample[2];
508 /* dithered encoding */
509 sample[0] = bytestream2_get_byte(&s->stream_ptr);
510 sample[1] = bytestream2_get_byte(&s->stream_ptr);
512 for (y = 0; y < 8; y++) {
513 for (x = 0; x < 8; x += 2) {
514 *s->pixel_ptr++ = sample[ y & 1 ];
515 *s->pixel_ptr++ = sample[!(y & 1)];
517 s->pixel_ptr += s->line_inc;
524 static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s)
528 /* copy a block from the second last frame using an expanded range */
529 x = bytestream2_get_byte(&s->stream_ptr);
530 y = bytestream2_get_byte(&s->stream_ptr);
532 av_dlog(NULL, " motion bytes = %d, %d\n", x, y);
533 return copy_from(s, &s->second_last_frame, x, y);
536 static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s)
541 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
543 /* 2-color encoding */
544 P[0] = bytestream2_get_le16(&s->stream_ptr);
545 P[1] = bytestream2_get_le16(&s->stream_ptr);
547 if (!(P[0] & 0x8000)) {
549 for (y = 0; y < 8; y++) {
550 flags = bytestream2_get_byte(&s->stream_ptr) | 0x100;
551 for (; flags != 1; flags >>= 1)
552 *pixel_ptr++ = P[flags & 1];
553 pixel_ptr += s->line_inc;
558 flags = bytestream2_get_le16(&s->stream_ptr);
559 for (y = 0; y < 8; y += 2) {
560 for (x = 0; x < 8; x += 2, flags >>= 1) {
563 pixel_ptr[x + s->stride] =
564 pixel_ptr[x + 1 + s->stride] = P[flags & 1];
566 pixel_ptr += s->stride * 2;
573 static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s)
577 unsigned int flags = 0;
578 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
580 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
581 * either top and bottom or left and right halves */
582 P[0] = bytestream2_get_le16(&s->stream_ptr);
583 P[1] = bytestream2_get_le16(&s->stream_ptr);
585 if (!(P[0] & 0x8000)) {
587 for (y = 0; y < 16; y++) {
588 // new values for each 4x4 block
591 P[0] = bytestream2_get_le16(&s->stream_ptr);
592 P[1] = bytestream2_get_le16(&s->stream_ptr);
594 flags = bytestream2_get_le16(&s->stream_ptr);
597 for (x = 0; x < 4; x++, flags >>= 1)
598 *pixel_ptr++ = P[flags & 1];
599 pixel_ptr += s->stride - 4;
600 // switch to right half
601 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
606 flags = bytestream2_get_le32(&s->stream_ptr);
607 P[2] = bytestream2_get_le16(&s->stream_ptr);
608 P[3] = bytestream2_get_le16(&s->stream_ptr);
610 if (!(P[2] & 0x8000)) {
612 /* vertical split; left & right halves are 2-color encoded */
614 for (y = 0; y < 16; y++) {
615 for (x = 0; x < 4; x++, flags >>= 1)
616 *pixel_ptr++ = P[flags & 1];
617 pixel_ptr += s->stride - 4;
618 // switch to right half
620 pixel_ptr -= 8 * s->stride - 4;
623 flags = bytestream2_get_le32(&s->stream_ptr);
629 /* horizontal split; top & bottom halves are 2-color encoded */
631 for (y = 0; y < 8; y++) {
635 flags = bytestream2_get_le32(&s->stream_ptr);
638 for (x = 0; x < 8; x++, flags >>= 1)
639 *pixel_ptr++ = P[flags & 1];
640 pixel_ptr += s->line_inc;
649 static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s)
653 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
655 /* 4-color encoding */
656 for (x = 0; x < 4; x++)
657 P[x] = bytestream2_get_le16(&s->stream_ptr);
659 if (!(P[0] & 0x8000)) {
660 if (!(P[2] & 0x8000)) {
662 /* 1 of 4 colors for each pixel */
663 for (y = 0; y < 8; y++) {
664 /* get the next set of 8 2-bit flags */
665 int flags = bytestream2_get_le16(&s->stream_ptr);
666 for (x = 0; x < 8; x++, flags >>= 2)
667 *pixel_ptr++ = P[flags & 0x03];
668 pixel_ptr += s->line_inc;
674 /* 1 of 4 colors for each 2x2 block */
675 flags = bytestream2_get_le32(&s->stream_ptr);
677 for (y = 0; y < 8; y += 2) {
678 for (x = 0; x < 8; x += 2, flags >>= 2) {
681 pixel_ptr[x + s->stride] =
682 pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
684 pixel_ptr += s->stride * 2;
691 /* 1 of 4 colors for each 2x1 or 1x2 block */
692 flags = bytestream2_get_le64(&s->stream_ptr);
693 if (!(P[2] & 0x8000)) {
694 for (y = 0; y < 8; y++) {
695 for (x = 0; x < 8; x += 2, flags >>= 2) {
697 pixel_ptr[x + 1] = P[flags & 0x03];
699 pixel_ptr += s->stride;
702 for (y = 0; y < 8; y += 2) {
703 for (x = 0; x < 8; x++, flags >>= 2) {
705 pixel_ptr[x + s->stride] = P[flags & 0x03];
707 pixel_ptr += s->stride * 2;
716 static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s)
721 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
723 for (x = 0; x < 4; x++)
724 P[x] = bytestream2_get_le16(&s->stream_ptr);
726 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
727 * either top and bottom or left and right halves */
728 if (!(P[0] & 0x8000)) {
730 /* 4-color encoding for each quadrant */
731 for (y = 0; y < 16; y++) {
732 // new values for each 4x4 block
735 for (x = 0; x < 4; x++)
736 P[x] = bytestream2_get_le16(&s->stream_ptr);
737 flags = bytestream2_get_le32(&s->stream_ptr);
740 for (x = 0; x < 4; x++, flags >>= 2)
741 *pixel_ptr++ = P[flags & 0x03];
743 pixel_ptr += s->stride - 4;
744 // switch to right half
745 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
751 uint64_t flags = bytestream2_get_le64(&s->stream_ptr);
753 for (x = 4; x < 8; x++)
754 P[x] = bytestream2_get_le16(&s->stream_ptr);
755 vert = !(P[4] & 0x8000);
757 /* 4-color encoding for either left and right or top and bottom
760 for (y = 0; y < 16; y++) {
761 for (x = 0; x < 4; x++, flags >>= 2)
762 *pixel_ptr++ = P[flags & 0x03];
765 pixel_ptr += s->stride - 4;
766 // switch to right half
767 if (y == 7) pixel_ptr -= 8 * s->stride - 4;
768 } else if (y & 1) pixel_ptr += s->line_inc;
770 // load values for second half
773 flags = bytestream2_get_le64(&s->stream_ptr);
782 static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s)
785 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
787 /* 64-color encoding (each pixel in block is a different color) */
788 for (y = 0; y < 8; y++) {
789 for (x = 0; x < 8; x++)
790 pixel_ptr[x] = bytestream2_get_le16(&s->stream_ptr);
791 pixel_ptr += s->stride;
798 static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s)
801 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
803 /* 16-color block encoding: each 2x2 block is a different color */
804 for (y = 0; y < 8; y += 2) {
805 for (x = 0; x < 8; x += 2) {
808 pixel_ptr[x + s->stride] =
809 pixel_ptr[x + 1 + s->stride] = bytestream2_get_le16(&s->stream_ptr);
811 pixel_ptr += s->stride * 2;
818 static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s)
822 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
824 /* 4-color block encoding: each 4x4 block is a different color */
825 for (y = 0; y < 8; y++) {
827 P[0] = bytestream2_get_le16(&s->stream_ptr);
828 P[1] = bytestream2_get_le16(&s->stream_ptr);
830 for (x = 0; x < 8; x++)
831 pixel_ptr[x] = P[x >> 2];
832 pixel_ptr += s->stride;
839 static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s)
843 uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
845 /* 1-color encoding: the whole block is 1 solid color */
846 pix = bytestream2_get_le16(&s->stream_ptr);
848 for (y = 0; y < 8; y++) {
849 for (x = 0; x < 8; x++)
851 pixel_ptr += s->stride;
858 static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
859 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
860 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
861 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
862 ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
863 ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
864 ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
865 ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
866 ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
869 static int (* const ipvideo_decode_block16[])(IpvideoContext *s) = {
870 ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
871 ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
872 ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
873 ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
874 ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
875 ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
876 ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
877 ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
880 static void ipvideo_decode_opcodes(IpvideoContext *s)
883 unsigned char opcode;
885 static int frame = 0;
888 av_dlog(NULL, "------------------ frame %d\n", frame);
891 bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
893 /* this is PAL8, so make the palette available */
894 memcpy(s->current_frame.data[1], s->pal, AVPALETTE_SIZE);
896 s->stride = s->current_frame.linesize[0];
898 s->stride = s->current_frame.linesize[0] >> 1;
899 s->mv_ptr = s->stream_ptr;
900 bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
902 s->line_inc = s->stride - 8;
903 s->upper_motion_limit_offset = (s->avctx->height - 8) * s->current_frame.linesize[0]
904 + (s->avctx->width - 8) * (1 + s->is_16bpp);
906 init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
907 for (y = 0; y < s->avctx->height; y += 8) {
908 for (x = 0; x < s->avctx->width; x += 8) {
909 opcode = get_bits(&gb, 4);
912 " block @ (%3d, %3d): encoding 0x%X, data ptr offset %d\n",
913 x, y, opcode, bytestream2_tell(&s->stream_ptr));
916 s->pixel_ptr = s->current_frame.data[0] + x
917 + y*s->current_frame.linesize[0];
918 ret = ipvideo_decode_block[opcode](s);
920 s->pixel_ptr = s->current_frame.data[0] + x*2
921 + y*s->current_frame.linesize[0];
922 ret = ipvideo_decode_block16[opcode](s);
925 av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
931 if (bytestream2_get_bytes_left(&s->stream_ptr) > 1) {
932 av_log(s->avctx, AV_LOG_ERROR,
933 "Interplay video: decode finished with %d bytes left over\n",
934 bytestream2_get_bytes_left(&s->stream_ptr));
938 static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
940 IpvideoContext *s = avctx->priv_data;
944 s->is_16bpp = avctx->bits_per_coded_sample == 16;
945 avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
947 ff_dsputil_init(&s->dsp, avctx);
949 s->current_frame.data[0] = s->last_frame.data[0] =
950 s->second_last_frame.data[0] = NULL;
955 static int ipvideo_decode_frame(AVCodecContext *avctx,
956 void *data, int *data_size,
959 const uint8_t *buf = avpkt->data;
960 int buf_size = avpkt->size;
961 IpvideoContext *s = avctx->priv_data;
963 /* decoding map contains 4 bits of information per 8x8 block */
964 s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
966 /* compressed buffer needs to be large enough to at least hold an entire
968 if (buf_size < s->decoding_map_size)
971 s->decoding_map = buf;
972 bytestream2_init(&s->stream_ptr, buf + s->decoding_map_size,
973 buf_size - s->decoding_map_size);
975 s->current_frame.reference = 3;
976 if (ff_get_buffer(avctx, &s->current_frame)) {
977 av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
982 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
984 s->current_frame.palette_has_changed = 1;
985 memcpy(s->pal, pal, AVPALETTE_SIZE);
989 ipvideo_decode_opcodes(s);
991 *data_size = sizeof(AVFrame);
992 *(AVFrame*)data = s->current_frame;
995 if (s->second_last_frame.data[0])
996 avctx->release_buffer(avctx, &s->second_last_frame);
997 s->second_last_frame = s->last_frame;
998 s->last_frame = s->current_frame;
999 s->current_frame.data[0] = NULL; /* catch any access attempts */
1001 /* report that the buffer was completely consumed */
1005 static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
1007 IpvideoContext *s = avctx->priv_data;
1009 /* release the last frame */
1010 if (s->last_frame.data[0])
1011 avctx->release_buffer(avctx, &s->last_frame);
1012 if (s->second_last_frame.data[0])
1013 avctx->release_buffer(avctx, &s->second_last_frame);
1018 AVCodec ff_interplay_video_decoder = {
1019 .name = "interplayvideo",
1020 .type = AVMEDIA_TYPE_VIDEO,
1021 .id = AV_CODEC_ID_INTERPLAY_VIDEO,
1022 .priv_data_size = sizeof(IpvideoContext),
1023 .init = ipvideo_decode_init,
1024 .close = ipvideo_decode_end,
1025 .decode = ipvideo_decode_frame,
1026 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_PARAM_CHANGE,
1027 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),