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 {
40 int background_color_index;
41 int transparent_color_index;
45 uint32_t image_palette[256];
48 /* after the frame is displayed, the disposal method is used */
50 /* delay during which the frame is shown */
53 /* LZW compatible decoder */
60 int cursize; /* The current code size */
65 int newcodes; /* First available code */
66 int top_slot; /* Highest code for current size */
67 int slot; /* Last read code */
70 uint8_t stack[SIZTABLE];
71 uint8_t suffix[SIZTABLE];
72 uint16_t prefix[SIZTABLE];
75 uint8_t global_palette[256 * 3];
76 uint8_t local_palette[256 * 3];
80 static const uint8_t gif87a_sig[6] = "GIF87a";
81 static const uint8_t gif89a_sig[6] = "GIF89a";
83 static const uint16_t mask[17] =
85 0x0000, 0x0001, 0x0003, 0x0007,
86 0x000F, 0x001F, 0x003F, 0x007F,
87 0x00FF, 0x01FF, 0x03FF, 0x07FF,
88 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
91 static void GLZWDecodeInit(GifState * s, int csize)
102 s->cursize = s->codesize + 1;
103 s->curmask = mask[s->cursize];
104 s->top_slot = 1 << s->cursize;
105 s->clear_code = 1 << s->codesize;
106 s->end_code = s->clear_code + 1;
107 s->slot = s->newcodes = s->clear_code + 2;
113 static inline int GetCode(GifState * s)
118 while (s->bbits < s->cursize) {
120 if (ptr >= s->ebuf) {
121 if (!s->eob_reached) {
122 sizbuf = bytestream_get_byte(&s->bytestream);
123 s->ebuf = s->buf + sizbuf;
126 bytestream_get_buffer(&s->bytestream, s->buf, sizbuf);
133 s->bbuf |= ptr[0] << s->bbits;
138 c = s->bbuf & s->curmask;
139 s->bbuf >>= s->cursize;
140 s->bbits -= s->cursize;
144 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
145 written by Steven A. Bennett in 1987. */
146 /* return the number of byte decoded */
147 static int GLZWDecode(GifState * s, uint8_t * buf, int len)
149 int l, c, code, oc, fc;
160 while (sp > s->stack) {
168 if (c == s->end_code) {
171 } else if (c == s->clear_code) {
172 s->cursize = s->codesize + 1;
173 s->curmask = mask[s->cursize];
174 s->slot = s->newcodes;
175 s->top_slot = 1 << s->cursize;
176 while ((c = GetCode(s)) == s->clear_code);
177 if (c == s->end_code) {
190 if (code >= s->slot) {
194 while (code >= s->newcodes) {
195 *sp++ = s->suffix[code];
196 code = s->prefix[code];
199 if (s->slot < s->top_slot) {
200 s->suffix[s->slot] = fc = code;
201 s->prefix[s->slot++] = oc;
204 if (s->slot >= s->top_slot) {
205 if (s->cursize < MAXBITS) {
207 s->curmask = mask[++s->cursize];
210 while (sp > s->stack) {
224 static int gif_read_image(GifState *s)
226 int left, top, width, height, bits_per_pixel, code_size, flags;
227 int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
228 uint8_t *ptr, *line, *spal, *palette, *ptr1;
230 left = bytestream_get_le16(&s->bytestream);
231 top = bytestream_get_le16(&s->bytestream);
232 width = bytestream_get_le16(&s->bytestream);
233 height = bytestream_get_le16(&s->bytestream);
234 flags = bytestream_get_byte(&s->bytestream);
235 is_interleaved = flags & 0x40;
236 has_local_palette = flags & 0x80;
237 bits_per_pixel = (flags & 0x07) + 1;
239 dprintf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
242 if (has_local_palette) {
243 bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
244 palette = s->local_palette;
246 palette = s->global_palette;
247 bits_per_pixel = s->bits_per_pixel;
250 /* verify that all the image is inside the screen dimensions */
251 if (left + width > s->screen_width ||
252 top + height > s->screen_height)
255 /* build the palette */
256 n = (1 << bits_per_pixel);
258 for(i = 0; i < n; i++) {
259 s->image_palette[i] = (0xff << 24) |
260 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
264 s->image_palette[i] = (0xff << 24);
265 /* handle transparency */
266 if (s->transparent_color_index >= 0)
267 s->image_palette[s->transparent_color_index] = 0;
270 /* now get the image data */
271 code_size = bytestream_get_byte(&s->bytestream);
272 GLZWDecodeInit(s, code_size);
274 /* read all the image */
275 linesize = s->image_linesize;
276 ptr1 = s->image_buf + top * linesize + (left * 3);
280 for (y = 0; y < height; y++) {
281 GLZWDecode(s, ptr, width);
282 if (is_interleaved) {
292 ptr = ptr1 + linesize * 4;
294 ptr = ptr1 + linesize * 2;
303 ptr = ptr1 + linesize;
318 /* read the garbage data until end marker is found */
319 while (!s->eob_reached)
324 static int gif_read_extension(GifState *s)
326 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
329 ext_code = bytestream_get_byte(&s->bytestream);
330 ext_len = bytestream_get_byte(&s->bytestream);
332 dprintf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
338 s->transparent_color_index = -1;
339 gce_flags = bytestream_get_byte(&s->bytestream);
340 s->gce_delay = bytestream_get_le16(&s->bytestream);
341 gce_transparent_index = bytestream_get_byte(&s->bytestream);
342 if (gce_flags & 0x01)
343 s->transparent_color_index = gce_transparent_index;
345 s->transparent_color_index = -1;
346 s->gce_disposal = (gce_flags >> 2) & 0x7;
348 dprintf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
349 gce_flags, s->gce_delay,
350 s->transparent_color_index, s->gce_disposal);
352 ext_len = bytestream_get_byte(&s->bytestream);
356 /* NOTE: many extension blocks can come after */
358 while (ext_len != 0) {
359 for (i = 0; i < ext_len; i++)
360 bytestream_get_byte(&s->bytestream);
361 ext_len = bytestream_get_byte(&s->bytestream);
363 dprintf("gif: ext_len1=%d\n", ext_len);
369 static int gif_read_header1(GifState *s)
373 int has_global_palette;
375 /* read gif signature */
376 bytestream_get_buffer(&s->bytestream, sig, 6);
377 if (memcmp(sig, gif87a_sig, 6) != 0 &&
378 memcmp(sig, gif89a_sig, 6) != 0)
381 /* read screen header */
382 s->transparent_color_index = -1;
383 s->screen_width = bytestream_get_le16(&s->bytestream);
384 s->screen_height = bytestream_get_le16(&s->bytestream);
385 if( (unsigned)s->screen_width > 32767
386 || (unsigned)s->screen_height > 32767){
387 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
391 v = bytestream_get_byte(&s->bytestream);
392 s->color_resolution = ((v & 0x70) >> 4) + 1;
393 has_global_palette = (v & 0x80);
394 s->bits_per_pixel = (v & 0x07) + 1;
395 s->background_color_index = bytestream_get_byte(&s->bytestream);
396 bytestream_get_byte(&s->bytestream); /* ignored */
398 dprintf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
399 s->screen_width, s->screen_height, s->bits_per_pixel,
402 if (has_global_palette) {
403 n = 1 << s->bits_per_pixel;
404 bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
409 static int gif_parse_next_image(GifState *s)
414 code = bytestream_get_byte(&s->bytestream);
416 dprintf("gif: code=%02x '%c'\n", code, code);
420 if (gif_read_image(s) < 0)
429 if (gif_read_extension(s) < 0)
434 /* error or errneous EOF */
443 static int gif_decode_init(AVCodecContext *avctx)
445 GifState *s = avctx->priv_data;
450 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
452 GifState *s = avctx->priv_data;
453 AVFrame *picture = data;
457 if (gif_read_header1(s) < 0)
460 /* allocate image buffer */
461 s->image_linesize = s->screen_width * 3;
462 s->image_buf = av_malloc(s->screen_height * s->image_linesize);
465 s->pix_fmt = PIX_FMT_PAL8;
466 /* now we are ready: build format streams */
468 /* XXX: check if screen size is always valid */
469 avctx->width = s->screen_width;
470 avctx->height = s->screen_height;
471 avctx->pix_fmt = PIX_FMT_PAL8;
473 ret = gif_parse_next_image(s);
477 picture->data[0] = s->image_buf;
478 picture->linesize[0] = s->image_linesize;
479 picture->data[1] = s->image_palette;
480 picture->linesize[1] = 4;
482 *data_size = sizeof(AVPicture);
486 AVCodec gif_decoder = {