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/intreadwrite.h"
35 #define ZMBV_KEYFRAME 1
36 #define ZMBV_DELTAPAL 2
53 typedef struct ZmbvContext {
54 AVCodecContext *avctx;
58 unsigned int decomp_size;
69 int (*decode_intra)(struct ZmbvContext *c);
70 int (*decode_xor)(struct ZmbvContext *c);
74 * Decode XOR'ed frame - 8bpp version
77 static int zmbv_decode_xor_8(ZmbvContext *c)
79 uint8_t *src = c->decomp_buf;
80 uint8_t *output, *prev;
83 int d, dx, dy, bw2, bh2;
91 if (c->flags & ZMBV_DELTAPAL) {
92 for (i = 0; i < 768; i++)
97 src += ((c->bx * c->by * 2 + 3) & ~3);
100 for (y = 0; y < c->height; y += c->bh) {
101 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
102 for (x = 0; x < c->width; x += c->bw) {
103 uint8_t *out, *tprev;
106 dx = mvec[block] >> 1;
107 dy = mvec[block + 1] >> 1;
110 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
112 /* copy block - motion vectors out of bounds are used to zero blocks */
114 tprev = prev + x + dx + dy * c->width;
117 for (j = 0; j < bh2; j++) {
118 if (my + j < 0 || my + j >= c->height) {
121 for (i = 0; i < bw2; i++) {
122 if (mx + i < 0 || mx + i >= c->width)
132 if (d) { /* apply XOR'ed difference */
134 for (j = 0; j < bh2; j++) {
135 for (i = 0; i < bw2; i++)
141 output += c->width * c->bh;
142 prev += c->width * c->bh;
144 if (src - c->decomp_buf != c->decomp_len)
145 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
146 src-c->decomp_buf, c->decomp_len);
151 * Decode XOR'ed frame - 15bpp and 16bpp version
154 static int zmbv_decode_xor_16(ZmbvContext *c)
156 uint8_t *src = c->decomp_buf;
157 uint16_t *output, *prev;
160 int d, dx, dy, bw2, bh2;
165 output = (uint16_t*)c->cur;
166 prev = (uint16_t*)c->prev;
169 src += ((c->bx * c->by * 2 + 3) & ~3);
172 for (y = 0; y < c->height; y += c->bh) {
173 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
174 for (x = 0; x < c->width; x += c->bw) {
175 uint16_t *out, *tprev;
178 dx = mvec[block] >> 1;
179 dy = mvec[block + 1] >> 1;
182 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
184 /* copy block - motion vectors out of bounds are used to zero blocks */
186 tprev = prev + x + dx + dy * c->width;
189 for (j = 0; j < bh2; j++) {
190 if (my + j < 0 || my + j >= c->height) {
191 memset(out, 0, bw2 * 2);
193 for (i = 0; i < bw2; i++) {
194 if (mx + i < 0 || mx + i >= c->width)
204 if (d) { /* apply XOR'ed difference */
206 for (j = 0; j < bh2; j++){
207 for (i = 0; i < bw2; i++) {
208 out[i] ^= *((uint16_t*)src);
215 output += c->width * c->bh;
216 prev += c->width * c->bh;
218 if (src - c->decomp_buf != c->decomp_len)
219 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
220 src-c->decomp_buf, c->decomp_len);
224 #ifdef ZMBV_ENABLE_24BPP
226 * Decode XOR'ed frame - 24bpp version
229 static int zmbv_decode_xor_24(ZmbvContext *c)
231 uint8_t *src = c->decomp_buf;
232 uint8_t *output, *prev;
235 int d, dx, dy, bw2, bh2;
244 stride = c->width * 3;
246 src += ((c->bx * c->by * 2 + 3) & ~3);
249 for (y = 0; y < c->height; y += c->bh) {
250 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
251 for (x = 0; x < c->width; x += c->bw) {
252 uint8_t *out, *tprev;
255 dx = mvec[block] >> 1;
256 dy = mvec[block + 1] >> 1;
259 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
261 /* copy block - motion vectors out of bounds are used to zero blocks */
262 out = output + x * 3;
263 tprev = prev + (x + dx) * 3 + dy * stride;
266 for (j = 0; j < bh2; j++) {
267 if (my + j < 0 || my + j >= c->height) {
268 memset(out, 0, bw2 * 3);
270 for (i = 0; i < bw2; i++){
271 if (mx + i < 0 || mx + i >= c->width) {
276 out[i * 3 + 0] = tprev[i * 3 + 0];
277 out[i * 3 + 1] = tprev[i * 3 + 1];
278 out[i * 3 + 2] = tprev[i * 3 + 2];
286 if (d) { /* apply XOR'ed difference */
287 out = output + x * 3;
288 for (j = 0; j < bh2; j++) {
289 for (i = 0; i < bw2; i++) {
290 out[i * 3 + 0] ^= *src++;
291 out[i * 3 + 1] ^= *src++;
292 out[i * 3 + 2] ^= *src++;
298 output += stride * c->bh;
299 prev += stride * c->bh;
301 if (src - c->decomp_buf != c->decomp_len)
302 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
303 src-c->decomp_buf, c->decomp_len);
306 #endif //ZMBV_ENABLE_24BPP
309 * Decode XOR'ed frame - 32bpp version
312 static int zmbv_decode_xor_32(ZmbvContext *c)
314 uint8_t *src = c->decomp_buf;
315 uint32_t *output, *prev;
318 int d, dx, dy, bw2, bh2;
323 output = (uint32_t*)c->cur;
324 prev = (uint32_t*)c->prev;
327 src += ((c->bx * c->by * 2 + 3) & ~3);
330 for (y = 0; y < c->height; y += c->bh) {
331 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
332 for (x = 0; x < c->width; x += c->bw) {
333 uint32_t *out, *tprev;
336 dx = mvec[block] >> 1;
337 dy = mvec[block + 1] >> 1;
340 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
342 /* copy block - motion vectors out of bounds are used to zero blocks */
344 tprev = prev + x + dx + dy * c->width;
347 for (j = 0; j < bh2; j++) {
348 if (my + j < 0 || my + j >= c->height) {
349 memset(out, 0, bw2 * 4);
351 for (i = 0; i < bw2; i++){
352 if (mx + i < 0 || mx + i >= c->width)
362 if (d) { /* apply XOR'ed difference */
364 for (j = 0; j < bh2; j++){
365 for (i = 0; i < bw2; i++) {
366 out[i] ^= *((uint32_t *) src);
373 output += c->width * c->bh;
374 prev += c->width * c->bh;
376 if (src - c->decomp_buf != c->decomp_len)
377 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
378 src-c->decomp_buf, c->decomp_len);
385 static int zmbv_decode_intra(ZmbvContext *c)
387 uint8_t *src = c->decomp_buf;
389 /* make the palette available on the way out */
390 if (c->fmt == ZMBV_FMT_8BPP) {
391 memcpy(c->pal, src, 768);
395 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
399 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
401 const uint8_t *buf = avpkt->data;
402 int buf_size = avpkt->size;
403 ZmbvContext * const c = avctx->priv_data;
404 int zret = Z_OK; // Zlib return code
406 int hi_ver, lo_ver, ret;
409 avctx->release_buffer(avctx, &c->pic);
411 c->pic.reference = 3;
412 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
413 if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
414 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
421 if (c->flags & ZMBV_KEYFRAME) {
422 void *decode_intra = NULL;
423 c->decode_intra= NULL;
433 av_log(avctx, AV_LOG_DEBUG,
434 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
435 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
436 if (hi_ver != 0 || lo_ver != 1) {
437 av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
439 return AVERROR_PATCHWELCOME;
441 if (c->bw == 0 || c->bh == 0) {
442 av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
444 return AVERROR_PATCHWELCOME;
446 if (c->comp != 0 && c->comp != 1) {
447 av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
449 return AVERROR_PATCHWELCOME;
455 decode_intra = zmbv_decode_intra;
456 c->decode_xor = zmbv_decode_xor_8;
461 decode_intra = zmbv_decode_intra;
462 c->decode_xor = zmbv_decode_xor_16;
464 #ifdef ZMBV_ENABLE_24BPP
467 decode_intra = zmbv_decode_intra;
468 c->decode_xor = zmbv_decode_xor_24;
470 #endif //ZMBV_ENABLE_24BPP
473 decode_intra = zmbv_decode_intra;
474 c->decode_xor = zmbv_decode_xor_32;
477 c->decode_xor = NULL;
478 av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
480 return AVERROR_PATCHWELCOME;
483 zret = inflateReset(&c->zstream);
485 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
489 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
490 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
491 c->bx = (c->width + c->bw - 1) / c->bw;
492 c->by = (c->height+ c->bh - 1) / c->bh;
493 if (!c->cur || !c->prev)
495 c->decode_intra= decode_intra;
498 if (c->decode_intra == NULL) {
499 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
500 return AVERROR_INVALIDDATA;
503 if (c->comp == 0) { //Uncompressed data
504 memcpy(c->decomp_buf, buf, len);
506 } else { // ZLIB-compressed data
507 c->zstream.total_in = c->zstream.total_out = 0;
508 c->zstream.next_in = buf;
509 c->zstream.avail_in = len;
510 c->zstream.next_out = c->decomp_buf;
511 c->zstream.avail_out = c->decomp_size;
512 inflate(&c->zstream, Z_FINISH);
513 c->decomp_len = c->zstream.total_out;
515 if (c->flags & ZMBV_KEYFRAME) {
516 c->pic.key_frame = 1;
517 c->pic.pict_type = AV_PICTURE_TYPE_I;
520 c->pic.key_frame = 0;
521 c->pic.pict_type = AV_PICTURE_TYPE_P;
531 out = c->pic.data[0];
535 for (j = 0; j < c->height; j++) {
536 for (i = 0; i < c->width; i++) {
537 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
538 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
539 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
542 out += c->pic.linesize[0];
546 for (j = 0; j < c->height; j++) {
547 for (i = 0; i < c->width; i++) {
548 uint16_t tmp = AV_RL16(src);
550 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
551 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
552 out[i * 3 + 2] = (tmp & 0x001F) << 3;
554 out += c->pic.linesize[0];
558 for (j = 0; j < c->height; j++) {
559 for (i = 0; i < c->width; i++) {
560 uint16_t tmp = AV_RL16(src);
562 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
563 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
564 out[i * 3 + 2] = (tmp & 0x001F) << 3;
566 out += c->pic.linesize[0];
569 #ifdef ZMBV_ENABLE_24BPP
571 for (j = 0; j < c->height; j++) {
572 memcpy(out, src, c->width * 3);
574 out += c->pic.linesize[0];
577 #endif //ZMBV_ENABLE_24BPP
579 for (j = 0; j < c->height; j++) {
580 for (i = 0; i < c->width; i++) {
581 uint32_t tmp = AV_RL32(src);
583 AV_WB24(out+(i*3), tmp);
585 out += c->pic.linesize[0];
589 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
591 FFSWAP(uint8_t *, c->cur, c->prev);
593 *data_size = sizeof(AVFrame);
594 *(AVFrame*)data = c->pic;
596 /* always report that the buffer was completely consumed */
607 static av_cold int decode_init(AVCodecContext *avctx)
609 ZmbvContext * const c = avctx->priv_data;
610 int zret; // Zlib return code
614 c->width = avctx->width;
615 c->height = avctx->height;
616 avcodec_get_frame_defaults(&c->pic);
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 avctx->pix_fmt = PIX_FMT_RGB24;
624 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
626 /* Allocate decompression buffer */
627 if (c->decomp_size) {
628 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
629 av_log(avctx, AV_LOG_ERROR,
630 "Can't allocate decompression buffer.\n");
631 return AVERROR(ENOMEM);
635 c->zstream.zalloc = Z_NULL;
636 c->zstream.zfree = Z_NULL;
637 c->zstream.opaque = Z_NULL;
638 zret = inflateInit(&c->zstream);
640 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
651 * Uninit zmbv decoder
654 static av_cold int decode_end(AVCodecContext *avctx)
656 ZmbvContext * const c = avctx->priv_data;
658 av_freep(&c->decomp_buf);
661 avctx->release_buffer(avctx, &c->pic);
662 inflateEnd(&c->zstream);
669 AVCodec ff_zmbv_decoder = {
671 .type = AVMEDIA_TYPE_VIDEO,
673 .priv_data_size = sizeof(ZmbvContext),
676 .decode = decode_frame,
677 .capabilities = CODEC_CAP_DR1,
678 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),