3 * Copyright (c) 2015 Donny Yang
5 * first version by Donny Yang <work@kota.moe>
7 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/avassert.h"
27 #include "libavutil/crc.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/log.h"
30 #include "libavutil/opt.h"
31 #include "libavcodec/png.h"
32 #include "libavcodec/apng.h"
34 typedef struct APNGMuxContext {
38 AVRational last_delay;
41 uint32_t frame_number;
43 AVPacket *prev_packet;
44 AVRational prev_delay;
49 static uint8_t *apng_find_chunk(uint32_t tag, uint8_t *buf, size_t length)
52 for (b = 0; b < length; b += AV_RB32(buf + b) + 12)
53 if (AV_RB32(&buf[b + 4]) == tag)
58 static void apng_write_chunk(AVIOContext *io_context, uint32_t tag,
59 uint8_t *buf, size_t length)
61 const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
65 av_assert0(crc_table);
67 avio_wb32(io_context, length);
69 crc = av_crc(crc_table, crc, tagbuf, 4);
70 avio_wb32(io_context, tag);
72 crc = av_crc(crc_table, crc, buf, length);
73 avio_write(io_context, buf, length);
75 avio_wb32(io_context, ~crc);
78 static int apng_write_header(AVFormatContext *format_context)
80 APNGMuxContext *apng = format_context->priv_data;
82 if (format_context->nb_streams != 1 ||
83 format_context->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
84 format_context->streams[0]->codec->codec_id != AV_CODEC_ID_APNG) {
85 av_log(format_context, AV_LOG_ERROR,
86 "APNG muxer supports only a single video APNG stream.\n");
87 return AVERROR(EINVAL);
90 if (apng->last_delay.num > USHRT_MAX || apng->last_delay.den > USHRT_MAX) {
91 av_reduce(&apng->last_delay.num, &apng->last_delay.den,
92 apng->last_delay.num, apng->last_delay.den, USHRT_MAX);
93 av_log(format_context, AV_LOG_WARNING,
94 "Last frame delay is too precise. Reducing to %d/%d (%f).\n",
95 apng->last_delay.num, apng->last_delay.den, (double)apng->last_delay.num / apng->last_delay.den);
98 avio_wb64(format_context->pb, PNGSIG);
99 // Remaining headers are written when they are copied from the encoder
104 static void flush_packet(AVFormatContext *format_context, AVPacket *packet)
106 APNGMuxContext *apng = format_context->priv_data;
107 AVIOContext *io_context = format_context->pb;
108 AVStream *codec_stream = format_context->streams[0];
109 AVCodecContext *codec_context = codec_stream->codec;
111 av_assert0(apng->prev_packet);
113 if (apng->frame_number == 0 && !packet) {
114 uint8_t *existing_acTL_chunk;
115 uint8_t *existing_fcTL_chunk;
117 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'), codec_context->extradata, codec_context->extradata_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, codec_context->extradata, existing_acTL_chunk - codec_context->extradata);
124 avio_write(io_context, chunk_after_acTL, codec_context->extradata + codec_context->extradata_size - chunk_after_acTL);
126 avio_write(io_context, codec_context->extradata, codec_context->extradata_size);
129 // Write frame data without fcTL chunk
130 existing_fcTL_chunk = apng_find_chunk(MKBETAG('f', 'c', 'T', 'L'), apng->prev_packet->data, apng->prev_packet->size);
131 if (existing_fcTL_chunk) {
132 uint8_t *chunk_after_fcTL = existing_fcTL_chunk + AV_RB32(existing_fcTL_chunk) + 12;
133 avio_write(io_context, apng->prev_packet->data, existing_fcTL_chunk - apng->prev_packet->data);
134 avio_write(io_context, chunk_after_fcTL, apng->prev_packet->data + apng->prev_packet->size - chunk_after_fcTL);
136 avio_write(io_context, apng->prev_packet->data, apng->prev_packet->size);
139 uint8_t *existing_fcTL_chunk;
141 if (apng->frame_number == 0) {
142 uint8_t *existing_acTL_chunk;
144 // Write normal PNG headers
145 avio_write(io_context, codec_context->extradata, codec_context->extradata_size);
147 existing_acTL_chunk = apng_find_chunk(MKBETAG('a', 'c', 'T', 'L'), codec_context->extradata, codec_context->extradata_size);
148 if (!existing_acTL_chunk) {
150 // Write animation control header
151 apng->acTL_offset = avio_tell(io_context);
152 AV_WB32(buf, UINT_MAX); // number of frames (filled in later)
153 AV_WB32(buf + 4, apng->plays);
154 apng_write_chunk(io_context, MKBETAG('a', 'c', 'T', 'L'), buf, 8);
158 existing_fcTL_chunk = apng_find_chunk(MKBETAG('f', 'c', 'T', 'L'), apng->prev_packet->data, apng->prev_packet->size);
159 if (existing_fcTL_chunk) {
162 existing_fcTL_chunk += 8;
163 delay.num = AV_RB16(existing_fcTL_chunk + 20);
164 delay.den = AV_RB16(existing_fcTL_chunk + 22);
166 if (delay.num == 0 && delay.den == 0) {
168 int64_t delay_num_raw = (packet->dts - apng->prev_packet->dts) * codec_stream->time_base.num;
169 int64_t delay_den_raw = codec_stream->time_base.den;
170 if (!av_reduce(&delay.num, &delay.den, delay_num_raw, delay_den_raw, USHRT_MAX) &&
171 !apng->framerate_warned) {
172 av_log(format_context, AV_LOG_WARNING,
173 "Frame rate is too high or specified too precisely. Unable to copy losslessly.\n");
174 apng->framerate_warned = 1;
176 } else if (apng->last_delay.den > 0) {
177 delay = apng->last_delay;
179 delay = apng->prev_delay;
182 // Update frame control header with new delay
183 AV_WB16(existing_fcTL_chunk + 20, delay.num);
184 AV_WB16(existing_fcTL_chunk + 22, delay.den);
185 AV_WB32(existing_fcTL_chunk + 26, ~av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), ~0U, existing_fcTL_chunk - 4, 26 + 4));
187 apng->prev_delay = delay;
191 avio_write(io_context, apng->prev_packet->data, apng->prev_packet->size);
193 ++apng->frame_number;
195 av_free_packet(apng->prev_packet);
197 av_copy_packet(apng->prev_packet, packet);
200 static int apng_write_packet(AVFormatContext *format_context, AVPacket *packet)
202 APNGMuxContext *apng = format_context->priv_data;
204 if (!apng->prev_packet) {
205 apng->prev_packet = av_malloc(sizeof(*apng->prev_packet));
206 if (!apng->prev_packet)
207 return AVERROR(ENOMEM);
209 av_copy_packet(apng->prev_packet, packet);
211 flush_packet(format_context, packet);
217 static int apng_write_trailer(AVFormatContext *format_context)
219 APNGMuxContext *apng = format_context->priv_data;
220 AVIOContext *io_context = format_context->pb;
223 if (apng->prev_packet) {
224 flush_packet(format_context, NULL);
225 av_freep(&apng->prev_packet);
228 apng_write_chunk(io_context, MKBETAG('I', 'E', 'N', 'D'), NULL, 0);
230 if (apng->acTL_offset && io_context->seekable) {
231 avio_seek(io_context, apng->acTL_offset, SEEK_SET);
233 AV_WB32(buf, apng->frame_number);
234 AV_WB32(buf + 4, apng->plays);
235 apng_write_chunk(io_context, MKBETAG('a', 'c', 'T', 'L'), buf, 8);
241 #define OFFSET(x) offsetof(APNGMuxContext, x)
242 #define ENC AV_OPT_FLAG_ENCODING_PARAM
243 static const AVOption options[] = {
244 { "plays", "Number of times to play the output: 0 - infinite loop, 1 - no loop", OFFSET(plays),
245 AV_OPT_TYPE_INT, { .i64 = 1 }, 0, UINT_MAX, ENC },
246 { "final_delay", "Force delay after the last frame", OFFSET(last_delay),
247 AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, USHRT_MAX, ENC },
251 static const AVClass apng_muxer_class = {
252 .class_name = "APNG muxer",
253 .item_name = av_default_item_name,
254 .version = LIBAVUTIL_VERSION_INT,
258 AVOutputFormat ff_apng_muxer = {
260 .long_name = NULL_IF_CONFIG_SMALL("Animated Portable Network Graphics"),
261 .mime_type = "image/png",
262 .extensions = "apng",
263 .priv_data_size = sizeof(APNGMuxContext),
264 .audio_codec = AV_CODEC_ID_NONE,
265 .video_codec = AV_CODEC_ID_APNG,
266 .write_header = apng_write_header,
267 .write_packet = apng_write_packet,
268 .write_trailer = apng_write_trailer,
269 .priv_class = &apng_muxer_class,
270 .flags = AVFMT_VARIABLE_FPS,