]> git.sesse.net Git - ffmpeg/blob - libavcodec/gifdec.c
arm: Add VFP-accelerated version of qmf_32_subbands
[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 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "lzw.h"
28
29 #define GCE_DISPOSAL_NONE       0
30 #define GCE_DISPOSAL_INPLACE    1
31 #define GCE_DISPOSAL_BACKGROUND 2
32 #define GCE_DISPOSAL_RESTORE    3
33
34 typedef struct GifState {
35     int screen_width;
36     int screen_height;
37     int bits_per_pixel;
38     int background_color_index;
39     int transparent_color_index;
40     int color_resolution;
41     uint32_t *image_palette;
42
43     /* after the frame is displayed, the disposal method is used */
44     int gce_disposal;
45     /* delay during which the frame is shown */
46     int gce_delay;
47
48     /* LZW compatible decoder */
49     const uint8_t *bytestream;
50     const uint8_t *bytestream_end;
51     LZWState *lzw;
52
53     /* aux buffers */
54     uint8_t global_palette[256 * 3];
55     uint8_t local_palette[256 * 3];
56
57   AVCodecContext* avctx;
58 } GifState;
59
60 static const uint8_t gif87a_sig[6] = "GIF87a";
61 static const uint8_t gif89a_sig[6] = "GIF89a";
62
63 static int gif_read_image(GifState *s, AVFrame *frame)
64 {
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;
68
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;
77
78     av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
79
80     if (has_local_palette) {
81         bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
82         palette = s->local_palette;
83     } else {
84         palette = s->global_palette;
85         bits_per_pixel = s->bits_per_pixel;
86     }
87
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);
92
93     /* build the palette */
94     n = (1 << bits_per_pixel);
95     spal = palette;
96     for(i = 0; i < n; i++) {
97         s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
98         spal += 3;
99     }
100     for(; i < 256; i++)
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;
105
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);
110
111     /* read all the image */
112     linesize = frame->linesize[0];
113     ptr1 = frame->data[0] + top * linesize + left;
114     ptr = ptr1;
115     pass = 0;
116     y1 = 0;
117     for (y = 0; y < height; y++) {
118         ff_lzw_decode(s->lzw, ptr, width);
119         if (is_interleaved) {
120             switch(pass) {
121             default:
122             case 0:
123             case 1:
124                 y1 += 8;
125                 ptr += linesize * 8;
126                 if (y1 >= height) {
127                     y1 = pass ? 2 : 4;
128                     ptr = ptr1 + linesize * y1;
129                     pass++;
130                 }
131                 break;
132             case 2:
133                 y1 += 4;
134                 ptr += linesize * 4;
135                 if (y1 >= height) {
136                     y1 = 1;
137                     ptr = ptr1 + linesize;
138                     pass++;
139                 }
140                 break;
141             case 3:
142                 y1 += 2;
143                 ptr += linesize * 2;
144                 break;
145             }
146         } else {
147             ptr += linesize;
148         }
149     }
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);
153     return 0;
154 }
155
156 static int gif_read_extension(GifState *s)
157 {
158     int ext_code, ext_len, i, gce_flags, gce_transparent_index;
159
160     /* extension */
161     ext_code = bytestream_get_byte(&s->bytestream);
162     ext_len = bytestream_get_byte(&s->bytestream);
163
164     av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
165
166     switch(ext_code) {
167     case 0xf9:
168         if (ext_len != 4)
169             goto discard_ext;
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;
176         else
177             s->transparent_color_index = -1;
178         s->gce_disposal = (gce_flags >> 2) & 0x7;
179
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);
183
184         ext_len = bytestream_get_byte(&s->bytestream);
185         break;
186     }
187
188     /* NOTE: many extension blocks can come after */
189  discard_ext:
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);
194
195         av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
196     }
197     return 0;
198 }
199
200 static int gif_read_header1(GifState *s)
201 {
202     uint8_t sig[6];
203     int v, n;
204     int has_global_palette;
205
206     if (s->bytestream_end < s->bytestream + 13)
207         return AVERROR_INVALIDDATA;
208
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;
214
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;
223     }
224
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 */
231
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,
234            has_global_palette);
235
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);
241     }
242     return 0;
243 }
244
245 static int gif_parse_next_image(GifState *s, AVFrame *frame)
246 {
247     while (s->bytestream < s->bytestream_end) {
248         int code = bytestream_get_byte(&s->bytestream);
249         int ret;
250
251         av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
252
253         switch (code) {
254         case ',':
255             return gif_read_image(s, frame);
256         case '!':
257             if ((ret = gif_read_extension(s)) < 0)
258                 return ret;
259             break;
260         case ';':
261             /* end of image */
262         default:
263             /* error or erroneous EOF */
264             return AVERROR_INVALIDDATA;
265         }
266     }
267     return AVERROR_INVALIDDATA;
268 }
269
270 static av_cold int gif_decode_init(AVCodecContext *avctx)
271 {
272     GifState *s = avctx->priv_data;
273
274     s->avctx = avctx;
275
276     ff_lzw_decode_open(&s->lzw);
277     return 0;
278 }
279
280 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
281                             AVPacket *avpkt)
282 {
283     const uint8_t *buf = avpkt->data;
284     int buf_size = avpkt->size;
285     GifState *s = avctx->priv_data;
286     AVFrame *picture = data;
287     int ret;
288
289     s->bytestream = buf;
290     s->bytestream_end = buf + buf_size;
291     if ((ret = gif_read_header1(s)) < 0)
292         return ret;
293
294     avctx->pix_fmt = AV_PIX_FMT_PAL8;
295     if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
296         return ret;
297     avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
298
299     if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
300         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
301         return ret;
302     }
303     s->image_palette = (uint32_t *)picture->data[1];
304     ret = gif_parse_next_image(s, picture);
305     if (ret < 0)
306         return ret;
307
308     *got_frame = 1;
309     return s->bytestream - buf;
310 }
311
312 static av_cold int gif_decode_close(AVCodecContext *avctx)
313 {
314     GifState *s = avctx->priv_data;
315
316     ff_lzw_decode_close(&s->lzw);
317     return 0;
318 }
319
320 AVCodec ff_gif_decoder = {
321     .name           = "gif",
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)"),
330 };