1 /*
2 * GIF decoder
3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2006 Baptiste Coudurier
5 * Copyright (c) 2012 Vitaliy E Sugrobov
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
31
32 /* This value is intentionally set to "transparent white" color.
33 * It is much better to have white background instead of black
34 * when gif image converted to format which not support transparency.
35 */
36 #define GIF_TRANSPARENT_COLOR 0x00ffffff
37
49 /* intermediate buffer for storing color indices
50 * obtained from lzw-encoded data stream */
53
54 /* after the frame is displayed, the disposal method is used */
57 /* rectangle describing area that must be disposed */
59 /* depending on disposal method we store either part of the image
60 * drawn on the canvas or background color that
61 * should be used upon disposal */
65
67 /* LZW compatible decoder */
69
70 /* aux buffers */
73
77 int trans_color;
/**< color value that is used instead of transparent color */
79
81 {
82 int i;
83
84 for (i = 0; i < nb; i++, pal++)
85 *pal = (0xffu << 24) | bytestream2_get_be24u(&s->
gb);
86 }
87
89 {
90 uint32_t *p = (uint32_t *)picture->
data[0];
91 uint32_t *p_end = p + (picture->
linesize[0] /
sizeof(uint32_t)) * picture->
height;
92
93 for (; p < p_end; p++)
94 *p = color;
95 }
96
98 {
99 const int linesize = picture->
linesize[0] /
sizeof(uint32_t);
100 const uint32_t *py = (uint32_t *)picture->
data[0] + t * linesize;
101 const uint32_t *pr, *pb = py + h * linesize;
102 uint32_t *px;
103
104 for (; py < pb; py += linesize) {
105 px = (uint32_t *)py + l;
106 pr = px + w;
107
108 for (; px < pr; px++)
109 *px = color;
110 }
111 }
112
114 int linesize,
int l,
int t,
int w,
int h)
115 {
116 const int y_start = t * linesize;
117 const uint32_t *src_px,
118 *src_py = src + y_start,
119 *dst_py = dst + y_start;
120 const uint32_t *src_pb = src_py + h * linesize;
121 uint32_t *dst_px;
122
123 for (; src_py < src_pb; src_py += linesize, dst_py += linesize) {
124 src_px = src_py + l;
125 dst_px = (uint32_t *)dst_py + l;
126
127 memcpy(dst_px, src_px, w * sizeof(uint32_t));
128 }
129 }
130
132 {
134 int is_interleaved, has_local_palette,
y,
pass, y1, linesize, pal_size;
135 uint32_t *ptr, *pal, *px, *pr, *ptr1;
138
139 /* At least 9 bytes of Image Descriptor. */
142
143 left = bytestream2_get_le16u(&s->
gb);
144 top = bytestream2_get_le16u(&s->
gb);
145 width = bytestream2_get_le16u(&s->
gb);
146 height = bytestream2_get_le16u(&s->
gb);
147 flags = bytestream2_get_byteu(&s->
gb);
148 is_interleaved = flags & 0x40;
149 has_local_palette = flags & 0x80;
150 bits_per_pixel = (flags & 0x07) + 1;
151
152 av_dlog(s->
avctx,
"image x=%d y=%d w=%d h=%d\n", left, top, width, height);
153
154 if (has_local_palette) {
155 pal_size = 1 << bits_per_pixel;
156
159
162 } else {
166 }
167
169 }
170
173 /* transparency wasn't set before the first frame, fill with background color */
175 } else {
176 /* otherwise fill with transparent color.
177 * this is necessary since by default picture filled with 0x80808080. */
179 }
180 }
181
182 /* verify that all the image is inside the screen dimensions */
186 if (width <= 0 || height <= 0)
188
189 /* process disposal method */
195 }
196
198
202
206 else
212
214 frame->
linesize[0] /
sizeof(uint32_t), left, top, width, height);
215 }
216 }
217
218 /* Expect at least 2 bytes: 1 for lzw code size and 1 for block size. */
221
222 /* now get the image data */
223 code_size = bytestream2_get_byteu(&s->
gb);
228 }
229
230 /* read all the image */
231 linesize = frame->
linesize[0] /
sizeof(uint32_t);
232 ptr1 = (uint32_t *)frame->
data[0] + top * linesize + left;
233 ptr = ptr1;
234 pass = 0;
235 y1 = 0;
238 goto decode_tail;
239
241
242 for (px = ptr, idx = s->
idx_line; px < pr; px++, idx++) {
244 *px = pal[*idx];
245 }
246
247 if (is_interleaved) {
248 switch(pass) {
249 default:
250 case 0:
251 case 1:
252 y1 += 8;
253 ptr += linesize * 8;
254 if (y1 >= height) {
255 y1 = pass ? 2 : 4;
256 ptr = ptr1 + linesize * y1;
257 pass++;
258 }
259 break;
260 case 2:
261 y1 += 4;
262 ptr += linesize * 4;
263 if (y1 >= height) {
264 y1 = 1;
265 ptr = ptr1 + linesize;
266 pass++;
267 }
268 break;
269 case 3:
270 y1 += 2;
271 ptr += linesize * 2;
272 break;
273 }
274 } else {
275 ptr += linesize;
276 }
277 }
278
279 decode_tail:
280 /* read the garbage data until end marker is found */
282
283 /* Graphic Control Extension's scope is single frame.
284 * Remove its influence. */
287
288 return 0;
289 }
290
292 {
293 int ext_code, ext_len, gce_flags, gce_transparent_index;
294
295 /* There must be at least 2 bytes:
296 * 1 for extension label and 1 for extension length. */
299
300 ext_code = bytestream2_get_byteu(&s->
gb);
301 ext_len = bytestream2_get_byteu(&s->
gb);
302
303 av_dlog(s->
avctx,
"ext_code=0x%x len=%d\n", ext_code, ext_len);
304
305 switch(ext_code) {
307 if (ext_len != 4)
308 goto discard_ext;
309
310 /* We need at least 5 bytes more: 4 is for extension body
311 * and 1 for next block size. */
314
315 gce_flags = bytestream2_get_byteu(&s->
gb);
317 gce_transparent_index = bytestream2_get_byteu(&s->
gb);
318 if (gce_flags & 0x01)
320 else
323
324 av_dlog(s->
avctx,
"gce_flags=%x tcolor=%d disposal=%d\n",
325 gce_flags,
327
330 av_dlog(s->
avctx,
"invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);
331 }
332
333 ext_len = bytestream2_get_byteu(&s->
gb);
334 break;
335 }
336
337 /* NOTE: many extension blocks can come after */
338 discard_ext:
339 while (ext_len) {
340 /* There must be at least ext_len bytes and 1 for next block size byte. */
343
345 ext_len = bytestream2_get_byteu(&s->
gb);
346
348 }
349 return 0;
350 }
351
353 {
356 int background_color_index;
357
360
361 /* read gif signature */
366
367 /* read screen header */
371
372 v = bytestream2_get_byteu(&s->
gb);
376 background_color_index = bytestream2_get_byteu(&s->
gb);
377 n = bytestream2_get_byteu(&s->
gb);
378 if (n) {
381 }
382
383 av_dlog(s->
avctx,
"screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
386
392
395 } else
397
398 return 0;
399 }
400
402 {
404 int code = bytestream2_get_byte(&s->
gb);
406
408
409 switch (code) {
415 break;
417 /* end of image */
419 default:
420 /* erroneous block label */
422 }
423 }
425 }
426
428 {
430
432
438 return 0;
439 }
440
442 {
445
447
452
453 if (avpkt->
size >= 6) {
456 } else {
458 }
459
465
469
473
477
481 } else {
485 }
486
489
492 }
493
495 if (ret < 0)
497
500 *got_frame = 1;
501
503 }
504
506 {
508
513
514 return 0;
515 }
516
518 { "trans_color", "color value (ARGB) that is used instead of transparent color",
522 { NULL },
523 };
524
531 };
532
544 };