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
25 * Zip Motion Blocks Video decoder
37 #define ZMBV_KEYFRAME 1
38 #define ZMBV_DELTAPAL 2
55 typedef struct ZmbvContext {
56 AVCodecContext *avctx;
60 unsigned int decomp_size;
73 int (*decode_intra)(struct ZmbvContext *c);
74 int (*decode_xor)(struct ZmbvContext *c);
78 * Decode XOR'ed frame - 8bpp version
81 static int zmbv_decode_xor_8(ZmbvContext *c)
83 uint8_t *src = c->decomp_buf;
84 uint8_t *output, *prev;
87 int d, dx, dy, bw2, bh2;
95 if(c->flags & ZMBV_DELTAPAL){
96 for(i = 0; i < 768; i++)
101 src += ((c->bx * c->by * 2 + 3) & ~3);
104 for(y = 0; y < c->height; y += c->bh) {
105 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
106 for(x = 0; x < c->width; x += c->bw) {
107 uint8_t *out, *tprev;
110 dx = mvec[block] >> 1;
111 dy = mvec[block + 1] >> 1;
114 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
116 /* copy block - motion vectors out of bounds are used to zero blocks */
118 tprev = prev + x + dx + dy * c->width;
121 for(j = 0; j < bh2; j++){
122 if((my + j < 0) || (my + j >= c->height)) {
125 for(i = 0; i < bw2; i++){
126 if((mx + i < 0) || (mx + i >= c->width))
136 if(d) { /* apply XOR'ed difference */
138 for(j = 0; j < bh2; j++){
139 for(i = 0; i < bw2; i++)
145 output += c->width * c->bh;
146 prev += c->width * c->bh;
148 if(src - c->decomp_buf != c->decomp_len)
149 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n", 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 %ti of %i bytes\n", src-c->decomp_buf, c->decomp_len);
226 #ifdef ZMBV_ENABLE_24BPP
228 * Decode XOR'ed frame - 24bpp version
231 static int zmbv_decode_xor_24(ZmbvContext *c)
233 uint8_t *src = c->decomp_buf;
234 uint8_t *output, *prev;
237 int d, dx, dy, bw2, bh2;
246 stride = c->width * 3;
248 src += ((c->bx * c->by * 2 + 3) & ~3);
251 for(y = 0; y < c->height; y += c->bh) {
252 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
253 for(x = 0; x < c->width; x += c->bw) {
254 uint8_t *out, *tprev;
257 dx = mvec[block] >> 1;
258 dy = mvec[block + 1] >> 1;
261 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
263 /* copy block - motion vectors out of bounds are used to zero blocks */
264 out = output + x * 3;
265 tprev = prev + (x + dx) * 3 + dy * stride;
268 for(j = 0; j < bh2; j++){
269 if((my + j < 0) || (my + j >= c->height)) {
270 memset(out, 0, bw2 * 3);
272 for(i = 0; i < bw2; i++){
273 if((mx + i < 0) || (mx + i >= c->width)) {
278 out[i * 3 + 0] = tprev[i * 3 + 0];
279 out[i * 3 + 1] = tprev[i * 3 + 1];
280 out[i * 3 + 2] = tprev[i * 3 + 2];
288 if(d) { /* apply XOR'ed difference */
289 out = output + x * 3;
290 for(j = 0; j < bh2; j++){
291 for(i = 0; i < bw2; i++) {
292 out[i * 3 + 0] ^= *src++;
293 out[i * 3 + 1] ^= *src++;
294 out[i * 3 + 2] ^= *src++;
300 output += stride * c->bh;
301 prev += stride * c->bh;
303 if(src - c->decomp_buf != c->decomp_len)
304 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", 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", 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, uint8_t *buf, int buf_size)
401 ZmbvContext * const c = avctx->priv_data;
404 int zret = Z_OK; // Zlib return code
410 avctx->release_buffer(avctx, &c->pic);
412 c->pic.reference = 1;
413 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
414 if(avctx->get_buffer(avctx, &c->pic) < 0){
415 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
419 outptr = c->pic.data[0]; // Output image pointer
424 if(c->flags & ZMBV_KEYFRAME) {
434 av_log(avctx, AV_LOG_DEBUG, "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",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(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
439 if(c->bw == 0 || c->bh == 0) {
440 av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
442 if(c->comp != 0 && c->comp != 1) {
443 av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
450 c->decode_intra = zmbv_decode_intra;
451 c->decode_xor = zmbv_decode_xor_8;
456 c->decode_intra = zmbv_decode_intra;
457 c->decode_xor = zmbv_decode_xor_16;
459 #ifdef ZMBV_ENABLE_24BPP
462 c->decode_intra = zmbv_decode_intra;
463 c->decode_xor = zmbv_decode_xor_24;
465 #endif //ZMBV_ENABLE_24BPP
468 c->decode_intra = zmbv_decode_intra;
469 c->decode_xor = zmbv_decode_xor_32;
472 c->decode_intra = NULL;
473 c->decode_xor = NULL;
474 av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
478 zret = inflateReset(&c->zstream);
480 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
484 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
486 #endif /* CONFIG_ZLIB */
487 c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
488 c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
489 c->bx = (c->width + c->bw - 1) / c->bw;
490 c->by = (c->height+ c->bh - 1) / c->bh;
493 if(c->decode_intra == NULL) {
494 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
498 if(c->comp == 0) { //Uncompressed data
499 memcpy(c->decomp_buf, buf, len);
501 } else { // ZLIB-compressed data
503 c->zstream.total_in = c->zstream.total_out = 0;
504 c->zstream.next_in = buf;
505 c->zstream.avail_in = len;
506 c->zstream.next_out = c->decomp_buf;
507 c->zstream.avail_out = c->decomp_size;
508 inflate(&c->zstream, Z_FINISH);
509 c->decomp_len = c->zstream.total_out;
511 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
515 if(c->flags & ZMBV_KEYFRAME) {
516 c->pic.key_frame = 1;
517 c->pic.pict_type = FF_I_TYPE;
520 c->pic.key_frame = 0;
521 c->pic.pict_type = FF_P_TYPE;
530 out = c->pic.data[0];
534 for(j = 0; j < c->height; j++) {
535 for(i = 0; i < c->width; i++) {
536 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
537 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
538 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
541 out += c->pic.linesize[0];
545 for(j = 0; j < c->height; j++) {
546 for(i = 0; i < c->width; i++) {
547 uint16_t tmp = AV_RL16(src);
549 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
550 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
551 out[i * 3 + 2] = (tmp & 0x001F) << 3;
553 out += c->pic.linesize[0];
557 for(j = 0; j < c->height; j++) {
558 for(i = 0; i < c->width; i++) {
559 uint16_t tmp = AV_RL16(src);
561 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
562 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
563 out[i * 3 + 2] = (tmp & 0x001F) << 3;
565 out += c->pic.linesize[0];
568 #ifdef ZMBV_ENABLE_24BPP
570 for(j = 0; j < c->height; j++) {
571 memcpy(out, src, c->width * 3);
573 out += c->pic.linesize[0];
576 #endif //ZMBV_ENABLE_24BPP
578 for(j = 0; j < c->height; j++) {
579 for(i = 0; i < c->width; i++) {
580 uint32_t tmp = AV_RL32(src);
582 AV_WB24(out+(i*3), tmp);
584 out += c->pic.linesize[0];
588 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
590 memcpy(c->prev, c->cur, c->width * c->height * (c->bpp / 8));
592 *data_size = sizeof(AVFrame);
593 *(AVFrame*)data = c->pic;
595 /* always report that the buffer was completely consumed */
606 static int decode_init(AVCodecContext *avctx)
608 ZmbvContext * const c = avctx->priv_data;
609 int zret; // Zlib return code
613 c->pic.data[0] = NULL;
614 c->width = avctx->width;
615 c->height = avctx->height;
617 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
620 c->bpp = avctx->bits_per_sample;
623 // Needed if zlib unused or init aborted before inflateInit
624 memset(&(c->zstream), 0, sizeof(z_stream));
626 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
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, "Can't allocate decompression buffer.\n");
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);
658 * Uninit zmbv decoder
661 static int decode_end(AVCodecContext *avctx)
663 ZmbvContext * const c = avctx->priv_data;
665 av_freep(&c->decomp_buf);
668 avctx->release_buffer(avctx, &c->pic);
670 inflateEnd(&(c->zstream));
678 AVCodec zmbv_decoder = {