]> git.sesse.net Git - ffmpeg/blob - libavcodec/gifdec.c
d5f1c42255e13067fb14815f09d840ec9fa63551
[ffmpeg] / libavcodec / gifdec.c
1 /*
2  * GIF decoder
3  * Copyright (c) 2003 Fabrice Bellard
4  * Copyright (c) 2006 Baptiste Coudurier
5  *
6  * This file is part of Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 //#define DEBUG
24
25 #include "libavutil/imgutils.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "internal.h"
29 #include "lzw.h"
30
31 #define GCE_DISPOSAL_NONE       0
32 #define GCE_DISPOSAL_INPLACE    1
33 #define GCE_DISPOSAL_BACKGROUND 2
34 #define GCE_DISPOSAL_RESTORE    3
35
36 typedef struct GifState {
37     AVFrame picture;
38     int screen_width;
39     int screen_height;
40     int bits_per_pixel;
41     int background_color_index;
42     int transparent_color_index;
43     int color_resolution;
44     uint32_t *image_palette;
45
46     /* after the frame is displayed, the disposal method is used */
47     int gce_disposal;
48     /* delay during which the frame is shown */
49     int gce_delay;
50
51     /* LZW compatible decoder */
52     const uint8_t *bytestream;
53     const uint8_t *bytestream_end;
54     LZWState *lzw;
55
56     /* aux buffers */
57     uint8_t global_palette[256 * 3];
58     uint8_t local_palette[256 * 3];
59
60   AVCodecContext* avctx;
61 } GifState;
62
63 static const uint8_t gif87a_sig[6] = "GIF87a";
64 static const uint8_t gif89a_sig[6] = "GIF89a";
65
66 static int gif_read_image(GifState *s)
67 {
68     int left, top, width, height, bits_per_pixel, code_size, flags;
69     int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
70     uint8_t *ptr, *spal, *palette, *ptr1;
71
72     left = bytestream_get_le16(&s->bytestream);
73     top = bytestream_get_le16(&s->bytestream);
74     width = bytestream_get_le16(&s->bytestream);
75     height = bytestream_get_le16(&s->bytestream);
76     flags = bytestream_get_byte(&s->bytestream);
77     is_interleaved = flags & 0x40;
78     has_local_palette = flags & 0x80;
79     bits_per_pixel = (flags & 0x07) + 1;
80
81     av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
82
83     if (has_local_palette) {
84         bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
85         palette = s->local_palette;
86     } else {
87         palette = s->global_palette;
88         bits_per_pixel = s->bits_per_pixel;
89     }
90
91     /* verify that all the image is inside the screen dimensions */
92     if (left + width > s->screen_width ||
93         top + height > s->screen_height)
94         return AVERROR(EINVAL);
95
96     /* build the palette */
97     n = (1 << bits_per_pixel);
98     spal = palette;
99     for(i = 0; i < n; i++) {
100         s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
101         spal += 3;
102     }
103     for(; i < 256; i++)
104         s->image_palette[i] = (0xffu << 24);
105     /* handle transparency */
106     if (s->transparent_color_index >= 0)
107         s->image_palette[s->transparent_color_index] = 0;
108
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);
113
114     /* read all the image */
115     linesize = s->picture.linesize[0];
116     ptr1 = s->picture.data[0] + top * linesize + left;
117     ptr = ptr1;
118     pass = 0;
119     y1 = 0;
120     for (y = 0; y < height; y++) {
121         ff_lzw_decode(s->lzw, ptr, width);
122         if (is_interleaved) {
123             switch(pass) {
124             default:
125             case 0:
126             case 1:
127                 y1 += 8;
128                 ptr += linesize * 8;
129                 if (y1 >= height) {
130                     y1 = pass ? 2 : 4;
131                     ptr = ptr1 + linesize * y1;
132                     pass++;
133                 }
134                 break;
135             case 2:
136                 y1 += 4;
137                 ptr += linesize * 4;
138                 if (y1 >= height) {
139                     y1 = 1;
140                     ptr = ptr1 + linesize;
141                     pass++;
142                 }
143                 break;
144             case 3:
145                 y1 += 2;
146                 ptr += linesize * 2;
147                 break;
148             }
149         } else {
150             ptr += linesize;
151         }
152     }
153     /* read the garbage data until end marker is found */
154     ff_lzw_decode_tail(s->lzw);
155     s->bytestream = ff_lzw_cur_ptr(s->lzw);
156     return 0;
157 }
158
159 static int gif_read_extension(GifState *s)
160 {
161     int ext_code, ext_len, i, gce_flags, gce_transparent_index;
162
163     /* extension */
164     ext_code = bytestream_get_byte(&s->bytestream);
165     ext_len = bytestream_get_byte(&s->bytestream);
166
167     av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
168
169     switch(ext_code) {
170     case 0xf9:
171         if (ext_len != 4)
172             goto discard_ext;
173         s->transparent_color_index = -1;
174         gce_flags = bytestream_get_byte(&s->bytestream);
175         s->gce_delay = bytestream_get_le16(&s->bytestream);
176         gce_transparent_index = bytestream_get_byte(&s->bytestream);
177         if (gce_flags & 0x01)
178             s->transparent_color_index = gce_transparent_index;
179         else
180             s->transparent_color_index = -1;
181         s->gce_disposal = (gce_flags >> 2) & 0x7;
182
183         av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
184                gce_flags, s->gce_delay,
185                s->transparent_color_index, s->gce_disposal);
186
187         ext_len = bytestream_get_byte(&s->bytestream);
188         break;
189     }
190
191     /* NOTE: many extension blocks can come after */
192  discard_ext:
193     while (ext_len != 0) {
194         for (i = 0; i < ext_len; i++)
195             bytestream_get_byte(&s->bytestream);
196         ext_len = bytestream_get_byte(&s->bytestream);
197
198         av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
199     }
200     return 0;
201 }
202
203 static int gif_read_header1(GifState *s)
204 {
205     uint8_t sig[6];
206     int v, n;
207     int has_global_palette;
208
209     if (s->bytestream_end < s->bytestream + 13)
210         return -1;
211
212     /* read gif signature */
213     bytestream_get_buffer(&s->bytestream, sig, 6);
214     if (memcmp(sig, gif87a_sig, 6) != 0 &&
215         memcmp(sig, gif89a_sig, 6) != 0)
216         return -1;
217
218     /* read screen header */
219     s->transparent_color_index = -1;
220     s->screen_width = bytestream_get_le16(&s->bytestream);
221     s->screen_height = bytestream_get_le16(&s->bytestream);
222     if(   (unsigned)s->screen_width  > 32767
223        || (unsigned)s->screen_height > 32767){
224         av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
225         return -1;
226     }
227
228     v = bytestream_get_byte(&s->bytestream);
229     s->color_resolution = ((v & 0x70) >> 4) + 1;
230     has_global_palette = (v & 0x80);
231     s->bits_per_pixel = (v & 0x07) + 1;
232     s->background_color_index = bytestream_get_byte(&s->bytestream);
233     bytestream_get_byte(&s->bytestream);                /* ignored */
234
235     av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
236            s->screen_width, s->screen_height, s->bits_per_pixel,
237            has_global_palette);
238
239     if (has_global_palette) {
240         n = 1 << s->bits_per_pixel;
241         if (s->bytestream_end < s->bytestream + n * 3)
242             return -1;
243         bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
244     }
245     return 0;
246 }
247
248 static int gif_parse_next_image(GifState *s)
249 {
250     while (s->bytestream < s->bytestream_end) {
251         int code = bytestream_get_byte(&s->bytestream);
252
253         av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
254
255         switch (code) {
256         case ',':
257             return gif_read_image(s);
258         case '!':
259             if (gif_read_extension(s) < 0)
260                 return -1;
261             break;
262         case ';':
263             /* end of image */
264         default:
265             /* error or erroneous EOF */
266             return -1;
267         }
268     }
269     return -1;
270 }
271
272 static av_cold int gif_decode_init(AVCodecContext *avctx)
273 {
274     GifState *s = avctx->priv_data;
275
276     s->avctx = avctx;
277
278     avcodec_get_frame_defaults(&s->picture);
279     avctx->coded_frame= &s->picture;
280     s->picture.data[0] = NULL;
281     ff_lzw_decode_open(&s->lzw);
282     return 0;
283 }
284
285 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
286 {
287     const uint8_t *buf = avpkt->data;
288     int buf_size = avpkt->size;
289     GifState *s = avctx->priv_data;
290     AVFrame *picture = data;
291     int ret;
292
293     s->bytestream = buf;
294     s->bytestream_end = buf + buf_size;
295     if (gif_read_header1(s) < 0)
296         return -1;
297
298     avctx->pix_fmt = AV_PIX_FMT_PAL8;
299     if (av_image_check_size(s->screen_width, s->screen_height, 0, avctx))
300         return -1;
301     avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
302
303     if (s->picture.data[0])
304         avctx->release_buffer(avctx, &s->picture);
305     if (ff_get_buffer(avctx, &s->picture) < 0) {
306         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
307         return -1;
308     }
309     s->image_palette = (uint32_t *)s->picture.data[1];
310     ret = gif_parse_next_image(s);
311     if (ret < 0)
312         return ret;
313
314     *picture = s->picture;
315     *data_size = sizeof(AVPicture);
316     return s->bytestream - buf;
317 }
318
319 static av_cold int gif_decode_close(AVCodecContext *avctx)
320 {
321     GifState *s = avctx->priv_data;
322
323     ff_lzw_decode_close(&s->lzw);
324     if(s->picture.data[0])
325         avctx->release_buffer(avctx, &s->picture);
326     return 0;
327 }
328
329 AVCodec ff_gif_decoder = {
330     .name           = "gif",
331     .type           = AVMEDIA_TYPE_VIDEO,
332     .id             = AV_CODEC_ID_GIF,
333     .priv_data_size = sizeof(GifState),
334     .init           = gif_decode_init,
335     .close          = gif_decode_close,
336     .decode         = gif_decode_frame,
337     .capabilities   = CODEC_CAP_DR1,
338     .long_name      = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
339 };