3 * Copyright (c) 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
26 #define SIZTABLE (1<<MAXBITS)
28 #define GCE_DISPOSAL_NONE 0
29 #define GCE_DISPOSAL_INPLACE 1
30 #define GCE_DISPOSAL_BACKGROUND 2
31 #define GCE_DISPOSAL_RESTORE 3
33 typedef struct GifState {
37 int background_color_index;
38 int transparent_color_index;
42 uint32_t *image_palette;
45 /* after the frame is displayed, the disposal method is used */
47 /* delay during which the frame is shown */
50 /* LZW compatible decoder */
57 int cursize; /* The current code size */
62 int newcodes; /* First available code */
63 int top_slot; /* Highest code for current size */
64 int slot; /* Last read code */
67 uint8_t stack[SIZTABLE];
68 uint8_t suffix[SIZTABLE];
69 uint16_t prefix[SIZTABLE];
72 uint8_t global_palette[256 * 3];
73 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 /* Probe gif video format or gif image format. The current heuristic
90 supposes the gif87a is always a single image. For gif89a, we
91 consider it as a video only if a GCE extension is present in the
93 static int gif_video_probe(AVProbeData * pd)
95 const uint8_t *p, *p_end;
96 int bits_per_pixel, has_global_palette, ext_code, ext_len;
97 int gce_flags, gce_disposal;
99 if (pd->buf_size < 24 ||
100 memcmp(pd->buf, gif89a_sig, 6) != 0)
102 p_end = pd->buf + pd->buf_size;
104 bits_per_pixel = (p[4] & 0x07) + 1;
105 has_global_palette = (p[4] & 0x80);
107 if (has_global_palette)
108 p += (1 << bits_per_pixel) * 3;
121 if (ext_code == 0xf9) {
124 /* if GCE extension found with gce_disposal != 0: it is
125 likely to be an animation */
127 gce_disposal = (gce_flags >> 2) & 0x7;
128 if (gce_disposal != 0)
129 return AVPROBE_SCORE_MAX;
145 static void GLZWDecodeInit(GifState * s, int csize)
156 s->cursize = s->codesize + 1;
157 s->curmask = mask[s->cursize];
158 s->top_slot = 1 << s->cursize;
159 s->clear_code = 1 << s->codesize;
160 s->end_code = s->clear_code + 1;
161 s->slot = s->newcodes = s->clear_code + 2;
167 static inline int GetCode(GifState * s)
172 while (s->bbits < s->cursize) {
174 if (ptr >= s->ebuf) {
175 if (!s->eob_reached) {
176 sizbuf = get_byte(s->f);
177 s->ebuf = s->buf + sizbuf;
180 get_buffer(s->f, s->buf, sizbuf);
187 s->bbuf |= ptr[0] << s->bbits;
192 c = s->bbuf & s->curmask;
193 s->bbuf >>= s->cursize;
194 s->bbits -= s->cursize;
198 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
199 written by Steven A. Bennett in 1987. */
200 /* return the number of byte decoded */
201 static int GLZWDecode(GifState * s, uint8_t * buf, int len)
203 int l, c, code, oc, fc;
214 while (sp > s->stack) {
222 if (c == s->end_code) {
225 } else if (c == s->clear_code) {
226 s->cursize = s->codesize + 1;
227 s->curmask = mask[s->cursize];
228 s->slot = s->newcodes;
229 s->top_slot = 1 << s->cursize;
230 while ((c = GetCode(s)) == s->clear_code);
231 if (c == s->end_code) {
244 if (code >= s->slot) {
248 while (code >= s->newcodes) {
249 *sp++ = s->suffix[code];
250 code = s->prefix[code];
253 if (s->slot < s->top_slot) {
254 s->suffix[s->slot] = fc = code;
255 s->prefix[s->slot++] = oc;
258 if (s->slot >= s->top_slot) {
259 if (s->cursize < MAXBITS) {
261 s->curmask = mask[++s->cursize];
264 while (sp > s->stack) {
278 static int gif_read_image(GifState *s)
280 ByteIOContext *f = s->f;
281 int left, top, width, height, bits_per_pixel, code_size, flags;
282 int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
283 uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
288 height = get_le16(f);
290 is_interleaved = flags & 0x40;
291 has_local_palette = flags & 0x80;
292 bits_per_pixel = (flags & 0x07) + 1;
294 printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
297 if (has_local_palette) {
298 get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
299 palette = s->local_palette;
301 palette = s->global_palette;
302 bits_per_pixel = s->bits_per_pixel;
305 /* verify that all the image is inside the screen dimensions */
306 if (left + width > s->screen_width ||
307 top + height > s->screen_height)
310 /* build the palette */
311 if (s->pix_fmt == PIX_FMT_RGB24) {
312 line = av_malloc(width);
316 n = (1 << bits_per_pixel);
318 for(i = 0; i < n; i++) {
319 s->image_palette[i] = (0xff << 24) |
320 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
324 s->image_palette[i] = (0xff << 24);
325 /* handle transparency */
326 if (s->transparent_color_index >= 0)
327 s->image_palette[s->transparent_color_index] = 0;
331 /* now get the image data */
333 code_size = get_byte(f);
334 GLZWDecodeInit(s, code_size);
336 /* read all the image */
337 linesize = s->image_linesize;
338 ptr1 = s->image_buf + top * linesize + (left * 3);
342 for (y = 0; y < height; y++) {
343 if (s->pix_fmt == PIX_FMT_RGB24) {
344 /* transcode to RGB24 */
345 GLZWDecode(s, line, width);
348 for(x = 0; x < width; x++) {
349 spal = palette + sptr[0] * 3;
357 GLZWDecode(s, ptr, width);
359 if (is_interleaved) {
369 ptr = ptr1 + linesize * 4;
371 ptr = ptr1 + linesize * 2;
380 ptr = ptr1 + linesize;
395 /* read the garbage data until end marker is found */
396 while (!s->eob_reached)
401 static int gif_read_extension(GifState *s)
403 ByteIOContext *f = s->f;
404 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
407 ext_code = get_byte(f);
408 ext_len = get_byte(f);
410 printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
416 s->transparent_color_index = -1;
417 gce_flags = get_byte(f);
418 s->gce_delay = get_le16(f);
419 gce_transparent_index = get_byte(f);
420 if (gce_flags & 0x01)
421 s->transparent_color_index = gce_transparent_index;
423 s->transparent_color_index = -1;
424 s->gce_disposal = (gce_flags >> 2) & 0x7;
426 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
427 gce_flags, s->gce_delay,
428 s->transparent_color_index, s->gce_disposal);
430 ext_len = get_byte(f);
434 /* NOTE: many extension blocks can come after */
436 while (ext_len != 0) {
437 for (i = 0; i < ext_len; i++)
439 ext_len = get_byte(f);
441 printf("gif: ext_len1=%d\n", ext_len);
447 static int gif_read_header1(GifState *s)
449 ByteIOContext *f = s->f;
452 int has_global_palette;
454 /* read gif signature */
455 ret = get_buffer(f, sig, 6);
458 if (memcmp(sig, gif87a_sig, 6) != 0 &&
459 memcmp(sig, gif89a_sig, 6) != 0)
462 /* read screen header */
463 s->transparent_color_index = -1;
464 s->screen_width = get_le16(f);
465 s->screen_height = get_le16(f);
466 if( (unsigned)s->screen_width > 32767
467 || (unsigned)s->screen_height > 32767){
468 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
473 s->color_resolution = ((v & 0x70) >> 4) + 1;
474 has_global_palette = (v & 0x80);
475 s->bits_per_pixel = (v & 0x07) + 1;
476 s->background_color_index = get_byte(f);
477 get_byte(f); /* ignored */
479 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
480 s->screen_width, s->screen_height, s->bits_per_pixel,
483 if (has_global_palette) {
484 n = 1 << s->bits_per_pixel;
485 get_buffer(f, s->global_palette, n * 3);
490 static int gif_parse_next_image(GifState *s)
492 ByteIOContext *f = s->f;
498 printf("gif: code=%02x '%c'\n", code, code);
502 if (gif_read_image(s) < 0)
511 if (gif_read_extension(s) < 0)
516 /* error or errneous EOF */
525 static int gif_read_header(AVFormatContext * s1,
526 AVFormatParameters * ap)
528 GifState *s = s1->priv_data;
529 ByteIOContext *f = &s1->pb;
533 if (gif_read_header1(s) < 0)
536 /* allocate image buffer */
537 s->image_linesize = s->screen_width * 3;
538 s->image_buf = av_malloc(s->screen_height * s->image_linesize);
541 s->pix_fmt = PIX_FMT_RGB24;
542 /* now we are ready: build format streams */
543 st = av_new_stream(s1, 0);
547 st->codec->codec_type = CODEC_TYPE_VIDEO;
548 st->codec->codec_id = CODEC_ID_RAWVIDEO;
549 st->codec->time_base.den = 5;
550 st->codec->time_base.num = 1;
551 /* XXX: check if screen size is always valid */
552 st->codec->width = s->screen_width;
553 st->codec->height = s->screen_height;
554 st->codec->pix_fmt = PIX_FMT_RGB24;
558 static int gif_read_packet(AVFormatContext * s1,
561 GifState *s = s1->priv_data;
564 ret = gif_parse_next_image(s);
568 /* XXX: avoid copying */
569 if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
572 pkt->stream_index = 0;
573 memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
577 static int gif_read_close(AVFormatContext *s1)
579 GifState *s = s1->priv_data;
580 av_free(s->image_buf);
584 AVInputFormat gif_demuxer =