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.

442 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. int pkt_duration;
  42. int is_key_frame;
  43. /*
  44. * loop options
  45. */
  46. int ignore_loop;
  47. uint32_t num_frames;
  48. uint32_t num_play;
  49. uint32_t cur_loop;
  50. } APNGDemuxContext;
  51. /*
  52. * To be a valid APNG file, we mandate, in this order:
  53. * PNGSIG
  54. * IHDR
  55. * ...
  56. * acTL
  57. * ...
  58. * IDAT
  59. */
  60. static int apng_probe(const AVProbeData *p)
  61. {
  62. GetByteContext gb;
  63. int state = 0;
  64. uint32_t len, tag;
  65. bytestream2_init(&gb, p->buf, p->buf_size);
  66. if (bytestream2_get_be64(&gb) != PNGSIG)
  67. return 0;
  68. for (;;) {
  69. len = bytestream2_get_be32(&gb);
  70. if (len > 0x7fffffff)
  71. return 0;
  72. tag = bytestream2_get_le32(&gb);
  73. /* we don't check IDAT size, as this is the last tag
  74. * we check, and it may be larger than the probe buffer */
  75. if (tag != MKTAG('I', 'D', 'A', 'T') &&
  76. len + 4 > bytestream2_get_bytes_left(&gb))
  77. return 0;
  78. switch (tag) {
  79. case MKTAG('I', 'H', 'D', 'R'):
  80. if (len != 13)
  81. return 0;
  82. if (av_image_check_size(bytestream2_get_be32(&gb), bytestream2_get_be32(&gb), 0, NULL))
  83. return 0;
  84. bytestream2_skip(&gb, 9);
  85. state++;
  86. break;
  87. case MKTAG('a', 'c', 'T', 'L'):
  88. if (state != 1 ||
  89. len != 8 ||
  90. bytestream2_get_be32(&gb) == 0) /* 0 is not a valid value for number of frames */
  91. return 0;
  92. bytestream2_skip(&gb, 8);
  93. state++;
  94. break;
  95. case MKTAG('I', 'D', 'A', 'T'):
  96. if (state != 2)
  97. return 0;
  98. goto end;
  99. default:
  100. /* skip other tags */
  101. bytestream2_skip(&gb, len + 4);
  102. break;
  103. }
  104. }
  105. end:
  106. return AVPROBE_SCORE_MAX;
  107. }
  108. static int append_extradata(AVCodecParameters *par, AVIOContext *pb, int len)
  109. {
  110. int previous_size = par->extradata_size;
  111. int new_size, ret;
  112. uint8_t *new_extradata;
  113. if (previous_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - len)
  114. return AVERROR_INVALIDDATA;
  115. new_size = previous_size + len;
  116. new_extradata = av_realloc(par->extradata, new_size + AV_INPUT_BUFFER_PADDING_SIZE);
  117. if (!new_extradata)
  118. return AVERROR(ENOMEM);
  119. memset(new_extradata + new_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  120. par->extradata = new_extradata;
  121. par->extradata_size = new_size;
  122. if ((ret = ffio_read_size(pb, par->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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  149. st->codecpar->codec_id = AV_CODEC_ID_APNG;
  150. st->codecpar->width = avio_rb32(pb);
  151. st->codecpar->height = avio_rb32(pb);
  152. if ((ret = av_image_check_size(st->codecpar->width, st->codecpar->height, 0, s)) < 0)
  153. return ret;
  154. /* extradata will contain every chunk up to the first fcTL (excluded) */
  155. ret = ff_alloc_extradata(st->codecpar, len + 12);
  156. if (ret < 0)
  157. return ret;
  158. AV_WB32(st->codecpar->extradata, len);
  159. AV_WL32(st->codecpar->extradata+4, tag);
  160. AV_WB32(st->codecpar->extradata+8, st->codecpar->width);
  161. AV_WB32(st->codecpar->extradata+12, st->codecpar->height);
  162. if ((ret = ffio_read_size(pb, st->codecpar->extradata + 16, 9)) < 0)
  163. return ret;
  164. while (1) {
  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->codecpar, pb, len + 12)) < 0)
  192. goto fail;
  193. acTL_found = 1;
  194. ctx->num_frames = AV_RB32(st->codecpar->extradata + ret + 8);
  195. ctx->num_play = AV_RB32(st->codecpar->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->codecpar, pb, len + 12)) < 0)
  210. goto fail;
  211. }
  212. }
  213. fail:
  214. return ret;
  215. }
  216. static int decode_fctl_chunk(AVFormatContext *s, APNGDemuxContext *ctx, AVPacket *pkt)
  217. {
  218. uint32_t sequence_number, width, height, x_offset, y_offset;
  219. uint16_t delay_num, delay_den;
  220. uint8_t dispose_op, blend_op;
  221. sequence_number = avio_rb32(s->pb);
  222. width = avio_rb32(s->pb);
  223. height = avio_rb32(s->pb);
  224. x_offset = avio_rb32(s->pb);
  225. y_offset = avio_rb32(s->pb);
  226. delay_num = avio_rb16(s->pb);
  227. delay_den = avio_rb16(s->pb);
  228. dispose_op = avio_r8(s->pb);
  229. blend_op = avio_r8(s->pb);
  230. avio_skip(s->pb, 4); /* crc */
  231. /* default is hundredths of seconds */
  232. if (!delay_den)
  233. delay_den = 100;
  234. if (!delay_num || (ctx->max_fps && delay_den / delay_num > ctx->max_fps)) {
  235. delay_num = 1;
  236. delay_den = ctx->default_fps;
  237. }
  238. ctx->pkt_duration = av_rescale_q(delay_num,
  239. (AVRational){ 1, delay_den },
  240. s->streams[0]->time_base);
  241. av_log(s, AV_LOG_DEBUG, "%s: "
  242. "sequence_number: %"PRId32", "
  243. "width: %"PRIu32", "
  244. "height: %"PRIu32", "
  245. "x_offset: %"PRIu32", "
  246. "y_offset: %"PRIu32", "
  247. "delay_num: %"PRIu16", "
  248. "delay_den: %"PRIu16", "
  249. "dispose_op: %d, "
  250. "blend_op: %d\n",
  251. __FUNCTION__,
  252. sequence_number,
  253. width,
  254. height,
  255. x_offset,
  256. y_offset,
  257. delay_num,
  258. delay_den,
  259. dispose_op,
  260. blend_op);
  261. if (width != s->streams[0]->codecpar->width ||
  262. height != s->streams[0]->codecpar->height ||
  263. x_offset != 0 ||
  264. y_offset != 0) {
  265. if (sequence_number == 0 ||
  266. x_offset >= s->streams[0]->codecpar->width ||
  267. width > s->streams[0]->codecpar->width - x_offset ||
  268. y_offset >= s->streams[0]->codecpar->height ||
  269. height > s->streams[0]->codecpar->height - y_offset)
  270. return AVERROR_INVALIDDATA;
  271. ctx->is_key_frame = 0;
  272. } else {
  273. if (sequence_number == 0 && dispose_op == APNG_DISPOSE_OP_PREVIOUS)
  274. dispose_op = APNG_DISPOSE_OP_BACKGROUND;
  275. ctx->is_key_frame = dispose_op == APNG_DISPOSE_OP_BACKGROUND ||
  276. blend_op == APNG_BLEND_OP_SOURCE;
  277. }
  278. return 0;
  279. }
  280. static int apng_read_packet(AVFormatContext *s, AVPacket *pkt)
  281. {
  282. APNGDemuxContext *ctx = s->priv_data;
  283. int64_t ret;
  284. int64_t size;
  285. AVIOContext *pb = s->pb;
  286. uint32_t len, tag;
  287. /*
  288. * fcTL chunk length, in bytes:
  289. * 4 (length)
  290. * 4 (tag)
  291. * 26 (actual chunk)
  292. * 4 (crc) bytes
  293. * and needed next:
  294. * 4 (length)
  295. * 4 (tag (must be fdAT or IDAT))
  296. */
  297. /* if num_play is not 1, then the seekback is already guaranteed */
  298. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 46)) < 0)
  299. return ret;
  300. len = avio_rb32(pb);
  301. tag = avio_rl32(pb);
  302. if (avio_feof(pb))
  303. return AVERROR_EOF;
  304. switch (tag) {
  305. case MKTAG('f', 'c', 'T', 'L'):
  306. if (len != 26)
  307. return AVERROR_INVALIDDATA;
  308. if ((ret = decode_fctl_chunk(s, ctx, pkt)) < 0)
  309. return ret;
  310. /* fcTL must precede fdAT or IDAT */
  311. len = avio_rb32(pb);
  312. tag = avio_rl32(pb);
  313. if (len > 0x7fffffff ||
  314. tag != MKTAG('f', 'd', 'A', 'T') &&
  315. tag != MKTAG('I', 'D', 'A', 'T'))
  316. return AVERROR_INVALIDDATA;
  317. size = 38 /* fcTL */ + 8 /* len, tag */ + len + 4 /* crc */;
  318. if (size > INT_MAX)
  319. return AVERROR(EINVAL);
  320. if ((ret = avio_seek(pb, -46, SEEK_CUR)) < 0 ||
  321. (ret = av_append_packet(pb, pkt, size)) < 0)
  322. return ret;
  323. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 8)) < 0)
  324. return ret;
  325. len = avio_rb32(pb);
  326. tag = avio_rl32(pb);
  327. while (tag &&
  328. tag != MKTAG('f', 'c', 'T', 'L') &&
  329. tag != MKTAG('I', 'E', 'N', 'D')) {
  330. if (len > 0x7fffffff)
  331. return AVERROR_INVALIDDATA;
  332. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
  333. (ret = av_append_packet(pb, pkt, len + 12)) < 0)
  334. return ret;
  335. if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 8)) < 0)
  336. return ret;
  337. len = avio_rb32(pb);
  338. tag = avio_rl32(pb);
  339. }
  340. if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0)
  341. return ret;
  342. if (ctx->is_key_frame)
  343. pkt->flags |= AV_PKT_FLAG_KEY;
  344. pkt->pts = pkt->dts = AV_NOPTS_VALUE;
  345. pkt->duration = ctx->pkt_duration;
  346. return ret;
  347. case MKTAG('I', 'E', 'N', 'D'):
  348. ctx->cur_loop++;
  349. if (ctx->ignore_loop || ctx->num_play >= 1 && ctx->cur_loop == ctx->num_play) {
  350. avio_seek(pb, -8, SEEK_CUR);
  351. return AVERROR_EOF;
  352. }
  353. if ((ret = avio_seek(pb, s->streams[0]->codecpar->extradata_size + 8, SEEK_SET)) < 0)
  354. return ret;
  355. return 0;
  356. default:
  357. avpriv_request_sample(s, "In-stream tag=%s (0x%08"PRIX32") len=%"PRIu32,
  358. av_fourcc2str(tag), tag, len);
  359. avio_skip(pb, len + 4);
  360. }
  361. /* Handle the unsupported yet cases */
  362. return AVERROR_PATCHWELCOME;
  363. }
  364. static const AVOption options[] = {
  365. { "ignore_loop", "ignore loop setting" , offsetof(APNGDemuxContext, ignore_loop),
  366. AV_OPT_TYPE_BOOL, { .i64 = 1 } , 0, 1 , AV_OPT_FLAG_DECODING_PARAM },
  367. { "max_fps" , "maximum framerate (0 is no limit)" , offsetof(APNGDemuxContext, max_fps),
  368. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  369. { "default_fps", "default framerate (0 is as fast as possible)", offsetof(APNGDemuxContext, default_fps),
  370. AV_OPT_TYPE_INT, { .i64 = DEFAULT_APNG_FPS }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  371. { NULL },
  372. };
  373. static const AVClass demuxer_class = {
  374. .class_name = "APNG demuxer",
  375. .item_name = av_default_item_name,
  376. .option = options,
  377. .version = LIBAVUTIL_VERSION_INT,
  378. .category = AV_CLASS_CATEGORY_DEMUXER,
  379. };
  380. AVInputFormat ff_apng_demuxer = {
  381. .name = "apng",
  382. .long_name = NULL_IF_CONFIG_SMALL("Animated Portable Network Graphics"),
  383. .priv_data_size = sizeof(APNGDemuxContext),
  384. .read_probe = apng_probe,
  385. .read_header = apng_read_header,
  386. .read_packet = apng_read_packet,
  387. .flags = AVFMT_GENERIC_INDEX,
  388. .priv_class = &demuxer_class,
  389. };