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