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
)
308 return AVERROR(EINVAL
);
310 /* build the palette */
311 if (s
->pix_fmt
== PIX_FMT_RGB24
) {
312 line
= av_malloc(width
);
314 return AVERROR(ENOMEM
);
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
) {
367 y1
= pass
== 0 ? 4 : 2;
368 ptr
= ptr1
+ linesize
* y1
;
377 ptr
= ptr1
+ linesize
;
392 /* read the garbage data until end marker is found */
393 while (!s
->eob_reached
)
398 static int gif_read_extension(GifState
*s
)
400 ByteIOContext
*f
= s
->f
;
401 int ext_code
, ext_len
, i
, gce_flags
, gce_transparent_index
;
404 ext_code
= get_byte(f
);
405 ext_len
= get_byte(f
);
407 printf("gif: ext_code=0x%x len=%d\n", ext_code
, ext_len
);
413 s
->transparent_color_index
= -1;
414 gce_flags
= get_byte(f
);
415 s
->gce_delay
= get_le16(f
);
416 gce_transparent_index
= get_byte(f
);
417 if (gce_flags
& 0x01)
418 s
->transparent_color_index
= gce_transparent_index
;
420 s
->transparent_color_index
= -1;
421 s
->gce_disposal
= (gce_flags
>> 2) & 0x7;
423 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
424 gce_flags
, s
->gce_delay
,
425 s
->transparent_color_index
, s
->gce_disposal
);
427 ext_len
= get_byte(f
);
431 /* NOTE: many extension blocks can come after */
433 while (ext_len
!= 0) {
434 for (i
= 0; i
< ext_len
; i
++)
436 ext_len
= get_byte(f
);
438 printf("gif: ext_len1=%d\n", ext_len
);
444 static int gif_read_header1(GifState
*s
)
446 ByteIOContext
*f
= s
->f
;
449 int has_global_palette
;
451 /* read gif signature */
452 ret
= get_buffer(f
, sig
, 6);
455 if (memcmp(sig
, gif87a_sig
, 6) != 0 &&
456 memcmp(sig
, gif89a_sig
, 6) != 0)
459 /* read screen header */
460 s
->transparent_color_index
= -1;
461 s
->screen_width
= get_le16(f
);
462 s
->screen_height
= get_le16(f
);
463 if( (unsigned)s
->screen_width
> 32767
464 || (unsigned)s
->screen_height
> 32767){
465 av_log(NULL
, AV_LOG_ERROR
, "picture size too large\n");
470 s
->color_resolution
= ((v
& 0x70) >> 4) + 1;
471 has_global_palette
= (v
& 0x80);
472 s
->bits_per_pixel
= (v
& 0x07) + 1;
473 s
->background_color_index
= get_byte(f
);
474 get_byte(f
); /* ignored */
476 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
477 s
->screen_width
, s
->screen_height
, s
->bits_per_pixel
,
480 if (has_global_palette
) {
481 n
= 1 << s
->bits_per_pixel
;
482 get_buffer(f
, s
->global_palette
, n
* 3);
487 static int gif_parse_next_image(GifState
*s
)
489 ByteIOContext
*f
= s
->f
;
495 printf("gif: code=%02x '%c'\n", code
, code
);
499 if (gif_read_image(s
) < 0)
508 if (gif_read_extension(s
) < 0)
513 /* error or errneous EOF */
522 static int gif_read_header(AVFormatContext
* s1
,
523 AVFormatParameters
* ap
)
525 GifState
*s
= s1
->priv_data
;
526 ByteIOContext
*f
= s1
->pb
;
530 if (gif_read_header1(s
) < 0)
533 /* allocate image buffer */
534 s
->image_linesize
= s
->screen_width
* 3;
535 s
->image_buf
= av_malloc(s
->screen_height
* s
->image_linesize
);
537 return AVERROR(ENOMEM
);
538 s
->pix_fmt
= PIX_FMT_RGB24
;
539 /* now we are ready: build format streams */
540 st
= av_new_stream(s1
, 0);
544 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
545 st
->codec
->codec_id
= CODEC_ID_RAWVIDEO
;
546 st
->codec
->time_base
.den
= 5;
547 st
->codec
->time_base
.num
= 1;
548 /* XXX: check if screen size is always valid */
549 st
->codec
->width
= s
->screen_width
;
550 st
->codec
->height
= s
->screen_height
;
551 st
->codec
->pix_fmt
= PIX_FMT_RGB24
;
555 static int gif_read_packet(AVFormatContext
* s1
,
558 GifState
*s
= s1
->priv_data
;
561 ret
= gif_parse_next_image(s
);
565 /* XXX: avoid copying */
566 if (av_new_packet(pkt
, s
->screen_width
* s
->screen_height
* 3)) {
569 pkt
->stream_index
= 0;
570 memcpy(pkt
->data
, s
->image_buf
, s
->screen_width
* s
->screen_height
* 3);
574 static int gif_read_close(AVFormatContext
*s1
)
576 GifState
*s
= s1
->priv_data
;
577 av_free(s
->image_buf
);
581 AVInputFormat gif_demuxer
=