2 * Zip Motion Blocks Video (ZMBV) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
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 * Zip Motion Blocks Video decoder
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
37 #define ZMBV_KEYFRAME 1
38 #define ZMBV_DELTAPAL 2
55 typedef struct ZmbvContext {
56 AVCodecContext *avctx;
60 unsigned int decomp_size;
71 int (*decode_intra)(struct ZmbvContext *c);
72 int (*decode_xor)(struct ZmbvContext *c);
76 * Decode XOR'ed frame - 8bpp version
79 static int zmbv_decode_xor_8(ZmbvContext *c)
81 uint8_t *src = c->decomp_buf;
82 uint8_t *output, *prev;
85 int d, dx, dy, bw2, bh2;
93 if (c->flags & ZMBV_DELTAPAL) {
94 for (i = 0; i < 768; i++)
99 src += ((c->bx * c->by * 2 + 3) & ~3);
102 for (y = 0; y < c->height; y += c->bh) {
103 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
104 for (x = 0; x < c->width; x += c->bw) {
105 uint8_t *out, *tprev;
108 dx = mvec[block] >> 1;
109 dy = mvec[block + 1] >> 1;
112 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
114 /* copy block - motion vectors out of bounds are used to zero blocks */
116 tprev = prev + x + dx + dy * c->width;
119 for (j = 0; j < bh2; j++) {
120 if (my + j < 0 || my + j >= c->height) {
123 for (i = 0; i < bw2; i++) {
124 if (mx + i < 0 || mx + i >= c->width)
134 if (d) { /* apply XOR'ed difference */
136 for (j = 0; j < bh2; j++) {
137 for (i = 0; i < bw2; i++)
143 output += c->width * c->bh;
144 prev += c->width * c->bh;
146 if (src - c->decomp_buf != c->decomp_len)
147 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
148 src-c->decomp_buf, c->decomp_len);
153 * Decode XOR'ed frame - 15bpp and 16bpp version
156 static int zmbv_decode_xor_16(ZmbvContext *c)
158 uint8_t *src = c->decomp_buf;
159 uint16_t *output, *prev;
162 int d, dx, dy, bw2, bh2;
167 output = (uint16_t*)c->cur;
168 prev = (uint16_t*)c->prev;
171 src += ((c->bx * c->by * 2 + 3) & ~3);
174 for (y = 0; y < c->height; y += c->bh) {
175 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
176 for (x = 0; x < c->width; x += c->bw) {
177 uint16_t *out, *tprev;
180 dx = mvec[block] >> 1;
181 dy = mvec[block + 1] >> 1;
184 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
186 /* copy block - motion vectors out of bounds are used to zero blocks */
188 tprev = prev + x + dx + dy * c->width;
191 for (j = 0; j < bh2; j++) {
192 if (my + j < 0 || my + j >= c->height) {
193 memset(out, 0, bw2 * 2);
195 for (i = 0; i < bw2; i++) {
196 if (mx + i < 0 || mx + i >= c->width)
206 if (d) { /* apply XOR'ed difference */
208 for (j = 0; j < bh2; j++){
209 for (i = 0; i < bw2; i++) {
210 out[i] ^= *((uint16_t*)src);
217 output += c->width * c->bh;
218 prev += c->width * c->bh;
220 if (src - c->decomp_buf != c->decomp_len)
221 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
222 src-c->decomp_buf, c->decomp_len);
226 #ifdef ZMBV_ENABLE_24BPP
228 * Decode XOR'ed frame - 24bpp version
231 static int zmbv_decode_xor_24(ZmbvContext *c)
233 uint8_t *src = c->decomp_buf;
234 uint8_t *output, *prev;
237 int d, dx, dy, bw2, bh2;
246 stride = c->width * 3;
248 src += ((c->bx * c->by * 2 + 3) & ~3);
251 for (y = 0; y < c->height; y += c->bh) {
252 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
253 for (x = 0; x < c->width; x += c->bw) {
254 uint8_t *out, *tprev;
257 dx = mvec[block] >> 1;
258 dy = mvec[block + 1] >> 1;
261 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
263 /* copy block - motion vectors out of bounds are used to zero blocks */
264 out = output + x * 3;
265 tprev = prev + (x + dx) * 3 + dy * stride;
268 for (j = 0; j < bh2; j++) {
269 if (my + j < 0 || my + j >= c->height) {
270 memset(out, 0, bw2 * 3);
272 for (i = 0; i < bw2; i++){
273 if (mx + i < 0 || mx + i >= c->width) {
278 out[i * 3 + 0] = tprev[i * 3 + 0];
279 out[i * 3 + 1] = tprev[i * 3 + 1];
280 out[i * 3 + 2] = tprev[i * 3 + 2];
288 if (d) { /* apply XOR'ed difference */
289 out = output + x * 3;
290 for (j = 0; j < bh2; j++) {
291 for (i = 0; i < bw2; i++) {
292 out[i * 3 + 0] ^= *src++;
293 out[i * 3 + 1] ^= *src++;
294 out[i * 3 + 2] ^= *src++;
300 output += stride * c->bh;
301 prev += stride * c->bh;
303 if (src - c->decomp_buf != c->decomp_len)
304 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
305 src-c->decomp_buf, c->decomp_len);
308 #endif //ZMBV_ENABLE_24BPP
311 * Decode XOR'ed frame - 32bpp version
314 static int zmbv_decode_xor_32(ZmbvContext *c)
316 uint8_t *src = c->decomp_buf;
317 uint32_t *output, *prev;
320 int d, dx, dy, bw2, bh2;
325 output = (uint32_t*)c->cur;
326 prev = (uint32_t*)c->prev;
329 src += ((c->bx * c->by * 2 + 3) & ~3);
332 for (y = 0; y < c->height; y += c->bh) {
333 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
334 for (x = 0; x < c->width; x += c->bw) {
335 uint32_t *out, *tprev;
338 dx = mvec[block] >> 1;
339 dy = mvec[block + 1] >> 1;
342 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
344 /* copy block - motion vectors out of bounds are used to zero blocks */
346 tprev = prev + x + dx + dy * c->width;
349 for (j = 0; j < bh2; j++) {
350 if (my + j < 0 || my + j >= c->height) {
351 memset(out, 0, bw2 * 4);
353 for (i = 0; i < bw2; i++){
354 if (mx + i < 0 || mx + i >= c->width)
364 if (d) { /* apply XOR'ed difference */
366 for (j = 0; j < bh2; j++){
367 for (i = 0; i < bw2; i++) {
368 out[i] ^= *((uint32_t *) src);
375 output += c->width * c->bh;
376 prev += c->width * c->bh;
378 if (src - c->decomp_buf != c->decomp_len)
379 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
380 src-c->decomp_buf, c->decomp_len);
387 static int zmbv_decode_intra(ZmbvContext *c)
389 uint8_t *src = c->decomp_buf;
391 /* make the palette available on the way out */
392 if (c->fmt == ZMBV_FMT_8BPP) {
393 memcpy(c->pal, src, 768);
397 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
401 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
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
408 int hi_ver, lo_ver, ret;
411 avctx->release_buffer(avctx, &c->pic);
413 c->pic.reference = 3;
414 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
415 if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
416 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
423 if (c->flags & ZMBV_KEYFRAME) {
424 void *decode_intra = NULL;
425 c->decode_intra= NULL;
432 c->decode_intra = NULL;
433 c->decode_xor = NULL;
437 av_log(avctx, AV_LOG_DEBUG,
438 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
439 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
440 if (hi_ver != 0 || lo_ver != 1) {
441 av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
443 return AVERROR_PATCHWELCOME;
445 if (c->bw == 0 || c->bh == 0) {
446 av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
448 return AVERROR_PATCHWELCOME;
450 if (c->comp != 0 && c->comp != 1) {
451 av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
453 return AVERROR_PATCHWELCOME;
459 decode_intra = zmbv_decode_intra;
460 c->decode_xor = zmbv_decode_xor_8;
465 decode_intra = zmbv_decode_intra;
466 c->decode_xor = zmbv_decode_xor_16;
468 #ifdef ZMBV_ENABLE_24BPP
471 decode_intra = zmbv_decode_intra;
472 c->decode_xor = zmbv_decode_xor_24;
474 #endif //ZMBV_ENABLE_24BPP
477 decode_intra = zmbv_decode_intra;
478 c->decode_xor = zmbv_decode_xor_32;
481 c->decode_xor = NULL;
482 av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
484 return AVERROR_PATCHWELCOME;
487 zret = inflateReset(&c->zstream);
489 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
490 return AVERROR_UNKNOWN;
493 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
494 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
495 c->bx = (c->width + c->bw - 1) / c->bw;
496 c->by = (c->height+ c->bh - 1) / c->bh;
497 if (!c->cur || !c->prev)
499 memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
500 memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
501 c->decode_intra= decode_intra;
504 if (c->decode_intra == NULL) {
505 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
506 return AVERROR_INVALIDDATA;
509 if (c->comp == 0) { //Uncompressed data
510 if (c->decomp_size < len) {
511 av_log(avctx, AV_LOG_ERROR, "decomp buffer too small\n");
512 return AVERROR_INVALIDDATA;
514 memcpy(c->decomp_buf, buf, len);
515 } else { // ZLIB-compressed data
516 c->zstream.total_in = c->zstream.total_out = 0;
517 c->zstream.next_in = (uint8_t*)buf;
518 c->zstream.avail_in = len;
519 c->zstream.next_out = c->decomp_buf;
520 c->zstream.avail_out = c->decomp_size;
521 zret = inflate(&c->zstream, Z_SYNC_FLUSH);
522 if (zret != Z_OK && zret != Z_STREAM_END) {
523 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
524 return AVERROR_INVALIDDATA;
526 c->decomp_len = c->zstream.total_out;
528 if (c->flags & ZMBV_KEYFRAME) {
529 c->pic.key_frame = 1;
530 c->pic.pict_type = AV_PICTURE_TYPE_I;
533 c->pic.key_frame = 0;
534 c->pic.pict_type = AV_PICTURE_TYPE_P;
544 out = c->pic.data[0];
548 for (j = 0; j < c->height; j++) {
549 for (i = 0; i < c->width; i++) {
550 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
551 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
552 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
555 out += c->pic.linesize[0];
559 for (j = 0; j < c->height; j++) {
560 for (i = 0; i < c->width; i++) {
561 uint16_t tmp = AV_RL16(src);
563 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
564 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
565 out[i * 3 + 2] = (tmp & 0x001F) << 3;
567 out += c->pic.linesize[0];
571 for (j = 0; j < c->height; j++) {
572 for (i = 0; i < c->width; i++) {
573 uint16_t tmp = AV_RL16(src);
575 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
576 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
577 out[i * 3 + 2] = (tmp & 0x001F) << 3;
579 out += c->pic.linesize[0];
582 #ifdef ZMBV_ENABLE_24BPP
584 for (j = 0; j < c->height; j++) {
585 memcpy(out, src, c->width * 3);
587 out += c->pic.linesize[0];
590 #endif //ZMBV_ENABLE_24BPP
592 for (j = 0; j < c->height; j++) {
593 for (i = 0; i < c->width; i++) {
594 uint32_t tmp = AV_RL32(src);
596 AV_WB24(out+(i*3), tmp);
598 out += c->pic.linesize[0];
602 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
604 FFSWAP(uint8_t *, c->cur, c->prev);
607 *(AVFrame*)data = c->pic;
609 /* always report that the buffer was completely consumed */
613 static av_cold int decode_init(AVCodecContext *avctx)
615 ZmbvContext * const c = avctx->priv_data;
616 int zret; // Zlib return code
620 c->width = avctx->width;
621 c->height = avctx->height;
622 avcodec_get_frame_defaults(&c->pic);
624 c->bpp = avctx->bits_per_coded_sample;
626 // Needed if zlib unused or init aborted before inflateInit
627 memset(&c->zstream, 0, sizeof(z_stream));
629 avctx->pix_fmt = AV_PIX_FMT_RGB24;
630 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
632 /* Allocate decompression buffer */
633 if (c->decomp_size) {
634 if ((c->decomp_buf = av_mallocz(c->decomp_size)) == NULL) {
635 av_log(avctx, AV_LOG_ERROR,
636 "Can't allocate decompression buffer.\n");
637 return AVERROR(ENOMEM);
641 c->zstream.zalloc = Z_NULL;
642 c->zstream.zfree = Z_NULL;
643 c->zstream.opaque = Z_NULL;
644 zret = inflateInit(&c->zstream);
646 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
647 return AVERROR_UNKNOWN;
653 static av_cold int decode_end(AVCodecContext *avctx)
655 ZmbvContext * const c = avctx->priv_data;
657 av_freep(&c->decomp_buf);
660 avctx->release_buffer(avctx, &c->pic);
661 inflateEnd(&c->zstream);
668 AVCodec ff_zmbv_decoder = {
670 .type = AVMEDIA_TYPE_VIDEO,
671 .id = AV_CODEC_ID_ZMBV,
672 .priv_data_size = sizeof(ZmbvContext),
675 .decode = decode_frame,
676 .capabilities = CODEC_CAP_DR1,
677 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),