3 * Copyright (c) 2003 Fabrice Bellard.
4 * Copyright (c) 2006 Baptiste Coudurier.
6 * This file is part of FFmpeg.
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.
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.
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
26 #include "bytestream.h"
29 #define SIZTABLE (1<<MAXBITS)
31 #define GCE_DISPOSAL_NONE 0
32 #define GCE_DISPOSAL_INPLACE 1
33 #define GCE_DISPOSAL_BACKGROUND 2
34 #define GCE_DISPOSAL_RESTORE 3
36 typedef struct GifState {
41 int background_color_index;
42 int transparent_color_index;
44 uint32_t *image_palette;
46 /* after the frame is displayed, the disposal method is used */
48 /* delay during which the frame is shown */
51 /* LZW compatible decoder */
58 int cursize; /* The current code size */
63 int newcodes; /* First available code */
64 int top_slot; /* Highest code for current size */
65 int slot; /* Last read code */
68 uint8_t stack[SIZTABLE];
69 uint8_t suffix[SIZTABLE];
70 uint16_t prefix[SIZTABLE];
73 uint8_t global_palette[256 * 3];
74 uint8_t local_palette[256 * 3];
78 static const uint8_t gif87a_sig[6] = "GIF87a";
79 static const uint8_t gif89a_sig[6] = "GIF89a";
81 static const uint16_t mask[17] =
83 0x0000, 0x0001, 0x0003, 0x0007,
84 0x000F, 0x001F, 0x003F, 0x007F,
85 0x00FF, 0x01FF, 0x03FF, 0x07FF,
86 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
89 static void GLZWDecodeInit(GifState * s, int 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;
111 static inline int GetCode(GifState * s)
116 while (s->bbits < s->cursize) {
118 if (ptr >= s->ebuf) {
119 if (!s->eob_reached) {
120 sizbuf = bytestream_get_byte(&s->bytestream);
121 s->ebuf = s->buf + sizbuf;
124 bytestream_get_buffer(&s->bytestream, s->buf, sizbuf);
131 s->bbuf |= ptr[0] << s->bbits;
136 c = s->bbuf & s->curmask;
137 s->bbuf >>= s->cursize;
138 s->bbits -= s->cursize;
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)
147 int l, c, code, oc, fc;
158 while (sp > s->stack) {
166 if (c == s->end_code) {
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) {
188 if (code >= s->slot) {
192 while (code >= s->newcodes) {
193 *sp++ = s->suffix[code];
194 code = s->prefix[code];
197 if (s->slot < s->top_slot) {
198 s->suffix[s->slot] = fc = code;
199 s->prefix[s->slot++] = oc;
202 if (s->slot >= s->top_slot) {
203 if (s->cursize < MAXBITS) {
205 s->curmask = mask[++s->cursize];
208 while (sp > s->stack) {
222 static int gif_read_image(GifState *s)
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;
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;
237 dprintf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
240 if (has_local_palette) {
241 bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
242 palette = s->local_palette;
244 palette = s->global_palette;
245 bits_per_pixel = s->bits_per_pixel;
248 /* verify that all the image is inside the screen dimensions */
249 if (left + width > s->screen_width ||
250 top + height > s->screen_height)
253 /* build the palette */
254 n = (1 << bits_per_pixel);
256 for(i = 0; i < n; i++) {
257 s->image_palette[i] = (0xff << 24) |
258 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
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;
268 /* now get the image data */
269 code_size = bytestream_get_byte(&s->bytestream);
270 GLZWDecodeInit(s, code_size);
272 /* read all the image */
273 linesize = s->picture.linesize[0];
274 ptr1 = s->picture.data[0] + top * linesize + (left * 3);
278 for (y = 0; y < height; y++) {
279 GLZWDecode(s, ptr, width);
280 if (is_interleaved) {
290 ptr = ptr1 + linesize * 4;
292 ptr = ptr1 + linesize * 2;
301 ptr = ptr1 + linesize;
316 /* read the garbage data until end marker is found */
317 while (!s->eob_reached)
322 static int gif_read_extension(GifState *s)
324 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
327 ext_code = bytestream_get_byte(&s->bytestream);
328 ext_len = bytestream_get_byte(&s->bytestream);
330 dprintf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
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;
343 s->transparent_color_index = -1;
344 s->gce_disposal = (gce_flags >> 2) & 0x7;
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);
350 ext_len = bytestream_get_byte(&s->bytestream);
354 /* NOTE: many extension blocks can come after */
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);
361 dprintf("gif: ext_len1=%d\n", ext_len);
367 static int gif_read_header1(GifState *s)
371 int has_global_palette;
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)
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");
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 */
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,
400 if (has_global_palette) {
401 n = 1 << s->bits_per_pixel;
402 bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
407 static int gif_parse_next_image(GifState *s)
412 code = bytestream_get_byte(&s->bytestream);
414 dprintf("gif: code=%02x '%c'\n", code, code);
418 if (gif_read_image(s) < 0)
427 if (gif_read_extension(s) < 0)
432 /* error or errneous EOF */
441 static int gif_decode_init(AVCodecContext *avctx)
443 GifState *s = avctx->priv_data;
445 avcodec_get_frame_defaults(&s->picture);
446 avctx->coded_frame= &s->picture;
447 s->picture.data[0] = NULL;
451 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
453 GifState *s = avctx->priv_data;
454 AVFrame *picture = data;
458 if (gif_read_header1(s) < 0)
461 avctx->pix_fmt = PIX_FMT_PAL8;
462 if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
464 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
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");
472 s->image_palette = (uint32_t *)s->picture.data[1];
473 ret = gif_parse_next_image(s);
477 *picture = s->picture;
478 *data_size = sizeof(AVPicture);
482 static int gif_decode_close(AVCodecContext *avctx)
484 GifState *s = avctx->priv_data;
486 if(s->picture.data[0])
487 avctx->release_buffer(avctx, &s->picture);
491 AVCodec gif_decoder = {