You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
10KB

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