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/intreadwrite.h"
36 #define ZMBV_KEYFRAME 1
37 #define ZMBV_DELTAPAL 2
54 typedef struct ZmbvContext {
55 AVCodecContext *avctx;
59 unsigned int decomp_size;
70 int (*decode_intra)(struct ZmbvContext *c);
71 int (*decode_xor)(struct ZmbvContext *c);
75 * Decode XOR'ed frame - 8bpp version
78 static int zmbv_decode_xor_8(ZmbvContext *c)
80 uint8_t *src = c->decomp_buf;
81 uint8_t *output, *prev;
84 int d, dx, dy, bw2, bh2;
92 if (c->flags & ZMBV_DELTAPAL) {
93 for (i = 0; i < 768; i++)
98 src += ((c->bx * c->by * 2 + 3) & ~3);
101 for (y = 0; y < c->height; y += c->bh) {
102 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
103 for (x = 0; x < c->width; x += c->bw) {
104 uint8_t *out, *tprev;
107 dx = mvec[block] >> 1;
108 dy = mvec[block + 1] >> 1;
111 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
113 /* copy block - motion vectors out of bounds are used to zero blocks */
115 tprev = prev + x + dx + dy * c->width;
118 for (j = 0; j < bh2; j++) {
119 if (my + j < 0 || my + j >= c->height) {
122 for (i = 0; i < bw2; i++) {
123 if (mx + i < 0 || mx + i >= c->width)
133 if (d) { /* apply XOR'ed difference */
135 for (j = 0; j < bh2; j++) {
136 for (i = 0; i < bw2; i++)
142 output += c->width * c->bh;
143 prev += c->width * c->bh;
145 if (src - c->decomp_buf != c->decomp_len)
146 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
147 src-c->decomp_buf, c->decomp_len);
152 * Decode XOR'ed frame - 15bpp and 16bpp version
155 static int zmbv_decode_xor_16(ZmbvContext *c)
157 uint8_t *src = c->decomp_buf;
158 uint16_t *output, *prev;
161 int d, dx, dy, bw2, bh2;
166 output = (uint16_t*)c->cur;
167 prev = (uint16_t*)c->prev;
170 src += ((c->bx * c->by * 2 + 3) & ~3);
173 for (y = 0; y < c->height; y += c->bh) {
174 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
175 for (x = 0; x < c->width; x += c->bw) {
176 uint16_t *out, *tprev;
179 dx = mvec[block] >> 1;
180 dy = mvec[block + 1] >> 1;
183 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
185 /* copy block - motion vectors out of bounds are used to zero blocks */
187 tprev = prev + x + dx + dy * c->width;
190 for (j = 0; j < bh2; j++) {
191 if (my + j < 0 || my + j >= c->height) {
192 memset(out, 0, bw2 * 2);
194 for (i = 0; i < bw2; i++) {
195 if (mx + i < 0 || mx + i >= c->width)
205 if (d) { /* apply XOR'ed difference */
207 for (j = 0; j < bh2; j++){
208 for (i = 0; i < bw2; i++) {
209 out[i] ^= *((uint16_t*)src);
216 output += c->width * c->bh;
217 prev += c->width * c->bh;
219 if (src - c->decomp_buf != c->decomp_len)
220 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
221 src-c->decomp_buf, c->decomp_len);
225 #ifdef ZMBV_ENABLE_24BPP
227 * Decode XOR'ed frame - 24bpp version
230 static int zmbv_decode_xor_24(ZmbvContext *c)
232 uint8_t *src = c->decomp_buf;
233 uint8_t *output, *prev;
236 int d, dx, dy, bw2, bh2;
245 stride = c->width * 3;
247 src += ((c->bx * c->by * 2 + 3) & ~3);
250 for (y = 0; y < c->height; y += c->bh) {
251 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
252 for (x = 0; x < c->width; x += c->bw) {
253 uint8_t *out, *tprev;
256 dx = mvec[block] >> 1;
257 dy = mvec[block + 1] >> 1;
260 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
262 /* copy block - motion vectors out of bounds are used to zero blocks */
263 out = output + x * 3;
264 tprev = prev + (x + dx) * 3 + dy * stride;
267 for (j = 0; j < bh2; j++) {
268 if (my + j < 0 || my + j >= c->height) {
269 memset(out, 0, bw2 * 3);
271 for (i = 0; i < bw2; i++){
272 if (mx + i < 0 || mx + i >= c->width) {
277 out[i * 3 + 0] = tprev[i * 3 + 0];
278 out[i * 3 + 1] = tprev[i * 3 + 1];
279 out[i * 3 + 2] = tprev[i * 3 + 2];
287 if (d) { /* apply XOR'ed difference */
288 out = output + x * 3;
289 for (j = 0; j < bh2; j++) {
290 for (i = 0; i < bw2; i++) {
291 out[i * 3 + 0] ^= *src++;
292 out[i * 3 + 1] ^= *src++;
293 out[i * 3 + 2] ^= *src++;
299 output += stride * c->bh;
300 prev += stride * c->bh;
302 if (src - c->decomp_buf != c->decomp_len)
303 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n",
304 src-c->decomp_buf, c->decomp_len);
307 #endif //ZMBV_ENABLE_24BPP
310 * Decode XOR'ed frame - 32bpp version
313 static int zmbv_decode_xor_32(ZmbvContext *c)
315 uint8_t *src = c->decomp_buf;
316 uint32_t *output, *prev;
319 int d, dx, dy, bw2, bh2;
324 output = (uint32_t*)c->cur;
325 prev = (uint32_t*)c->prev;
328 src += ((c->bx * c->by * 2 + 3) & ~3);
331 for (y = 0; y < c->height; y += c->bh) {
332 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
333 for (x = 0; x < c->width; x += c->bw) {
334 uint32_t *out, *tprev;
337 dx = mvec[block] >> 1;
338 dy = mvec[block + 1] >> 1;
341 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
343 /* copy block - motion vectors out of bounds are used to zero blocks */
345 tprev = prev + x + dx + dy * c->width;
348 for (j = 0; j < bh2; j++) {
349 if (my + j < 0 || my + j >= c->height) {
350 memset(out, 0, bw2 * 4);
352 for (i = 0; i < bw2; i++){
353 if (mx + i < 0 || mx + i >= c->width)
363 if (d) { /* apply XOR'ed difference */
365 for (j = 0; j < bh2; j++){
366 for (i = 0; i < bw2; i++) {
367 out[i] ^= *((uint32_t *) src);
374 output += c->width * c->bh;
375 prev += c->width * c->bh;
377 if (src - c->decomp_buf != c->decomp_len)
378 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n",
379 src-c->decomp_buf, c->decomp_len);
386 static int zmbv_decode_intra(ZmbvContext *c)
388 uint8_t *src = c->decomp_buf;
390 /* make the palette available on the way out */
391 if (c->fmt == ZMBV_FMT_8BPP) {
392 memcpy(c->pal, src, 768);
396 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
400 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
402 const uint8_t *buf = avpkt->data;
403 int buf_size = avpkt->size;
404 ZmbvContext * const c = avctx->priv_data;
405 int zret = Z_OK; // Zlib return code
407 int hi_ver, lo_ver, ret;
410 avctx->release_buffer(avctx, &c->pic);
412 c->pic.reference = 3;
413 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
414 if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
415 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
422 if (c->flags & ZMBV_KEYFRAME) {
423 void *decode_intra = NULL;
424 c->decode_intra= 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 av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
440 return AVERROR_PATCHWELCOME;
442 if (c->bw == 0 || c->bh == 0) {
443 av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
445 return AVERROR_PATCHWELCOME;
447 if (c->comp != 0 && c->comp != 1) {
448 av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
450 return AVERROR_PATCHWELCOME;
456 decode_intra = zmbv_decode_intra;
457 c->decode_xor = zmbv_decode_xor_8;
462 decode_intra = zmbv_decode_intra;
463 c->decode_xor = zmbv_decode_xor_16;
465 #ifdef ZMBV_ENABLE_24BPP
468 decode_intra = zmbv_decode_intra;
469 c->decode_xor = zmbv_decode_xor_24;
471 #endif //ZMBV_ENABLE_24BPP
474 decode_intra = zmbv_decode_intra;
475 c->decode_xor = zmbv_decode_xor_32;
478 c->decode_xor = NULL;
479 av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
481 return AVERROR_PATCHWELCOME;
484 zret = inflateReset(&c->zstream);
486 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
490 c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8));
491 c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8));
492 c->bx = (c->width + c->bw - 1) / c->bw;
493 c->by = (c->height+ c->bh - 1) / c->bh;
494 if (!c->cur || !c->prev)
496 c->decode_intra= decode_intra;
499 if (c->decode_intra == NULL) {
500 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
501 return AVERROR_INVALIDDATA;
504 if (c->comp == 0) { //Uncompressed data
505 if (c->decomp_size < len) {
506 av_log(avctx, AV_LOG_ERROR, "decomp buffer too small\n");
507 return AVERROR_INVALIDDATA;
509 memcpy(c->decomp_buf, buf, len);
510 } else { // ZLIB-compressed data
511 c->zstream.total_in = c->zstream.total_out = 0;
512 c->zstream.next_in = (uint8_t*)buf;
513 c->zstream.avail_in = len;
514 c->zstream.next_out = c->decomp_buf;
515 c->zstream.avail_out = c->decomp_size;
516 zret = inflate(&c->zstream, Z_SYNC_FLUSH);
517 if (zret != Z_OK && zret != Z_STREAM_END) {
518 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
519 return AVERROR_INVALIDDATA;
521 c->decomp_len = c->zstream.total_out;
523 if (c->flags & ZMBV_KEYFRAME) {
524 c->pic.key_frame = 1;
525 c->pic.pict_type = AV_PICTURE_TYPE_I;
528 c->pic.key_frame = 0;
529 c->pic.pict_type = AV_PICTURE_TYPE_P;
539 out = c->pic.data[0];
543 for (j = 0; j < c->height; j++) {
544 for (i = 0; i < c->width; i++) {
545 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
546 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
547 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
550 out += c->pic.linesize[0];
554 for (j = 0; j < c->height; j++) {
555 for (i = 0; i < c->width; i++) {
556 uint16_t tmp = AV_RL16(src);
558 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
559 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
560 out[i * 3 + 2] = (tmp & 0x001F) << 3;
562 out += c->pic.linesize[0];
566 for (j = 0; j < c->height; j++) {
567 for (i = 0; i < c->width; i++) {
568 uint16_t tmp = AV_RL16(src);
570 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
571 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
572 out[i * 3 + 2] = (tmp & 0x001F) << 3;
574 out += c->pic.linesize[0];
577 #ifdef ZMBV_ENABLE_24BPP
579 for (j = 0; j < c->height; j++) {
580 memcpy(out, src, c->width * 3);
582 out += c->pic.linesize[0];
585 #endif //ZMBV_ENABLE_24BPP
587 for (j = 0; j < c->height; j++) {
588 for (i = 0; i < c->width; i++) {
589 uint32_t tmp = AV_RL32(src);
591 AV_WB24(out+(i*3), tmp);
593 out += c->pic.linesize[0];
597 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
599 FFSWAP(uint8_t *, c->cur, c->prev);
601 *data_size = sizeof(AVFrame);
602 *(AVFrame*)data = c->pic;
604 /* always report that the buffer was completely consumed */
615 static av_cold int decode_init(AVCodecContext *avctx)
617 ZmbvContext * const c = avctx->priv_data;
618 int zret; // Zlib return code
622 c->width = avctx->width;
623 c->height = avctx->height;
624 avcodec_get_frame_defaults(&c->pic);
626 c->bpp = avctx->bits_per_coded_sample;
628 // Needed if zlib unused or init aborted before inflateInit
629 memset(&c->zstream, 0, sizeof(z_stream));
631 avctx->pix_fmt = AV_PIX_FMT_RGB24;
632 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
634 /* Allocate decompression buffer */
635 if (c->decomp_size) {
636 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
637 av_log(avctx, AV_LOG_ERROR,
638 "Can't allocate decompression buffer.\n");
639 return AVERROR(ENOMEM);
643 c->zstream.zalloc = Z_NULL;
644 c->zstream.zfree = Z_NULL;
645 c->zstream.opaque = Z_NULL;
646 zret = inflateInit(&c->zstream);
648 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
659 * Uninit zmbv decoder
662 static av_cold int decode_end(AVCodecContext *avctx)
664 ZmbvContext * const c = avctx->priv_data;
666 av_freep(&c->decomp_buf);
669 avctx->release_buffer(avctx, &c->pic);
670 inflateEnd(&c->zstream);
677 AVCodec ff_zmbv_decoder = {
679 .type = AVMEDIA_TYPE_VIDEO,
680 .id = AV_CODEC_ID_ZMBV,
681 .priv_data_size = sizeof(ZmbvContext),
684 .decode = decode_frame,
685 .capabilities = CODEC_CAP_DR1,
686 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),