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