Statistics
| Branch: | Revision:

ffmpeg / libavcodec / gifdec.c @ 73a0b19b

History | View | Annotate | Download (9.76 KB)

1 06d392a7 Baptiste Coudurier
/*
2
 * GIF decoder
3 406792e7 Diego Biurrun
 * Copyright (c) 2003 Fabrice Bellard
4
 * Copyright (c) 2006 Baptiste Coudurier
5 06d392a7 Baptiste Coudurier
 *
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 6ce9b431 Stefano Sabatini
#include "libavcore/imgutils.h"
26 06d392a7 Baptiste Coudurier
#include "avcodec.h"
27
#include "bytestream.h"
28 ffc5430b Kostya Shishkov
#include "lzw.h"
29 06d392a7 Baptiste Coudurier
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 d54d396d Baptiste Coudurier
    AVFrame picture;
37 06d392a7 Baptiste Coudurier
    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 d54d396d Baptiste Coudurier
    uint32_t *image_palette;
44 06d392a7 Baptiste Coudurier
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 7993df65 Michael Niedermayer
    const uint8_t *bytestream;
52
    const uint8_t *bytestream_end;
53 ffc5430b Kostya Shishkov
    LZWState *lzw;
54 06d392a7 Baptiste Coudurier
55
    /* aux buffers */
56
    uint8_t global_palette[256 * 3];
57
    uint8_t local_palette[256 * 3];
58 318c5e05 Michel Bardiaux
59
  AVCodecContext* avctx;
60 06d392a7 Baptiste Coudurier
} 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 a7fb3a96 Baptiste Coudurier
    int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
69 bc52b0c5 Baptiste Coudurier
    uint8_t *ptr, *spal, *palette, *ptr1;
70 06d392a7 Baptiste Coudurier
71
    left = bytestream_get_le16(&s->bytestream);
72
    top = bytestream_get_le16(&s->bytestream);
73
    width = bytestream_get_le16(&s->bytestream);
74
    height = bytestream_get_le16(&s->bytestream);
75
    flags = bytestream_get_byte(&s->bytestream);
76
    is_interleaved = flags & 0x40;
77
    has_local_palette = flags & 0x80;
78
    bits_per_pixel = (flags & 0x07) + 1;
79 73a0b19b Diego Elio 'Flameeyes' Pettenò
80 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
81 06d392a7 Baptiste Coudurier
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 2c124cb6 Ramiro Polla
        s->image_palette[i] = (0xff << 24) | AV_RB24(spal);
100 47babca8 Baptiste Coudurier
        spal += 3;
101
    }
102
    for(; i < 256; i++)
103
        s->image_palette[i] = (0xff << 24);
104
    /* handle transparency */
105
    if (s->transparent_color_index >= 0)
106
        s->image_palette[s->transparent_color_index] = 0;
107 06d392a7 Baptiste Coudurier
108
    /* now get the image data */
109
    code_size = bytestream_get_byte(&s->bytestream);
110 10d4899c Baptiste Coudurier
    ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
111
                       s->bytestream_end - s->bytestream, FF_LZW_GIF);
112 06d392a7 Baptiste Coudurier
113
    /* read all the image */
114 d54d396d Baptiste Coudurier
    linesize = s->picture.linesize[0];
115 b190aa89 Baptiste Coudurier
    ptr1 = s->picture.data[0] + top * linesize + left;
116 06d392a7 Baptiste Coudurier
    ptr = ptr1;
117
    pass = 0;
118
    y1 = 0;
119
    for (y = 0; y < height; y++) {
120 ffc5430b Kostya Shishkov
        ff_lzw_decode(s->lzw, ptr, width);
121 06d392a7 Baptiste Coudurier
        if (is_interleaved) {
122
            switch(pass) {
123
            default:
124
            case 0:
125
            case 1:
126
                y1 += 8;
127
                ptr += linesize * 8;
128
                if (y1 >= height) {
129 30400f0d Baptiste Coudurier
                    y1 = pass ? 2 : 4;
130
                    ptr = ptr1 + linesize * y1;
131 06d392a7 Baptiste Coudurier
                    pass++;
132
                }
133
                break;
134
            case 2:
135
                y1 += 4;
136
                ptr += linesize * 4;
137
                if (y1 >= height) {
138
                    y1 = 1;
139
                    ptr = ptr1 + linesize;
140
                    pass++;
141
                }
142
                break;
143
            case 3:
144
                y1 += 2;
145
                ptr += linesize * 2;
146
                break;
147
            }
148
        } else {
149
            ptr += linesize;
150
        }
151
    }
152
    /* read the garbage data until end marker is found */
153 ffc5430b Kostya Shishkov
    ff_lzw_decode_tail(s->lzw);
154
    s->bytestream = ff_lzw_cur_ptr(s->lzw);
155 06d392a7 Baptiste Coudurier
    return 0;
156
}
157
158
static int gif_read_extension(GifState *s)
159
{
160
    int ext_code, ext_len, i, gce_flags, gce_transparent_index;
161
162
    /* extension */
163
    ext_code = bytestream_get_byte(&s->bytestream);
164
    ext_len = bytestream_get_byte(&s->bytestream);
165 73a0b19b Diego Elio 'Flameeyes' Pettenò
166 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
167 73a0b19b Diego Elio 'Flameeyes' Pettenò
168 06d392a7 Baptiste Coudurier
    switch(ext_code) {
169
    case 0xf9:
170
        if (ext_len != 4)
171
            goto discard_ext;
172
        s->transparent_color_index = -1;
173
        gce_flags = bytestream_get_byte(&s->bytestream);
174
        s->gce_delay = bytestream_get_le16(&s->bytestream);
175
        gce_transparent_index = bytestream_get_byte(&s->bytestream);
176
        if (gce_flags & 0x01)
177
            s->transparent_color_index = gce_transparent_index;
178
        else
179
            s->transparent_color_index = -1;
180
        s->gce_disposal = (gce_flags >> 2) & 0x7;
181 73a0b19b Diego Elio 'Flameeyes' Pettenò
182 318c5e05 Michel Bardiaux
        dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
183 06d392a7 Baptiste Coudurier
               gce_flags, s->gce_delay,
184
               s->transparent_color_index, s->gce_disposal);
185 73a0b19b Diego Elio 'Flameeyes' Pettenò
186 06d392a7 Baptiste Coudurier
        ext_len = bytestream_get_byte(&s->bytestream);
187
        break;
188
    }
189
190
    /* NOTE: many extension blocks can come after */
191
 discard_ext:
192
    while (ext_len != 0) {
193
        for (i = 0; i < ext_len; i++)
194
            bytestream_get_byte(&s->bytestream);
195
        ext_len = bytestream_get_byte(&s->bytestream);
196 73a0b19b Diego Elio 'Flameeyes' Pettenò
197 318c5e05 Michel Bardiaux
        dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
198 06d392a7 Baptiste Coudurier
    }
199
    return 0;
200
}
201
202
static int gif_read_header1(GifState *s)
203
{
204
    uint8_t sig[6];
205 a7fb3a96 Baptiste Coudurier
    int v, n;
206 06d392a7 Baptiste Coudurier
    int has_global_palette;
207
208 7a28b771 Baptiste Coudurier
    if (s->bytestream_end < s->bytestream + 13)
209
        return -1;
210
211 06d392a7 Baptiste Coudurier
    /* read gif signature */
212
    bytestream_get_buffer(&s->bytestream, sig, 6);
213
    if (memcmp(sig, gif87a_sig, 6) != 0 &&
214
        memcmp(sig, gif89a_sig, 6) != 0)
215
        return -1;
216
217
    /* read screen header */
218
    s->transparent_color_index = -1;
219
    s->screen_width = bytestream_get_le16(&s->bytestream);
220
    s->screen_height = bytestream_get_le16(&s->bytestream);
221
    if(   (unsigned)s->screen_width  > 32767
222
       || (unsigned)s->screen_height > 32767){
223
        av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
224
        return -1;
225
    }
226
227
    v = bytestream_get_byte(&s->bytestream);
228
    s->color_resolution = ((v & 0x70) >> 4) + 1;
229
    has_global_palette = (v & 0x80);
230
    s->bits_per_pixel = (v & 0x07) + 1;
231
    s->background_color_index = bytestream_get_byte(&s->bytestream);
232
    bytestream_get_byte(&s->bytestream);                /* ignored */
233 73a0b19b Diego Elio 'Flameeyes' Pettenò
234 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
235 06d392a7 Baptiste Coudurier
           s->screen_width, s->screen_height, s->bits_per_pixel,
236
           has_global_palette);
237 73a0b19b Diego Elio 'Flameeyes' Pettenò
238 06d392a7 Baptiste Coudurier
    if (has_global_palette) {
239
        n = 1 << s->bits_per_pixel;
240 7a28b771 Baptiste Coudurier
        if (s->bytestream_end < s->bytestream + n * 3)
241
            return -1;
242 06d392a7 Baptiste Coudurier
        bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
243
    }
244
    return 0;
245
}
246
247
static int gif_parse_next_image(GifState *s)
248
{
249 7a28b771 Baptiste Coudurier
    while (s->bytestream < s->bytestream_end) {
250 5f7ab90a Baptiste Coudurier
        int code = bytestream_get_byte(&s->bytestream);
251 73a0b19b Diego Elio 'Flameeyes' Pettenò
252 318c5e05 Michel Bardiaux
        dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
253 73a0b19b Diego Elio 'Flameeyes' Pettenò
254 06d392a7 Baptiste Coudurier
        switch (code) {
255
        case ',':
256 94cebc56 Mark Cox
            return gif_read_image(s);
257 06d392a7 Baptiste Coudurier
        case '!':
258
            if (gif_read_extension(s) < 0)
259
                return -1;
260
            break;
261 94cebc56 Mark Cox
        case ';':
262
            /* end of image */
263 06d392a7 Baptiste Coudurier
        default:
264 3154f6db Diego Biurrun
            /* error or erroneous EOF */
265 5f7ab90a Baptiste Coudurier
            return -1;
266 06d392a7 Baptiste Coudurier
        }
267
    }
268 ea82552c Baptiste Coudurier
    return -1;
269 06d392a7 Baptiste Coudurier
}
270
271 98a6fff9 Zuxy Meng
static av_cold int gif_decode_init(AVCodecContext *avctx)
272 06d392a7 Baptiste Coudurier
{
273
    GifState *s = avctx->priv_data;
274
275 318c5e05 Michel Bardiaux
    s->avctx = avctx;
276
277 d54d396d Baptiste Coudurier
    avcodec_get_frame_defaults(&s->picture);
278
    avctx->coded_frame= &s->picture;
279
    s->picture.data[0] = NULL;
280 ffc5430b Kostya Shishkov
    ff_lzw_decode_open(&s->lzw);
281 06d392a7 Baptiste Coudurier
    return 0;
282
}
283
284 7a00bbad Thilo Borgmann
static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
285 06d392a7 Baptiste Coudurier
{
286 7a00bbad Thilo Borgmann
    const uint8_t *buf = avpkt->data;
287
    int buf_size = avpkt->size;
288 06d392a7 Baptiste Coudurier
    GifState *s = avctx->priv_data;
289
    AVFrame *picture = data;
290
    int ret;
291
292
    s->bytestream = buf;
293 7a28b771 Baptiste Coudurier
    s->bytestream_end = buf + buf_size;
294 06d392a7 Baptiste Coudurier
    if (gif_read_header1(s) < 0)
295
        return -1;
296
297
    avctx->pix_fmt = PIX_FMT_PAL8;
298 e16f217c Stefano Sabatini
    if (av_image_check_size(s->screen_width, s->screen_height, 0, avctx))
299 d54d396d Baptiste Coudurier
        return -1;
300
    avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
301 06d392a7 Baptiste Coudurier
302 d54d396d Baptiste Coudurier
    if (s->picture.data[0])
303
        avctx->release_buffer(avctx, &s->picture);
304
    if (avctx->get_buffer(avctx, &s->picture) < 0) {
305
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
306
        return -1;
307
    }
308
    s->image_palette = (uint32_t *)s->picture.data[1];
309 06d392a7 Baptiste Coudurier
    ret = gif_parse_next_image(s);
310
    if (ret < 0)
311
        return ret;
312
313 d54d396d Baptiste Coudurier
    *picture = s->picture;
314 06d392a7 Baptiste Coudurier
    *data_size = sizeof(AVPicture);
315 877519f5 Baptiste Coudurier
    return s->bytestream - buf;
316 06d392a7 Baptiste Coudurier
}
317
318 98a6fff9 Zuxy Meng
static av_cold int gif_decode_close(AVCodecContext *avctx)
319 d54d396d Baptiste Coudurier
{
320
    GifState *s = avctx->priv_data;
321
322 ffc5430b Kostya Shishkov
    ff_lzw_decode_close(&s->lzw);
323 d54d396d Baptiste Coudurier
    if(s->picture.data[0])
324
        avctx->release_buffer(avctx, &s->picture);
325
    return 0;
326
}
327
328 06d392a7 Baptiste Coudurier
AVCodec gif_decoder = {
329
    "gif",
330 72415b2a Stefano Sabatini
    AVMEDIA_TYPE_VIDEO,
331 06d392a7 Baptiste Coudurier
    CODEC_ID_GIF,
332
    sizeof(GifState),
333
    gif_decode_init,
334
    NULL,
335 d54d396d Baptiste Coudurier
    gif_decode_close,
336 06d392a7 Baptiste Coudurier
    gif_decode_frame,
337 3bbcd0bb Baptiste Coudurier
    CODEC_CAP_DR1,
338 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
339 06d392a7 Baptiste Coudurier
};