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.
57 * Major web browsers display gifs at ~10-15fps when rate
58 * is not explicitly set or have too low values. We assume default rate to be 10.
59 * Default delay = 100hundredths of second / 10fps = 10hos per frame.
61 #define GIF_DEFAULT_DELAY 10
63 * By default delay values less than this threshold considered to be invalid.
65 #define GIF_MIN_DELAY 2
67 static int gif_probe(AVProbeData *p)
70 if (memcmp(p->buf, gif87a_sig, 6) && memcmp(p->buf, gif89a_sig, 6))
73 /* width or height contains zero? */
74 if (!AV_RL16(&p->buf[6]) || !AV_RL16(&p->buf[8]))
77 return AVPROBE_SCORE_MAX;
80 static int resync(AVIOContext *pb)
83 for (i = 0; i < 6; i++) {
85 if (b != gif87a_sig[i] && b != gif89a_sig[i])
93 static int gif_read_header(AVFormatContext *s)
95 GIFDemuxContext *gdc = s->priv_data;
96 AVIOContext *pb = s->pb;
98 int width, height, ret;
100 if ((ret = resync(pb)) < 0)
103 gdc->delay = gdc->default_delay;
104 width = avio_rl16(pb);
105 height = avio_rl16(pb);
107 if (width == 0 || height == 0)
108 return AVERROR_INVALIDDATA;
110 st = avformat_new_stream(s, NULL);
112 return AVERROR(ENOMEM);
114 /* GIF format operates with time in "hundredths of second",
115 * therefore timebase is 1/100 */
116 avpriv_set_pts_info(st, 64, 1, 100);
117 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
118 st->codec->codec_id = AV_CODEC_ID_GIF;
119 st->codec->width = width;
120 st->codec->height = height;
122 /* jump to start because gif decoder needs header data too */
123 if (avio_seek(pb, 0, SEEK_SET) != 0)
129 static int gif_skip_subblocks(AVIOContext *pb)
131 int sb_size, ret = 0;
133 while (0x00 != (sb_size = avio_r8(pb))) {
134 if ((ret = avio_skip(pb, sb_size)) < 0)
141 static int gif_read_ext(AVFormatContext *s)
143 GIFDemuxContext *gdc = s->priv_data;
144 AVIOContext *pb = s->pb;
145 int sb_size, ext_label = avio_r8(pb);
148 if (ext_label == GIF_GCE_EXT_LABEL) {
149 if ((sb_size = avio_r8(pb)) < 4) {
150 av_log(s, AV_LOG_FATAL, "Graphic Control Extension block's size less than 4.\n");
151 return AVERROR_INVALIDDATA;
154 /* skip packed fields */
155 if ((ret = avio_skip(pb, 1)) < 0)
158 gdc->delay = avio_rl16(pb);
160 if (gdc->delay < gdc->min_delay)
161 gdc->delay = gdc->default_delay;
163 /* skip the rest of the Graphic Control Extension block */
164 if ((ret = avio_skip(pb, sb_size - 3)) < 0 )
166 } else if (ext_label == GIF_APP_EXT_LABEL) {
167 uint8_t netscape_ext[sizeof(NETSCAPE_EXT_STR)-1 + 2];
169 if ((sb_size = avio_r8(pb)) != strlen(NETSCAPE_EXT_STR))
171 ret = avio_read(pb, netscape_ext, sizeof(netscape_ext));
172 if (ret < sizeof(netscape_ext))
174 gdc->total_iter = avio_rl16(pb);
175 if (gdc->total_iter == 0)
176 gdc->total_iter = -1;
179 if ((ret = gif_skip_subblocks(pb)) < 0)
185 static int gif_read_packet(AVFormatContext *s, AVPacket *pkt)
187 GIFDemuxContext *gdc = s->priv_data;
188 AVIOContext *pb = s->pb;
189 int packed_fields, block_label, ct_size,
190 keyframe, frame_parsed = 0, ret;
191 int64_t frame_start = avio_tell(pb), frame_end;
192 unsigned char buf[6];
194 if ((ret = avio_read(pb, buf, 6)) == 6) {
195 keyframe = memcmp(buf, gif87a_sig, 6) == 0 ||
196 memcmp(buf, gif89a_sig, 6) == 0;
197 } else if (ret < 0) {
205 /* skip 2 bytes of width and 2 of height */
206 if ((ret = avio_skip(pb, 4)) < 0)
209 packed_fields = avio_r8(pb);
211 /* skip 1 byte of Background Color Index and 1 byte of Pixel Aspect Ratio */
212 if ((ret = avio_skip(pb, 2)) < 0)
215 /* global color table presence */
216 if (packed_fields & 0x80) {
217 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
219 if ((ret = avio_skip(pb, ct_size)) < 0)
223 avio_seek(pb, -ret, SEEK_CUR);
227 while (GIF_TRAILER != (block_label = avio_r8(pb)) && !url_feof(pb)) {
228 if (block_label == GIF_EXTENSION_INTRODUCER) {
229 if ((ret = gif_read_ext (s)) < 0 )
231 } else if (block_label == GIF_IMAGE_SEPARATOR) {
232 /* skip to last byte of Image Descriptor header */
233 if ((ret = avio_skip(pb, 8)) < 0)
236 packed_fields = avio_r8(pb);
238 /* local color table presence */
239 if (packed_fields & 0x80) {
240 ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
242 if ((ret = avio_skip(pb, ct_size)) < 0)
246 /* read LZW Minimum Code Size */
247 if (avio_r8(pb) < 1) {
248 av_log(s, AV_LOG_ERROR, "lzw minimum code size must be >= 1\n");
252 if ((ret = gif_skip_subblocks(pb)) < 0)
255 frame_end = avio_tell(pb);
257 if (avio_seek(pb, frame_start, SEEK_SET) != frame_start)
260 ret = av_get_packet(pb, pkt, frame_end - frame_start);
265 pkt->flags |= AV_PKT_FLAG_KEY;
267 pkt->stream_index = 0;
268 pkt->duration = gdc->delay;
270 /* Graphic Control Extension's scope is single frame.
271 * Remove its influence. */
272 gdc->delay = gdc->default_delay;
277 av_log(s, AV_LOG_ERROR, "invalid block label\n");
280 avio_seek(pb, frame_start, SEEK_SET);
281 if ((ret = resync(pb)) < 0)
283 frame_start = avio_tell(pb) - 6;
289 if ((ret >= 0 && !frame_parsed) || ret == AVERROR_EOF) {
290 /* This might happen when there is no image block
291 * between extension blocks and GIF_TRAILER or EOF */
292 if (!gdc->ignore_loop && (block_label == GIF_TRAILER || url_feof(pb))
293 && (gdc->total_iter < 0 || ++gdc->iter_count < gdc->total_iter))
294 return avio_seek(pb, 0, SEEK_SET);
300 static const AVOption options[] = {
301 { "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 },
302 { "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 },
303 { "ignore_loop" , "ignore loop setting (netscape extension)" , offsetof(GIFDemuxContext, ignore_loop) , AV_OPT_TYPE_INT, {.i64 = 1} , 0, 1, AV_OPT_FLAG_DECODING_PARAM },
307 static const AVClass demuxer_class = {
308 .class_name = "GIF demuxer",
309 .item_name = av_default_item_name,
311 .version = LIBAVUTIL_VERSION_INT,
312 .category = AV_CLASS_CATEGORY_DEMUXER,
315 AVInputFormat ff_gif_demuxer = {
317 .long_name = NULL_IF_CONFIG_SMALL("CompuServe Graphics Interchange Format (GIF)"),
318 .priv_data_size = sizeof(GIFDemuxContext),
319 .read_probe = gif_probe,
320 .read_header = gif_read_header,
321 .read_packet = gif_read_packet,
322 .flags = AVFMT_GENERIC_INDEX,
323 .priv_class = &demuxer_class,