LCOV - code coverage report
Current view: top level - libavcodec - pngenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 382 682 56.0 %
Date: 2017-12-16 01:21:47 Functions: 15 18 83.3 %

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

Generated by: LCOV version 1.13