ac3dec: simplify zero-bit mantissa dithering by calculating it
[FFMpeg-mirror/lagarith.git] / libavcodec / interplayvideo.c
blobd5b73a1163739fed3fb90a21b91e30ead2a993c8
1 /*
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
22 /**
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.
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include "avcodec.h"
42 #include "bytestream.h"
43 #include "dsputil.h"
44 #define ALT_BITSTREAM_READER_LE
45 #include "get_bits.h"
47 #define PALETTE_COUNT 256
49 /* debugging support */
50 #define DEBUG_INTERPLAY 0
51 #if DEBUG_INTERPLAY
52 #define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
53 #else
54 static inline void debug_interplay(const char *format, ...) { }
55 #endif
57 typedef struct IpvideoContext {
59 AVCodecContext *avctx;
60 DSPContext dsp;
61 AVFrame second_last_frame;
62 AVFrame last_frame;
63 AVFrame current_frame;
64 const unsigned char *decoding_map;
65 int decoding_map_size;
67 const unsigned char *buf;
68 int size;
70 const unsigned char *stream_ptr;
71 const unsigned char *stream_end;
72 unsigned char *pixel_ptr;
73 int line_inc;
74 int stride;
75 int upper_motion_limit_offset;
77 } IpvideoContext;
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); \
83 return -1; \
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);
92 return -1;
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);
96 return -1;
98 s->dsp.put_pixels_tab[1][0](s->pixel_ptr, src->data[0] + motion_offset, s->stride, 8);
99 return 0;
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)
114 unsigned char B;
115 int x, y;
117 /* copy block from 2 frames ago using a motion vector; need 1 more byte */
118 CHECK_STREAM_PTR(1);
119 B = *s->stream_ptr++;
121 if (B < 56) {
122 x = 8 + (B % 7);
123 y = B / 7;
124 } else {
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)
135 unsigned char B;
136 int x, y;
138 /* copy 8x8 block from current frame from an up/left block */
140 /* need 1 more byte for motion */
141 CHECK_STREAM_PTR(1);
142 B = *s->stream_ptr++;
144 if (B < 56) {
145 x = -(8 + (B % 7));
146 y = -(B / 7);
147 } else {
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)
158 int x, y;
159 unsigned char B, BL, BH;
161 /* copy a block from the previous frame; need 1 more byte */
162 CHECK_STREAM_PTR(1);
164 B = *s->stream_ptr++;
165 BL = B & 0x0F;
166 BH = (B >> 4) & 0x0F;
167 x = -8 + BL;
168 y = -8 + BH;
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)
176 signed char x, y;
178 /* copy a block from the previous frame using an expanded range;
179 * need 2 more bytes */
180 CHECK_STREAM_PTR(2);
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");
194 /* report success */
195 return 0;
198 static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
200 int x, y;
201 unsigned char P[2];
202 unsigned int flags;
204 /* 2-color encoding */
205 CHECK_STREAM_PTR(2);
207 P[0] = *s->stream_ptr++;
208 P[1] = *s->stream_ptr++;
210 if (P[0] <= P[1]) {
212 /* need 8 more bytes from the stream */
213 CHECK_STREAM_PTR(8);
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;
222 } else {
224 /* need 2 more bytes from the stream */
225 CHECK_STREAM_PTR(2);
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) {
230 s->pixel_ptr[x ] =
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;
239 /* report success */
240 return 0;
243 static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
245 int x, y;
246 unsigned char P[2];
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 */
251 CHECK_STREAM_PTR(2);
253 P[0] = *s->stream_ptr++;
254 P[1] = *s->stream_ptr++;
256 if (P[0] <= P[1]) {
258 CHECK_STREAM_PTR(14);
259 s->stream_ptr -= 2;
261 for (y = 0; y < 16; y++) {
262 // new values for each 4x4 block
263 if (!(y & 3)) {
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;
275 } else {
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
291 if (y == 7) {
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);
298 } else {
300 /* horizontal split; top & bottom halves are 2-color encoded */
302 for (y = 0; y < 8; y++) {
303 if (y == 4) {
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;
316 /* report success */
317 return 0;
320 static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
322 int x, y;
323 unsigned char P[4];
325 /* 4-color encoding */
326 CHECK_STREAM_PTR(4);
328 memcpy(P, s->stream_ptr, 4);
329 s->stream_ptr += 4;
331 if (P[0] <= P[1]) {
332 if (P[2] <= P[3]) {
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;
345 } else {
346 uint32_t flags;
348 /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
349 CHECK_STREAM_PTR(4);
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) {
355 s->pixel_ptr[x ] =
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;
364 } else {
365 uint64_t flags;
367 /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
368 CHECK_STREAM_PTR(8);
370 flags = bytestream_get_le64(&s->stream_ptr);
371 if (P[2] <= P[3]) {
372 for (y = 0; y < 8; y++) {
373 for (x = 0; x < 8; x += 2, flags >>= 2) {
374 s->pixel_ptr[x ] =
375 s->pixel_ptr[x + 1] = P[flags & 0x03];
377 s->pixel_ptr += s->stride;
379 } else {
380 for (y = 0; y < 8; y += 2) {
381 for (x = 0; x < 8; x++, flags >>= 2) {
382 s->pixel_ptr[x ] =
383 s->pixel_ptr[x + s->stride] = P[flags & 0x03];
385 s->pixel_ptr += s->stride * 2;
390 /* report success */
391 return 0;
394 static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
396 int x, y;
397 unsigned char P[4];
398 int flags = 0;
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
411 if (!(y & 3)) {
412 memcpy(P, s->stream_ptr, 4);
413 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;
425 } else {
426 // vertical split?
427 int vert = s->stream_ptr[12] <= s->stream_ptr[13];
428 uint64_t flags = 0;
430 /* 4-color encoding for either left and right or top and bottom
431 * halves */
433 for (y = 0; y < 16; y++) {
434 // load values for each half
435 if (!(y & 7)) {
436 memcpy(P, s->stream_ptr, 4);
437 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];
444 if (vert) {
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;
452 /* report success */
453 return 0;
456 static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
458 int y;
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);
465 s->stream_ptr += 8;
466 s->pixel_ptr += s->stride;
469 /* report success */
470 return 0;
473 static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
475 int x, y;
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) {
482 s->pixel_ptr[x ] =
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;
490 /* report success */
491 return 0;
494 static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
496 int y;
497 unsigned char P[2];
499 /* 4-color block encoding: each 4x4 block is a different color */
500 CHECK_STREAM_PTR(4);
502 for (y = 0; y < 8; y++) {
503 if (!(y & 3)) {
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;
512 /* report success */
513 return 0;
516 static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
518 int y;
519 unsigned char pix;
521 /* 1-color encoding: the whole block is 1 solid color */
522 CHECK_STREAM_PTR(1);
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;
530 /* report success */
531 return 0;
534 static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
536 int x, y;
537 unsigned char sample[2];
539 /* dithered encoding */
540 CHECK_STREAM_PTR(2);
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;
552 /* report success */
553 return 0;
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)
569 int x, y;
570 unsigned char opcode;
571 int ret;
572 static int frame = 0;
573 GetBitContext gb;
575 debug_interplay("------------------ frame %d\n", frame);
576 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);
598 if (ret != 0) {
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);
601 return;
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;
615 s->avctx = avctx;
617 if (s->avctx->palctrl == NULL) {
618 av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
619 return -1;
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;
631 return 0;
634 static int ipvideo_decode_frame(AVCodecContext *avctx,
635 void *data, int *data_size,
636 AVPacket *avpkt)
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
644 * decoding map */
645 if (buf_size < s->decoding_map_size)
646 return buf_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");
655 return -1;
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;
668 /* shuffle frames */
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 */
676 return buf_size;
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);
689 return 0;
692 AVCodec interplay_video_decoder = {
693 "interplayvideo",
694 CODEC_TYPE_VIDEO,
695 CODEC_ID_INTERPLAY_VIDEO,
696 sizeof(IpvideoContext),
697 ipvideo_decode_init,
698 NULL,
699 ipvideo_decode_end,
700 ipvideo_decode_frame,
701 CODEC_CAP_DR1,
702 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),