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.

447 lines
14KB

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