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.

448 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 acTL_found = 0;
  133. int64_t ret = AVERROR_INVALIDDATA;
  134. /* verify PNGSIG */
  135. if (avio_rb64(pb) != PNGSIG)
  136. return ret;
  137. /* parse IHDR (must be first chunk) */
  138. len = avio_rb32(pb);
  139. tag = avio_rl32(pb);
  140. if (len != 13 || tag != MKTAG('I', 'H', 'D', 'R'))
  141. return ret;
  142. st = avformat_new_stream(s, NULL);
  143. if (!st)
  144. return AVERROR(ENOMEM);
  145. /* set the timebase to something large enough (1/100,000 of second)
  146. * to hopefully cope with all sane frame durations */
  147. avpriv_set_pts_info(st, 64, 1, 100000);
  148. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  149. st->codec->codec_id = AV_CODEC_ID_APNG;
  150. st->codec->width = avio_rb32(pb);
  151. st->codec->height = avio_rb32(pb);
  152. if ((ret = av_image_check_size(st->codec->width, st->codec->height, 0, s)) < 0)
  153. return ret;
  154. /* extradata will contain every chunk up to the first fcTL (excluded) */
  155. st->codec->extradata = av_malloc(len + 12 + FF_INPUT_BUFFER_PADDING_SIZE);
  156. if (!st->codec->extradata)
  157. return AVERROR(ENOMEM);
  158. st->codec->extradata_size = len + 12;
  159. AV_WB32(st->codec->extradata, len);
  160. AV_WL32(st->codec->extradata+4, tag);
  161. AV_WB32(st->codec->extradata+8, st->codec->width);
  162. AV_WB32(st->codec->extradata+12, st->codec->height);
  163. if ((ret = avio_read(pb, st->codec->extradata+16, 9)) < 0)
  164. goto fail;
  165. while (!avio_feof(pb)) {
  166. if (acTL_found && ctx->num_play != 1) {
  167. int64_t size = avio_size(pb);
  168. int64_t offset = avio_tell(pb);
  169. if (size < 0) {
  170. ret = size;
  171. goto fail;
  172. } else if (offset < 0) {
  173. ret = offset;
  174. goto fail;
  175. } else if ((ret = ffio_ensure_seekback(pb, size - offset)) < 0) {
  176. av_log(s, AV_LOG_WARNING, "Could not ensure seekback, will not loop\n");
  177. ctx->num_play = 1;
  178. }
  179. }
  180. if ((ctx->num_play == 1 || !acTL_found) &&
  181. ((ret = ffio_ensure_seekback(pb, 4 /* len */ + 4 /* tag */)) < 0))
  182. goto fail;
  183. len = avio_rb32(pb);
  184. if (len > 0x7fffffff) {
  185. ret = AVERROR_INVALIDDATA;
  186. goto fail;
  187. }
  188. tag = avio_rl32(pb);
  189. switch (tag) {
  190. case MKTAG('a', 'c', 'T', 'L'):
  191. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
  192. (ret = append_extradata(st->codec, pb, len + 12)) < 0)
  193. goto fail;
  194. acTL_found = 1;
  195. ctx->num_frames = AV_RB32(st->codec->extradata + ret + 8);
  196. ctx->num_play = AV_RB32(st->codec->extradata + ret + 12);
  197. av_log(s, AV_LOG_DEBUG, "num_frames: %"PRIu32", num_play: %"PRIu32"\n",
  198. ctx->num_frames, ctx->num_play);
  199. break;
  200. case MKTAG('f', 'c', 'T', 'L'):
  201. if (!acTL_found) {
  202. ret = AVERROR_INVALIDDATA;
  203. goto fail;
  204. }
  205. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0)
  206. goto fail;
  207. return 0;
  208. default:
  209. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
  210. (ret = append_extradata(st->codec, pb, len + 12)) < 0)
  211. goto fail;
  212. }
  213. }
  214. fail:
  215. if (st->codec->extradata_size) {
  216. av_freep(&st->codec->extradata);
  217. st->codec->extradata_size = 0;
  218. }
  219. return ret;
  220. }
  221. static int decode_fctl_chunk(AVFormatContext *s, APNGDemuxContext *ctx, AVPacket *pkt)
  222. {
  223. uint32_t sequence_number, width, height, x_offset, y_offset;
  224. uint16_t delay_num, delay_den;
  225. uint8_t dispose_op, blend_op;
  226. sequence_number = avio_rb32(s->pb);
  227. width = avio_rb32(s->pb);
  228. height = avio_rb32(s->pb);
  229. x_offset = avio_rb32(s->pb);
  230. y_offset = avio_rb32(s->pb);
  231. delay_num = avio_rb16(s->pb);
  232. delay_den = avio_rb16(s->pb);
  233. dispose_op = avio_r8(s->pb);
  234. blend_op = avio_r8(s->pb);
  235. avio_skip(s->pb, 4); /* crc */
  236. /* default is hundredths of seconds */
  237. if (!delay_den)
  238. delay_den = 100;
  239. if (!delay_num || delay_den / delay_num > ctx->max_fps) {
  240. delay_num = 1;
  241. delay_den = ctx->default_fps;
  242. }
  243. ctx->pkt_duration = av_rescale_q(delay_num,
  244. (AVRational){ 1, delay_den },
  245. s->streams[0]->time_base);
  246. av_log(s, AV_LOG_DEBUG, "%s: "
  247. "sequence_number: %"PRId32", "
  248. "width: %"PRIu32", "
  249. "height: %"PRIu32", "
  250. "x_offset: %"PRIu32", "
  251. "y_offset: %"PRIu32", "
  252. "delay_num: %"PRIu16", "
  253. "delay_den: %"PRIu16", "
  254. "dispose_op: %d, "
  255. "blend_op: %d\n",
  256. __FUNCTION__,
  257. sequence_number,
  258. width,
  259. height,
  260. x_offset,
  261. y_offset,
  262. delay_num,
  263. delay_den,
  264. dispose_op,
  265. blend_op);
  266. if (width != s->streams[0]->codec->width ||
  267. height != s->streams[0]->codec->height ||
  268. x_offset != 0 ||
  269. y_offset != 0) {
  270. if (sequence_number == 0 ||
  271. x_offset >= s->streams[0]->codec->width ||
  272. width > s->streams[0]->codec->width - x_offset ||
  273. y_offset >= s->streams[0]->codec->height ||
  274. height > s->streams[0]->codec->height - y_offset)
  275. return AVERROR_INVALIDDATA;
  276. ctx->is_key_frame = 0;
  277. } else {
  278. if (sequence_number == 0 && dispose_op == APNG_DISPOSE_OP_PREVIOUS)
  279. dispose_op = APNG_DISPOSE_OP_BACKGROUND;
  280. ctx->is_key_frame = dispose_op == APNG_DISPOSE_OP_BACKGROUND ||
  281. blend_op == APNG_BLEND_OP_SOURCE;
  282. }
  283. return 0;
  284. }
  285. static int apng_read_packet(AVFormatContext *s, AVPacket *pkt)
  286. {
  287. APNGDemuxContext *ctx = s->priv_data;
  288. int64_t ret;
  289. int64_t size;
  290. AVIOContext *pb = s->pb;
  291. uint32_t len, tag;
  292. /*
  293. * fcTL chunk length, in bytes:
  294. * 4 (length)
  295. * 4 (tag)
  296. * 26 (actual chunk)
  297. * 4 (crc) bytes
  298. * and needed next:
  299. * 4 (length)
  300. * 4 (tag (must be fdAT or IDAT))
  301. */
  302. /* if num_play is not 1, then the seekback is already guaranteed */
  303. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 46)) < 0)
  304. return ret;
  305. len = avio_rb32(pb);
  306. tag = avio_rl32(pb);
  307. switch (tag) {
  308. case MKTAG('f', 'c', 'T', 'L'):
  309. if (len != 26)
  310. return AVERROR_INVALIDDATA;
  311. if ((ret = decode_fctl_chunk(s, ctx, pkt)) < 0)
  312. return ret;
  313. /* fcTL must precede fdAT or IDAT */
  314. len = avio_rb32(pb);
  315. tag = avio_rl32(pb);
  316. if (len > 0x7fffffff ||
  317. tag != MKTAG('f', 'd', 'A', 'T') &&
  318. tag != MKTAG('I', 'D', 'A', 'T'))
  319. return AVERROR_INVALIDDATA;
  320. size = 38 /* fcTL */ + 8 /* len, tag */ + len + 4 /* crc */;
  321. if (size > INT_MAX)
  322. return AVERROR(EINVAL);
  323. if ((ret = avio_seek(pb, -46, SEEK_CUR)) < 0 ||
  324. (ret = av_append_packet(pb, pkt, size)) < 0)
  325. return ret;
  326. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 8)) < 0)
  327. return ret;
  328. len = avio_rb32(pb);
  329. tag = avio_rl32(pb);
  330. while (tag &&
  331. tag != MKTAG('f', 'c', 'T', 'L') &&
  332. tag != MKTAG('I', 'E', 'N', 'D')) {
  333. if (len > 0x7fffffff)
  334. return AVERROR_INVALIDDATA;
  335. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
  336. (ret = av_append_packet(pb, pkt, len + 12)) < 0)
  337. return ret;
  338. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 8)) < 0)
  339. return ret;
  340. len = avio_rb32(pb);
  341. tag = avio_rl32(pb);
  342. }
  343. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0)
  344. return ret;
  345. if (ctx->is_key_frame)
  346. pkt->flags |= AV_PKT_FLAG_KEY;
  347. pkt->pts = ctx->pkt_pts;
  348. pkt->duration = ctx->pkt_duration;
  349. ctx->pkt_pts += ctx->pkt_duration;
  350. return ret;
  351. case MKTAG('I', 'E', 'N', 'D'):
  352. ctx->cur_loop++;
  353. if (ctx->ignore_loop || ctx->num_play >= 1 && ctx->cur_loop == ctx->num_play) {
  354. avio_seek(pb, -8, SEEK_CUR);
  355. return AVERROR_EOF;
  356. }
  357. if ((ret = avio_seek(pb, s->streams[0]->codec->extradata_size + 8, SEEK_SET)) < 0)
  358. return ret;
  359. return 0;
  360. default:
  361. {
  362. char tag_buf[32];
  363. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), tag);
  364. avpriv_request_sample(s, "In-stream tag=%s (0x%08X) len=%"PRIu32, tag_buf, tag, len);
  365. avio_skip(pb, len + 4);
  366. }
  367. }
  368. /* Handle the unsupported yet cases */
  369. return AVERROR_PATCHWELCOME;
  370. }
  371. static const AVOption options[] = {
  372. { "ignore_loop", "ignore loop setting" , offsetof(APNGDemuxContext, ignore_loop),
  373. AV_OPT_TYPE_INT, { .i64 = 1 } , 0, 1 , AV_OPT_FLAG_DECODING_PARAM },
  374. { "max_fps" , "maximum framerate (0 is no limit)" , offsetof(APNGDemuxContext, max_fps),
  375. AV_OPT_TYPE_INT, { .i64 = DEFAULT_APNG_FPS }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  376. { "default_fps", "default framerate (0 is as fast as possible)", offsetof(APNGDemuxContext, default_fps),
  377. AV_OPT_TYPE_INT, { .i64 = DEFAULT_APNG_FPS }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  378. { NULL },
  379. };
  380. static const AVClass demuxer_class = {
  381. .class_name = "APNG demuxer",
  382. .item_name = av_default_item_name,
  383. .option = options,
  384. .version = LIBAVUTIL_VERSION_INT,
  385. .category = AV_CLASS_CATEGORY_DEMUXER,
  386. };
  387. AVInputFormat ff_apng_demuxer = {
  388. .name = "apng",
  389. .long_name = NULL_IF_CONFIG_SMALL("Animated Portable Network Graphics"),
  390. .priv_data_size = sizeof(APNGDemuxContext),
  391. .read_probe = apng_probe,
  392. .read_header = apng_read_header,
  393. .read_packet = apng_read_packet,
  394. .flags = AVFMT_GENERIC_INDEX,
  395. .priv_class = &demuxer_class,
  396. };