FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pngenc.c
Date: 2026-09-13 04:46:26
Exec Total Coverage
Lines: 512 769 66.6%
Functions: 20 21 95.2%
Branches: 188 379 49.6%

Line Branch Exec Source
1 /*
2 * PNG image format
3 * Copyright (c) 2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <math.h>
23
24 #include "avcodec.h"
25 #include "codec_internal.h"
26 #include "encode.h"
27 #include "exif_internal.h"
28 #include "bytestream.h"
29 #include "lossless_videoencdsp.h"
30 #include "png.h"
31 #include "apng.h"
32 #include "zlib_wrapper.h"
33
34 #include "libavutil/avassert.h"
35 #include "libavutil/buffer.h"
36 #include "libavutil/crc.h"
37 #include "libavutil/csp.h"
38 #include "libavutil/mastering_display_metadata.h"
39 #include "libavutil/mem.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/pixdesc.h"
42 #include "libavutil/rational.h"
43 #include "libavutil/stereo3d.h"
44 #include <zlib.h>
45
46 #define IOBUF_SIZE 4096
47
48 typedef struct APNGFctlChunk {
49 uint32_t sequence_number;
50 uint32_t width, height;
51 uint32_t x_offset, y_offset;
52 uint16_t delay_num, delay_den;
53 uint8_t dispose_op, blend_op;
54 } APNGFctlChunk;
55
56 typedef struct PNGEncContext {
57 AVClass *class;
58 LLVidEncDSPContext llvidencdsp;
59
60 uint8_t *bytestream;
61 uint8_t *bytestream_start;
62 uint8_t *bytestream_end;
63
64 int filter_type;
65
66 FFZStream zstream;
67 uint8_t buf[IOBUF_SIZE];
68 int dpi; ///< Physical pixel density, in dots per inch, if set
69 int dpm; ///< Physical pixel density, in dots per meter, if set
70
71 int is_progressive;
72 int bit_depth;
73 int color_type;
74 int bits_per_pixel;
75
76 AVBufferRef *exif_data;
77
78 // APNG
79 uint32_t palette_checksum; // Used to ensure a single unique palette
80 uint32_t sequence_number;
81 int extra_data_updated;
82 uint8_t *extra_data;
83 int extra_data_size;
84
85 AVFrame *prev_frame;
86 AVFrame *last_frame;
87 APNGFctlChunk last_frame_fctl;
88 uint8_t *last_frame_packet;
89 size_t last_frame_packet_size;
90 } PNGEncContext;
91
92 static void png_get_interlaced_row(uint8_t *dst, int row_size,
93 int bits_per_pixel, int pass,
94 const uint8_t *src, int width)
95 {
96 int x, mask, dst_x, j, b, bpp;
97 uint8_t *d;
98 const uint8_t *s;
99 static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
100
101 mask = masks[pass];
102 switch (bits_per_pixel) {
103 case 1:
104 memset(dst, 0, row_size);
105 dst_x = 0;
106 for (x = 0; x < width; x++) {
107 j = (x & 7);
108 if ((mask << j) & 0x80) {
109 b = (src[x >> 3] >> (7 - j)) & 1;
110 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
111 dst_x++;
112 }
113 }
114 break;
115 default:
116 bpp = bits_per_pixel >> 3;
117 d = dst;
118 s = src;
119 for (x = 0; x < width; x++) {
120 j = x & 7;
121 if ((mask << j) & 0x80) {
122 memcpy(d, s, bpp);
123 d += bpp;
124 }
125 s += bpp;
126 }
127 break;
128 }
129 }
130
131 73507 static void sub_png_paeth_prediction(uint8_t *dst, const uint8_t *src, const uint8_t *top,
132 int w, int bpp)
133 {
134 int i;
135
2/2
✓ Branch 0 taken 81167103 times.
✓ Branch 1 taken 73507 times.
81240610 for (i = 0; i < w; i++) {
136 int a, b, c, p, pa, pb, pc;
137
138 81167103 a = src[i - bpp];
139 81167103 b = top[i];
140 81167103 c = top[i - bpp];
141
142 81167103 p = b - c;
143 81167103 pc = a - c;
144
145 81167103 pa = abs(p);
146 81167103 pb = abs(pc);
147 81167103 pc = abs(p + pc);
148
149
4/4
✓ Branch 0 taken 47549329 times.
✓ Branch 1 taken 33617774 times.
✓ Branch 2 taken 41704888 times.
✓ Branch 3 taken 5844441 times.
81167103 if (pa <= pb && pa <= pc)
150 41704888 p = a;
151
2/2
✓ Branch 0 taken 30751731 times.
✓ Branch 1 taken 8710484 times.
39462215 else if (pb <= pc)
152 30751731 p = b;
153 else
154 8710484 p = c;
155 81167103 dst[i] = src[i] - p;
156 }
157 73507 }
158
159 297 static void sub_left_prediction(PNGEncContext *c, uint8_t *dst, const uint8_t *src, int bpp, int size)
160 {
161 297 const uint8_t *src1 = src + bpp;
162 297 const uint8_t *src2 = src;
163 int x, unaligned_w;
164
165 297 memcpy(dst, src, bpp);
166 297 dst += bpp;
167 297 size -= bpp;
168 297 unaligned_w = FFMIN(32 - bpp, size);
169
2/2
✓ Branch 0 taken 8587 times.
✓ Branch 1 taken 297 times.
8884 for (x = 0; x < unaligned_w; x++)
170 8587 *dst++ = *src1++ - *src2++;
171 297 size -= unaligned_w;
172 297 c->llvidencdsp.diff_bytes(dst, src1, src2, size);
173 297 }
174
175 73804 static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type,
176 const uint8_t *src, const uint8_t *top, int size, int bpp)
177 {
178 int i;
179
180
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 73507 times.
✗ Branch 5 not taken.
73804 switch (filter_type) {
181 case PNG_FILTER_VALUE_NONE:
182 memcpy(dst, src, size);
183 break;
184 297 case PNG_FILTER_VALUE_SUB:
185 297 sub_left_prediction(c, dst, src, bpp, size);
186 297 break;
187 case PNG_FILTER_VALUE_UP:
188 c->llvidencdsp.diff_bytes(dst, src, top, size);
189 break;
190 case PNG_FILTER_VALUE_AVG:
191 for (i = 0; i < bpp; i++)
192 dst[i] = src[i] - (top[i] >> 1);
193 for (; i < size; i++)
194 dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
195 break;
196 73507 case PNG_FILTER_VALUE_PAETH:
197
2/2
✓ Branch 0 taken 227983 times.
✓ Branch 1 taken 73507 times.
301490 for (i = 0; i < bpp; i++)
198 227983 dst[i] = src[i] - top[i];
199 73507 sub_png_paeth_prediction(dst + i, src + i, top + i, size - i, bpp);
200 73507 break;
201 default:
202 av_unreachable("PNG_FILTER_VALUE_MIXED can't happen here and all others are covered");
203 }
204 73804 }
205
206 73804 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
207 const uint8_t *src, const uint8_t *top, int size, int bpp)
208 {
209 73804 int pred = s->filter_type;
210
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 73804 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
73804 av_assert0(bpp || !pred);
211
3/4
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 73507 times.
✓ Branch 2 taken 297 times.
✗ Branch 3 not taken.
73804 if (!top && pred)
212 297 pred = PNG_FILTER_VALUE_SUB;
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73804 times.
73804 if (pred == PNG_FILTER_VALUE_MIXED) {
214 int i;
215 int cost, bcost = INT_MAX;
216 uint8_t *buf1 = dst, *buf2 = dst + size + 16;
217 for (pred = 0; pred < 5; pred++) {
218 png_filter_row(s, buf1 + 1, pred, src, top, size, bpp);
219 buf1[0] = pred;
220 cost = 0;
221 for (i = 0; i <= size; i++)
222 cost += abs((int8_t) buf1[i]);
223 if (cost < bcost) {
224 bcost = cost;
225 FFSWAP(uint8_t *, buf1, buf2);
226 }
227 }
228 return buf2;
229 } else {
230 73804 png_filter_row(s, dst + 1, pred, src, top, size, bpp);
231 73804 dst[0] = pred;
232 73804 return dst;
233 }
234 }
235
236 9741 static void png_write_chunk(uint8_t **f, uint32_t tag,
237 const uint8_t *buf, int length)
238 {
239 9741 const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
240 9741 uint32_t crc = ~0U;
241 uint8_t tagbuf[4];
242
243 9741 bytestream_put_be32(f, length);
244 9741 AV_WL32(tagbuf, tag);
245 9741 crc = av_crc(crc_table, crc, tagbuf, 4);
246 9741 bytestream_put_be32(f, av_bswap32(tag));
247
2/2
✓ Branch 0 taken 9493 times.
✓ Branch 1 taken 248 times.
9741 if (length > 0) {
248 9493 crc = av_crc(crc_table, crc, buf, length);
249
2/2
✓ Branch 0 taken 9492 times.
✓ Branch 1 taken 1 times.
9493 if (*f != buf)
250 9492 memcpy(*f, buf, length);
251 9493 *f += length;
252 }
253 9741 bytestream_put_be32(f, ~crc);
254 9741 }
255
256 10774 static void png_write_image_data(AVCodecContext *avctx,
257 const uint8_t *buf, int length)
258 {
259 10774 PNGEncContext *s = avctx->priv_data;
260 10774 const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
261 10774 uint32_t crc = ~0U;
262
263
4/4
✓ Branch 0 taken 1895 times.
✓ Branch 1 taken 8879 times.
✓ Branch 2 taken 78 times.
✓ Branch 3 taken 1817 times.
10774 if (avctx->codec_id == AV_CODEC_ID_PNG || avctx->frame_num == 0) {
264 8957 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), buf, length);
265 8957 return;
266 }
267
268 1817 bytestream_put_be32(&s->bytestream, length + 4);
269
270 1817 bytestream_put_be32(&s->bytestream, MKBETAG('f', 'd', 'A', 'T'));
271 1817 bytestream_put_be32(&s->bytestream, s->sequence_number);
272 1817 crc = av_crc(crc_table, crc, s->bytestream - 8, 8);
273
274 1817 crc = av_crc(crc_table, crc, buf, length);
275 1817 memcpy(s->bytestream, buf, length);
276 1817 s->bytestream += length;
277
278 1817 bytestream_put_be32(&s->bytestream, ~crc);
279
280 1817 ++s->sequence_number;
281 }
282
283 /* XXX: do filtering */
284 73804 static int png_write_row(AVCodecContext *avctx, const uint8_t *data, int size)
285 {
286 73804 PNGEncContext *s = avctx->priv_data;
287 73804 z_stream *const zstream = &s->zstream.zstream;
288 int ret;
289
290 73804 zstream->avail_in = size;
291 73804 zstream->next_in = data;
292
2/2
✓ Branch 0 taken 81587 times.
✓ Branch 1 taken 73804 times.
229195 while (zstream->avail_in > 0) {
293 81587 ret = deflate(zstream, Z_NO_FLUSH);
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81587 times.
81587 if (ret != Z_OK)
295 return -1;
296
2/2
✓ Branch 0 taken 71838 times.
✓ Branch 1 taken 9749 times.
81587 if (zstream->avail_out == 0) {
297
1/2
✓ Branch 0 taken 9749 times.
✗ Branch 1 not taken.
9749 if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
298 9749 png_write_image_data(avctx, s->buf, IOBUF_SIZE);
299 9749 zstream->avail_out = IOBUF_SIZE;
300 9749 zstream->next_out = s->buf;
301 }
302 }
303 73804 return 0;
304 }
305
306 #define PNG_LRINT(d, divisor) lrint((d) * (divisor))
307 #define PNG_Q2D(q, divisor) PNG_LRINT(av_q2d(q), (divisor))
308 #define AV_WB32_PNG_D(buf, q) AV_WB32(buf, PNG_Q2D(q, 100000))
309 250 static int png_get_chrm(enum AVColorPrimaries prim, uint8_t *buf)
310 {
311 250 const AVColorPrimariesDesc *desc = av_csp_primaries_desc_from_id(prim);
312
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 2 times.
250 if (!desc)
313 248 return 0;
314
315 2 AV_WB32_PNG_D(buf, desc->wp.x);
316 2 AV_WB32_PNG_D(buf + 4, desc->wp.y);
317 2 AV_WB32_PNG_D(buf + 8, desc->prim.r.x);
318 2 AV_WB32_PNG_D(buf + 12, desc->prim.r.y);
319 2 AV_WB32_PNG_D(buf + 16, desc->prim.g.x);
320 2 AV_WB32_PNG_D(buf + 20, desc->prim.g.y);
321 2 AV_WB32_PNG_D(buf + 24, desc->prim.b.x);
322 2 AV_WB32_PNG_D(buf + 28, desc->prim.b.y);
323
324 2 return 1;
325 }
326
327 250 static int png_get_gama(enum AVColorTransferCharacteristic trc, uint8_t *buf)
328 {
329 250 double gamma = av_csp_approximate_eotf_gamma(trc);
330
1/2
✓ Branch 0 taken 250 times.
✗ Branch 1 not taken.
250 if (gamma <= 1e-6)
331 250 return 0;
332
333 AV_WB32(buf, PNG_LRINT(1.0 / gamma, 100000));
334 return 1;
335 }
336
337 250 static int png_write_iccp(PNGEncContext *s, const AVFrameSideData *sd)
338 {
339 250 z_stream *const zstream = &s->zstream.zstream;
340 const AVDictionaryEntry *entry;
341 const char *name;
342 uint8_t *start, *buf;
343 int ret;
344
345
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
250 if (!sd || !sd->size)
346 249 return 0;
347 1 zstream->next_in = sd->data;
348 1 zstream->avail_in = sd->size;
349
350 /* write the chunk contents first */
351 1 start = s->bytestream + 8; /* make room for iCCP tag + length */
352 1 buf = start;
353
354 /* profile description */
355 1 entry = av_dict_get(sd->metadata, "name", NULL, 0);
356
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 name = (entry && entry->value[0]) ? entry->value : "icc";
357 22 for (int i = 0;; i++) {
358
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 char c = (i == 79) ? 0 : name[i];
359 22 bytestream_put_byte(&buf, c);
360
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
22 if (!c)
361 1 break;
362 }
363
364 /* compression method and profile data */
365 1 bytestream_put_byte(&buf, 0);
366 1 zstream->next_out = buf;
367 1 zstream->avail_out = s->bytestream_end - buf;
368 1 ret = deflate(zstream, Z_FINISH);
369 1 deflateReset(zstream);
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret != Z_STREAM_END)
371 return AVERROR_EXTERNAL;
372
373 /* rewind to the start and write the chunk header/crc */
374 1 png_write_chunk(&s->bytestream, MKTAG('i', 'C', 'C', 'P'), start,
375 1 zstream->next_out - start);
376 1 return 0;
377 }
378
379 250 static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
380 {
381 AVFrameSideData *side_data;
382 250 PNGEncContext *s = avctx->priv_data;
383 int ret;
384
385 /* write png header */
386 250 AV_WB32(s->buf, avctx->width);
387 250 AV_WB32(s->buf + 4, avctx->height);
388 250 s->buf[8] = s->bit_depth;
389 250 s->buf[9] = s->color_type;
390 250 s->buf[10] = 0; /* compression type */
391 250 s->buf[11] = 0; /* filter type */
392 250 s->buf[12] = s->is_progressive; /* interlace type */
393 250 png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
394
395 /* write physical information */
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (s->dpm) {
397 AV_WB32(s->buf, s->dpm);
398 AV_WB32(s->buf + 4, s->dpm);
399 s->buf[8] = 1; /* unit specifier is meter */
400 } else {
401 250 AV_WB32(s->buf, avctx->sample_aspect_ratio.num);
402 250 AV_WB32(s->buf + 4, avctx->sample_aspect_ratio.den);
403 250 s->buf[8] = 0; /* unit specifier is unknown */
404 }
405 250 png_write_chunk(&s->bytestream, MKTAG('p', 'H', 'Y', 's'), s->buf, 9);
406
407 /* write stereoscopic information */
408 250 side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_STEREO3D);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (side_data) {
410 AVStereo3D *stereo3d = (AVStereo3D *)side_data->data;
411 switch (stereo3d->type) {
412 case AV_STEREO3D_SIDEBYSIDE:
413 s->buf[0] = ((stereo3d->flags & AV_STEREO3D_FLAG_INVERT) == 0) ? 1 : 0;
414 png_write_chunk(&s->bytestream, MKTAG('s', 'T', 'E', 'R'), s->buf, 1);
415 break;
416 case AV_STEREO3D_2D:
417 break;
418 default:
419 av_log(avctx, AV_LOG_WARNING, "Only side-by-side stereo3d flag can be defined within sTER chunk\n");
420 break;
421 }
422 }
423
424
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 246 times.
250 if (s->exif_data) {
425 /* we checked for overflow when we attached the buffer to s->exif_data */
426 4 png_write_chunk(&s->bytestream, MKTAG('e','X','I','f'), s->exif_data->data, s->exif_data->size);
427 4 av_buffer_unref(&s->exif_data);
428 }
429
430 250 side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE);
431
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250 times.
250 if ((ret = png_write_iccp(s, side_data)))
432 return ret;
433
434 /* write colorspace information */
435
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 249 times.
250 if (pict->color_primaries == AVCOL_PRI_BT709 &&
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 pict->color_trc == AVCOL_TRC_IEC61966_2_1) {
437 s->buf[0] = 1; /* rendering intent, relative colorimetric by default */
438 png_write_chunk(&s->bytestream, MKTAG('s', 'R', 'G', 'B'), s->buf, 1);
439
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 249 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
250 } else if (pict->color_trc != AVCOL_TRC_UNSPECIFIED && !side_data) {
440 /*
441 * Avoid writing cICP if the transfer is unknown. Known primaries
442 * with unknown transfer can be handled by cHRM.
443 *
444 * We also avoid writing cICP if an ICC Profile is present, because
445 * the standard requires that cICP overrides iCCP.
446 *
447 * These values match H.273 so no translation is needed.
448 */
449 1 s->buf[0] = pict->color_primaries;
450 1 s->buf[1] = pict->color_trc;
451 1 s->buf[2] = 0; /* colorspace = RGB */
452 1 s->buf[3] = pict->color_range == AVCOL_RANGE_MPEG ? 0 : 1;
453 1 png_write_chunk(&s->bytestream, MKTAG('c', 'I', 'C', 'P'), s->buf, 4);
454 }
455
456 250 side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
457
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 249 times.
250 if (side_data) {
458 1 AVContentLightMetadata *clli = (AVContentLightMetadata *) side_data->data;
459 1 AV_WB32(s->buf, clli->MaxCLL * 10000);
460 1 AV_WB32(s->buf + 4, clli->MaxFALL * 10000);
461 1 png_write_chunk(&s->bytestream, MKTAG('c', 'L', 'L', 'I'), s->buf, 8);
462 }
463
464 250 side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
465
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 249 times.
250 if (side_data) {
466 1 AVMasteringDisplayMetadata *mdcv = (AVMasteringDisplayMetadata *) side_data->data;
467
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (mdcv->has_luminance && mdcv->has_primaries) {
468
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = 0; i < 3; i++) {
469 3 AV_WB16(s->buf + 2*i, PNG_Q2D(mdcv->display_primaries[i][0], 50000));
470 3 AV_WB16(s->buf + 2*i + 2, PNG_Q2D(mdcv->display_primaries[i][1], 50000));
471 }
472 1 AV_WB16(s->buf + 12, PNG_Q2D(mdcv->white_point[0], 50000));
473 1 AV_WB16(s->buf + 14, PNG_Q2D(mdcv->white_point[1], 50000));
474 1 AV_WB32(s->buf + 16, PNG_Q2D(mdcv->max_luminance, 10000));
475 1 AV_WB32(s->buf + 20, PNG_Q2D(mdcv->min_luminance, 10000));
476 1 png_write_chunk(&s->bytestream, MKTAG('m', 'D', 'C', 'V'), s->buf, 24);
477 }
478 }
479
480
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 248 times.
250 if (png_get_chrm(pict->color_primaries, s->buf))
481 2 png_write_chunk(&s->bytestream, MKTAG('c', 'H', 'R', 'M'), s->buf, 32);
482
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250 times.
250 if (png_get_gama(pict->color_trc, s->buf))
483 png_write_chunk(&s->bytestream, MKTAG('g', 'A', 'M', 'A'), s->buf, 4);
484
485
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
250 if (avctx->bits_per_raw_sample > 0 &&
486 avctx->bits_per_raw_sample < (s->color_type & PNG_COLOR_MASK_PALETTE ? 8 : s->bit_depth)) {
487 int len = s->color_type & PNG_COLOR_MASK_PALETTE ? 3 : ff_png_get_nb_channels(s->color_type);
488 memset(s->buf, avctx->bits_per_raw_sample, len);
489 png_write_chunk(&s->bytestream, MKTAG('s', 'B', 'I', 'T'), s->buf, len);
490 }
491
492 /* put the palette if needed, must be after colorspace information */
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
494 int has_alpha, alpha, i;
495 unsigned int v;
496 uint32_t *palette;
497 uint8_t *ptr, *alpha_ptr;
498
499 palette = (uint32_t *)pict->data[1];
500 ptr = s->buf;
501 alpha_ptr = s->buf + 256 * 3;
502 has_alpha = 0;
503 for (i = 0; i < 256; i++) {
504 v = palette[i];
505 alpha = v >> 24;
506 if (alpha != 0xff)
507 has_alpha = 1;
508 *alpha_ptr++ = alpha;
509 bytestream_put_be24(&ptr, v);
510 }
511 png_write_chunk(&s->bytestream,
512 MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
513 if (has_alpha) {
514 png_write_chunk(&s->bytestream,
515 MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
516 }
517 }
518
519 250 return 0;
520 }
521
522 297 static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
523 {
524 297 PNGEncContext *s = avctx->priv_data;
525 297 z_stream *const zstream = &s->zstream.zstream;
526 297 const AVFrame *const p = pict;
527 int y, len, ret;
528 int row_size, pass_row_size;
529 uint8_t *crow_buf, *crow;
530 297 uint8_t *crow_base = NULL;
531 297 uint8_t *progressive_buf = NULL;
532 297 uint8_t *top_buf = NULL;
533
534 297 row_size = (pict->width * s->bits_per_pixel + 7) >> 3;
535
536 297 crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (!crow_base) {
538 ret = AVERROR(ENOMEM);
539 goto the_end;
540 }
541 // pixel data should be aligned, but there's a control byte before it
542 297 crow_buf = crow_base + 15;
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (s->is_progressive) {
544 progressive_buf = av_malloc(row_size + 1);
545 top_buf = av_malloc(row_size + 1);
546 if (!progressive_buf || !top_buf) {
547 ret = AVERROR(ENOMEM);
548 goto the_end;
549 }
550 }
551
552 /* put each row */
553 297 zstream->avail_out = IOBUF_SIZE;
554 297 zstream->next_out = s->buf;
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (s->is_progressive) {
556 int pass;
557
558 for (pass = 0; pass < NB_PASSES; pass++) {
559 /* NOTE: a pass is completely omitted if no pixels would be
560 * output */
561 pass_row_size = ff_png_pass_row_size(pass, s->bits_per_pixel, pict->width);
562 if (pass_row_size > 0) {
563 uint8_t *top = NULL;
564 for (y = 0; y < pict->height; y++)
565 if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
566 const uint8_t *ptr = p->data[0] + y * p->linesize[0];
567 FFSWAP(uint8_t *, progressive_buf, top_buf);
568 png_get_interlaced_row(progressive_buf, pass_row_size,
569 s->bits_per_pixel, pass,
570 ptr, pict->width);
571 crow = png_choose_filter(s, crow_buf, progressive_buf,
572 top, pass_row_size, s->bits_per_pixel >> 3);
573 png_write_row(avctx, crow, pass_row_size + 1);
574 top = progressive_buf;
575 }
576 }
577 }
578 } else {
579 297 const uint8_t *top = NULL;
580
2/2
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 73804 times.
74101 for (y = 0; y < pict->height; y++) {
581 73804 const uint8_t *ptr = p->data[0] + y * p->linesize[0];
582 73804 crow = png_choose_filter(s, crow_buf, ptr, top,
583 73804 row_size, s->bits_per_pixel >> 3);
584 73804 png_write_row(avctx, crow, row_size + 1);
585 73804 top = ptr;
586 }
587 }
588 /* compress last bytes */
589 for (;;) {
590 1025 ret = deflate(zstream, Z_FINISH);
591
3/4
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 728 times.
✓ Branch 2 taken 297 times.
✗ Branch 3 not taken.
1025 if (ret == Z_OK || ret == Z_STREAM_END) {
592 1025 len = IOBUF_SIZE - zstream->avail_out;
593
2/4
✓ Branch 0 taken 1025 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1025 times.
✗ Branch 3 not taken.
1025 if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
594 1025 png_write_image_data(avctx, s->buf, len);
595 }
596 1025 zstream->avail_out = IOBUF_SIZE;
597 1025 zstream->next_out = s->buf;
598
2/2
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 728 times.
1025 if (ret == Z_STREAM_END)
599 297 break;
600 } else {
601 ret = -1;
602 goto the_end;
603 }
604 }
605
606 297 ret = 0;
607
608 297 the_end:
609 297 av_freep(&crow_base);
610 297 av_freep(&progressive_buf);
611 297 av_freep(&top_buf);
612 297 deflateReset(zstream);
613 297 return ret;
614 }
615
616 250 static int add_icc_profile_size(AVCodecContext *avctx, const AVFrame *pict,
617 uint64_t *max_packet_size)
618 {
619 250 PNGEncContext *s = avctx->priv_data;
620 const AVFrameSideData *sd;
621 250 const int hdr_size = 128;
622 uint64_t new_pkt_size;
623 uLong bound;
624
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (!pict)
626 return 0;
627 250 sd = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE);
628
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
250 if (!sd || !sd->size)
629 249 return 0;
630 if (sd->size != (uLong) sd->size)
631 return AVERROR_INVALIDDATA;
632
633 1 bound = deflateBound(&s->zstream.zstream, sd->size);
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (bound > INT32_MAX - hdr_size)
635 return AVERROR_INVALIDDATA;
636
637 1 new_pkt_size = *max_packet_size + bound + hdr_size;
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (new_pkt_size < *max_packet_size)
639 return AVERROR_INVALIDDATA;
640 1 *max_packet_size = new_pkt_size;
641 1 return 0;
642 }
643
644 250 static int add_exif_profile_size(AVCodecContext *avctx, const AVFrame *pict,
645 uint64_t *max_packet_size)
646 {
647 uint64_t new_pkt_size;
648 250 PNGEncContext *s = avctx->priv_data;
649
650 250 int result = ff_exif_get_buffer(avctx, pict, &s->exif_data, AV_EXIF_TIFF_HEADER);
651
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 4 times.
250 if (!s->exif_data) {
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (result < 0)
653 av_log(avctx, AV_LOG_WARNING, "unable to attach EXIF metadata: %s\n", av_err2str(result));
654 246 return 0;
655 }
656
657 /* png_write_chunk accepts an int, not a size_t, so we have to check overflow */
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->exif_data->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
659 /* that's a very big exif chunk, probably a bug */
660 av_log(avctx, AV_LOG_ERROR, "extremely large EXIF buffer detected, not writing\n");
661 av_buffer_unref(&s->exif_data);
662 return 0;
663 }
664
665 /* 12 is the png chunk header size */
666 4 new_pkt_size = *max_packet_size + s->exif_data->size + 12;
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (new_pkt_size < *max_packet_size) {
668 av_buffer_unref(&s->exif_data);
669 return AVERROR_INVALIDDATA;
670 }
671
672 4 *max_packet_size = new_pkt_size;
673
674 4 return 0;
675 }
676
677 248 static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
678 const AVFrame *pict, int *got_packet)
679 {
680 248 PNGEncContext *s = avctx->priv_data;
681 int ret;
682 int enc_row_size;
683 uint64_t max_packet_size;
684
685 496 enc_row_size = deflateBound(&s->zstream.zstream,
686 248 (avctx->width * s->bits_per_pixel + 7) >> 3);
687 248 max_packet_size =
688 248 FF_INPUT_BUFFER_MIN_SIZE + // headers
689 248 avctx->height * (
690 248 enc_row_size +
691 248 12 * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // IDAT * ceil(enc_row_size / IOBUF_SIZE)
692 );
693
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 248 times.
248 if ((ret = add_icc_profile_size(avctx, pict, &max_packet_size)))
694 return ret;
695 248 ret = add_exif_profile_size(avctx, pict, &max_packet_size);
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (ret < 0)
697 return ret;
698
699 248 ret = ff_alloc_packet(avctx, pkt, max_packet_size);
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (ret < 0)
701 return ret;
702
703 248 s->bytestream_start =
704 248 s->bytestream = pkt->data;
705 248 s->bytestream_end = pkt->data + pkt->size;
706
707 248 AV_WB64(s->bytestream, PNGSIG);
708 248 s->bytestream += 8;
709
710 248 ret = encode_headers(avctx, pict);
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (ret < 0)
712 return ret;
713
714 248 ret = encode_frame(avctx, pict);
715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (ret < 0)
716 return ret;
717
718 248 png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
719
720 248 pkt->size = s->bytestream - s->bytestream_start;
721 248 pkt->flags |= AV_PKT_FLAG_KEY;
722 248 *got_packet = 1;
723
724 248 return 0;
725 }
726
727 94 static int apng_do_inverse_blend(AVFrame *output, const AVFrame *input,
728 APNGFctlChunk *fctl_chunk, uint8_t bpp)
729 {
730 // output: background, input: foreground
731 // output the image such that when blended with the background, will produce the foreground
732
733 unsigned int x, y;
734 94 unsigned int leftmost_x = input->width;
735 94 unsigned int rightmost_x = 0;
736 94 unsigned int topmost_y = input->height;
737 94 unsigned int bottommost_y = 0;
738 94 const uint8_t *input_data = input->data[0];
739 94 uint8_t *output_data = output->data[0];
740 94 ptrdiff_t input_linesize = input->linesize[0];
741 94 ptrdiff_t output_linesize = output->linesize[0];
742
743 // Find bounding box of changes
744
2/2
✓ Branch 0 taken 27072 times.
✓ Branch 1 taken 94 times.
27166 for (y = 0; y < input->height; ++y) {
745
2/2
✓ Branch 0 taken 9529344 times.
✓ Branch 1 taken 27072 times.
9556416 for (x = 0; x < input->width; ++x) {
746
2/2
✓ Branch 0 taken 2546 times.
✓ Branch 1 taken 9526798 times.
9529344 if (!memcmp(input_data + bpp * x, output_data + bpp * x, bpp))
747 2546 continue;
748
749
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 9526704 times.
9526798 if (x < leftmost_x)
750 94 leftmost_x = x;
751
2/2
✓ Branch 0 taken 33088 times.
✓ Branch 1 taken 9493710 times.
9526798 if (x >= rightmost_x)
752 33088 rightmost_x = x + 1;
753
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 9526704 times.
9526798 if (y < topmost_y)
754 94 topmost_y = y;
755
2/2
✓ Branch 0 taken 27072 times.
✓ Branch 1 taken 9499726 times.
9526798 if (y >= bottommost_y)
756 27072 bottommost_y = y + 1;
757 }
758
759 27072 input_data += input_linesize;
760 27072 output_data += output_linesize;
761 }
762
763
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
94 if (leftmost_x == input->width && rightmost_x == 0) {
764 // Empty frame
765 // APNG does not support empty frames, so we make it a 1x1 frame
766 leftmost_x = topmost_y = 0;
767 rightmost_x = bottommost_y = 1;
768 }
769
770 // Do actual inverse blending
771
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 47 times.
94 if (fctl_chunk->blend_op == APNG_BLEND_OP_SOURCE) {
772 47 output_data = output->data[0];
773
2/2
✓ Branch 0 taken 13536 times.
✓ Branch 1 taken 47 times.
13583 for (y = topmost_y; y < bottommost_y; ++y) {
774 13536 memcpy(output_data,
775 13536 input->data[0] + input_linesize * y + bpp * leftmost_x,
776 13536 bpp * (rightmost_x - leftmost_x));
777 13536 output_data += output_linesize;
778 }
779 } else { // APNG_BLEND_OP_OVER
780 size_t transparent_palette_index;
781 uint32_t *palette;
782
783
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 switch (input->format) {
784 case AV_PIX_FMT_RGBA64BE:
785 case AV_PIX_FMT_YA16BE:
786 case AV_PIX_FMT_RGBA:
787 case AV_PIX_FMT_GRAY8A:
788 break;
789
790 case AV_PIX_FMT_PAL8:
791 palette = (uint32_t*)input->data[1];
792 for (transparent_palette_index = 0; transparent_palette_index < 256; ++transparent_palette_index)
793 if (palette[transparent_palette_index] >> 24 == 0)
794 break;
795 break;
796
797 47 default:
798 // No alpha, so blending not possible
799 47 return -1;
800 }
801
802 for (y = topmost_y; y < bottommost_y; ++y) {
803 const uint8_t *foreground = input->data[0] + input_linesize * y + bpp * leftmost_x;
804 uint8_t *background = output->data[0] + output_linesize * y + bpp * leftmost_x;
805 output_data = output->data[0] + output_linesize * (y - topmost_y);
806 for (x = leftmost_x; x < rightmost_x; ++x, foreground += bpp, background += bpp, output_data += bpp) {
807 if (!memcmp(foreground, background, bpp)) {
808 if (input->format == AV_PIX_FMT_PAL8) {
809 if (transparent_palette_index == 256) {
810 // Need fully transparent colour, but none exists
811 return -1;
812 }
813
814 *output_data = transparent_palette_index;
815 } else {
816 memset(output_data, 0, bpp);
817 }
818 continue;
819 }
820
821 // Check for special alpha values, since full inverse
822 // alpha-on-alpha blending is rarely possible, and when
823 // possible, doesn't compress much better than
824 // APNG_BLEND_OP_SOURCE blending
825 switch (input->format) {
826 case AV_PIX_FMT_RGBA64BE:
827 if (((uint16_t*)foreground)[3] == 0xffff ||
828 ((uint16_t*)background)[3] == 0)
829 break;
830 return -1;
831
832 case AV_PIX_FMT_YA16BE:
833 if (((uint16_t*)foreground)[1] == 0xffff ||
834 ((uint16_t*)background)[1] == 0)
835 break;
836 return -1;
837
838 case AV_PIX_FMT_RGBA:
839 if (foreground[3] == 0xff || background[3] == 0)
840 break;
841 return -1;
842
843 case AV_PIX_FMT_GRAY8A:
844 if (foreground[1] == 0xff || background[1] == 0)
845 break;
846 return -1;
847
848 case AV_PIX_FMT_PAL8:
849 if (palette[*foreground] >> 24 == 0xff ||
850 palette[*background] >> 24 == 0)
851 break;
852 return -1;
853
854 default:
855 av_unreachable("Pixfmt has been checked before");
856 }
857
858 memmove(output_data, foreground, bpp);
859 }
860 }
861 }
862
863 47 output->width = rightmost_x - leftmost_x;
864 47 output->height = bottommost_y - topmost_y;
865 47 fctl_chunk->width = output->width;
866 47 fctl_chunk->height = output->height;
867 47 fctl_chunk->x_offset = leftmost_x;
868 47 fctl_chunk->y_offset = topmost_y;
869
870 47 return 0;
871 }
872
873 26 static int apng_encode_frame(AVCodecContext *avctx, const AVFrame *pict,
874 APNGFctlChunk *best_fctl_chunk, APNGFctlChunk *best_last_fctl_chunk)
875 {
876 26 PNGEncContext *s = avctx->priv_data;
877 int ret;
878 unsigned int y;
879 AVFrame* diffFrame;
880 26 uint8_t bpp = (s->bits_per_pixel + 7) >> 3;
881 uint8_t *original_bytestream, *original_bytestream_end;
882 26 uint8_t *temp_bytestream = 0, *temp_bytestream_end;
883 uint32_t best_sequence_number;
884 uint8_t *best_bytestream;
885 26 size_t best_bytestream_size = SIZE_MAX;
886 26 APNGFctlChunk last_fctl_chunk = *best_last_fctl_chunk;
887 26 APNGFctlChunk fctl_chunk = *best_fctl_chunk;
888 26 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pict->format);
889
890
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
26 if (avctx->frame_num == 0) {
891 2 best_fctl_chunk->width = pict->width;
892 2 best_fctl_chunk->height = pict->height;
893 2 best_fctl_chunk->x_offset = 0;
894 2 best_fctl_chunk->y_offset = 0;
895 2 best_fctl_chunk->blend_op = APNG_BLEND_OP_SOURCE;
896 2 return encode_frame(avctx, pict);
897 }
898
899 24 diffFrame = av_frame_alloc();
900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!diffFrame)
901 return AVERROR(ENOMEM);
902
903 24 diffFrame->format = pict->format;
904 24 diffFrame->width = pict->width;
905 24 diffFrame->height = pict->height;
906
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if ((ret = av_frame_get_buffer(diffFrame, 0)) < 0)
907 goto fail;
908
909 24 original_bytestream = s->bytestream;
910 24 original_bytestream_end = s->bytestream_end;
911
912 24 temp_bytestream = av_malloc(original_bytestream_end - original_bytestream);
913
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!temp_bytestream) {
914 ret = AVERROR(ENOMEM);
915 goto fail;
916 }
917 24 temp_bytestream_end = temp_bytestream + (original_bytestream_end - original_bytestream);
918
919
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 24 times.
96 for (last_fctl_chunk.dispose_op = 0; last_fctl_chunk.dispose_op < 3; ++last_fctl_chunk.dispose_op) {
920 // 0: APNG_DISPOSE_OP_NONE
921 // 1: APNG_DISPOSE_OP_BACKGROUND
922 // 2: APNG_DISPOSE_OP_PREVIOUS
923
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 48 times.
72 if (last_fctl_chunk.dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
924
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (!(desc->flags & AV_PIX_FMT_FLAG_ALPHA))
925 24 continue;
926 }
927
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
144 for (fctl_chunk.blend_op = 0; fctl_chunk.blend_op < 2; ++fctl_chunk.blend_op) {
928 // 0: APNG_BLEND_OP_SOURCE
929 // 1: APNG_BLEND_OP_OVER
930
931 96 uint32_t original_sequence_number = s->sequence_number, sequence_number;
932 96 uint8_t *bytestream_start = s->bytestream;
933 size_t bytestream_size;
934
935 // Do disposal
936
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
96 if (last_fctl_chunk.dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
937 48 diffFrame->width = pict->width;
938 48 diffFrame->height = pict->height;
939 48 ret = av_frame_copy(diffFrame, s->last_frame);
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (ret < 0)
941 goto fail;
942
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (last_fctl_chunk.dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
944 for (y = last_fctl_chunk.y_offset; y < last_fctl_chunk.y_offset + last_fctl_chunk.height; ++y) {
945 size_t row_start = diffFrame->linesize[0] * y + bpp * last_fctl_chunk.x_offset;
946 memset(diffFrame->data[0] + row_start, 0, bpp * last_fctl_chunk.width);
947 }
948 }
949 } else {
950
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 46 times.
48 if (!s->prev_frame)
951 2 continue;
952
953 46 diffFrame->width = pict->width;
954 46 diffFrame->height = pict->height;
955 46 ret = av_frame_copy(diffFrame, s->prev_frame);
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret < 0)
957 goto fail;
958 }
959
960 // Do inverse blending
961
2/2
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 47 times.
94 if (apng_do_inverse_blend(diffFrame, pict, &fctl_chunk, bpp) < 0)
962 47 continue;
963
964 // Do encoding
965 47 ret = encode_frame(avctx, diffFrame);
966 47 sequence_number = s->sequence_number;
967 47 s->sequence_number = original_sequence_number;
968 47 bytestream_size = s->bytestream - bytestream_start;
969 47 s->bytestream = bytestream_start;
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (ret < 0)
971 goto fail;
972
973
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 23 times.
47 if (bytestream_size < best_bytestream_size) {
974 24 *best_fctl_chunk = fctl_chunk;
975 24 *best_last_fctl_chunk = last_fctl_chunk;
976
977 24 best_sequence_number = sequence_number;
978 24 best_bytestream = s->bytestream;
979 24 best_bytestream_size = bytestream_size;
980
981
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (best_bytestream == original_bytestream) {
982 24 s->bytestream = temp_bytestream;
983 24 s->bytestream_end = temp_bytestream_end;
984 } else {
985 s->bytestream = original_bytestream;
986 s->bytestream_end = original_bytestream_end;
987 }
988 }
989 }
990 }
991
992 24 s->sequence_number = best_sequence_number;
993 24 s->bytestream = original_bytestream + best_bytestream_size;
994 24 s->bytestream_end = original_bytestream_end;
995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (best_bytestream != original_bytestream)
996 memcpy(original_bytestream, best_bytestream, best_bytestream_size);
997
998 24 ret = 0;
999
1000 24 fail:
1001 24 av_freep(&temp_bytestream);
1002 24 av_frame_free(&diffFrame);
1003 24 return ret;
1004 }
1005
1006 30 static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
1007 const AVFrame *pict, int *got_packet)
1008 {
1009 30 PNGEncContext *s = avctx->priv_data;
1010 int ret;
1011 int enc_row_size;
1012 uint64_t max_packet_size;
1013 30 APNGFctlChunk fctl_chunk = {0};
1014
1015
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
30 if (pict && s->color_type == PNG_COLOR_TYPE_PALETTE) {
1016 uint32_t checksum = ~av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), ~0U, pict->data[1], 256 * sizeof(uint32_t));
1017
1018 if (avctx->frame_num == 0) {
1019 s->palette_checksum = checksum;
1020 } else if (checksum != s->palette_checksum) {
1021 av_log(avctx, AV_LOG_ERROR,
1022 "Input contains more than one unique palette. APNG does not support multiple palettes.\n");
1023 return -1;
1024 }
1025 }
1026
1027 60 enc_row_size = deflateBound(&s->zstream.zstream,
1028 30 (avctx->width * s->bits_per_pixel + 7) >> 3);
1029 30 max_packet_size =
1030 30 FF_INPUT_BUFFER_MIN_SIZE + // headers
1031 30 avctx->height * (
1032 30 enc_row_size +
1033 30 (4 + 12) * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // fdAT * ceil(enc_row_size / IOBUF_SIZE)
1034 );
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (max_packet_size > INT_MAX)
1036 return AVERROR(ENOMEM);
1037
1038
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
30 if (avctx->frame_num == 0) {
1039
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!pict)
1040 return AVERROR(EINVAL);
1041 2 uint64_t extradata_size = FF_INPUT_BUFFER_MIN_SIZE;
1042 2 ret = add_icc_profile_size(avctx, pict, &extradata_size);
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1044 return ret;
1045 2 ret = add_exif_profile_size(avctx, pict, &extradata_size);
1046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1047 return ret;
1048 /* the compiler will optimize this out if UINT64_MAX == SIZE_MAX */
1049 if (extradata_size > SIZE_MAX)
1050 return AVERROR(ENOMEM);
1051 2 s->bytestream = s->extra_data = av_malloc(extradata_size);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->extra_data)
1053 return AVERROR(ENOMEM);
1054
1055 2 ret = encode_headers(avctx, pict);
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1057 return ret;
1058
1059 2 s->extra_data_size = s->bytestream - s->extra_data;
1060
1061 2 s->last_frame_packet = av_malloc(max_packet_size);
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->last_frame_packet)
1063 return AVERROR(ENOMEM);
1064
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 2 times.
28 } else if (s->last_frame) {
1065 26 ret = ff_get_encode_buffer(avctx, pkt, s->last_frame_packet_size, 0);
1066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
1067 return ret;
1068
1069 26 memcpy(pkt->data, s->last_frame_packet, s->last_frame_packet_size);
1070 26 pkt->pts = s->last_frame->pts;
1071 26 pkt->duration = s->last_frame->duration;
1072
1073 26 ret = ff_encode_reordered_opaque(avctx, pkt, s->last_frame);
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
1075 return ret;
1076 }
1077
1078
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
30 if (pict) {
1079 26 s->bytestream_start =
1080 26 s->bytestream = s->last_frame_packet;
1081 26 s->bytestream_end = s->bytestream + max_packet_size;
1082
1083 // We're encoding the frame first, so we have to do a bit of shuffling around
1084 // to have the image data write to the correct place in the buffer
1085 26 fctl_chunk.sequence_number = s->sequence_number;
1086 26 ++s->sequence_number;
1087 26 s->bytestream += APNG_FCTL_CHUNK_SIZE + 12;
1088
1089 26 ret = apng_encode_frame(avctx, pict, &fctl_chunk, &s->last_frame_fctl);
1090
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
1091 return ret;
1092
1093 26 fctl_chunk.delay_num = 0; // delay filled in during muxing
1094 26 fctl_chunk.delay_den = 0;
1095 } else {
1096 4 s->last_frame_fctl.dispose_op = APNG_DISPOSE_OP_NONE;
1097 }
1098
1099
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
30 if (s->last_frame) {
1100 26 uint8_t* last_fctl_chunk_start = pkt->data;
1101 uint8_t buf[APNG_FCTL_CHUNK_SIZE];
1102
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
26 if (!s->extra_data_updated) {
1103 2 uint8_t *side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, s->extra_data_size);
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!side_data)
1105 return AVERROR(ENOMEM);
1106 2 memcpy(side_data, s->extra_data, s->extra_data_size);
1107 2 s->extra_data_updated = 1;
1108 }
1109
1110 26 AV_WB32(buf + 0, s->last_frame_fctl.sequence_number);
1111 26 AV_WB32(buf + 4, s->last_frame_fctl.width);
1112 26 AV_WB32(buf + 8, s->last_frame_fctl.height);
1113 26 AV_WB32(buf + 12, s->last_frame_fctl.x_offset);
1114 26 AV_WB32(buf + 16, s->last_frame_fctl.y_offset);
1115 26 AV_WB16(buf + 20, s->last_frame_fctl.delay_num);
1116 26 AV_WB16(buf + 22, s->last_frame_fctl.delay_den);
1117 26 buf[24] = s->last_frame_fctl.dispose_op;
1118 26 buf[25] = s->last_frame_fctl.blend_op;
1119 26 png_write_chunk(&last_fctl_chunk_start, MKTAG('f', 'c', 'T', 'L'), buf, sizeof(buf));
1120
1121 26 *got_packet = 1;
1122 }
1123
1124
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
30 if (pict) {
1125
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
26 if (!s->last_frame) {
1126 2 s->last_frame = av_frame_alloc();
1127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->last_frame)
1128 return AVERROR(ENOMEM);
1129
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 } else if (s->last_frame_fctl.dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
1130
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
24 if (!s->prev_frame) {
1131 1 s->prev_frame = av_frame_alloc();
1132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->prev_frame)
1133 return AVERROR(ENOMEM);
1134
1135 1 s->prev_frame->format = pict->format;
1136 1 s->prev_frame->width = pict->width;
1137 1 s->prev_frame->height = pict->height;
1138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = av_frame_get_buffer(s->prev_frame, 0)) < 0)
1139 return ret;
1140 }
1141
1142 // Do disposal, but not blending
1143 24 av_frame_copy(s->prev_frame, s->last_frame);
1144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->last_frame_fctl.dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
1145 uint32_t y;
1146 uint8_t bpp = (s->bits_per_pixel + 7) >> 3;
1147 for (y = s->last_frame_fctl.y_offset; y < s->last_frame_fctl.y_offset + s->last_frame_fctl.height; ++y) {
1148 size_t row_start = s->prev_frame->linesize[0] * y + bpp * s->last_frame_fctl.x_offset;
1149 memset(s->prev_frame->data[0] + row_start, 0, bpp * s->last_frame_fctl.width);
1150 }
1151 }
1152 }
1153
1154 26 ret = av_frame_replace(s->last_frame, pict);
1155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
1156 return ret;
1157
1158 26 s->last_frame_fctl = fctl_chunk;
1159 26 s->last_frame_packet_size = s->bytestream - s->bytestream_start;
1160 } else {
1161 4 av_frame_free(&s->last_frame);
1162 }
1163
1164 30 return 0;
1165 }
1166
1167 26 static av_cold int png_enc_init(AVCodecContext *avctx)
1168 {
1169 26 PNGEncContext *s = avctx->priv_data;
1170 int compression_level;
1171
1172
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
26 switch (avctx->pix_fmt) {
1173 case AV_PIX_FMT_RGBA:
1174 avctx->bits_per_coded_sample = 32;
1175 break;
1176 24 case AV_PIX_FMT_RGB24:
1177 24 avctx->bits_per_coded_sample = 24;
1178 24 break;
1179 case AV_PIX_FMT_GRAY8:
1180 avctx->bits_per_coded_sample = 0x28;
1181 break;
1182 case AV_PIX_FMT_MONOBLACK:
1183 avctx->bits_per_coded_sample = 1;
1184 break;
1185 case AV_PIX_FMT_PAL8:
1186 avctx->bits_per_coded_sample = 8;
1187 }
1188
1189 26 ff_llvidencdsp_init(&s->llvidencdsp);
1190
1191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
1192 s->filter_type = PNG_FILTER_VALUE_NONE;
1193
1194
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26 if (s->dpi && s->dpm) {
1195 av_log(avctx, AV_LOG_ERROR, "Only one of 'dpi' or 'dpm' options should be set\n");
1196 return AVERROR(EINVAL);
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 } else if (s->dpi) {
1198 s->dpm = s->dpi * 10000 / 254;
1199 }
1200
1201 26 s->is_progressive = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
1202
3/11
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
26 switch (avctx->pix_fmt) {
1203 case AV_PIX_FMT_RGBA64BE:
1204 s->bit_depth = 16;
1205 s->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1206 break;
1207 1 case AV_PIX_FMT_RGB48BE:
1208 1 s->bit_depth = 16;
1209 1 s->color_type = PNG_COLOR_TYPE_RGB;
1210 1 break;
1211 case AV_PIX_FMT_RGBA:
1212 s->bit_depth = 8;
1213 s->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1214 break;
1215 24 case AV_PIX_FMT_RGB24:
1216 24 s->bit_depth = 8;
1217 24 s->color_type = PNG_COLOR_TYPE_RGB;
1218 24 break;
1219 1 case AV_PIX_FMT_GRAY16BE:
1220 1 s->bit_depth = 16;
1221 1 s->color_type = PNG_COLOR_TYPE_GRAY;
1222 1 break;
1223 case AV_PIX_FMT_GRAY8:
1224 s->bit_depth = 8;
1225 s->color_type = PNG_COLOR_TYPE_GRAY;
1226 break;
1227 case AV_PIX_FMT_GRAY8A:
1228 s->bit_depth = 8;
1229 s->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
1230 break;
1231 case AV_PIX_FMT_YA16BE:
1232 s->bit_depth = 16;
1233 s->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
1234 break;
1235 case AV_PIX_FMT_MONOBLACK:
1236 s->bit_depth = 1;
1237 s->color_type = PNG_COLOR_TYPE_GRAY;
1238 break;
1239 case AV_PIX_FMT_PAL8:
1240 s->bit_depth = 8;
1241 s->color_type = PNG_COLOR_TYPE_PALETTE;
1242 break;
1243 default:
1244 av_unreachable("Already checked via CODEC_PIXFMTS");
1245 }
1246 26 s->bits_per_pixel = ff_png_get_nb_channels(s->color_type) * s->bit_depth;
1247
1248 52 compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
1249 ? Z_DEFAULT_COMPRESSION
1250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 : av_clip(avctx->compression_level, 0, 9);
1251 26 return ff_deflate_init(&s->zstream, compression_level, avctx);
1252 }
1253
1254 26 static av_cold int png_enc_close(AVCodecContext *avctx)
1255 {
1256 26 PNGEncContext *s = avctx->priv_data;
1257
1258 26 ff_deflate_end(&s->zstream);
1259 26 av_frame_free(&s->last_frame);
1260 26 av_frame_free(&s->prev_frame);
1261 26 av_buffer_unref(&s->exif_data);
1262 26 av_freep(&s->last_frame_packet);
1263 26 av_freep(&s->extra_data);
1264 26 s->extra_data_size = 0;
1265 26 return 0;
1266 }
1267
1268 #define OFFSET(x) offsetof(PNGEncContext, x)
1269 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1270 static const AVOption options[] = {
1271 {"dpi", "Set image resolution (in dots per inch)", OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE},
1272 {"dpm", "Set image resolution (in dots per meter)", OFFSET(dpm), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE},
1273 { "pred", "Prediction method", OFFSET(filter_type), AV_OPT_TYPE_INT, { .i64 = PNG_FILTER_VALUE_PAETH }, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED, VE, .unit = "pred" },
1274 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_NONE }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1275 { "sub", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_SUB }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1276 { "up", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_UP }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1277 { "avg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_AVG }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1278 { "paeth", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_PAETH }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1279 { "mixed", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_MIXED }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1280 { NULL},
1281 };
1282
1283 static const AVClass pngenc_class = {
1284 .class_name = "(A)PNG encoder",
1285 .item_name = av_default_item_name,
1286 .option = options,
1287 .version = LIBAVUTIL_VERSION_INT,
1288 };
1289
1290 const FFCodec ff_png_encoder = {
1291 .p.name = "png",
1292 CODEC_LONG_NAME("PNG (Portable Network Graphics) image"),
1293 .p.type = AVMEDIA_TYPE_VIDEO,
1294 .p.id = AV_CODEC_ID_PNG,
1295 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1296 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1297 .priv_data_size = sizeof(PNGEncContext),
1298 .init = png_enc_init,
1299 .close = png_enc_close,
1300 FF_CODEC_ENCODE_CB(encode_png),
1301 CODEC_PIXFMTS(AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
1302 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
1303 AV_PIX_FMT_PAL8,
1304 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
1305 AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE,
1306 AV_PIX_FMT_MONOBLACK),
1307 .alpha_modes = AVALPHA_MODE_STRAIGHT,
1308 .p.priv_class = &pngenc_class,
1309 .caps_internal = FF_CODEC_CAP_ICC_PROFILES,
1310 };
1311
1312 const FFCodec ff_apng_encoder = {
1313 .p.name = "apng",
1314 CODEC_LONG_NAME("APNG (Animated Portable Network Graphics) image"),
1315 .p.type = AVMEDIA_TYPE_VIDEO,
1316 .p.id = AV_CODEC_ID_APNG,
1317 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1318 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1319 .priv_data_size = sizeof(PNGEncContext),
1320 .init = png_enc_init,
1321 .close = png_enc_close,
1322 FF_CODEC_ENCODE_CB(encode_apng),
1323 CODEC_PIXFMTS(AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
1324 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
1325 AV_PIX_FMT_PAL8,
1326 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
1327 AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE),
1328 .alpha_modes = AVALPHA_MODE_STRAIGHT,
1329 .p.priv_class = &pngenc_class,
1330 .caps_internal = FF_CODEC_CAP_ICC_PROFILES,
1331 };
1332