FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/apngenc.c
Date: 2026-10-31 18:41:55
Exec Total Coverage
Lines: 117 136 86.0%
Functions: 7 7 100.0%
Branches: 47 68 69.1%

Line Branch Exec Source
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
24 #include "avformat.h"
25 #include "mux.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/crc.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/log.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "libavcodec/apng.h"
33 #include "libavcodec/png.h"
34
35 typedef struct APNGMuxContext {
36 AVClass *class;
37
38 uint32_t plays;
39 AVRational last_delay;
40
41 uint64_t acTL_offset;
42 uint32_t frame_number;
43
44 AVPacket *prev_packet;
45 AVRational prev_delay;
46
47 int framerate_warned;
48
49 uint8_t *extra_data;
50 int extra_data_size;
51 } APNGMuxContext;
52
53 54 static const uint8_t *apng_find_chunk(uint32_t tag, const uint8_t *buf,
54 size_t length)
55 {
56 size_t b;
57
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2 times.
60 for (b = 0; AV_RB32(buf + b) + 12ULL <= length - b; b += AV_RB32(buf + b) + 12ULL)
58
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 6 times.
58 if (AV_RB32(&buf[b + 4]) == tag)
59 52 return &buf[b];
60 2 return NULL;
61 }
62
63 30 static void apng_write_chunk(AVIOContext *io_context, uint32_t tag,
64 uint8_t *buf, size_t length)
65 {
66 30 const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
67 30 uint32_t crc = ~0U;
68 uint8_t tagbuf[4];
69
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 av_assert0(crc_table);
71
72 30 avio_wb32(io_context, length);
73 30 AV_WB32(tagbuf, tag);
74 30 crc = av_crc(crc_table, crc, tagbuf, 4);
75 30 avio_wb32(io_context, tag);
76
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 3 times.
30 if (length > 0) {
77 27 crc = av_crc(crc_table, crc, buf, length);
78 27 avio_write(io_context, buf, length);
79 }
80 30 avio_wb32(io_context, ~crc);
81 30 }
82
83 3 static int apng_write_header(AVFormatContext *format_context)
84 {
85 3 APNGMuxContext *apng = format_context->priv_data;
86 3 AVCodecParameters *par = format_context->streams[0]->codecpar;
87
88
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (apng->last_delay.num > UINT16_MAX || apng->last_delay.den > UINT16_MAX) {
89 ✗ av_reduce(&apng->last_delay.num, &apng->last_delay.den,
90 ✗ apng->last_delay.num, apng->last_delay.den, UINT16_MAX);
91 ✗ av_log(format_context, AV_LOG_WARNING,
92 "Last frame delay is too precise. Reducing to %d/%d (%f).\n",
93 ✗ apng->last_delay.num, apng->last_delay.den, (double)apng->last_delay.num / apng->last_delay.den);
94 }
95
96 3 avio_wb64(format_context->pb, PNGSIG);
97 // Remaining headers are written when they are copied from the encoder
98
99
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (par->extradata_size) {
100 1 apng->extra_data = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!apng->extra_data)
102 ✗ return AVERROR(ENOMEM);
103 1 apng->extra_data_size = par->extradata_size;
104 1 memcpy(apng->extra_data, par->extradata, par->extradata_size);
105 }
106
107 3 return 0;
108 }
109
110 51 static int flush_packet(AVFormatContext *format_context, AVPacket *packet)
111 {
112 51 APNGMuxContext *apng = format_context->priv_data;
113 51 AVIOContext *io_context = format_context->pb;
114 51 AVStream *codec_stream = format_context->streams[0];
115 51 uint8_t *side_data = NULL;
116 size_t side_data_size;
117
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 av_assert0(apng->prev_packet);
119
120 51 side_data = av_packet_get_side_data(apng->prev_packet, AV_PKT_DATA_NEW_EXTRADATA, &side_data_size);
121
122
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 49 times.
51 if (side_data_size) {
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (side_data_size > INT_MAX)
124 ✗ return AVERROR(EINVAL);
125 2 av_freep(&apng->extra_data);
126 2 apng->extra_data = av_mallocz(side_data_size + AV_INPUT_BUFFER_PADDING_SIZE);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!apng->extra_data)
128 ✗ return AVERROR(ENOMEM);
129 2 apng->extra_data_size = side_data_size;
130 2 memcpy(apng->extra_data, side_data, apng->extra_data_size);
131 }
132
133
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
52 if (apng->frame_number == 0 && !packet) {
134 const uint8_t *existing_acTL_chunk;
135 const uint8_t *existing_fcTL_chunk;
136
137 1 av_log(format_context, AV_LOG_INFO, "Only a single frame so saving as a normal PNG.\n");
138
139 // Write normal PNG headers without acTL chunk
140 1 existing_acTL_chunk = apng_find_chunk(MKBETAG('a', 'c', 'T', 'L'), apng->extra_data, apng->extra_data_size);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (existing_acTL_chunk) {
142 ✗ const uint8_t *chunk_after_acTL = existing_acTL_chunk + AV_RB32(existing_acTL_chunk) + 12;
143 ✗ avio_write(io_context, apng->extra_data, existing_acTL_chunk - apng->extra_data);
144 ✗ avio_write(io_context, chunk_after_acTL, apng->extra_data + apng->extra_data_size - chunk_after_acTL);
145 } else {
146 1 avio_write(io_context, apng->extra_data, apng->extra_data_size);
147 }
148
149 // Write frame data without fcTL chunk
150 1 existing_fcTL_chunk = apng_find_chunk(MKBETAG('f', 'c', 'T', 'L'), apng->prev_packet->data, apng->prev_packet->size);
151
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (existing_fcTL_chunk) {
152 1 const uint8_t *chunk_after_fcTL = existing_fcTL_chunk + AV_RB32(existing_fcTL_chunk) + 12;
153 1 avio_write(io_context, apng->prev_packet->data, existing_fcTL_chunk - apng->prev_packet->data);
154 1 avio_write(io_context, chunk_after_fcTL, apng->prev_packet->data + apng->prev_packet->size - chunk_after_fcTL);
155 } else {
156 ✗ avio_write(io_context, apng->prev_packet->data, apng->prev_packet->size);
157 }
158 } else {
159 const uint8_t *data, *data_end;
160 const uint8_t *existing_fcTL_chunk;
161
162
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 48 times.
50 if (apng->frame_number == 0) {
163 const uint8_t *existing_acTL_chunk;
164
165 // Write normal PNG headers
166 2 avio_write(io_context, apng->extra_data, apng->extra_data_size);
167
168 2 existing_acTL_chunk = apng_find_chunk(MKBETAG('a', 'c', 'T', 'L'), apng->extra_data, apng->extra_data_size);
169
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!existing_acTL_chunk) {
170 uint8_t buf[8];
171 // Write animation control header
172 1 apng->acTL_offset = avio_tell(io_context);
173 1 AV_WB32(buf, UINT_MAX); // number of frames (filled in later)
174 1 AV_WB32(buf + 4, apng->plays);
175 1 apng_write_chunk(io_context, MKBETAG('a', 'c', 'T', 'L'), buf, 8);
176 }
177 }
178
179 50 data = apng->prev_packet->data;
180 50 data_end = data + apng->prev_packet->size;
181 50 existing_fcTL_chunk = apng_find_chunk(MKBETAG('f', 'c', 'T', 'L'), apng->prev_packet->data, apng->prev_packet->size);
182
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (existing_fcTL_chunk) {
183 AVRational delay;
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (AV_RB32(existing_fcTL_chunk) != APNG_FCTL_CHUNK_SIZE)
186 ✗ return AVERROR_INVALIDDATA;
187
188 50 existing_fcTL_chunk += 8;
189 50 delay.num = AV_RB16(existing_fcTL_chunk + 20);
190 50 delay.den = AV_RB16(existing_fcTL_chunk + 22);
191
192
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
50 if (delay.num == 0 && delay.den == 0) {
193 uint8_t new_fcTL_chunk[APNG_FCTL_CHUNK_SIZE];
194
195
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
25 if (packet) {
196 24 int64_t delay_num_raw = (packet->dts - apng->prev_packet->dts) * codec_stream->time_base.num;
197 24 int64_t delay_den_raw = codec_stream->time_base.den;
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (!av_reduce(&delay.num, &delay.den, delay_num_raw, delay_den_raw, UINT16_MAX) &&
199 ✗ !apng->framerate_warned) {
200 ✗ av_log(format_context, AV_LOG_WARNING,
201 "Frame rate is too high or specified too precisely. Unable to copy losslessly.\n");
202 ✗ apng->framerate_warned = 1;
203 }
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (apng->last_delay.num > 0) {
205 ✗ delay = apng->last_delay;
206 } else {
207 1 delay = apng->prev_delay;
208 }
209
210 25 avio_write(io_context, data, (existing_fcTL_chunk - 8) - data);
211 25 data = existing_fcTL_chunk + APNG_FCTL_CHUNK_SIZE + 4 /* CRC-32 */;
212 // Update frame control header with new delay
213 25 memcpy(new_fcTL_chunk, existing_fcTL_chunk, sizeof(new_fcTL_chunk));
214 25 AV_WB16(new_fcTL_chunk + 20, delay.num);
215 25 AV_WB16(new_fcTL_chunk + 22, delay.den);
216 25 apng_write_chunk(io_context, MKBETAG('f', 'c', 'T', 'L'),
217 new_fcTL_chunk, sizeof(new_fcTL_chunk));
218 }
219 50 apng->prev_delay = delay;
220 }
221
222 // Write frame data
223 50 avio_write(io_context, data, data_end - data);
224 }
225 51 ++apng->frame_number;
226
227 51 av_packet_unref(apng->prev_packet);
228
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 if (packet)
229 48 av_packet_ref(apng->prev_packet, packet);
230 51 return 0;
231 }
232
233 51 static int apng_write_packet(AVFormatContext *format_context, AVPacket *packet)
234 {
235 51 APNGMuxContext *apng = format_context->priv_data;
236 int ret;
237
238
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 48 times.
51 if (!apng->prev_packet) {
239 3 apng->prev_packet = av_packet_alloc();
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!apng->prev_packet)
241 ✗ return AVERROR(ENOMEM);
242
243 3 av_packet_ref(apng->prev_packet, packet);
244 } else {
245 48 ret = flush_packet(format_context, packet);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (ret < 0)
247 ✗ return ret;
248 }
249
250 51 return 0;
251 }
252
253 3 static int apng_write_trailer(AVFormatContext *format_context)
254 {
255 3 APNGMuxContext *apng = format_context->priv_data;
256 3 AVIOContext *io_context = format_context->pb;
257 uint8_t buf[8];
258 int ret;
259
260
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (apng->prev_packet) {
261 3 ret = flush_packet(format_context, NULL);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
263 ✗ return ret;
264 }
265
266 3 apng_write_chunk(io_context, MKBETAG('I', 'E', 'N', 'D'), NULL, 0);
267
268
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
3 if (apng->acTL_offset && (io_context->seekable & AVIO_SEEKABLE_NORMAL)) {
269 1 avio_seek(io_context, apng->acTL_offset, SEEK_SET);
270
271 1 AV_WB32(buf, apng->frame_number);
272 1 AV_WB32(buf + 4, apng->plays);
273 1 apng_write_chunk(io_context, MKBETAG('a', 'c', 'T', 'L'), buf, 8);
274 }
275
276 3 return 0;
277 }
278
279 3 static void apng_deinit(AVFormatContext *s)
280 {
281 3 APNGMuxContext *apng = s->priv_data;
282
283 3 av_packet_free(&apng->prev_packet);
284 3 av_freep(&apng->extra_data);
285 3 apng->extra_data_size = 0;
286 3 }
287
288 #define OFFSET(x) offsetof(APNGMuxContext, x)
289 #define ENC AV_OPT_FLAG_ENCODING_PARAM
290 static const AVOption options[] = {
291 { "plays", "Number of times to play the output: 0 - infinite loop, 1 - no loop", OFFSET(plays),
292 AV_OPT_TYPE_INT, { .i64 = 1 }, 0, UINT16_MAX, ENC },
293 { "final_delay", "Force delay after the last frame", OFFSET(last_delay),
294 AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, UINT16_MAX, ENC },
295 { NULL },
296 };
297
298 static const AVClass apng_muxer_class = {
299 .class_name = "APNG muxer",
300 .item_name = av_default_item_name,
301 .version = LIBAVUTIL_VERSION_INT,
302 .option = options,
303 };
304
305 const FFOutputFormat ff_apng_muxer = {
306 .p.name = "apng",
307 .p.long_name = NULL_IF_CONFIG_SMALL("Animated Portable Network Graphics"),
308 .p.mime_type = "image/png",
309 .p.extensions = "apng",
310 .priv_data_size = sizeof(APNGMuxContext),
311 .p.audio_codec = AV_CODEC_ID_NONE,
312 .p.video_codec = AV_CODEC_ID_APNG,
313 .p.subtitle_codec = AV_CODEC_ID_NONE,
314 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
315 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
316 .write_header = apng_write_header,
317 .write_packet = apng_write_packet,
318 .write_trailer = apng_write_trailer,
319 .deinit = apng_deinit,
320 .p.priv_class = &apng_muxer_class,
321 .p.flags = AVFMT_VARIABLE_FPS,
322 };
323