cosmetics: change "get" to "decode"
[ffmpeg-lucabe.git] / libavcodec / zmbv.c
blobb9751a0faaf2714460c8b5835a1c3a54ba0c43bb
1 /*
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
22 /**
23 * @file zmbv.c
24 * Zip Motion Blocks Video decoder
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "avcodec.h"
32 #include <zlib.h>
34 #define ZMBV_KEYFRAME 1
35 #define ZMBV_DELTAPAL 2
37 enum ZmbvFormat {
38 ZMBV_FMT_NONE = 0,
39 ZMBV_FMT_1BPP = 1,
40 ZMBV_FMT_2BPP = 2,
41 ZMBV_FMT_4BPP = 3,
42 ZMBV_FMT_8BPP = 4,
43 ZMBV_FMT_15BPP = 5,
44 ZMBV_FMT_16BPP = 6,
45 ZMBV_FMT_24BPP = 7,
46 ZMBV_FMT_32BPP = 8
50 * Decoder context
52 typedef struct ZmbvContext {
53 AVCodecContext *avctx;
54 AVFrame pic;
56 int bpp;
57 unsigned int decomp_size;
58 uint8_t* decomp_buf;
59 uint8_t pal[768];
60 uint8_t *prev, *cur;
61 int width, height;
62 int fmt;
63 int comp;
64 int flags;
65 int bw, bh, bx, by;
66 int decomp_len;
67 z_stream zstream;
68 int (*decode_intra)(struct ZmbvContext *c);
69 int (*decode_xor)(struct ZmbvContext *c);
70 } ZmbvContext;
72 /**
73 * Decode XOR'ed frame - 8bpp version
76 static int zmbv_decode_xor_8(ZmbvContext *c)
78 uint8_t *src = c->decomp_buf;
79 uint8_t *output, *prev;
80 int8_t *mvec;
81 int x, y;
82 int d, dx, dy, bw2, bh2;
83 int block;
84 int i, j;
85 int mx, my;
87 output = c->cur;
88 prev = c->prev;
90 if(c->flags & ZMBV_DELTAPAL){
91 for(i = 0; i < 768; i++)
92 c->pal[i] ^= *src++;
95 mvec = (int8_t*)src;
96 src += ((c->bx * c->by * 2 + 3) & ~3);
98 block = 0;
99 for(y = 0; y < c->height; y += c->bh) {
100 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
101 for(x = 0; x < c->width; x += c->bw) {
102 uint8_t *out, *tprev;
104 d = mvec[block] & 1;
105 dx = mvec[block] >> 1;
106 dy = mvec[block + 1] >> 1;
107 block += 2;
109 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
111 /* copy block - motion vectors out of bounds are used to zero blocks */
112 out = output + x;
113 tprev = prev + x + dx + dy * c->width;
114 mx = x + dx;
115 my = y + dy;
116 for(j = 0; j < bh2; j++){
117 if((my + j < 0) || (my + j >= c->height)) {
118 memset(out, 0, bw2);
119 } else {
120 for(i = 0; i < bw2; i++){
121 if((mx + i < 0) || (mx + i >= c->width))
122 out[i] = 0;
123 else
124 out[i] = tprev[i];
127 out += c->width;
128 tprev += c->width;
131 if(d) { /* apply XOR'ed difference */
132 out = output + x;
133 for(j = 0; j < bh2; j++){
134 for(i = 0; i < bw2; i++)
135 out[i] ^= *src++;
136 out += c->width;
140 output += c->width * c->bh;
141 prev += c->width * c->bh;
143 if(src - c->decomp_buf != c->decomp_len)
144 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n", src-c->decomp_buf, c->decomp_len);
145 return 0;
149 * Decode XOR'ed frame - 15bpp and 16bpp version
152 static int zmbv_decode_xor_16(ZmbvContext *c)
154 uint8_t *src = c->decomp_buf;
155 uint16_t *output, *prev;
156 int8_t *mvec;
157 int x, y;
158 int d, dx, dy, bw2, bh2;
159 int block;
160 int i, j;
161 int mx, my;
163 output = (uint16_t*)c->cur;
164 prev = (uint16_t*)c->prev;
166 mvec = (int8_t*)src;
167 src += ((c->bx * c->by * 2 + 3) & ~3);
169 block = 0;
170 for(y = 0; y < c->height; y += c->bh) {
171 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
172 for(x = 0; x < c->width; x += c->bw) {
173 uint16_t *out, *tprev;
175 d = mvec[block] & 1;
176 dx = mvec[block] >> 1;
177 dy = mvec[block + 1] >> 1;
178 block += 2;
180 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
182 /* copy block - motion vectors out of bounds are used to zero blocks */
183 out = output + x;
184 tprev = prev + x + dx + dy * c->width;
185 mx = x + dx;
186 my = y + dy;
187 for(j = 0; j < bh2; j++){
188 if((my + j < 0) || (my + j >= c->height)) {
189 memset(out, 0, bw2 * 2);
190 } else {
191 for(i = 0; i < bw2; i++){
192 if((mx + i < 0) || (mx + i >= c->width))
193 out[i] = 0;
194 else
195 out[i] = tprev[i];
198 out += c->width;
199 tprev += c->width;
202 if(d) { /* apply XOR'ed difference */
203 out = output + x;
204 for(j = 0; j < bh2; j++){
205 for(i = 0; i < bw2; i++) {
206 out[i] ^= *((uint16_t*)src);
207 src += 2;
209 out += c->width;
213 output += c->width * c->bh;
214 prev += c->width * c->bh;
216 if(src - c->decomp_buf != c->decomp_len)
217 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n", src-c->decomp_buf, c->decomp_len);
218 return 0;
221 #ifdef ZMBV_ENABLE_24BPP
223 * Decode XOR'ed frame - 24bpp version
226 static int zmbv_decode_xor_24(ZmbvContext *c)
228 uint8_t *src = c->decomp_buf;
229 uint8_t *output, *prev;
230 int8_t *mvec;
231 int x, y;
232 int d, dx, dy, bw2, bh2;
233 int block;
234 int i, j;
235 int mx, my;
236 int stride;
238 output = c->cur;
239 prev = c->prev;
241 stride = c->width * 3;
242 mvec = (int8_t*)src;
243 src += ((c->bx * c->by * 2 + 3) & ~3);
245 block = 0;
246 for(y = 0; y < c->height; y += c->bh) {
247 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
248 for(x = 0; x < c->width; x += c->bw) {
249 uint8_t *out, *tprev;
251 d = mvec[block] & 1;
252 dx = mvec[block] >> 1;
253 dy = mvec[block + 1] >> 1;
254 block += 2;
256 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
258 /* copy block - motion vectors out of bounds are used to zero blocks */
259 out = output + x * 3;
260 tprev = prev + (x + dx) * 3 + dy * stride;
261 mx = x + dx;
262 my = y + dy;
263 for(j = 0; j < bh2; j++){
264 if((my + j < 0) || (my + j >= c->height)) {
265 memset(out, 0, bw2 * 3);
266 } else {
267 for(i = 0; i < bw2; i++){
268 if((mx + i < 0) || (mx + i >= c->width)) {
269 out[i * 3 + 0] = 0;
270 out[i * 3 + 1] = 0;
271 out[i * 3 + 2] = 0;
272 } else {
273 out[i * 3 + 0] = tprev[i * 3 + 0];
274 out[i * 3 + 1] = tprev[i * 3 + 1];
275 out[i * 3 + 2] = tprev[i * 3 + 2];
279 out += stride;
280 tprev += stride;
283 if(d) { /* apply XOR'ed difference */
284 out = output + x * 3;
285 for(j = 0; j < bh2; j++){
286 for(i = 0; i < bw2; i++) {
287 out[i * 3 + 0] ^= *src++;
288 out[i * 3 + 1] ^= *src++;
289 out[i * 3 + 2] ^= *src++;
291 out += stride;
295 output += stride * c->bh;
296 prev += stride * c->bh;
298 if(src - c->decomp_buf != c->decomp_len)
299 av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
300 return 0;
302 #endif //ZMBV_ENABLE_24BPP
305 * Decode XOR'ed frame - 32bpp version
308 static int zmbv_decode_xor_32(ZmbvContext *c)
310 uint8_t *src = c->decomp_buf;
311 uint32_t *output, *prev;
312 int8_t *mvec;
313 int x, y;
314 int d, dx, dy, bw2, bh2;
315 int block;
316 int i, j;
317 int mx, my;
319 output = (uint32_t*)c->cur;
320 prev = (uint32_t*)c->prev;
322 mvec = (int8_t*)src;
323 src += ((c->bx * c->by * 2 + 3) & ~3);
325 block = 0;
326 for(y = 0; y < c->height; y += c->bh) {
327 bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
328 for(x = 0; x < c->width; x += c->bw) {
329 uint32_t *out, *tprev;
331 d = mvec[block] & 1;
332 dx = mvec[block] >> 1;
333 dy = mvec[block + 1] >> 1;
334 block += 2;
336 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
338 /* copy block - motion vectors out of bounds are used to zero blocks */
339 out = output + x;
340 tprev = prev + x + dx + dy * c->width;
341 mx = x + dx;
342 my = y + dy;
343 for(j = 0; j < bh2; j++){
344 if((my + j < 0) || (my + j >= c->height)) {
345 memset(out, 0, bw2 * 4);
346 } else {
347 for(i = 0; i < bw2; i++){
348 if((mx + i < 0) || (mx + i >= c->width))
349 out[i] = 0;
350 else
351 out[i] = tprev[i];
354 out += c->width;
355 tprev += c->width;
358 if(d) { /* apply XOR'ed difference */
359 out = output + x;
360 for(j = 0; j < bh2; j++){
361 for(i = 0; i < bw2; i++) {
362 out[i] ^= *((uint32_t*)src);
363 src += 4;
365 out += c->width;
369 output += c->width * c->bh;
370 prev += c->width * c->bh;
372 if(src - c->decomp_buf != c->decomp_len)
373 av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n", src-c->decomp_buf, c->decomp_len);
374 return 0;
378 * Decode intraframe
380 static int zmbv_decode_intra(ZmbvContext *c)
382 uint8_t *src = c->decomp_buf;
384 /* make the palette available on the way out */
385 if (c->fmt == ZMBV_FMT_8BPP) {
386 memcpy(c->pal, src, 768);
387 src += 768;
390 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
391 return 0;
394 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
396 ZmbvContext * const c = avctx->priv_data;
397 uint8_t *outptr;
398 int zret = Z_OK; // Zlib return code
399 int len = buf_size;
400 int hi_ver, lo_ver;
402 if(c->pic.data[0])
403 avctx->release_buffer(avctx, &c->pic);
405 c->pic.reference = 1;
406 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
407 if(avctx->get_buffer(avctx, &c->pic) < 0){
408 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
409 return -1;
412 outptr = c->pic.data[0]; // Output image pointer
414 /* parse header */
415 c->flags = buf[0];
416 buf++; len--;
417 if(c->flags & ZMBV_KEYFRAME) {
418 hi_ver = buf[0];
419 lo_ver = buf[1];
420 c->comp = buf[2];
421 c->fmt = buf[3];
422 c->bw = buf[4];
423 c->bh = buf[5];
425 buf += 6;
426 len -= 6;
427 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);
428 if(hi_ver != 0 || lo_ver != 1) {
429 av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
430 return -1;
432 if(c->bw == 0 || c->bh == 0) {
433 av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
435 if(c->comp != 0 && c->comp != 1) {
436 av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
437 return -1;
440 switch(c->fmt) {
441 case ZMBV_FMT_8BPP:
442 c->bpp = 8;
443 c->decode_intra = zmbv_decode_intra;
444 c->decode_xor = zmbv_decode_xor_8;
445 break;
446 case ZMBV_FMT_15BPP:
447 case ZMBV_FMT_16BPP:
448 c->bpp = 16;
449 c->decode_intra = zmbv_decode_intra;
450 c->decode_xor = zmbv_decode_xor_16;
451 break;
452 #ifdef ZMBV_ENABLE_24BPP
453 case ZMBV_FMT_24BPP:
454 c->bpp = 24;
455 c->decode_intra = zmbv_decode_intra;
456 c->decode_xor = zmbv_decode_xor_24;
457 break;
458 #endif //ZMBV_ENABLE_24BPP
459 case ZMBV_FMT_32BPP:
460 c->bpp = 32;
461 c->decode_intra = zmbv_decode_intra;
462 c->decode_xor = zmbv_decode_xor_32;
463 break;
464 default:
465 c->decode_intra = NULL;
466 c->decode_xor = NULL;
467 av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
468 return -1;
471 zret = inflateReset(&c->zstream);
472 if (zret != Z_OK) {
473 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
474 return -1;
477 c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
478 c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
479 c->bx = (c->width + c->bw - 1) / c->bw;
480 c->by = (c->height+ c->bh - 1) / c->bh;
483 if(c->decode_intra == NULL) {
484 av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
485 return -1;
488 if(c->comp == 0) { //Uncompressed data
489 memcpy(c->decomp_buf, buf, len);
490 c->decomp_size = 1;
491 } else { // ZLIB-compressed data
492 c->zstream.total_in = c->zstream.total_out = 0;
493 c->zstream.next_in = buf;
494 c->zstream.avail_in = len;
495 c->zstream.next_out = c->decomp_buf;
496 c->zstream.avail_out = c->decomp_size;
497 inflate(&c->zstream, Z_FINISH);
498 c->decomp_len = c->zstream.total_out;
500 if(c->flags & ZMBV_KEYFRAME) {
501 c->pic.key_frame = 1;
502 c->pic.pict_type = FF_I_TYPE;
503 c->decode_intra(c);
504 } else {
505 c->pic.key_frame = 0;
506 c->pic.pict_type = FF_P_TYPE;
507 if(c->decomp_len)
508 c->decode_xor(c);
511 /* update frames */
513 uint8_t *out, *src;
514 int i, j;
516 out = c->pic.data[0];
517 src = c->cur;
518 switch(c->fmt) {
519 case ZMBV_FMT_8BPP:
520 for(j = 0; j < c->height; j++) {
521 for(i = 0; i < c->width; i++) {
522 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
523 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
524 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
525 src++;
527 out += c->pic.linesize[0];
529 break;
530 case ZMBV_FMT_15BPP:
531 for(j = 0; j < c->height; j++) {
532 for(i = 0; i < c->width; i++) {
533 uint16_t tmp = AV_RL16(src);
534 src += 2;
535 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
536 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
537 out[i * 3 + 2] = (tmp & 0x001F) << 3;
539 out += c->pic.linesize[0];
541 break;
542 case ZMBV_FMT_16BPP:
543 for(j = 0; j < c->height; j++) {
544 for(i = 0; i < c->width; i++) {
545 uint16_t tmp = AV_RL16(src);
546 src += 2;
547 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
548 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
549 out[i * 3 + 2] = (tmp & 0x001F) << 3;
551 out += c->pic.linesize[0];
553 break;
554 #ifdef ZMBV_ENABLE_24BPP
555 case ZMBV_FMT_24BPP:
556 for(j = 0; j < c->height; j++) {
557 memcpy(out, src, c->width * 3);
558 src += c->width * 3;
559 out += c->pic.linesize[0];
561 break;
562 #endif //ZMBV_ENABLE_24BPP
563 case ZMBV_FMT_32BPP:
564 for(j = 0; j < c->height; j++) {
565 for(i = 0; i < c->width; i++) {
566 uint32_t tmp = AV_RL32(src);
567 src += 4;
568 AV_WB24(out+(i*3), tmp);
570 out += c->pic.linesize[0];
572 break;
573 default:
574 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
576 memcpy(c->prev, c->cur, c->width * c->height * (c->bpp / 8));
578 *data_size = sizeof(AVFrame);
579 *(AVFrame*)data = c->pic;
581 /* always report that the buffer was completely consumed */
582 return buf_size;
589 * Init zmbv decoder
592 static av_cold int decode_init(AVCodecContext *avctx)
594 ZmbvContext * const c = avctx->priv_data;
595 int zret; // Zlib return code
597 c->avctx = avctx;
599 c->pic.data[0] = NULL;
600 c->width = avctx->width;
601 c->height = avctx->height;
603 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
604 return 1;
606 c->bpp = avctx->bits_per_sample;
608 // Needed if zlib unused or init aborted before inflateInit
609 memset(&(c->zstream), 0, sizeof(z_stream));
611 avctx->pix_fmt = PIX_FMT_RGB24;
612 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
614 /* Allocate decompression buffer */
615 if (c->decomp_size) {
616 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
617 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
618 return 1;
622 c->zstream.zalloc = Z_NULL;
623 c->zstream.zfree = Z_NULL;
624 c->zstream.opaque = Z_NULL;
625 zret = inflateInit(&(c->zstream));
626 if (zret != Z_OK) {
627 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
628 return 1;
631 return 0;
638 * Uninit zmbv decoder
641 static av_cold int decode_end(AVCodecContext *avctx)
643 ZmbvContext * const c = avctx->priv_data;
645 av_freep(&c->decomp_buf);
647 if (c->pic.data[0])
648 avctx->release_buffer(avctx, &c->pic);
649 inflateEnd(&(c->zstream));
650 av_freep(&c->cur);
651 av_freep(&c->prev);
653 return 0;
656 AVCodec zmbv_decoder = {
657 "zmbv",
658 CODEC_TYPE_VIDEO,
659 CODEC_ID_ZMBV,
660 sizeof(ZmbvContext),
661 decode_init,
662 NULL,
663 decode_end,
664 decode_frame,
665 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),