FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/gifdec.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 150 193 77.7%
Functions: 6 6 100.0%
Branches: 96 150 64.0%

Line Branch Exec Source
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
27 #include "avformat.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/opt.h"
31 #include "internal.h"
32 #include "libavcodec/gif.h"
33
34 typedef struct GIFDemuxContext {
35 const AVClass *class;
36 /**
37 * Time span in hundredths of second before
38 * the next frame should be drawn on screen.
39 */
40 int delay;
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 */
46 int min_delay;
47 int max_delay;
48 int default_delay;
49
50 /**
51 * loop options
52 */
53 int total_iter;
54 int iter_count;
55 int ignore_loop;
56
57 int nb_frames;
58 int last_duration;
59 } GIFDemuxContext;
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
72 6782 static int gif_probe(const AVProbeData *p)
73 {
74 /* check magick */
75
3/4
✓ Branch 0 taken 6782 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6767 times.
✓ Branch 3 taken 15 times.
6782 if (memcmp(p->buf, gif87a_sig, 6) && memcmp(p->buf, gif89a_sig, 6))
76 6767 return 0;
77
78 /* width or height contains zero? */
79
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 if (!AV_RL16(&p->buf[6]) || !AV_RL16(&p->buf[8]))
80 return 0;
81
82 15 return AVPROBE_SCORE_MAX;
83 }
84
85 15 static int resync(AVIOContext *pb)
86 {
87 int i;
88
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 15 times.
105 for (i = 0; i < 6; i++) {
89 90 int b = avio_r8(pb);
90
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
90 if (b != gif87a_sig[i] && b != gif89a_sig[i])
91 i = -(b != 'G');
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
90 if (avio_feof(pb))
93 return AVERROR_EOF;
94 }
95 15 return 0;
96 }
97
98 6409 static int gif_skip_subblocks(AVIOContext *pb)
99 {
100 6409 int sb_size, ret = 0;
101
102
2/2
✓ Branch 1 taken 190364 times.
✓ Branch 2 taken 6409 times.
196773 while (0x00 != (sb_size = avio_r8(pb))) {
103
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 190364 times.
190364 if ((ret = avio_skip(pb, sb_size)) < 0)
104 return ret;
105 }
106
107 6409 return ret;
108 }
109
110 15 static int gif_read_header(AVFormatContext *s)
111 {
112 15 GIFDemuxContext *gdc = s->priv_data;
113 15 AVIOContext *pb = s->pb;
114 AVStream *st;
115 int type, width, height, ret, n, flags;
116 15 int64_t nb_frames = 0, duration = 0;
117
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = resync(pb)) < 0)
119 return ret;
120
121 15 gdc->delay = gdc->default_delay;
122 15 width = avio_rl16(pb);
123 15 height = avio_rl16(pb);
124 15 flags = avio_r8(pb);
125 15 avio_skip(pb, 1);
126 15 n = avio_r8(pb);
127
128
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 if (width == 0 || height == 0)
129 return AVERROR_INVALIDDATA;
130
131 15 st = avformat_new_stream(s, NULL);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!st)
133 return AVERROR(ENOMEM);
134
135
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
15 if (flags & 0x80)
136 8 avio_skip(pb, 3 * (1 << ((flags & 0x07) + 1)));
137
138
2/2
✓ Branch 1 taken 2721 times.
✓ Branch 2 taken 15 times.
2736 while ((type = avio_r8(pb)) != GIF_TRAILER) {
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2721 times.
2721 if (avio_feof(pb))
140 break;
141
2/2
✓ Branch 0 taken 1366 times.
✓ Branch 1 taken 1355 times.
2721 if (type == GIF_EXTENSION_INTRODUCER) {
142 1366 int subtype = avio_r8(pb);
143
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1364 times.
1366 if (subtype == GIF_COM_EXT_LABEL) {
144 AVBPrint bp;
145 int block_size;
146
147 2 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
148
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
4 while ((block_size = avio_r8(pb)) != 0) {
149 2 avio_read_to_bprint(pb, &bp, block_size);
150 }
151 2 av_dict_set(&s->metadata, "comment", bp.str, 0);
152 2 av_bprint_finalize(&bp, NULL);
153
2/2
✓ Branch 0 taken 1355 times.
✓ Branch 1 taken 9 times.
1364 } else if (subtype == GIF_GCE_EXT_LABEL) {
154 1355 int block_size = avio_r8(pb);
155
156
1/2
✓ Branch 0 taken 1355 times.
✗ Branch 1 not taken.
1355 if (block_size == 4) {
157 int delay;
158
159 1355 avio_skip(pb, 1);
160 1355 delay = avio_rl16(pb);
161
2/2
✓ Branch 0 taken 1212 times.
✓ Branch 1 taken 143 times.
1355 if (delay < gdc->min_delay)
162 1212 delay = gdc->default_delay;
163 1355 delay = FFMIN(delay, gdc->max_delay);
164 1355 duration += delay;
165 1355 avio_skip(pb, 1);
166 } else {
167 avio_skip(pb, block_size);
168 }
169 1355 gif_skip_subblocks(pb);
170 } else {
171 9 gif_skip_subblocks(pb);
172 }
173
1/2
✓ Branch 0 taken 1355 times.
✗ Branch 1 not taken.
1355 } else if (type == GIF_IMAGE_SEPARATOR) {
174 1355 avio_skip(pb, 8);
175 1355 flags = avio_r8(pb);
176
2/2
✓ Branch 0 taken 1213 times.
✓ Branch 1 taken 142 times.
1355 if (flags & 0x80)
177 1213 avio_skip(pb, 3 * (1 << ((flags & 0x07) + 1)));
178 1355 avio_skip(pb, 1);
179 1355 gif_skip_subblocks(pb);
180 1355 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 */
188 15 avpriv_set_pts_info(st, 64, 1, 100);
189 15 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
190 15 st->codecpar->codec_id = AV_CODEC_ID_GIF;
191 15 st->codecpar->width = width;
192 15 st->codecpar->height = height;
193 15 st->start_time = 0;
194 15 st->duration = duration;
195 15 st->nb_frames = nb_frames;
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (n) {
197 st->codecpar->sample_aspect_ratio.num = n + 15;
198 st->codecpar->sample_aspect_ratio.den = 64;
199 }
200
201 /* jump to start because gif decoder needs header data too */
202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (avio_seek(pb, 0, SEEK_SET) != 0)
203 return AVERROR(EIO);
204
205 15 return 0;
206 }
207
208 1868 static int gif_read_ext(AVFormatContext *s)
209 {
210 1868 GIFDemuxContext *gdc = s->priv_data;
211 1868 AVIOContext *pb = s->pb;
212 1868 int sb_size, ext_label = avio_r8(pb);
213 int ret;
214
215
2/2
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 46 times.
1868 if (ext_label == GIF_GCE_EXT_LABEL) {
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1822 times.
1822 if ((sb_size = avio_r8(pb)) < 4) {
217 av_log(s, AV_LOG_FATAL, "Graphic Control Extension block's size less than 4.\n");
218 return AVERROR_INVALIDDATA;
219 }
220
221 /* skip packed fields */
222
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1822 times.
1822 if ((ret = avio_skip(pb, 1)) < 0)
223 return ret;
224
225 1822 gdc->delay = avio_rl16(pb);
226
227
2/2
✓ Branch 0 taken 1212 times.
✓ Branch 1 taken 610 times.
1822 if (gdc->delay < gdc->min_delay)
228 1212 gdc->delay = gdc->default_delay;
229 1822 gdc->delay = FFMIN(gdc->delay, gdc->max_delay);
230
231 /* skip the rest of the Graphic Control Extension block */
232
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1822 times.
1822 if ((ret = avio_skip(pb, sb_size - 3)) < 0 )
233 return ret;
234
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 2 times.
46 } else if (ext_label == GIF_APP_EXT_LABEL) {
235 uint8_t data[256];
236
237 44 sb_size = avio_r8(pb);
238 44 ret = avio_read(pb, data, sb_size);
239
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
44 if (ret < 0 || !sb_size)
240 return ret;
241
242
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (sb_size == strlen(NETSCAPE_EXT_STR)) {
243 44 sb_size = avio_r8(pb);
244 44 ret = avio_read(pb, data, sb_size);
245
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
44 if (ret < 0 || !sb_size)
246 return ret;
247
248
3/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
44 if (sb_size == 3 && data[0] == 1) {
249 43 gdc->total_iter = AV_RL16(data+1);
250 43 av_log(s, AV_LOG_DEBUG, "Loop count is %d\n", gdc->total_iter);
251
252
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (gdc->total_iter == 0)
253 43 gdc->total_iter = -1;
254 }
255 }
256 }
257
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1868 times.
1868 if ((ret = gif_skip_subblocks(pb)) < 0)
259 return ret;
260
261 1868 return 0;
262 }
263
264 1862 static int gif_read_packet(AVFormatContext *s, AVPacket *pkt)
265 {
266 1862 GIFDemuxContext *gdc = s->priv_data;
267 1862 AVIOContext *pb = s->pb;
268 int packed_fields, block_label, ct_size,
269 1862 keyframe, frame_parsed = 0, ret;
270 1862 int64_t frame_start = avio_tell(pb), frame_end;
271 unsigned char buf[6];
272
273
2/2
✓ Branch 1 taken 1822 times.
✓ Branch 2 taken 40 times.
1862 if ((ret = avio_read(pb, buf, 6)) == 6) {
274
1/2
✓ Branch 0 taken 1822 times.
✗ Branch 1 not taken.
3644 keyframe = memcmp(buf, gif87a_sig, 6) == 0 ||
275
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1772 times.
1822 memcmp(buf, gif89a_sig, 6) == 0;
276
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 33 times.
40 } else if (ret < 0) {
277 7 return ret;
278 } else {
279 33 keyframe = 0;
280 }
281
282
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1805 times.
1855 if (keyframe) {
283 50 parse_keyframe:
284 /* skip 2 bytes of width and 2 of height */
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if ((ret = avio_skip(pb, 4)) < 0)
286 return ret;
287
288 50 packed_fields = avio_r8(pb);
289
290 /* skip 1 byte of Background Color Index and 1 byte of Pixel Aspect Ratio */
291
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if ((ret = avio_skip(pb, 2)) < 0)
292 return ret;
293
294 /* global color table presence */
295
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 7 times.
50 if (packed_fields & 0x80) {
296 43 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
297
298
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if ((ret = avio_skip(pb, ct_size)) < 0)
299 return ret;
300 }
301 } else {
302 1805 avio_seek(pb, -ret, SEEK_CUR);
303 1805 ret = AVERROR_EOF;
304 }
305
306
3/4
✓ Branch 1 taken 3690 times.
✓ Branch 2 taken 33 times.
✓ Branch 4 taken 3690 times.
✗ Branch 5 not taken.
3723 while (GIF_TRAILER != (block_label = avio_r8(pb)) && !avio_feof(pb)) {
307
2/2
✓ Branch 0 taken 1868 times.
✓ Branch 1 taken 1822 times.
3690 if (block_label == GIF_EXTENSION_INTRODUCER) {
308
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1868 times.
1868 if ((ret = gif_read_ext (s)) < 0 )
309 goto resync;
310
1/2
✓ Branch 0 taken 1822 times.
✗ Branch 1 not taken.
1822 } else if (block_label == GIF_IMAGE_SEPARATOR) {
311 /* skip to last byte of Image Descriptor header */
312
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1822 times.
1822 if ((ret = avio_skip(pb, 8)) < 0)
313 return ret;
314
315 1822 packed_fields = avio_r8(pb);
316
317 /* local color table presence */
318
2/2
✓ Branch 0 taken 1213 times.
✓ Branch 1 taken 609 times.
1822 if (packed_fields & 0x80) {
319 1213 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
320
321
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1213 times.
1213 if ((ret = avio_skip(pb, ct_size)) < 0)
322 return ret;
323 }
324
325 /* read LZW Minimum Code Size */
326
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1822 times.
1822 if (avio_r8(pb) < 1) {
327 av_log(s, AV_LOG_ERROR, "lzw minimum code size must be >= 1\n");
328 goto resync;
329 }
330
331
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1822 times.
1822 if ((ret = gif_skip_subblocks(pb)) < 0)
332 goto resync;
333
334 1822 frame_end = avio_tell(pb);
335
336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1822 times.
1822 if (avio_seek(pb, frame_start, SEEK_SET) != frame_start)
337 return AVERROR(EIO);
338
339 1822 ret = av_get_packet(pb, pkt, frame_end - frame_start);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1822 times.
1822 if (ret < 0)
341 return ret;
342
343
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1772 times.
1822 if (keyframe)
344 50 pkt->flags |= AV_PKT_FLAG_KEY;
345
346 1822 pkt->stream_index = 0;
347 1822 pkt->duration = gdc->delay;
348
349 1822 gdc->nb_frames ++;
350 1822 gdc->last_duration = pkt->duration;
351
352 /* Graphic Control Extension's scope is single frame.
353 * Remove its influence. */
354 1822 gdc->delay = gdc->default_delay;
355 1822 frame_parsed = 1;
356
357 1822 break;
358 } else {
359 av_log(s, AV_LOG_ERROR, "invalid block label\n");
360 resync:
361 if (!keyframe)
362 avio_seek(pb, frame_start, SEEK_SET);
363 if ((ret = resync(pb)) < 0)
364 return ret;
365 frame_start = avio_tell(pb) - 6;
366 keyframe = 1;
367 goto parse_keyframe;
368 }
369 }
370
371
5/6
✓ Branch 0 taken 1822 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 1822 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 1822 times.
1855 if ((ret >= 0 && !frame_parsed) || ret == AVERROR_EOF) {
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (gdc->nb_frames == 1) {
373 s->streams[0]->r_frame_rate = (AVRational) {100, gdc->last_duration};
374 }
375 /* This might happen when there is no image block
376 * between extension blocks and GIF_TRAILER or EOF */
377
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
33 if (!gdc->ignore_loop && (block_label == GIF_TRAILER || avio_feof(pb))
378 && (gdc->total_iter < 0 || ++gdc->iter_count < gdc->total_iter))
379 return avio_seek(pb, 0, SEEK_SET);
380 33 return AVERROR_EOF;
381 } else
382 1822 return ret;
383 }
384
385 static const AVOption options[] = {
386 { "min_delay" , "minimum valid delay between frames (in hundredths of second)", offsetof(GIFDemuxContext, min_delay) , AV_OPT_TYPE_INT, {.i64 = GIF_MIN_DELAY} , 0, 100 * 60, AV_OPT_FLAG_DECODING_PARAM },
387 { "max_gif_delay", "maximum valid delay between frames (in hundredths of seconds)", offsetof(GIFDemuxContext, max_delay) , AV_OPT_TYPE_INT, {.i64 = 65535} , 0, 65535 , AV_OPT_FLAG_DECODING_PARAM },
388 { "default_delay", "default delay between frames (in hundredths of second)" , offsetof(GIFDemuxContext, default_delay), AV_OPT_TYPE_INT, {.i64 = GIF_DEFAULT_DELAY}, 0, 100 * 60, AV_OPT_FLAG_DECODING_PARAM },
389 { "ignore_loop" , "ignore loop setting (netscape extension)" , offsetof(GIFDemuxContext, ignore_loop) , AV_OPT_TYPE_BOOL,{.i64 = 1} , 0, 1, AV_OPT_FLAG_DECODING_PARAM },
390 { NULL },
391 };
392
393 static const AVClass demuxer_class = {
394 .class_name = "GIF demuxer",
395 .item_name = av_default_item_name,
396 .option = options,
397 .version = LIBAVUTIL_VERSION_INT,
398 .category = AV_CLASS_CATEGORY_DEMUXER,
399 };
400
401 const AVInputFormat ff_gif_demuxer = {
402 .name = "gif",
403 .long_name = NULL_IF_CONFIG_SMALL("CompuServe Graphics Interchange Format (GIF)"),
404 .priv_data_size = sizeof(GIFDemuxContext),
405 .read_probe = gif_probe,
406 .read_header = gif_read_header,
407 .read_packet = gif_read_packet,
408 .flags = AVFMT_GENERIC_INDEX,
409 .priv_class = &demuxer_class,
410 };
411