annotate gifdec.c @ 46:890e9121a54d libavformat

added animated GIF decoder (pts and various disposal handling are missing)
author bellard
date Sun, 02 Feb 2003 19:16:51 +0000
parents
children 658be43f6a0d
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 /*
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
2 * GIF decoder
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 *
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
6 * 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
7 * License as published by the Free Software Foundation; either
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
9 *
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
11 * 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
12 * 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
13 * Lesser General Public License for more details.
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
14 *
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
18 */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
19 #include "avformat.h"
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 //#define DEBUG
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 MAXBITS 12
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
24 #define SIZTABLE (1<<MAXBITS)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
25
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
26 #define GCE_DISPOSAL_NONE 0
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
27 #define GCE_DISPOSAL_INPLACE 1
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
28 #define GCE_DISPOSAL_BACKGROUND 2
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
29 #define GCE_DISPOSAL_RESTORE 3
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
30
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
31 typedef struct GifState {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
32 int screen_width;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
33 int screen_height;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
34 int bits_per_pixel;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
35 int background_color_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
36 int transparent_color_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
37 int color_resolution;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
38 int image_count;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
39 uint8_t *image_buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
40 /* 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
41 int gce_disposal;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
42 /* delay during which the frame is shown */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
43 int gce_delay;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
44
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
45 /* LZW compatible decoder */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
46 ByteIOContext *f;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
47 int eob_reached;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
48 uint8_t *pbuf, *ebuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
49 int bbits;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
50 unsigned int bbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
51
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
52 int cursize; /* The current code size */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
53 int curmask;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
54 int codesize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
55 int clear_code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
56 int end_code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
57 int newcodes; /* First available code */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
58 int top_slot; /* Highest code for current size */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
59 int slot; /* Last read code */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
60 int fc, oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
61 uint8_t *sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
62 uint8_t stack[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
63 uint8_t suffix[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
64 uint16_t prefix[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
65
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
66 /* aux buffers */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
67 uint8_t global_palette[256 * 3];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
68 uint8_t local_palette[256 * 3];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
69 uint8_t buf[256];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
70 } GifState;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
71
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
72
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
73 static const uint8_t gif87a_sig[6] = "GIF87a";
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
74 static const uint8_t gif89a_sig[6] = "GIF89a";
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
75
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
76 static const uint16_t mask[17] =
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 0x0000, 0x0001, 0x0003, 0x0007,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
79 0x000F, 0x001F, 0x003F, 0x007F,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
80 0x00FF, 0x01FF, 0x03FF, 0x07FF,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
81 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
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
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
84 static int gif_probe(AVProbeData * pd)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
85 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
86 if (pd->buf_size >= 24 &&
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
87 (memcmp(pd->buf, gif87a_sig, 6) == 0 ||
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
88 memcmp(pd->buf, gif89a_sig, 6) == 0))
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
89 return AVPROBE_SCORE_MAX;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
90 else
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
91 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
92 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
93
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
94 static void GLZWDecodeInit(GifState * s, int csize)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
95 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
96 /* read buffer */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
97 s->eob_reached = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
98 s->pbuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
99 s->ebuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
100 s->bbuf = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
101 s->bbits = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
102
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
103 /* decoder */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
104 s->codesize = csize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
105 s->cursize = s->codesize + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
106 s->curmask = mask[s->cursize];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
107 s->top_slot = 1 << s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
108 s->clear_code = 1 << s->codesize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
109 s->end_code = s->clear_code + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
110 s->slot = s->newcodes = s->clear_code + 2;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
111 s->oc = s->fc = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
112 s->sp = s->stack;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
113 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
114
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
115 /* XXX: optimize */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
116 static inline int GetCode(GifState * s)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
117 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
118 int c, sizbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
119 uint8_t *ptr;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
120
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
121 while (s->bbits < s->cursize) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
122 ptr = s->pbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
123 if (ptr >= s->ebuf) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
124 if (!s->eob_reached) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
125 sizbuf = get_byte(s->f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
126 s->ebuf = s->buf + sizbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
127 s->pbuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
128 if (sizbuf > 0) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
129 get_buffer(s->f, s->buf, sizbuf);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
130 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
131 s->eob_reached = 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
132 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
133 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
134 ptr = s->pbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
135 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
136 s->bbuf |= ptr[0] << s->bbits;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
137 ptr++;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
138 s->pbuf = ptr;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
139 s->bbits += 8;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
140 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
141 c = s->bbuf & s->curmask;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
142 s->bbuf >>= s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
143 s->bbits -= s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
144 return c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
145 }
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 /* 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
148 written by Steven A. Bennett in 1987. */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
149 /* return the number of byte decoded */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
150 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
151 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
152 int l, c, code, oc, fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
153 uint8_t *sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
154
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
155 if (s->end_code < 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
156 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
157
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
158 l = len;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
159 sp = s->sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
160 oc = s->oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
161 fc = s->fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
162
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
163 while (sp > s->stack) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
164 *buf++ = *(--sp);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
165 if ((--l) == 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
166 goto the_end;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
167 }
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 for (;;) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
170 c = GetCode(s);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
171 if (c == s->end_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
172 s->end_code = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
173 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
174 } else if (c == s->clear_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
175 s->cursize = s->codesize + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
176 s->curmask = mask[s->cursize];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
177 s->slot = s->newcodes;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
178 s->top_slot = 1 << s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
179 while ((c = GetCode(s)) == s->clear_code);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
180 if (c == s->end_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
181 s->end_code = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
182 break;
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 /* test error */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
185 if (c >= s->slot)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
186 c = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
187 fc = oc = c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
188 *buf++ = c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
189 if ((--l) == 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
190 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
191 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
192 code = c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
193 if (code >= s->slot) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
194 *sp++ = fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
195 code = oc;
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 while (code >= s->newcodes) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
198 *sp++ = s->suffix[code];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
199 code = s->prefix[code];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
200 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
201 *sp++ = code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
202 if (s->slot < s->top_slot) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
203 s->suffix[s->slot] = fc = code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
204 s->prefix[s->slot++] = oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
205 oc = c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
206 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
207 if (s->slot >= s->top_slot) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
208 if (s->cursize < MAXBITS) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
209 s->top_slot <<= 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
210 s->curmask = mask[++s->cursize];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
211 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
212 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
213 while (sp > s->stack) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
214 *buf++ = *(--sp);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
215 if ((--l) == 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
216 goto the_end;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
217 }
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 the_end:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
221 s->sp = sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
222 s->oc = oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
223 s->fc = fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
224 return len - l;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
225 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
226
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
227 static int gif_read_image(AVFormatContext * s1, AVPacket * pkt)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
228 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
229 GifState *s = s1->priv_data;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
230 int left, top, width, height, bits_per_pixel, code_size, flags;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
231 int is_interleaved, has_local_palette, y, x, linesize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
232 ByteIOContext *f = &s1->pb;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
233 uint8_t *ptr, *line, *d, *spal, *palette, *sptr;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
234
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
235 left = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
236 top = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
237 width = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
238 height = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
239 flags = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
240 is_interleaved = flags & 0x40;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
241 has_local_palette = flags & 0x80;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
242 bits_per_pixel = (flags & 0x07) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
243 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
244 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
245 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
246
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
247 if (has_local_palette) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
248 get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
249 palette = s->local_palette;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
250 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
251 palette = s->global_palette;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
252 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
253
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
254 /* allocate local image (XXX: horrible, needs API change) */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
255 if (s->image_count == 0) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
256 /* modify screen width/height if invalid */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
257 if (left + width > s->screen_width)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
258 s->screen_width = left + width;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
259 if (top + height > s->screen_height)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
260 s->screen_width = top + height;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
261 s->image_buf = av_malloc(s->screen_width * s->screen_height * 3);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
262 if (!s->image_buf)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
263 return -ENOMEM;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
264 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
265 /* verify that all the image is inside the screen dimensions */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
266 if (left + width > s->screen_width ||
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
267 top + height > s->screen_height)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
268 return -EINVAL;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
269 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
270 s->image_count++;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
271
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
272 line = av_malloc(width);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
273 if (!line)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
274 return -ENOMEM;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
275
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
276 /* now get the image data */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
277 s->f = f;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
278 code_size = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
279 GLZWDecodeInit(s, code_size);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
280
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
281 /* read all the image and transcode it to RGB24 (horrible) */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
282 linesize = s->screen_width * 3;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
283 ptr = s->image_buf + top * linesize + (left * 3);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
284 for (y = 0; y < height; y++) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
285 GLZWDecode(s, line, width);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
286 d = ptr;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
287 sptr = line;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
288 for(x = 0; x < width; x++) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
289 spal = palette + sptr[0] * 3;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
290 d[0] = spal[0];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
291 d[1] = spal[1];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
292 d[2] = spal[2];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
293 d += 3;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
294 sptr++;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
295 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
296 ptr += linesize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
297 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
298 av_free(line);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
299
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
300 /* 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
301 while (!s->eob_reached)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
302 GetCode(s);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
303
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
304 /* output image */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
305 /* XXX: avoid copying */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
306 if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
307 av_free(line);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
308 return -EIO;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
309 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
310 pkt->stream_index = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
311 memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
312 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
313 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
314
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
315 static int gif_read_extension(AVFormatContext * s1)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
316 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
317 GifState *s = s1->priv_data;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
318 ByteIOContext *f = &s1->pb;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
319 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
320
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
321 /* extension */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
322 ext_code = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
323 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
324 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
325 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
326 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
327 switch(ext_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
328 case 0xf9:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
329 if (ext_len != 4)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
330 goto discard_ext;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
331 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
332 gce_flags = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
333 s->gce_delay = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
334 gce_transparent_index = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
335 if (gce_flags & 0x01)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
336 s->transparent_color_index = gce_transparent_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
337 else
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
338 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
339 s->gce_disposal = (gce_flags >> 2) & 0x7;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
340 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
341 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
342 gce_flags, s->gce_delay,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
343 s->transparent_color_index, s->gce_disposal);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
344 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
345 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
346 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
347 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
348
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
349 /* NOTE: many extension blocks can come after */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
350 discard_ext:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
351 while (ext_len != 0) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
352 for (i = 0; i < ext_len; i++)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
353 get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
354 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
355 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
356 printf("gif: ext_len1=%d\n", ext_len);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
357 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
358 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
359 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
360 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
361
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
362 static int gif_read_header(AVFormatContext * s1,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
363 AVFormatParameters * ap)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
364 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
365 GifState *s = s1->priv_data;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
366 ByteIOContext *f = &s1->pb;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
367 AVStream *st;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
368 uint8_t sig[6];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
369 int ret, v, n;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
370 int has_global_palette;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
371
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
372 /* read gif signature */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
373 ret = get_buffer(f, sig, 6);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
374 if (ret != 6)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
375 return -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
376 if (memcmp(sig, gif87a_sig, 6) != 0 &&
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
377 memcmp(sig, gif89a_sig, 6) != 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
378 return -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
379
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
380 /* read screen header */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
381 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
382 s->screen_width = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
383 s->screen_height = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
384 v = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
385 s->color_resolution = ((v & 0x70) >> 4) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
386 has_global_palette = (v & 0x80);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
387 s->bits_per_pixel = (v & 0x07) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
388 s->background_color_index = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
389 get_byte(f); /* ignored */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
390 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
391 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
392 s->screen_width, s->screen_height, s->bits_per_pixel,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
393 has_global_palette);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
394 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
395 if (has_global_palette) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
396 n = 1 << s->bits_per_pixel;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
397 get_buffer(f, s->global_palette, n * 3);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
398 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
399 /* now we are ready: build format streams */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
400 st = av_new_stream(s1, 0);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
401 if (!st)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
402 return -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
403
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
404 st->codec.codec_type = CODEC_TYPE_VIDEO;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
405 st->codec.codec_id = CODEC_ID_RAWVIDEO;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
406 st->codec.frame_rate = 5 * FRAME_RATE_BASE;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
407 /* XXX: check if screen size is always valid */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
408 st->codec.width = s->screen_width;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
409 st->codec.height = s->screen_height;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
410 st->codec.pix_fmt = PIX_FMT_RGB24;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
411 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
412 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
413
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
414 static int gif_read_packet(AVFormatContext * s1,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
415 AVPacket * pkt)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
416 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
417 ByteIOContext *f = &s1->pb;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
418 int ret, code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
419
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
420 for (;;) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
421 code = url_fgetc(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
422 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
423 printf("gif: code=%02x '%c'\n", code, code);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
424 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
425 switch (code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
426 case ',':
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
427 if (gif_read_image(s1, pkt) < 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
428 return -EIO;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
429 ret = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
430 goto the_end;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
431 case ';':
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
432 /* end of image */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
433 ret = -EIO;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
434 goto the_end;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
435 case '!':
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
436 if (gif_read_extension(s1) < 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
437 return -EIO;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
438 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
439 case EOF:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
440 default:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
441 /* error or errneous EOF */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
442 ret = -EIO;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
443 goto the_end;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
444 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
445 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
446 the_end:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
447 return ret;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
448 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
449
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
450 static int gif_read_close(AVFormatContext *s1)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
451 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
452 GifState *s = s1->priv_data;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
453 av_free(s->image_buf);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
454 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
455 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
456
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
457 AVInputFormat gif_iformat =
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 "gif",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
460 "gif format",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
461 sizeof(GifState),
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
462 gif_probe,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
463 gif_read_header,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
464 gif_read_packet,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
465 gif_read_close,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
466 };