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
23 * @file libavcodec/interplayvideo.c
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 ALT_BITSTREAM_READER_LE
47 #define PALETTE_COUNT 256
49 /* debugging support */
50 #define DEBUG_INTERPLAY 0
52 #define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
54 static inline void debug_interplay(const char *format
, ...) { }
57 typedef struct IpvideoContext
{
59 AVCodecContext
*avctx
;
61 AVFrame second_last_frame
;
63 AVFrame current_frame
;
64 const unsigned char *decoding_map
;
65 int decoding_map_size
;
67 const unsigned char *buf
;
70 const unsigned char *stream_ptr
;
71 const unsigned char *stream_end
;
72 unsigned char *pixel_ptr
;
75 int upper_motion_limit_offset
;
79 #define CHECK_STREAM_PTR(n) \
80 if (s->stream_end - s->stream_ptr < n) { \
81 av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
82 s->stream_ptr + n, s->stream_end); \
86 static int copy_from(IpvideoContext
*s
, AVFrame
*src
, int delta_x
, int delta_y
)
88 int current_offset
= s
->pixel_ptr
- s
->current_frame
.data
[0];
89 int motion_offset
= current_offset
+ delta_y
* s
->stride
+ delta_x
;
90 if (motion_offset
< 0) {
91 av_log(s
->avctx
, AV_LOG_ERROR
, " Interplay video: motion offset < 0 (%d)\n", motion_offset
);
93 } else if (motion_offset
> s
->upper_motion_limit_offset
) {
94 av_log(s
->avctx
, AV_LOG_ERROR
, " Interplay video: motion offset above limit (%d >= %d)\n",
95 motion_offset
, s
->upper_motion_limit_offset
);
98 s
->dsp
.put_pixels_tab
[1][0](s
->pixel_ptr
, src
->data
[0] + motion_offset
, s
->stride
, 8);
102 static int ipvideo_decode_block_opcode_0x0(IpvideoContext
*s
)
104 return copy_from(s
, &s
->last_frame
, 0, 0);
107 static int ipvideo_decode_block_opcode_0x1(IpvideoContext
*s
)
109 return copy_from(s
, &s
->second_last_frame
, 0, 0);
112 static int ipvideo_decode_block_opcode_0x2(IpvideoContext
*s
)
117 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
119 B
= *s
->stream_ptr
++;
125 x
= -14 + ((B
- 56) % 29);
126 y
= 8 + ((B
- 56) / 29);
129 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B
, x
, y
);
130 return copy_from(s
, &s
->second_last_frame
, x
, y
);
133 static int ipvideo_decode_block_opcode_0x3(IpvideoContext
*s
)
138 /* copy 8x8 block from current frame from an up/left block */
140 /* need 1 more byte for motion */
142 B
= *s
->stream_ptr
++;
148 x
= -(-14 + ((B
- 56) % 29));
149 y
= -( 8 + ((B
- 56) / 29));
152 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B
, x
, y
);
153 return copy_from(s
, &s
->current_frame
, x
, y
);
156 static int ipvideo_decode_block_opcode_0x4(IpvideoContext
*s
)
159 unsigned char B
, BL
, BH
;
161 /* copy a block from the previous frame; need 1 more byte */
164 B
= *s
->stream_ptr
++;
166 BH
= (B
>> 4) & 0x0F;
170 debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B
, x
, y
);
171 return copy_from(s
, &s
->last_frame
, x
, y
);
174 static int ipvideo_decode_block_opcode_0x5(IpvideoContext
*s
)
178 /* copy a block from the previous frame using an expanded range;
179 * need 2 more bytes */
182 x
= *s
->stream_ptr
++;
183 y
= *s
->stream_ptr
++;
185 debug_interplay (" motion bytes = %d, %d\n", x
, y
);
186 return copy_from(s
, &s
->last_frame
, x
, y
);
189 static int ipvideo_decode_block_opcode_0x6(IpvideoContext
*s
)
191 /* mystery opcode? skip multiple blocks? */
192 av_log(s
->avctx
, AV_LOG_ERROR
, " Interplay video: Help! Mystery opcode 0x6 seen\n");
198 static int ipvideo_decode_block_opcode_0x7(IpvideoContext
*s
)
204 /* 2-color encoding */
207 P
[0] = *s
->stream_ptr
++;
208 P
[1] = *s
->stream_ptr
++;
212 /* need 8 more bytes from the stream */
215 for (y
= 0; y
< 8; y
++) {
216 flags
= *s
->stream_ptr
++ | 0x100;
217 for (; flags
!= 1; flags
>>= 1)
218 *s
->pixel_ptr
++ = P
[flags
& 1];
219 s
->pixel_ptr
+= s
->line_inc
;
224 /* need 2 more bytes from the stream */
227 flags
= bytestream_get_le16(&s
->stream_ptr
);
228 for (y
= 0; y
< 8; y
+= 2) {
229 for (x
= 0; x
< 8; x
+= 2, flags
>>= 1) {
231 s
->pixel_ptr
[x
+ 1 ] =
232 s
->pixel_ptr
[x
+ s
->stride
] =
233 s
->pixel_ptr
[x
+ 1 + s
->stride
] = P
[flags
& 1];
235 s
->pixel_ptr
+= s
->stride
* 2;
243 static int ipvideo_decode_block_opcode_0x8(IpvideoContext
*s
)
247 unsigned int flags
= 0;
249 /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
250 * either top and bottom or left and right halves */
253 P
[0] = *s
->stream_ptr
++;
254 P
[1] = *s
->stream_ptr
++;
258 CHECK_STREAM_PTR(14);
261 for (y
= 0; y
< 16; y
++) {
262 // new values for each 4x4 block
264 P
[0] = *s
->stream_ptr
++; P
[1] = *s
->stream_ptr
++;
265 flags
= bytestream_get_le16(&s
->stream_ptr
);
268 for (x
= 0; x
< 4; x
++, flags
>>= 1)
269 *s
->pixel_ptr
++ = P
[flags
& 1];
270 s
->pixel_ptr
+= s
->stride
- 4;
271 // switch to right half
272 if (y
== 7) s
->pixel_ptr
-= 8 * s
->stride
- 4;
277 /* need 10 more bytes */
278 CHECK_STREAM_PTR(10);
280 if (s
->stream_ptr
[4] <= s
->stream_ptr
[5]) {
282 flags
= bytestream_get_le32(&s
->stream_ptr
);
284 /* vertical split; left & right halves are 2-color encoded */
286 for (y
= 0; y
< 16; y
++) {
287 for (x
= 0; x
< 4; x
++, flags
>>= 1)
288 *s
->pixel_ptr
++ = P
[flags
& 1];
289 s
->pixel_ptr
+= s
->stride
- 4;
290 // switch to right half
292 s
->pixel_ptr
-= 8 * s
->stride
- 4;
293 P
[0] = *s
->stream_ptr
++; P
[1] = *s
->stream_ptr
++;
294 flags
= bytestream_get_le32(&s
->stream_ptr
);
300 /* horizontal split; top & bottom halves are 2-color encoded */
302 for (y
= 0; y
< 8; y
++) {
304 P
[0] = *s
->stream_ptr
++;
305 P
[1] = *s
->stream_ptr
++;
307 flags
= *s
->stream_ptr
++ | 0x100;
309 for (; flags
!= 1; flags
>>= 1)
310 *s
->pixel_ptr
++ = P
[flags
& 1];
311 s
->pixel_ptr
+= s
->line_inc
;
320 static int ipvideo_decode_block_opcode_0x9(IpvideoContext
*s
)
325 /* 4-color encoding */
328 memcpy(P
, s
->stream_ptr
, 4);
334 /* 1 of 4 colors for each pixel, need 16 more bytes */
335 CHECK_STREAM_PTR(16);
337 for (y
= 0; y
< 8; y
++) {
338 /* get the next set of 8 2-bit flags */
339 int flags
= bytestream_get_le16(&s
->stream_ptr
);
340 for (x
= 0; x
< 8; x
++, flags
>>= 2)
341 *s
->pixel_ptr
++ = P
[flags
& 0x03];
342 s
->pixel_ptr
+= s
->line_inc
;
348 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
351 flags
= bytestream_get_le32(&s
->stream_ptr
);
353 for (y
= 0; y
< 8; y
+= 2) {
354 for (x
= 0; x
< 8; x
+= 2, flags
>>= 2) {
356 s
->pixel_ptr
[x
+ 1 ] =
357 s
->pixel_ptr
[x
+ s
->stride
] =
358 s
->pixel_ptr
[x
+ 1 + s
->stride
] = P
[flags
& 0x03];
360 s
->pixel_ptr
+= s
->stride
* 2;
367 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
370 flags
= bytestream_get_le64(&s
->stream_ptr
);
372 for (y
= 0; y
< 8; y
++) {
373 for (x
= 0; x
< 8; x
+= 2, flags
>>= 2) {
375 s
->pixel_ptr
[x
+ 1] = P
[flags
& 0x03];
377 s
->pixel_ptr
+= s
->stride
;
380 for (y
= 0; y
< 8; y
+= 2) {
381 for (x
= 0; x
< 8; x
++, flags
>>= 2) {
383 s
->pixel_ptr
[x
+ s
->stride
] = P
[flags
& 0x03];
385 s
->pixel_ptr
+= s
->stride
* 2;
394 static int ipvideo_decode_block_opcode_0xA(IpvideoContext
*s
)
400 /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
401 * either top and bottom or left and right halves */
402 CHECK_STREAM_PTR(24);
404 if (s
->stream_ptr
[0] <= s
->stream_ptr
[1]) {
406 /* 4-color encoding for each quadrant; need 32 bytes */
407 CHECK_STREAM_PTR(32);
409 for (y
= 0; y
< 16; y
++) {
410 // new values for each 4x4 block
412 memcpy(P
, s
->stream_ptr
, 4);
414 flags
= bytestream_get_le32(&s
->stream_ptr
);
417 for (x
= 0; x
< 4; x
++, flags
>>= 2)
418 *s
->pixel_ptr
++ = P
[flags
& 0x03];
420 s
->pixel_ptr
+= s
->stride
- 4;
421 // switch to right half
422 if (y
== 7) s
->pixel_ptr
-= 8 * s
->stride
- 4;
427 int vert
= s
->stream_ptr
[12] <= s
->stream_ptr
[13];
430 /* 4-color encoding for either left and right or top and bottom
433 for (y
= 0; y
< 16; y
++) {
434 // load values for each half
436 memcpy(P
, s
->stream_ptr
, 4);
438 flags
= bytestream_get_le64(&s
->stream_ptr
);
441 for (x
= 0; x
< 4; x
++, flags
>>= 2)
442 *s
->pixel_ptr
++ = P
[flags
& 0x03];
445 s
->pixel_ptr
+= s
->stride
- 4;
446 // switch to right half
447 if (y
== 7) s
->pixel_ptr
-= 8 * s
->stride
- 4;
448 } else if (y
& 1) s
->pixel_ptr
+= s
->line_inc
;
456 static int ipvideo_decode_block_opcode_0xB(IpvideoContext
*s
)
460 /* 64-color encoding (each pixel in block is a different color) */
461 CHECK_STREAM_PTR(64);
463 for (y
= 0; y
< 8; y
++) {
464 memcpy(s
->pixel_ptr
, s
->stream_ptr
, 8);
466 s
->pixel_ptr
+= s
->stride
;
473 static int ipvideo_decode_block_opcode_0xC(IpvideoContext
*s
)
477 /* 16-color block encoding: each 2x2 block is a different color */
478 CHECK_STREAM_PTR(16);
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
] = *s
->stream_ptr
++;
487 s
->pixel_ptr
+= s
->stride
* 2;
494 static int ipvideo_decode_block_opcode_0xD(IpvideoContext
*s
)
499 /* 4-color block encoding: each 4x4 block is a different color */
502 for (y
= 0; y
< 8; y
++) {
504 P
[0] = *s
->stream_ptr
++;
505 P
[1] = *s
->stream_ptr
++;
507 memset(s
->pixel_ptr
, P
[0], 4);
508 memset(s
->pixel_ptr
+ 4, P
[1], 4);
509 s
->pixel_ptr
+= s
->stride
;
516 static int ipvideo_decode_block_opcode_0xE(IpvideoContext
*s
)
521 /* 1-color encoding: the whole block is 1 solid color */
523 pix
= *s
->stream_ptr
++;
525 for (y
= 0; y
< 8; y
++) {
526 memset(s
->pixel_ptr
, pix
, 8);
527 s
->pixel_ptr
+= s
->stride
;
534 static int ipvideo_decode_block_opcode_0xF(IpvideoContext
*s
)
537 unsigned char sample
[2];
539 /* dithered encoding */
541 sample
[0] = *s
->stream_ptr
++;
542 sample
[1] = *s
->stream_ptr
++;
544 for (y
= 0; y
< 8; y
++) {
545 for (x
= 0; x
< 8; x
+= 2) {
546 *s
->pixel_ptr
++ = sample
[ y
& 1 ];
547 *s
->pixel_ptr
++ = sample
[!(y
& 1)];
549 s
->pixel_ptr
+= s
->line_inc
;
556 static int (* const ipvideo_decode_block
[])(IpvideoContext
*s
) = {
557 ipvideo_decode_block_opcode_0x0
, ipvideo_decode_block_opcode_0x1
,
558 ipvideo_decode_block_opcode_0x2
, ipvideo_decode_block_opcode_0x3
,
559 ipvideo_decode_block_opcode_0x4
, ipvideo_decode_block_opcode_0x5
,
560 ipvideo_decode_block_opcode_0x6
, ipvideo_decode_block_opcode_0x7
,
561 ipvideo_decode_block_opcode_0x8
, ipvideo_decode_block_opcode_0x9
,
562 ipvideo_decode_block_opcode_0xA
, ipvideo_decode_block_opcode_0xB
,
563 ipvideo_decode_block_opcode_0xC
, ipvideo_decode_block_opcode_0xD
,
564 ipvideo_decode_block_opcode_0xE
, ipvideo_decode_block_opcode_0xF
,
567 static void ipvideo_decode_opcodes(IpvideoContext
*s
)
570 unsigned char opcode
;
572 static int frame
= 0;
575 debug_interplay("------------------ frame %d\n", frame
);
578 /* this is PAL8, so make the palette available */
579 memcpy(s
->current_frame
.data
[1], s
->avctx
->palctrl
->palette
, PALETTE_COUNT
* 4);
581 s
->stride
= s
->current_frame
.linesize
[0];
582 s
->stream_ptr
= s
->buf
+ 14; /* data starts 14 bytes in */
583 s
->stream_end
= s
->buf
+ s
->size
;
584 s
->line_inc
= s
->stride
- 8;
585 s
->upper_motion_limit_offset
= (s
->avctx
->height
- 8) * s
->stride
586 + s
->avctx
->width
- 8;
588 init_get_bits(&gb
, s
->decoding_map
, s
->decoding_map_size
* 8);
589 for (y
= 0; y
< (s
->stride
* s
->avctx
->height
); y
+= s
->stride
* 8) {
590 for (x
= y
; x
< y
+ s
->avctx
->width
; x
+= 8) {
591 opcode
= get_bits(&gb
, 4);
593 debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
594 x
- y
, y
/ s
->stride
, opcode
, s
->stream_ptr
);
596 s
->pixel_ptr
= s
->current_frame
.data
[0] + x
;
597 ret
= ipvideo_decode_block
[opcode
](s
);
599 av_log(s
->avctx
, AV_LOG_ERROR
, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
600 frame
, x
- y
, y
/ s
->stride
);
605 if (s
->stream_end
- s
->stream_ptr
> 1) {
606 av_log(s
->avctx
, AV_LOG_ERROR
, " Interplay video: decode finished with %td bytes left over\n",
607 s
->stream_end
- s
->stream_ptr
);
611 static av_cold
int ipvideo_decode_init(AVCodecContext
*avctx
)
613 IpvideoContext
*s
= avctx
->priv_data
;
617 if (s
->avctx
->palctrl
== NULL
) {
618 av_log(avctx
, AV_LOG_ERROR
, " Interplay video: palette expected.\n");
622 avctx
->pix_fmt
= PIX_FMT_PAL8
;
623 dsputil_init(&s
->dsp
, avctx
);
625 /* decoding map contains 4 bits of information per 8x8 block */
626 s
->decoding_map_size
= avctx
->width
* avctx
->height
/ (8 * 8 * 2);
628 s
->current_frame
.data
[0] = s
->last_frame
.data
[0] =
629 s
->second_last_frame
.data
[0] = NULL
;
634 static int ipvideo_decode_frame(AVCodecContext
*avctx
,
635 void *data
, int *data_size
,
638 const uint8_t *buf
= avpkt
->data
;
639 int buf_size
= avpkt
->size
;
640 IpvideoContext
*s
= avctx
->priv_data
;
641 AVPaletteControl
*palette_control
= avctx
->palctrl
;
643 /* compressed buffer needs to be large enough to at least hold an entire
645 if (buf_size
< s
->decoding_map_size
)
648 s
->decoding_map
= buf
;
649 s
->buf
= buf
+ s
->decoding_map_size
;
650 s
->size
= buf_size
- s
->decoding_map_size
;
652 s
->current_frame
.reference
= 3;
653 if (avctx
->get_buffer(avctx
, &s
->current_frame
)) {
654 av_log(avctx
, AV_LOG_ERROR
, " Interplay Video: get_buffer() failed\n");
658 ipvideo_decode_opcodes(s
);
660 if (palette_control
->palette_changed
) {
661 palette_control
->palette_changed
= 0;
662 s
->current_frame
.palette_has_changed
= 1;
665 *data_size
= sizeof(AVFrame
);
666 *(AVFrame
*)data
= s
->current_frame
;
669 if (s
->second_last_frame
.data
[0])
670 avctx
->release_buffer(avctx
, &s
->second_last_frame
);
671 s
->second_last_frame
= s
->last_frame
;
672 s
->last_frame
= s
->current_frame
;
673 s
->current_frame
.data
[0] = NULL
; /* catch any access attempts */
675 /* report that the buffer was completely consumed */
679 static av_cold
int ipvideo_decode_end(AVCodecContext
*avctx
)
681 IpvideoContext
*s
= avctx
->priv_data
;
683 /* release the last frame */
684 if (s
->last_frame
.data
[0])
685 avctx
->release_buffer(avctx
, &s
->last_frame
);
686 if (s
->second_last_frame
.data
[0])
687 avctx
->release_buffer(avctx
, &s
->second_last_frame
);
692 AVCodec interplay_video_decoder
= {
695 CODEC_ID_INTERPLAY_VIDEO
,
696 sizeof(IpvideoContext
),
700 ipvideo_decode_frame
,
702 .long_name
= NULL_IF_CONFIG_SMALL("Interplay MVE video"),