3 * Copyright (c) 2012 Vitaliy E Sugrobov
5 * This file is part of FFmpeg.
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.
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.
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
28 #include "libavutil/bprint.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/opt.h"
32 #include "libavcodec/gif.h"
34 typedef struct GIFDemuxContext {
37 * Time span in hundredths of second before
38 * the next frame should be drawn on screen.
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.
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.
66 #define GIF_DEFAULT_DELAY 10
68 * By default delay values less than this threshold considered to be invalid.
70 #define GIF_MIN_DELAY 2
72 static int gif_probe(const AVProbeData *p)
75 if (memcmp(p->buf, gif87a_sig, 6) && memcmp(p->buf, gif89a_sig, 6))
78 /* width or height contains zero? */
79 if (!AV_RL16(&p->buf[6]) || !AV_RL16(&p->buf[8]))
82 return AVPROBE_SCORE_MAX;
85 static int resync(AVIOContext *pb)
88 for (i = 0; i < 6; i++) {
90 if (b != gif87a_sig[i] && b != gif89a_sig[i])
98 static int gif_skip_subblocks(AVIOContext *pb)
100 int sb_size, ret = 0;
102 while (0x00 != (sb_size = avio_r8(pb))) {
103 if ((ret = avio_skip(pb, sb_size)) < 0)
110 static int gif_read_header(AVFormatContext *s)
112 GIFDemuxContext *gdc = s->priv_data;
113 AVIOContext *pb = s->pb;
115 int type, width, height, ret, n, flags;
116 int64_t nb_frames = 0, duration = 0;
118 if ((ret = resync(pb)) < 0)
121 gdc->delay = gdc->default_delay;
122 width = avio_rl16(pb);
123 height = avio_rl16(pb);
128 if (width == 0 || height == 0)
129 return AVERROR_INVALIDDATA;
131 st = avformat_new_stream(s, NULL);
133 return AVERROR(ENOMEM);
136 avio_skip(pb, 3 * (1 << ((flags & 0x07) + 1)));
138 while ((type = avio_r8(pb)) != GIF_TRAILER) {
141 if (type == GIF_EXTENSION_INTRODUCER) {
142 int subtype = avio_r8(pb);
143 if (subtype == GIF_COM_EXT_LABEL) {
147 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
148 while ((block_size = avio_r8(pb)) != 0) {
149 avio_read_to_bprint(pb, &bp, block_size);
151 av_dict_set(&s->metadata, "comment", bp.str, 0);
152 av_bprint_finalize(&bp, NULL);
153 } else if (subtype == GIF_GCE_EXT_LABEL) {
154 int block_size = avio_r8(pb);
156 if (block_size == 4) {
160 delay = avio_rl16(pb);
161 if (delay < gdc->min_delay)
162 delay = gdc->default_delay;
163 delay = FFMIN(delay, gdc->max_delay);
167 avio_skip(pb, block_size);
169 gif_skip_subblocks(pb);
171 gif_skip_subblocks(pb);
173 } else if (type == GIF_IMAGE_SEPARATOR) {
177 avio_skip(pb, 3 * (1 << ((flags & 0x07) + 1)));
179 gif_skip_subblocks(pb);
186 /* GIF format operates with time in "hundredths of second",
187 * therefore timebase is 1/100 */
188 avpriv_set_pts_info(st, 64, 1, 100);
189 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
190 st->codecpar->codec_id = AV_CODEC_ID_GIF;
191 st->codecpar->width = width;
192 st->codecpar->height = height;
194 st->duration = duration;
195 st->nb_frames = nb_frames;
197 st->codecpar->sample_aspect_ratio.num = n + 15;
198 st->codecpar->sample_aspect_ratio.den = 64;
201 /* jump to start because gif decoder needs header data too */
202 if (avio_seek(pb, 0, SEEK_SET) != 0)
208 static int gif_read_ext(AVFormatContext *s)
210 GIFDemuxContext *gdc = s->priv_data;
211 AVIOContext *pb = s->pb;
212 int sb_size, ext_label = avio_r8(pb);
215 if (ext_label == GIF_GCE_EXT_LABEL) {
216 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;
221 /* skip packed fields */
222 if ((ret = avio_skip(pb, 1)) < 0)
225 gdc->delay = avio_rl16(pb);
227 if (gdc->delay < gdc->min_delay)
228 gdc->delay = gdc->default_delay;
229 gdc->delay = FFMIN(gdc->delay, gdc->max_delay);
231 /* skip the rest of the Graphic Control Extension block */
232 if ((ret = avio_skip(pb, sb_size - 3)) < 0 )
234 } else if (ext_label == GIF_APP_EXT_LABEL) {
237 sb_size = avio_r8(pb);
238 ret = avio_read(pb, data, sb_size);
239 if (ret < 0 || !sb_size)
242 if (sb_size == strlen(NETSCAPE_EXT_STR)) {
243 sb_size = avio_r8(pb);
244 ret = avio_read(pb, data, sb_size);
245 if (ret < 0 || !sb_size)
248 if (sb_size == 3 && data[0] == 1) {
249 gdc->total_iter = AV_RL16(data+1);
251 if (gdc->total_iter == 0)
252 gdc->total_iter = -1;
257 if ((ret = gif_skip_subblocks(pb)) < 0)
263 static int gif_read_packet(AVFormatContext *s, AVPacket *pkt)
265 GIFDemuxContext *gdc = s->priv_data;
266 AVIOContext *pb = s->pb;
267 int packed_fields, block_label, ct_size,
268 keyframe, frame_parsed = 0, ret;
269 int64_t frame_start = avio_tell(pb), frame_end;
270 unsigned char buf[6];
272 if ((ret = avio_read(pb, buf, 6)) == 6) {
273 keyframe = memcmp(buf, gif87a_sig, 6) == 0 ||
274 memcmp(buf, gif89a_sig, 6) == 0;
275 } else if (ret < 0) {
283 /* skip 2 bytes of width and 2 of height */
284 if ((ret = avio_skip(pb, 4)) < 0)
287 packed_fields = avio_r8(pb);
289 /* skip 1 byte of Background Color Index and 1 byte of Pixel Aspect Ratio */
290 if ((ret = avio_skip(pb, 2)) < 0)
293 /* global color table presence */
294 if (packed_fields & 0x80) {
295 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
297 if ((ret = avio_skip(pb, ct_size)) < 0)
301 avio_seek(pb, -ret, SEEK_CUR);
305 while (GIF_TRAILER != (block_label = avio_r8(pb)) && !avio_feof(pb)) {
306 if (block_label == GIF_EXTENSION_INTRODUCER) {
307 if ((ret = gif_read_ext (s)) < 0 )
309 } else if (block_label == GIF_IMAGE_SEPARATOR) {
310 /* skip to last byte of Image Descriptor header */
311 if ((ret = avio_skip(pb, 8)) < 0)
314 packed_fields = avio_r8(pb);
316 /* local color table presence */
317 if (packed_fields & 0x80) {
318 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
320 if ((ret = avio_skip(pb, ct_size)) < 0)
324 /* read LZW Minimum Code Size */
325 if (avio_r8(pb) < 1) {
326 av_log(s, AV_LOG_ERROR, "lzw minimum code size must be >= 1\n");
330 if ((ret = gif_skip_subblocks(pb)) < 0)
333 frame_end = avio_tell(pb);
335 if (avio_seek(pb, frame_start, SEEK_SET) != frame_start)
338 ret = av_get_packet(pb, pkt, frame_end - frame_start);
343 pkt->flags |= AV_PKT_FLAG_KEY;
345 pkt->stream_index = 0;
346 pkt->duration = gdc->delay;
349 gdc->last_duration = pkt->duration;
351 /* Graphic Control Extension's scope is single frame.
352 * Remove its influence. */
353 gdc->delay = gdc->default_delay;
358 av_log(s, AV_LOG_ERROR, "invalid block label\n");
361 avio_seek(pb, frame_start, SEEK_SET);
362 if ((ret = resync(pb)) < 0)
364 frame_start = avio_tell(pb) - 6;
370 if ((ret >= 0 && !frame_parsed) || ret == AVERROR_EOF) {
371 if (gdc->nb_frames == 1) {
372 s->streams[0]->r_frame_rate = (AVRational) {100, gdc->last_duration};
374 /* This might happen when there is no image block
375 * between extension blocks and GIF_TRAILER or EOF */
376 if (!gdc->ignore_loop && (block_label == GIF_TRAILER || avio_feof(pb))
377 && (gdc->total_iter < 0 || ++gdc->iter_count < gdc->total_iter))
378 return avio_seek(pb, 0, SEEK_SET);
384 static const AVOption options[] = {
385 { "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 },
386 { "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 },
387 { "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 },
388 { "ignore_loop" , "ignore loop setting (netscape extension)" , offsetof(GIFDemuxContext, ignore_loop) , AV_OPT_TYPE_BOOL,{.i64 = 1} , 0, 1, AV_OPT_FLAG_DECODING_PARAM },
392 static const AVClass demuxer_class = {
393 .class_name = "GIF demuxer",
394 .item_name = av_default_item_name,
396 .version = LIBAVUTIL_VERSION_INT,
397 .category = AV_CLASS_CATEGORY_DEMUXER,
400 AVInputFormat ff_gif_demuxer = {
402 .long_name = NULL_IF_CONFIG_SMALL("CompuServe Graphics Interchange Format (GIF)"),
403 .priv_data_size = sizeof(GIFDemuxContext),
404 .read_probe = gif_probe,
405 .read_header = gif_read_header,
406 .read_packet = gif_read_packet,
407 .flags = AVFMT_GENERIC_INDEX,
408 .priv_class = &demuxer_class,