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) {
124 } else if (mx >= 0 && mx + bw2 <= c->width){
125 memcpy(out, tprev, sizeof(*out) * bw2);
127 for (i = 0; i < bw2; i++) {
128 if (mx + i < 0 || mx + i >= c->width)
138 if (d) { /* apply XOR'ed difference */
140 for (j = 0; j < bh2; j++) {
141 for (i = 0; i < bw2; i++)
147 output += c->width * c->bh;
148 prev += c->width * c->bh;
150 if (src - c->decomp_buf != c->decomp_len)
151 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
152 src-c->decomp_buf, c->decomp_len);
157 * Decode XOR'ed frame - 15bpp and 16bpp version
160 static int zmbv_decode_xor_16(ZmbvContext *c)
162 uint8_t *src = c->decomp_buf;
163 uint16_t *output, *prev;
166 int d, dx, dy, bw2, bh2;
171 output = (uint16_t*)c->cur;
172 prev = (uint16_t*)c->prev;
175 src += ((c->bx * c->by * 2 + 3) & ~3);
178 for (y = 0; y < c->height; y += c->bh) {
179 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
180 for (x = 0; x < c->width; x += c->bw) {
181 uint16_t *out, *tprev;
184 dx = mvec[block] >> 1;
185 dy = mvec[block + 1] >> 1;
188 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
190 /* copy block - motion vectors out of bounds are used to zero blocks */
192 tprev = prev + x + dx + dy * c->width;
195 for (j = 0; j < bh2; j++) {
196 if (my + j < 0 || my + j >= c->height) {
197 memset(out, 0, bw2 * 2);
198 } else if (mx >= 0 && mx + bw2 <= c->width){
199 memcpy(out, tprev, sizeof(*out) * bw2);
201 for (i = 0; i < bw2; i++) {
202 if (mx + i < 0 || mx + i >= c->width)
212 if (d) { /* apply XOR'ed difference */
214 for (j = 0; j < bh2; j++){
215 for (i = 0; i < bw2; i++) {
216 out[i] ^= *((uint16_t*)src);
223 output += c->width * c->bh;
224 prev += c->width * c->bh;
226 if (src - c->decomp_buf != c->decomp_len)
227 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
228 src-c->decomp_buf, c->decomp_len);
232 #ifdef ZMBV_ENABLE_24BPP
234 * Decode XOR'ed frame - 24bpp version
237 static int zmbv_decode_xor_24(ZmbvContext *c)
239 uint8_t *src = c->decomp_buf;
240 uint8_t *output, *prev;
243 int d, dx, dy, bw2, bh2;
252 stride = c->width * 3;
254 src += ((c->bx * c->by * 2 + 3) & ~3);
257 for (y = 0; y < c->height; y += c->bh) {
258 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
259 for (x = 0; x < c->width; x += c->bw) {
260 uint8_t *out, *tprev;
263 dx = mvec[block] >> 1;
264 dy = mvec[block + 1] >> 1;
267 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
269 /* copy block - motion vectors out of bounds are used to zero blocks */
270 out = output + x * 3;
271 tprev = prev + (x + dx) * 3 + dy * stride;
274 for (j = 0; j < bh2; j++) {
275 if (my + j < 0 || my + j >= c->height) {
276 memset(out, 0, bw2 * 3);
277 } else if (mx >= 0 && mx + bw2 <= c->width){
278 memcpy(out, tprev, 3 * bw2);
280 for (i = 0; i < bw2; i++){
281 if (mx + i < 0 || mx + i >= c->width) {
286 out[i * 3 + 0] = tprev[i * 3 + 0];
287 out[i * 3 + 1] = tprev[i * 3 + 1];
288 out[i * 3 + 2] = tprev[i * 3 + 2];
296 if (d) { /* apply XOR'ed difference */
297 out = output + x * 3;
298 for (j = 0; j < bh2; j++) {
299 for (i = 0; i < bw2; i++) {
300 out[i * 3 + 0] ^= *src++;
301 out[i * 3 + 1] ^= *src++;
302 out[i * 3 + 2] ^= *src++;
308 output += stride * c->bh;
309 prev += stride * c->bh;
311 if (src - c->decomp_buf != c->decomp_len)
312 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
313 src-c->decomp_buf, c->decomp_len);
316 #endif //ZMBV_ENABLE_24BPP
319 * Decode XOR'ed frame - 32bpp version
322 static int zmbv_decode_xor_32(ZmbvContext *c)
324 uint8_t *src = c->decomp_buf;
325 uint32_t *output, *prev;
328 int d, dx, dy, bw2, bh2;
333 output = (uint32_t*)c->cur;
334 prev = (uint32_t*)c->prev;
337 src += ((c->bx * c->by * 2 + 3) & ~3);
340 for (y = 0; y < c->height; y += c->bh) {
341 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
342 for (x = 0; x < c->width; x += c->bw) {
343 uint32_t *out, *tprev;
346 dx = mvec[block] >> 1;
347 dy = mvec[block + 1] >> 1;
350 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
352 /* copy block - motion vectors out of bounds are used to zero blocks */
354 tprev = prev + x + dx + dy * c->width;
357 for (j = 0; j < bh2; j++) {
358 if (my + j < 0 || my + j >= c->height) {
359 memset(out, 0, bw2 * 4);
360 } else if (mx >= 0 && mx + bw2 <= c->width){
361 memcpy(out, tprev, sizeof(*out) * bw2);
363 for (i = 0; i < bw2; i++){
364 if (mx + i < 0 || mx + i >= c->width)
374 if (d) { /* apply XOR'ed difference */
376 for (j = 0; j < bh2; j++){
377 for (i = 0; i < bw2; i++) {
378 out[i] ^= *((uint32_t *) src);
385 output += c->width * c->bh;
386 prev += c->width * c->bh;
388 if (src - c->decomp_buf != c->decomp_len)
389 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
390 src-c->decomp_buf, c->decomp_len);
397 static int zmbv_decode_intra(ZmbvContext *c)
399 uint8_t *src = c->decomp_buf;
401 /* make the palette available on the way out */
402 if (c->fmt == ZMBV_FMT_8BPP) {
403 memcpy(c->pal, src, 768);
407 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
411 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
413 AVFrame *frame = data;
414 const uint8_t *buf = avpkt->data;
415 int buf_size = avpkt->size;
416 ZmbvContext * const c = avctx->priv_data;
417 int zret = Z_OK; // Zlib return code
419 int hi_ver, lo_ver, ret;
424 return AVERROR_INVALIDDATA;
427 if (c->flags & ZMBV_KEYFRAME) {
428 void *decode_intra = NULL;
429 c->decode_intra= NULL;
432 return AVERROR_INVALIDDATA;
439 c->decode_intra = NULL;
440 c->decode_xor = NULL;
444 av_log(avctx, AV_LOG_DEBUG,
445 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
446 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
447 if (hi_ver != 0 || lo_ver != 1) {
448 avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
449 return AVERROR_PATCHWELCOME;
451 if (c->bw == 0 || c->bh == 0) {
452 avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
453 return AVERROR_PATCHWELCOME;
455 if (c->comp != 0 && c->comp != 1) {
456 avpriv_request_sample(avctx, "Compression type %i", c->comp);
457 return AVERROR_PATCHWELCOME;
463 decode_intra = zmbv_decode_intra;
464 c->decode_xor = zmbv_decode_xor_8;
465 avctx->pix_fmt = AV_PIX_FMT_PAL8;
466 c->stride = c->width;
471 decode_intra = zmbv_decode_intra;
472 c->decode_xor = zmbv_decode_xor_16;
473 if (c->fmt == ZMBV_FMT_15BPP)
474 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
476 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
477 c->stride = c->width * 2;
479 #ifdef ZMBV_ENABLE_24BPP
482 decode_intra = zmbv_decode_intra;
483 c->decode_xor = zmbv_decode_xor_24;
484 avctx->pix_fmt = AV_PIX_FMT_BGR24;
485 c->stride = c->width * 3;
487 #endif //ZMBV_ENABLE_24BPP
490 decode_intra = zmbv_decode_intra;
491 c->decode_xor = zmbv_decode_xor_32;
492 avctx->pix_fmt = AV_PIX_FMT_BGR0;
493 c->stride = c->width * 4;
496 c->decode_xor = NULL;
497 avpriv_request_sample(avctx, "Format %i", c->fmt);
498 return AVERROR_PATCHWELCOME;
501 zret = inflateReset(&c->zstream);
503 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
504 return AVERROR_UNKNOWN;
507 if (c->alloc_bpp < c->bpp) {
508 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
509 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
510 c->alloc_bpp = c->bpp;
512 c->bx = (c->width + c->bw - 1) / c->bw;
513 c->by = (c->height+ c->bh - 1) / c->bh;
514 if (!c->cur || !c->prev) {
516 return AVERROR(ENOMEM);
518 memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
519 memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
520 c->decode_intra= decode_intra;
522 if (c->flags & ZMBV_KEYFRAME) {
523 expected_size = avctx->width * avctx->height * (c->bpp / 8);
525 expected_size = (c->bx * c->by * 2 + 3) & ~3;
527 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
528 (c->flags & (ZMBV_DELTAPAL | ZMBV_KEYFRAME)))
529 expected_size += 768;
531 if (!c->decode_intra) {
532 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
533 return AVERROR_INVALIDDATA;
536 if (c->comp == 0) { // uncompressed data
537 if (c->decomp_size < len) {
538 av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
539 return AVERROR_INVALIDDATA;
541 memcpy(c->decomp_buf, buf, len);
543 } else { // ZLIB-compressed data
544 c->zstream.total_in = c->zstream.total_out = 0;
545 c->zstream.next_in = (uint8_t*)buf;
546 c->zstream.avail_in = len;
547 c->zstream.next_out = c->decomp_buf;
548 c->zstream.avail_out = c->decomp_size;
549 zret = inflate(&c->zstream, Z_SYNC_FLUSH);
550 if (zret != Z_OK && zret != Z_STREAM_END) {
551 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
552 return AVERROR_INVALIDDATA;
554 c->decomp_len = c->zstream.total_out;
556 if (expected_size > c->decomp_len ||
557 (c->flags & ZMBV_KEYFRAME) && expected_size < c->decomp_len) {
558 av_log(avctx, AV_LOG_ERROR, "decompressed size %d is incorrect, expected %d\n", c->decomp_len, expected_size);
559 return AVERROR_INVALIDDATA;
561 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
564 if (c->flags & ZMBV_KEYFRAME) {
565 frame->key_frame = 1;
566 frame->pict_type = AV_PICTURE_TYPE_I;
569 frame->key_frame = 0;
570 frame->pict_type = AV_PICTURE_TYPE_P;
571 if (c->decomp_len < 2LL * ((c->width + c->bw - 1) / c->bw) * ((c->height + c->bh - 1) / c->bh))
572 return AVERROR_INVALIDDATA;
582 out = frame->data[0];
586 for (j = 0; j < 256; j++)
587 AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
590 #ifdef ZMBV_ENABLE_24BPP
594 av_image_copy_plane(out, frame->linesize[0], src, c->stride,
595 c->stride, c->height);
598 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
600 FFSWAP(uint8_t *, c->cur, c->prev);
604 /* always report that the buffer was completely consumed */
608 static av_cold int decode_init(AVCodecContext *avctx)
610 ZmbvContext * const c = avctx->priv_data;
611 int zret; // Zlib return code
615 c->width = avctx->width;
616 c->height = avctx->height;
618 c->bpp = avctx->bits_per_coded_sample;
620 // Needed if zlib unused or init aborted before inflateInit
621 memset(&c->zstream, 0, sizeof(z_stream));
623 if ((avctx->width + 255ULL) * (avctx->height + 64ULL) > FFMIN(avctx->max_pixels, INT_MAX / 4) ) {
624 av_log(avctx, AV_LOG_ERROR, "Internal buffer (decomp_size) larger than max_pixels or too large\n");
625 return AVERROR_INVALIDDATA;
628 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
630 /* Allocate decompression buffer */
631 c->decomp_buf = av_mallocz(c->decomp_size);
632 if (!c->decomp_buf) {
633 av_log(avctx, AV_LOG_ERROR,
634 "Can't allocate decompression buffer.\n");
635 return AVERROR(ENOMEM);
638 c->zstream.zalloc = Z_NULL;
639 c->zstream.zfree = Z_NULL;
640 c->zstream.opaque = Z_NULL;
641 zret = inflateInit(&c->zstream);
643 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
644 return AVERROR_UNKNOWN;
650 static av_cold int decode_end(AVCodecContext *avctx)
652 ZmbvContext * const c = avctx->priv_data;
654 av_freep(&c->decomp_buf);
656 inflateEnd(&c->zstream);
663 AVCodec ff_zmbv_decoder = {
665 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
666 .type = AVMEDIA_TYPE_VIDEO,
667 .id = AV_CODEC_ID_ZMBV,
668 .priv_data_size = sizeof(ZmbvContext),
671 .decode = decode_frame,
672 .capabilities = AV_CODEC_CAP_DR1,
673 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,