FFmpeg coverage


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