apngenc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. uint8_t *extra_data;
  42. int extra_data_size;
  43. } APNGMuxContext;
  44. static uint8_t *apng_find_chunk(uint32_t tag, uint8_t *buf, size_t length)
  45. {
  46. size_t b;
  47. for (b = 0; b < length; b += AV_RB32(buf + b) + 12)
  48. if (AV_RB32(&buf[b + 4]) == tag)
  49. return &buf[b];
  50. return NULL;
  51. }
  52. static void apng_write_chunk(AVIOContext *io_context, uint32_t tag,
  53. uint8_t *buf, size_t length)
  54. {
  55. const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  56. uint32_t crc = ~0U;
  57. uint8_t tagbuf[4];
  58. av_assert0(crc_table);
  59. avio_wb32(io_context, length);
  60. AV_WB32(tagbuf, tag);
  61. crc = av_crc(crc_table, crc, tagbuf, 4);
  62. avio_wb32(io_context, tag);
  63. if (length > 0) {
  64. crc = av_crc(crc_table, crc, buf, length);
  65. avio_write(io_context, buf, length);
  66. }
  67. avio_wb32(io_context, ~crc);
  68. }
  69. static int apng_write_header(AVFormatContext *format_context)
  70. {
  71. APNGMuxContext *apng = format_context->priv_data;
  72. AVCodecParameters *par = format_context->streams[0]->codecpar;
  73. if (format_context->nb_streams != 1 ||
  74. format_context->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
  75. format_context->streams[0]->codecpar->codec_id != AV_CODEC_ID_APNG) {
  76. av_log(format_context, AV_LOG_ERROR,
  77. "APNG muxer supports only a single video APNG stream.\n");
  78. return AVERROR(EINVAL);
  79. }
  80. if (apng->last_delay.num > USHRT_MAX || apng->last_delay.den > USHRT_MAX) {
  81. av_reduce(&apng->last_delay.num, &apng->last_delay.den,
  82. apng->last_delay.num, apng->last_delay.den, USHRT_MAX);
  83. av_log(format_context, AV_LOG_WARNING,
  84. "Last frame delay is too precise. Reducing to %d/%d (%f).\n",
  85. apng->last_delay.num, apng->last_delay.den, (double)apng->last_delay.num / apng->last_delay.den);
  86. }
  87. avio_wb64(format_context->pb, PNGSIG);
  88. // Remaining headers are written when they are copied from the encoder
  89. if (par->extradata_size) {
  90. apng->extra_data = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  91. if (!apng->extra_data)
  92. return AVERROR(ENOMEM);
  93. apng->extra_data_size = par->extradata_size;
  94. memcpy(apng->extra_data, par->extradata, par->extradata_size);
  95. }
  96. return 0;
  97. }
  98. static int flush_packet(AVFormatContext *format_context, AVPacket *packet)
  99. {
  100. APNGMuxContext *apng = format_context->priv_data;
  101. AVIOContext *io_context = format_context->pb;
  102. AVStream *codec_stream = format_context->streams[0];
  103. uint8_t *side_data = NULL;
  104. int side_data_size = 0;
  105. av_assert0(apng->prev_packet);
  106. side_data = av_packet_get_side_data(apng->prev_packet, AV_PKT_DATA_NEW_EXTRADATA, &side_data_size);
  107. if (side_data_size) {
  108. av_freep(&apng->extra_data);
  109. apng->extra_data = av_mallocz(side_data_size + AV_INPUT_BUFFER_PADDING_SIZE);
  110. if (!apng->extra_data)
  111. return AVERROR(ENOMEM);
  112. apng->extra_data_size = side_data_size;
  113. memcpy(apng->extra_data, side_data, apng->extra_data_size);
  114. }
  115. if (apng->frame_number == 0 && !packet) {
  116. uint8_t *existing_acTL_chunk;
  117. uint8_t *existing_fcTL_chunk;
  118. av_log(format_context, AV_LOG_INFO, "Only a single frame so saving as a normal PNG.\n");
  119. // Write normal PNG headers without acTL chunk
  120. existing_acTL_chunk = apng_find_chunk(MKBETAG('a', 'c', 'T', 'L'), apng->extra_data, apng->extra_data_size);
  121. if (existing_acTL_chunk) {
  122. uint8_t *chunk_after_acTL = existing_acTL_chunk + AV_RB32(existing_acTL_chunk) + 12;
  123. avio_write(io_context, apng->extra_data, existing_acTL_chunk - apng->extra_data);
  124. avio_write(io_context, chunk_after_acTL, apng->extra_data + apng->extra_data_size - chunk_after_acTL);
  125. } else {
  126. avio_write(io_context, apng->extra_data, apng->extra_data_size);
  127. }
  128. // Write frame data without fcTL chunk
  129. existing_fcTL_chunk = apng_find_chunk(MKBETAG('f', 'c', 'T', 'L'), apng->prev_packet->data, apng->prev_packet->size);
  130. if (existing_fcTL_chunk) {
  131. uint8_t *chunk_after_fcTL = existing_fcTL_chunk + AV_RB32(existing_fcTL_chunk) + 12;
  132. avio_write(io_context, apng->prev_packet->data, existing_fcTL_chunk - apng->prev_packet->data);
  133. avio_write(io_context, chunk_after_fcTL, apng->prev_packet->data + apng->prev_packet->size - chunk_after_fcTL);
  134. } else {
  135. avio_write(io_context, apng->prev_packet->data, apng->prev_packet->size);
  136. }
  137. } else {
  138. uint8_t *existing_fcTL_chunk;
  139. if (apng->frame_number == 0) {
  140. uint8_t *existing_acTL_chunk;
  141. // Write normal PNG headers
  142. avio_write(io_context, apng->extra_data, apng->extra_data_size);
  143. existing_acTL_chunk = apng_find_chunk(MKBETAG('a', 'c', 'T', 'L'), apng->extra_data, apng->extra_data_size);
  144. if (!existing_acTL_chunk) {
  145. uint8_t buf[8];
  146. // Write animation control header
  147. apng->acTL_offset = avio_tell(io_context);
  148. AV_WB32(buf, UINT_MAX); // number of frames (filled in later)
  149. AV_WB32(buf + 4, apng->plays);
  150. apng_write_chunk(io_context, MKBETAG('a', 'c', 'T', 'L'), buf, 8);
  151. }
  152. }
  153. existing_fcTL_chunk = apng_find_chunk(MKBETAG('f', 'c', 'T', 'L'), apng->prev_packet->data, apng->prev_packet->size);
  154. if (existing_fcTL_chunk) {
  155. AVRational delay;
  156. existing_fcTL_chunk += 8;
  157. delay.num = AV_RB16(existing_fcTL_chunk + 20);
  158. delay.den = AV_RB16(existing_fcTL_chunk + 22);
  159. if (delay.num == 0 && delay.den == 0) {
  160. if (packet) {
  161. int64_t delay_num_raw = (packet->dts - apng->prev_packet->dts) * codec_stream->time_base.num;
  162. int64_t delay_den_raw = codec_stream->time_base.den;
  163. if (!av_reduce(&delay.num, &delay.den, delay_num_raw, delay_den_raw, USHRT_MAX) &&
  164. !apng->framerate_warned) {
  165. av_log(format_context, AV_LOG_WARNING,
  166. "Frame rate is too high or specified too precisely. Unable to copy losslessly.\n");
  167. apng->framerate_warned = 1;
  168. }
  169. } else if (apng->last_delay.num > 0) {
  170. delay = apng->last_delay;
  171. } else {
  172. delay = apng->prev_delay;
  173. }
  174. // Update frame control header with new delay
  175. AV_WB16(existing_fcTL_chunk + 20, delay.num);
  176. AV_WB16(existing_fcTL_chunk + 22, delay.den);
  177. AV_WB32(existing_fcTL_chunk + 26, ~av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), ~0U, existing_fcTL_chunk - 4, 26 + 4));
  178. }
  179. apng->prev_delay = delay;
  180. }
  181. // Write frame data
  182. avio_write(io_context, apng->prev_packet->data, apng->prev_packet->size);
  183. }
  184. ++apng->frame_number;
  185. av_packet_unref(apng->prev_packet);
  186. if (packet)
  187. av_copy_packet(apng->prev_packet, packet);
  188. return 0;
  189. }
  190. static int apng_write_packet(AVFormatContext *format_context, AVPacket *packet)
  191. {
  192. APNGMuxContext *apng = format_context->priv_data;
  193. int ret;
  194. if (!apng->prev_packet) {
  195. apng->prev_packet = av_malloc(sizeof(*apng->prev_packet));
  196. if (!apng->prev_packet)
  197. return AVERROR(ENOMEM);
  198. av_copy_packet(apng->prev_packet, packet);
  199. } else {
  200. ret = flush_packet(format_context, packet);
  201. if (ret < 0)
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. static int apng_write_trailer(AVFormatContext *format_context)
  207. {
  208. APNGMuxContext *apng = format_context->priv_data;
  209. AVIOContext *io_context = format_context->pb;
  210. uint8_t buf[8];
  211. int ret;
  212. if (apng->prev_packet) {
  213. ret = flush_packet(format_context, NULL);
  214. av_freep(&apng->prev_packet);
  215. if (ret < 0)
  216. return ret;
  217. }
  218. apng_write_chunk(io_context, MKBETAG('I', 'E', 'N', 'D'), NULL, 0);
  219. if (apng->acTL_offset && io_context->seekable) {
  220. avio_seek(io_context, apng->acTL_offset, SEEK_SET);
  221. AV_WB32(buf, apng->frame_number);
  222. AV_WB32(buf + 4, apng->plays);
  223. apng_write_chunk(io_context, MKBETAG('a', 'c', 'T', 'L'), buf, 8);
  224. }
  225. av_freep(&apng->extra_data);
  226. apng->extra_data = 0;
  227. return 0;
  228. }
  229. #define OFFSET(x) offsetof(APNGMuxContext, x)
  230. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  231. static const AVOption options[] = {
  232. { "plays", "Number of times to play the output: 0 - infinite loop, 1 - no loop", OFFSET(plays),
  233. AV_OPT_TYPE_INT, { .i64 = 1 }, 0, UINT_MAX, ENC },
  234. { "final_delay", "Force delay after the last frame", OFFSET(last_delay),
  235. AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, USHRT_MAX, ENC },
  236. { NULL },
  237. };
  238. static const AVClass apng_muxer_class = {
  239. .class_name = "APNG muxer",
  240. .item_name = av_default_item_name,
  241. .version = LIBAVUTIL_VERSION_INT,
  242. .option = options,
  243. };
  244. AVOutputFormat ff_apng_muxer = {
  245. .name = "apng",
  246. .long_name = NULL_IF_CONFIG_SMALL("Animated Portable Network Graphics"),
  247. .mime_type = "image/png",
  248. .extensions = "apng",
  249. .priv_data_size = sizeof(APNGMuxContext),
  250. .audio_codec = AV_CODEC_ID_NONE,
  251. .video_codec = AV_CODEC_ID_APNG,
  252. .write_header = apng_write_header,
  253. .write_packet = apng_write_packet,
  254. .write_trailer = apng_write_trailer,
  255. .priv_class = &apng_muxer_class,
  256. .flags = AVFMT_VARIABLE_FPS,
  257. };