3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2006 Baptiste Coudurier
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/imgutils.h"
25 #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 {
38 int background_color_index;
39 int transparent_color_index;
41 uint32_t *image_palette;
43 /* after the frame is displayed, the disposal method is used */
45 /* delay during which the frame is shown */
48 /* LZW compatible decoder */
49 const uint8_t *bytestream;
50 const uint8_t *bytestream_end;
54 uint8_t global_palette[256 * 3];
55 uint8_t local_palette[256 * 3];
57 AVCodecContext* avctx;
60 static const uint8_t gif87a_sig[6] = "GIF87a";
61 static const uint8_t gif89a_sig[6] = "GIF89a";
63 static int gif_read_image(GifState *s, AVFrame *frame)
65 int left, top, width, height, bits_per_pixel, code_size, flags;
66 int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
67 uint8_t *ptr, *spal, *palette, *ptr1;
69 left = bytestream_get_le16(&s->bytestream);
70 top = bytestream_get_le16(&s->bytestream);
71 width = bytestream_get_le16(&s->bytestream);
72 height = bytestream_get_le16(&s->bytestream);
73 flags = bytestream_get_byte(&s->bytestream);
74 is_interleaved = flags & 0x40;
75 has_local_palette = flags & 0x80;
76 bits_per_pixel = (flags & 0x07) + 1;
78 av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
80 if (has_local_palette) {
81 bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
82 palette = s->local_palette;
84 palette = s->global_palette;
85 bits_per_pixel = s->bits_per_pixel;
88 /* verify that all the image is inside the screen dimensions */
89 if (left + width > s->screen_width ||
90 top + height > s->screen_height)
91 return AVERROR(EINVAL);
93 /* build the palette */
94 n = (1 << bits_per_pixel);
96 for(i = 0; i < n; i++) {
97 s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
101 s->image_palette[i] = (0xffu << 24);
102 /* handle transparency */
103 if (s->transparent_color_index >= 0)
104 s->image_palette[s->transparent_color_index] = 0;
106 /* now get the image data */
107 code_size = bytestream_get_byte(&s->bytestream);
108 ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
109 s->bytestream_end - s->bytestream, FF_LZW_GIF);
111 /* read all the image */
112 linesize = frame->linesize[0];
113 ptr1 = frame->data[0] + top * linesize + left;
117 for (y = 0; y < height; y++) {
118 ff_lzw_decode(s->lzw, ptr, width);
119 if (is_interleaved) {
128 ptr = ptr1 + linesize * y1;
137 ptr = ptr1 + linesize;
150 /* read the garbage data until end marker is found */
151 ff_lzw_decode_tail(s->lzw);
152 s->bytestream = ff_lzw_cur_ptr(s->lzw);
156 static int gif_read_extension(GifState *s)
158 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
161 ext_code = bytestream_get_byte(&s->bytestream);
162 ext_len = bytestream_get_byte(&s->bytestream);
164 av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
170 s->transparent_color_index = -1;
171 gce_flags = bytestream_get_byte(&s->bytestream);
172 s->gce_delay = bytestream_get_le16(&s->bytestream);
173 gce_transparent_index = bytestream_get_byte(&s->bytestream);
174 if (gce_flags & 0x01)
175 s->transparent_color_index = gce_transparent_index;
177 s->transparent_color_index = -1;
178 s->gce_disposal = (gce_flags >> 2) & 0x7;
180 av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
181 gce_flags, s->gce_delay,
182 s->transparent_color_index, s->gce_disposal);
184 ext_len = bytestream_get_byte(&s->bytestream);
188 /* NOTE: many extension blocks can come after */
190 while (ext_len != 0) {
191 for (i = 0; i < ext_len; i++)
192 bytestream_get_byte(&s->bytestream);
193 ext_len = bytestream_get_byte(&s->bytestream);
195 av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
200 static int gif_read_header1(GifState *s)
204 int has_global_palette;
206 if (s->bytestream_end < s->bytestream + 13)
207 return AVERROR_INVALIDDATA;
209 /* read gif signature */
210 bytestream_get_buffer(&s->bytestream, sig, 6);
211 if (memcmp(sig, gif87a_sig, 6) != 0 &&
212 memcmp(sig, gif89a_sig, 6) != 0)
213 return AVERROR_INVALIDDATA;
215 /* read screen header */
216 s->transparent_color_index = -1;
217 s->screen_width = bytestream_get_le16(&s->bytestream);
218 s->screen_height = bytestream_get_le16(&s->bytestream);
219 if( (unsigned)s->screen_width > 32767
220 || (unsigned)s->screen_height > 32767){
221 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
222 return AVERROR_INVALIDDATA;
225 v = bytestream_get_byte(&s->bytestream);
226 s->color_resolution = ((v & 0x70) >> 4) + 1;
227 has_global_palette = (v & 0x80);
228 s->bits_per_pixel = (v & 0x07) + 1;
229 s->background_color_index = bytestream_get_byte(&s->bytestream);
230 bytestream_get_byte(&s->bytestream); /* ignored */
232 av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
233 s->screen_width, s->screen_height, s->bits_per_pixel,
236 if (has_global_palette) {
237 n = 1 << s->bits_per_pixel;
238 if (s->bytestream_end < s->bytestream + n * 3)
239 return AVERROR_INVALIDDATA;
240 bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
245 static int gif_parse_next_image(GifState *s, AVFrame *frame)
247 while (s->bytestream < s->bytestream_end) {
248 int code = bytestream_get_byte(&s->bytestream);
251 av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
255 return gif_read_image(s, frame);
257 if ((ret = gif_read_extension(s)) < 0)
263 /* error or erroneous EOF */
264 return AVERROR_INVALIDDATA;
267 return AVERROR_INVALIDDATA;
270 static av_cold int gif_decode_init(AVCodecContext *avctx)
272 GifState *s = avctx->priv_data;
276 ff_lzw_decode_open(&s->lzw);
280 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
283 const uint8_t *buf = avpkt->data;
284 int buf_size = avpkt->size;
285 GifState *s = avctx->priv_data;
286 AVFrame *picture = data;
290 s->bytestream_end = buf + buf_size;
291 if ((ret = gif_read_header1(s)) < 0)
294 avctx->pix_fmt = AV_PIX_FMT_PAL8;
295 if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
297 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
299 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
300 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
303 s->image_palette = (uint32_t *)picture->data[1];
304 ret = gif_parse_next_image(s, picture);
309 return s->bytestream - buf;
312 static av_cold int gif_decode_close(AVCodecContext *avctx)
314 GifState *s = avctx->priv_data;
316 ff_lzw_decode_close(&s->lzw);
320 AVCodec ff_gif_decoder = {
322 .type = AVMEDIA_TYPE_VIDEO,
323 .id = AV_CODEC_ID_GIF,
324 .priv_data_size = sizeof(GifState),
325 .init = gif_decode_init,
326 .close = gif_decode_close,
327 .decode = gif_decode_frame,
328 .capabilities = CODEC_CAP_DR1,
329 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),