]> git.sesse.net Git - ffmpeg/blob - libavcodec/zmbv.c
fix some crashes when missing frames
[ffmpeg] / libavcodec / zmbv.c
1 /*
2  * Zip Motion Blocks Video (ZMBV) decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20
21 /**
22  * @file zmbv.c
23  * Zip Motion Blocks Video decoder
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "common.h"
30 #include "avcodec.h"
31
32 #ifdef CONFIG_ZLIB
33 #include <zlib.h>
34 #endif
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 #ifdef CONFIG_ZLIB
70     z_stream zstream;
71 #endif
72     int (*decode_intra)(struct ZmbvContext *c);
73     int (*decode_xor)(struct ZmbvContext *c);
74 } ZmbvContext;
75
76 /**
77  * Decode XOR'ed frame - 8bpp version
78  */
79
80 static int zmbv_decode_xor_8(ZmbvContext *c)
81 {
82     uint8_t *src = c->decomp_buf;
83     uint8_t *output, *prev;
84     int8_t *mvec;
85     int x, y;
86     int d, dx, dy, bw2, bh2;
87     int block;
88     int i, j;
89     int mx, my;
90
91     output = c->cur;
92     prev = c->prev;
93
94     if(c->flags & ZMBV_DELTAPAL){
95         for(i = 0; i < 768; i++)
96             c->pal[i] ^= *src++;
97     }
98
99     mvec = (int8_t*)src;
100     src += ((c->bx * c->by * 2 + 3) & ~3);
101
102     block = 0;
103     for(y = 0; y < c->height; y += c->bh) {
104         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
105         for(x = 0; x < c->width; x += c->bw) {
106             uint8_t *out, *tprev;
107
108             d = mvec[block] & 1;
109             dx = mvec[block] >> 1;
110             dy = mvec[block + 1] >> 1;
111             block += 2;
112
113             bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
114
115             /* copy block - motion vectors out of bounds are used to zero blocks */
116             out = output + x;
117             tprev = prev + x + dx + dy * c->width;
118             mx = x + dx;
119             my = y + dy;
120             for(j = 0; j < bh2; j++){
121                 if((my + j < 0) || (my + j >= c->height)) {
122                     memset(out, 0, bw2);
123                 } else {
124                     for(i = 0; i < bw2; i++){
125                         if((mx + i < 0) || (mx + i >= c->width))
126                             out[i] = 0;
127                         else
128                             out[i] = tprev[i];
129                     }
130                 }
131                 out += c->width;
132                 tprev += c->width;
133             }
134
135             if(d) { /* apply XOR'ed difference */
136                 out = output + x;
137                 for(j = 0; j < bh2; j++){
138                     for(i = 0; i < bw2; i++)
139                         out[i] ^= *src++;
140                     out += c->width;
141                 }
142             }
143         }
144         output += c->width * c->bh;
145         prev += c->width * c->bh;
146     }
147     if(src - c->decomp_buf != c->decomp_len)
148         av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
149     return 0;
150 }
151
152 /**
153  * Decode XOR'ed frame - 24bpp version
154  */
155
156 static int zmbv_decode_xor_24(ZmbvContext *c)
157 {
158     uint8_t *src = c->decomp_buf;
159     uint8_t *output, *prev;
160     int8_t *mvec;
161     int x, y;
162     int d, dx, dy, bw2, bh2;
163     int block;
164     int i, j;
165     int mx, my;
166     int stride;
167
168     output = c->cur;
169     prev = c->prev;
170
171     stride = c->width * 3;
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             uint8_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 * 3;
190             tprev = prev + (x + dx) * 3 + dy * stride;
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 * 3);
196                 } else {
197                     for(i = 0; i < bw2; i++){
198                         if((mx + i < 0) || (mx + i >= c->width)) {
199                             out[i * 3 + 0] = 0;
200                             out[i * 3 + 1] = 0;
201                             out[i * 3 + 2] = 0;
202                         } else {
203                             out[i * 3 + 0] = tprev[i * 3 + 0];
204                             out[i * 3 + 1] = tprev[i * 3 + 1];
205                             out[i * 3 + 2] = tprev[i * 3 + 2];
206                         }
207                     }
208                 }
209                 out += stride;
210                 tprev += stride;
211             }
212
213             if(d) { /* apply XOR'ed difference */
214                 out = output + x * 3;
215                 for(j = 0; j < bh2; j++){
216                     for(i = 0; i < bw2; i++) {
217                         out[i * 3 + 0] ^= *src++;
218                         out[i * 3 + 1] ^= *src++;
219                         out[i * 3 + 2] ^= *src++;
220                     }
221                     out += stride;
222                 }
223             }
224         }
225         output += stride * c->bh;
226         prev += stride * c->bh;
227     }
228     if(src - c->decomp_buf != c->decomp_len)
229         av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
230     return 0;
231 }
232
233 /**
234  * Decode intraframe
235  */
236 static int zmbv_decode_intra(ZmbvContext *c)
237 {
238     uint8_t *src = c->decomp_buf;
239
240     /* make the palette available on the way out */
241     if (c->fmt == ZMBV_FMT_8BPP) {
242         memcpy(c->pal, src, 768);
243         src += 768;
244     }
245
246     memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
247     return 0;
248 }
249
250 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
251 {
252     ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
253     uint8_t *outptr;
254 #ifdef CONFIG_ZLIB
255     int zret = Z_OK; // Zlib return code
256 #endif
257     int len = buf_size;
258     int hi_ver, lo_ver;
259
260     if(c->pic.data[0])
261             avctx->release_buffer(avctx, &c->pic);
262
263     c->pic.reference = 1;
264     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
265     if(avctx->get_buffer(avctx, &c->pic) < 0){
266         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
267         return -1;
268     }
269
270     outptr = c->pic.data[0]; // Output image pointer
271
272     /* parse header */
273     c->flags = buf[0];
274     buf++; len--;
275     if(c->flags & ZMBV_KEYFRAME) {
276         hi_ver = buf[0];
277         lo_ver = buf[1];
278         c->comp = buf[2];
279         c->fmt = buf[3];
280         c->bw = buf[4];
281         c->bh = buf[5];
282
283         buf += 6;
284         len -= 6;
285         av_log(avctx, AV_LOG_DEBUG, "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
286         if(hi_ver != 0 || lo_ver != 1) {
287             av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
288             return -1;
289         }
290         if(c->bw == 0 || c->bh == 0) {
291             av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
292         }
293         if(c->comp != 0 && c->comp != 1) {
294             av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
295             return -1;
296         }
297         if(c->fmt != ZMBV_FMT_8BPP && c->fmt != ZMBV_FMT_24BPP) {
298             av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
299             return -1;
300         }
301 #ifdef CONFIG_ZLIB
302         zret = inflateReset(&c->zstream);
303         if (zret != Z_OK) {
304             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
305             return -1;
306         }
307 #else
308         av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
309         return -1;
310 #endif  /* CONFIG_ZLIB */
311         if(c->fmt == ZMBV_FMT_8BPP) {
312             c->bpp = 8;
313             c->decode_intra = zmbv_decode_intra;
314             c->decode_xor = zmbv_decode_xor_8;
315         } else {
316             c->bpp = 24;
317             c->decode_intra = zmbv_decode_intra;
318             c->decode_xor = zmbv_decode_xor_24;
319         }
320         c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
321         c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
322         c->bx = (c->width + c->bw - 1) / c->bw;
323         c->by = (c->height+ c->bh - 1) / c->bh;
324     }
325
326     if(c->fmt == 0) {
327         av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
328         return -1;
329     }
330
331     if(c->comp == 0) { //Uncompressed data
332         memcpy(c->decomp_buf, buf, len);
333         c->decomp_size = 1;
334     } else { // ZLIB-compressed data
335 #ifdef CONFIG_ZLIB
336         c->zstream.total_in = c->zstream.total_out = 0;
337         c->zstream.next_in = buf;
338         c->zstream.avail_in = len;
339         c->zstream.next_out = c->decomp_buf;
340         c->zstream.avail_out = c->decomp_size;
341         inflate(&c->zstream, Z_FINISH);
342         c->decomp_len = c->zstream.total_out;
343 #else
344         av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
345         return -1;
346 #endif
347     }
348     if(c->flags & ZMBV_KEYFRAME) {
349         c->pic.key_frame = 1;
350         c->pic.pict_type = FF_I_TYPE;
351         c->decode_intra(c);
352     } else {
353         c->pic.key_frame = 0;
354         c->pic.pict_type = FF_P_TYPE;
355         c->decode_xor(c);
356     }
357
358     /* update frames */
359     {
360         uint8_t *out, *src;
361         int i, j;
362
363         out = c->pic.data[0];
364         src = c->cur;
365         for(j = 0; j < c->height; j++) {
366             for(i = 0; i < c->width; i++) {
367                 out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
368                 out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
369                 out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
370                 *src++;
371             }
372             out += c->pic.linesize[0];
373         }
374         memcpy(c->prev, c->cur, c->width * c->height);
375     }
376     *data_size = sizeof(AVFrame);
377     *(AVFrame*)data = c->pic;
378
379     /* always report that the buffer was completely consumed */
380     return buf_size;
381 }
382
383
384
385 /*
386  *
387  * Init zmbv decoder
388  *
389  */
390 static int decode_init(AVCodecContext *avctx)
391 {
392     ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
393     int zret; // Zlib return code
394
395     c->avctx = avctx;
396     avctx->has_b_frames = 0;
397
398     c->pic.data[0] = NULL;
399     c->width = avctx->width;
400     c->height = avctx->height;
401
402     if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
403         return 1;
404     }
405     c->bpp = avctx->bits_per_sample;
406
407 #ifdef CONFIG_ZLIB
408     // Needed if zlib unused or init aborted before inflateInit
409     memset(&(c->zstream), 0, sizeof(z_stream));
410 #else
411     av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
412     return 1;
413 #endif
414     avctx->pix_fmt = PIX_FMT_RGB24;
415     c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
416
417     /* Allocate decompression buffer */
418     if (c->decomp_size) {
419         if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
420             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
421             return 1;
422         }
423     }
424
425 #ifdef CONFIG_ZLIB
426     c->zstream.zalloc = Z_NULL;
427     c->zstream.zfree = Z_NULL;
428     c->zstream.opaque = Z_NULL;
429     zret = inflateInit(&(c->zstream));
430     if (zret != Z_OK) {
431         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
432         return 1;
433     }
434 #endif
435
436     return 0;
437 }
438
439
440
441 /*
442  *
443  * Uninit zmbv decoder
444  *
445  */
446 static int decode_end(AVCodecContext *avctx)
447 {
448     ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
449
450     av_freep(&c->decomp_buf);
451
452     if (c->pic.data[0])
453         avctx->release_buffer(avctx, &c->pic);
454 #ifdef CONFIG_ZLIB
455     inflateEnd(&(c->zstream));
456 #endif
457     if(c->cur)
458         av_freep(&c->cur);
459     if(c->prev)
460         av_freep(&c->prev);
461
462     return 0;
463 }
464
465 AVCodec zmbv_decoder = {
466     "zmbv",
467     CODEC_TYPE_VIDEO,
468     CODEC_ID_ZMBV,
469     sizeof(ZmbvContext),
470     decode_init,
471     NULL,
472     decode_end,
473     decode_frame
474 };
475