yading@10
|
1 /*
|
yading@10
|
2 * GIF decoder
|
yading@10
|
3 * Copyright (c) 2003 Fabrice Bellard
|
yading@10
|
4 * Copyright (c) 2006 Baptiste Coudurier
|
yading@10
|
5 * Copyright (c) 2012 Vitaliy E Sugrobov
|
yading@10
|
6 *
|
yading@10
|
7 * This file is part of FFmpeg.
|
yading@10
|
8 *
|
yading@10
|
9 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
10 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
11 * License as published by the Free Software Foundation; either
|
yading@10
|
12 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
13 *
|
yading@10
|
14 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
17 * Lesser General Public License for more details.
|
yading@10
|
18 *
|
yading@10
|
19 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
20 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
22 */
|
yading@10
|
23
|
yading@10
|
24 //#define DEBUG
|
yading@10
|
25
|
yading@10
|
26 #include "libavutil/imgutils.h"
|
yading@10
|
27 #include "libavutil/opt.h"
|
yading@10
|
28 #include "avcodec.h"
|
yading@10
|
29 #include "bytestream.h"
|
yading@10
|
30 #include "internal.h"
|
yading@10
|
31 #include "lzw.h"
|
yading@10
|
32 #include "gif.h"
|
yading@10
|
33
|
yading@10
|
34 /* This value is intentionally set to "transparent white" color.
|
yading@10
|
35 * It is much better to have white background instead of black
|
yading@10
|
36 * when gif image converted to format which not support transparency.
|
yading@10
|
37 */
|
yading@10
|
38 #define GIF_TRANSPARENT_COLOR 0x00ffffff
|
yading@10
|
39
|
yading@10
|
40 typedef struct GifState {
|
yading@10
|
41 const AVClass *class;
|
yading@10
|
42 AVFrame *frame;
|
yading@10
|
43 int screen_width;
|
yading@10
|
44 int screen_height;
|
yading@10
|
45 int has_global_palette;
|
yading@10
|
46 int bits_per_pixel;
|
yading@10
|
47 uint32_t bg_color;
|
yading@10
|
48 int background_color_index;
|
yading@10
|
49 int transparent_color_index;
|
yading@10
|
50 int color_resolution;
|
yading@10
|
51 /* intermediate buffer for storing color indices
|
yading@10
|
52 * obtained from lzw-encoded data stream */
|
yading@10
|
53 uint8_t *idx_line;
|
yading@10
|
54 int idx_line_size;
|
yading@10
|
55
|
yading@10
|
56 /* after the frame is displayed, the disposal method is used */
|
yading@10
|
57 int gce_prev_disposal;
|
yading@10
|
58 int gce_disposal;
|
yading@10
|
59 /* rectangle describing area that must be disposed */
|
yading@10
|
60 int gce_l, gce_t, gce_w, gce_h;
|
yading@10
|
61 /* depending on disposal method we store either part of the image
|
yading@10
|
62 * drawn on the canvas or background color that
|
yading@10
|
63 * should be used upon disposal */
|
yading@10
|
64 uint32_t * stored_img;
|
yading@10
|
65 int stored_img_size;
|
yading@10
|
66 int stored_bg_color;
|
yading@10
|
67
|
yading@10
|
68 GetByteContext gb;
|
yading@10
|
69 /* LZW compatible decoder */
|
yading@10
|
70 LZWState *lzw;
|
yading@10
|
71
|
yading@10
|
72 /* aux buffers */
|
yading@10
|
73 uint32_t global_palette[256];
|
yading@10
|
74 uint32_t local_palette[256];
|
yading@10
|
75
|
yading@10
|
76 AVCodecContext *avctx;
|
yading@10
|
77 int keyframe;
|
yading@10
|
78 int keyframe_ok;
|
yading@10
|
79 int trans_color; /**< color value that is used instead of transparent color */
|
yading@10
|
80 } GifState;
|
yading@10
|
81
|
yading@10
|
82 static void gif_read_palette(GifState *s, uint32_t *pal, int nb)
|
yading@10
|
83 {
|
yading@10
|
84 int i;
|
yading@10
|
85
|
yading@10
|
86 for (i = 0; i < nb; i++, pal++)
|
yading@10
|
87 *pal = (0xffu << 24) | bytestream2_get_be24u(&s->gb);
|
yading@10
|
88 }
|
yading@10
|
89
|
yading@10
|
90 static void gif_fill(AVFrame *picture, uint32_t color)
|
yading@10
|
91 {
|
yading@10
|
92 uint32_t *p = (uint32_t *)picture->data[0];
|
yading@10
|
93 uint32_t *p_end = p + (picture->linesize[0] / sizeof(uint32_t)) * picture->height;
|
yading@10
|
94
|
yading@10
|
95 for (; p < p_end; p++)
|
yading@10
|
96 *p = color;
|
yading@10
|
97 }
|
yading@10
|
98
|
yading@10
|
99 static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h)
|
yading@10
|
100 {
|
yading@10
|
101 const int linesize = picture->linesize[0] / sizeof(uint32_t);
|
yading@10
|
102 const uint32_t *py = (uint32_t *)picture->data[0] + t * linesize;
|
yading@10
|
103 const uint32_t *pr, *pb = py + h * linesize;
|
yading@10
|
104 uint32_t *px;
|
yading@10
|
105
|
yading@10
|
106 for (; py < pb; py += linesize) {
|
yading@10
|
107 px = (uint32_t *)py + l;
|
yading@10
|
108 pr = px + w;
|
yading@10
|
109
|
yading@10
|
110 for (; px < pr; px++)
|
yading@10
|
111 *px = color;
|
yading@10
|
112 }
|
yading@10
|
113 }
|
yading@10
|
114
|
yading@10
|
115 static void gif_copy_img_rect(const uint32_t *src, uint32_t *dst,
|
yading@10
|
116 int linesize, int l, int t, int w, int h)
|
yading@10
|
117 {
|
yading@10
|
118 const int y_start = t * linesize;
|
yading@10
|
119 const uint32_t *src_px,
|
yading@10
|
120 *src_py = src + y_start,
|
yading@10
|
121 *dst_py = dst + y_start;
|
yading@10
|
122 const uint32_t *src_pb = src_py + h * linesize;
|
yading@10
|
123 uint32_t *dst_px;
|
yading@10
|
124
|
yading@10
|
125 for (; src_py < src_pb; src_py += linesize, dst_py += linesize) {
|
yading@10
|
126 src_px = src_py + l;
|
yading@10
|
127 dst_px = (uint32_t *)dst_py + l;
|
yading@10
|
128
|
yading@10
|
129 memcpy(dst_px, src_px, w * sizeof(uint32_t));
|
yading@10
|
130 }
|
yading@10
|
131 }
|
yading@10
|
132
|
yading@10
|
133 static int gif_read_image(GifState *s, AVFrame *frame)
|
yading@10
|
134 {
|
yading@10
|
135 int left, top, width, height, bits_per_pixel, code_size, flags;
|
yading@10
|
136 int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size;
|
yading@10
|
137 uint32_t *ptr, *pal, *px, *pr, *ptr1;
|
yading@10
|
138 int ret;
|
yading@10
|
139 uint8_t *idx;
|
yading@10
|
140
|
yading@10
|
141 /* At least 9 bytes of Image Descriptor. */
|
yading@10
|
142 if (bytestream2_get_bytes_left(&s->gb) < 9)
|
yading@10
|
143 return AVERROR_INVALIDDATA;
|
yading@10
|
144
|
yading@10
|
145 left = bytestream2_get_le16u(&s->gb);
|
yading@10
|
146 top = bytestream2_get_le16u(&s->gb);
|
yading@10
|
147 width = bytestream2_get_le16u(&s->gb);
|
yading@10
|
148 height = bytestream2_get_le16u(&s->gb);
|
yading@10
|
149 flags = bytestream2_get_byteu(&s->gb);
|
yading@10
|
150 is_interleaved = flags & 0x40;
|
yading@10
|
151 has_local_palette = flags & 0x80;
|
yading@10
|
152 bits_per_pixel = (flags & 0x07) + 1;
|
yading@10
|
153
|
yading@10
|
154 av_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height);
|
yading@10
|
155
|
yading@10
|
156 if (has_local_palette) {
|
yading@10
|
157 pal_size = 1 << bits_per_pixel;
|
yading@10
|
158
|
yading@10
|
159 if (bytestream2_get_bytes_left(&s->gb) < pal_size * 3)
|
yading@10
|
160 return AVERROR_INVALIDDATA;
|
yading@10
|
161
|
yading@10
|
162 gif_read_palette(s, s->local_palette, pal_size);
|
yading@10
|
163 pal = s->local_palette;
|
yading@10
|
164 } else {
|
yading@10
|
165 if (!s->has_global_palette) {
|
yading@10
|
166 av_log(s->avctx, AV_LOG_ERROR, "picture doesn't have either global or local palette.\n");
|
yading@10
|
167 return AVERROR_INVALIDDATA;
|
yading@10
|
168 }
|
yading@10
|
169
|
yading@10
|
170 pal = s->global_palette;
|
yading@10
|
171 }
|
yading@10
|
172
|
yading@10
|
173 if (s->keyframe) {
|
yading@10
|
174 if (s->transparent_color_index == -1 && s->has_global_palette) {
|
yading@10
|
175 /* transparency wasn't set before the first frame, fill with background color */
|
yading@10
|
176 gif_fill(frame, s->bg_color);
|
yading@10
|
177 } else {
|
yading@10
|
178 /* otherwise fill with transparent color.
|
yading@10
|
179 * this is necessary since by default picture filled with 0x80808080. */
|
yading@10
|
180 gif_fill(frame, s->trans_color);
|
yading@10
|
181 }
|
yading@10
|
182 }
|
yading@10
|
183
|
yading@10
|
184 /* verify that all the image is inside the screen dimensions */
|
yading@10
|
185 if (left + width > s->screen_width ||
|
yading@10
|
186 top + height > s->screen_height)
|
yading@10
|
187 return AVERROR_INVALIDDATA;
|
yading@10
|
188 if (width <= 0 || height <= 0)
|
yading@10
|
189 return AVERROR_INVALIDDATA;
|
yading@10
|
190
|
yading@10
|
191 /* process disposal method */
|
yading@10
|
192 if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) {
|
yading@10
|
193 gif_fill_rect(frame, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
|
yading@10
|
194 } else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) {
|
yading@10
|
195 gif_copy_img_rect(s->stored_img, (uint32_t *)frame->data[0],
|
yading@10
|
196 frame->linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h);
|
yading@10
|
197 }
|
yading@10
|
198
|
yading@10
|
199 s->gce_prev_disposal = s->gce_disposal;
|
yading@10
|
200
|
yading@10
|
201 if (s->gce_disposal != GCE_DISPOSAL_NONE) {
|
yading@10
|
202 s->gce_l = left; s->gce_t = top;
|
yading@10
|
203 s->gce_w = width; s->gce_h = height;
|
yading@10
|
204
|
yading@10
|
205 if (s->gce_disposal == GCE_DISPOSAL_BACKGROUND) {
|
yading@10
|
206 if (s->transparent_color_index >= 0)
|
yading@10
|
207 s->stored_bg_color = s->trans_color;
|
yading@10
|
208 else
|
yading@10
|
209 s->stored_bg_color = s->bg_color;
|
yading@10
|
210 } else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) {
|
yading@10
|
211 av_fast_malloc(&s->stored_img, &s->stored_img_size, frame->linesize[0] * frame->height);
|
yading@10
|
212 if (!s->stored_img)
|
yading@10
|
213 return AVERROR(ENOMEM);
|
yading@10
|
214
|
yading@10
|
215 gif_copy_img_rect((uint32_t *)frame->data[0], s->stored_img,
|
yading@10
|
216 frame->linesize[0] / sizeof(uint32_t), left, top, width, height);
|
yading@10
|
217 }
|
yading@10
|
218 }
|
yading@10
|
219
|
yading@10
|
220 /* Expect at least 2 bytes: 1 for lzw code size and 1 for block size. */
|
yading@10
|
221 if (bytestream2_get_bytes_left(&s->gb) < 2)
|
yading@10
|
222 return AVERROR_INVALIDDATA;
|
yading@10
|
223
|
yading@10
|
224 /* now get the image data */
|
yading@10
|
225 code_size = bytestream2_get_byteu(&s->gb);
|
yading@10
|
226 if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
|
yading@10
|
227 bytestream2_get_bytes_left(&s->gb), FF_LZW_GIF)) < 0) {
|
yading@10
|
228 av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n");
|
yading@10
|
229 return ret;
|
yading@10
|
230 }
|
yading@10
|
231
|
yading@10
|
232 /* read all the image */
|
yading@10
|
233 linesize = frame->linesize[0] / sizeof(uint32_t);
|
yading@10
|
234 ptr1 = (uint32_t *)frame->data[0] + top * linesize + left;
|
yading@10
|
235 ptr = ptr1;
|
yading@10
|
236 pass = 0;
|
yading@10
|
237 y1 = 0;
|
yading@10
|
238 for (y = 0; y < height; y++) {
|
yading@10
|
239 if (ff_lzw_decode(s->lzw, s->idx_line, width) == 0)
|
yading@10
|
240 goto decode_tail;
|
yading@10
|
241
|
yading@10
|
242 pr = ptr + width;
|
yading@10
|
243
|
yading@10
|
244 for (px = ptr, idx = s->idx_line; px < pr; px++, idx++) {
|
yading@10
|
245 if (*idx != s->transparent_color_index)
|
yading@10
|
246 *px = pal[*idx];
|
yading@10
|
247 }
|
yading@10
|
248
|
yading@10
|
249 if (is_interleaved) {
|
yading@10
|
250 switch(pass) {
|
yading@10
|
251 default:
|
yading@10
|
252 case 0:
|
yading@10
|
253 case 1:
|
yading@10
|
254 y1 += 8;
|
yading@10
|
255 ptr += linesize * 8;
|
yading@10
|
256 if (y1 >= height) {
|
yading@10
|
257 y1 = pass ? 2 : 4;
|
yading@10
|
258 ptr = ptr1 + linesize * y1;
|
yading@10
|
259 pass++;
|
yading@10
|
260 }
|
yading@10
|
261 break;
|
yading@10
|
262 case 2:
|
yading@10
|
263 y1 += 4;
|
yading@10
|
264 ptr += linesize * 4;
|
yading@10
|
265 if (y1 >= height) {
|
yading@10
|
266 y1 = 1;
|
yading@10
|
267 ptr = ptr1 + linesize;
|
yading@10
|
268 pass++;
|
yading@10
|
269 }
|
yading@10
|
270 break;
|
yading@10
|
271 case 3:
|
yading@10
|
272 y1 += 2;
|
yading@10
|
273 ptr += linesize * 2;
|
yading@10
|
274 break;
|
yading@10
|
275 }
|
yading@10
|
276 } else {
|
yading@10
|
277 ptr += linesize;
|
yading@10
|
278 }
|
yading@10
|
279 }
|
yading@10
|
280
|
yading@10
|
281 decode_tail:
|
yading@10
|
282 /* read the garbage data until end marker is found */
|
yading@10
|
283 ff_lzw_decode_tail(s->lzw);
|
yading@10
|
284
|
yading@10
|
285 /* Graphic Control Extension's scope is single frame.
|
yading@10
|
286 * Remove its influence. */
|
yading@10
|
287 s->transparent_color_index = -1;
|
yading@10
|
288 s->gce_disposal = GCE_DISPOSAL_NONE;
|
yading@10
|
289
|
yading@10
|
290 return 0;
|
yading@10
|
291 }
|
yading@10
|
292
|
yading@10
|
293 static int gif_read_extension(GifState *s)
|
yading@10
|
294 {
|
yading@10
|
295 int ext_code, ext_len, gce_flags, gce_transparent_index;
|
yading@10
|
296
|
yading@10
|
297 /* There must be at least 2 bytes:
|
yading@10
|
298 * 1 for extension label and 1 for extension length. */
|
yading@10
|
299 if (bytestream2_get_bytes_left(&s->gb) < 2)
|
yading@10
|
300 return AVERROR_INVALIDDATA;
|
yading@10
|
301
|
yading@10
|
302 ext_code = bytestream2_get_byteu(&s->gb);
|
yading@10
|
303 ext_len = bytestream2_get_byteu(&s->gb);
|
yading@10
|
304
|
yading@10
|
305 av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
|
yading@10
|
306
|
yading@10
|
307 switch(ext_code) {
|
yading@10
|
308 case GIF_GCE_EXT_LABEL:
|
yading@10
|
309 if (ext_len != 4)
|
yading@10
|
310 goto discard_ext;
|
yading@10
|
311
|
yading@10
|
312 /* We need at least 5 bytes more: 4 is for extension body
|
yading@10
|
313 * and 1 for next block size. */
|
yading@10
|
314 if (bytestream2_get_bytes_left(&s->gb) < 5)
|
yading@10
|
315 return AVERROR_INVALIDDATA;
|
yading@10
|
316
|
yading@10
|
317 gce_flags = bytestream2_get_byteu(&s->gb);
|
yading@10
|
318 bytestream2_skipu(&s->gb, 2); // delay during which the frame is shown
|
yading@10
|
319 gce_transparent_index = bytestream2_get_byteu(&s->gb);
|
yading@10
|
320 if (gce_flags & 0x01)
|
yading@10
|
321 s->transparent_color_index = gce_transparent_index;
|
yading@10
|
322 else
|
yading@10
|
323 s->transparent_color_index = -1;
|
yading@10
|
324 s->gce_disposal = (gce_flags >> 2) & 0x7;
|
yading@10
|
325
|
yading@10
|
326 av_dlog(s->avctx, "gce_flags=%x tcolor=%d disposal=%d\n",
|
yading@10
|
327 gce_flags,
|
yading@10
|
328 s->transparent_color_index, s->gce_disposal);
|
yading@10
|
329
|
yading@10
|
330 if (s->gce_disposal > 3) {
|
yading@10
|
331 s->gce_disposal = GCE_DISPOSAL_NONE;
|
yading@10
|
332 av_dlog(s->avctx, "invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);
|
yading@10
|
333 }
|
yading@10
|
334
|
yading@10
|
335 ext_len = bytestream2_get_byteu(&s->gb);
|
yading@10
|
336 break;
|
yading@10
|
337 }
|
yading@10
|
338
|
yading@10
|
339 /* NOTE: many extension blocks can come after */
|
yading@10
|
340 discard_ext:
|
yading@10
|
341 while (ext_len) {
|
yading@10
|
342 /* There must be at least ext_len bytes and 1 for next block size byte. */
|
yading@10
|
343 if (bytestream2_get_bytes_left(&s->gb) < ext_len + 1)
|
yading@10
|
344 return AVERROR_INVALIDDATA;
|
yading@10
|
345
|
yading@10
|
346 bytestream2_skipu(&s->gb, ext_len);
|
yading@10
|
347 ext_len = bytestream2_get_byteu(&s->gb);
|
yading@10
|
348
|
yading@10
|
349 av_dlog(s->avctx, "ext_len1=%d\n", ext_len);
|
yading@10
|
350 }
|
yading@10
|
351 return 0;
|
yading@10
|
352 }
|
yading@10
|
353
|
yading@10
|
354 static int gif_read_header1(GifState *s)
|
yading@10
|
355 {
|
yading@10
|
356 uint8_t sig[6];
|
yading@10
|
357 int v, n;
|
yading@10
|
358 int background_color_index;
|
yading@10
|
359
|
yading@10
|
360 if (bytestream2_get_bytes_left(&s->gb) < 13)
|
yading@10
|
361 return AVERROR_INVALIDDATA;
|
yading@10
|
362
|
yading@10
|
363 /* read gif signature */
|
yading@10
|
364 bytestream2_get_bufferu(&s->gb, sig, 6);
|
yading@10
|
365 if (memcmp(sig, gif87a_sig, 6) &&
|
yading@10
|
366 memcmp(sig, gif89a_sig, 6))
|
yading@10
|
367 return AVERROR_INVALIDDATA;
|
yading@10
|
368
|
yading@10
|
369 /* read screen header */
|
yading@10
|
370 s->transparent_color_index = -1;
|
yading@10
|
371 s->screen_width = bytestream2_get_le16u(&s->gb);
|
yading@10
|
372 s->screen_height = bytestream2_get_le16u(&s->gb);
|
yading@10
|
373
|
yading@10
|
374 v = bytestream2_get_byteu(&s->gb);
|
yading@10
|
375 s->color_resolution = ((v & 0x70) >> 4) + 1;
|
yading@10
|
376 s->has_global_palette = (v & 0x80);
|
yading@10
|
377 s->bits_per_pixel = (v & 0x07) + 1;
|
yading@10
|
378 background_color_index = bytestream2_get_byteu(&s->gb);
|
yading@10
|
379 n = bytestream2_get_byteu(&s->gb);
|
yading@10
|
380 if (n) {
|
yading@10
|
381 s->avctx->sample_aspect_ratio.num = n + 15;
|
yading@10
|
382 s->avctx->sample_aspect_ratio.den = 64;
|
yading@10
|
383 }
|
yading@10
|
384
|
yading@10
|
385 av_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
|
yading@10
|
386 s->screen_width, s->screen_height, s->bits_per_pixel,
|
yading@10
|
387 s->has_global_palette);
|
yading@10
|
388
|
yading@10
|
389 if (s->has_global_palette) {
|
yading@10
|
390 s->background_color_index = background_color_index;
|
yading@10
|
391 n = 1 << s->bits_per_pixel;
|
yading@10
|
392 if (bytestream2_get_bytes_left(&s->gb) < n * 3)
|
yading@10
|
393 return AVERROR_INVALIDDATA;
|
yading@10
|
394
|
yading@10
|
395 gif_read_palette(s, s->global_palette, n);
|
yading@10
|
396 s->bg_color = s->global_palette[s->background_color_index];
|
yading@10
|
397 } else
|
yading@10
|
398 s->background_color_index = -1;
|
yading@10
|
399
|
yading@10
|
400 return 0;
|
yading@10
|
401 }
|
yading@10
|
402
|
yading@10
|
403 static int gif_parse_next_image(GifState *s, AVFrame *frame)
|
yading@10
|
404 {
|
yading@10
|
405 while (bytestream2_get_bytes_left(&s->gb)) {
|
yading@10
|
406 int code = bytestream2_get_byte(&s->gb);
|
yading@10
|
407 int ret;
|
yading@10
|
408
|
yading@10
|
409 av_log(s->avctx, AV_LOG_DEBUG, "code=%02x '%c'\n", code, code);
|
yading@10
|
410
|
yading@10
|
411 switch (code) {
|
yading@10
|
412 case GIF_IMAGE_SEPARATOR:
|
yading@10
|
413 return gif_read_image(s, frame);
|
yading@10
|
414 case GIF_EXTENSION_INTRODUCER:
|
yading@10
|
415 if ((ret = gif_read_extension(s)) < 0)
|
yading@10
|
416 return ret;
|
yading@10
|
417 break;
|
yading@10
|
418 case GIF_TRAILER:
|
yading@10
|
419 /* end of image */
|
yading@10
|
420 return AVERROR_EOF;
|
yading@10
|
421 default:
|
yading@10
|
422 /* erroneous block label */
|
yading@10
|
423 return AVERROR_INVALIDDATA;
|
yading@10
|
424 }
|
yading@10
|
425 }
|
yading@10
|
426 return AVERROR_EOF;
|
yading@10
|
427 }
|
yading@10
|
428
|
yading@10
|
429 static av_cold int gif_decode_init(AVCodecContext *avctx)
|
yading@10
|
430 {
|
yading@10
|
431 GifState *s = avctx->priv_data;
|
yading@10
|
432
|
yading@10
|
433 s->avctx = avctx;
|
yading@10
|
434
|
yading@10
|
435 avctx->pix_fmt = AV_PIX_FMT_RGB32;
|
yading@10
|
436 s->frame = av_frame_alloc();
|
yading@10
|
437 if (!s->frame)
|
yading@10
|
438 return AVERROR(ENOMEM);
|
yading@10
|
439 ff_lzw_decode_open(&s->lzw);
|
yading@10
|
440 return 0;
|
yading@10
|
441 }
|
yading@10
|
442
|
yading@10
|
443 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
|
yading@10
|
444 {
|
yading@10
|
445 GifState *s = avctx->priv_data;
|
yading@10
|
446 int ret;
|
yading@10
|
447
|
yading@10
|
448 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
|
yading@10
|
449
|
yading@10
|
450 s->frame->pts = avpkt->pts;
|
yading@10
|
451 s->frame->pkt_pts = avpkt->pts;
|
yading@10
|
452 s->frame->pkt_dts = avpkt->dts;
|
yading@10
|
453 av_frame_set_pkt_duration(s->frame, avpkt->duration);
|
yading@10
|
454
|
yading@10
|
455 if (avpkt->size >= 6) {
|
yading@10
|
456 s->keyframe = memcmp(avpkt->data, gif87a_sig, 6) == 0 ||
|
yading@10
|
457 memcmp(avpkt->data, gif89a_sig, 6) == 0;
|
yading@10
|
458 } else {
|
yading@10
|
459 s->keyframe = 0;
|
yading@10
|
460 }
|
yading@10
|
461
|
yading@10
|
462 if (s->keyframe) {
|
yading@10
|
463 s->keyframe_ok = 0;
|
yading@10
|
464 if ((ret = gif_read_header1(s)) < 0)
|
yading@10
|
465 return ret;
|
yading@10
|
466
|
yading@10
|
467 if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
|
yading@10
|
468 return ret;
|
yading@10
|
469 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
|
yading@10
|
470
|
yading@10
|
471 av_frame_unref(s->frame);
|
yading@10
|
472 if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
|
yading@10
|
473 return ret;
|
yading@10
|
474
|
yading@10
|
475 av_fast_malloc(&s->idx_line, &s->idx_line_size, s->screen_width);
|
yading@10
|
476 if (!s->idx_line)
|
yading@10
|
477 return AVERROR(ENOMEM);
|
yading@10
|
478
|
yading@10
|
479 s->frame->pict_type = AV_PICTURE_TYPE_I;
|
yading@10
|
480 s->frame->key_frame = 1;
|
yading@10
|
481 s->keyframe_ok = 1;
|
yading@10
|
482 } else {
|
yading@10
|
483 if (!s->keyframe_ok) {
|
yading@10
|
484 av_log(avctx, AV_LOG_ERROR, "cannot decode frame without keyframe\n");
|
yading@10
|
485 return AVERROR_INVALIDDATA;
|
yading@10
|
486 }
|
yading@10
|
487
|
yading@10
|
488 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
|
yading@10
|
489 return ret;
|
yading@10
|
490
|
yading@10
|
491 s->frame->pict_type = AV_PICTURE_TYPE_P;
|
yading@10
|
492 s->frame->key_frame = 0;
|
yading@10
|
493 }
|
yading@10
|
494
|
yading@10
|
495 ret = gif_parse_next_image(s, s->frame);
|
yading@10
|
496 if (ret < 0)
|
yading@10
|
497 return ret;
|
yading@10
|
498
|
yading@10
|
499 if ((ret = av_frame_ref(data, s->frame)) < 0)
|
yading@10
|
500 return ret;
|
yading@10
|
501 *got_frame = 1;
|
yading@10
|
502
|
yading@10
|
503 return avpkt->size;
|
yading@10
|
504 }
|
yading@10
|
505
|
yading@10
|
506 static av_cold int gif_decode_close(AVCodecContext *avctx)
|
yading@10
|
507 {
|
yading@10
|
508 GifState *s = avctx->priv_data;
|
yading@10
|
509
|
yading@10
|
510 ff_lzw_decode_close(&s->lzw);
|
yading@10
|
511 av_frame_free(&s->frame);
|
yading@10
|
512 av_freep(&s->idx_line);
|
yading@10
|
513 av_freep(&s->stored_img);
|
yading@10
|
514
|
yading@10
|
515 return 0;
|
yading@10
|
516 }
|
yading@10
|
517
|
yading@10
|
518 static const AVOption options[] = {
|
yading@10
|
519 { "trans_color", "color value (ARGB) that is used instead of transparent color",
|
yading@10
|
520 offsetof(GifState, trans_color), AV_OPT_TYPE_INT,
|
yading@10
|
521 {.i64 = GIF_TRANSPARENT_COLOR}, 0, 0xffffffff,
|
yading@10
|
522 AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM },
|
yading@10
|
523 { NULL },
|
yading@10
|
524 };
|
yading@10
|
525
|
yading@10
|
526 static const AVClass decoder_class = {
|
yading@10
|
527 .class_name = "gif decoder",
|
yading@10
|
528 .item_name = av_default_item_name,
|
yading@10
|
529 .option = options,
|
yading@10
|
530 .version = LIBAVUTIL_VERSION_INT,
|
yading@10
|
531 .category = AV_CLASS_CATEGORY_DECODER,
|
yading@10
|
532 };
|
yading@10
|
533
|
yading@10
|
534 AVCodec ff_gif_decoder = {
|
yading@10
|
535 .name = "gif",
|
yading@10
|
536 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
537 .id = AV_CODEC_ID_GIF,
|
yading@10
|
538 .priv_data_size = sizeof(GifState),
|
yading@10
|
539 .init = gif_decode_init,
|
yading@10
|
540 .close = gif_decode_close,
|
yading@10
|
541 .decode = gif_decode_frame,
|
yading@10
|
542 .capabilities = CODEC_CAP_DR1,
|
yading@10
|
543 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
|
yading@10
|
544 .priv_class = &decoder_class,
|
yading@10
|
545 };
|