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