]> git.sesse.net Git - ffmpeg/blob - libavcodec/gifdec.c
removed useless variables
[ffmpeg] / libavcodec / gifdec.c
1 /*
2  * GIF decoder
3  * Copyright (c) 2003 Fabrice Bellard.
4  * Copyright (c) 2006 Baptiste Coudurier.
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 //#define DEBUG
24
25 #include "avcodec.h"
26 #include "bytestream.h"
27
28 #define MAXBITS                 12
29 #define SIZTABLE                (1<<MAXBITS)
30
31 #define GCE_DISPOSAL_NONE       0
32 #define GCE_DISPOSAL_INPLACE    1
33 #define GCE_DISPOSAL_BACKGROUND 2
34 #define GCE_DISPOSAL_RESTORE    3
35
36 typedef struct GifState {
37     AVFrame picture;
38     int screen_width;
39     int screen_height;
40     int bits_per_pixel;
41     int background_color_index;
42     int transparent_color_index;
43     int color_resolution;
44     uint32_t *image_palette;
45
46     /* after the frame is displayed, the disposal method is used */
47     int gce_disposal;
48     /* delay during which the frame is shown */
49     int gce_delay;
50
51     /* LZW compatible decoder */
52     uint8_t *bytestream;
53     int eob_reached;
54     uint8_t *pbuf, *ebuf;
55     int bbits;
56     unsigned int bbuf;
57
58     int cursize;                /* The current code size */
59     int curmask;
60     int codesize;
61     int clear_code;
62     int end_code;
63     int newcodes;               /* First available code */
64     int top_slot;               /* Highest code for current size */
65     int slot;                   /* Last read code */
66     int fc, oc;
67     uint8_t *sp;
68     uint8_t stack[SIZTABLE];
69     uint8_t suffix[SIZTABLE];
70     uint16_t prefix[SIZTABLE];
71
72     /* aux buffers */
73     uint8_t global_palette[256 * 3];
74     uint8_t local_palette[256 * 3];
75     uint8_t buf[256];
76 } GifState;
77
78 static const uint8_t gif87a_sig[6] = "GIF87a";
79 static const uint8_t gif89a_sig[6] = "GIF89a";
80
81 static const uint16_t mask[17] =
82 {
83     0x0000, 0x0001, 0x0003, 0x0007,
84     0x000F, 0x001F, 0x003F, 0x007F,
85     0x00FF, 0x01FF, 0x03FF, 0x07FF,
86     0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
87 };
88
89 static void GLZWDecodeInit(GifState * s, int csize)
90 {
91     /* read buffer */
92     s->eob_reached = 0;
93     s->pbuf = s->buf;
94     s->ebuf = s->buf;
95     s->bbuf = 0;
96     s->bbits = 0;
97
98     /* decoder */
99     s->codesize = csize;
100     s->cursize = s->codesize + 1;
101     s->curmask = mask[s->cursize];
102     s->top_slot = 1 << s->cursize;
103     s->clear_code = 1 << s->codesize;
104     s->end_code = s->clear_code + 1;
105     s->slot = s->newcodes = s->clear_code + 2;
106     s->oc = s->fc = 0;
107     s->sp = s->stack;
108 }
109
110 /* XXX: optimize */
111 static inline int GetCode(GifState * s)
112 {
113     int c, sizbuf;
114     uint8_t *ptr;
115
116     while (s->bbits < s->cursize) {
117         ptr = s->pbuf;
118         if (ptr >= s->ebuf) {
119             if (!s->eob_reached) {
120                 sizbuf = bytestream_get_byte(&s->bytestream);
121                 s->ebuf = s->buf + sizbuf;
122                 s->pbuf = s->buf;
123                 if (sizbuf > 0) {
124                     bytestream_get_buffer(&s->bytestream, s->buf, sizbuf);
125                 } else {
126                     s->eob_reached = 1;
127                 }
128             }
129             ptr = s->pbuf;
130         }
131         s->bbuf |= ptr[0] << s->bbits;
132         ptr++;
133         s->pbuf = ptr;
134         s->bbits += 8;
135     }
136     c = s->bbuf & s->curmask;
137     s->bbuf >>= s->cursize;
138     s->bbits -= s->cursize;
139     return c;
140 }
141
142 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
143    written by Steven A. Bennett in 1987. */
144 /* return the number of byte decoded */
145 static int GLZWDecode(GifState * s, uint8_t * buf, int len)
146 {
147     int l, c, code, oc, fc;
148     uint8_t *sp;
149
150     if (s->end_code < 0)
151         return 0;
152
153     l = len;
154     sp = s->sp;
155     oc = s->oc;
156     fc = s->fc;
157
158     while (sp > s->stack) {
159         *buf++ = *(--sp);
160         if ((--l) == 0)
161             goto the_end;
162     }
163
164     for (;;) {
165         c = GetCode(s);
166         if (c == s->end_code) {
167             s->end_code = -1;
168             break;
169         } else if (c == s->clear_code) {
170             s->cursize = s->codesize + 1;
171             s->curmask = mask[s->cursize];
172             s->slot = s->newcodes;
173             s->top_slot = 1 << s->cursize;
174             while ((c = GetCode(s)) == s->clear_code);
175             if (c == s->end_code) {
176                 s->end_code = -1;
177                 break;
178             }
179             /* test error */
180             if (c >= s->slot)
181                 c = 0;
182             fc = oc = c;
183             *buf++ = c;
184             if ((--l) == 0)
185                 break;
186         } else {
187             code = c;
188             if (code >= s->slot) {
189                 *sp++ = fc;
190                 code = oc;
191             }
192             while (code >= s->newcodes) {
193                 *sp++ = s->suffix[code];
194                 code = s->prefix[code];
195             }
196             *sp++ = code;
197             if (s->slot < s->top_slot) {
198                 s->suffix[s->slot] = fc = code;
199                 s->prefix[s->slot++] = oc;
200                 oc = c;
201             }
202             if (s->slot >= s->top_slot) {
203                 if (s->cursize < MAXBITS) {
204                     s->top_slot <<= 1;
205                     s->curmask = mask[++s->cursize];
206                 }
207             }
208             while (sp > s->stack) {
209                 *buf++ = *(--sp);
210                 if ((--l) == 0)
211                     goto the_end;
212             }
213         }
214     }
215   the_end:
216     s->sp = sp;
217     s->oc = oc;
218     s->fc = fc;
219     return len - l;
220 }
221
222 static int gif_read_image(GifState *s)
223 {
224     int left, top, width, height, bits_per_pixel, code_size, flags;
225     int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
226     uint8_t *ptr, *line, *spal, *palette, *ptr1;
227
228     left = bytestream_get_le16(&s->bytestream);
229     top = bytestream_get_le16(&s->bytestream);
230     width = bytestream_get_le16(&s->bytestream);
231     height = bytestream_get_le16(&s->bytestream);
232     flags = bytestream_get_byte(&s->bytestream);
233     is_interleaved = flags & 0x40;
234     has_local_palette = flags & 0x80;
235     bits_per_pixel = (flags & 0x07) + 1;
236 #ifdef DEBUG
237     dprintf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
238 #endif
239
240     if (has_local_palette) {
241         bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
242         palette = s->local_palette;
243     } else {
244         palette = s->global_palette;
245         bits_per_pixel = s->bits_per_pixel;
246     }
247
248     /* verify that all the image is inside the screen dimensions */
249     if (left + width > s->screen_width ||
250         top + height > s->screen_height)
251         return -EINVAL;
252
253     /* build the palette */
254         n = (1 << bits_per_pixel);
255         spal = palette;
256         for(i = 0; i < n; i++) {
257             s->image_palette[i] = (0xff << 24) |
258                 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
259             spal += 3;
260         }
261         for(; i < 256; i++)
262             s->image_palette[i] = (0xff << 24);
263         /* handle transparency */
264         if (s->transparent_color_index >= 0)
265             s->image_palette[s->transparent_color_index] = 0;
266         line = NULL;
267
268     /* now get the image data */
269     code_size = bytestream_get_byte(&s->bytestream);
270     GLZWDecodeInit(s, code_size);
271
272     /* read all the image */
273     linesize = s->picture.linesize[0];
274     ptr1 = s->picture.data[0] + top * linesize + (left * 3);
275     ptr = ptr1;
276     pass = 0;
277     y1 = 0;
278     for (y = 0; y < height; y++) {
279             GLZWDecode(s, ptr, width);
280         if (is_interleaved) {
281             switch(pass) {
282             default:
283             case 0:
284             case 1:
285                 y1 += 8;
286                 ptr += linesize * 8;
287                 if (y1 >= height) {
288                     y1 = 4;
289                     if (pass == 0)
290                         ptr = ptr1 + linesize * 4;
291                     else
292                         ptr = ptr1 + linesize * 2;
293                     pass++;
294                 }
295                 break;
296             case 2:
297                 y1 += 4;
298                 ptr += linesize * 4;
299                 if (y1 >= height) {
300                     y1 = 1;
301                     ptr = ptr1 + linesize;
302                     pass++;
303                 }
304                 break;
305             case 3:
306                 y1 += 2;
307                 ptr += linesize * 2;
308                 break;
309             }
310         } else {
311             ptr += linesize;
312         }
313     }
314     av_free(line);
315
316     /* read the garbage data until end marker is found */
317     while (!s->eob_reached)
318         GetCode(s);
319     return 0;
320 }
321
322 static int gif_read_extension(GifState *s)
323 {
324     int ext_code, ext_len, i, gce_flags, gce_transparent_index;
325
326     /* extension */
327     ext_code = bytestream_get_byte(&s->bytestream);
328     ext_len = bytestream_get_byte(&s->bytestream);
329 #ifdef DEBUG
330     dprintf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
331 #endif
332     switch(ext_code) {
333     case 0xf9:
334         if (ext_len != 4)
335             goto discard_ext;
336         s->transparent_color_index = -1;
337         gce_flags = bytestream_get_byte(&s->bytestream);
338         s->gce_delay = bytestream_get_le16(&s->bytestream);
339         gce_transparent_index = bytestream_get_byte(&s->bytestream);
340         if (gce_flags & 0x01)
341             s->transparent_color_index = gce_transparent_index;
342         else
343             s->transparent_color_index = -1;
344         s->gce_disposal = (gce_flags >> 2) & 0x7;
345 #ifdef DEBUG
346         dprintf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
347                gce_flags, s->gce_delay,
348                s->transparent_color_index, s->gce_disposal);
349 #endif
350         ext_len = bytestream_get_byte(&s->bytestream);
351         break;
352     }
353
354     /* NOTE: many extension blocks can come after */
355  discard_ext:
356     while (ext_len != 0) {
357         for (i = 0; i < ext_len; i++)
358             bytestream_get_byte(&s->bytestream);
359         ext_len = bytestream_get_byte(&s->bytestream);
360 #ifdef DEBUG
361         dprintf("gif: ext_len1=%d\n", ext_len);
362 #endif
363     }
364     return 0;
365 }
366
367 static int gif_read_header1(GifState *s)
368 {
369     uint8_t sig[6];
370     int v, n;
371     int has_global_palette;
372
373     /* read gif signature */
374     bytestream_get_buffer(&s->bytestream, sig, 6);
375     if (memcmp(sig, gif87a_sig, 6) != 0 &&
376         memcmp(sig, gif89a_sig, 6) != 0)
377         return -1;
378
379     /* read screen header */
380     s->transparent_color_index = -1;
381     s->screen_width = bytestream_get_le16(&s->bytestream);
382     s->screen_height = bytestream_get_le16(&s->bytestream);
383     if(   (unsigned)s->screen_width  > 32767
384        || (unsigned)s->screen_height > 32767){
385         av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
386         return -1;
387     }
388
389     v = bytestream_get_byte(&s->bytestream);
390     s->color_resolution = ((v & 0x70) >> 4) + 1;
391     has_global_palette = (v & 0x80);
392     s->bits_per_pixel = (v & 0x07) + 1;
393     s->background_color_index = bytestream_get_byte(&s->bytestream);
394     bytestream_get_byte(&s->bytestream);                /* ignored */
395 #ifdef DEBUG
396     dprintf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
397            s->screen_width, s->screen_height, s->bits_per_pixel,
398            has_global_palette);
399 #endif
400     if (has_global_palette) {
401         n = 1 << s->bits_per_pixel;
402         bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
403     }
404     return 0;
405 }
406
407 static int gif_parse_next_image(GifState *s)
408 {
409     int ret, code;
410
411     for (;;) {
412         code = bytestream_get_byte(&s->bytestream);
413 #ifdef DEBUG
414         dprintf("gif: code=%02x '%c'\n", code, code);
415 #endif
416         switch (code) {
417         case ',':
418             if (gif_read_image(s) < 0)
419                 return -1;
420             ret = 0;
421             goto the_end;
422         case ';':
423             /* end of image */
424             ret = -1;
425             goto the_end;
426         case '!':
427             if (gif_read_extension(s) < 0)
428                 return -1;
429             break;
430         case EOF:
431         default:
432             /* error or errneous EOF */
433             ret = -1;
434             goto the_end;
435         }
436     }
437   the_end:
438     return ret;
439 }
440
441 static int gif_decode_init(AVCodecContext *avctx)
442 {
443     GifState *s = avctx->priv_data;
444
445     avcodec_get_frame_defaults(&s->picture);
446     avctx->coded_frame= &s->picture;
447     s->picture.data[0] = NULL;
448     return 0;
449 }
450
451 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
452 {
453     GifState *s = avctx->priv_data;
454     AVFrame *picture = data;
455     int ret;
456
457     s->bytestream = buf;
458     if (gif_read_header1(s) < 0)
459         return -1;
460
461     avctx->pix_fmt = PIX_FMT_PAL8;
462     if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
463         return -1;
464     avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
465
466     if (s->picture.data[0])
467         avctx->release_buffer(avctx, &s->picture);
468     if (avctx->get_buffer(avctx, &s->picture) < 0) {
469         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
470         return -1;
471     }
472     s->image_palette = (uint32_t *)s->picture.data[1];
473     ret = gif_parse_next_image(s);
474     if (ret < 0)
475         return ret;
476
477     *picture = s->picture;
478     *data_size = sizeof(AVPicture);
479     return 0;
480 }
481
482 static int gif_decode_close(AVCodecContext *avctx)
483 {
484     GifState *s = avctx->priv_data;
485
486     if(s->picture.data[0])
487         avctx->release_buffer(avctx, &s->picture);
488     return 0;
489 }
490
491 AVCodec gif_decoder = {
492     "gif",
493     CODEC_TYPE_VIDEO,
494     CODEC_ID_GIF,
495     sizeof(GifState),
496     gif_decode_init,
497     NULL,
498     gif_decode_close,
499     gif_decode_frame,
500 };