lavc decoders: work with refcounted frames.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / zmbv.c
blob8cf6a0cb5bfedbc34e5eafe0b08ad6ad7d6cdc3d
1 /*
2 * Zip Motion Blocks Video (ZMBV) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
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
22 /**
23 * @file
24 * Zip Motion Blocks Video decoder
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avcodec.h"
33 #include "internal.h"
35 #include <zlib.h>
37 #define ZMBV_KEYFRAME 1
38 #define ZMBV_DELTAPAL 2
40 enum ZmbvFormat {
41 ZMBV_FMT_NONE = 0,
42 ZMBV_FMT_1BPP = 1,
43 ZMBV_FMT_2BPP = 2,
44 ZMBV_FMT_4BPP = 3,
45 ZMBV_FMT_8BPP = 4,
46 ZMBV_FMT_15BPP = 5,
47 ZMBV_FMT_16BPP = 6,
48 ZMBV_FMT_24BPP = 7,
49 ZMBV_FMT_32BPP = 8
53 * Decoder context
55 typedef struct ZmbvContext {
56 AVCodecContext *avctx;
58 int bpp;
59 unsigned int decomp_size;
60 uint8_t* decomp_buf;
61 uint8_t pal[768];
62 uint8_t *prev, *cur;
63 int width, height;
64 int fmt;
65 int comp;
66 int flags;
67 int bw, bh, bx, by;
68 int decomp_len;
69 z_stream zstream;
70 int (*decode_intra)(struct ZmbvContext *c);
71 int (*decode_xor)(struct ZmbvContext *c);
72 } ZmbvContext;
74 /**
75 * Decode XOR'ed frame - 8bpp version
78 static int zmbv_decode_xor_8(ZmbvContext *c)
80 uint8_t *src = c->decomp_buf;
81 uint8_t *output, *prev;
82 int8_t *mvec;
83 int x, y;
84 int d, dx, dy, bw2, bh2;
85 int block;
86 int i, j;
87 int mx, my;
89 output = c->cur;
90 prev = c->prev;
92 if (c->flags & ZMBV_DELTAPAL) {
93 for (i = 0; i < 768; i++)
94 c->pal[i] ^= *src++;
97 mvec = (int8_t*)src;
98 src += ((c->bx * c->by * 2 + 3) & ~3);
100 block = 0;
101 for (y = 0; y < c->height; y += c->bh) {
102 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
103 for (x = 0; x < c->width; x += c->bw) {
104 uint8_t *out, *tprev;
106 d = mvec[block] & 1;
107 dx = mvec[block] >> 1;
108 dy = mvec[block + 1] >> 1;
109 block += 2;
111 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
113 /* copy block - motion vectors out of bounds are used to zero blocks */
114 out = output + x;
115 tprev = prev + x + dx + dy * c->width;
116 mx = x + dx;
117 my = y + dy;
118 for (j = 0; j < bh2; j++) {
119 if (my + j < 0 || my + j >= c->height) {
120 memset(out, 0, bw2);
121 } else {
122 for (i = 0; i < bw2; i++) {
123 if (mx + i < 0 || mx + i >= c->width)
124 out[i] = 0;
125 else
126 out[i] = tprev[i];
129 out += c->width;
130 tprev += c->width;
133 if (d) { /* apply XOR'ed difference */
134 out = output + x;
135 for (j = 0; j < bh2; j++) {
136 for (i = 0; i < bw2; i++)
137 out[i] ^= *src++;
138 out += c->width;
142 output += c->width * c->bh;
143 prev += c->width * c->bh;
145 if (src - c->decomp_buf != c->decomp_len)
146 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
147 src-c->decomp_buf, c->decomp_len);
148 return 0;
152 * Decode XOR'ed frame - 15bpp and 16bpp version
155 static int zmbv_decode_xor_16(ZmbvContext *c)
157 uint8_t *src = c->decomp_buf;
158 uint16_t *output, *prev;
159 int8_t *mvec;
160 int x, y;
161 int d, dx, dy, bw2, bh2;
162 int block;
163 int i, j;
164 int mx, my;
166 output = (uint16_t*)c->cur;
167 prev = (uint16_t*)c->prev;
169 mvec = (int8_t*)src;
170 src += ((c->bx * c->by * 2 + 3) & ~3);
172 block = 0;
173 for (y = 0; y < c->height; y += c->bh) {
174 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
175 for (x = 0; x < c->width; x += c->bw) {
176 uint16_t *out, *tprev;
178 d = mvec[block] & 1;
179 dx = mvec[block] >> 1;
180 dy = mvec[block + 1] >> 1;
181 block += 2;
183 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
185 /* copy block - motion vectors out of bounds are used to zero blocks */
186 out = output + x;
187 tprev = prev + x + dx + dy * c->width;
188 mx = x + dx;
189 my = y + dy;
190 for (j = 0; j < bh2; j++) {
191 if (my + j < 0 || my + j >= c->height) {
192 memset(out, 0, bw2 * 2);
193 } else {
194 for (i = 0; i < bw2; i++) {
195 if (mx + i < 0 || mx + i >= c->width)
196 out[i] = 0;
197 else
198 out[i] = tprev[i];
201 out += c->width;
202 tprev += c->width;
205 if (d) { /* apply XOR'ed difference */
206 out = output + x;
207 for (j = 0; j < bh2; j++){
208 for (i = 0; i < bw2; i++) {
209 out[i] ^= *((uint16_t*)src);
210 src += 2;
212 out += c->width;
216 output += c->width * c->bh;
217 prev += c->width * c->bh;
219 if (src - c->decomp_buf != c->decomp_len)
220 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
221 src-c->decomp_buf, c->decomp_len);
222 return 0;
225 #ifdef ZMBV_ENABLE_24BPP
227 * Decode XOR'ed frame - 24bpp version
230 static int zmbv_decode_xor_24(ZmbvContext *c)
232 uint8_t *src = c->decomp_buf;
233 uint8_t *output, *prev;
234 int8_t *mvec;
235 int x, y;
236 int d, dx, dy, bw2, bh2;
237 int block;
238 int i, j;
239 int mx, my;
240 int stride;
242 output = c->cur;
243 prev = c->prev;
245 stride = c->width * 3;
246 mvec = (int8_t*)src;
247 src += ((c->bx * c->by * 2 + 3) & ~3);
249 block = 0;
250 for (y = 0; y < c->height; y += c->bh) {
251 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
252 for (x = 0; x < c->width; x += c->bw) {
253 uint8_t *out, *tprev;
255 d = mvec[block] & 1;
256 dx = mvec[block] >> 1;
257 dy = mvec[block + 1] >> 1;
258 block += 2;
260 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
262 /* copy block - motion vectors out of bounds are used to zero blocks */
263 out = output + x * 3;
264 tprev = prev + (x + dx) * 3 + dy * stride;
265 mx = x + dx;
266 my = y + dy;
267 for (j = 0; j < bh2; j++) {
268 if (my + j < 0 || my + j >= c->height) {
269 memset(out, 0, bw2 * 3);
270 } else {
271 for (i = 0; i < bw2; i++){
272 if (mx + i < 0 || mx + i >= c->width) {
273 out[i * 3 + 0] = 0;
274 out[i * 3 + 1] = 0;
275 out[i * 3 + 2] = 0;
276 } else {
277 out[i * 3 + 0] = tprev[i * 3 + 0];
278 out[i * 3 + 1] = tprev[i * 3 + 1];
279 out[i * 3 + 2] = tprev[i * 3 + 2];
283 out += stride;
284 tprev += stride;
287 if (d) { /* apply XOR'ed difference */
288 out = output + x * 3;
289 for (j = 0; j < bh2; j++) {
290 for (i = 0; i < bw2; i++) {
291 out[i * 3 + 0] ^= *src++;
292 out[i * 3 + 1] ^= *src++;
293 out[i * 3 + 2] ^= *src++;
295 out += stride;
299 output += stride * c->bh;
300 prev += stride * c->bh;
302 if (src - c->decomp_buf != c->decomp_len)
303 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
304 src-c->decomp_buf, c->decomp_len);
305 return 0;
307 #endif //ZMBV_ENABLE_24BPP
310 * Decode XOR'ed frame - 32bpp version
313 static int zmbv_decode_xor_32(ZmbvContext *c)
315 uint8_t *src = c->decomp_buf;
316 uint32_t *output, *prev;
317 int8_t *mvec;
318 int x, y;
319 int d, dx, dy, bw2, bh2;
320 int block;
321 int i, j;
322 int mx, my;
324 output = (uint32_t*)c->cur;
325 prev = (uint32_t*)c->prev;
327 mvec = (int8_t*)src;
328 src += ((c->bx * c->by * 2 + 3) & ~3);
330 block = 0;
331 for (y = 0; y < c->height; y += c->bh) {
332 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
333 for (x = 0; x < c->width; x += c->bw) {
334 uint32_t *out, *tprev;
336 d = mvec[block] & 1;
337 dx = mvec[block] >> 1;
338 dy = mvec[block + 1] >> 1;
339 block += 2;
341 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
343 /* copy block - motion vectors out of bounds are used to zero blocks */
344 out = output + x;
345 tprev = prev + x + dx + dy * c->width;
346 mx = x + dx;
347 my = y + dy;
348 for (j = 0; j < bh2; j++) {
349 if (my + j < 0 || my + j >= c->height) {
350 memset(out, 0, bw2 * 4);
351 } else {
352 for (i = 0; i < bw2; i++){
353 if (mx + i < 0 || mx + i >= c->width)
354 out[i] = 0;
355 else
356 out[i] = tprev[i];
359 out += c->width;
360 tprev += c->width;
363 if (d) { /* apply XOR'ed difference */
364 out = output + x;
365 for (j = 0; j < bh2; j++){
366 for (i = 0; i < bw2; i++) {
367 out[i] ^= *((uint32_t *) src);
368 src += 4;
370 out += c->width;
374 output += c->width * c->bh;
375 prev += c->width * c->bh;
377 if (src - c->decomp_buf != c->decomp_len)
378 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
379 src-c->decomp_buf, c->decomp_len);
380 return 0;
384 * Decode intraframe
386 static int zmbv_decode_intra(ZmbvContext *c)
388 uint8_t *src = c->decomp_buf;
390 /* make the palette available on the way out */
391 if (c->fmt == ZMBV_FMT_8BPP) {
392 memcpy(c->pal, src, 768);
393 src += 768;
396 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
397 return 0;
400 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
402 AVFrame *frame = data;
403 const uint8_t *buf = avpkt->data;
404 int buf_size = avpkt->size;
405 ZmbvContext * const c = avctx->priv_data;
406 int zret = Z_OK; // Zlib return code
407 int len = buf_size;
408 int hi_ver, lo_ver, ret;
409 uint8_t *tmp;
411 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
412 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
413 return ret;
416 /* parse header */
417 c->flags = buf[0];
418 buf++; len--;
419 if (c->flags & ZMBV_KEYFRAME) {
420 hi_ver = buf[0];
421 lo_ver = buf[1];
422 c->comp = buf[2];
423 c->fmt = buf[3];
424 c->bw = buf[4];
425 c->bh = buf[5];
426 c->decode_intra = NULL;
427 c->decode_xor = NULL;
429 buf += 6;
430 len -= 6;
431 av_log(avctx, AV_LOG_DEBUG,
432 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
433 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
434 if (hi_ver != 0 || lo_ver != 1) {
435 av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
436 hi_ver, lo_ver);
437 return AVERROR_PATCHWELCOME;
439 if (c->bw == 0 || c->bh == 0) {
440 av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
441 c->bw, c->bh);
442 return AVERROR_PATCHWELCOME;
444 if (c->comp != 0 && c->comp != 1) {
445 av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
446 c->comp);
447 return AVERROR_PATCHWELCOME;
450 switch (c->fmt) {
451 case ZMBV_FMT_8BPP:
452 c->bpp = 8;
453 c->decode_intra = zmbv_decode_intra;
454 c->decode_xor = zmbv_decode_xor_8;
455 break;
456 case ZMBV_FMT_15BPP:
457 case ZMBV_FMT_16BPP:
458 c->bpp = 16;
459 c->decode_intra = zmbv_decode_intra;
460 c->decode_xor = zmbv_decode_xor_16;
461 break;
462 #ifdef ZMBV_ENABLE_24BPP
463 case ZMBV_FMT_24BPP:
464 c->bpp = 24;
465 c->decode_intra = zmbv_decode_intra;
466 c->decode_xor = zmbv_decode_xor_24;
467 break;
468 #endif //ZMBV_ENABLE_24BPP
469 case ZMBV_FMT_32BPP:
470 c->bpp = 32;
471 c->decode_intra = zmbv_decode_intra;
472 c->decode_xor = zmbv_decode_xor_32;
473 break;
474 default:
475 c->decode_intra = NULL;
476 c->decode_xor = NULL;
477 av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
478 c->fmt);
479 return AVERROR_PATCHWELCOME;
482 zret = inflateReset(&c->zstream);
483 if (zret != Z_OK) {
484 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
485 return AVERROR_UNKNOWN;
488 tmp = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
489 if (!tmp)
490 return AVERROR(ENOMEM);
491 c->cur = tmp;
492 tmp = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
493 if (!tmp)
494 return AVERROR(ENOMEM);
495 c->prev = tmp;
496 c->bx = (c->width + c->bw - 1) / c->bw;
497 c->by = (c->height + c->bh - 1) / c->bh;
500 if (c->decode_intra == NULL) {
501 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
502 return AVERROR_INVALIDDATA;
505 if (c->comp == 0) { //Uncompressed data
506 memcpy(c->decomp_buf, buf, len);
507 c->decomp_size = 1;
508 } else { // ZLIB-compressed data
509 c->zstream.total_in = c->zstream.total_out = 0;
510 c->zstream.next_in = buf;
511 c->zstream.avail_in = len;
512 c->zstream.next_out = c->decomp_buf;
513 c->zstream.avail_out = c->decomp_size;
514 zret = inflate(&c->zstream, Z_SYNC_FLUSH);
515 if (zret != Z_OK && zret != Z_STREAM_END) {
516 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
517 return AVERROR_INVALIDDATA;
519 c->decomp_len = c->zstream.total_out;
521 if (c->flags & ZMBV_KEYFRAME) {
522 frame->key_frame = 1;
523 frame->pict_type = AV_PICTURE_TYPE_I;
524 c->decode_intra(c);
525 } else {
526 frame->key_frame = 0;
527 frame->pict_type = AV_PICTURE_TYPE_P;
528 if (c->decomp_len)
529 c->decode_xor(c);
532 /* update frames */
534 uint8_t *out, *src;
535 int i, j;
537 out = frame->data[0];
538 src = c->cur;
539 switch (c->fmt) {
540 case ZMBV_FMT_8BPP:
541 for (j = 0; j < c->height; j++) {
542 for (i = 0; i < c->width; i++) {
543 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
544 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
545 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
546 src++;
548 out += frame->linesize[0];
550 break;
551 case ZMBV_FMT_15BPP:
552 for (j = 0; j < c->height; j++) {
553 for (i = 0; i < c->width; i++) {
554 uint16_t tmp = AV_RL16(src);
555 src += 2;
556 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
557 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
558 out[i * 3 + 2] = (tmp & 0x001F) << 3;
560 out += frame->linesize[0];
562 break;
563 case ZMBV_FMT_16BPP:
564 for (j = 0; j < c->height; j++) {
565 for (i = 0; i < c->width; i++) {
566 uint16_t tmp = AV_RL16(src);
567 src += 2;
568 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
569 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
570 out[i * 3 + 2] = (tmp & 0x001F) << 3;
572 out += frame->linesize[0];
574 break;
575 #ifdef ZMBV_ENABLE_24BPP
576 case ZMBV_FMT_24BPP:
577 for (j = 0; j < c->height; j++) {
578 memcpy(out, src, c->width * 3);
579 src += c->width * 3;
580 out += frame->linesize[0];
582 break;
583 #endif //ZMBV_ENABLE_24BPP
584 case ZMBV_FMT_32BPP:
585 for (j = 0; j < c->height; j++) {
586 for (i = 0; i < c->width; i++) {
587 uint32_t tmp = AV_RL32(src);
588 src += 4;
589 AV_WB24(out+(i*3), tmp);
591 out += frame->linesize[0];
593 break;
594 default:
595 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
597 FFSWAP(uint8_t *, c->cur, c->prev);
599 *got_frame = 1;
601 /* always report that the buffer was completely consumed */
602 return buf_size;
605 static av_cold int decode_init(AVCodecContext *avctx)
607 ZmbvContext * const c = avctx->priv_data;
608 int zret; // Zlib return code
610 c->avctx = avctx;
612 c->width = avctx->width;
613 c->height = avctx->height;
615 c->bpp = avctx->bits_per_coded_sample;
617 // Needed if zlib unused or init aborted before inflateInit
618 memset(&c->zstream, 0, sizeof(z_stream));
620 avctx->pix_fmt = AV_PIX_FMT_RGB24;
621 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
623 /* Allocate decompression buffer */
624 if (c->decomp_size) {
625 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
626 av_log(avctx, AV_LOG_ERROR,
627 "Can't allocate decompression buffer.\n");
628 return AVERROR(ENOMEM);
632 c->zstream.zalloc = Z_NULL;
633 c->zstream.zfree = Z_NULL;
634 c->zstream.opaque = Z_NULL;
635 zret = inflateInit(&c->zstream);
636 if (zret != Z_OK) {
637 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
638 return AVERROR_UNKNOWN;
641 return 0;
644 static av_cold int decode_end(AVCodecContext *avctx)
646 ZmbvContext * const c = avctx->priv_data;
648 av_freep(&c->decomp_buf);
650 inflateEnd(&c->zstream);
651 av_freep(&c->cur);
652 av_freep(&c->prev);
654 return 0;
657 AVCodec ff_zmbv_decoder = {
658 .name = "zmbv",
659 .type = AVMEDIA_TYPE_VIDEO,
660 .id = AV_CODEC_ID_ZMBV,
661 .priv_data_size = sizeof(ZmbvContext),
662 .init = decode_init,
663 .close = decode_end,
664 .decode = decode_frame,
665 .capabilities = CODEC_CAP_DR1,
666 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),