zmbv: Employ more meaningful return values.
[libav.git] / libavcodec / zmbv.c
1 /*
2 * Zip Motion Blocks Video (ZMBV) decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * Zip Motion Blocks Video decoder
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "libavutil/intreadwrite.h"
31 #include "avcodec.h"
32
33 #include <zlib.h>
34
35 #define ZMBV_KEYFRAME 1
36 #define ZMBV_DELTAPAL 2
37
38 enum ZmbvFormat {
39 ZMBV_FMT_NONE = 0,
40 ZMBV_FMT_1BPP = 1,
41 ZMBV_FMT_2BPP = 2,
42 ZMBV_FMT_4BPP = 3,
43 ZMBV_FMT_8BPP = 4,
44 ZMBV_FMT_15BPP = 5,
45 ZMBV_FMT_16BPP = 6,
46 ZMBV_FMT_24BPP = 7,
47 ZMBV_FMT_32BPP = 8
48 };
49
50 /*
51 * Decoder context
52 */
53 typedef struct ZmbvContext {
54 AVCodecContext *avctx;
55 AVFrame pic;
56
57 int bpp;
58 unsigned int decomp_size;
59 uint8_t* decomp_buf;
60 uint8_t pal[768];
61 uint8_t *prev, *cur;
62 int width, height;
63 int fmt;
64 int comp;
65 int flags;
66 int bw, bh, bx, by;
67 int decomp_len;
68 z_stream zstream;
69 int (*decode_intra)(struct ZmbvContext *c);
70 int (*decode_xor)(struct ZmbvContext *c);
71 } ZmbvContext;
72
73 /**
74 * Decode XOR'ed frame - 8bpp version
75 */
76
77 static int zmbv_decode_xor_8(ZmbvContext *c)
78 {
79 uint8_t *src = c->decomp_buf;
80 uint8_t *output, *prev;
81 int8_t *mvec;
82 int x, y;
83 int d, dx, dy, bw2, bh2;
84 int block;
85 int i, j;
86 int mx, my;
87
88 output = c->cur;
89 prev = c->prev;
90
91 if (c->flags & ZMBV_DELTAPAL) {
92 for (i = 0; i < 768; i++)
93 c->pal[i] ^= *src++;
94 }
95
96 mvec = (int8_t*)src;
97 src += ((c->bx * c->by * 2 + 3) & ~3);
98
99 block = 0;
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;
104
105 d = mvec[block] & 1;
106 dx = mvec[block] >> 1;
107 dy = mvec[block + 1] >> 1;
108 block += 2;
109
110 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
111
112 /* copy block - motion vectors out of bounds are used to zero blocks */
113 out = output + x;
114 tprev = prev + x + dx + dy * c->width;
115 mx = x + dx;
116 my = y + dy;
117 for (j = 0; j < bh2; j++) {
118 if (my + j < 0 || my + j >= c->height) {
119 memset(out, 0, bw2);
120 } else {
121 for (i = 0; i < bw2; i++) {
122 if (mx + i < 0 || mx + i >= c->width)
123 out[i] = 0;
124 else
125 out[i] = tprev[i];
126 }
127 }
128 out += c->width;
129 tprev += c->width;
130 }
131
132 if (d) { /* apply XOR'ed difference */
133 out = output + x;
134 for (j = 0; j < bh2; j++) {
135 for (i = 0; i < bw2; i++)
136 out[i] ^= *src++;
137 out += c->width;
138 }
139 }
140 }
141 output += c->width * c->bh;
142 prev += c->width * c->bh;
143 }
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);
147 return 0;
148 }
149
150 /**
151 * Decode XOR'ed frame - 15bpp and 16bpp version
152 */
153
154 static int zmbv_decode_xor_16(ZmbvContext *c)
155 {
156 uint8_t *src = c->decomp_buf;
157 uint16_t *output, *prev;
158 int8_t *mvec;
159 int x, y;
160 int d, dx, dy, bw2, bh2;
161 int block;
162 int i, j;
163 int mx, my;
164
165 output = (uint16_t*)c->cur;
166 prev = (uint16_t*)c->prev;
167
168 mvec = (int8_t*)src;
169 src += ((c->bx * c->by * 2 + 3) & ~3);
170
171 block = 0;
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;
176
177 d = mvec[block] & 1;
178 dx = mvec[block] >> 1;
179 dy = mvec[block + 1] >> 1;
180 block += 2;
181
182 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
183
184 /* copy block - motion vectors out of bounds are used to zero blocks */
185 out = output + x;
186 tprev = prev + x + dx + dy * c->width;
187 mx = x + dx;
188 my = y + dy;
189 for (j = 0; j < bh2; j++) {
190 if (my + j < 0 || my + j >= c->height) {
191 memset(out, 0, bw2 * 2);
192 } else {
193 for (i = 0; i < bw2; i++) {
194 if (mx + i < 0 || mx + i >= c->width)
195 out[i] = 0;
196 else
197 out[i] = tprev[i];
198 }
199 }
200 out += c->width;
201 tprev += c->width;
202 }
203
204 if (d) { /* apply XOR'ed difference */
205 out = output + x;
206 for (j = 0; j < bh2; j++){
207 for (i = 0; i < bw2; i++) {
208 out[i] ^= *((uint16_t*)src);
209 src += 2;
210 }
211 out += c->width;
212 }
213 }
214 }
215 output += c->width * c->bh;
216 prev += c->width * c->bh;
217 }
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);
221 return 0;
222 }
223
224 #ifdef ZMBV_ENABLE_24BPP
225 /**
226 * Decode XOR'ed frame - 24bpp version
227 */
228
229 static int zmbv_decode_xor_24(ZmbvContext *c)
230 {
231 uint8_t *src = c->decomp_buf;
232 uint8_t *output, *prev;
233 int8_t *mvec;
234 int x, y;
235 int d, dx, dy, bw2, bh2;
236 int block;
237 int i, j;
238 int mx, my;
239 int stride;
240
241 output = c->cur;
242 prev = c->prev;
243
244 stride = c->width * 3;
245 mvec = (int8_t*)src;
246 src += ((c->bx * c->by * 2 + 3) & ~3);
247
248 block = 0;
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;
253
254 d = mvec[block] & 1;
255 dx = mvec[block] >> 1;
256 dy = mvec[block + 1] >> 1;
257 block += 2;
258
259 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
260
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;
264 mx = x + dx;
265 my = y + dy;
266 for (j = 0; j < bh2; j++) {
267 if (my + j < 0 || my + j >= c->height) {
268 memset(out, 0, bw2 * 3);
269 } else {
270 for (i = 0; i < bw2; i++){
271 if (mx + i < 0 || mx + i >= c->width) {
272 out[i * 3 + 0] = 0;
273 out[i * 3 + 1] = 0;
274 out[i * 3 + 2] = 0;
275 } else {
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];
279 }
280 }
281 }
282 out += stride;
283 tprev += stride;
284 }
285
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++;
293 }
294 out += stride;
295 }
296 }
297 }
298 output += stride * c->bh;
299 prev += stride * c->bh;
300 }
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);
304 return 0;
305 }
306 #endif //ZMBV_ENABLE_24BPP
307
308 /**
309 * Decode XOR'ed frame - 32bpp version
310 */
311
312 static int zmbv_decode_xor_32(ZmbvContext *c)
313 {
314 uint8_t *src = c->decomp_buf;
315 uint32_t *output, *prev;
316 int8_t *mvec;
317 int x, y;
318 int d, dx, dy, bw2, bh2;
319 int block;
320 int i, j;
321 int mx, my;
322
323 output = (uint32_t*)c->cur;
324 prev = (uint32_t*)c->prev;
325
326 mvec = (int8_t*)src;
327 src += ((c->bx * c->by * 2 + 3) & ~3);
328
329 block = 0;
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;
334
335 d = mvec[block] & 1;
336 dx = mvec[block] >> 1;
337 dy = mvec[block + 1] >> 1;
338 block += 2;
339
340 bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
341
342 /* copy block - motion vectors out of bounds are used to zero blocks */
343 out = output + x;
344 tprev = prev + x + dx + dy * c->width;
345 mx = x + dx;
346 my = y + dy;
347 for (j = 0; j < bh2; j++) {
348 if (my + j < 0 || my + j >= c->height) {
349 memset(out, 0, bw2 * 4);
350 } else {
351 for (i = 0; i < bw2; i++){
352 if (mx + i < 0 || mx + i >= c->width)
353 out[i] = 0;
354 else
355 out[i] = tprev[i];
356 }
357 }
358 out += c->width;
359 tprev += c->width;
360 }
361
362 if (d) { /* apply XOR'ed difference */
363 out = output + x;
364 for (j = 0; j < bh2; j++){
365 for (i = 0; i < bw2; i++) {
366 out[i] ^= *((uint32_t *) src);
367 src += 4;
368 }
369 out += c->width;
370 }
371 }
372 }
373 output += c->width * c->bh;
374 prev += c->width * c->bh;
375 }
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);
379 return 0;
380 }
381
382 /**
383 * Decode intraframe
384 */
385 static int zmbv_decode_intra(ZmbvContext *c)
386 {
387 uint8_t *src = c->decomp_buf;
388
389 /* make the palette available on the way out */
390 if (c->fmt == ZMBV_FMT_8BPP) {
391 memcpy(c->pal, src, 768);
392 src += 768;
393 }
394
395 memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
396 return 0;
397 }
398
399 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
400 {
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
405 int len = buf_size;
406 int hi_ver, lo_ver, ret;
407 uint8_t *tmp;
408
409 if (c->pic.data[0])
410 avctx->release_buffer(avctx, &c->pic);
411
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");
416 return ret;
417 }
418
419 /* parse header */
420 c->flags = buf[0];
421 buf++; len--;
422 if (c->flags & ZMBV_KEYFRAME) {
423 hi_ver = buf[0];
424 lo_ver = buf[1];
425 c->comp = buf[2];
426 c->fmt = buf[3];
427 c->bw = buf[4];
428 c->bh = buf[5];
429
430 buf += 6;
431 len -= 6;
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",
437 hi_ver, lo_ver);
438 return AVERROR_PATCHWELCOME;
439 }
440 if (c->bw == 0 || c->bh == 0) {
441 av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
442 c->bw, c->bh);
443 return AVERROR_PATCHWELCOME;
444 }
445 if (c->comp != 0 && c->comp != 1) {
446 av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
447 c->comp);
448 return AVERROR_PATCHWELCOME;
449 }
450
451 switch (c->fmt) {
452 case ZMBV_FMT_8BPP:
453 c->bpp = 8;
454 c->decode_intra = zmbv_decode_intra;
455 c->decode_xor = zmbv_decode_xor_8;
456 break;
457 case ZMBV_FMT_15BPP:
458 case ZMBV_FMT_16BPP:
459 c->bpp = 16;
460 c->decode_intra = zmbv_decode_intra;
461 c->decode_xor = zmbv_decode_xor_16;
462 break;
463 #ifdef ZMBV_ENABLE_24BPP
464 case ZMBV_FMT_24BPP:
465 c->bpp = 24;
466 c->decode_intra = zmbv_decode_intra;
467 c->decode_xor = zmbv_decode_xor_24;
468 break;
469 #endif //ZMBV_ENABLE_24BPP
470 case ZMBV_FMT_32BPP:
471 c->bpp = 32;
472 c->decode_intra = zmbv_decode_intra;
473 c->decode_xor = zmbv_decode_xor_32;
474 break;
475 default:
476 c->decode_intra = NULL;
477 c->decode_xor = NULL;
478 av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
479 c->fmt);
480 return AVERROR_PATCHWELCOME;
481 }
482
483 zret = inflateReset(&c->zstream);
484 if (zret != Z_OK) {
485 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
486 return -1;
487 }
488
489 tmp = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
490 if (!tmp)
491 return AVERROR(ENOMEM);
492 c->cur = tmp;
493 tmp = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
494 if (!tmp)
495 return AVERROR(ENOMEM);
496 c->prev = tmp;
497 c->bx = (c->width + c->bw - 1) / c->bw;
498 c->by = (c->height+ c->bh - 1) / c->bh;
499 }
500
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;
504 }
505
506 if (c->comp == 0) { //Uncompressed data
507 memcpy(c->decomp_buf, buf, len);
508 c->decomp_size = 1;
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 inflate(&c->zstream, Z_FINISH);
516 c->decomp_len = c->zstream.total_out;
517 }
518 if (c->flags & ZMBV_KEYFRAME) {
519 c->pic.key_frame = 1;
520 c->pic.pict_type = AV_PICTURE_TYPE_I;
521 c->decode_intra(c);
522 } else {
523 c->pic.key_frame = 0;
524 c->pic.pict_type = AV_PICTURE_TYPE_P;
525 if (c->decomp_len)
526 c->decode_xor(c);
527 }
528
529 /* update frames */
530 {
531 uint8_t *out, *src;
532 int i, j;
533
534 out = c->pic.data[0];
535 src = c->cur;
536 switch (c->fmt) {
537 case ZMBV_FMT_8BPP:
538 for (j = 0; j < c->height; j++) {
539 for (i = 0; i < c->width; i++) {
540 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
541 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
542 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
543 src++;
544 }
545 out += c->pic.linesize[0];
546 }
547 break;
548 case ZMBV_FMT_15BPP:
549 for (j = 0; j < c->height; j++) {
550 for (i = 0; i < c->width; i++) {
551 uint16_t tmp = AV_RL16(src);
552 src += 2;
553 out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
554 out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
555 out[i * 3 + 2] = (tmp & 0x001F) << 3;
556 }
557 out += c->pic.linesize[0];
558 }
559 break;
560 case ZMBV_FMT_16BPP:
561 for (j = 0; j < c->height; j++) {
562 for (i = 0; i < c->width; i++) {
563 uint16_t tmp = AV_RL16(src);
564 src += 2;
565 out[i * 3 + 0] = (tmp & 0xF800) >> 8;
566 out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
567 out[i * 3 + 2] = (tmp & 0x001F) << 3;
568 }
569 out += c->pic.linesize[0];
570 }
571 break;
572 #ifdef ZMBV_ENABLE_24BPP
573 case ZMBV_FMT_24BPP:
574 for (j = 0; j < c->height; j++) {
575 memcpy(out, src, c->width * 3);
576 src += c->width * 3;
577 out += c->pic.linesize[0];
578 }
579 break;
580 #endif //ZMBV_ENABLE_24BPP
581 case ZMBV_FMT_32BPP:
582 for (j = 0; j < c->height; j++) {
583 for (i = 0; i < c->width; i++) {
584 uint32_t tmp = AV_RL32(src);
585 src += 4;
586 AV_WB24(out+(i*3), tmp);
587 }
588 out += c->pic.linesize[0];
589 }
590 break;
591 default:
592 av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
593 }
594 FFSWAP(uint8_t *, c->cur, c->prev);
595 }
596 *data_size = sizeof(AVFrame);
597 *(AVFrame*)data = c->pic;
598
599 /* always report that the buffer was completely consumed */
600 return buf_size;
601 }
602
603
604
605 /*
606 *
607 * Init zmbv decoder
608 *
609 */
610 static av_cold int decode_init(AVCodecContext *avctx)
611 {
612 ZmbvContext * const c = avctx->priv_data;
613 int zret; // Zlib return code
614
615 c->avctx = avctx;
616
617 c->width = avctx->width;
618 c->height = avctx->height;
619
620 c->bpp = avctx->bits_per_coded_sample;
621
622 // Needed if zlib unused or init aborted before inflateInit
623 memset(&c->zstream, 0, sizeof(z_stream));
624
625 avctx->pix_fmt = PIX_FMT_RGB24;
626 c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
627
628 /* Allocate decompression buffer */
629 if (c->decomp_size) {
630 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
631 av_log(avctx, AV_LOG_ERROR,
632 "Can't allocate decompression buffer.\n");
633 return AVERROR(ENOMEM);
634 }
635 }
636
637 c->zstream.zalloc = Z_NULL;
638 c->zstream.zfree = Z_NULL;
639 c->zstream.opaque = Z_NULL;
640 zret = inflateInit(&c->zstream);
641 if (zret != Z_OK) {
642 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
643 return -1;
644 }
645
646 return 0;
647 }
648
649
650
651 /*
652 *
653 * Uninit zmbv decoder
654 *
655 */
656 static av_cold int decode_end(AVCodecContext *avctx)
657 {
658 ZmbvContext * const c = avctx->priv_data;
659
660 av_freep(&c->decomp_buf);
661
662 if (c->pic.data[0])
663 avctx->release_buffer(avctx, &c->pic);
664 inflateEnd(&c->zstream);
665 av_freep(&c->cur);
666 av_freep(&c->prev);
667
668 return 0;
669 }
670
671 AVCodec ff_zmbv_decoder = {
672 .name = "zmbv",
673 .type = AVMEDIA_TYPE_VIDEO,
674 .id = CODEC_ID_ZMBV,
675 .priv_data_size = sizeof(ZmbvContext),
676 .init = decode_init,
677 .close = decode_end,
678 .decode = decode_frame,
679 .capabilities = CODEC_CAP_DR1,
680 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
681 };