Statistics
| Branch: | Revision:

ffmpeg / libavcodec / gifdec.c @ 5f7ab90a

History | View | Annotate | Download (9.58 KB)

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 "avcodec.h"
26
#include "bytestream.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
    AVFrame picture;
36
    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
    uint32_t *image_palette;
43

    
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
    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)
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
#ifdef DEBUG
78
    dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
79
#endif
80

    
81
    if (has_local_palette) {
82
        bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
83
        palette = s->local_palette;
84
    } else {
85
        palette = s->global_palette;
86
        bits_per_pixel = s->bits_per_pixel;
87
    }
88

    
89
    /* verify that all the image is inside the screen dimensions */
90
    if (left + width > s->screen_width ||
91
        top + height > s->screen_height)
92
        return AVERROR(EINVAL);
93

    
94
    /* build the palette */
95
    n = (1 << bits_per_pixel);
96
    spal = palette;
97
    for(i = 0; i < n; i++) {
98
        s->image_palette[i] = (0xff << 24) |
99
            (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
100
        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

    
108
    /* now get the image data */
109
    code_size = bytestream_get_byte(&s->bytestream);
110
    //TODO: add proper data size
111
    ff_lzw_decode_init(s->lzw, code_size, s->bytestream, 0, FF_LZW_GIF);
112

    
113
    /* read all the image */
114
    linesize = s->picture.linesize[0];
115
    ptr1 = s->picture.data[0] + top * linesize + left;
116
    ptr = ptr1;
117
    pass = 0;
118
    y1 = 0;
119
    for (y = 0; y < height; y++) {
120
        ff_lzw_decode(s->lzw, ptr, width);
121
        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
                    y1 = 4;
130
                    if (pass == 0)
131
                        ptr = ptr1 + linesize * 4;
132
                    else
133
                        ptr = ptr1 + linesize * 2;
134
                    pass++;
135
                }
136
                break;
137
            case 2:
138
                y1 += 4;
139
                ptr += linesize * 4;
140
                if (y1 >= height) {
141
                    y1 = 1;
142
                    ptr = ptr1 + linesize;
143
                    pass++;
144
                }
145
                break;
146
            case 3:
147
                y1 += 2;
148
                ptr += linesize * 2;
149
                break;
150
            }
151
        } else {
152
            ptr += linesize;
153
        }
154
    }
155
    /* read the garbage data until end marker is found */
156
    ff_lzw_decode_tail(s->lzw);
157
    s->bytestream = ff_lzw_cur_ptr(s->lzw);
158
    return 0;
159
}
160

    
161
static int gif_read_extension(GifState *s)
162
{
163
    int ext_code, ext_len, i, gce_flags, gce_transparent_index;
164

    
165
    /* extension */
166
    ext_code = bytestream_get_byte(&s->bytestream);
167
    ext_len = bytestream_get_byte(&s->bytestream);
168
#ifdef DEBUG
169
    dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
170
#endif
171
    switch(ext_code) {
172
    case 0xf9:
173
        if (ext_len != 4)
174
            goto discard_ext;
175
        s->transparent_color_index = -1;
176
        gce_flags = bytestream_get_byte(&s->bytestream);
177
        s->gce_delay = bytestream_get_le16(&s->bytestream);
178
        gce_transparent_index = bytestream_get_byte(&s->bytestream);
179
        if (gce_flags & 0x01)
180
            s->transparent_color_index = gce_transparent_index;
181
        else
182
            s->transparent_color_index = -1;
183
        s->gce_disposal = (gce_flags >> 2) & 0x7;
184
#ifdef DEBUG
185
        dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
186
               gce_flags, s->gce_delay,
187
               s->transparent_color_index, s->gce_disposal);
188
#endif
189
        ext_len = bytestream_get_byte(&s->bytestream);
190
        break;
191
    }
192

    
193
    /* NOTE: many extension blocks can come after */
194
 discard_ext:
195
    while (ext_len != 0) {
196
        for (i = 0; i < ext_len; i++)
197
            bytestream_get_byte(&s->bytestream);
198
        ext_len = bytestream_get_byte(&s->bytestream);
199
#ifdef DEBUG
200
        dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
201
#endif
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
    /* 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
#ifdef DEBUG
235
    dprintf(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
#endif
239
    if (has_global_palette) {
240
        n = 1 << s->bits_per_pixel;
241
        bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
242
    }
243
    return 0;
244
}
245

    
246
static int gif_parse_next_image(GifState *s)
247
{
248
    for (;;) {
249
        int code = bytestream_get_byte(&s->bytestream);
250
#ifdef DEBUG
251
        dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
252
#endif
253
        switch (code) {
254
        case ',':
255
            if (gif_read_image(s) < 0)
256
                return -1;
257
            return 0;
258
        case ';':
259
            /* end of image */
260
            return -1;
261
        case '!':
262
            if (gif_read_extension(s) < 0)
263
                return -1;
264
            break;
265
        default:
266
            /* error or errneous EOF */
267
            return -1;
268
        }
269
    }
270
}
271

    
272
static 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, uint8_t *buf, int buf_size)
286
{
287
    GifState *s = avctx->priv_data;
288
    AVFrame *picture = data;
289
    int ret;
290

    
291
    s->bytestream = buf;
292
    if (gif_read_header1(s) < 0)
293
        return -1;
294

    
295
    avctx->pix_fmt = PIX_FMT_PAL8;
296
    if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
297
        return -1;
298
    avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
299

    
300
    if (s->picture.data[0])
301
        avctx->release_buffer(avctx, &s->picture);
302
    if (avctx->get_buffer(avctx, &s->picture) < 0) {
303
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
304
        return -1;
305
    }
306
    s->image_palette = (uint32_t *)s->picture.data[1];
307
    ret = gif_parse_next_image(s);
308
    if (ret < 0)
309
        return ret;
310

    
311
    *picture = s->picture;
312
    *data_size = sizeof(AVPicture);
313
    return 0;
314
}
315

    
316
static int gif_decode_close(AVCodecContext *avctx)
317
{
318
    GifState *s = avctx->priv_data;
319

    
320
    ff_lzw_decode_close(&s->lzw);
321
    if(s->picture.data[0])
322
        avctx->release_buffer(avctx, &s->picture);
323
    return 0;
324
}
325

    
326
AVCodec gif_decoder = {
327
    "gif",
328
    CODEC_TYPE_VIDEO,
329
    CODEC_ID_GIF,
330
    sizeof(GifState),
331
    gif_decode_init,
332
    NULL,
333
    gif_decode_close,
334
    gif_decode_frame,
335
};