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.
61 * Major web browsers display gifs at ~10-15fps when rate
62 * is not explicitly set or have too low values. We assume default rate to be 10.
63 * Default delay = 100hundredths of second / 10fps = 10hos per frame.
65 #define GIF_DEFAULT_DELAY 10
67 * By default delay values less than this threshold considered to be invalid.
69 #define GIF_MIN_DELAY 2
71 static int gif_probe(AVProbeData *p)
74 if (memcmp(p->buf, gif87a_sig, 6) && memcmp(p->buf, gif89a_sig, 6))
77 /* width or height contains zero? */
78 if (!AV_RL16(&p->buf[6]) || !AV_RL16(&p->buf[8]))
81 return AVPROBE_SCORE_MAX;
84 static int resync(AVIOContext *pb)
87 for (i = 0; i < 6; i++) {
89 if (b != gif87a_sig[i] && b != gif89a_sig[i])
97 static int gif_read_header(AVFormatContext *s)
99 GIFDemuxContext *gdc = s->priv_data;
100 AVIOContext *pb = s->pb;
102 int width, height, ret, n;
104 if ((ret = resync(pb)) < 0)
107 gdc->delay = gdc->default_delay;
108 width = avio_rl16(pb);
109 height = avio_rl16(pb);
113 if (width == 0 || height == 0)
114 return AVERROR_INVALIDDATA;
116 st = avformat_new_stream(s, NULL);
118 return AVERROR(ENOMEM);
120 /* GIF format operates with time in "hundredths of second",
121 * therefore timebase is 1/100 */
122 avpriv_set_pts_info(st, 64, 1, 100);
123 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
124 st->codecpar->codec_id = AV_CODEC_ID_GIF;
125 st->codecpar->width = width;
126 st->codecpar->height = height;
128 st->codecpar->sample_aspect_ratio.num = n + 15;
129 st->codecpar->sample_aspect_ratio.den = 64;
132 /* jump to start because gif decoder needs header data too */
133 if (avio_seek(pb, 0, SEEK_SET) != 0)
139 static int gif_skip_subblocks(AVIOContext *pb)
141 int sb_size, ret = 0;
143 while (0x00 != (sb_size = avio_r8(pb))) {
144 if ((ret = avio_skip(pb, sb_size)) < 0)
151 static int gif_read_ext(AVFormatContext *s)
153 GIFDemuxContext *gdc = s->priv_data;
154 AVIOContext *pb = s->pb;
155 int sb_size, ext_label = avio_r8(pb);
158 if (ext_label == GIF_GCE_EXT_LABEL) {
159 if ((sb_size = avio_r8(pb)) < 4) {
160 av_log(s, AV_LOG_FATAL, "Graphic Control Extension block's size less than 4.\n");
161 return AVERROR_INVALIDDATA;
164 /* skip packed fields */
165 if ((ret = avio_skip(pb, 1)) < 0)
168 gdc->delay = avio_rl16(pb);
170 if (gdc->delay < gdc->min_delay)
171 gdc->delay = gdc->default_delay;
172 gdc->delay = FFMIN(gdc->delay, gdc->max_delay);
174 /* skip the rest of the Graphic Control Extension block */
175 if ((ret = avio_skip(pb, sb_size - 3)) < 0 )
177 } else if (ext_label == GIF_APP_EXT_LABEL) {
180 sb_size = avio_r8(pb);
181 ret = avio_read(pb, data, sb_size);
182 if (ret < 0 || !sb_size)
185 if (sb_size == strlen(NETSCAPE_EXT_STR)) {
186 sb_size = avio_r8(pb);
187 ret = avio_read(pb, data, sb_size);
188 if (ret < 0 || !sb_size)
191 if (sb_size == 3 && data[0] == 1) {
192 gdc->total_iter = AV_RL16(data+1);
194 if (gdc->total_iter == 0)
195 gdc->total_iter = -1;
200 if ((ret = gif_skip_subblocks(pb)) < 0)
206 static int gif_read_packet(AVFormatContext *s, AVPacket *pkt)
208 GIFDemuxContext *gdc = s->priv_data;
209 AVIOContext *pb = s->pb;
210 int packed_fields, block_label, ct_size,
211 keyframe, frame_parsed = 0, ret;
212 int64_t frame_start = avio_tell(pb), frame_end;
213 unsigned char buf[6];
215 if ((ret = avio_read(pb, buf, 6)) == 6) {
216 keyframe = memcmp(buf, gif87a_sig, 6) == 0 ||
217 memcmp(buf, gif89a_sig, 6) == 0;
218 } else if (ret < 0) {
226 /* skip 2 bytes of width and 2 of height */
227 if ((ret = avio_skip(pb, 4)) < 0)
230 packed_fields = avio_r8(pb);
232 /* skip 1 byte of Background Color Index and 1 byte of Pixel Aspect Ratio */
233 if ((ret = avio_skip(pb, 2)) < 0)
236 /* global color table presence */
237 if (packed_fields & 0x80) {
238 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
240 if ((ret = avio_skip(pb, ct_size)) < 0)
244 avio_seek(pb, -ret, SEEK_CUR);
248 while (GIF_TRAILER != (block_label = avio_r8(pb)) && !avio_feof(pb)) {
249 if (block_label == GIF_EXTENSION_INTRODUCER) {
250 if ((ret = gif_read_ext (s)) < 0 )
252 } else if (block_label == GIF_IMAGE_SEPARATOR) {
253 /* skip to last byte of Image Descriptor header */
254 if ((ret = avio_skip(pb, 8)) < 0)
257 packed_fields = avio_r8(pb);
259 /* local color table presence */
260 if (packed_fields & 0x80) {
261 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
263 if ((ret = avio_skip(pb, ct_size)) < 0)
267 /* read LZW Minimum Code Size */
268 if (avio_r8(pb) < 1) {
269 av_log(s, AV_LOG_ERROR, "lzw minimum code size must be >= 1\n");
273 if ((ret = gif_skip_subblocks(pb)) < 0)
276 frame_end = avio_tell(pb);
278 if (avio_seek(pb, frame_start, SEEK_SET) != frame_start)
281 ret = av_get_packet(pb, pkt, frame_end - frame_start);
286 pkt->flags |= AV_PKT_FLAG_KEY;
288 pkt->stream_index = 0;
289 pkt->duration = gdc->delay;
292 gdc->last_duration = pkt->duration;
294 /* Graphic Control Extension's scope is single frame.
295 * Remove its influence. */
296 gdc->delay = gdc->default_delay;
301 av_log(s, AV_LOG_ERROR, "invalid block label\n");
304 avio_seek(pb, frame_start, SEEK_SET);
305 if ((ret = resync(pb)) < 0)
307 frame_start = avio_tell(pb) - 6;
313 if ((ret >= 0 && !frame_parsed) || ret == AVERROR_EOF) {
314 if (gdc->nb_frames == 1) {
315 s->streams[0]->r_frame_rate = (AVRational) {100, gdc->last_duration};
317 /* This might happen when there is no image block
318 * between extension blocks and GIF_TRAILER or EOF */
319 if (!gdc->ignore_loop && (block_label == GIF_TRAILER || avio_feof(pb))
320 && (gdc->total_iter < 0 || ++gdc->iter_count < gdc->total_iter))
321 return avio_seek(pb, 0, SEEK_SET);
327 static const AVOption options[] = {
328 { "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 },
329 { "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 },
330 { "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 },
331 { "ignore_loop" , "ignore loop setting (netscape extension)" , offsetof(GIFDemuxContext, ignore_loop) , AV_OPT_TYPE_BOOL,{.i64 = 1} , 0, 1, AV_OPT_FLAG_DECODING_PARAM },
335 static const AVClass demuxer_class = {
336 .class_name = "GIF demuxer",
337 .item_name = av_default_item_name,
339 .version = LIBAVUTIL_VERSION_INT,
340 .category = AV_CLASS_CATEGORY_DEMUXER,
343 AVInputFormat ff_gif_demuxer = {
345 .long_name = NULL_IF_CONFIG_SMALL("CompuServe Graphics Interchange Format (GIF)"),
346 .priv_data_size = sizeof(GIFDemuxContext),
347 .read_probe = gif_probe,
348 .read_header = gif_read_header,
349 .read_packet = gif_read_packet,
350 .flags = AVFMT_GENERIC_INDEX,
351 .priv_class = &demuxer_class,