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.

430 lines
13KB

  1. /*
  2. * APNG demuxer
  3. * Copyright (c) 2014 Benoit Fouet
  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. * APNG demuxer.
  24. * @see https://wiki.mozilla.org/APNG_Specification
  25. * @see http://www.w3.org/TR/PNG
  26. */
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "internal.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/opt.h"
  33. #include "libavcodec/png.h"
  34. #include "libavcodec/bytestream.h"
  35. #define DEFAULT_APNG_FPS 15
  36. typedef struct APNGDemuxContext {
  37. const AVClass *class;
  38. int max_fps;
  39. int default_fps;
  40. int is_key_frame;
  41. /*
  42. * loop options
  43. */
  44. int ignore_loop;
  45. uint32_t num_frames;
  46. uint32_t num_play;
  47. uint32_t cur_loop;
  48. } APNGDemuxContext;
  49. /*
  50. * To be a valid APNG file, we mandate, in this order:
  51. * PNGSIG
  52. * IHDR
  53. * ...
  54. * acTL
  55. * ...
  56. * IDAT
  57. */
  58. static int apng_probe(AVProbeData *p)
  59. {
  60. GetByteContext gb;
  61. int state = 0;
  62. uint32_t len, tag;
  63. bytestream2_init(&gb, p->buf, p->buf_size);
  64. if (bytestream2_get_be64(&gb) != PNGSIG)
  65. return 0;
  66. for (;;) {
  67. len = bytestream2_get_be32(&gb);
  68. if (len > 0x7fffffff)
  69. return 0;
  70. tag = bytestream2_get_le32(&gb);
  71. /* we don't check IDAT size, as this is the last tag
  72. * we check, and it may be larger than the probe buffer */
  73. if (tag != MKTAG('I', 'D', 'A', 'T') &&
  74. len > bytestream2_get_bytes_left(&gb))
  75. return 0;
  76. switch (tag) {
  77. case MKTAG('I', 'H', 'D', 'R'):
  78. if (len != 13)
  79. return 0;
  80. if (av_image_check_size(bytestream2_get_be32(&gb), bytestream2_get_be32(&gb), 0, NULL))
  81. return 0;
  82. bytestream2_skip(&gb, 9);
  83. state++;
  84. break;
  85. case MKTAG('a', 'c', 'T', 'L'):
  86. if (state != 1 ||
  87. len != 8 ||
  88. bytestream2_get_be32(&gb) == 0) /* 0 is not a valid value for number of frames */
  89. return 0;
  90. bytestream2_skip(&gb, 8);
  91. state++;
  92. break;
  93. case MKTAG('I', 'D', 'A', 'T'):
  94. if (state != 2)
  95. return 0;
  96. goto end;
  97. default:
  98. /* skip other tags */
  99. bytestream2_skip(&gb, len + 4);
  100. break;
  101. }
  102. }
  103. end:
  104. return AVPROBE_SCORE_MAX;
  105. }
  106. static int append_extradata(AVCodecContext *s, AVIOContext *pb, int len)
  107. {
  108. int previous_size = s->extradata_size;
  109. int new_size, ret;
  110. uint8_t *new_extradata;
  111. if (previous_size > INT_MAX - len)
  112. return AVERROR_INVALIDDATA;
  113. new_size = previous_size + len;
  114. new_extradata = av_realloc(s->extradata, new_size + FF_INPUT_BUFFER_PADDING_SIZE);
  115. if (!new_extradata)
  116. return AVERROR(ENOMEM);
  117. s->extradata = new_extradata;
  118. s->extradata_size = new_size;
  119. if ((ret = avio_read(pb, s->extradata + previous_size, len)) < 0)
  120. return ret;
  121. return previous_size;
  122. }
  123. static int apng_read_header(AVFormatContext *s)
  124. {
  125. APNGDemuxContext *ctx = s->priv_data;
  126. AVIOContext *pb = s->pb;
  127. uint32_t len, tag;
  128. AVStream *st;
  129. int ret = AVERROR_INVALIDDATA, acTL_found = 0;
  130. /* verify PNGSIG */
  131. if (avio_rb64(pb) != PNGSIG)
  132. return ret;
  133. /* parse IHDR (must be first chunk) */
  134. len = avio_rb32(pb);
  135. tag = avio_rl32(pb);
  136. if (len != 13 || tag != MKTAG('I', 'H', 'D', 'R'))
  137. return ret;
  138. st = avformat_new_stream(s, NULL);
  139. if (!st)
  140. return AVERROR(ENOMEM);
  141. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  142. st->codec->codec_id = AV_CODEC_ID_APNG;
  143. st->codec->width = avio_rb32(pb);
  144. st->codec->height = avio_rb32(pb);
  145. if ((ret = av_image_check_size(st->codec->width, st->codec->height, 0, s)) < 0)
  146. return ret;
  147. /* extradata will contain every chunk up to the first fcTL (excluded) */
  148. st->codec->extradata = av_malloc(len + 12 + FF_INPUT_BUFFER_PADDING_SIZE);
  149. if (!st->codec->extradata)
  150. return AVERROR(ENOMEM);
  151. st->codec->extradata_size = len + 12;
  152. AV_WB32(st->codec->extradata, len);
  153. AV_WL32(st->codec->extradata+4, tag);
  154. AV_WB32(st->codec->extradata+8, st->codec->width);
  155. AV_WB32(st->codec->extradata+12, st->codec->height);
  156. if ((ret = avio_read(pb, st->codec->extradata+16, 9)) < 0)
  157. goto fail;
  158. while (!avio_feof(pb)) {
  159. if (acTL_found && ctx->num_play != 1) {
  160. int64_t size = avio_size(pb);
  161. int64_t offset = avio_tell(pb);
  162. if (size < 0) {
  163. ret = size;
  164. goto fail;
  165. } else if (offset < 0) {
  166. ret = offset;
  167. goto fail;
  168. } else if ((ret = ffio_ensure_seekback(pb, size - offset)) < 0) {
  169. av_log(s, AV_LOG_WARNING, "Could not ensure seekback, will not loop\n");
  170. ctx->num_play = 1;
  171. }
  172. }
  173. if ((ctx->num_play == 1 || !acTL_found) &&
  174. ((ret = ffio_ensure_seekback(pb, 4 /* len */ + 4 /* tag */)) < 0))
  175. goto fail;
  176. len = avio_rb32(pb);
  177. if (len > 0x7fffffff) {
  178. ret = AVERROR_INVALIDDATA;
  179. goto fail;
  180. }
  181. tag = avio_rl32(pb);
  182. switch (tag) {
  183. case MKTAG('a', 'c', 'T', 'L'):
  184. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
  185. (ret = append_extradata(st->codec, pb, len + 12)) < 0)
  186. goto fail;
  187. acTL_found = 1;
  188. ctx->num_frames = AV_RB32(st->codec->extradata + ret + 8);
  189. ctx->num_play = AV_RB32(st->codec->extradata + ret + 12);
  190. av_log(s, AV_LOG_DEBUG, "num_frames: %"PRIu32", num_play: %"PRIu32"\n",
  191. ctx->num_frames, ctx->num_play);
  192. break;
  193. case MKTAG('f', 'c', 'T', 'L'):
  194. if (!acTL_found) {
  195. ret = AVERROR_INVALIDDATA;
  196. goto fail;
  197. }
  198. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0)
  199. goto fail;
  200. return 0;
  201. default:
  202. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
  203. (ret = append_extradata(st->codec, pb, len + 12)) < 0)
  204. goto fail;
  205. }
  206. }
  207. fail:
  208. if (st->codec->extradata_size) {
  209. av_freep(&st->codec->extradata);
  210. st->codec->extradata_size = 0;
  211. }
  212. return ret;
  213. }
  214. static int decode_fctl_chunk(AVFormatContext *s, APNGDemuxContext *ctx, AVPacket *pkt)
  215. {
  216. uint32_t sequence_number, width, height, x_offset, y_offset;
  217. uint16_t delay_num, delay_den;
  218. uint8_t dispose_op, blend_op;
  219. sequence_number = avio_rb32(s->pb);
  220. width = avio_rb32(s->pb);
  221. height = avio_rb32(s->pb);
  222. x_offset = avio_rb32(s->pb);
  223. y_offset = avio_rb32(s->pb);
  224. delay_num = avio_rb16(s->pb);
  225. delay_den = avio_rb16(s->pb);
  226. dispose_op = avio_r8(s->pb);
  227. blend_op = avio_r8(s->pb);
  228. avio_skip(s->pb, 4); /* crc */
  229. /* default is hundredths of seconds */
  230. if (!delay_den)
  231. delay_den = 100;
  232. if (!delay_num || delay_den / delay_num > ctx->max_fps) {
  233. delay_num = 1;
  234. delay_den = ctx->default_fps;
  235. }
  236. s->streams[0]->r_frame_rate.num = delay_den;
  237. s->streams[0]->r_frame_rate.den = delay_num;
  238. pkt->duration = 1;
  239. av_log(s, AV_LOG_DEBUG, "%s: "
  240. "sequence_number: %"PRId32", "
  241. "width: %"PRIu32", "
  242. "height: %"PRIu32", "
  243. "x_offset: %"PRIu32", "
  244. "y_offset: %"PRIu32", "
  245. "delay_num: %"PRIu16", "
  246. "delay_den: %"PRIu16", "
  247. "dispose_op: %d, "
  248. "blend_op: %d\n",
  249. __FUNCTION__,
  250. sequence_number,
  251. width,
  252. height,
  253. x_offset,
  254. y_offset,
  255. delay_num,
  256. delay_den,
  257. dispose_op,
  258. blend_op);
  259. if (width != s->streams[0]->codec->width ||
  260. height != s->streams[0]->codec->height ||
  261. x_offset != 0 ||
  262. y_offset != 0) {
  263. if (sequence_number == 0)
  264. return AVERROR_INVALIDDATA;
  265. ctx->is_key_frame = 0;
  266. } else {
  267. ctx->is_key_frame = 1;
  268. }
  269. return 0;
  270. }
  271. static int apng_read_packet(AVFormatContext *s, AVPacket *pkt)
  272. {
  273. APNGDemuxContext *ctx = s->priv_data;
  274. int ret;
  275. int64_t size;
  276. AVIOContext *pb = s->pb;
  277. uint32_t len, tag;
  278. /*
  279. * fcTL chunk length, in bytes:
  280. * 4 (length)
  281. * 4 (tag)
  282. * 26 (actual chunk)
  283. * 4 (crc) bytes
  284. * and needed next:
  285. * 4 (length)
  286. * 4 (tag (must be fdAT or IDAT))
  287. */
  288. /* if num_play is not 1, then the seekback is already guaranteed */
  289. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 46)) < 0)
  290. return ret;
  291. len = avio_rb32(pb);
  292. tag = avio_rl32(pb);
  293. switch (tag) {
  294. case MKTAG('f', 'c', 'T', 'L'):
  295. if (len != 26)
  296. return AVERROR_INVALIDDATA;
  297. if ((ret = decode_fctl_chunk(s, ctx, pkt)) < 0)
  298. return ret;
  299. /* fcTL must precede fdAT or IDAT */
  300. len = avio_rb32(pb);
  301. tag = avio_rl32(pb);
  302. if (len > 0x7fffffff ||
  303. tag != MKTAG('f', 'd', 'A', 'T') &&
  304. tag != MKTAG('I', 'D', 'A', 'T'))
  305. return AVERROR_INVALIDDATA;
  306. size = 38 /* fcTL */ + 8 /* len, tag */ + len + 4 /* crc */;
  307. if (size > INT_MAX)
  308. return AVERROR(EINVAL);
  309. if ((ret = avio_seek(pb, -46, SEEK_CUR)) < 0 ||
  310. (ret = av_append_packet(pb, pkt, size)) < 0)
  311. return ret;
  312. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 8)) < 0)
  313. return ret;
  314. len = avio_rb32(pb);
  315. tag = avio_rl32(pb);
  316. while (tag &&
  317. tag != MKTAG('f', 'c', 'T', 'L') &&
  318. tag != MKTAG('I', 'E', 'N', 'D')) {
  319. if (len > 0x7fffffff)
  320. return AVERROR_INVALIDDATA;
  321. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
  322. (ret = av_append_packet(pb, pkt, len + 12)) < 0)
  323. return ret;
  324. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 8)) < 0)
  325. return ret;
  326. len = avio_rb32(pb);
  327. tag = avio_rl32(pb);
  328. }
  329. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0)
  330. return ret;
  331. if (ctx->is_key_frame)
  332. pkt->flags |= AV_PKT_FLAG_KEY;
  333. return ret;
  334. case MKTAG('I', 'E', 'N', 'D'):
  335. ctx->cur_loop++;
  336. if (ctx->ignore_loop || ctx->num_play >= 1 && ctx->cur_loop == ctx->num_play) {
  337. avio_seek(pb, -8, SEEK_CUR);
  338. return AVERROR_EOF;
  339. }
  340. if ((ret = avio_seek(pb, s->streams[0]->codec->extradata_size + 8, SEEK_SET)) < 0)
  341. return ret;
  342. return 0;
  343. default:
  344. {
  345. char tag_buf[5];
  346. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), tag);
  347. avpriv_request_sample(s, "In-stream tag=%s (0x%08X) len=%"PRIu32, tag_buf, tag, len);
  348. avio_skip(pb, len + 4);
  349. }
  350. }
  351. /* Handle the unsupported yet cases */
  352. return AVERROR_PATCHWELCOME;
  353. }
  354. static const AVOption options[] = {
  355. { "ignore_loop", "ignore loop setting" , offsetof(APNGDemuxContext, ignore_loop),
  356. AV_OPT_TYPE_INT, { .i64 = 1 } , 0, 1 , AV_OPT_FLAG_DECODING_PARAM },
  357. { "max_fps" , "maximum framerate (0 is no limit)" , offsetof(APNGDemuxContext, max_fps),
  358. AV_OPT_TYPE_INT, { .i64 = DEFAULT_APNG_FPS }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  359. { "default_fps", "default framerate (0 is as fast as possible)", offsetof(APNGDemuxContext, default_fps),
  360. AV_OPT_TYPE_INT, { .i64 = DEFAULT_APNG_FPS }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  361. { NULL },
  362. };
  363. static const AVClass demuxer_class = {
  364. .class_name = "APNG demuxer",
  365. .item_name = av_default_item_name,
  366. .option = options,
  367. .version = LIBAVUTIL_VERSION_INT,
  368. .category = AV_CLASS_CATEGORY_DEMUXER,
  369. };
  370. AVInputFormat ff_apng_demuxer = {
  371. .name = "apng",
  372. .long_name = NULL_IF_CONFIG_SMALL("Animated Portable Network Graphics"),
  373. .priv_data_size = sizeof(APNGDemuxContext),
  374. .read_probe = apng_probe,
  375. .read_header = apng_read_header,
  376. .read_packet = apng_read_packet,
  377. .flags = AVFMT_GENERIC_INDEX,
  378. .priv_class = &demuxer_class,
  379. };