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 GCE_DISPOSAL_NONE 0
30 #define GCE_DISPOSAL_INPLACE 1
31 #define GCE_DISPOSAL_BACKGROUND 2
32 #define GCE_DISPOSAL_RESTORE 3
34 typedef struct GifState
{
39 int background_color_index
;
40 int transparent_color_index
;
42 uint32_t *image_palette
;
44 /* after the frame is displayed, the disposal method is used */
46 /* delay during which the frame is shown */
49 /* LZW compatible decoder */
51 uint8_t *bytestream_end
;
55 uint8_t global_palette
[256 * 3];
56 uint8_t local_palette
[256 * 3];
58 AVCodecContext
* avctx
;
61 static const uint8_t gif87a_sig
[6] = "GIF87a";
62 static const uint8_t gif89a_sig
[6] = "GIF89a";
64 static int gif_read_image(GifState
*s
)
66 int left
, top
, width
, height
, bits_per_pixel
, code_size
, flags
;
67 int is_interleaved
, has_local_palette
, y
, pass
, y1
, linesize
, n
, i
;
68 uint8_t *ptr
, *spal
, *palette
, *ptr1
;
70 left
= bytestream_get_le16(&s
->bytestream
);
71 top
= bytestream_get_le16(&s
->bytestream
);
72 width
= bytestream_get_le16(&s
->bytestream
);
73 height
= bytestream_get_le16(&s
->bytestream
);
74 flags
= bytestream_get_byte(&s
->bytestream
);
75 is_interleaved
= flags
& 0x40;
76 has_local_palette
= flags
& 0x80;
77 bits_per_pixel
= (flags
& 0x07) + 1;
79 dprintf(s
->avctx
, "gif: image x=%d y=%d w=%d h=%d\n", left
, top
, width
, height
);
82 if (has_local_palette
) {
83 bytestream_get_buffer(&s
->bytestream
, s
->local_palette
, 3 * (1 << bits_per_pixel
));
84 palette
= s
->local_palette
;
86 palette
= s
->global_palette
;
87 bits_per_pixel
= s
->bits_per_pixel
;
90 /* verify that all the image is inside the screen dimensions */
91 if (left
+ width
> s
->screen_width
||
92 top
+ height
> s
->screen_height
)
93 return AVERROR(EINVAL
);
95 /* build the palette */
96 n
= (1 << bits_per_pixel
);
98 for(i
= 0; i
< n
; i
++) {
99 s
->image_palette
[i
] = (0xff << 24) |
100 (spal
[0] << 16) | (spal
[1] << 8) | (spal
[2]);
104 s
->image_palette
[i
] = (0xff << 24);
105 /* handle transparency */
106 if (s
->transparent_color_index
>= 0)
107 s
->image_palette
[s
->transparent_color_index
] = 0;
109 /* now get the image data */
110 code_size
= bytestream_get_byte(&s
->bytestream
);
111 ff_lzw_decode_init(s
->lzw
, code_size
, s
->bytestream
,
112 s
->bytestream_end
- s
->bytestream
, FF_LZW_GIF
);
114 /* read all the image */
115 linesize
= s
->picture
.linesize
[0];
116 ptr1
= s
->picture
.data
[0] + top
* linesize
+ left
;
120 for (y
= 0; y
< height
; y
++) {
121 ff_lzw_decode(s
->lzw
, ptr
, width
);
122 if (is_interleaved
) {
132 ptr
= ptr1
+ linesize
* 4;
134 ptr
= ptr1
+ linesize
* 2;
143 ptr
= ptr1
+ linesize
;
156 /* read the garbage data until end marker is found */
157 ff_lzw_decode_tail(s
->lzw
);
158 s
->bytestream
= ff_lzw_cur_ptr(s
->lzw
);
162 static int gif_read_extension(GifState
*s
)
164 int ext_code
, ext_len
, i
, gce_flags
, gce_transparent_index
;
167 ext_code
= bytestream_get_byte(&s
->bytestream
);
168 ext_len
= bytestream_get_byte(&s
->bytestream
);
170 dprintf(s
->avctx
, "gif: ext_code=0x%x len=%d\n", ext_code
, ext_len
);
176 s
->transparent_color_index
= -1;
177 gce_flags
= bytestream_get_byte(&s
->bytestream
);
178 s
->gce_delay
= bytestream_get_le16(&s
->bytestream
);
179 gce_transparent_index
= bytestream_get_byte(&s
->bytestream
);
180 if (gce_flags
& 0x01)
181 s
->transparent_color_index
= gce_transparent_index
;
183 s
->transparent_color_index
= -1;
184 s
->gce_disposal
= (gce_flags
>> 2) & 0x7;
186 dprintf(s
->avctx
, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
187 gce_flags
, s
->gce_delay
,
188 s
->transparent_color_index
, s
->gce_disposal
);
190 ext_len
= bytestream_get_byte(&s
->bytestream
);
194 /* NOTE: many extension blocks can come after */
196 while (ext_len
!= 0) {
197 for (i
= 0; i
< ext_len
; i
++)
198 bytestream_get_byte(&s
->bytestream
);
199 ext_len
= bytestream_get_byte(&s
->bytestream
);
201 dprintf(s
->avctx
, "gif: ext_len1=%d\n", ext_len
);
207 static int gif_read_header1(GifState
*s
)
211 int has_global_palette
;
213 if (s
->bytestream_end
< s
->bytestream
+ 13)
216 /* read gif signature */
217 bytestream_get_buffer(&s
->bytestream
, sig
, 6);
218 if (memcmp(sig
, gif87a_sig
, 6) != 0 &&
219 memcmp(sig
, gif89a_sig
, 6) != 0)
222 /* read screen header */
223 s
->transparent_color_index
= -1;
224 s
->screen_width
= bytestream_get_le16(&s
->bytestream
);
225 s
->screen_height
= bytestream_get_le16(&s
->bytestream
);
226 if( (unsigned)s
->screen_width
> 32767
227 || (unsigned)s
->screen_height
> 32767){
228 av_log(NULL
, AV_LOG_ERROR
, "picture size too large\n");
232 v
= bytestream_get_byte(&s
->bytestream
);
233 s
->color_resolution
= ((v
& 0x70) >> 4) + 1;
234 has_global_palette
= (v
& 0x80);
235 s
->bits_per_pixel
= (v
& 0x07) + 1;
236 s
->background_color_index
= bytestream_get_byte(&s
->bytestream
);
237 bytestream_get_byte(&s
->bytestream
); /* ignored */
239 dprintf(s
->avctx
, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
240 s
->screen_width
, s
->screen_height
, s
->bits_per_pixel
,
243 if (has_global_palette
) {
244 n
= 1 << s
->bits_per_pixel
;
245 if (s
->bytestream_end
< s
->bytestream
+ n
* 3)
247 bytestream_get_buffer(&s
->bytestream
, s
->global_palette
, n
* 3);
252 static int gif_parse_next_image(GifState
*s
)
254 while (s
->bytestream
< s
->bytestream_end
) {
255 int code
= bytestream_get_byte(&s
->bytestream
);
257 dprintf(s
->avctx
, "gif: code=%02x '%c'\n", code
, code
);
261 return gif_read_image(s
);
263 if (gif_read_extension(s
) < 0)
269 /* error or erroneous EOF */
276 static int gif_decode_init(AVCodecContext
*avctx
)
278 GifState
*s
= avctx
->priv_data
;
282 avcodec_get_frame_defaults(&s
->picture
);
283 avctx
->coded_frame
= &s
->picture
;
284 s
->picture
.data
[0] = NULL
;
285 ff_lzw_decode_open(&s
->lzw
);
289 static int gif_decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
, uint8_t *buf
, int buf_size
)
291 GifState
*s
= avctx
->priv_data
;
292 AVFrame
*picture
= data
;
296 s
->bytestream_end
= buf
+ buf_size
;
297 if (gif_read_header1(s
) < 0)
300 avctx
->pix_fmt
= PIX_FMT_PAL8
;
301 if (avcodec_check_dimensions(avctx
, s
->screen_width
, s
->screen_height
))
303 avcodec_set_dimensions(avctx
, s
->screen_width
, s
->screen_height
);
305 if (s
->picture
.data
[0])
306 avctx
->release_buffer(avctx
, &s
->picture
);
307 if (avctx
->get_buffer(avctx
, &s
->picture
) < 0) {
308 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
311 s
->image_palette
= (uint32_t *)s
->picture
.data
[1];
312 ret
= gif_parse_next_image(s
);
316 *picture
= s
->picture
;
317 *data_size
= sizeof(AVPicture
);
318 return s
->bytestream
- buf
;
321 static int gif_decode_close(AVCodecContext
*avctx
)
323 GifState
*s
= avctx
->priv_data
;
325 ff_lzw_decode_close(&s
->lzw
);
326 if(s
->picture
.data
[0])
327 avctx
->release_buffer(avctx
, &s
->picture
);
331 AVCodec gif_decoder
= {