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