FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pngenc.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 498 741 67.2%
Functions: 19 20 95.0%
Branches: 184 367 50.1%

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