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
29 #include "libavutil/common.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/intreadwrite.h"
33 #include "codec_internal.h"
35 #include "zlib_wrapper.h"
39 #define ZMBV_KEYFRAME 1
40 #define ZMBV_DELTAPAL 2
57 typedef struct ZmbvContext {
58 AVCodecContext *avctx;
62 unsigned int decomp_size;
75 int (*decode_xor)(struct ZmbvContext *c);
79 * Decode XOR'ed frame - 8bpp version
82 static int zmbv_decode_xor_8(ZmbvContext *c)
84 uint8_t *src = c->decomp_buf;
85 uint8_t *output, *prev;
88 int d, dx, dy, bw2, bh2;
96 if (c->flags & ZMBV_DELTAPAL) {
97 for (i = 0; i < 768; i++)
102 src += ((c->bx * c->by * 2 + 3) & ~3);
105 for (y = 0; y < c->height; y += c->bh) {
106 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
107 for (x = 0; x < c->width; x += c->bw) {
108 uint8_t *out, *tprev;
111 dx = mvec[block] >> 1;
112 dy = mvec[block + 1] >> 1;
115 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
117 /* copy block - motion vectors out of bounds are used to zero blocks */
119 tprev = prev + x + dx + dy * c->width;
122 for (j = 0; j < bh2; j++) {
123 if (my + j < 0 || my + j >= c->height) {
125 } else if (mx >= 0 && mx + bw2 <= c->width){
126 memcpy(out, tprev, sizeof(*out) * bw2);
128 for (i = 0; i < bw2; i++) {
129 if (mx + i < 0 || mx + i >= c->width)
139 if (d) { /* apply XOR'ed difference */
141 for (j = 0; j < bh2; j++) {
142 for (i = 0; i < bw2; i++)
148 output += c->width * c->bh;
149 prev += c->width * c->bh;
151 if (src - c->decomp_buf != c->decomp_len)
152 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
153 src-c->decomp_buf, c->decomp_len);
158 * Decode XOR'ed frame - 15bpp and 16bpp version
161 static int zmbv_decode_xor_16(ZmbvContext *c)
163 uint8_t *src = c->decomp_buf;
164 uint16_t *output, *prev;
167 int d, dx, dy, bw2, bh2;
172 output = (uint16_t*)c->cur;
173 prev = (uint16_t*)c->prev;
176 src += ((c->bx * c->by * 2 + 3) & ~3);
179 for (y = 0; y < c->height; y += c->bh) {
180 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
181 for (x = 0; x < c->width; x += c->bw) {
182 uint16_t *out, *tprev;
185 dx = mvec[block] >> 1;
186 dy = mvec[block + 1] >> 1;
189 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
191 /* copy block - motion vectors out of bounds are used to zero blocks */
193 tprev = prev + x + dx + dy * c->width;
196 for (j = 0; j < bh2; j++) {
197 if (my + j < 0 || my + j >= c->height) {
198 memset(out, 0, bw2 * 2);
199 } else if (mx >= 0 && mx + bw2 <= c->width){
200 memcpy(out, tprev, sizeof(*out) * bw2);
202 for (i = 0; i < bw2; i++) {
203 if (mx + i < 0 || mx + i >= c->width)
213 if (d) { /* apply XOR'ed difference */
215 for (j = 0; j < bh2; j++){
216 for (i = 0; i < bw2; i++) {
217 out[i] ^= *((uint16_t*)src);
224 output += c->width * c->bh;
225 prev += c->width * c->bh;
227 if (src - c->decomp_buf != c->decomp_len)
228 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
229 src-c->decomp_buf, c->decomp_len);
233 #ifdef ZMBV_ENABLE_24BPP
235 * Decode XOR'ed frame - 24bpp version
238 static int zmbv_decode_xor_24(ZmbvContext *c)
240 uint8_t *src = c->decomp_buf;
241 uint8_t *output, *prev;
244 int d, dx, dy, bw2, bh2;
253 stride = c->width * 3;
255 src += ((c->bx * c->by * 2 + 3) & ~3);
258 for (y = 0; y < c->height; y += c->bh) {
259 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
260 for (x = 0; x < c->width; x += c->bw) {
261 uint8_t *out, *tprev;
264 dx = mvec[block] >> 1;
265 dy = mvec[block + 1] >> 1;
268 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
270 /* copy block - motion vectors out of bounds are used to zero blocks */
271 out = output + x * 3;
272 tprev = prev + (x + dx) * 3 + dy * stride;
275 for (j = 0; j < bh2; j++) {
276 if (my + j < 0 || my + j >= c->height) {
277 memset(out, 0, bw2 * 3);
278 } else if (mx >= 0 && mx + bw2 <= c->width){
279 memcpy(out, tprev, 3 * bw2);
281 for (i = 0; i < bw2; i++){
282 if (mx + i < 0 || mx + i >= c->width) {
287 out[i * 3 + 0] = tprev[i * 3 + 0];
288 out[i * 3 + 1] = tprev[i * 3 + 1];
289 out[i * 3 + 2] = tprev[i * 3 + 2];
297 if (d) { /* apply XOR'ed difference */
298 out = output + x * 3;
299 for (j = 0; j < bh2; j++) {
300 for (i = 0; i < bw2; i++) {
301 out[i * 3 + 0] ^= *src++;
302 out[i * 3 + 1] ^= *src++;
303 out[i * 3 + 2] ^= *src++;
309 output += stride * c->bh;
310 prev += stride * c->bh;
312 if (src - c->decomp_buf != c->decomp_len)
313 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
314 src-c->decomp_buf, c->decomp_len);
317 #endif //ZMBV_ENABLE_24BPP
320 * Decode XOR'ed frame - 32bpp version
323 static int zmbv_decode_xor_32(ZmbvContext *c)
325 uint8_t *src = c->decomp_buf;
326 uint32_t *output, *prev;
329 int d, dx, dy, bw2, bh2;
334 output = (uint32_t*)c->cur;
335 prev = (uint32_t*)c->prev;
338 src += ((c->bx * c->by * 2 + 3) & ~3);
341 for (y = 0; y < c->height; y += c->bh) {
342 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
343 for (x = 0; x < c->width; x += c->bw) {
344 uint32_t *out, *tprev;
347 dx = mvec[block] >> 1;
348 dy = mvec[block + 1] >> 1;
351 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
353 /* copy block - motion vectors out of bounds are used to zero blocks */
355 tprev = prev + x + dx + dy * c->width;
358 for (j = 0; j < bh2; j++) {
359 if (my + j < 0 || my + j >= c->height) {
360 memset(out, 0, bw2 * 4);
361 } else if (mx >= 0 && mx + bw2 <= c->width){
362 memcpy(out, tprev, sizeof(*out) * bw2);
364 for (i = 0; i < bw2; i++){
365 if (mx + i < 0 || mx + i >= c->width)
375 if (d) { /* apply XOR'ed difference */
377 for (j = 0; j < bh2; j++){
378 for (i = 0; i < bw2; i++) {
379 out[i] ^= *((uint32_t *) src);
386 output += c->width * c->bh;
387 prev += c->width * c->bh;
389 if (src - c->decomp_buf != c->decomp_len)
390 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
391 src-c->decomp_buf, c->decomp_len);
398 static int zmbv_decode_intra(ZmbvContext *c)
400 uint8_t *src = c->decomp_buf;
402 /* make the palette available on the way out */
403 if (c->fmt == ZMBV_FMT_8BPP) {
404 memcpy(c->pal, src, 768);
408 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
412 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
413 int *got_frame, AVPacket *avpkt)
415 const uint8_t *buf = avpkt->data;
416 int buf_size = avpkt->size;
417 ZmbvContext * const c = avctx->priv_data;
418 int zret = Z_OK; // Zlib return code
420 int hi_ver, lo_ver, ret;
425 return AVERROR_INVALIDDATA;
428 if (c->flags & ZMBV_KEYFRAME) {
432 return AVERROR_INVALIDDATA;
439 c->decode_xor = NULL;
443 av_log(avctx, AV_LOG_DEBUG,
444 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
445 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
446 if (hi_ver != 0 || lo_ver != 1) {
447 avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
448 return AVERROR_PATCHWELCOME;
450 if (c->bw == 0 || c->bh == 0) {
451 avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
452 return AVERROR_PATCHWELCOME;
454 if (c->comp != 0 && c->comp != 1) {
455 avpriv_request_sample(avctx, "Compression type %i", c->comp);
456 return AVERROR_PATCHWELCOME;
462 c->decode_xor = zmbv_decode_xor_8;
463 avctx->pix_fmt = AV_PIX_FMT_PAL8;
464 c->stride = c->width;
469 c->decode_xor = zmbv_decode_xor_16;
470 if (c->fmt == ZMBV_FMT_15BPP)
471 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
473 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
474 c->stride = c->width * 2;
476 #ifdef ZMBV_ENABLE_24BPP
479 c->decode_xor = zmbv_decode_xor_24;
480 avctx->pix_fmt = AV_PIX_FMT_BGR24;
481 c->stride = c->width * 3;
483 #endif //ZMBV_ENABLE_24BPP
486 c->decode_xor = zmbv_decode_xor_32;
487 avctx->pix_fmt = AV_PIX_FMT_BGR0;
488 c->stride = c->width * 4;
491 c->decode_xor = NULL;
492 avpriv_request_sample(avctx, "Format %i", c->fmt);
493 return AVERROR_PATCHWELCOME;
496 zret = inflateReset(&c->zstream.zstream);
498 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
499 return AVERROR_UNKNOWN;
502 if (c->alloc_bpp < c->bpp) {
503 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
504 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
505 c->alloc_bpp = c->bpp;
507 c->bx = (c->width + c->bw - 1) / c->bw;
508 c->by = (c->height+ c->bh - 1) / c->bh;
509 if (!c->cur || !c->prev) {
511 return AVERROR(ENOMEM);
513 memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
514 memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
517 if (c->flags & ZMBV_KEYFRAME) {
518 expected_size = avctx->width * avctx->height * (c->bpp / 8);
520 expected_size = (c->bx * c->by * 2 + 3) & ~3;
522 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
523 (c->flags & (ZMBV_DELTAPAL | ZMBV_KEYFRAME)))
524 expected_size += 768;
526 if (!c->got_keyframe) {
527 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
528 return AVERROR_INVALIDDATA;
531 if (c->comp == 0) { // uncompressed data
532 if (c->decomp_size < len) {
533 av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
534 return AVERROR_INVALIDDATA;
536 memcpy(c->decomp_buf, buf, len);
538 } else { // ZLIB-compressed data
539 z_stream *const zstream = &c->zstream.zstream;
541 zstream->total_in = zstream->total_out = 0;
542 zstream->next_in = buf;
543 zstream->avail_in = len;
544 zstream->next_out = c->decomp_buf;
545 zstream->avail_out = c->decomp_size;
546 zret = inflate(zstream, Z_SYNC_FLUSH);
547 if (zret != Z_OK && zret != Z_STREAM_END) {
548 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
549 return AVERROR_INVALIDDATA;
551 c->decomp_len = zstream->total_out;
553 if (expected_size > c->decomp_len ||
554 (c->flags & ZMBV_KEYFRAME) && expected_size < c->decomp_len) {
555 av_log(avctx, AV_LOG_ERROR, "decompressed size %d is incorrect, expected %d\n", c->decomp_len, expected_size);
556 return AVERROR_INVALIDDATA;
558 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
561 if (c->flags & ZMBV_KEYFRAME) {
562 frame->flags |= AV_FRAME_FLAG_KEY;
563 frame->pict_type = AV_PICTURE_TYPE_I;
564 zmbv_decode_intra(c);
566 frame->flags &= ~AV_FRAME_FLAG_KEY;
567 frame->pict_type = AV_PICTURE_TYPE_P;
568 if (c->decomp_len < 2LL * ((c->width + c->bw - 1) / c->bw) * ((c->height + c->bh - 1) / c->bh))
569 return AVERROR_INVALIDDATA;
579 out = frame->data[0];
583 for (j = 0; j < 256; j++)
584 AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
587 #ifdef ZMBV_ENABLE_24BPP
591 av_image_copy_plane(out, frame->linesize[0], src, c->stride,
592 c->stride, c->height);
595 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
597 FFSWAP(uint8_t *, c->cur, c->prev);
601 /* always report that the buffer was completely consumed */
605 static av_cold int decode_init(AVCodecContext *avctx)
607 ZmbvContext * const c = avctx->priv_data;
611 c->width = avctx->width;
612 c->height = avctx->height;
614 c->bpp = avctx->bits_per_coded_sample;
616 if ((avctx->width + 255ULL) * (avctx->height + 64ULL) > FFMIN(avctx->max_pixels, INT_MAX / 4) ) {
617 av_log(avctx, AV_LOG_ERROR, "Internal buffer (decomp_size) larger than max_pixels or too large\n");
618 return AVERROR_INVALIDDATA;
621 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
623 /* Allocate decompression buffer */
624 c->decomp_buf = av_mallocz(c->decomp_size);
625 if (!c->decomp_buf) {
626 av_log(avctx, AV_LOG_ERROR,
627 "Can't allocate decompression buffer.\n");
628 return AVERROR(ENOMEM);
631 return ff_inflate_init(&c->zstream, avctx);
634 static av_cold int decode_end(AVCodecContext *avctx)
636 ZmbvContext * const c = avctx->priv_data;
638 av_freep(&c->decomp_buf);
642 ff_inflate_end(&c->zstream);
647 const FFCodec ff_zmbv_decoder = {
649 CODEC_LONG_NAME("Zip Motion Blocks Video"),
650 .p.type = AVMEDIA_TYPE_VIDEO,
651 .p.id = AV_CODEC_ID_ZMBV,
652 .priv_data_size = sizeof(ZmbvContext),
655 FF_CODEC_DECODE_CB(decode_frame),
656 .p.capabilities = AV_CODEC_CAP_DR1,
657 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,