apngenc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * APNG muxer
  3. * Copyright (c) 2015 Donny Yang
  4. *
  5. * first version by Donny Yang <work@kota.moe>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/crc.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/log.h"
  29. #include "libavutil/opt.h"
  30. #include "libavcodec/png.h"
  31. #include "libavcodec/apng.h"
  32. typedef struct APNGMuxContext {
  33. AVClass *class;
  34. uint32_t plays;
  35. AVRational last_delay;
  36. uint64_t acTL_offset;
  37. uint32_t frame_number;
  38. AVPacket *prev_packet;
  39. AVRational prev_delay;
  40. int framerate_warned;
  41. } APNGMuxContext;
  42. static uint8_t *apng_find_chunk(uint32_t tag, uint8_t *buf, size_t length)
  43. {
  44. size_t b;
  45. for (b = 0; b < length; b += AV_RB32(buf + b) + 12)
  46. if (AV_RB32(&buf[b + 4]) == tag)
  47. return &buf[b];
  48. return NULL;
  49. }
  50. static void apng_write_chunk(AVIOContext *io_context, uint32_t tag,
  51. uint8_t *buf, size_t length)
  52. {
  53. const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  54. uint32_t crc = ~0U;
  55. uint8_t tagbuf[4];
  56. av_assert0(crc_table);
  57. avio_wb32(io_context, length);
  58. AV_WB32(tagbuf, tag);
  59. crc = av_crc(crc_table, crc, tagbuf, 4);
  60. avio_wb32(io_context, tag);
  61. if (length > 0) {
  62. crc = av_crc(crc_table, crc, buf, length);
  63. avio_write(io_context, buf, length);
  64. }
  65. avio_wb32(io_context, ~crc);
  66. }
  67. static int apng_write_header(AVFormatContext *format_context)
  68. {
  69. APNGMuxContext *apng = format_context->priv_data;
  70. if (format_context->nb_streams != 1 ||
  71. format_context->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
  72. format_context->streams[0]->codec->codec_id != AV_CODEC_ID_APNG) {
  73. av_log(format_context, AV_LOG_ERROR,
  74. "APNG muxer supports only a single video APNG stream.\n");
  75. return AVERROR(EINVAL);
  76. }
  77. if (apng->last_delay.num > USHRT_MAX || apng->last_delay.den > USHRT_MAX) {
  78. av_reduce(&apng->last_delay.num, &apng->last_delay.den,
  79. apng->last_delay.num, apng->last_delay.den, USHRT_MAX);
  80. av_log(format_context, AV_LOG_WARNING,
  81. "Last frame delay is too precise. Reducing to %d/%d (%f).\n",
  82. apng->last_delay.num, apng->last_delay.den, (double)apng->last_delay.num / apng->last_delay.den);
  83. }
  84. avio_wb64(format_context->pb, PNGSIG);
  85. // Remaining headers are written when they are copied from the encoder
  86. return 0;
  87. }
  88. static void flush_packet(AVFormatContext *format_context, AVPacket *packet)
  89. {
  90. APNGMuxContext *apng = format_context->priv_data;
  91. AVIOContext *io_context = format_context->pb;
  92. AVStream *codec_stream = format_context->streams[0];
  93. AVCodecContext *codec_context = codec_stream->codec;
  94. av_assert0(apng->prev_packet);
  95. if (apng->frame_number == 0 && !packet) {
  96. uint8_t *existing_acTL_chunk;
  97. uint8_t *existing_fcTL_chunk;
  98. av_log(format_context, AV_LOG_INFO, "Only a single frame so saving as a normal PNG.\n");
  99. // Write normal PNG headers without acTL chunk
  100. existing_acTL_chunk = apng_find_chunk(MKBETAG('a', 'c', 'T', 'L'), codec_context->extradata, codec_context->extradata_size);
  101. if (existing_acTL_chunk) {
  102. uint8_t *chunk_after_acTL = existing_acTL_chunk + AV_RB32(existing_acTL_chunk) + 12;
  103. avio_write(io_context, codec_context->extradata, existing_acTL_chunk - codec_context->extradata);
  104. avio_write(io_context, chunk_after_acTL, codec_context->extradata + codec_context->extradata_size - chunk_after_acTL);
  105. } else {
  106. avio_write(io_context, codec_context->extradata, codec_context->extradata_size);
  107. }
  108. // Write frame data without fcTL chunk
  109. existing_fcTL_chunk = apng_find_chunk(MKBETAG('f', 'c', 'T', 'L'), apng->prev_packet->data, apng->prev_packet->size);
  110. if (existing_fcTL_chunk) {
  111. uint8_t *chunk_after_fcTL = existing_fcTL_chunk + AV_RB32(existing_fcTL_chunk) + 12;
  112. avio_write(io_context, apng->prev_packet->data, existing_fcTL_chunk - apng->prev_packet->data);
  113. avio_write(io_context, chunk_after_fcTL, apng->prev_packet->data + apng->prev_packet->size - chunk_after_fcTL);
  114. } else {
  115. avio_write(io_context, apng->prev_packet->data, apng->prev_packet->size);
  116. }
  117. } else {
  118. uint8_t *existing_fcTL_chunk;
  119. if (apng->frame_number == 0) {
  120. uint8_t *existing_acTL_chunk;
  121. // Write normal PNG headers
  122. avio_write(io_context, codec_context->extradata, codec_context->extradata_size);
  123. existing_acTL_chunk = apng_find_chunk(MKBETAG('a', 'c', 'T', 'L'), codec_context->extradata, codec_context->extradata_size);
  124. if (!existing_acTL_chunk) {
  125. uint8_t buf[8];
  126. // Write animation control header
  127. apng->acTL_offset = avio_tell(io_context);
  128. AV_WB32(buf, UINT_MAX); // number of frames (filled in later)
  129. AV_WB32(buf + 4, apng->plays);
  130. apng_write_chunk(io_context, MKBETAG('a', 'c', 'T', 'L'), buf, 8);
  131. }
  132. }
  133. existing_fcTL_chunk = apng_find_chunk(MKBETAG('f', 'c', 'T', 'L'), apng->prev_packet->data, apng->prev_packet->size);
  134. if (existing_fcTL_chunk) {
  135. AVRational delay;
  136. existing_fcTL_chunk += 8;
  137. delay.num = AV_RB16(existing_fcTL_chunk + 20);
  138. delay.den = AV_RB16(existing_fcTL_chunk + 22);
  139. if (delay.num == 0 && delay.den == 0) {
  140. if (packet) {
  141. int64_t delay_num_raw = (packet->dts - apng->prev_packet->dts) * codec_stream->time_base.num;
  142. int64_t delay_den_raw = codec_stream->time_base.den;
  143. if (!av_reduce(&delay.num, &delay.den, delay_num_raw, delay_den_raw, USHRT_MAX) &&
  144. !apng->framerate_warned) {
  145. av_log(format_context, AV_LOG_WARNING,
  146. "Frame rate is too high or specified too precisely. Unable to copy losslessly.\n");
  147. apng->framerate_warned = 1;
  148. }
  149. } else if (apng->last_delay.den > 0) {
  150. delay = apng->last_delay;
  151. } else {
  152. delay = apng->prev_delay;
  153. }
  154. // Update frame control header with new delay
  155. AV_WB16(existing_fcTL_chunk + 20, delay.num);
  156. AV_WB16(existing_fcTL_chunk + 22, delay.den);
  157. AV_WB32(existing_fcTL_chunk + 26, ~av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), ~0U, existing_fcTL_chunk - 4, 26 + 4));
  158. }
  159. apng->prev_delay = delay;
  160. }
  161. // Write frame data
  162. avio_write(io_context, apng->prev_packet->data, apng->prev_packet->size);
  163. }
  164. ++apng->frame_number;
  165. av_free_packet(apng->prev_packet);
  166. if (packet)
  167. av_copy_packet(apng->prev_packet, packet);
  168. }
  169. static int apng_write_packet(AVFormatContext *format_context, AVPacket *packet)
  170. {
  171. APNGMuxContext *apng = format_context->priv_data;
  172. if (!apng->prev_packet) {
  173. apng->prev_packet = av_malloc(sizeof(*apng->prev_packet));
  174. if (!apng->prev_packet)
  175. return AVERROR(ENOMEM);
  176. av_copy_packet(apng->prev_packet, packet);
  177. } else {
  178. flush_packet(format_context, packet);
  179. }
  180. return 0;
  181. }
  182. static int apng_write_trailer(AVFormatContext *format_context)
  183. {
  184. APNGMuxContext *apng = format_context->priv_data;
  185. AVIOContext *io_context = format_context->pb;
  186. uint8_t buf[8];
  187. if (apng->prev_packet) {
  188. flush_packet(format_context, NULL);
  189. av_freep(&apng->prev_packet);
  190. }
  191. apng_write_chunk(io_context, MKBETAG('I', 'E', 'N', 'D'), NULL, 0);
  192. if (apng->acTL_offset && io_context->seekable) {
  193. avio_seek(io_context, apng->acTL_offset, SEEK_SET);
  194. AV_WB32(buf, apng->frame_number);
  195. AV_WB32(buf + 4, apng->plays);
  196. apng_write_chunk(io_context, MKBETAG('a', 'c', 'T', 'L'), buf, 8);
  197. }
  198. return 0;
  199. }
  200. #define OFFSET(x) offsetof(APNGMuxContext, x)
  201. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  202. static const AVOption options[] = {
  203. { "plays", "Number of times to play the output: 0 - infinite loop, 1 - no loop", OFFSET(plays),
  204. AV_OPT_TYPE_INT, { .i64 = 1 }, 0, UINT_MAX, ENC },
  205. { "final_delay", "Force delay after the last frame", OFFSET(last_delay),
  206. AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, USHRT_MAX, ENC },
  207. { NULL },
  208. };
  209. static const AVClass apng_muxer_class = {
  210. .class_name = "APNG muxer",
  211. .item_name = av_default_item_name,
  212. .version = LIBAVUTIL_VERSION_INT,
  213. .option = options,
  214. };
  215. AVOutputFormat ff_apng_muxer = {
  216. .name = "apng",
  217. .long_name = NULL_IF_CONFIG_SMALL("Animated Portable Network Graphics"),
  218. .mime_type = "image/png",
  219. .extensions = "apng",
  220. .priv_data_size = sizeof(APNGMuxContext),
  221. .audio_codec = AV_CODEC_ID_NONE,
  222. .video_codec = AV_CODEC_ID_APNG,
  223. .write_header = apng_write_header,
  224. .write_packet = apng_write_packet,
  225. .write_trailer = apng_write_trailer,
  226. .priv_class = &apng_muxer_class,
  227. .flags = AVFMT_VARIABLE_FPS,
  228. };