annotate gifdec.c @ 3130:bc8df1731826 libavformat

recognize the CLJR FourCC (Cirrus Logic AccuPak)
author melanson
date Sun, 09 Mar 2008 04:18:48 +0000
parents ed30b032fa7b
children 7a0230981402
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
1 /*
1415
3b00fb8ef8e4 replace coder/decoder file description in libavformat by muxer/demuxer
aurel
parents: 1358
diff changeset
2 * GIF demuxer
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard.
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
4 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
5 * This file is part of FFmpeg.
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
6 *
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
11 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
15 * Lesser General Public License for more details.
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
16 *
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
896
edbe5c3717f9 Update licensing information: The FSF changed postal address.
diego
parents: 887
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
20 */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
21 #include "avformat.h"
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
22
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
23 //#define DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
24
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
25 #define MAXBITS 12
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
26 #define SIZTABLE (1<<MAXBITS)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
27
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
28 #define GCE_DISPOSAL_NONE 0
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
29 #define GCE_DISPOSAL_INPLACE 1
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
30 #define GCE_DISPOSAL_BACKGROUND 2
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
31 #define GCE_DISPOSAL_RESTORE 3
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
32
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
33 typedef struct GifState {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
34 int screen_width;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
35 int screen_height;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
36 int bits_per_pixel;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
37 int background_color_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
38 int transparent_color_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
39 int color_resolution;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
40 uint8_t *image_buf;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
41 int image_linesize;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
42 uint32_t *image_palette;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
43 int pix_fmt;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
44
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
45 /* after the frame is displayed, the disposal method is used */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
46 int gce_disposal;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
47 /* delay during which the frame is shown */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
48 int gce_delay;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
49
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
50 /* LZW compatible decoder */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
51 ByteIOContext *f;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
52 int eob_reached;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
53 uint8_t *pbuf, *ebuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
54 int bbits;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
55 unsigned int bbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
56
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
57 int cursize; /* The current code size */
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
58 int curmask;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
59 int codesize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
60 int clear_code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
61 int end_code;
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
62 int newcodes; /* First available code */
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
63 int top_slot; /* Highest code for current size */
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
64 int slot; /* Last read code */
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
65 int fc, oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
66 uint8_t *sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
67 uint8_t stack[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
68 uint8_t suffix[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
69 uint16_t prefix[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
70
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
71 /* aux buffers */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
72 uint8_t global_palette[256 * 3];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
73 uint8_t local_palette[256 * 3];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
74 uint8_t buf[256];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
75 } GifState;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
76
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
77
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
78 static const uint8_t gif87a_sig[6] = "GIF87a";
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
79 static const uint8_t gif89a_sig[6] = "GIF89a";
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
80
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
81 static const uint16_t mask[17] =
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
82 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
83 0x0000, 0x0001, 0x0003, 0x0007,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
84 0x000F, 0x001F, 0x003F, 0x007F,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
85 0x00FF, 0x01FF, 0x03FF, 0x07FF,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
86 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
87 };
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
88
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
89 /* Probe gif video format or gif image format. The current heuristic
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
90 supposes the gif87a is always a single image. For gif89a, we
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
91 consider it as a video only if a GCE extension is present in the
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
92 first kilobyte. */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
93 static int gif_video_probe(AVProbeData * pd)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
94 {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
95 const uint8_t *p, *p_end;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
96 int bits_per_pixel, has_global_palette, ext_code, ext_len;
116
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
97 int gce_flags, gce_disposal;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
98
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
99 if (pd->buf_size < 24 ||
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
100 memcmp(pd->buf, gif89a_sig, 6) != 0)
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
101 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
102 p_end = pd->buf + pd->buf_size;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
103 p = pd->buf + 6;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
104 bits_per_pixel = (p[4] & 0x07) + 1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
105 has_global_palette = (p[4] & 0x80);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
106 p += 7;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
107 if (has_global_palette)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
108 p += (1 << bits_per_pixel) * 3;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
109 for(;;) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
110 if (p >= p_end)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
111 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
112 if (*p != '!')
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
113 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
114 p++;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
115 if (p >= p_end)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
116 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
117 ext_code = *p++;
116
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
118 if (p >= p_end)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
119 return 0;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
120 ext_len = *p++;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
121 if (ext_code == 0xf9) {
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
122 if (p >= p_end)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
123 return 0;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
124 /* if GCE extension found with gce_disposal != 0: it is
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
125 likely to be an animation */
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
126 gce_flags = *p++;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
127 gce_disposal = (gce_flags >> 2) & 0x7;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
128 if (gce_disposal != 0)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
129 return AVPROBE_SCORE_MAX;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
130 else
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
131 return 0;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
132 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
133 for(;;) {
116
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
134 if (ext_len == 0)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
135 break;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
136 p += ext_len;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
137 if (p >= p_end)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
138 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
139 ext_len = *p++;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
140 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
141 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
142 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
143 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
144
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
145 static void GLZWDecodeInit(GifState * s, int csize)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
146 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
147 /* read buffer */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
148 s->eob_reached = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
149 s->pbuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
150 s->ebuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
151 s->bbuf = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
152 s->bbits = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
153
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
154 /* decoder */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
155 s->codesize = csize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
156 s->cursize = s->codesize + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
157 s->curmask = mask[s->cursize];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
158 s->top_slot = 1 << s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
159 s->clear_code = 1 << s->codesize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
160 s->end_code = s->clear_code + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
161 s->slot = s->newcodes = s->clear_code + 2;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
162 s->oc = s->fc = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
163 s->sp = s->stack;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
164 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
165
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
166 /* XXX: optimize */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
167 static inline int GetCode(GifState * s)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
168 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
169 int c, sizbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
170 uint8_t *ptr;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
171
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
172 while (s->bbits < s->cursize) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
173 ptr = s->pbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
174 if (ptr >= s->ebuf) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
175 if (!s->eob_reached) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
176 sizbuf = get_byte(s->f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
177 s->ebuf = s->buf + sizbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
178 s->pbuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
179 if (sizbuf > 0) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
180 get_buffer(s->f, s->buf, sizbuf);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
181 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
182 s->eob_reached = 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
183 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
184 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
185 ptr = s->pbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
186 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
187 s->bbuf |= ptr[0] << s->bbits;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
188 ptr++;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
189 s->pbuf = ptr;
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
190 s->bbits += 8;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
191 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
192 c = s->bbuf & s->curmask;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
193 s->bbuf >>= s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
194 s->bbits -= s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
195 return c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
196 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
197
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
198 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
199 written by Steven A. Bennett in 1987. */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
200 /* return the number of byte decoded */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
201 static int GLZWDecode(GifState * s, uint8_t * buf, int len)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
202 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
203 int l, c, code, oc, fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
204 uint8_t *sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
205
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
206 if (s->end_code < 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
207 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
208
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
209 l = len;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
210 sp = s->sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
211 oc = s->oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
212 fc = s->fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
213
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
214 while (sp > s->stack) {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
215 *buf++ = *(--sp);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
216 if ((--l) == 0)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
217 goto the_end;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
218 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
219
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
220 for (;;) {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
221 c = GetCode(s);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
222 if (c == s->end_code) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
223 s->end_code = -1;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
224 break;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
225 } else if (c == s->clear_code) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
226 s->cursize = s->codesize + 1;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
227 s->curmask = mask[s->cursize];
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
228 s->slot = s->newcodes;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
229 s->top_slot = 1 << s->cursize;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
230 while ((c = GetCode(s)) == s->clear_code);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
231 if (c == s->end_code) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
232 s->end_code = -1;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
233 break;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
234 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
235 /* test error */
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
236 if (c >= s->slot)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
237 c = 0;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
238 fc = oc = c;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
239 *buf++ = c;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
240 if ((--l) == 0)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
241 break;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
242 } else {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
243 code = c;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
244 if (code >= s->slot) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
245 *sp++ = fc;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
246 code = oc;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
247 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
248 while (code >= s->newcodes) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
249 *sp++ = s->suffix[code];
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
250 code = s->prefix[code];
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
251 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
252 *sp++ = code;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
253 if (s->slot < s->top_slot) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
254 s->suffix[s->slot] = fc = code;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
255 s->prefix[s->slot++] = oc;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
256 oc = c;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
257 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
258 if (s->slot >= s->top_slot) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
259 if (s->cursize < MAXBITS) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
260 s->top_slot <<= 1;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
261 s->curmask = mask[++s->cursize];
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
262 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
263 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
264 while (sp > s->stack) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
265 *buf++ = *(--sp);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
266 if ((--l) == 0)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
267 goto the_end;
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
268 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
269 }
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
270 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
271 the_end:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
272 s->sp = sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
273 s->oc = oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
274 s->fc = fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
275 return len - l;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
276 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
277
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
278 static int gif_read_image(GifState *s)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
279 {
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
280 ByteIOContext *f = s->f;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
281 int left, top, width, height, bits_per_pixel, code_size, flags;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
282 int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
283 uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
284
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
285 left = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
286 top = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
287 width = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
288 height = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
289 flags = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
290 is_interleaved = flags & 0x40;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
291 has_local_palette = flags & 0x80;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
292 bits_per_pixel = (flags & 0x07) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
293 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
294 printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
295 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
296
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
297 if (has_local_palette) {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
298 get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
299 palette = s->local_palette;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
300 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
301 palette = s->global_palette;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
302 bits_per_pixel = s->bits_per_pixel;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
303 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
304
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
305 /* verify that all the image is inside the screen dimensions */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
306 if (left + width > s->screen_width ||
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
307 top + height > s->screen_height)
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1444
diff changeset
308 return AVERROR(EINVAL);
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
309
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
310 /* build the palette */
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
311 if (s->pix_fmt == PIX_FMT_RGB24) {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
312 line = av_malloc(width);
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
313 if (!line)
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1444
diff changeset
314 return AVERROR(ENOMEM);
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
315 } else {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
316 n = (1 << bits_per_pixel);
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
317 spal = palette;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
318 for(i = 0; i < n; i++) {
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
319 s->image_palette[i] = (0xff << 24) |
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
320 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
321 spal += 3;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
322 }
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
323 for(; i < 256; i++)
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
324 s->image_palette[i] = (0xff << 24);
116
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
325 /* handle transparency */
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
326 if (s->transparent_color_index >= 0)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
327 s->image_palette[s->transparent_color_index] = 0;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
328 line = NULL;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
329 }
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
330
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
331 /* now get the image data */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
332 s->f = f;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
333 code_size = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
334 GLZWDecodeInit(s, code_size);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
335
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
336 /* read all the image */
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
337 linesize = s->image_linesize;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
338 ptr1 = s->image_buf + top * linesize + (left * 3);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
339 ptr = ptr1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
340 pass = 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
341 y1 = 0;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
342 for (y = 0; y < height; y++) {
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
343 if (s->pix_fmt == PIX_FMT_RGB24) {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
344 /* transcode to RGB24 */
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
345 GLZWDecode(s, line, width);
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
346 d = ptr;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
347 sptr = line;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
348 for(x = 0; x < width; x++) {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
349 spal = palette + sptr[0] * 3;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
350 d[0] = spal[0];
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
351 d[1] = spal[1];
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
352 d[2] = spal[2];
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
353 d += 3;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
354 sptr++;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
355 }
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
356 } else {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
357 GLZWDecode(s, ptr, width);
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
358 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
359 if (is_interleaved) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
360 switch(pass) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
361 default:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
362 case 0:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
363 case 1:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
364 y1 += 8;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
365 ptr += linesize * 8;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
366 if (y1 >= height) {
3106
ed30b032fa7b Fix decoding of interlaced gif, e.g. http://samples.mplayerhq.hu/GIF/7up.gif
reimar
parents: 2771
diff changeset
367 y1 = pass == 0 ? 4 : 2;
ed30b032fa7b Fix decoding of interlaced gif, e.g. http://samples.mplayerhq.hu/GIF/7up.gif
reimar
parents: 2771
diff changeset
368 ptr = ptr1 + linesize * y1;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
369 pass++;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
370 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
371 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
372 case 2:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
373 y1 += 4;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
374 ptr += linesize * 4;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
375 if (y1 >= height) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
376 y1 = 1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
377 ptr = ptr1 + linesize;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
378 pass++;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
379 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
380 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
381 case 3:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
382 y1 += 2;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
383 ptr += linesize * 2;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
384 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
385 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
386 } else {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
387 ptr += linesize;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
388 }
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
389 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
390 av_free(line);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
391
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
392 /* read the garbage data until end marker is found */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
393 while (!s->eob_reached)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
394 GetCode(s);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
395 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
396 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
397
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
398 static int gif_read_extension(GifState *s)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
399 {
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
400 ByteIOContext *f = s->f;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
401 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
402
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
403 /* extension */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
404 ext_code = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
405 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
406 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
407 printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
408 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
409 switch(ext_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
410 case 0xf9:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
411 if (ext_len != 4)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
412 goto discard_ext;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
413 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
414 gce_flags = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
415 s->gce_delay = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
416 gce_transparent_index = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
417 if (gce_flags & 0x01)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
418 s->transparent_color_index = gce_transparent_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
419 else
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
420 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
421 s->gce_disposal = (gce_flags >> 2) & 0x7;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
422 #ifdef DEBUG
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
423 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
424 gce_flags, s->gce_delay,
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
425 s->transparent_color_index, s->gce_disposal);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
426 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
427 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
428 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
429 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
430
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
431 /* NOTE: many extension blocks can come after */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
432 discard_ext:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
433 while (ext_len != 0) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
434 for (i = 0; i < ext_len; i++)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
435 get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
436 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
437 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
438 printf("gif: ext_len1=%d\n", ext_len);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
439 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
440 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
441 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
442 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
443
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
444 static int gif_read_header1(GifState *s)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
445 {
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
446 ByteIOContext *f = s->f;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
447 uint8_t sig[6];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
448 int ret, v, n;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
449 int has_global_palette;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
450
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
451 /* read gif signature */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
452 ret = get_buffer(f, sig, 6);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
453 if (ret != 6)
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
454 return -1;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
455 if (memcmp(sig, gif87a_sig, 6) != 0 &&
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
456 memcmp(sig, gif89a_sig, 6) != 0)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
457 return -1;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
458
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
459 /* read screen header */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
460 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
461 s->screen_width = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
462 s->screen_height = get_le16(f);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
463 if( (unsigned)s->screen_width > 32767
639
0b52743104ac integer overflows, heap corruption
michael
parents: 482
diff changeset
464 || (unsigned)s->screen_height > 32767){
0b52743104ac integer overflows, heap corruption
michael
parents: 482
diff changeset
465 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
0b52743104ac integer overflows, heap corruption
michael
parents: 482
diff changeset
466 return -1;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
467 }
639
0b52743104ac integer overflows, heap corruption
michael
parents: 482
diff changeset
468
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
469 v = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
470 s->color_resolution = ((v & 0x70) >> 4) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
471 has_global_palette = (v & 0x80);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
472 s->bits_per_pixel = (v & 0x07) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
473 s->background_color_index = get_byte(f);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
474 get_byte(f); /* ignored */
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
475 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
476 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
477 s->screen_width, s->screen_height, s->bits_per_pixel,
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
478 has_global_palette);
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
479 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
480 if (has_global_palette) {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
481 n = 1 << s->bits_per_pixel;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
482 get_buffer(f, s->global_palette, n * 3);
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
483 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
484 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
485 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
486
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
487 static int gif_parse_next_image(GifState *s)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
488 {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
489 ByteIOContext *f = s->f;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
490 int ret, code;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
491
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
492 for (;;) {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
493 code = url_fgetc(f);
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
494 #ifdef DEBUG
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
495 printf("gif: code=%02x '%c'\n", code, code);
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
496 #endif
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
497 switch (code) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
498 case ',':
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
499 if (gif_read_image(s) < 0)
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 1787
diff changeset
500 return AVERROR(EIO);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
501 ret = 0;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
502 goto the_end;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
503 case ';':
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
504 /* end of image */
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 1787
diff changeset
505 ret = AVERROR(EIO);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
506 goto the_end;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
507 case '!':
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
508 if (gif_read_extension(s) < 0)
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 1787
diff changeset
509 return AVERROR(EIO);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
510 break;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
511 case EOF:
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
512 default:
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
513 /* error or errneous EOF */
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 1787
diff changeset
514 ret = AVERROR(EIO);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
515 goto the_end;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
516 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
517 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
518 the_end:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
519 return ret;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
520 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
521
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
522 static int gif_read_header(AVFormatContext * s1,
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
523 AVFormatParameters * ap)
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
524 {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
525 GifState *s = s1->priv_data;
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2274
diff changeset
526 ByteIOContext *f = s1->pb;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
527 AVStream *st;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
528
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
529 s->f = f;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
530 if (gif_read_header1(s) < 0)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
531 return -1;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 820
diff changeset
532
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
533 /* allocate image buffer */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
534 s->image_linesize = s->screen_width * 3;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
535 s->image_buf = av_malloc(s->screen_height * s->image_linesize);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
536 if (!s->image_buf)
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1444
diff changeset
537 return AVERROR(ENOMEM);
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
538 s->pix_fmt = PIX_FMT_RGB24;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
539 /* now we are ready: build format streams */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
540 st = av_new_stream(s1, 0);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
541 if (!st)
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
542 return -1;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
543
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 743
diff changeset
544 st->codec->codec_type = CODEC_TYPE_VIDEO;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 743
diff changeset
545 st->codec->codec_id = CODEC_ID_RAWVIDEO;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 743
diff changeset
546 st->codec->time_base.den = 5;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 743
diff changeset
547 st->codec->time_base.num = 1;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
548 /* XXX: check if screen size is always valid */
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 743
diff changeset
549 st->codec->width = s->screen_width;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 743
diff changeset
550 st->codec->height = s->screen_height;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 743
diff changeset
551 st->codec->pix_fmt = PIX_FMT_RGB24;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
552 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
553 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
554
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
555 static int gif_read_packet(AVFormatContext * s1,
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
556 AVPacket * pkt)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
557 {
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
558 GifState *s = s1->priv_data;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
559 int ret;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
560
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
561 ret = gif_parse_next_image(s);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
562 if (ret < 0)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
563 return ret;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
564
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
565 /* XXX: avoid copying */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
566 if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
2274
b21c2af60bc9 Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents: 1787
diff changeset
567 return AVERROR(EIO);
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
568 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
569 pkt->stream_index = 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
570 memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
571 return 0;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
572 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
573
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
574 static int gif_read_close(AVFormatContext *s1)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
575 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
576 GifState *s = s1->priv_data;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
577 av_free(s->image_buf);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
578 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
579 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
580
1167
d89d7ef290da give AVInput/OutputFormat structs consistent names
mru
parents: 896
diff changeset
581 AVInputFormat gif_demuxer =
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
582 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
583 "gif",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
584 "gif format",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
585 sizeof(GifState),
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
586 gif_video_probe,
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
587 gif_read_header,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
588 gif_read_packet,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
589 gif_read_close,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
590 };