Statistics
| Branch: | Revision:

ffmpeg / libavcodec / gifdec.c @ 06d392a7

History | View | Annotate | Download (13.4 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

    
28
#define MAXBITS                 12
29
#define         SIZTABLE        (1<<MAXBITS)
30

    
31
#define GCE_DISPOSAL_NONE       0
32
#define GCE_DISPOSAL_INPLACE    1
33
#define GCE_DISPOSAL_BACKGROUND 2
34
#define GCE_DISPOSAL_RESTORE    3
35

    
36
typedef struct GifState {
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
    uint8_t *image_buf;
44
    int image_linesize;
45
    uint32_t image_palette[256];
46
    int pix_fmt;
47

    
48
    /* after the frame is displayed, the disposal method is used */
49
    int gce_disposal;
50
    /* delay during which the frame is shown */
51
    int gce_delay;
52

    
53
    /* LZW compatible decoder */
54
    uint8_t *bytestream;
55
    int eob_reached;
56
    uint8_t *pbuf, *ebuf;
57
    int bbits;
58
    unsigned int bbuf;
59

    
60
    int cursize;                /* The current code size */
61
    int curmask;
62
    int codesize;
63
    int clear_code;
64
    int end_code;
65
    int newcodes;               /* First available code */
66
    int top_slot;               /* Highest code for current size */
67
    int slot;                   /* Last read code */
68
    int fc, oc;
69
    uint8_t *sp;
70
    uint8_t stack[SIZTABLE];
71
    uint8_t suffix[SIZTABLE];
72
    uint16_t prefix[SIZTABLE];
73

    
74
    /* aux buffers */
75
    uint8_t global_palette[256 * 3];
76
    uint8_t local_palette[256 * 3];
77
    uint8_t buf[256];
78
} GifState;
79

    
80
static const uint8_t gif87a_sig[6] = "GIF87a";
81
static const uint8_t gif89a_sig[6] = "GIF89a";
82

    
83
static const uint16_t mask[17] =
84
{
85
    0x0000, 0x0001, 0x0003, 0x0007,
86
    0x000F, 0x001F, 0x003F, 0x007F,
87
    0x00FF, 0x01FF, 0x03FF, 0x07FF,
88
    0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
89
};
90

    
91
static void GLZWDecodeInit(GifState * s, int csize)
92
{
93
    /* read buffer */
94
    s->eob_reached = 0;
95
    s->pbuf = s->buf;
96
    s->ebuf = s->buf;
97
    s->bbuf = 0;
98
    s->bbits = 0;
99

    
100
    /* decoder */
101
    s->codesize = csize;
102
    s->cursize = s->codesize + 1;
103
    s->curmask = mask[s->cursize];
104
    s->top_slot = 1 << s->cursize;
105
    s->clear_code = 1 << s->codesize;
106
    s->end_code = s->clear_code + 1;
107
    s->slot = s->newcodes = s->clear_code + 2;
108
    s->oc = s->fc = 0;
109
    s->sp = s->stack;
110
}
111

    
112
/* XXX: optimize */
113
static inline int GetCode(GifState * s)
114
{
115
    int c, sizbuf;
116
    uint8_t *ptr;
117

    
118
    while (s->bbits < s->cursize) {
119
        ptr = s->pbuf;
120
        if (ptr >= s->ebuf) {
121
            if (!s->eob_reached) {
122
                sizbuf = bytestream_get_byte(&s->bytestream);
123
                s->ebuf = s->buf + sizbuf;
124
                s->pbuf = s->buf;
125
                if (sizbuf > 0) {
126
                    bytestream_get_buffer(&s->bytestream, s->buf, sizbuf);
127
                } else {
128
                    s->eob_reached = 1;
129
                }
130
            }
131
            ptr = s->pbuf;
132
        }
133
        s->bbuf |= ptr[0] << s->bbits;
134
        ptr++;
135
        s->pbuf = ptr;
136
        s->bbits += 8;
137
    }
138
    c = s->bbuf & s->curmask;
139
    s->bbuf >>= s->cursize;
140
    s->bbits -= s->cursize;
141
    return c;
142
}
143

    
144
/* NOTE: the algorithm here is inspired from the LZW GIF decoder
145
   written by Steven A. Bennett in 1987. */
146
/* return the number of byte decoded */
147
static int GLZWDecode(GifState * s, uint8_t * buf, int len)
148
{
149
    int l, c, code, oc, fc;
150
    uint8_t *sp;
151

    
152
    if (s->end_code < 0)
153
        return 0;
154

    
155
    l = len;
156
    sp = s->sp;
157
    oc = s->oc;
158
    fc = s->fc;
159

    
160
    while (sp > s->stack) {
161
        *buf++ = *(--sp);
162
        if ((--l) == 0)
163
            goto the_end;
164
    }
165

    
166
    for (;;) {
167
        c = GetCode(s);
168
        if (c == s->end_code) {
169
            s->end_code = -1;
170
            break;
171
        } else if (c == s->clear_code) {
172
            s->cursize = s->codesize + 1;
173
            s->curmask = mask[s->cursize];
174
            s->slot = s->newcodes;
175
            s->top_slot = 1 << s->cursize;
176
            while ((c = GetCode(s)) == s->clear_code);
177
            if (c == s->end_code) {
178
                s->end_code = -1;
179
                break;
180
            }
181
            /* test error */
182
            if (c >= s->slot)
183
                c = 0;
184
            fc = oc = c;
185
            *buf++ = c;
186
            if ((--l) == 0)
187
                break;
188
        } else {
189
            code = c;
190
            if (code >= s->slot) {
191
                *sp++ = fc;
192
                code = oc;
193
            }
194
            while (code >= s->newcodes) {
195
                *sp++ = s->suffix[code];
196
                code = s->prefix[code];
197
            }
198
            *sp++ = code;
199
            if (s->slot < s->top_slot) {
200
                s->suffix[s->slot] = fc = code;
201
                s->prefix[s->slot++] = oc;
202
                oc = c;
203
            }
204
            if (s->slot >= s->top_slot) {
205
                if (s->cursize < MAXBITS) {
206
                    s->top_slot <<= 1;
207
                    s->curmask = mask[++s->cursize];
208
                }
209
            }
210
            while (sp > s->stack) {
211
                *buf++ = *(--sp);
212
                if ((--l) == 0)
213
                    goto the_end;
214
            }
215
        }
216
    }
217
  the_end:
218
    s->sp = sp;
219
    s->oc = oc;
220
    s->fc = fc;
221
    return len - l;
222
}
223

    
224
static int gif_read_image(GifState *s)
225
{
226
    int left, top, width, height, bits_per_pixel, code_size, flags;
227
    int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
228
    uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
229

    
230
    left = bytestream_get_le16(&s->bytestream);
231
    top = bytestream_get_le16(&s->bytestream);
232
    width = bytestream_get_le16(&s->bytestream);
233
    height = bytestream_get_le16(&s->bytestream);
234
    flags = bytestream_get_byte(&s->bytestream);
235
    is_interleaved = flags & 0x40;
236
    has_local_palette = flags & 0x80;
237
    bits_per_pixel = (flags & 0x07) + 1;
238
#ifdef DEBUG
239
    printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
240
#endif
241

    
242
    if (has_local_palette) {
243
        bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
244
        palette = s->local_palette;
245
    } else {
246
        palette = s->global_palette;
247
        bits_per_pixel = s->bits_per_pixel;
248
    }
249

    
250
    /* verify that all the image is inside the screen dimensions */
251
    if (left + width > s->screen_width ||
252
        top + height > s->screen_height)
253
        return -EINVAL;
254

    
255
    /* build the palette */
256
        n = (1 << bits_per_pixel);
257
        spal = palette;
258
        for(i = 0; i < n; i++) {
259
            s->image_palette[i] = (0xff << 24) |
260
                (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
261
            spal += 3;
262
        }
263
        for(; i < 256; i++)
264
            s->image_palette[i] = (0xff << 24);
265
        /* handle transparency */
266
        if (s->transparent_color_index >= 0)
267
            s->image_palette[s->transparent_color_index] = 0;
268
        line = NULL;
269

    
270
    /* now get the image data */
271
    code_size = bytestream_get_byte(&s->bytestream);
272
    GLZWDecodeInit(s, code_size);
273

    
274
    /* read all the image */
275
    linesize = s->image_linesize;
276
    ptr1 = s->image_buf + top * linesize + (left * 3);
277
    ptr = ptr1;
278
    pass = 0;
279
    y1 = 0;
280
    for (y = 0; y < height; y++) {
281
            GLZWDecode(s, ptr, width);
282
        if (is_interleaved) {
283
            switch(pass) {
284
            default:
285
            case 0:
286
            case 1:
287
                y1 += 8;
288
                ptr += linesize * 8;
289
                if (y1 >= height) {
290
                    y1 = 4;
291
                    if (pass == 0)
292
                        ptr = ptr1 + linesize * 4;
293
                    else
294
                        ptr = ptr1 + linesize * 2;
295
                    pass++;
296
                }
297
                break;
298
            case 2:
299
                y1 += 4;
300
                ptr += linesize * 4;
301
                if (y1 >= height) {
302
                    y1 = 1;
303
                    ptr = ptr1 + linesize;
304
                    pass++;
305
                }
306
                break;
307
            case 3:
308
                y1 += 2;
309
                ptr += linesize * 2;
310
                break;
311
            }
312
        } else {
313
            ptr += linesize;
314
        }
315
    }
316
    av_free(line);
317

    
318
    /* read the garbage data until end marker is found */
319
    while (!s->eob_reached)
320
        GetCode(s);
321
    return 0;
322
}
323

    
324
static int gif_read_extension(GifState *s)
325
{
326
    int ext_code, ext_len, i, gce_flags, gce_transparent_index;
327

    
328
    /* extension */
329
    ext_code = bytestream_get_byte(&s->bytestream);
330
    ext_len = bytestream_get_byte(&s->bytestream);
331
#ifdef DEBUG
332
    printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
333
#endif
334
    switch(ext_code) {
335
    case 0xf9:
336
        if (ext_len != 4)
337
            goto discard_ext;
338
        s->transparent_color_index = -1;
339
        gce_flags = bytestream_get_byte(&s->bytestream);
340
        s->gce_delay = bytestream_get_le16(&s->bytestream);
341
        gce_transparent_index = bytestream_get_byte(&s->bytestream);
342
        if (gce_flags & 0x01)
343
            s->transparent_color_index = gce_transparent_index;
344
        else
345
            s->transparent_color_index = -1;
346
        s->gce_disposal = (gce_flags >> 2) & 0x7;
347
#ifdef DEBUG
348
        printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
349
               gce_flags, s->gce_delay,
350
               s->transparent_color_index, s->gce_disposal);
351
#endif
352
        ext_len = bytestream_get_byte(&s->bytestream);
353
        break;
354
    }
355

    
356
    /* NOTE: many extension blocks can come after */
357
 discard_ext:
358
    while (ext_len != 0) {
359
        for (i = 0; i < ext_len; i++)
360
            bytestream_get_byte(&s->bytestream);
361
        ext_len = bytestream_get_byte(&s->bytestream);
362
#ifdef DEBUG
363
        printf("gif: ext_len1=%d\n", ext_len);
364
#endif
365
    }
366
    return 0;
367
}
368

    
369
static int gif_read_header1(GifState *s)
370
{
371
    uint8_t sig[6];
372
    int ret, v, n;
373
    int has_global_palette;
374

    
375
    /* read gif signature */
376
    bytestream_get_buffer(&s->bytestream, sig, 6);
377
    if (memcmp(sig, gif87a_sig, 6) != 0 &&
378
        memcmp(sig, gif89a_sig, 6) != 0)
379
        return -1;
380

    
381
    /* read screen header */
382
    s->transparent_color_index = -1;
383
    s->screen_width = bytestream_get_le16(&s->bytestream);
384
    s->screen_height = bytestream_get_le16(&s->bytestream);
385
    if(   (unsigned)s->screen_width  > 32767
386
       || (unsigned)s->screen_height > 32767){
387
        av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
388
        return -1;
389
    }
390

    
391
    v = bytestream_get_byte(&s->bytestream);
392
    s->color_resolution = ((v & 0x70) >> 4) + 1;
393
    has_global_palette = (v & 0x80);
394
    s->bits_per_pixel = (v & 0x07) + 1;
395
    s->background_color_index = bytestream_get_byte(&s->bytestream);
396
    bytestream_get_byte(&s->bytestream);                /* ignored */
397
#ifdef DEBUG
398
    printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
399
           s->screen_width, s->screen_height, s->bits_per_pixel,
400
           has_global_palette);
401
#endif
402
    if (has_global_palette) {
403
        n = 1 << s->bits_per_pixel;
404
        bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
405
    }
406
    return 0;
407
}
408

    
409
static int gif_parse_next_image(GifState *s)
410
{
411
    int ret, code;
412

    
413
    for (;;) {
414
        code = bytestream_get_byte(&s->bytestream);
415
#ifdef DEBUG
416
        printf("gif: code=%02x '%c'\n", code, code);
417
#endif
418
        switch (code) {
419
        case ',':
420
            if (gif_read_image(s) < 0)
421
                return -1;
422
            ret = 0;
423
            goto the_end;
424
        case ';':
425
            /* end of image */
426
            ret = -1;
427
            goto the_end;
428
        case '!':
429
            if (gif_read_extension(s) < 0)
430
                return -1;
431
            break;
432
        case EOF:
433
        default:
434
            /* error or errneous EOF */
435
            ret = -1;
436
            goto the_end;
437
        }
438
    }
439
  the_end:
440
    return ret;
441
}
442

    
443
static int gif_decode_init(AVCodecContext *avctx)
444
{
445
    GifState *s = avctx->priv_data;
446

    
447
    return 0;
448
}
449

    
450
static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
451
{
452
    GifState *s = avctx->priv_data;
453
    AVFrame *picture = data;
454
    int ret;
455

    
456
    s->bytestream = buf;
457
    if (gif_read_header1(s) < 0)
458
        return -1;
459

    
460
    /* allocate image buffer */
461
    s->image_linesize = s->screen_width * 3;
462
    s->image_buf = av_malloc(s->screen_height * s->image_linesize);
463
    if (!s->image_buf)
464
        return -ENOMEM;
465
    s->pix_fmt = PIX_FMT_PAL8;
466
    /* now we are ready: build format streams */
467

    
468
    /* XXX: check if screen size is always valid */
469
    avctx->width = s->screen_width;
470
    avctx->height = s->screen_height;
471
    avctx->pix_fmt = PIX_FMT_PAL8;
472

    
473
    ret = gif_parse_next_image(s);
474
    if (ret < 0)
475
        return ret;
476

    
477
    picture->data[0] = s->image_buf;
478
    picture->linesize[0] = s->image_linesize;
479
    picture->data[1] = s->image_palette;
480
    picture->linesize[1] = 4;
481

    
482
    *data_size = sizeof(AVPicture);
483
    return 0;
484
}
485

    
486
AVCodec gif_decoder = {
487
    "gif",
488
    CODEC_TYPE_VIDEO,
489
    CODEC_ID_GIF,
490
    sizeof(GifState),
491
    gif_decode_init,
492
    NULL,
493
    NULL,
494
    gif_decode_frame,
495
};