]> git.sesse.net Git - ffmpeg/blob - libavcodec/zmbv.c
Merge commit 'b4b27dce95a6d40bfcd78043d3abec7d80dae143'
[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     int expected_size;
413
414     /* parse header */
415     if (len < 1)
416         return AVERROR_INVALIDDATA;
417     c->flags = buf[0];
418     buf++; len--;
419     if (c->flags & ZMBV_KEYFRAME) {
420         void *decode_intra = NULL;
421         c->decode_intra= NULL;
422
423         if (len < 6)
424             return AVERROR_INVALIDDATA;
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         c->decode_intra = NULL;
432         c->decode_xor = NULL;
433
434         buf += 6;
435         len -= 6;
436         av_log(avctx, AV_LOG_DEBUG,
437                "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
438                c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
439         if (hi_ver != 0 || lo_ver != 1) {
440             avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
441             return AVERROR_PATCHWELCOME;
442         }
443         if (c->bw == 0 || c->bh == 0) {
444             avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
445             return AVERROR_PATCHWELCOME;
446         }
447         if (c->comp != 0 && c->comp != 1) {
448             avpriv_request_sample(avctx, "Compression type %i", c->comp);
449             return AVERROR_PATCHWELCOME;
450         }
451
452         switch (c->fmt) {
453         case ZMBV_FMT_8BPP:
454             c->bpp = 8;
455             decode_intra = zmbv_decode_intra;
456             c->decode_xor = zmbv_decode_xor_8;
457             avctx->pix_fmt = AV_PIX_FMT_PAL8;
458             c->stride = c->width;
459             break;
460         case ZMBV_FMT_15BPP:
461         case ZMBV_FMT_16BPP:
462             c->bpp = 16;
463             decode_intra = zmbv_decode_intra;
464             c->decode_xor = zmbv_decode_xor_16;
465             if (c->fmt == ZMBV_FMT_15BPP)
466                 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
467             else
468                 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
469             c->stride = c->width * 2;
470             break;
471 #ifdef ZMBV_ENABLE_24BPP
472         case ZMBV_FMT_24BPP:
473             c->bpp = 24;
474             decode_intra = zmbv_decode_intra;
475             c->decode_xor = zmbv_decode_xor_24;
476             avctx->pix_fmt = AV_PIX_FMT_RGB24;
477             c->stride = c->width * 3;
478             break;
479 #endif //ZMBV_ENABLE_24BPP
480         case ZMBV_FMT_32BPP:
481             c->bpp = 32;
482             decode_intra = zmbv_decode_intra;
483             c->decode_xor = zmbv_decode_xor_32;
484             avctx->pix_fmt = AV_PIX_FMT_BGR0;
485             c->stride = c->width * 4;
486             break;
487         default:
488             c->decode_xor = NULL;
489             avpriv_request_sample(avctx, "Format %i", c->fmt);
490             return AVERROR_PATCHWELCOME;
491         }
492
493         zret = inflateReset(&c->zstream);
494         if (zret != Z_OK) {
495             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
496             return AVERROR_UNKNOWN;
497         }
498
499         if (c->alloc_bpp < c->bpp) {
500             c->cur  = av_realloc_f(c->cur, avctx->width * avctx->height,  (c->bpp / 8));
501             c->prev = av_realloc_f(c->prev, avctx->width * avctx->height,  (c->bpp / 8));
502             c->alloc_bpp = c->bpp;
503         }
504         c->bx = (c->width + c->bw - 1) / c->bw;
505         c->by = (c->height+ c->bh - 1) / c->bh;
506         if (!c->cur || !c->prev) {
507             c->alloc_bpp = 0;
508             return AVERROR(ENOMEM);
509         }
510         memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
511         memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
512         c->decode_intra= decode_intra;
513     }
514     if (c->flags & ZMBV_KEYFRAME) {
515         expected_size = avctx->width * avctx->height * (c->bpp / 8);
516     } else {
517         expected_size = (c->bx * c->by * 2 + 3) & ~3;
518     }
519     if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
520         (c->flags & (ZMBV_DELTAPAL | ZMBV_KEYFRAME)))
521         expected_size += 768;
522
523     if (!c->decode_intra) {
524         av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
525         return AVERROR_INVALIDDATA;
526     }
527
528     if (c->comp == 0) { // uncompressed data
529         if (c->decomp_size < len) {
530             av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
531             return AVERROR_INVALIDDATA;
532         }
533         memcpy(c->decomp_buf, buf, len);
534         c->decomp_len = len;
535     } else { // ZLIB-compressed data
536         c->zstream.total_in = c->zstream.total_out = 0;
537         c->zstream.next_in = (uint8_t*)buf;
538         c->zstream.avail_in = len;
539         c->zstream.next_out = c->decomp_buf;
540         c->zstream.avail_out = c->decomp_size;
541         zret = inflate(&c->zstream, Z_SYNC_FLUSH);
542         if (zret != Z_OK && zret != Z_STREAM_END) {
543             av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
544             return AVERROR_INVALIDDATA;
545         }
546         c->decomp_len = c->zstream.total_out;
547     }
548     if (expected_size > c->decomp_len ||
549         (c->flags & ZMBV_KEYFRAME) && expected_size < c->decomp_len) {
550         av_log(avctx, AV_LOG_ERROR, "decompressed size %d is incorrect, expected %d\n", c->decomp_len, expected_size);
551         return AVERROR_INVALIDDATA;
552     }
553     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
554         return ret;
555
556     if (c->flags & ZMBV_KEYFRAME) {
557         frame->key_frame = 1;
558         frame->pict_type = AV_PICTURE_TYPE_I;
559         c->decode_intra(c);
560     } else {
561         frame->key_frame = 0;
562         frame->pict_type = AV_PICTURE_TYPE_P;
563         if (c->decomp_len < 2LL * ((c->width + c->bw - 1) / c->bw) * ((c->height + c->bh - 1) / c->bh))
564             return AVERROR_INVALIDDATA;
565         if (c->decomp_len)
566             c->decode_xor(c);
567     }
568
569     /* update frames */
570     {
571         uint8_t *out, *src;
572         int j;
573
574         out = frame->data[0];
575         src = c->cur;
576         switch (c->fmt) {
577         case ZMBV_FMT_8BPP:
578             for (j = 0; j < 256; j++)
579                 AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
580         case ZMBV_FMT_15BPP:
581         case ZMBV_FMT_16BPP:
582 #ifdef ZMBV_ENABLE_24BPP
583         case ZMBV_FMT_24BPP:
584 #endif
585         case ZMBV_FMT_32BPP:
586             av_image_copy_plane(out, frame->linesize[0], src, c->stride,
587                                 c->stride, c->height);
588             break;
589         default:
590             av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
591         }
592         FFSWAP(uint8_t *, c->cur, c->prev);
593     }
594     *got_frame = 1;
595
596     /* always report that the buffer was completely consumed */
597     return buf_size;
598 }
599
600 static av_cold int decode_init(AVCodecContext *avctx)
601 {
602     ZmbvContext * const c = avctx->priv_data;
603     int zret; // Zlib return code
604
605     c->avctx = avctx;
606
607     c->width = avctx->width;
608     c->height = avctx->height;
609
610     c->bpp = avctx->bits_per_coded_sample;
611
612     // Needed if zlib unused or init aborted before inflateInit
613     memset(&c->zstream, 0, sizeof(z_stream));
614
615     if ((avctx->width + 255ULL) * (avctx->height + 64ULL) > FFMIN(avctx->max_pixels, INT_MAX / 4) ) {
616         av_log(avctx, AV_LOG_ERROR, "Internal buffer (decomp_size) larger than max_pixels or too large\n");
617         return AVERROR_INVALIDDATA;
618     }
619
620     c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
621
622     /* Allocate decompression buffer */
623     c->decomp_buf = av_mallocz(c->decomp_size);
624     if (!c->decomp_buf) {
625         av_log(avctx, AV_LOG_ERROR,
626                 "Can't allocate decompression buffer.\n");
627         return AVERROR(ENOMEM);
628     }
629
630     c->zstream.zalloc = Z_NULL;
631     c->zstream.zfree = Z_NULL;
632     c->zstream.opaque = Z_NULL;
633     zret = inflateInit(&c->zstream);
634     if (zret != Z_OK) {
635         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
636         return AVERROR_UNKNOWN;
637     }
638
639     return 0;
640 }
641
642 static av_cold int decode_end(AVCodecContext *avctx)
643 {
644     ZmbvContext * const c = avctx->priv_data;
645
646     av_freep(&c->decomp_buf);
647
648     inflateEnd(&c->zstream);
649     av_freep(&c->cur);
650     av_freep(&c->prev);
651
652     return 0;
653 }
654
655 AVCodec ff_zmbv_decoder = {
656     .name           = "zmbv",
657     .long_name      = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
658     .type           = AVMEDIA_TYPE_VIDEO,
659     .id             = AV_CODEC_ID_ZMBV,
660     .priv_data_size = sizeof(ZmbvContext),
661     .init           = decode_init,
662     .close          = decode_end,
663     .decode         = decode_frame,
664     .capabilities   = AV_CODEC_CAP_DR1,
665     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
666 };