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/intreadwrite.h"
29 #include "libavutil/opt.h"
31 #include "libavcodec/gif.h"
33 typedef struct GIFDemuxContext {
36 * Time span in hundredths of second before
37 * the next frame should be drawn on screen.
41 * Minimum allowed delay between frames in hundredths of
42 * second. Values below this threshold considered to be
43 * invalid and set to value of default_delay.
47 int total_duration; ///< In hundredths of second.
52 * Major web browsers display gifs at ~10-15fps when rate
53 * is not explicitly set or have too low values. We assume default rate to be 10.
54 * Default delay = 100hundredths of second / 10fps = 10hos per frame.
56 #define GIF_DEFAULT_DELAY 10
58 * By default delay values less than this threshold considered to be invalid.
60 #define GIF_MIN_DELAY 2
62 static int gif_probe(AVProbeData *p)
65 if (memcmp(p->buf, gif87a_sig, 6) && memcmp(p->buf, gif89a_sig, 6))
68 /* width or height contains zero? */
69 if (!AV_RL16(&p->buf[6]) || !AV_RL16(&p->buf[8]))
72 return AVPROBE_SCORE_MAX;
75 static int gif_read_header(AVFormatContext *s)
77 GIFDemuxContext *gdc = s->priv_data;
78 AVIOContext *pb = s->pb;
80 int width, height, ret;
82 /* skip 6-byte magick */
83 if ((ret = avio_skip(pb, 6)) < 0)
86 gdc->delay = gdc->default_delay;
87 width = avio_rl16(pb);
88 height = avio_rl16(pb);
90 if (width == 0 || height == 0)
91 return AVERROR_INVALIDDATA;
93 st = avformat_new_stream(s, NULL);
95 return AVERROR(ENOMEM);
97 /* GIF format operates with time in "hundredths of second",
98 * therefore timebase is 1/100 */
99 avpriv_set_pts_info(st, 64, 1, 100);
100 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
101 st->codec->codec_id = AV_CODEC_ID_GIF;
102 st->codec->width = width;
103 st->codec->height = height;
105 /* jump to start because gif decoder needs header data too */
106 if (avio_seek(pb, 0, SEEK_SET) != 0)
112 static int gif_skip_subblocks(AVIOContext *pb)
114 int sb_size, ret = 0;
116 while (0x00 != (sb_size = avio_r8(pb))) {
117 if ((ret = avio_skip(pb, sb_size)) < 0)
124 static int gif_read_ext(AVFormatContext *s)
126 GIFDemuxContext *gdc = s->priv_data;
127 AVIOContext *pb = s->pb;
128 int sb_size, ext_label = avio_r8(pb);
131 if (ext_label == GIF_GCE_EXT_LABEL) {
132 if ((sb_size = avio_r8(pb)) < 4) {
133 av_log(s, AV_LOG_FATAL, "Graphic Control Extension block's size less than 4.\n");
134 return AVERROR_INVALIDDATA;
137 /* skip packed fields */
138 if ((ret = avio_skip(pb, 1)) < 0)
141 gdc->delay = avio_rl16(pb);
143 if (gdc->delay < gdc->min_delay)
144 gdc->delay = gdc->default_delay;
146 /* skip the rest of the Graphic Control Extension block */
147 if ((ret = avio_skip(pb, sb_size - 3)) < 0 )
151 if ((ret = gif_skip_subblocks(pb)) < 0)
157 static int gif_read_packet(AVFormatContext *s, AVPacket *pkt)
159 GIFDemuxContext *gdc = s->priv_data;
160 AVIOContext *pb = s->pb;
161 int packed_fields, block_label, ct_size,
162 keyframe, frame_parsed = 0, ret;
163 int64_t frame_start = avio_tell(pb), frame_end;
164 unsigned char buf[6];
166 if ((ret = avio_read(pb, buf, 6)) == 6) {
167 keyframe = memcmp(buf, gif87a_sig, 6) == 0 ||
168 memcmp(buf, gif89a_sig, 6) == 0;
169 } else if (ret < 0) {
176 /* skip 2 bytes of width and 2 of height */
177 if ((ret = avio_skip(pb, 4)) < 0)
180 packed_fields = avio_r8(pb);
182 /* skip 1 byte of Background Color Index and 1 byte of Pixel Aspect Ratio */
183 if ((ret = avio_skip(pb, 2)) < 0)
186 /* glogal color table presence */
187 if (packed_fields & 0x80) {
188 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
190 if ((ret = avio_skip(pb, ct_size)) < 0)
194 gdc->total_duration = 0;
197 avio_seek(pb, -ret, SEEK_CUR);
201 while (GIF_TRAILER != (block_label = avio_r8(pb)) && !url_feof(pb)) {
202 if (block_label == GIF_EXTENSION_INTRODUCER) {
203 if ((ret = gif_read_ext (s)) < 0 )
205 } else if (block_label == GIF_IMAGE_SEPARATOR) {
206 /* skip to last byte of Image Descriptor header */
207 if ((ret = avio_skip(pb, 8)) < 0)
210 packed_fields = avio_r8(pb);
212 /* local color table presence */
213 if (packed_fields & 0x80) {
214 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
216 if ((ret = avio_skip(pb, ct_size)) < 0)
220 /* read LZW Minimum Code Size */
221 if (avio_r8(pb) < 1) {
222 av_log(s, AV_LOG_ERROR, "lzw minimum code size must be >= 1\n");
223 return AVERROR_INVALIDDATA;
226 if ((ret = gif_skip_subblocks(pb)) < 0)
229 frame_end = avio_tell(pb);
231 if (avio_seek(pb, frame_start, SEEK_SET) != frame_start)
234 ret = av_get_packet(pb, pkt, frame_end - frame_start);
239 pkt->flags |= AV_PKT_FLAG_KEY;
241 pkt->stream_index = 0;
242 pkt->pts = gdc->total_duration;
243 gdc->total_duration += gdc->delay;
244 pkt->duration = gdc->delay;
245 pkt->dts = gdc->frame_idx;
247 /* Graphic Control Extension's scope is single frame.
248 * Remove its influence. */
249 gdc->delay = gdc->default_delay;
255 av_log(s, AV_LOG_ERROR, "invalid block label\n");
256 return AVERROR_INVALIDDATA;
260 if (ret >= 0 && !frame_parsed) {
261 /* This might happen when there is no image block
262 * between extension blocks and GIF_TRAILER or EOF */
268 static const AVOption options[] = {
269 { "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 },
270 { "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 },
274 static const AVClass demuxer_class = {
275 .class_name = "GIF demuxer",
276 .item_name = av_default_item_name,
278 .version = LIBAVUTIL_VERSION_INT,
279 .category = AV_CLASS_CATEGORY_DEMUXER,
282 AVInputFormat ff_gif_demuxer = {
284 .long_name = NULL_IF_CONFIG_SMALL("CompuServe Graphics Interchange Format (GIF)"),
285 .priv_data_size = sizeof(GIFDemuxContext),
286 .read_probe = gif_probe,
287 .read_header = gif_read_header,
288 .read_packet = gif_read_packet,
289 .flags = AVFMT_GENERIC_INDEX,
290 .priv_class = &demuxer_class,