1 /*
2 * GIF demuxer
3 * Copyright (c) 2012 Vitaliy E Sugrobov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * GIF demuxer.
25 */
26
33
36 /**
37 * Time span in hundredths of second before
38 * the next frame should be drawn on screen.
39 */
41 /**
42 * Minimum allowed delay between frames in hundredths of
43 * second. Values below this threshold considered to be
44 * invalid and set to value of default_delay.
45 */
49
50 /**
51 * loop options
52 */
56
60
61 /**
62 * Major web browsers display gifs at ~10-15fps when rate
63 * is not explicitly set or have too low values. We assume default rate to be 10.
64 * Default delay = 100hundredths of second / 10fps = 10hos per frame.
65 */
66 #define GIF_DEFAULT_DELAY 10
67 /**
68 * By default delay values less than this threshold considered to be invalid.
69 */
70 #define GIF_MIN_DELAY 2
71
73 {
74 /* check magick */
76 return 0;
77
78 /* width or height contains zero? */
80 return 0;
81
83 }
84
86 {
88 for (
i = 0;
i < 6;
i++) {
94 }
95 return 0;
96 }
97
99 {
100 int sb_size,
ret = 0;
101
102 while (0x00 != (sb_size =
avio_r8(pb))) {
105 }
106
108 }
109
111 {
116 int64_t nb_frames = 0,
duration = 0;
117
120
127
130
132 if (!st)
134
137
140 break;
144 AVBPrint bp;
145 int block_size;
146
148 while ((block_size =
avio_r8(pb)) != 0) {
150 }
155
156 if (block_size == 4) {
157 int delay;
158
161 if (delay < gdc->min_delay)
166 } else {
168 }
170 } else {
172 }
180 nb_frames++;
181 } else {
182 break;
183 }
184 }
185
186 /* GIF format operates with time in "hundredths of second",
187 * therefore timebase is 1/100 */
196 if (n) {
199 }
200
201 /* jump to start because gif decoder needs header data too */
204
205 return 0;
206 }
207
209 {
212 int sb_size, ext_label =
avio_r8(pb);
214
216 if ((sb_size =
avio_r8(pb)) < 4) {
219 }
220
221 /* skip packed fields */
224
226
230
231 /* skip the rest of the Graphic Control Extension block */
236
239 if (
ret < 0 || !sb_size)
241
245 if (
ret < 0 || !sb_size)
247
248 if (sb_size == 3 &&
data[0] == 1) {
251
254 }
255 }
256 }
257
260
261 return 0;
262 }
263
265 {
268 int packed_fields, block_label, ct_size,
269 keyframe, frame_parsed = 0,
ret;
271 unsigned char buf[6];
272
276 }
else if (
ret < 0) {
278 } else {
279 keyframe = 0;
280 }
281
282 if (keyframe) {
283 parse_keyframe:
284 /* skip 2 bytes of width and 2 of height */
287
289
290 /* skip 1 byte of Background Color Index and 1 byte of Pixel Aspect Ratio */
293
294 /* global color table presence */
295 if (packed_fields & 0x80) {
296 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
297
300 }
301 } else {
304 }
305
311 /* skip to last byte of Image Descriptor header */
314
316
317 /* local color table presence */
318 if (packed_fields & 0x80) {
319 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
320
323 }
324
325 /* read LZW Minimum Code Size */
329 }
330
333
335
338
342
343 if (keyframe)
345
348
351
352 /* Graphic Control Extension's scope is single frame.
353 * Remove its influence. */
355 frame_parsed = 1;
356
357 break;
358 } else {
361 if (!keyframe)
366 keyframe = 1;
367 goto parse_keyframe;
368 }
369 }
370
374 }
375 /* This might happen when there is no image block
376 * between extension blocks and GIF_TRAILER or EOF */
381 } else
383 }
384
391 };
392
399 };
400
410 };