FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gifdec.c
Go to the documentation of this file.
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/intreadwrite.h"
29 #include "libavutil/opt.h"
30 #include "internal.h"
31 #include "libavcodec/gif.h"
32 
33 typedef struct GIFDemuxContext {
34  const AVClass *class;
35  /**
36  * Time span in hundredths of second before
37  * the next frame should be drawn on screen.
38  */
39  int delay;
40  /**
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.
44  */
45  int min_delay;
48 
49 /**
50  * Major web browsers display gifs at ~10-15fps when rate
51  * is not explicitly set or have too low values. We assume default rate to be 10.
52  * Default delay = 100hundredths of second / 10fps = 10hos per frame.
53  */
54 #define GIF_DEFAULT_DELAY 10
55 /**
56  * By default delay values less than this threshold considered to be invalid.
57  */
58 #define GIF_MIN_DELAY 2
59 
60 static int gif_probe(AVProbeData *p)
61 {
62  /* check magick */
63  if (memcmp(p->buf, gif87a_sig, 6) && memcmp(p->buf, gif89a_sig, 6))
64  return 0;
65 
66  /* width or height contains zero? */
67  if (!AV_RL16(&p->buf[6]) || !AV_RL16(&p->buf[8]))
68  return 0;
69 
70  return AVPROBE_SCORE_MAX;
71 }
72 
74 {
75  GIFDemuxContext *gdc = s->priv_data;
76  AVIOContext *pb = s->pb;
77  AVStream *st;
78  int width, height, ret;
79 
80  /* skip 6-byte magick */
81  if ((ret = avio_skip(pb, 6)) < 0)
82  return ret;
83 
84  gdc->delay = gdc->default_delay;
85  width = avio_rl16(pb);
86  height = avio_rl16(pb);
87 
88  if (width == 0 || height == 0)
89  return AVERROR_INVALIDDATA;
90 
91  st = avformat_new_stream(s, NULL);
92  if (!st)
93  return AVERROR(ENOMEM);
94 
95  /* GIF format operates with time in "hundredths of second",
96  * therefore timebase is 1/100 */
97  avpriv_set_pts_info(st, 64, 1, 100);
100  st->codec->width = width;
101  st->codec->height = height;
102 
103  /* jump to start because gif decoder needs header data too */
104  if (avio_seek(pb, 0, SEEK_SET) != 0)
105  return AVERROR(EIO);
106 
107  return 0;
108 }
109 
111 {
112  int sb_size, ret = 0;
113 
114  while (0x00 != (sb_size = avio_r8(pb))) {
115  if ((ret = avio_skip(pb, sb_size)) < 0)
116  return ret;
117  }
118 
119  return ret;
120 }
121 
123 {
124  GIFDemuxContext *gdc = s->priv_data;
125  AVIOContext *pb = s->pb;
126  int sb_size, ext_label = avio_r8(pb);
127  int ret;
128 
129  if (ext_label == GIF_GCE_EXT_LABEL) {
130  if ((sb_size = avio_r8(pb)) < 4) {
131  av_log(s, AV_LOG_FATAL, "Graphic Control Extension block's size less than 4.\n");
132  return AVERROR_INVALIDDATA;
133  }
134 
135  /* skip packed fields */
136  if ((ret = avio_skip(pb, 1)) < 0)
137  return ret;
138 
139  gdc->delay = avio_rl16(pb);
140 
141  if (gdc->delay < gdc->min_delay)
142  gdc->delay = gdc->default_delay;
143 
144  /* skip the rest of the Graphic Control Extension block */
145  if ((ret = avio_skip(pb, sb_size - 3)) < 0 )
146  return ret;
147  }
148 
149  if ((ret = gif_skip_subblocks(pb)) < 0)
150  return ret;
151 
152  return 0;
153 }
154 
156 {
157  GIFDemuxContext *gdc = s->priv_data;
158  AVIOContext *pb = s->pb;
159  int packed_fields, block_label, ct_size,
160  keyframe, frame_parsed = 0, ret;
161  int64_t frame_start = avio_tell(pb), frame_end;
162  unsigned char buf[6];
163 
164  if ((ret = avio_read(pb, buf, 6)) == 6) {
165  keyframe = memcmp(buf, gif87a_sig, 6) == 0 ||
166  memcmp(buf, gif89a_sig, 6) == 0;
167  } else if (ret < 0) {
168  return ret;
169  } else {
170  keyframe = 0;
171  }
172 
173  if (keyframe) {
174  /* skip 2 bytes of width and 2 of height */
175  if ((ret = avio_skip(pb, 4)) < 0)
176  return ret;
177 
178  packed_fields = avio_r8(pb);
179 
180  /* skip 1 byte of Background Color Index and 1 byte of Pixel Aspect Ratio */
181  if ((ret = avio_skip(pb, 2)) < 0)
182  return ret;
183 
184  /* glogal color table presence */
185  if (packed_fields & 0x80) {
186  ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
187 
188  if ((ret = avio_skip(pb, ct_size)) < 0)
189  return ret;
190  }
191  } else {
192  avio_seek(pb, -ret, SEEK_CUR);
193  ret = AVERROR_EOF;
194  }
195 
196  while (GIF_TRAILER != (block_label = avio_r8(pb)) && !url_feof(pb)) {
197  if (block_label == GIF_EXTENSION_INTRODUCER) {
198  if ((ret = gif_read_ext (s)) < 0 )
199  return ret;
200  } else if (block_label == GIF_IMAGE_SEPARATOR) {
201  /* skip to last byte of Image Descriptor header */
202  if ((ret = avio_skip(pb, 8)) < 0)
203  return ret;
204 
205  packed_fields = avio_r8(pb);
206 
207  /* local color table presence */
208  if (packed_fields & 0x80) {
209  ct_size = 3 * (1 << ((packed_fields & 0x07) + 1));
210 
211  if ((ret = avio_skip(pb, ct_size)) < 0)
212  return ret;
213  }
214 
215  /* read LZW Minimum Code Size */
216  if (avio_r8(pb) < 1) {
217  av_log(s, AV_LOG_ERROR, "lzw minimum code size must be >= 1\n");
218  return AVERROR_INVALIDDATA;
219  }
220 
221  if ((ret = gif_skip_subblocks(pb)) < 0)
222  return ret;
223 
224  frame_end = avio_tell(pb);
225 
226  if (avio_seek(pb, frame_start, SEEK_SET) != frame_start)
227  return AVERROR(EIO);
228 
229  ret = av_get_packet(pb, pkt, frame_end - frame_start);
230  if (ret < 0)
231  return ret;
232 
233  if (keyframe)
234  pkt->flags |= AV_PKT_FLAG_KEY;
235 
236  pkt->stream_index = 0;
237  pkt->duration = gdc->delay;
238 
239  /* Graphic Control Extension's scope is single frame.
240  * Remove its influence. */
241  gdc->delay = gdc->default_delay;
242  frame_parsed = 1;
243 
244  break;
245  } else {
246  av_log(s, AV_LOG_ERROR, "invalid block label\n");
247  return AVERROR_INVALIDDATA;
248  }
249  }
250 
251  if (ret >= 0 && !frame_parsed) {
252  /* This might happen when there is no image block
253  * between extension blocks and GIF_TRAILER or EOF */
254  return AVERROR_EOF;
255  } else
256  return ret;
257 }
258 
259 static const AVOption options[] = {
260  { "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 },
261  { "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 },
262  { NULL },
263 };
264 
265 static const AVClass demuxer_class = {
266  .class_name = "GIF demuxer",
267  .item_name = av_default_item_name,
268  .option = options,
269  .version = LIBAVUTIL_VERSION_INT,
270  .category = AV_CLASS_CATEGORY_DEMUXER,
271 };
272 
274  .name = "gif",
275  .long_name = NULL_IF_CONFIG_SMALL("CompuServe Graphics Interchange Format (GIF)"),
276  .priv_data_size = sizeof(GIFDemuxContext),
281  .priv_class = &demuxer_class,
282 };