FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pngenc.c
Date: 2024-04-18 10:05:09
Exec Total Coverage
Lines: 437 731 59.8%
Functions: 17 20 85.0%
Branches: 159 357 44.5%

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