Statistics
| Branch: | Revision:

ffmpeg / libavcodec / gifdec.c @ ad1ea1f1

History | View | Annotate | Download (9.88 KB)

1 06d392a7 Baptiste Coudurier
/*
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 "avcodec.h"
26
#include "bytestream.h"
27 ffc5430b Kostya Shishkov
#include "lzw.h"
28 06d392a7 Baptiste Coudurier
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 d54d396d Baptiste Coudurier
    AVFrame picture;
36 06d392a7 Baptiste Coudurier
    int screen_width;
37
    int screen_height;
38
    int bits_per_pixel;
39
    int background_color_index;
40
    int transparent_color_index;
41
    int color_resolution;
42 d54d396d Baptiste Coudurier
    uint32_t *image_palette;
43 06d392a7 Baptiste Coudurier
44
    /* after the frame is displayed, the disposal method is used */
45
    int gce_disposal;
46
    /* delay during which the frame is shown */
47
    int gce_delay;
48
49
    /* LZW compatible decoder */
50
    uint8_t *bytestream;
51 7a28b771 Baptiste Coudurier
    uint8_t *bytestream_end;
52 ffc5430b Kostya Shishkov
    LZWState *lzw;
53 06d392a7 Baptiste Coudurier
54
    /* aux buffers */
55
    uint8_t global_palette[256 * 3];
56
    uint8_t local_palette[256 * 3];
57 318c5e05 Michel Bardiaux
58
  AVCodecContext* avctx;
59 06d392a7 Baptiste Coudurier
} GifState;
60
61
static const uint8_t gif87a_sig[6] = "GIF87a";
62
static const uint8_t gif89a_sig[6] = "GIF89a";
63
64
static int gif_read_image(GifState *s)
65
{
66
    int left, top, width, height, bits_per_pixel, code_size, flags;
67 a7fb3a96 Baptiste Coudurier
    int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
68 bc52b0c5 Baptiste Coudurier
    uint8_t *ptr, *spal, *palette, *ptr1;
69 06d392a7 Baptiste Coudurier
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;
78
#ifdef DEBUG
79 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
80 06d392a7 Baptiste Coudurier
#endif
81
82
    if (has_local_palette) {
83
        bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
84
        palette = s->local_palette;
85
    } else {
86
        palette = s->global_palette;
87
        bits_per_pixel = s->bits_per_pixel;
88
    }
89
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 8fa36ae0 Fran├žois Revol
        return AVERROR(EINVAL);
94 06d392a7 Baptiste Coudurier
95
    /* build the palette */
96 47babca8 Baptiste Coudurier
    n = (1 << bits_per_pixel);
97
    spal = palette;
98
    for(i = 0; i < n; i++) {
99
        s->image_palette[i] = (0xff << 24) |
100
            (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
101
        spal += 3;
102
    }
103
    for(; i < 256; i++)
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;
108 06d392a7 Baptiste Coudurier
109
    /* now get the image data */
110
    code_size = bytestream_get_byte(&s->bytestream);
111 10d4899c Baptiste Coudurier
    ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
112
                       s->bytestream_end - s->bytestream, FF_LZW_GIF);
113 06d392a7 Baptiste Coudurier
114
    /* read all the image */
115 d54d396d Baptiste Coudurier
    linesize = s->picture.linesize[0];
116 b190aa89 Baptiste Coudurier
    ptr1 = s->picture.data[0] + top * linesize + left;
117 06d392a7 Baptiste Coudurier
    ptr = ptr1;
118
    pass = 0;
119
    y1 = 0;
120
    for (y = 0; y < height; y++) {
121 ffc5430b Kostya Shishkov
        ff_lzw_decode(s->lzw, ptr, width);
122 06d392a7 Baptiste Coudurier
        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 = 4;
131
                    if (pass == 0)
132
                        ptr = ptr1 + linesize * 4;
133
                    else
134
                        ptr = ptr1 + linesize * 2;
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 ffc5430b Kostya Shishkov
    ff_lzw_decode_tail(s->lzw);
158
    s->bytestream = ff_lzw_cur_ptr(s->lzw);
159 06d392a7 Baptiste Coudurier
    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
#ifdef DEBUG
170 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
171 06d392a7 Baptiste Coudurier
#endif
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
#ifdef DEBUG
186 318c5e05 Michel Bardiaux
        dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
187 06d392a7 Baptiste Coudurier
               gce_flags, s->gce_delay,
188
               s->transparent_color_index, s->gce_disposal);
189
#endif
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
#ifdef DEBUG
201 318c5e05 Michel Bardiaux
        dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
202 06d392a7 Baptiste Coudurier
#endif
203
    }
204
    return 0;
205
}
206
207
static int gif_read_header1(GifState *s)
208
{
209
    uint8_t sig[6];
210 a7fb3a96 Baptiste Coudurier
    int v, n;
211 06d392a7 Baptiste Coudurier
    int has_global_palette;
212
213 7a28b771 Baptiste Coudurier
    if (s->bytestream_end < s->bytestream + 13)
214
        return -1;
215
216 06d392a7 Baptiste Coudurier
    /* 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)
220
        return -1;
221
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");
229
        return -1;
230
    }
231
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 */
238
#ifdef DEBUG
239 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
240 06d392a7 Baptiste Coudurier
           s->screen_width, s->screen_height, s->bits_per_pixel,
241
           has_global_palette);
242
#endif
243
    if (has_global_palette) {
244
        n = 1 << s->bits_per_pixel;
245 7a28b771 Baptiste Coudurier
        if (s->bytestream_end < s->bytestream + n * 3)
246
            return -1;
247 06d392a7 Baptiste Coudurier
        bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
248
    }
249
    return 0;
250
}
251
252
static int gif_parse_next_image(GifState *s)
253
{
254 7a28b771 Baptiste Coudurier
    while (s->bytestream < s->bytestream_end) {
255 5f7ab90a Baptiste Coudurier
        int code = bytestream_get_byte(&s->bytestream);
256 06d392a7 Baptiste Coudurier
#ifdef DEBUG
257 318c5e05 Michel Bardiaux
        dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
258 06d392a7 Baptiste Coudurier
#endif
259
        switch (code) {
260
        case ',':
261
            if (gif_read_image(s) < 0)
262
                return -1;
263 5f7ab90a Baptiste Coudurier
            return 0;
264 06d392a7 Baptiste Coudurier
        case ';':
265
            /* end of image */
266 5f7ab90a Baptiste Coudurier
            return -1;
267 06d392a7 Baptiste Coudurier
        case '!':
268
            if (gif_read_extension(s) < 0)
269
                return -1;
270
            break;
271
        default:
272
            /* error or errneous EOF */
273 5f7ab90a Baptiste Coudurier
            return -1;
274 06d392a7 Baptiste Coudurier
        }
275
    }
276 ea82552c Baptiste Coudurier
    return -1;
277 06d392a7 Baptiste Coudurier
}
278
279
static int gif_decode_init(AVCodecContext *avctx)
280
{
281
    GifState *s = avctx->priv_data;
282
283 318c5e05 Michel Bardiaux
    s->avctx = avctx;
284
285 d54d396d Baptiste Coudurier
    avcodec_get_frame_defaults(&s->picture);
286
    avctx->coded_frame= &s->picture;
287
    s->picture.data[0] = NULL;
288 ffc5430b Kostya Shishkov
    ff_lzw_decode_open(&s->lzw);
289 06d392a7 Baptiste Coudurier
    return 0;
290
}
291
292
static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
293
{
294
    GifState *s = avctx->priv_data;
295
    AVFrame *picture = data;
296
    int ret;
297
298
    s->bytestream = buf;
299 7a28b771 Baptiste Coudurier
    s->bytestream_end = buf + buf_size;
300 06d392a7 Baptiste Coudurier
    if (gif_read_header1(s) < 0)
301
        return -1;
302
303
    avctx->pix_fmt = PIX_FMT_PAL8;
304 d54d396d Baptiste Coudurier
    if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
305
        return -1;
306
    avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
307 06d392a7 Baptiste Coudurier
308 d54d396d Baptiste Coudurier
    if (s->picture.data[0])
309
        avctx->release_buffer(avctx, &s->picture);
310
    if (avctx->get_buffer(avctx, &s->picture) < 0) {
311
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
312
        return -1;
313
    }
314
    s->image_palette = (uint32_t *)s->picture.data[1];
315 06d392a7 Baptiste Coudurier
    ret = gif_parse_next_image(s);
316
    if (ret < 0)
317
        return ret;
318
319 d54d396d Baptiste Coudurier
    *picture = s->picture;
320 06d392a7 Baptiste Coudurier
    *data_size = sizeof(AVPicture);
321 877519f5 Baptiste Coudurier
    return s->bytestream - buf;
322 06d392a7 Baptiste Coudurier
}
323
324 d54d396d Baptiste Coudurier
static int gif_decode_close(AVCodecContext *avctx)
325
{
326
    GifState *s = avctx->priv_data;
327
328 ffc5430b Kostya Shishkov
    ff_lzw_decode_close(&s->lzw);
329 d54d396d Baptiste Coudurier
    if(s->picture.data[0])
330
        avctx->release_buffer(avctx, &s->picture);
331
    return 0;
332
}
333
334 06d392a7 Baptiste Coudurier
AVCodec gif_decoder = {
335
    "gif",
336
    CODEC_TYPE_VIDEO,
337
    CODEC_ID_GIF,
338
    sizeof(GifState),
339
    gif_decode_init,
340
    NULL,
341 d54d396d Baptiste Coudurier
    gif_decode_close,
342 06d392a7 Baptiste Coudurier
    gif_decode_frame,
343
};