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
68
69 /* aux buffers */
72
76 int trans_color;
/**< color value that is used instead of transparent color */
78
80 {
81 int i;
82
83 for (i = 0; i < nb; i++, pal++)
84 *pal = (0xffu << 24) | bytestream2_get_be24u(&s->
gb);
85 }
86
88 {
89 uint32_t *p = (uint32_t *)picture->
data[0];
90 uint32_t *p_end = p + (picture->
linesize[0] /
sizeof(uint32_t)) * picture->
height;
91
92 for (; p < p_end; p++)
93 *p = color;
94 }
95
97 {
98 const int linesize = picture->
linesize[0] /
sizeof(uint32_t);
99 const uint32_t *py = (uint32_t *)picture->
data[0] + t * linesize;
100 const uint32_t *pr, *pb = py + h * linesize;
101 uint32_t *px;
102
103 for (; py < pb; py += linesize) {
104 px = (uint32_t *)py + l;
105 pr = px + w;
106
107 for (; px < pr; px++)
108 *px = color;
109 }
110 }
111
113 int linesize, int l, int t, int w, int h)
114 {
115 const int y_start = t * linesize;
116 const uint32_t *src_px,
117 *src_py = src + y_start,
118 *dst_py = dst + y_start;
119 const uint32_t *src_pb = src_py + h * linesize;
120 uint32_t *dst_px;
121
122 for (; src_py < src_pb; src_py += linesize, dst_py += linesize) {
123 src_px = src_py + l;
124 dst_px = (uint32_t *)dst_py + l;
125
126 memcpy(dst_px, src_px, w * sizeof(uint32_t));
127 }
128 }
129
131 {
133 int is_interleaved, has_local_palette,
y,
pass, y1, linesize, pal_size;
134 uint32_t *ptr, *pal, *px, *pr, *ptr1;
137
138 /* At least 9 bytes of Image Descriptor. */
141
142 left = bytestream2_get_le16u(&s->
gb);
143 top = bytestream2_get_le16u(&s->
gb);
144 width = bytestream2_get_le16u(&s->
gb);
145 height = bytestream2_get_le16u(&s->
gb);
146 flags = bytestream2_get_byteu(&s->
gb);
147 is_interleaved = flags & 0x40;
148 has_local_palette = flags & 0x80;
149 bits_per_pixel = (flags & 0x07) + 1;
150
151 av_dlog(s->
avctx,
"image x=%d y=%d w=%d h=%d\n", left, top, width, height);
152
153 if (has_local_palette) {
154 pal_size = 1 << bits_per_pixel;
155
158
161 } else {
165 }
166
168 }
169
172 /* transparency wasn't set before the first frame, fill with background color */
174 } else {
175 /* otherwise fill with transparent color.
176 * this is necessary since by default picture filled with 0x80808080. */
178 }
179 }
180
181 /* verify that all the image is inside the screen dimensions */
185 }
189 }
191 /* width must be kept around to avoid lzw vs line desync */
195 } else {
197 }
199 /* we don't care about the extra invisible lines */
203 }
204
205 /* process disposal method */
211 }
212
214
218
222 else
228
230 frame->
linesize[0] /
sizeof(uint32_t), left, top, pw, height);
231 }
232 }
233
234 /* Expect at least 2 bytes: 1 for lzw code size and 1 for block size. */
237
238 /* now get the image data */
239 code_size = bytestream2_get_byteu(&s->
gb);
244 }
245
246 /* read all the image */
247 linesize = frame->
linesize[0] /
sizeof(uint32_t);
248 ptr1 = (uint32_t *)frame->
data[0] + top * linesize + left;
249 ptr = ptr1;
250 pass = 0;
251 y1 = 0;
254 if (count != width) {
255 if (count)
257 goto decode_tail;
258 }
259
260 pr = ptr + pw;
261
262 for (px = ptr, idx = s->
idx_line; px < pr; px++, idx++) {
264 *px = pal[*idx];
265 }
266
267 if (is_interleaved) {
268 switch(pass) {
269 default:
270 case 0:
271 case 1:
272 y1 += 8;
273 ptr += linesize * 8;
274 break;
275 case 2:
276 y1 += 4;
277 ptr += linesize * 4;
278 break;
279 case 3:
280 y1 += 2;
281 ptr += linesize * 2;
282 break;
283 }
284 while (y1 >= height) {
286 ptr = ptr1 + linesize * y1;
287 pass++;
288 }
289 } else {
290 ptr += linesize;
291 }
292 }
293
294 decode_tail:
295 /* read the garbage data until end marker is found */
297
298 /* Graphic Control Extension's scope is single frame.
299 * Remove its influence. */
302
303 return 0;
304 }
305
307 {
308 int ext_code, ext_len, gce_flags, gce_transparent_index;
309
310 /* There must be at least 2 bytes:
311 * 1 for extension label and 1 for extension length. */
314
315 ext_code = bytestream2_get_byteu(&s->
gb);
316 ext_len = bytestream2_get_byteu(&s->
gb);
317
318 av_dlog(s->
avctx,
"ext_code=0x%x len=%d\n", ext_code, ext_len);
319
320 switch(ext_code) {
322 if (ext_len != 4)
323 goto discard_ext;
324
325 /* We need at least 5 bytes more: 4 is for extension body
326 * and 1 for next block size. */
329
330 gce_flags = bytestream2_get_byteu(&s->
gb);
332 gce_transparent_index = bytestream2_get_byteu(&s->
gb);
333 if (gce_flags & 0x01)
335 else
338
339 av_dlog(s->
avctx,
"gce_flags=%x tcolor=%d disposal=%d\n",
340 gce_flags,
342
345 av_dlog(s->
avctx,
"invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);
346 }
347
348 ext_len = bytestream2_get_byteu(&s->
gb);
349 break;
350 }
351
352 /* NOTE: many extension blocks can come after */
353 discard_ext:
354 while (ext_len) {
355 /* There must be at least ext_len bytes and 1 for next block size byte. */
358
360 ext_len = bytestream2_get_byteu(&s->
gb);
361
363 }
364 return 0;
365 }
366
368 {
371 int background_color_index;
372
375
376 /* read gif signature */
381
382 /* read screen header */
386
387 v = bytestream2_get_byteu(&s->
gb);
391 background_color_index = bytestream2_get_byteu(&s->
gb);
392 n = bytestream2_get_byteu(&s->
gb);
393 if (n) {
396 }
397
398 av_dlog(s->
avctx,
"screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
401
407
410 } else
412
413 return 0;
414 }
415
417 {
419 int code = bytestream2_get_byte(&s->
gb);
421
423
424 switch (code) {
430 break;
432 /* end of image */
434 default:
435 /* erroneous block label */
437 }
438 }
440 }
441
443 {
445
447
453 return 0;
454 }
455
457 {
460
462
467
468 if (avpkt->
size >= 6) {
471 } else {
473 }
474
480
483
487
491
495 } else {
499 }
500
503
506 }
507
509 if (ret < 0)
511
514 *got_frame = 1;
515
517 }
518
520 {
522
527
528 return 0;
529 }
530
532 { "trans_color", "color value (ARGB) that is used instead of transparent color",
537 };
538
545 };
546
557 .priv_class = &decoder_class,
558 };