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.

325 lines
9.6KB

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