FFmpeg coverage


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