2 * Zip Motion Blocks Video (ZMBV) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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;
410 avctx->release_buffer(avctx, &c->pic);
412 c->pic.reference = 1;
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) {
432 av_log(avctx, AV_LOG_DEBUG,
433 "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
434 c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
435 if (hi_ver != 0 || lo_ver != 1) {
436 av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
438 return AVERROR_PATCHWELCOME;
440 if (c->bw == 0 || c->bh == 0) {
441 av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
443 return AVERROR_PATCHWELCOME;
445 if (c->comp != 0 && c->comp != 1) {
446 av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
448 return AVERROR_PATCHWELCOME;
454 c->decode_intra = zmbv_decode_intra;
455 c->decode_xor = zmbv_decode_xor_8;
460 c->decode_intra = zmbv_decode_intra;
461 c->decode_xor = zmbv_decode_xor_16;
463 #ifdef ZMBV_ENABLE_24BPP
466 c->decode_intra = zmbv_decode_intra;
467 c->decode_xor = zmbv_decode_xor_24;
469 #endif //ZMBV_ENABLE_24BPP
472 c->decode_intra = zmbv_decode_intra;
473 c->decode_xor = zmbv_decode_xor_32;
476 c->decode_intra = NULL;
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 tmp = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
491 return AVERROR(ENOMEM);
493 tmp = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
495 return AVERROR(ENOMEM);
497 c->bx = (c->width + c->bw - 1) / c->bw;
498 c->by = (c->height + c->bh - 1) / c->bh;
501 if (c->decode_intra == NULL) {
502 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
503 return AVERROR_INVALIDDATA;
506 if (c->comp == 0) { //Uncompressed data
507 memcpy(c->decomp_buf, buf, len);
509 } else { // ZLIB-compressed data
510 c->zstream.total_in = c->zstream.total_out = 0;
511 c->zstream.next_in = buf;
512 c->zstream.avail_in = len;
513 c->zstream.next_out = c->decomp_buf;
514 c->zstream.avail_out = c->decomp_size;
515 zret = inflate(&c->zstream, Z_SYNC_FLUSH);
516 if (zret != Z_OK && zret != Z_STREAM_END) {
517 av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
518 return AVERROR_INVALIDDATA;
520 c->decomp_len = c->zstream.total_out;
522 if (c->flags & ZMBV_KEYFRAME) {
523 c->pic.key_frame = 1;
524 c->pic.pict_type = AV_PICTURE_TYPE_I;
527 c->pic.key_frame = 0;
528 c->pic.pict_type = AV_PICTURE_TYPE_P;
538 out = c->pic.data[0];
542 for (j = 0; j < c->height; j++) {
543 for (i = 0; i < c->width; i++) {
544 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
545 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
546 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
549 out += c->pic.linesize[0];
553 for (j = 0; j < c->height; j++) {
554 for (i = 0; i < c->width; i++) {
555 uint16_t tmp = AV_RL16(src);
557 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
558 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
559 out[i * 3 + 2] = (tmp & 0x001F) << 3;
561 out += c->pic.linesize[0];
565 for (j = 0; j < c->height; j++) {
566 for (i = 0; i < c->width; i++) {
567 uint16_t tmp = AV_RL16(src);
569 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
570 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
571 out[i * 3 + 2] = (tmp & 0x001F) << 3;
573 out += c->pic.linesize[0];
576 #ifdef ZMBV_ENABLE_24BPP
578 for (j = 0; j < c->height; j++) {
579 memcpy(out, src, c->width * 3);
581 out += c->pic.linesize[0];
584 #endif //ZMBV_ENABLE_24BPP
586 for (j = 0; j < c->height; j++) {
587 for (i = 0; i < c->width; i++) {
588 uint32_t tmp = AV_RL32(src);
590 AV_WB24(out+(i*3), tmp);
592 out += c->pic.linesize[0];
596 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
598 FFSWAP(uint8_t *, c->cur, c->prev);
600 *data_size = sizeof(AVFrame);
601 *(AVFrame*)data = c->pic;
603 /* always report that the buffer was completely consumed */
614 static av_cold int decode_init(AVCodecContext *avctx)
616 ZmbvContext * const c = avctx->priv_data;
617 int zret; // Zlib return code
621 c->width = avctx->width;
622 c->height = avctx->height;
624 c->bpp = avctx->bits_per_coded_sample;
626 // Needed if zlib unused or init aborted before inflateInit
627 memset(&c->zstream, 0, sizeof(z_stream));
629 avctx->pix_fmt = PIX_FMT_RGB24;
630 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
632 /* Allocate decompression buffer */
633 if (c->decomp_size) {
634 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
635 av_log(avctx, AV_LOG_ERROR,
636 "Can't allocate decompression buffer.\n");
637 return AVERROR(ENOMEM);
641 c->zstream.zalloc = Z_NULL;
642 c->zstream.zfree = Z_NULL;
643 c->zstream.opaque = Z_NULL;
644 zret = inflateInit(&c->zstream);
646 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
657 * Uninit zmbv decoder
660 static av_cold int decode_end(AVCodecContext *avctx)
662 ZmbvContext * const c = avctx->priv_data;
664 av_freep(&c->decomp_buf);
667 avctx->release_buffer(avctx, &c->pic);
668 inflateEnd(&c->zstream);
675 AVCodec ff_zmbv_decoder = {
677 .type = AVMEDIA_TYPE_VIDEO,
679 .priv_data_size = sizeof(ZmbvContext),
682 .decode = decode_frame,
683 .capabilities = CODEC_CAP_DR1,
684 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),