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;
60 unsigned int decomp_size;
72 int (*decode_intra)(struct ZmbvContext *c);
73 int (*decode_xor)(struct ZmbvContext *c);
77 * Decode XOR'ed frame - 8bpp version
80 static int zmbv_decode_xor_8(ZmbvContext *c)
82 uint8_t *src = c->decomp_buf;
83 uint8_t *output, *prev;
86 int d, dx, dy, bw2, bh2;
94 if (c->flags & ZMBV_DELTAPAL) {
95 for (i = 0; i < 768; i++)
100 src += ((c->bx * c->by * 2 + 3) & ~3);
103 for (y = 0; y < c->height; y += c->bh) {
104 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
105 for (x = 0; x < c->width; x += c->bw) {
106 uint8_t *out, *tprev;
109 dx = mvec[block] >> 1;
110 dy = mvec[block + 1] >> 1;
113 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
115 /* copy block - motion vectors out of bounds are used to zero blocks */
117 tprev = prev + x + dx + dy * c->width;
120 for (j = 0; j < bh2; j++) {
121 if (my + j < 0 || my + j >= c->height) {
124 for (i = 0; i < bw2; i++) {
125 if (mx + i < 0 || mx + i >= c->width)
135 if (d) { /* apply XOR'ed difference */
137 for (j = 0; j < bh2; j++) {
138 for (i = 0; i < bw2; i++)
144 output += c->width * c->bh;
145 prev += c->width * c->bh;
147 if (src - c->decomp_buf != c->decomp_len)
148 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
149 src-c->decomp_buf, c->decomp_len);
154 * Decode XOR'ed frame - 15bpp and 16bpp version
157 static int zmbv_decode_xor_16(ZmbvContext *c)
159 uint8_t *src = c->decomp_buf;
160 uint16_t *output, *prev;
163 int d, dx, dy, bw2, bh2;
168 output = (uint16_t*)c->cur;
169 prev = (uint16_t*)c->prev;
172 src += ((c->bx * c->by * 2 + 3) & ~3);
175 for (y = 0; y < c->height; y += c->bh) {
176 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
177 for (x = 0; x < c->width; x += c->bw) {
178 uint16_t *out, *tprev;
181 dx = mvec[block] >> 1;
182 dy = mvec[block + 1] >> 1;
185 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
187 /* copy block - motion vectors out of bounds are used to zero blocks */
189 tprev = prev + x + dx + dy * c->width;
192 for (j = 0; j < bh2; j++) {
193 if (my + j < 0 || my + j >= c->height) {
194 memset(out, 0, bw2 * 2);
196 for (i = 0; i < bw2; i++) {
197 if (mx + i < 0 || mx + i >= c->width)
207 if (d) { /* apply XOR'ed difference */
209 for (j = 0; j < bh2; j++){
210 for (i = 0; i < bw2; i++) {
211 out[i] ^= *((uint16_t*)src);
218 output += c->width * c->bh;
219 prev += c->width * c->bh;
221 if (src - c->decomp_buf != c->decomp_len)
222 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
223 src-c->decomp_buf, c->decomp_len);
227 #ifdef ZMBV_ENABLE_24BPP
229 * Decode XOR'ed frame - 24bpp version
232 static int zmbv_decode_xor_24(ZmbvContext *c)
234 uint8_t *src = c->decomp_buf;
235 uint8_t *output, *prev;
238 int d, dx, dy, bw2, bh2;
247 stride = c->width * 3;
249 src += ((c->bx * c->by * 2 + 3) & ~3);
252 for (y = 0; y < c->height; y += c->bh) {
253 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
254 for (x = 0; x < c->width; x += c->bw) {
255 uint8_t *out, *tprev;
258 dx = mvec[block] >> 1;
259 dy = mvec[block + 1] >> 1;
262 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
264 /* copy block - motion vectors out of bounds are used to zero blocks */
265 out = output + x * 3;
266 tprev = prev + (x + dx) * 3 + dy * stride;
269 for (j = 0; j < bh2; j++) {
270 if (my + j < 0 || my + j >= c->height) {
271 memset(out, 0, bw2 * 3);
273 for (i = 0; i < bw2; i++){
274 if (mx + i < 0 || mx + i >= c->width) {
279 out[i * 3 + 0] = tprev[i * 3 + 0];
280 out[i * 3 + 1] = tprev[i * 3 + 1];
281 out[i * 3 + 2] = tprev[i * 3 + 2];
289 if (d) { /* apply XOR'ed difference */
290 out = output + x * 3;
291 for (j = 0; j < bh2; j++) {
292 for (i = 0; i < bw2; i++) {
293 out[i * 3 + 0] ^= *src++;
294 out[i * 3 + 1] ^= *src++;
295 out[i * 3 + 2] ^= *src++;
301 output += stride * c->bh;
302 prev += stride * c->bh;
304 if (src - c->decomp_buf != c->decomp_len)
305 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
306 src-c->decomp_buf, c->decomp_len);
309 #endif //ZMBV_ENABLE_24BPP
312 * Decode XOR'ed frame - 32bpp version
315 static int zmbv_decode_xor_32(ZmbvContext *c)
317 uint8_t *src = c->decomp_buf;
318 uint32_t *output, *prev;
321 int d, dx, dy, bw2, bh2;
326 output = (uint32_t*)c->cur;
327 prev = (uint32_t*)c->prev;
330 src += ((c->bx * c->by * 2 + 3) & ~3);
333 for (y = 0; y < c->height; y += c->bh) {
334 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
335 for (x = 0; x < c->width; x += c->bw) {
336 uint32_t *out, *tprev;
339 dx = mvec[block] >> 1;
340 dy = mvec[block + 1] >> 1;
343 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
345 /* copy block - motion vectors out of bounds are used to zero blocks */
347 tprev = prev + x + dx + dy * c->width;
350 for (j = 0; j < bh2; j++) {
351 if (my + j < 0 || my + j >= c->height) {
352 memset(out, 0, bw2 * 4);
354 for (i = 0; i < bw2; i++){
355 if (mx + i < 0 || mx + i >= c->width)
365 if (d) { /* apply XOR'ed difference */
367 for (j = 0; j < bh2; j++){
368 for (i = 0; i < bw2; i++) {
369 out[i] ^= *((uint32_t *) src);
376 output += c->width * c->bh;
377 prev += c->width * c->bh;
379 if (src - c->decomp_buf != c->decomp_len)
380 av_log(c->avctx, AV_LOG_ERROR, "Used %"PTRDIFF_SPECIFIER" of %i bytes\n",
381 src-c->decomp_buf, c->decomp_len);
388 static int zmbv_decode_intra(ZmbvContext *c)
390 uint8_t *src = c->decomp_buf;
392 /* make the palette available on the way out */
393 if (c->fmt == ZMBV_FMT_8BPP) {
394 memcpy(c->pal, src, 768);
398 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
402 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
404 AVFrame *frame = data;
405 const uint8_t *buf = avpkt->data;
406 int buf_size = avpkt->size;
407 ZmbvContext * const c = avctx->priv_data;
408 int zret = Z_OK; // Zlib return code
410 int hi_ver, lo_ver, ret;
414 return AVERROR_INVALIDDATA;
417 if (c->flags & ZMBV_KEYFRAME) {
418 void *decode_intra = NULL;
419 c->decode_intra= NULL;
422 return AVERROR_INVALIDDATA;
429 c->decode_intra = NULL;
430 c->decode_xor = NULL;
434 av_log(avctx, AV_LOG_DEBUG,
435 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
436 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
437 if (hi_ver != 0 || lo_ver != 1) {
438 avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
439 return AVERROR_PATCHWELCOME;
441 if (c->bw == 0 || c->bh == 0) {
442 avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
443 return AVERROR_PATCHWELCOME;
445 if (c->comp != 0 && c->comp != 1) {
446 avpriv_request_sample(avctx, "Compression type %i", c->comp);
447 return AVERROR_PATCHWELCOME;
453 decode_intra = zmbv_decode_intra;
454 c->decode_xor = zmbv_decode_xor_8;
455 avctx->pix_fmt = AV_PIX_FMT_PAL8;
456 c->stride = c->width;
461 decode_intra = zmbv_decode_intra;
462 c->decode_xor = zmbv_decode_xor_16;
463 if (c->fmt == ZMBV_FMT_15BPP)
464 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
466 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
467 c->stride = c->width * 2;
469 #ifdef ZMBV_ENABLE_24BPP
472 decode_intra = zmbv_decode_intra;
473 c->decode_xor = zmbv_decode_xor_24;
474 avctx->pix_fmt = AV_PIX_FMT_RGB24;
475 c->stride = c->width * 3;
477 #endif //ZMBV_ENABLE_24BPP
480 decode_intra = zmbv_decode_intra;
481 c->decode_xor = zmbv_decode_xor_32;
482 avctx->pix_fmt = AV_PIX_FMT_BGR0;
483 c->stride = c->width * 4;
486 c->decode_xor = NULL;
487 avpriv_request_sample(avctx, "Format %i", c->fmt);
488 return AVERROR_PATCHWELCOME;
491 zret = inflateReset(&c->zstream);
493 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
494 return AVERROR_UNKNOWN;
497 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
498 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
499 c->bx = (c->width + c->bw - 1) / c->bw;
500 c->by = (c->height+ c->bh - 1) / c->bh;
501 if (!c->cur || !c->prev)
502 return AVERROR(ENOMEM);
503 memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
504 memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
505 c->decode_intra= decode_intra;
508 if (!c->decode_intra) {
509 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
510 return AVERROR_INVALIDDATA;
513 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
516 if (c->comp == 0) { //Uncompressed data
517 if (c->decomp_size < len) {
518 av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
519 return AVERROR_INVALIDDATA;
521 memcpy(c->decomp_buf, buf, len);
522 } else { // ZLIB-compressed data
523 c->zstream.total_in = c->zstream.total_out = 0;
524 c->zstream.next_in = (uint8_t*)buf;
525 c->zstream.avail_in = len;
526 c->zstream.next_out = c->decomp_buf;
527 c->zstream.avail_out = c->decomp_size;
528 zret = inflate(&c->zstream, Z_SYNC_FLUSH);
529 if (zret != Z_OK && zret != Z_STREAM_END) {
530 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
531 return AVERROR_INVALIDDATA;
533 c->decomp_len = c->zstream.total_out;
535 if (c->flags & ZMBV_KEYFRAME) {
536 frame->key_frame = 1;
537 frame->pict_type = AV_PICTURE_TYPE_I;
540 frame->key_frame = 0;
541 frame->pict_type = AV_PICTURE_TYPE_P;
551 out = frame->data[0];
555 for (j = 0; j < 256; j++)
556 AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
559 #ifdef ZMBV_ENABLE_24BPP
563 av_image_copy_plane(out, frame->linesize[0], src, c->stride,
564 c->stride, c->height);
567 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
569 FFSWAP(uint8_t *, c->cur, c->prev);
573 /* always report that the buffer was completely consumed */
577 static av_cold int decode_init(AVCodecContext *avctx)
579 ZmbvContext * const c = avctx->priv_data;
580 int zret; // Zlib return code
584 c->width = avctx->width;
585 c->height = avctx->height;
587 c->bpp = avctx->bits_per_coded_sample;
589 // Needed if zlib unused or init aborted before inflateInit
590 memset(&c->zstream, 0, sizeof(z_stream));
592 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
594 /* Allocate decompression buffer */
595 if (c->decomp_size) {
596 if (!(c->decomp_buf = av_mallocz(c->decomp_size))) {
597 av_log(avctx, AV_LOG_ERROR,
598 "Can't allocate decompression buffer.\n");
599 return AVERROR(ENOMEM);
603 c->zstream.zalloc = Z_NULL;
604 c->zstream.zfree = Z_NULL;
605 c->zstream.opaque = Z_NULL;
606 zret = inflateInit(&c->zstream);
608 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
609 return AVERROR_UNKNOWN;
615 static av_cold int decode_end(AVCodecContext *avctx)
617 ZmbvContext * const c = avctx->priv_data;
619 av_freep(&c->decomp_buf);
621 inflateEnd(&c->zstream);
628 AVCodec ff_zmbv_decoder = {
630 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
631 .type = AVMEDIA_TYPE_VIDEO,
632 .id = AV_CODEC_ID_ZMBV,
633 .priv_data_size = sizeof(ZmbvContext),
636 .decode = decode_frame,
637 .capabilities = CODEC_CAP_DR1,