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/imgutils.h"
32 #include "libavutil/intreadwrite.h"
38 #define ZMBV_KEYFRAME 1
39 #define ZMBV_DELTAPAL 2
56 typedef struct ZmbvContext {
57 AVCodecContext *avctx;
61 unsigned int decomp_size;
73 int (*decode_intra)(struct ZmbvContext *c);
74 int (*decode_xor)(struct ZmbvContext *c);
78 * Decode XOR'ed frame - 8bpp version
81 static int zmbv_decode_xor_8(ZmbvContext *c)
83 uint8_t *src = c->decomp_buf;
84 uint8_t *output, *prev;
87 int d, dx, dy, bw2, bh2;
95 if (c->flags & ZMBV_DELTAPAL) {
96 for (i = 0; i < 768; i++)
101 src += ((c->bx * c->by * 2 + 3) & ~3);
104 for (y = 0; y < c->height; y += c->bh) {
105 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
106 for (x = 0; x < c->width; x += c->bw) {
107 uint8_t *out, *tprev;
110 dx = mvec[block] >> 1;
111 dy = mvec[block + 1] >> 1;
114 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
116 /* copy block - motion vectors out of bounds are used to zero blocks */
118 tprev = prev + x + dx + dy * c->width;
121 for (j = 0; j < bh2; j++) {
122 if (my + j < 0 || my + j >= c->height) {
125 for (i = 0; i < bw2; i++) {
126 if (mx + i < 0 || mx + i >= c->width)
136 if (d) { /* apply XOR'ed difference */
138 for (j = 0; j < bh2; j++) {
139 for (i = 0; i < bw2; i++)
145 output += c->width * c->bh;
146 prev += c->width * c->bh;
148 if (src - c->decomp_buf != c->decomp_len)
149 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
150 src-c->decomp_buf, c->decomp_len);
155 * Decode XOR'ed frame - 15bpp and 16bpp version
158 static int zmbv_decode_xor_16(ZmbvContext *c)
160 uint8_t *src = c->decomp_buf;
161 uint16_t *output, *prev;
164 int d, dx, dy, bw2, bh2;
169 output = (uint16_t*)c->cur;
170 prev = (uint16_t*)c->prev;
173 src += ((c->bx * c->by * 2 + 3) & ~3);
176 for (y = 0; y < c->height; y += c->bh) {
177 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
178 for (x = 0; x < c->width; x += c->bw) {
179 uint16_t *out, *tprev;
182 dx = mvec[block] >> 1;
183 dy = mvec[block + 1] >> 1;
186 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
188 /* copy block - motion vectors out of bounds are used to zero blocks */
190 tprev = prev + x + dx + dy * c->width;
193 for (j = 0; j < bh2; j++) {
194 if (my + j < 0 || my + j >= c->height) {
195 memset(out, 0, bw2 * 2);
197 for (i = 0; i < bw2; i++) {
198 if (mx + i < 0 || mx + i >= c->width)
208 if (d) { /* apply XOR'ed difference */
210 for (j = 0; j < bh2; j++){
211 for (i = 0; i < bw2; i++) {
212 out[i] ^= *((uint16_t*)src);
219 output += c->width * c->bh;
220 prev += c->width * c->bh;
222 if (src - c->decomp_buf != c->decomp_len)
223 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
224 src-c->decomp_buf, c->decomp_len);
228 #ifdef ZMBV_ENABLE_24BPP
230 * Decode XOR'ed frame - 24bpp version
233 static int zmbv_decode_xor_24(ZmbvContext *c)
235 uint8_t *src = c->decomp_buf;
236 uint8_t *output, *prev;
239 int d, dx, dy, bw2, bh2;
248 stride = c->width * 3;
250 src += ((c->bx * c->by * 2 + 3) & ~3);
253 for (y = 0; y < c->height; y += c->bh) {
254 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
255 for (x = 0; x < c->width; x += c->bw) {
256 uint8_t *out, *tprev;
259 dx = mvec[block] >> 1;
260 dy = mvec[block + 1] >> 1;
263 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
265 /* copy block - motion vectors out of bounds are used to zero blocks */
266 out = output + x * 3;
267 tprev = prev + (x + dx) * 3 + dy * stride;
270 for (j = 0; j < bh2; j++) {
271 if (my + j < 0 || my + j >= c->height) {
272 memset(out, 0, bw2 * 3);
274 for (i = 0; i < bw2; i++){
275 if (mx + i < 0 || mx + i >= c->width) {
280 out[i * 3 + 0] = tprev[i * 3 + 0];
281 out[i * 3 + 1] = tprev[i * 3 + 1];
282 out[i * 3 + 2] = tprev[i * 3 + 2];
290 if (d) { /* apply XOR'ed difference */
291 out = output + x * 3;
292 for (j = 0; j < bh2; j++) {
293 for (i = 0; i < bw2; i++) {
294 out[i * 3 + 0] ^= *src++;
295 out[i * 3 + 1] ^= *src++;
296 out[i * 3 + 2] ^= *src++;
302 output += stride * c->bh;
303 prev += stride * c->bh;
305 if (src - c->decomp_buf != c->decomp_len)
306 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
307 src-c->decomp_buf, c->decomp_len);
310 #endif //ZMBV_ENABLE_24BPP
313 * Decode XOR'ed frame - 32bpp version
316 static int zmbv_decode_xor_32(ZmbvContext *c)
318 uint8_t *src = c->decomp_buf;
319 uint32_t *output, *prev;
322 int d, dx, dy, bw2, bh2;
327 output = (uint32_t*)c->cur;
328 prev = (uint32_t*)c->prev;
331 src += ((c->bx * c->by * 2 + 3) & ~3);
334 for (y = 0; y < c->height; y += c->bh) {
335 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
336 for (x = 0; x < c->width; x += c->bw) {
337 uint32_t *out, *tprev;
340 dx = mvec[block] >> 1;
341 dy = mvec[block + 1] >> 1;
344 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
346 /* copy block - motion vectors out of bounds are used to zero blocks */
348 tprev = prev + x + dx + dy * c->width;
351 for (j = 0; j < bh2; j++) {
352 if (my + j < 0 || my + j >= c->height) {
353 memset(out, 0, bw2 * 4);
355 for (i = 0; i < bw2; i++){
356 if (mx + i < 0 || mx + i >= c->width)
366 if (d) { /* apply XOR'ed difference */
368 for (j = 0; j < bh2; j++){
369 for (i = 0; i < bw2; i++) {
370 out[i] ^= *((uint32_t *) src);
377 output += c->width * c->bh;
378 prev += c->width * c->bh;
380 if (src - c->decomp_buf != c->decomp_len)
381 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
382 src-c->decomp_buf, c->decomp_len);
389 static int zmbv_decode_intra(ZmbvContext *c)
391 uint8_t *src = c->decomp_buf;
393 /* make the palette available on the way out */
394 if (c->fmt == ZMBV_FMT_8BPP) {
395 memcpy(c->pal, src, 768);
399 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
403 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
405 AVFrame *frame = data;
406 const uint8_t *buf = avpkt->data;
407 int buf_size = avpkt->size;
408 ZmbvContext * const c = avctx->priv_data;
409 int zret = Z_OK; // Zlib return code
411 int hi_ver, lo_ver, ret;
416 return AVERROR_INVALIDDATA;
419 if (c->flags & ZMBV_KEYFRAME) {
420 void *decode_intra = NULL;
421 c->decode_intra= NULL;
424 return AVERROR_INVALIDDATA;
431 c->decode_intra = NULL;
432 c->decode_xor = NULL;
436 av_log(avctx, AV_LOG_DEBUG,
437 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
438 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
439 if (hi_ver != 0 || lo_ver != 1) {
440 avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
441 return AVERROR_PATCHWELCOME;
443 if (c->bw == 0 || c->bh == 0) {
444 avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
445 return AVERROR_PATCHWELCOME;
447 if (c->comp != 0 && c->comp != 1) {
448 avpriv_request_sample(avctx, "Compression type %i", c->comp);
449 return AVERROR_PATCHWELCOME;
455 decode_intra = zmbv_decode_intra;
456 c->decode_xor = zmbv_decode_xor_8;
457 avctx->pix_fmt = AV_PIX_FMT_PAL8;
458 c->stride = c->width;
463 decode_intra = zmbv_decode_intra;
464 c->decode_xor = zmbv_decode_xor_16;
465 if (c->fmt == ZMBV_FMT_15BPP)
466 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
468 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
469 c->stride = c->width * 2;
471 #ifdef ZMBV_ENABLE_24BPP
474 decode_intra = zmbv_decode_intra;
475 c->decode_xor = zmbv_decode_xor_24;
476 avctx->pix_fmt = AV_PIX_FMT_BGR24;
477 c->stride = c->width * 3;
479 #endif //ZMBV_ENABLE_24BPP
482 decode_intra = zmbv_decode_intra;
483 c->decode_xor = zmbv_decode_xor_32;
484 avctx->pix_fmt = AV_PIX_FMT_BGR0;
485 c->stride = c->width * 4;
488 c->decode_xor = NULL;
489 avpriv_request_sample(avctx, "Format %i", c->fmt);
490 return AVERROR_PATCHWELCOME;
493 zret = inflateReset(&c->zstream);
495 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
496 return AVERROR_UNKNOWN;
499 if (c->alloc_bpp < c->bpp) {
500 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
501 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
502 c->alloc_bpp = c->bpp;
504 c->bx = (c->width + c->bw - 1) / c->bw;
505 c->by = (c->height+ c->bh - 1) / c->bh;
506 if (!c->cur || !c->prev) {
508 return AVERROR(ENOMEM);
510 memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
511 memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
512 c->decode_intra= decode_intra;
514 if (c->flags & ZMBV_KEYFRAME) {
515 expected_size = avctx->width * avctx->height * (c->bpp / 8);
517 expected_size = (c->bx * c->by * 2 + 3) & ~3;
519 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
520 (c->flags & (ZMBV_DELTAPAL | ZMBV_KEYFRAME)))
521 expected_size += 768;
523 if (!c->decode_intra) {
524 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
525 return AVERROR_INVALIDDATA;
528 if (c->comp == 0) { // uncompressed data
529 if (c->decomp_size < len) {
530 av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
531 return AVERROR_INVALIDDATA;
533 memcpy(c->decomp_buf, buf, len);
535 } else { // ZLIB-compressed data
536 c->zstream.total_in = c->zstream.total_out = 0;
537 c->zstream.next_in = (uint8_t*)buf;
538 c->zstream.avail_in = len;
539 c->zstream.next_out = c->decomp_buf;
540 c->zstream.avail_out = c->decomp_size;
541 zret = inflate(&c->zstream, Z_SYNC_FLUSH);
542 if (zret != Z_OK && zret != Z_STREAM_END) {
543 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
544 return AVERROR_INVALIDDATA;
546 c->decomp_len = c->zstream.total_out;
548 if (expected_size > c->decomp_len ||
549 (c->flags & ZMBV_KEYFRAME) && expected_size < c->decomp_len) {
550 av_log(avctx, AV_LOG_ERROR, "decompressed size %d is incorrect, expected %d\n", c->decomp_len, expected_size);
551 return AVERROR_INVALIDDATA;
553 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
556 if (c->flags & ZMBV_KEYFRAME) {
557 frame->key_frame = 1;
558 frame->pict_type = AV_PICTURE_TYPE_I;
561 frame->key_frame = 0;
562 frame->pict_type = AV_PICTURE_TYPE_P;
563 if (c->decomp_len < 2LL * ((c->width + c->bw - 1) / c->bw) * ((c->height + c->bh - 1) / c->bh))
564 return AVERROR_INVALIDDATA;
574 out = frame->data[0];
578 for (j = 0; j < 256; j++)
579 AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
582 #ifdef ZMBV_ENABLE_24BPP
586 av_image_copy_plane(out, frame->linesize[0], src, c->stride,
587 c->stride, c->height);
590 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
592 FFSWAP(uint8_t *, c->cur, c->prev);
596 /* always report that the buffer was completely consumed */
600 static av_cold int decode_init(AVCodecContext *avctx)
602 ZmbvContext * const c = avctx->priv_data;
603 int zret; // Zlib return code
607 c->width = avctx->width;
608 c->height = avctx->height;
610 c->bpp = avctx->bits_per_coded_sample;
612 // Needed if zlib unused or init aborted before inflateInit
613 memset(&c->zstream, 0, sizeof(z_stream));
615 if ((avctx->width + 255ULL) * (avctx->height + 64ULL) > FFMIN(avctx->max_pixels, INT_MAX / 4) ) {
616 av_log(avctx, AV_LOG_ERROR, "Internal buffer (decomp_size) larger than max_pixels or too large\n");
617 return AVERROR_INVALIDDATA;
620 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
622 /* Allocate decompression buffer */
623 c->decomp_buf = av_mallocz(c->decomp_size);
624 if (!c->decomp_buf) {
625 av_log(avctx, AV_LOG_ERROR,
626 "Can't allocate decompression buffer.\n");
627 return AVERROR(ENOMEM);
630 c->zstream.zalloc = Z_NULL;
631 c->zstream.zfree = Z_NULL;
632 c->zstream.opaque = Z_NULL;
633 zret = inflateInit(&c->zstream);
635 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
636 return AVERROR_UNKNOWN;
642 static av_cold int decode_end(AVCodecContext *avctx)
644 ZmbvContext * const c = avctx->priv_data;
646 av_freep(&c->decomp_buf);
648 inflateEnd(&c->zstream);
655 AVCodec ff_zmbv_decoder = {
657 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
658 .type = AVMEDIA_TYPE_VIDEO,
659 .id = AV_CODEC_ID_ZMBV,
660 .priv_data_size = sizeof(ZmbvContext),
663 .decode = decode_frame,
664 .capabilities = AV_CODEC_CAP_DR1,
665 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,