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
23 int gif_write(ByteIOContext *pb, AVImageInfo *info);
28 #define SIZTABLE (1<<MAXBITS)
30 #define GCE_DISPOSAL_NONE 0
31 #define GCE_DISPOSAL_INPLACE 1
32 #define GCE_DISPOSAL_BACKGROUND 2
33 #define GCE_DISPOSAL_RESTORE 3
35 typedef struct GifState {
39 int background_color_index;
40 int transparent_color_index;
44 uint32_t *image_palette;
47 /* after the frame is displayed, the disposal method is used */
49 /* delay during which the frame is shown */
52 /* LZW compatible decoder */
59 int cursize; /* The current code size */
64 int newcodes; /* First available code */
65 int top_slot; /* Highest code for current size */
66 int slot; /* Last read code */
69 uint8_t stack[SIZTABLE];
70 uint8_t suffix[SIZTABLE];
71 uint16_t prefix[SIZTABLE];
74 uint8_t global_palette[256 * 3];
75 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 /* Probe gif video format or gif image format. The current heuristic
92 supposes the gif87a is always a single image. For gif89a, we
93 consider it as a video only if a GCE extension is present in the
95 static int gif_video_probe(AVProbeData * pd)
97 const uint8_t *p, *p_end;
98 int bits_per_pixel, has_global_palette, ext_code, ext_len;
99 int gce_flags, gce_disposal;
101 if (pd->buf_size < 24 ||
102 memcmp(pd->buf, gif89a_sig, 6) != 0)
104 p_end = pd->buf + pd->buf_size;
106 bits_per_pixel = (p[4] & 0x07) + 1;
107 has_global_palette = (p[4] & 0x80);
109 if (has_global_palette)
110 p += (1 << bits_per_pixel) * 3;
123 if (ext_code == 0xf9) {
126 /* if GCE extension found with gce_disposal != 0: it is
127 likely to be an animation */
129 gce_disposal = (gce_flags >> 2) & 0x7;
130 if (gce_disposal != 0)
131 return AVPROBE_SCORE_MAX;
147 static int gif_image_probe(AVProbeData * pd)
149 if (pd->buf_size >= 24 &&
150 (memcmp(pd->buf, gif87a_sig, 6) == 0 ||
151 memcmp(pd->buf, gif89a_sig, 6) == 0))
152 return AVPROBE_SCORE_MAX - 1;
158 static void GLZWDecodeInit(GifState * s, int csize)
169 s->cursize = s->codesize + 1;
170 s->curmask = mask[s->cursize];
171 s->top_slot = 1 << s->cursize;
172 s->clear_code = 1 << s->codesize;
173 s->end_code = s->clear_code + 1;
174 s->slot = s->newcodes = s->clear_code + 2;
180 static inline int GetCode(GifState * s)
185 while (s->bbits < s->cursize) {
187 if (ptr >= s->ebuf) {
188 if (!s->eob_reached) {
189 sizbuf = get_byte(s->f);
190 s->ebuf = s->buf + sizbuf;
193 get_buffer(s->f, s->buf, sizbuf);
200 s->bbuf |= ptr[0] << s->bbits;
205 c = s->bbuf & s->curmask;
206 s->bbuf >>= s->cursize;
207 s->bbits -= s->cursize;
211 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
212 written by Steven A. Bennett in 1987. */
213 /* return the number of byte decoded */
214 static int GLZWDecode(GifState * s, uint8_t * buf, int len)
216 int l, c, code, oc, fc;
227 while (sp > s->stack) {
235 if (c == s->end_code) {
238 } else if (c == s->clear_code) {
239 s->cursize = s->codesize + 1;
240 s->curmask = mask[s->cursize];
241 s->slot = s->newcodes;
242 s->top_slot = 1 << s->cursize;
243 while ((c = GetCode(s)) == s->clear_code);
244 if (c == s->end_code) {
257 if (code >= s->slot) {
261 while (code >= s->newcodes) {
262 *sp++ = s->suffix[code];
263 code = s->prefix[code];
266 if (s->slot < s->top_slot) {
267 s->suffix[s->slot] = fc = code;
268 s->prefix[s->slot++] = oc;
271 if (s->slot >= s->top_slot) {
272 if (s->cursize < MAXBITS) {
274 s->curmask = mask[++s->cursize];
277 while (sp > s->stack) {
291 static int gif_read_image(GifState *s)
293 ByteIOContext *f = s->f;
294 int left, top, width, height, bits_per_pixel, code_size, flags;
295 int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
296 uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
301 height = get_le16(f);
303 is_interleaved = flags & 0x40;
304 has_local_palette = flags & 0x80;
305 bits_per_pixel = (flags & 0x07) + 1;
307 printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
310 if (has_local_palette) {
311 get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
312 palette = s->local_palette;
314 palette = s->global_palette;
315 bits_per_pixel = s->bits_per_pixel;
318 /* verify that all the image is inside the screen dimensions */
319 if (left + width > s->screen_width ||
320 top + height > s->screen_height)
323 /* build the palette */
324 if (s->pix_fmt == PIX_FMT_RGB24) {
325 line = av_malloc(width);
329 n = (1 << bits_per_pixel);
331 for(i = 0; i < n; i++) {
332 s->image_palette[i] = (0xff << 24) |
333 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
337 s->image_palette[i] = (0xff << 24);
338 /* handle transparency */
339 if (s->transparent_color_index >= 0)
340 s->image_palette[s->transparent_color_index] = 0;
344 /* now get the image data */
346 code_size = get_byte(f);
347 GLZWDecodeInit(s, code_size);
349 /* read all the image */
350 linesize = s->image_linesize;
351 ptr1 = s->image_buf + top * linesize + (left * 3);
355 for (y = 0; y < height; y++) {
356 if (s->pix_fmt == PIX_FMT_RGB24) {
357 /* transcode to RGB24 */
358 GLZWDecode(s, line, width);
361 for(x = 0; x < width; x++) {
362 spal = palette + sptr[0] * 3;
370 GLZWDecode(s, ptr, width);
372 if (is_interleaved) {
382 ptr = ptr1 + linesize * 4;
384 ptr = ptr1 + linesize * 2;
393 ptr = ptr1 + linesize;
408 /* read the garbage data until end marker is found */
409 while (!s->eob_reached)
414 static int gif_read_extension(GifState *s)
416 ByteIOContext *f = s->f;
417 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
420 ext_code = get_byte(f);
421 ext_len = get_byte(f);
423 printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
429 s->transparent_color_index = -1;
430 gce_flags = get_byte(f);
431 s->gce_delay = get_le16(f);
432 gce_transparent_index = get_byte(f);
433 if (gce_flags & 0x01)
434 s->transparent_color_index = gce_transparent_index;
436 s->transparent_color_index = -1;
437 s->gce_disposal = (gce_flags >> 2) & 0x7;
439 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
440 gce_flags, s->gce_delay,
441 s->transparent_color_index, s->gce_disposal);
443 ext_len = get_byte(f);
447 /* NOTE: many extension blocks can come after */
449 while (ext_len != 0) {
450 for (i = 0; i < ext_len; i++)
452 ext_len = get_byte(f);
454 printf("gif: ext_len1=%d\n", ext_len);
460 static int gif_read_header1(GifState *s)
462 ByteIOContext *f = s->f;
465 int has_global_palette;
467 /* read gif signature */
468 ret = get_buffer(f, sig, 6);
471 if (memcmp(sig, gif87a_sig, 6) != 0 &&
472 memcmp(sig, gif89a_sig, 6) != 0)
475 /* read screen header */
476 s->transparent_color_index = -1;
477 s->screen_width = get_le16(f);
478 s->screen_height = get_le16(f);
479 if( (unsigned)s->screen_width > 32767
480 || (unsigned)s->screen_height > 32767){
481 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
486 s->color_resolution = ((v & 0x70) >> 4) + 1;
487 has_global_palette = (v & 0x80);
488 s->bits_per_pixel = (v & 0x07) + 1;
489 s->background_color_index = get_byte(f);
490 get_byte(f); /* ignored */
492 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
493 s->screen_width, s->screen_height, s->bits_per_pixel,
496 if (has_global_palette) {
497 n = 1 << s->bits_per_pixel;
498 get_buffer(f, s->global_palette, n * 3);
503 static int gif_parse_next_image(GifState *s)
505 ByteIOContext *f = s->f;
511 printf("gif: code=%02x '%c'\n", code, code);
515 if (gif_read_image(s) < 0)
524 if (gif_read_extension(s) < 0)
529 /* error or errneous EOF */
538 static int gif_read_header(AVFormatContext * s1,
539 AVFormatParameters * ap)
541 GifState *s = s1->priv_data;
542 ByteIOContext *f = &s1->pb;
546 if (gif_read_header1(s) < 0)
549 /* allocate image buffer */
550 s->image_linesize = s->screen_width * 3;
551 s->image_buf = av_malloc(s->screen_height * s->image_linesize);
554 s->pix_fmt = PIX_FMT_RGB24;
555 /* now we are ready: build format streams */
556 st = av_new_stream(s1, 0);
560 st->codec->codec_type = CODEC_TYPE_VIDEO;
561 st->codec->codec_id = CODEC_ID_RAWVIDEO;
562 st->codec->time_base.den = 5;
563 st->codec->time_base.num = 1;
564 /* XXX: check if screen size is always valid */
565 st->codec->width = s->screen_width;
566 st->codec->height = s->screen_height;
567 st->codec->pix_fmt = PIX_FMT_RGB24;
571 static int gif_read_packet(AVFormatContext * s1,
574 GifState *s = s1->priv_data;
577 ret = gif_parse_next_image(s);
581 /* XXX: avoid copying */
582 if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
585 pkt->stream_index = 0;
586 memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
590 static int gif_read_close(AVFormatContext *s1)
592 GifState *s = s1->priv_data;
593 av_free(s->image_buf);
597 /* read gif as image */
598 static int gif_read(ByteIOContext *f,
599 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
601 GifState s1, *s = &s1;
602 AVImageInfo info1, *info = &info1;
605 memset(s, 0, sizeof(GifState));
607 if (gif_read_header1(s) < 0)
609 info->width = s->screen_width;
610 info->height = s->screen_height;
611 info->pix_fmt = PIX_FMT_PAL8;
612 ret = alloc_cb(opaque, info);
615 s->image_buf = info->pict.data[0];
616 s->image_linesize = info->pict.linesize[0];
617 s->image_palette = (uint32_t *)info->pict.data[1];
619 if (gif_parse_next_image(s) < 0)
624 AVInputFormat gif_demuxer =
635 AVImageFormat gif_image_format = {
641 #ifdef CONFIG_GIF_MUXER