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 | 79576 | 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 79576 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
79576 | switch (filter_type) { |
176 | 79576 | case PNG_FILTER_VALUE_NONE: | |
177 | 79576 | memcpy(dst, src, size); | |
178 | 79576 | 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 | 79576 | } | |
198 | |||
199 | 79576 | 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 | 79576 | int pred = s->filter_type; | |
203 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 79576 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
79576 | av_assert0(bpp || !pred); |
204 |
3/4✓ Branch 0 taken 318 times.
✓ Branch 1 taken 79258 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 318 times.
|
79576 | if (!top && pred) |
205 | ✗ | pred = PNG_FILTER_VALUE_SUB; | |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 79576 times.
|
79576 | 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 | 79576 | png_filter_row(s, dst + 1, pred, src, top, size, bpp); | |
224 | 79576 | dst[0] = pred; | |
225 | 79576 | return dst; | |
226 | } | ||
227 | } | ||
228 | |||
229 | 12813 | static void png_write_chunk(uint8_t **f, uint32_t tag, | |
230 | const uint8_t *buf, int length) | ||
231 | { | ||
232 | 12813 | const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE); | |
233 | 12813 | uint32_t crc = ~0U; | |
234 | uint8_t tagbuf[4]; | ||
235 | |||
236 | 12813 | bytestream_put_be32(f, length); | |
237 | 12813 | AV_WL32(tagbuf, tag); | |
238 | 12813 | crc = av_crc(crc_table, crc, tagbuf, 4); | |
239 | 12813 | bytestream_put_be32(f, av_bswap32(tag)); | |
240 |
2/2✓ Branch 0 taken 12568 times.
✓ Branch 1 taken 245 times.
|
12813 | if (length > 0) { |
241 | 12568 | crc = av_crc(crc_table, crc, buf, length); | |
242 |
2/2✓ Branch 0 taken 12567 times.
✓ Branch 1 taken 1 times.
|
12568 | if (*f != buf) |
243 | 12567 | memcpy(*f, buf, length); | |
244 | 12568 | *f += length; | |
245 | } | ||
246 | 12813 | bytestream_put_be32(f, ~crc); | |
247 | 12813 | } | |
248 | |||
249 | 16364 | static void png_write_image_data(AVCodecContext *avctx, | |
250 | const uint8_t *buf, int length) | ||
251 | { | ||
252 | 16364 | PNGEncContext *s = avctx->priv_data; | |
253 | 16364 | const AVCRC *crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE); | |
254 | 16364 | uint32_t crc = ~0U; | |
255 | |||
256 |
4/4✓ Branch 0 taken 4444 times.
✓ Branch 1 taken 11920 times.
✓ Branch 2 taken 122 times.
✓ Branch 3 taken 4322 times.
|
16364 | if (avctx->codec_id == AV_CODEC_ID_PNG || avctx->frame_num == 0) { |
257 | 12042 | png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), buf, length); | |
258 | 12042 | 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 | 79576 | static int png_write_row(AVCodecContext *avctx, const uint8_t *data, int size) | |
278 | { | ||
279 | 79576 | PNGEncContext *s = avctx->priv_data; | |
280 | 79576 | z_stream *const zstream = &s->zstream.zstream; | |
281 | int ret; | ||
282 | |||
283 | 79576 | zstream->avail_in = size; | |
284 | 79576 | zstream->next_in = data; | |
285 |
2/2✓ Branch 0 taken 91210 times.
✓ Branch 1 taken 79576 times.
|
250362 | while (zstream->avail_in > 0) { |
286 | 91210 | ret = deflate(zstream, Z_NO_FLUSH); | |
287 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 91210 times.
|
91210 | if (ret != Z_OK) |
288 | ✗ | return -1; | |
289 |
2/2✓ Branch 0 taken 75815 times.
✓ Branch 1 taken 15395 times.
|
91210 | if (zstream->avail_out == 0) { |
290 |
1/2✓ Branch 0 taken 15395 times.
✗ Branch 1 not taken.
|
15395 | if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100) |
291 | 15395 | png_write_image_data(avctx, s->buf, IOBUF_SIZE); | |
292 | 15395 | zstream->avail_out = IOBUF_SIZE; | |
293 | 15395 | zstream->next_out = s->buf; | |
294 | } | ||
295 | } | ||
296 | 79576 | 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 | 247 | static int png_get_chrm(enum AVColorPrimaries prim, uint8_t *buf) | |
303 | { | ||
304 | 247 | const AVColorPrimariesDesc *desc = av_csp_primaries_desc_from_id(prim); | |
305 |
2/2✓ Branch 0 taken 245 times.
✓ Branch 1 taken 2 times.
|
247 | if (!desc) |
306 | 245 | return 0; | |
307 | |||
308 | 2 | AV_WB32_PNG_D(buf, desc->wp.x); | |
309 | 2 | AV_WB32_PNG_D(buf + 4, desc->wp.y); | |
310 | 2 | AV_WB32_PNG_D(buf + 8, desc->prim.r.x); | |
311 | 2 | AV_WB32_PNG_D(buf + 12, desc->prim.r.y); | |
312 | 2 | AV_WB32_PNG_D(buf + 16, desc->prim.g.x); | |
313 | 2 | AV_WB32_PNG_D(buf + 20, desc->prim.g.y); | |
314 | 2 | AV_WB32_PNG_D(buf + 24, desc->prim.b.x); | |
315 | 2 | AV_WB32_PNG_D(buf + 28, desc->prim.b.y); | |
316 | |||
317 | 2 | return 1; | |
318 | } | ||
319 | |||
320 | 247 | static int png_get_gama(enum AVColorTransferCharacteristic trc, uint8_t *buf) | |
321 | { | ||
322 | 247 | double gamma = av_csp_approximate_trc_gamma(trc); | |
323 |
1/2✓ Branch 0 taken 247 times.
✗ Branch 1 not taken.
|
247 | if (gamma <= 1e-6) |
324 | 247 | return 0; | |
325 | |||
326 | ✗ | AV_WB32(buf, PNG_LRINT(1.0 / gamma, 100000)); | |
327 | ✗ | return 1; | |
328 | } | ||
329 | |||
330 | 247 | static int png_write_iccp(PNGEncContext *s, const AVFrameSideData *sd) | |
331 | { | ||
332 | 247 | 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 246 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
247 | if (!sd || !sd->size) |
339 | 246 | 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 | 247 | static int encode_headers(AVCodecContext *avctx, const AVFrame *pict) | |
373 | { | ||
374 | AVFrameSideData *side_data; | ||
375 | 247 | PNGEncContext *s = avctx->priv_data; | |
376 | int ret; | ||
377 | |||
378 | /* write png header */ | ||
379 | 247 | AV_WB32(s->buf, avctx->width); | |
380 | 247 | AV_WB32(s->buf + 4, avctx->height); | |
381 | 247 | s->buf[8] = s->bit_depth; | |
382 | 247 | s->buf[9] = s->color_type; | |
383 | 247 | s->buf[10] = 0; /* compression type */ | |
384 | 247 | s->buf[11] = 0; /* filter type */ | |
385 | 247 | s->buf[12] = s->is_progressive; /* interlace type */ | |
386 | 247 | 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 247 times.
|
247 | 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 | 247 | AV_WB32(s->buf, avctx->sample_aspect_ratio.num); | |
395 | 247 | AV_WB32(s->buf + 4, avctx->sample_aspect_ratio.den); | |
396 | 247 | s->buf[8] = 0; /* unit specifier is unknown */ | |
397 | } | ||
398 | 247 | png_write_chunk(&s->bytestream, MKTAG('p', 'H', 'Y', 's'), s->buf, 9); | |
399 | |||
400 | /* write stereoscopic information */ | ||
401 | 247 | side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_STEREO3D); | |
402 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 247 times.
|
247 | 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 | 247 | side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE); | |
418 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 247 times.
|
247 | 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 246 times.
|
247 | 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 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 246 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
247 | } 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 | 1 | s->buf[0] = pict->color_primaries; | |
437 | 1 | s->buf[1] = pict->color_trc; | |
438 | 1 | s->buf[2] = 0; /* colorspace = RGB */ | |
439 | 1 | s->buf[3] = pict->color_range == AVCOL_RANGE_MPEG ? 0 : 1; | |
440 | 1 | png_write_chunk(&s->bytestream, MKTAG('c', 'I', 'C', 'P'), s->buf, 4); | |
441 | } | ||
442 | |||
443 | 247 | side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL); | |
444 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 246 times.
|
247 | if (side_data) { |
445 | 1 | AVContentLightMetadata *clli = (AVContentLightMetadata *) side_data->data; | |
446 | 1 | AV_WB32(s->buf, clli->MaxCLL * 10000); | |
447 | 1 | AV_WB32(s->buf + 4, clli->MaxFALL * 10000); | |
448 | 1 | png_write_chunk(&s->bytestream, MKTAG('c', 'L', 'L', 'i'), s->buf, 8); | |
449 | } | ||
450 | |||
451 | 247 | side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA); | |
452 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 246 times.
|
247 | if (side_data) { |
453 | 1 | AVMasteringDisplayMetadata *mdcv = (AVMasteringDisplayMetadata *) side_data->data; | |
454 |
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) { |
455 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | for (int i = 0; i < 3; i++) { |
456 | 3 | AV_WB16(s->buf + 2*i, PNG_Q2D(mdcv->display_primaries[i][0], 50000)); | |
457 | 3 | AV_WB16(s->buf + 2*i + 2, PNG_Q2D(mdcv->display_primaries[i][1], 50000)); | |
458 | } | ||
459 | 1 | AV_WB16(s->buf + 12, PNG_Q2D(mdcv->white_point[0], 50000)); | |
460 | 1 | AV_WB16(s->buf + 14, PNG_Q2D(mdcv->white_point[1], 50000)); | |
461 | 1 | AV_WB32(s->buf + 16, PNG_Q2D(mdcv->max_luminance, 10000)); | |
462 | 1 | AV_WB32(s->buf + 20, PNG_Q2D(mdcv->min_luminance, 10000)); | |
463 | 1 | png_write_chunk(&s->bytestream, MKTAG('m', 'D', 'C', 'v'), s->buf, 24); | |
464 | } | ||
465 | } | ||
466 | |||
467 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 245 times.
|
247 | if (png_get_chrm(pict->color_primaries, s->buf)) |
468 | 2 | png_write_chunk(&s->bytestream, MKTAG('c', 'H', 'R', 'M'), s->buf, 32); | |
469 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 247 times.
|
247 | 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 247 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
247 | if (avctx->bits_per_raw_sample > 0 && |
473 | ✗ | avctx->bits_per_raw_sample < (s->color_type & PNG_COLOR_MASK_PALETTE ? 8 : s->bit_depth)) { | |
474 | ✗ | int len = s->color_type & PNG_COLOR_MASK_PALETTE ? 3 : ff_png_get_nb_channels(s->color_type); | |
475 | ✗ | memset(s->buf, avctx->bits_per_raw_sample, len); | |
476 | ✗ | png_write_chunk(&s->bytestream, MKTAG('s', 'B', 'I', 'T'), s->buf, len); | |
477 | } | ||
478 | |||
479 | /* put the palette if needed, must be after colorspace information */ | ||
480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 247 times.
|
247 | if (s->color_type == PNG_COLOR_TYPE_PALETTE) { |
481 | int has_alpha, alpha, i; | ||
482 | unsigned int v; | ||
483 | uint32_t *palette; | ||
484 | uint8_t *ptr, *alpha_ptr; | ||
485 | |||
486 | ✗ | palette = (uint32_t *)pict->data[1]; | |
487 | ✗ | ptr = s->buf; | |
488 | ✗ | alpha_ptr = s->buf + 256 * 3; | |
489 | ✗ | has_alpha = 0; | |
490 | ✗ | for (i = 0; i < 256; i++) { | |
491 | ✗ | v = palette[i]; | |
492 | ✗ | alpha = v >> 24; | |
493 | ✗ | if (alpha != 0xff) | |
494 | ✗ | has_alpha = 1; | |
495 | ✗ | *alpha_ptr++ = alpha; | |
496 | ✗ | bytestream_put_be24(&ptr, v); | |
497 | } | ||
498 | ✗ | png_write_chunk(&s->bytestream, | |
499 | ✗ | MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3); | |
500 | ✗ | if (has_alpha) { | |
501 | ✗ | png_write_chunk(&s->bytestream, | |
502 | ✗ | MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256); | |
503 | } | ||
504 | } | ||
505 | |||
506 | 247 | return 0; | |
507 | } | ||
508 | |||
509 | 318 | static int encode_frame(AVCodecContext *avctx, const AVFrame *pict) | |
510 | { | ||
511 | 318 | PNGEncContext *s = avctx->priv_data; | |
512 | 318 | z_stream *const zstream = &s->zstream.zstream; | |
513 | 318 | const AVFrame *const p = pict; | |
514 | int y, len, ret; | ||
515 | int row_size, pass_row_size; | ||
516 | uint8_t *crow_buf, *crow; | ||
517 | 318 | uint8_t *crow_base = NULL; | |
518 | 318 | uint8_t *progressive_buf = NULL; | |
519 | 318 | uint8_t *top_buf = NULL; | |
520 | |||
521 | 318 | row_size = (pict->width * s->bits_per_pixel + 7) >> 3; | |
522 | |||
523 | 318 | crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED)); | |
524 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
|
318 | if (!crow_base) { |
525 | ✗ | ret = AVERROR(ENOMEM); | |
526 | ✗ | goto the_end; | |
527 | } | ||
528 | // pixel data should be aligned, but there's a control byte before it | ||
529 | 318 | crow_buf = crow_base + 15; | |
530 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
|
318 | if (s->is_progressive) { |
531 | ✗ | progressive_buf = av_malloc(row_size + 1); | |
532 | ✗ | top_buf = av_malloc(row_size + 1); | |
533 | ✗ | if (!progressive_buf || !top_buf) { | |
534 | ✗ | ret = AVERROR(ENOMEM); | |
535 | ✗ | goto the_end; | |
536 | } | ||
537 | } | ||
538 | |||
539 | /* put each row */ | ||
540 | 318 | zstream->avail_out = IOBUF_SIZE; | |
541 | 318 | zstream->next_out = s->buf; | |
542 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 318 times.
|
318 | if (s->is_progressive) { |
543 | int pass; | ||
544 | |||
545 | ✗ | for (pass = 0; pass < NB_PASSES; pass++) { | |
546 | /* NOTE: a pass is completely omitted if no pixels would be | ||
547 | * output */ | ||
548 | ✗ | pass_row_size = ff_png_pass_row_size(pass, s->bits_per_pixel, pict->width); | |
549 | ✗ | if (pass_row_size > 0) { | |
550 | ✗ | uint8_t *top = NULL; | |
551 | ✗ | for (y = 0; y < pict->height; y++) | |
552 | ✗ | if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) { | |
553 | ✗ | const uint8_t *ptr = p->data[0] + y * p->linesize[0]; | |
554 | ✗ | FFSWAP(uint8_t *, progressive_buf, top_buf); | |
555 | ✗ | png_get_interlaced_row(progressive_buf, pass_row_size, | |
556 | s->bits_per_pixel, pass, | ||
557 | ✗ | ptr, pict->width); | |
558 | ✗ | crow = png_choose_filter(s, crow_buf, progressive_buf, | |
559 | ✗ | top, pass_row_size, s->bits_per_pixel >> 3); | |
560 | ✗ | png_write_row(avctx, crow, pass_row_size + 1); | |
561 | ✗ | top = progressive_buf; | |
562 | } | ||
563 | } | ||
564 | } | ||
565 | } else { | ||
566 | 318 | const uint8_t *top = NULL; | |
567 |
2/2✓ Branch 0 taken 318 times.
✓ Branch 1 taken 79576 times.
|
79894 | for (y = 0; y < pict->height; y++) { |
568 | 79576 | const uint8_t *ptr = p->data[0] + y * p->linesize[0]; | |
569 | 79576 | crow = png_choose_filter(s, crow_buf, ptr, top, | |
570 | 79576 | row_size, s->bits_per_pixel >> 3); | |
571 | 79576 | png_write_row(avctx, crow, row_size + 1); | |
572 | 79576 | top = ptr; | |
573 | } | ||
574 | } | ||
575 | /* compress last bytes */ | ||
576 | for (;;) { | ||
577 | 969 | ret = deflate(zstream, Z_FINISH); | |
578 |
3/4✓ Branch 0 taken 318 times.
✓ Branch 1 taken 651 times.
✓ Branch 2 taken 318 times.
✗ Branch 3 not taken.
|
969 | if (ret == Z_OK || ret == Z_STREAM_END) { |
579 | 969 | len = IOBUF_SIZE - zstream->avail_out; | |
580 |
2/4✓ Branch 0 taken 969 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 969 times.
✗ Branch 3 not taken.
|
969 | if (len > 0 && s->bytestream_end - s->bytestream > len + 100) { |
581 | 969 | png_write_image_data(avctx, s->buf, len); | |
582 | } | ||
583 | 969 | zstream->avail_out = IOBUF_SIZE; | |
584 | 969 | zstream->next_out = s->buf; | |
585 |
2/2✓ Branch 0 taken 318 times.
✓ Branch 1 taken 651 times.
|
969 | if (ret == Z_STREAM_END) |
586 | 318 | break; | |
587 | } else { | ||
588 | ✗ | ret = -1; | |
589 | ✗ | goto the_end; | |
590 | } | ||
591 | } | ||
592 | |||
593 | 318 | ret = 0; | |
594 | |||
595 | 318 | the_end: | |
596 | 318 | av_freep(&crow_base); | |
597 | 318 | av_freep(&progressive_buf); | |
598 | 318 | av_freep(&top_buf); | |
599 | 318 | deflateReset(zstream); | |
600 | 318 | return ret; | |
601 | } | ||
602 | |||
603 | 275 | static int add_icc_profile_size(AVCodecContext *avctx, const AVFrame *pict, | |
604 | uint64_t *max_packet_size) | ||
605 | { | ||
606 | 275 | PNGEncContext *s = avctx->priv_data; | |
607 | const AVFrameSideData *sd; | ||
608 | 275 | const int hdr_size = 128; | |
609 | uint64_t new_pkt_size; | ||
610 | uLong bound; | ||
611 | |||
612 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 271 times.
|
275 | if (!pict) |
613 | 4 | return 0; | |
614 | 271 | sd = av_frame_get_side_data(pict, AV_FRAME_DATA_ICC_PROFILE); | |
615 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 270 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
271 | if (!sd || !sd->size) |
616 | 270 | return 0; | |
617 | if (sd->size != (uLong) sd->size) | ||
618 | return AVERROR_INVALIDDATA; | ||
619 | |||
620 | 1 | bound = deflateBound(&s->zstream.zstream, sd->size); | |
621 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (bound > INT32_MAX - hdr_size) |
622 | ✗ | return AVERROR_INVALIDDATA; | |
623 | |||
624 | 1 | new_pkt_size = *max_packet_size + bound + hdr_size; | |
625 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (new_pkt_size < *max_packet_size) |
626 | ✗ | return AVERROR_INVALIDDATA; | |
627 | 1 | *max_packet_size = new_pkt_size; | |
628 | 1 | return 0; | |
629 | } | ||
630 | |||
631 | 245 | static int encode_png(AVCodecContext *avctx, AVPacket *pkt, | |
632 | const AVFrame *pict, int *got_packet) | ||
633 | { | ||
634 | 245 | PNGEncContext *s = avctx->priv_data; | |
635 | int ret; | ||
636 | int enc_row_size; | ||
637 | uint64_t max_packet_size; | ||
638 | |||
639 | 490 | enc_row_size = deflateBound(&s->zstream.zstream, | |
640 | 245 | (avctx->width * s->bits_per_pixel + 7) >> 3); | |
641 | 245 | max_packet_size = | |
642 | 245 | FF_INPUT_BUFFER_MIN_SIZE + // headers | |
643 | 245 | avctx->height * ( | |
644 | 245 | enc_row_size + | |
645 | 245 | 12 * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // IDAT * ceil(enc_row_size / IOBUF_SIZE) | |
646 | ); | ||
647 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 245 times.
|
245 | if ((ret = add_icc_profile_size(avctx, pict, &max_packet_size))) |
648 | ✗ | return ret; | |
649 | 245 | ret = ff_alloc_packet(avctx, pkt, max_packet_size); | |
650 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
|
245 | if (ret < 0) |
651 | ✗ | return ret; | |
652 | |||
653 | 245 | s->bytestream_start = | |
654 | 245 | s->bytestream = pkt->data; | |
655 | 245 | s->bytestream_end = pkt->data + pkt->size; | |
656 | |||
657 | 245 | AV_WB64(s->bytestream, PNGSIG); | |
658 | 245 | s->bytestream += 8; | |
659 | |||
660 | 245 | ret = encode_headers(avctx, pict); | |
661 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
|
245 | if (ret < 0) |
662 | ✗ | return ret; | |
663 | |||
664 | 245 | ret = encode_frame(avctx, pict); | |
665 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
|
245 | if (ret < 0) |
666 | ✗ | return ret; | |
667 | |||
668 | 245 | png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0); | |
669 | |||
670 | 245 | pkt->size = s->bytestream - s->bytestream_start; | |
671 | 245 | pkt->flags |= AV_PKT_FLAG_KEY; | |
672 | 245 | *got_packet = 1; | |
673 | |||
674 | 245 | return 0; | |
675 | } | ||
676 | |||
677 | 142 | static int apng_do_inverse_blend(AVFrame *output, const AVFrame *input, | |
678 | APNGFctlChunk *fctl_chunk, uint8_t bpp) | ||
679 | { | ||
680 | // output: background, input: foreground | ||
681 | // output the image such that when blended with the background, will produce the foreground | ||
682 | |||
683 | unsigned int x, y; | ||
684 | 142 | unsigned int leftmost_x = input->width; | |
685 | 142 | unsigned int rightmost_x = 0; | |
686 | 142 | unsigned int topmost_y = input->height; | |
687 | 142 | unsigned int bottommost_y = 0; | |
688 | 142 | const uint8_t *input_data = input->data[0]; | |
689 | 142 | uint8_t *output_data = output->data[0]; | |
690 | 142 | ptrdiff_t input_linesize = input->linesize[0]; | |
691 | 142 | ptrdiff_t output_linesize = output->linesize[0]; | |
692 | |||
693 | // Find bounding box of changes | ||
694 |
2/2✓ Branch 0 taken 40896 times.
✓ Branch 1 taken 142 times.
|
41038 | for (y = 0; y < input->height; ++y) { |
695 |
2/2✓ Branch 0 taken 14395392 times.
✓ Branch 1 taken 40896 times.
|
14436288 | for (x = 0; x < input->width; ++x) { |
696 |
2/2✓ Branch 0 taken 3066 times.
✓ Branch 1 taken 14392326 times.
|
14395392 | if (!memcmp(input_data + bpp * x, output_data + bpp * x, bpp)) |
697 | 3066 | continue; | |
698 | |||
699 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 14392184 times.
|
14392326 | if (x < leftmost_x) |
700 | 142 | leftmost_x = x; | |
701 |
2/2✓ Branch 0 taken 49984 times.
✓ Branch 1 taken 14342342 times.
|
14392326 | if (x >= rightmost_x) |
702 | 49984 | rightmost_x = x + 1; | |
703 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 14392184 times.
|
14392326 | if (y < topmost_y) |
704 | 142 | topmost_y = y; | |
705 |
2/2✓ Branch 0 taken 40896 times.
✓ Branch 1 taken 14351430 times.
|
14392326 | if (y >= bottommost_y) |
706 | 40896 | bottommost_y = y + 1; | |
707 | } | ||
708 | |||
709 | 40896 | input_data += input_linesize; | |
710 | 40896 | output_data += output_linesize; | |
711 | } | ||
712 | |||
713 |
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) { |
714 | // Empty frame | ||
715 | // APNG does not support empty frames, so we make it a 1x1 frame | ||
716 | ✗ | leftmost_x = topmost_y = 0; | |
717 | ✗ | rightmost_x = bottommost_y = 1; | |
718 | } | ||
719 | |||
720 | // Do actual inverse blending | ||
721 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 71 times.
|
142 | if (fctl_chunk->blend_op == APNG_BLEND_OP_SOURCE) { |
722 | 71 | output_data = output->data[0]; | |
723 |
2/2✓ Branch 0 taken 20448 times.
✓ Branch 1 taken 71 times.
|
20519 | for (y = topmost_y; y < bottommost_y; ++y) { |
724 | 20448 | memcpy(output_data, | |
725 | 20448 | input->data[0] + input_linesize * y + bpp * leftmost_x, | |
726 | 20448 | bpp * (rightmost_x - leftmost_x)); | |
727 | 20448 | output_data += output_linesize; | |
728 | } | ||
729 | } else { // APNG_BLEND_OP_OVER | ||
730 | size_t transparent_palette_index; | ||
731 | uint32_t *palette; | ||
732 | |||
733 |
1/3✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | switch (input->format) { |
734 | ✗ | case AV_PIX_FMT_RGBA64BE: | |
735 | case AV_PIX_FMT_YA16BE: | ||
736 | case AV_PIX_FMT_RGBA: | ||
737 | case AV_PIX_FMT_GRAY8A: | ||
738 | ✗ | break; | |
739 | |||
740 | ✗ | case AV_PIX_FMT_PAL8: | |
741 | ✗ | palette = (uint32_t*)input->data[1]; | |
742 | ✗ | for (transparent_palette_index = 0; transparent_palette_index < 256; ++transparent_palette_index) | |
743 | ✗ | if (palette[transparent_palette_index] >> 24 == 0) | |
744 | ✗ | break; | |
745 | ✗ | break; | |
746 | |||
747 | 71 | default: | |
748 | // No alpha, so blending not possible | ||
749 | 71 | return -1; | |
750 | } | ||
751 | |||
752 | ✗ | for (y = topmost_y; y < bottommost_y; ++y) { | |
753 | ✗ | const uint8_t *foreground = input->data[0] + input_linesize * y + bpp * leftmost_x; | |
754 | ✗ | uint8_t *background = output->data[0] + output_linesize * y + bpp * leftmost_x; | |
755 | ✗ | output_data = output->data[0] + output_linesize * (y - topmost_y); | |
756 | ✗ | for (x = leftmost_x; x < rightmost_x; ++x, foreground += bpp, background += bpp, output_data += bpp) { | |
757 | ✗ | if (!memcmp(foreground, background, bpp)) { | |
758 | ✗ | if (input->format == AV_PIX_FMT_PAL8) { | |
759 | ✗ | if (transparent_palette_index == 256) { | |
760 | // Need fully transparent colour, but none exists | ||
761 | ✗ | return -1; | |
762 | } | ||
763 | |||
764 | ✗ | *output_data = transparent_palette_index; | |
765 | } else { | ||
766 | ✗ | memset(output_data, 0, bpp); | |
767 | } | ||
768 | ✗ | continue; | |
769 | } | ||
770 | |||
771 | // Check for special alpha values, since full inverse | ||
772 | // alpha-on-alpha blending is rarely possible, and when | ||
773 | // possible, doesn't compress much better than | ||
774 | // APNG_BLEND_OP_SOURCE blending | ||
775 | ✗ | switch (input->format) { | |
776 | ✗ | case AV_PIX_FMT_RGBA64BE: | |
777 | ✗ | if (((uint16_t*)foreground)[3] == 0xffff || | |
778 | ✗ | ((uint16_t*)background)[3] == 0) | |
779 | break; | ||
780 | ✗ | return -1; | |
781 | |||
782 | ✗ | case AV_PIX_FMT_YA16BE: | |
783 | ✗ | if (((uint16_t*)foreground)[1] == 0xffff || | |
784 | ✗ | ((uint16_t*)background)[1] == 0) | |
785 | break; | ||
786 | ✗ | return -1; | |
787 | |||
788 | ✗ | case AV_PIX_FMT_RGBA: | |
789 | ✗ | if (foreground[3] == 0xff || background[3] == 0) | |
790 | break; | ||
791 | ✗ | return -1; | |
792 | |||
793 | ✗ | case AV_PIX_FMT_GRAY8A: | |
794 | ✗ | if (foreground[1] == 0xff || background[1] == 0) | |
795 | break; | ||
796 | ✗ | return -1; | |
797 | |||
798 | ✗ | case AV_PIX_FMT_PAL8: | |
799 | ✗ | if (palette[*foreground] >> 24 == 0xff || | |
800 | ✗ | palette[*background] >> 24 == 0) | |
801 | break; | ||
802 | ✗ | return -1; | |
803 | } | ||
804 | |||
805 | ✗ | memmove(output_data, foreground, bpp); | |
806 | } | ||
807 | } | ||
808 | } | ||
809 | |||
810 | 71 | output->width = rightmost_x - leftmost_x; | |
811 | 71 | output->height = bottommost_y - topmost_y; | |
812 | 71 | fctl_chunk->width = output->width; | |
813 | 71 | fctl_chunk->height = output->height; | |
814 | 71 | fctl_chunk->x_offset = leftmost_x; | |
815 | 71 | fctl_chunk->y_offset = topmost_y; | |
816 | |||
817 | 71 | return 0; | |
818 | } | ||
819 | |||
820 | 26 | static int apng_encode_frame(AVCodecContext *avctx, const AVFrame *pict, | |
821 | APNGFctlChunk *best_fctl_chunk, APNGFctlChunk *best_last_fctl_chunk) | ||
822 | { | ||
823 | 26 | PNGEncContext *s = avctx->priv_data; | |
824 | int ret; | ||
825 | unsigned int y; | ||
826 | AVFrame* diffFrame; | ||
827 | 26 | uint8_t bpp = (s->bits_per_pixel + 7) >> 3; | |
828 | uint8_t *original_bytestream, *original_bytestream_end; | ||
829 | 26 | uint8_t *temp_bytestream = 0, *temp_bytestream_end; | |
830 | uint32_t best_sequence_number; | ||
831 | uint8_t *best_bytestream; | ||
832 | 26 | size_t best_bytestream_size = SIZE_MAX; | |
833 | 26 | APNGFctlChunk last_fctl_chunk = *best_last_fctl_chunk; | |
834 | 26 | APNGFctlChunk fctl_chunk = *best_fctl_chunk; | |
835 | |||
836 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
|
26 | if (avctx->frame_num == 0) { |
837 | 2 | best_fctl_chunk->width = pict->width; | |
838 | 2 | best_fctl_chunk->height = pict->height; | |
839 | 2 | best_fctl_chunk->x_offset = 0; | |
840 | 2 | best_fctl_chunk->y_offset = 0; | |
841 | 2 | best_fctl_chunk->blend_op = APNG_BLEND_OP_SOURCE; | |
842 | 2 | return encode_frame(avctx, pict); | |
843 | } | ||
844 | |||
845 | 24 | diffFrame = av_frame_alloc(); | |
846 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (!diffFrame) |
847 | ✗ | return AVERROR(ENOMEM); | |
848 | |||
849 | 24 | diffFrame->format = pict->format; | |
850 | 24 | diffFrame->width = pict->width; | |
851 | 24 | diffFrame->height = pict->height; | |
852 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if ((ret = av_frame_get_buffer(diffFrame, 0)) < 0) |
853 | ✗ | goto fail; | |
854 | |||
855 | 24 | original_bytestream = s->bytestream; | |
856 | 24 | original_bytestream_end = s->bytestream_end; | |
857 | |||
858 | 24 | temp_bytestream = av_malloc(original_bytestream_end - original_bytestream); | |
859 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (!temp_bytestream) { |
860 | ✗ | ret = AVERROR(ENOMEM); | |
861 | ✗ | goto fail; | |
862 | } | ||
863 | 24 | temp_bytestream_end = temp_bytestream + (original_bytestream_end - original_bytestream); | |
864 | |||
865 |
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) { |
866 | // 0: APNG_DISPOSE_OP_NONE | ||
867 | // 1: APNG_DISPOSE_OP_BACKGROUND | ||
868 | // 2: APNG_DISPOSE_OP_PREVIOUS | ||
869 | |||
870 |
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) { |
871 | // 0: APNG_BLEND_OP_SOURCE | ||
872 | // 1: APNG_BLEND_OP_OVER | ||
873 | |||
874 | 144 | uint32_t original_sequence_number = s->sequence_number, sequence_number; | |
875 | 144 | uint8_t *bytestream_start = s->bytestream; | |
876 | size_t bytestream_size; | ||
877 | |||
878 | // Do disposal | ||
879 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | if (last_fctl_chunk.dispose_op != APNG_DISPOSE_OP_PREVIOUS) { |
880 | 96 | diffFrame->width = pict->width; | |
881 | 96 | diffFrame->height = pict->height; | |
882 | 96 | ret = av_frame_copy(diffFrame, s->last_frame); | |
883 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 | if (ret < 0) |
884 | ✗ | goto fail; | |
885 | |||
886 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
|
96 | if (last_fctl_chunk.dispose_op == APNG_DISPOSE_OP_BACKGROUND) { |
887 |
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) { |
888 | 13824 | size_t row_start = diffFrame->linesize[0] * y + bpp * last_fctl_chunk.x_offset; | |
889 | 13824 | memset(diffFrame->data[0] + row_start, 0, bpp * last_fctl_chunk.width); | |
890 | } | ||
891 | } | ||
892 | } else { | ||
893 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 46 times.
|
48 | if (!s->prev_frame) |
894 | 2 | continue; | |
895 | |||
896 | 46 | diffFrame->width = pict->width; | |
897 | 46 | diffFrame->height = pict->height; | |
898 | 46 | ret = av_frame_copy(diffFrame, s->prev_frame); | |
899 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
|
46 | if (ret < 0) |
900 | ✗ | goto fail; | |
901 | } | ||
902 | |||
903 | // Do inverse blending | ||
904 |
2/2✓ Branch 1 taken 71 times.
✓ Branch 2 taken 71 times.
|
142 | if (apng_do_inverse_blend(diffFrame, pict, &fctl_chunk, bpp) < 0) |
905 | 71 | continue; | |
906 | |||
907 | // Do encoding | ||
908 | 71 | ret = encode_frame(avctx, diffFrame); | |
909 | 71 | sequence_number = s->sequence_number; | |
910 | 71 | s->sequence_number = original_sequence_number; | |
911 | 71 | bytestream_size = s->bytestream - bytestream_start; | |
912 | 71 | s->bytestream = bytestream_start; | |
913 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (ret < 0) |
914 | ✗ | goto fail; | |
915 | |||
916 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 47 times.
|
71 | if (bytestream_size < best_bytestream_size) { |
917 | 24 | *best_fctl_chunk = fctl_chunk; | |
918 | 24 | *best_last_fctl_chunk = last_fctl_chunk; | |
919 | |||
920 | 24 | best_sequence_number = sequence_number; | |
921 | 24 | best_bytestream = s->bytestream; | |
922 | 24 | best_bytestream_size = bytestream_size; | |
923 | |||
924 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (best_bytestream == original_bytestream) { |
925 | 24 | s->bytestream = temp_bytestream; | |
926 | 24 | s->bytestream_end = temp_bytestream_end; | |
927 | } else { | ||
928 | ✗ | s->bytestream = original_bytestream; | |
929 | ✗ | s->bytestream_end = original_bytestream_end; | |
930 | } | ||
931 | } | ||
932 | } | ||
933 | } | ||
934 | |||
935 | 24 | s->sequence_number = best_sequence_number; | |
936 | 24 | s->bytestream = original_bytestream + best_bytestream_size; | |
937 | 24 | s->bytestream_end = original_bytestream_end; | |
938 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (best_bytestream != original_bytestream) |
939 | ✗ | memcpy(original_bytestream, best_bytestream, best_bytestream_size); | |
940 | |||
941 | 24 | ret = 0; | |
942 | |||
943 | 24 | fail: | |
944 | 24 | av_freep(&temp_bytestream); | |
945 | 24 | av_frame_free(&diffFrame); | |
946 | 24 | return ret; | |
947 | } | ||
948 | |||
949 | 30 | static int encode_apng(AVCodecContext *avctx, AVPacket *pkt, | |
950 | const AVFrame *pict, int *got_packet) | ||
951 | { | ||
952 | 30 | PNGEncContext *s = avctx->priv_data; | |
953 | int ret; | ||
954 | int enc_row_size; | ||
955 | uint64_t max_packet_size; | ||
956 | 30 | APNGFctlChunk fctl_chunk = {0}; | |
957 | |||
958 |
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) { |
959 | ✗ | uint32_t checksum = ~av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), ~0U, pict->data[1], 256 * sizeof(uint32_t)); | |
960 | |||
961 | ✗ | if (avctx->frame_num == 0) { | |
962 | ✗ | s->palette_checksum = checksum; | |
963 | ✗ | } else if (checksum != s->palette_checksum) { | |
964 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
965 | "Input contains more than one unique palette. APNG does not support multiple palettes.\n"); | ||
966 | ✗ | return -1; | |
967 | } | ||
968 | } | ||
969 | |||
970 | 60 | enc_row_size = deflateBound(&s->zstream.zstream, | |
971 | 30 | (avctx->width * s->bits_per_pixel + 7) >> 3); | |
972 | 30 | max_packet_size = | |
973 | 30 | FF_INPUT_BUFFER_MIN_SIZE + // headers | |
974 | 30 | avctx->height * ( | |
975 | 30 | enc_row_size + | |
976 | 30 | (4 + 12) * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // fdAT * ceil(enc_row_size / IOBUF_SIZE) | |
977 | ); | ||
978 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
|
30 | if ((ret = add_icc_profile_size(avctx, pict, &max_packet_size))) |
979 | ✗ | return ret; | |
980 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (max_packet_size > INT_MAX) |
981 | ✗ | return AVERROR(ENOMEM); | |
982 | |||
983 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
|
30 | if (avctx->frame_num == 0) { |
984 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!pict) |
985 | ✗ | return AVERROR(EINVAL); | |
986 | |||
987 | 2 | s->bytestream = s->extra_data = av_malloc(FF_INPUT_BUFFER_MIN_SIZE); | |
988 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->extra_data) |
989 | ✗ | return AVERROR(ENOMEM); | |
990 | |||
991 | 2 | ret = encode_headers(avctx, pict); | |
992 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
993 | ✗ | return ret; | |
994 | |||
995 | 2 | s->extra_data_size = s->bytestream - s->extra_data; | |
996 | |||
997 | 2 | s->last_frame_packet = av_malloc(max_packet_size); | |
998 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->last_frame_packet) |
999 | ✗ | return AVERROR(ENOMEM); | |
1000 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 2 times.
|
28 | } else if (s->last_frame) { |
1001 | 26 | ret = ff_get_encode_buffer(avctx, pkt, s->last_frame_packet_size, 0); | |
1002 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (ret < 0) |
1003 | ✗ | return ret; | |
1004 | |||
1005 | 26 | memcpy(pkt->data, s->last_frame_packet, s->last_frame_packet_size); | |
1006 | 26 | pkt->pts = s->last_frame->pts; | |
1007 | 26 | pkt->duration = s->last_frame->duration; | |
1008 | |||
1009 | 26 | ret = ff_encode_reordered_opaque(avctx, pkt, s->last_frame); | |
1010 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (ret < 0) |
1011 | ✗ | return ret; | |
1012 | } | ||
1013 | |||
1014 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
|
30 | if (pict) { |
1015 | 26 | s->bytestream_start = | |
1016 | 26 | s->bytestream = s->last_frame_packet; | |
1017 | 26 | s->bytestream_end = s->bytestream + max_packet_size; | |
1018 | |||
1019 | // We're encoding the frame first, so we have to do a bit of shuffling around | ||
1020 | // to have the image data write to the correct place in the buffer | ||
1021 | 26 | fctl_chunk.sequence_number = s->sequence_number; | |
1022 | 26 | ++s->sequence_number; | |
1023 | 26 | s->bytestream += APNG_FCTL_CHUNK_SIZE + 12; | |
1024 | |||
1025 | 26 | ret = apng_encode_frame(avctx, pict, &fctl_chunk, &s->last_frame_fctl); | |
1026 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (ret < 0) |
1027 | ✗ | return ret; | |
1028 | |||
1029 | 26 | fctl_chunk.delay_num = 0; // delay filled in during muxing | |
1030 | 26 | fctl_chunk.delay_den = 0; | |
1031 | } else { | ||
1032 | 4 | s->last_frame_fctl.dispose_op = APNG_DISPOSE_OP_NONE; | |
1033 | } | ||
1034 | |||
1035 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
|
30 | if (s->last_frame) { |
1036 | 26 | uint8_t* last_fctl_chunk_start = pkt->data; | |
1037 | uint8_t buf[APNG_FCTL_CHUNK_SIZE]; | ||
1038 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
|
26 | if (!s->extra_data_updated) { |
1039 | 2 | uint8_t *side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, s->extra_data_size); | |
1040 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!side_data) |
1041 | ✗ | return AVERROR(ENOMEM); | |
1042 | 2 | memcpy(side_data, s->extra_data, s->extra_data_size); | |
1043 | 2 | s->extra_data_updated = 1; | |
1044 | } | ||
1045 | |||
1046 | 26 | AV_WB32(buf + 0, s->last_frame_fctl.sequence_number); | |
1047 | 26 | AV_WB32(buf + 4, s->last_frame_fctl.width); | |
1048 | 26 | AV_WB32(buf + 8, s->last_frame_fctl.height); | |
1049 | 26 | AV_WB32(buf + 12, s->last_frame_fctl.x_offset); | |
1050 | 26 | AV_WB32(buf + 16, s->last_frame_fctl.y_offset); | |
1051 | 26 | AV_WB16(buf + 20, s->last_frame_fctl.delay_num); | |
1052 | 26 | AV_WB16(buf + 22, s->last_frame_fctl.delay_den); | |
1053 | 26 | buf[24] = s->last_frame_fctl.dispose_op; | |
1054 | 26 | buf[25] = s->last_frame_fctl.blend_op; | |
1055 | 26 | png_write_chunk(&last_fctl_chunk_start, MKTAG('f', 'c', 'T', 'L'), buf, sizeof(buf)); | |
1056 | |||
1057 | 26 | *got_packet = 1; | |
1058 | } | ||
1059 | |||
1060 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 4 times.
|
30 | if (pict) { |
1061 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
|
26 | if (!s->last_frame) { |
1062 | 2 | s->last_frame = av_frame_alloc(); | |
1063 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->last_frame) |
1064 | ✗ | return AVERROR(ENOMEM); | |
1065 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | } else if (s->last_frame_fctl.dispose_op != APNG_DISPOSE_OP_PREVIOUS) { |
1066 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
|
24 | if (!s->prev_frame) { |
1067 | 1 | s->prev_frame = av_frame_alloc(); | |
1068 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!s->prev_frame) |
1069 | ✗ | return AVERROR(ENOMEM); | |
1070 | |||
1071 | 1 | s->prev_frame->format = pict->format; | |
1072 | 1 | s->prev_frame->width = pict->width; | |
1073 | 1 | s->prev_frame->height = pict->height; | |
1074 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = av_frame_get_buffer(s->prev_frame, 0)) < 0) |
1075 | ✗ | return ret; | |
1076 | } | ||
1077 | |||
1078 | // Do disposal, but not blending | ||
1079 | 24 | av_frame_copy(s->prev_frame, s->last_frame); | |
1080 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (s->last_frame_fctl.dispose_op == APNG_DISPOSE_OP_BACKGROUND) { |
1081 | uint32_t y; | ||
1082 | ✗ | uint8_t bpp = (s->bits_per_pixel + 7) >> 3; | |
1083 | ✗ | for (y = s->last_frame_fctl.y_offset; y < s->last_frame_fctl.y_offset + s->last_frame_fctl.height; ++y) { | |
1084 | ✗ | size_t row_start = s->prev_frame->linesize[0] * y + bpp * s->last_frame_fctl.x_offset; | |
1085 | ✗ | memset(s->prev_frame->data[0] + row_start, 0, bpp * s->last_frame_fctl.width); | |
1086 | } | ||
1087 | } | ||
1088 | } | ||
1089 | |||
1090 | 26 | ret = av_frame_replace(s->last_frame, pict); | |
1091 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (ret < 0) |
1092 | ✗ | return ret; | |
1093 | |||
1094 | 26 | s->last_frame_fctl = fctl_chunk; | |
1095 | 26 | s->last_frame_packet_size = s->bytestream - s->bytestream_start; | |
1096 | } else { | ||
1097 | 4 | av_frame_free(&s->last_frame); | |
1098 | } | ||
1099 | |||
1100 | 30 | return 0; | |
1101 | } | ||
1102 | |||
1103 | 15 | static av_cold int png_enc_init(AVCodecContext *avctx) | |
1104 | { | ||
1105 | 15 | PNGEncContext *s = avctx->priv_data; | |
1106 | int compression_level; | ||
1107 | |||
1108 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
15 | switch (avctx->pix_fmt) { |
1109 | ✗ | case AV_PIX_FMT_RGBA: | |
1110 | ✗ | avctx->bits_per_coded_sample = 32; | |
1111 | ✗ | break; | |
1112 | 13 | case AV_PIX_FMT_RGB24: | |
1113 | 13 | avctx->bits_per_coded_sample = 24; | |
1114 | 13 | break; | |
1115 | ✗ | case AV_PIX_FMT_GRAY8: | |
1116 | ✗ | avctx->bits_per_coded_sample = 0x28; | |
1117 | ✗ | break; | |
1118 | ✗ | case AV_PIX_FMT_MONOBLACK: | |
1119 | ✗ | avctx->bits_per_coded_sample = 1; | |
1120 | ✗ | break; | |
1121 | ✗ | case AV_PIX_FMT_PAL8: | |
1122 | ✗ | avctx->bits_per_coded_sample = 8; | |
1123 | } | ||
1124 | |||
1125 | 15 | ff_llvidencdsp_init(&s->llvidencdsp); | |
1126 | |||
1127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK) |
1128 | ✗ | s->filter_type = PNG_FILTER_VALUE_NONE; | |
1129 | |||
1130 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
15 | if (s->dpi && s->dpm) { |
1131 | ✗ | av_log(avctx, AV_LOG_ERROR, "Only one of 'dpi' or 'dpm' options should be set\n"); | |
1132 | ✗ | return AVERROR(EINVAL); | |
1133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | } else if (s->dpi) { |
1134 | ✗ | s->dpm = s->dpi * 10000 / 254; | |
1135 | } | ||
1136 | |||
1137 | 15 | s->is_progressive = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT); | |
1138 |
3/11✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 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.
|
15 | switch (avctx->pix_fmt) { |
1139 | ✗ | case AV_PIX_FMT_RGBA64BE: | |
1140 | ✗ | s->bit_depth = 16; | |
1141 | ✗ | s->color_type = PNG_COLOR_TYPE_RGB_ALPHA; | |
1142 | ✗ | break; | |
1143 | 1 | case AV_PIX_FMT_RGB48BE: | |
1144 | 1 | s->bit_depth = 16; | |
1145 | 1 | s->color_type = PNG_COLOR_TYPE_RGB; | |
1146 | 1 | break; | |
1147 | ✗ | case AV_PIX_FMT_RGBA: | |
1148 | ✗ | s->bit_depth = 8; | |
1149 | ✗ | s->color_type = PNG_COLOR_TYPE_RGB_ALPHA; | |
1150 | ✗ | break; | |
1151 | 13 | case AV_PIX_FMT_RGB24: | |
1152 | 13 | s->bit_depth = 8; | |
1153 | 13 | s->color_type = PNG_COLOR_TYPE_RGB; | |
1154 | 13 | break; | |
1155 | 1 | case AV_PIX_FMT_GRAY16BE: | |
1156 | 1 | s->bit_depth = 16; | |
1157 | 1 | s->color_type = PNG_COLOR_TYPE_GRAY; | |
1158 | 1 | break; | |
1159 | ✗ | case AV_PIX_FMT_GRAY8: | |
1160 | ✗ | s->bit_depth = 8; | |
1161 | ✗ | s->color_type = PNG_COLOR_TYPE_GRAY; | |
1162 | ✗ | break; | |
1163 | ✗ | case AV_PIX_FMT_GRAY8A: | |
1164 | ✗ | s->bit_depth = 8; | |
1165 | ✗ | s->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; | |
1166 | ✗ | break; | |
1167 | ✗ | case AV_PIX_FMT_YA16BE: | |
1168 | ✗ | s->bit_depth = 16; | |
1169 | ✗ | s->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; | |
1170 | ✗ | break; | |
1171 | ✗ | case AV_PIX_FMT_MONOBLACK: | |
1172 | ✗ | s->bit_depth = 1; | |
1173 | ✗ | s->color_type = PNG_COLOR_TYPE_GRAY; | |
1174 | ✗ | break; | |
1175 | ✗ | case AV_PIX_FMT_PAL8: | |
1176 | ✗ | s->bit_depth = 8; | |
1177 | ✗ | s->color_type = PNG_COLOR_TYPE_PALETTE; | |
1178 | ✗ | break; | |
1179 | ✗ | default: | |
1180 | ✗ | return -1; | |
1181 | } | ||
1182 | 15 | s->bits_per_pixel = ff_png_get_nb_channels(s->color_type) * s->bit_depth; | |
1183 | |||
1184 | 30 | compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT | |
1185 | ? Z_DEFAULT_COMPRESSION | ||
1186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | : av_clip(avctx->compression_level, 0, 9); |
1187 | 15 | return ff_deflate_init(&s->zstream, compression_level, avctx); | |
1188 | } | ||
1189 | |||
1190 | 15 | static av_cold int png_enc_close(AVCodecContext *avctx) | |
1191 | { | ||
1192 | 15 | PNGEncContext *s = avctx->priv_data; | |
1193 | |||
1194 | 15 | ff_deflate_end(&s->zstream); | |
1195 | 15 | av_frame_free(&s->last_frame); | |
1196 | 15 | av_frame_free(&s->prev_frame); | |
1197 | 15 | av_freep(&s->last_frame_packet); | |
1198 | 15 | av_freep(&s->extra_data); | |
1199 | 15 | s->extra_data_size = 0; | |
1200 | 15 | return 0; | |
1201 | } | ||
1202 | |||
1203 | #define OFFSET(x) offsetof(PNGEncContext, x) | ||
1204 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
1205 | static const AVOption options[] = { | ||
1206 | {"dpi", "Set image resolution (in dots per inch)", OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE}, | ||
1207 | {"dpm", "Set image resolution (in dots per meter)", OFFSET(dpm), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE}, | ||
1208 | { "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" }, | ||
1209 | { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_NONE }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
1210 | { "sub", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_SUB }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
1211 | { "up", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_UP }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
1212 | { "avg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_AVG }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
1213 | { "paeth", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_PAETH }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
1214 | { "mixed", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_MIXED }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
1215 | { NULL}, | ||
1216 | }; | ||
1217 | |||
1218 | static const AVClass pngenc_class = { | ||
1219 | .class_name = "(A)PNG encoder", | ||
1220 | .item_name = av_default_item_name, | ||
1221 | .option = options, | ||
1222 | .version = LIBAVUTIL_VERSION_INT, | ||
1223 | }; | ||
1224 | |||
1225 | const FFCodec ff_png_encoder = { | ||
1226 | .p.name = "png", | ||
1227 | CODEC_LONG_NAME("PNG (Portable Network Graphics) image"), | ||
1228 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1229 | .p.id = AV_CODEC_ID_PNG, | ||
1230 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | | ||
1231 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
1232 | .priv_data_size = sizeof(PNGEncContext), | ||
1233 | .init = png_enc_init, | ||
1234 | .close = png_enc_close, | ||
1235 | FF_CODEC_ENCODE_CB(encode_png), | ||
1236 | .p.pix_fmts = (const enum AVPixelFormat[]) { | ||
1237 | AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, | ||
1238 | AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE, | ||
1239 | AV_PIX_FMT_PAL8, | ||
1240 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, | ||
1241 | AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE, | ||
1242 | AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE | ||
1243 | }, | ||
1244 | .p.priv_class = &pngenc_class, | ||
1245 | .caps_internal = FF_CODEC_CAP_ICC_PROFILES, | ||
1246 | }; | ||
1247 | |||
1248 | const FFCodec ff_apng_encoder = { | ||
1249 | .p.name = "apng", | ||
1250 | CODEC_LONG_NAME("APNG (Animated Portable Network Graphics) image"), | ||
1251 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1252 | .p.id = AV_CODEC_ID_APNG, | ||
1253 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | | ||
1254 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
1255 | .priv_data_size = sizeof(PNGEncContext), | ||
1256 | .init = png_enc_init, | ||
1257 | .close = png_enc_close, | ||
1258 | FF_CODEC_ENCODE_CB(encode_apng), | ||
1259 | .p.pix_fmts = (const enum AVPixelFormat[]) { | ||
1260 | AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, | ||
1261 | AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE, | ||
1262 | AV_PIX_FMT_PAL8, | ||
1263 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, | ||
1264 | AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE, | ||
1265 | AV_PIX_FMT_NONE | ||
1266 | }, | ||
1267 | .p.priv_class = &pngenc_class, | ||
1268 | .caps_internal = FF_CODEC_CAP_ICC_PROFILES, | ||
1269 | }; | ||
1270 |