annotate gifdec.c @ 4054:f20c7bb12103 libavcodec

change gif demuxer to gif decoder
author bcoudurier
date Sun, 22 Oct 2006 15:05:03 +0000
parents
children a76659cf76d3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
1 /*
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
2 * GIF decoder
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard.
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
4 * Copyright (c) 2006 Baptiste Coudurier.
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
5 *
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
6 * This file is part of FFmpeg.
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
7 *
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
12 *
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
16 * Lesser General Public License for more details.
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
17 *
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
21 */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
22
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
23 //#define DEBUG
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
24
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
25 #include "avcodec.h"
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
26 #include "bytestream.h"
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
27
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
28 #define MAXBITS 12
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
29 #define SIZTABLE (1<<MAXBITS)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
30
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
31 #define GCE_DISPOSAL_NONE 0
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
32 #define GCE_DISPOSAL_INPLACE 1
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
33 #define GCE_DISPOSAL_BACKGROUND 2
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
34 #define GCE_DISPOSAL_RESTORE 3
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
35
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
36 typedef struct GifState {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
37 int screen_width;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
38 int screen_height;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
39 int bits_per_pixel;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
40 int background_color_index;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
41 int transparent_color_index;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
42 int color_resolution;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
43 uint8_t *image_buf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
44 int image_linesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
45 uint32_t image_palette[256];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
46 int pix_fmt;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
47
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
48 /* after the frame is displayed, the disposal method is used */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
49 int gce_disposal;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
50 /* delay during which the frame is shown */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
51 int gce_delay;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
52
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
53 /* LZW compatible decoder */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
54 uint8_t *bytestream;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
55 int eob_reached;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
56 uint8_t *pbuf, *ebuf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
57 int bbits;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
58 unsigned int bbuf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
59
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
60 int cursize; /* The current code size */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
61 int curmask;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
62 int codesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
63 int clear_code;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
64 int end_code;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
65 int newcodes; /* First available code */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
66 int top_slot; /* Highest code for current size */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
67 int slot; /* Last read code */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
68 int fc, oc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
69 uint8_t *sp;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
70 uint8_t stack[SIZTABLE];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
71 uint8_t suffix[SIZTABLE];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
72 uint16_t prefix[SIZTABLE];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
73
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
74 /* aux buffers */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
75 uint8_t global_palette[256 * 3];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
76 uint8_t local_palette[256 * 3];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
77 uint8_t buf[256];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
78 } GifState;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
79
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
80 static const uint8_t gif87a_sig[6] = "GIF87a";
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
81 static const uint8_t gif89a_sig[6] = "GIF89a";
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
82
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
83 static const uint16_t mask[17] =
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
84 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
85 0x0000, 0x0001, 0x0003, 0x0007,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
86 0x000F, 0x001F, 0x003F, 0x007F,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
87 0x00FF, 0x01FF, 0x03FF, 0x07FF,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
88 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
89 };
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
90
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
91 static void GLZWDecodeInit(GifState * s, int csize)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
92 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
93 /* read buffer */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
94 s->eob_reached = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
95 s->pbuf = s->buf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
96 s->ebuf = s->buf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
97 s->bbuf = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
98 s->bbits = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
99
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
100 /* decoder */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
101 s->codesize = csize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
102 s->cursize = s->codesize + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
103 s->curmask = mask[s->cursize];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
104 s->top_slot = 1 << s->cursize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
105 s->clear_code = 1 << s->codesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
106 s->end_code = s->clear_code + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
107 s->slot = s->newcodes = s->clear_code + 2;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
108 s->oc = s->fc = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
109 s->sp = s->stack;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
110 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
111
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
112 /* XXX: optimize */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
113 static inline int GetCode(GifState * s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
114 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
115 int c, sizbuf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
116 uint8_t *ptr;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
117
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
118 while (s->bbits < s->cursize) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
119 ptr = s->pbuf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
120 if (ptr >= s->ebuf) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
121 if (!s->eob_reached) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
122 sizbuf = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
123 s->ebuf = s->buf + sizbuf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
124 s->pbuf = s->buf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
125 if (sizbuf > 0) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
126 bytestream_get_buffer(&s->bytestream, s->buf, sizbuf);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
127 } else {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
128 s->eob_reached = 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
129 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
130 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
131 ptr = s->pbuf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
132 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
133 s->bbuf |= ptr[0] << s->bbits;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
134 ptr++;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
135 s->pbuf = ptr;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
136 s->bbits += 8;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
137 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
138 c = s->bbuf & s->curmask;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
139 s->bbuf >>= s->cursize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
140 s->bbits -= s->cursize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
141 return c;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
142 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
143
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
144 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
145 written by Steven A. Bennett in 1987. */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
146 /* return the number of byte decoded */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
147 static int GLZWDecode(GifState * s, uint8_t * buf, int len)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
148 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
149 int l, c, code, oc, fc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
150 uint8_t *sp;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
151
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
152 if (s->end_code < 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
153 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
154
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
155 l = len;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
156 sp = s->sp;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
157 oc = s->oc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
158 fc = s->fc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
159
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
160 while (sp > s->stack) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
161 *buf++ = *(--sp);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
162 if ((--l) == 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
163 goto the_end;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
164 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
165
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
166 for (;;) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
167 c = GetCode(s);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
168 if (c == s->end_code) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
169 s->end_code = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
170 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
171 } else if (c == s->clear_code) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
172 s->cursize = s->codesize + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
173 s->curmask = mask[s->cursize];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
174 s->slot = s->newcodes;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
175 s->top_slot = 1 << s->cursize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
176 while ((c = GetCode(s)) == s->clear_code);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
177 if (c == s->end_code) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
178 s->end_code = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
179 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
180 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
181 /* test error */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
182 if (c >= s->slot)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
183 c = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
184 fc = oc = c;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
185 *buf++ = c;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
186 if ((--l) == 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
187 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
188 } else {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
189 code = c;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
190 if (code >= s->slot) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
191 *sp++ = fc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
192 code = oc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
193 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
194 while (code >= s->newcodes) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
195 *sp++ = s->suffix[code];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
196 code = s->prefix[code];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
197 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
198 *sp++ = code;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
199 if (s->slot < s->top_slot) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
200 s->suffix[s->slot] = fc = code;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
201 s->prefix[s->slot++] = oc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
202 oc = c;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
203 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
204 if (s->slot >= s->top_slot) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
205 if (s->cursize < MAXBITS) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
206 s->top_slot <<= 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
207 s->curmask = mask[++s->cursize];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
208 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
209 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
210 while (sp > s->stack) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
211 *buf++ = *(--sp);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
212 if ((--l) == 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
213 goto the_end;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
214 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
215 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
216 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
217 the_end:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
218 s->sp = sp;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
219 s->oc = oc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
220 s->fc = fc;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
221 return len - l;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
222 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
223
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
224 static int gif_read_image(GifState *s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
225 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
226 int left, top, width, height, bits_per_pixel, code_size, flags;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
227 int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
228 uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
229
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
230 left = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
231 top = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
232 width = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
233 height = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
234 flags = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
235 is_interleaved = flags & 0x40;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
236 has_local_palette = flags & 0x80;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
237 bits_per_pixel = (flags & 0x07) + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
238 #ifdef DEBUG
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
239 printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
240 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
241
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
242 if (has_local_palette) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
243 bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
244 palette = s->local_palette;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
245 } else {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
246 palette = s->global_palette;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
247 bits_per_pixel = s->bits_per_pixel;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
248 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
249
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
250 /* verify that all the image is inside the screen dimensions */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
251 if (left + width > s->screen_width ||
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
252 top + height > s->screen_height)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
253 return -EINVAL;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
254
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
255 /* build the palette */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
256 n = (1 << bits_per_pixel);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
257 spal = palette;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
258 for(i = 0; i < n; i++) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
259 s->image_palette[i] = (0xff << 24) |
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
260 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
261 spal += 3;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
262 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
263 for(; i < 256; i++)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
264 s->image_palette[i] = (0xff << 24);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
265 /* handle transparency */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
266 if (s->transparent_color_index >= 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
267 s->image_palette[s->transparent_color_index] = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
268 line = NULL;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
269
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
270 /* now get the image data */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
271 code_size = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
272 GLZWDecodeInit(s, code_size);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
273
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
274 /* read all the image */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
275 linesize = s->image_linesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
276 ptr1 = s->image_buf + top * linesize + (left * 3);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
277 ptr = ptr1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
278 pass = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
279 y1 = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
280 for (y = 0; y < height; y++) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
281 GLZWDecode(s, ptr, width);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
282 if (is_interleaved) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
283 switch(pass) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
284 default:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
285 case 0:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
286 case 1:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
287 y1 += 8;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
288 ptr += linesize * 8;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
289 if (y1 >= height) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
290 y1 = 4;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
291 if (pass == 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
292 ptr = ptr1 + linesize * 4;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
293 else
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
294 ptr = ptr1 + linesize * 2;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
295 pass++;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
296 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
297 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
298 case 2:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
299 y1 += 4;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
300 ptr += linesize * 4;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
301 if (y1 >= height) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
302 y1 = 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
303 ptr = ptr1 + linesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
304 pass++;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
305 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
306 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
307 case 3:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
308 y1 += 2;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
309 ptr += linesize * 2;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
310 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
311 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
312 } else {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
313 ptr += linesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
314 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
315 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
316 av_free(line);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
317
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
318 /* read the garbage data until end marker is found */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
319 while (!s->eob_reached)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
320 GetCode(s);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
321 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
322 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
323
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
324 static int gif_read_extension(GifState *s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
325 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
326 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
327
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
328 /* extension */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
329 ext_code = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
330 ext_len = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
331 #ifdef DEBUG
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
332 printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
333 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
334 switch(ext_code) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
335 case 0xf9:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
336 if (ext_len != 4)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
337 goto discard_ext;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
338 s->transparent_color_index = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
339 gce_flags = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
340 s->gce_delay = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
341 gce_transparent_index = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
342 if (gce_flags & 0x01)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
343 s->transparent_color_index = gce_transparent_index;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
344 else
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
345 s->transparent_color_index = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
346 s->gce_disposal = (gce_flags >> 2) & 0x7;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
347 #ifdef DEBUG
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
348 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
349 gce_flags, s->gce_delay,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
350 s->transparent_color_index, s->gce_disposal);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
351 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
352 ext_len = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
353 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
354 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
355
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
356 /* NOTE: many extension blocks can come after */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
357 discard_ext:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
358 while (ext_len != 0) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
359 for (i = 0; i < ext_len; i++)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
360 bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
361 ext_len = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
362 #ifdef DEBUG
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
363 printf("gif: ext_len1=%d\n", ext_len);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
364 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
365 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
366 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
367 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
368
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
369 static int gif_read_header1(GifState *s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
370 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
371 uint8_t sig[6];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
372 int ret, v, n;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
373 int has_global_palette;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
374
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
375 /* read gif signature */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
376 bytestream_get_buffer(&s->bytestream, sig, 6);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
377 if (memcmp(sig, gif87a_sig, 6) != 0 &&
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
378 memcmp(sig, gif89a_sig, 6) != 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
379 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
380
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
381 /* read screen header */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
382 s->transparent_color_index = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
383 s->screen_width = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
384 s->screen_height = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
385 if( (unsigned)s->screen_width > 32767
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
386 || (unsigned)s->screen_height > 32767){
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
387 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
388 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
389 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
390
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
391 v = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
392 s->color_resolution = ((v & 0x70) >> 4) + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
393 has_global_palette = (v & 0x80);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
394 s->bits_per_pixel = (v & 0x07) + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
395 s->background_color_index = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
396 bytestream_get_byte(&s->bytestream); /* ignored */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
397 #ifdef DEBUG
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
398 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
399 s->screen_width, s->screen_height, s->bits_per_pixel,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
400 has_global_palette);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
401 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
402 if (has_global_palette) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
403 n = 1 << s->bits_per_pixel;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
404 bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
405 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
406 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
407 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
408
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
409 static int gif_parse_next_image(GifState *s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
410 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
411 int ret, code;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
412
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
413 for (;;) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
414 code = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
415 #ifdef DEBUG
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
416 printf("gif: code=%02x '%c'\n", code, code);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
417 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
418 switch (code) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
419 case ',':
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
420 if (gif_read_image(s) < 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
421 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
422 ret = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
423 goto the_end;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
424 case ';':
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
425 /* end of image */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
426 ret = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
427 goto the_end;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
428 case '!':
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
429 if (gif_read_extension(s) < 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
430 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
431 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
432 case EOF:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
433 default:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
434 /* error or errneous EOF */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
435 ret = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
436 goto the_end;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
437 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
438 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
439 the_end:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
440 return ret;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
441 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
442
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
443 static int gif_decode_init(AVCodecContext *avctx)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
444 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
445 GifState *s = avctx->priv_data;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
446
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
447 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
448 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
449
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
450 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
451 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
452 GifState *s = avctx->priv_data;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
453 AVFrame *picture = data;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
454 int ret;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
455
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
456 s->bytestream = buf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
457 if (gif_read_header1(s) < 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
458 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
459
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
460 /* allocate image buffer */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
461 s->image_linesize = s->screen_width * 3;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
462 s->image_buf = av_malloc(s->screen_height * s->image_linesize);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
463 if (!s->image_buf)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
464 return -ENOMEM;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
465 s->pix_fmt = PIX_FMT_PAL8;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
466 /* now we are ready: build format streams */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
467
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
468 /* XXX: check if screen size is always valid */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
469 avctx->width = s->screen_width;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
470 avctx->height = s->screen_height;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
471 avctx->pix_fmt = PIX_FMT_PAL8;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
472
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
473 ret = gif_parse_next_image(s);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
474 if (ret < 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
475 return ret;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
476
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
477 picture->data[0] = s->image_buf;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
478 picture->linesize[0] = s->image_linesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
479 picture->data[1] = s->image_palette;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
480 picture->linesize[1] = 4;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
481
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
482 *data_size = sizeof(AVPicture);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
483 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
484 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
485
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
486 AVCodec gif_decoder = {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
487 "gif",
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
488 CODEC_TYPE_VIDEO,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
489 CODEC_ID_GIF,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
490 sizeof(GifState),
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
491 gif_decode_init,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
492 NULL,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
493 NULL,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
494 gif_decode_frame,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
495 };