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