3 * Copyright (c) 2003 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 int gif_write(ByteIOContext
*pb
, AVImageInfo
*info
);
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 int gif_image_probe(AVProbeData
* pd
)
147 if (pd
->buf_size
>= 24 &&
148 (memcmp(pd
->buf
, gif87a_sig
, 6) == 0 ||
149 memcmp(pd
->buf
, gif89a_sig
, 6) == 0))
150 return AVPROBE_SCORE_MAX
- 1;
156 static void GLZWDecodeInit(GifState
* s
, int csize
)
167 s
->cursize
= s
->codesize
+ 1;
168 s
->curmask
= mask
[s
->cursize
];
169 s
->top_slot
= 1 << s
->cursize
;
170 s
->clear_code
= 1 << s
->codesize
;
171 s
->end_code
= s
->clear_code
+ 1;
172 s
->slot
= s
->newcodes
= s
->clear_code
+ 2;
178 static inline int GetCode(GifState
* s
)
183 while (s
->bbits
< s
->cursize
) {
185 if (ptr
>= s
->ebuf
) {
186 if (!s
->eob_reached
) {
187 sizbuf
= get_byte(s
->f
);
188 s
->ebuf
= s
->buf
+ sizbuf
;
191 get_buffer(s
->f
, s
->buf
, sizbuf
);
198 s
->bbuf
|= ptr
[0] << s
->bbits
;
203 c
= s
->bbuf
& s
->curmask
;
204 s
->bbuf
>>= s
->cursize
;
205 s
->bbits
-= s
->cursize
;
209 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
210 written by Steven A. Bennett in 1987. */
211 /* return the number of byte decoded */
212 static int GLZWDecode(GifState
* s
, uint8_t * buf
, int len
)
214 int l
, c
, code
, oc
, fc
;
225 while (sp
> s
->stack
) {
233 if (c
== s
->end_code
) {
236 } else if (c
== s
->clear_code
) {
237 s
->cursize
= s
->codesize
+ 1;
238 s
->curmask
= mask
[s
->cursize
];
239 s
->slot
= s
->newcodes
;
240 s
->top_slot
= 1 << s
->cursize
;
241 while ((c
= GetCode(s
)) == s
->clear_code
);
242 if (c
== s
->end_code
) {
255 if (code
>= s
->slot
) {
259 while (code
>= s
->newcodes
) {
260 *sp
++ = s
->suffix
[code
];
261 code
= s
->prefix
[code
];
264 if (s
->slot
< s
->top_slot
) {
265 s
->suffix
[s
->slot
] = fc
= code
;
266 s
->prefix
[s
->slot
++] = oc
;
269 if (s
->slot
>= s
->top_slot
) {
270 if (s
->cursize
< MAXBITS
) {
272 s
->curmask
= mask
[++s
->cursize
];
275 while (sp
> s
->stack
) {
289 static int gif_read_image(GifState
*s
)
291 ByteIOContext
*f
= s
->f
;
292 int left
, top
, width
, height
, bits_per_pixel
, code_size
, flags
;
293 int is_interleaved
, has_local_palette
, y
, x
, pass
, y1
, linesize
, n
, i
;
294 uint8_t *ptr
, *line
, *d
, *spal
, *palette
, *sptr
, *ptr1
;
299 height
= get_le16(f
);
301 is_interleaved
= flags
& 0x40;
302 has_local_palette
= flags
& 0x80;
303 bits_per_pixel
= (flags
& 0x07) + 1;
305 printf("gif: image x=%d y=%d w=%d h=%d\n", left
, top
, width
, height
);
308 if (has_local_palette
) {
309 get_buffer(f
, s
->local_palette
, 3 * (1 << bits_per_pixel
));
310 palette
= s
->local_palette
;
312 palette
= s
->global_palette
;
313 bits_per_pixel
= s
->bits_per_pixel
;
316 /* verify that all the image is inside the screen dimensions */
317 if (left
+ width
> s
->screen_width
||
318 top
+ height
> s
->screen_height
)
321 /* build the palette */
322 if (s
->pix_fmt
== PIX_FMT_RGB24
) {
323 line
= av_malloc(width
);
327 n
= (1 << bits_per_pixel
);
329 for(i
= 0; i
< n
; i
++) {
330 s
->image_palette
[i
] = (0xff << 24) |
331 (spal
[0] << 16) | (spal
[1] << 8) | (spal
[2]);
335 s
->image_palette
[i
] = (0xff << 24);
336 /* handle transparency */
337 if (s
->transparent_color_index
>= 0)
338 s
->image_palette
[s
->transparent_color_index
] = 0;
342 /* now get the image data */
344 code_size
= get_byte(f
);
345 GLZWDecodeInit(s
, code_size
);
347 /* read all the image */
348 linesize
= s
->image_linesize
;
349 ptr1
= s
->image_buf
+ top
* linesize
+ (left
* 3);
353 for (y
= 0; y
< height
; y
++) {
354 if (s
->pix_fmt
== PIX_FMT_RGB24
) {
355 /* transcode to RGB24 */
356 GLZWDecode(s
, line
, width
);
359 for(x
= 0; x
< width
; x
++) {
360 spal
= palette
+ sptr
[0] * 3;
368 GLZWDecode(s
, ptr
, width
);
370 if (is_interleaved
) {
380 ptr
= ptr1
+ linesize
* 4;
382 ptr
= ptr1
+ linesize
* 2;
391 ptr
= ptr1
+ linesize
;
406 /* read the garbage data until end marker is found */
407 while (!s
->eob_reached
)
412 static int gif_read_extension(GifState
*s
)
414 ByteIOContext
*f
= s
->f
;
415 int ext_code
, ext_len
, i
, gce_flags
, gce_transparent_index
;
418 ext_code
= get_byte(f
);
419 ext_len
= get_byte(f
);
421 printf("gif: ext_code=0x%x len=%d\n", ext_code
, ext_len
);
427 s
->transparent_color_index
= -1;
428 gce_flags
= get_byte(f
);
429 s
->gce_delay
= get_le16(f
);
430 gce_transparent_index
= get_byte(f
);
431 if (gce_flags
& 0x01)
432 s
->transparent_color_index
= gce_transparent_index
;
434 s
->transparent_color_index
= -1;
435 s
->gce_disposal
= (gce_flags
>> 2) & 0x7;
437 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
438 gce_flags
, s
->gce_delay
,
439 s
->transparent_color_index
, s
->gce_disposal
);
441 ext_len
= get_byte(f
);
445 /* NOTE: many extension blocks can come after */
447 while (ext_len
!= 0) {
448 for (i
= 0; i
< ext_len
; i
++)
450 ext_len
= get_byte(f
);
452 printf("gif: ext_len1=%d\n", ext_len
);
458 static int gif_read_header1(GifState
*s
)
460 ByteIOContext
*f
= s
->f
;
463 int has_global_palette
;
465 /* read gif signature */
466 ret
= get_buffer(f
, sig
, 6);
469 if (memcmp(sig
, gif87a_sig
, 6) != 0 &&
470 memcmp(sig
, gif89a_sig
, 6) != 0)
473 /* read screen header */
474 s
->transparent_color_index
= -1;
475 s
->screen_width
= get_le16(f
);
476 s
->screen_height
= get_le16(f
);
477 if( (unsigned)s
->screen_width
> 32767
478 || (unsigned)s
->screen_height
> 32767){
479 av_log(NULL
, AV_LOG_ERROR
, "picture size too large\n");
484 s
->color_resolution
= ((v
& 0x70) >> 4) + 1;
485 has_global_palette
= (v
& 0x80);
486 s
->bits_per_pixel
= (v
& 0x07) + 1;
487 s
->background_color_index
= get_byte(f
);
488 get_byte(f
); /* ignored */
490 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
491 s
->screen_width
, s
->screen_height
, s
->bits_per_pixel
,
494 if (has_global_palette
) {
495 n
= 1 << s
->bits_per_pixel
;
496 get_buffer(f
, s
->global_palette
, n
* 3);
501 static int gif_parse_next_image(GifState
*s
)
503 ByteIOContext
*f
= s
->f
;
509 printf("gif: code=%02x '%c'\n", code
, code
);
513 if (gif_read_image(s
) < 0)
522 if (gif_read_extension(s
) < 0)
527 /* error or errneous EOF */
536 static int gif_read_header(AVFormatContext
* s1
,
537 AVFormatParameters
* ap
)
539 GifState
*s
= s1
->priv_data
;
540 ByteIOContext
*f
= &s1
->pb
;
544 if (gif_read_header1(s
) < 0)
547 /* allocate image buffer */
548 s
->image_linesize
= s
->screen_width
* 3;
549 s
->image_buf
= av_malloc(s
->screen_height
* s
->image_linesize
);
552 s
->pix_fmt
= PIX_FMT_RGB24
;
553 /* now we are ready: build format streams */
554 st
= av_new_stream(s1
, 0);
558 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
559 st
->codec
->codec_id
= CODEC_ID_RAWVIDEO
;
560 st
->codec
->time_base
.den
= 5;
561 st
->codec
->time_base
.num
= 1;
562 /* XXX: check if screen size is always valid */
563 st
->codec
->width
= s
->screen_width
;
564 st
->codec
->height
= s
->screen_height
;
565 st
->codec
->pix_fmt
= PIX_FMT_RGB24
;
569 static int gif_read_packet(AVFormatContext
* s1
,
572 GifState
*s
= s1
->priv_data
;
575 ret
= gif_parse_next_image(s
);
579 /* XXX: avoid copying */
580 if (av_new_packet(pkt
, s
->screen_width
* s
->screen_height
* 3)) {
583 pkt
->stream_index
= 0;
584 memcpy(pkt
->data
, s
->image_buf
, s
->screen_width
* s
->screen_height
* 3);
588 static int gif_read_close(AVFormatContext
*s1
)
590 GifState
*s
= s1
->priv_data
;
591 av_free(s
->image_buf
);
595 /* read gif as image */
596 static int gif_read(ByteIOContext
*f
,
597 int (*alloc_cb
)(void *opaque
, AVImageInfo
*info
), void *opaque
)
599 GifState s1
, *s
= &s1
;
600 AVImageInfo info1
, *info
= &info1
;
603 memset(s
, 0, sizeof(GifState
));
605 if (gif_read_header1(s
) < 0)
607 info
->width
= s
->screen_width
;
608 info
->height
= s
->screen_height
;
609 info
->pix_fmt
= PIX_FMT_PAL8
;
610 ret
= alloc_cb(opaque
, info
);
613 s
->image_buf
= info
->pict
.data
[0];
614 s
->image_linesize
= info
->pict
.linesize
[0];
615 s
->image_palette
= (uint32_t *)info
->pict
.data
[1];
617 if (gif_parse_next_image(s
) < 0)
622 AVInputFormat gif_demuxer
=
633 AVImageFormat gif_image_format
= {
639 #ifdef CONFIG_GIF_MUXER