]> git.sesse.net Git - ffmpeg/blob - libavcodec/pngenc.c
Merge commit 'e3ec6fe7bb2a622a863e3912181717a659eb1bad'
[ffmpeg] / libavcodec / pngenc.c
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 "huffyuvencdsp.h"
26 #include "png.h"
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/libm.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/color_utils.h"
32
33 #include <zlib.h>
34
35 #define IOBUF_SIZE 4096
36
37 typedef struct PNGEncContext {
38     AVClass *class;
39     HuffYUVEncDSPContext hdsp;
40
41     uint8_t *bytestream;
42     uint8_t *bytestream_start;
43     uint8_t *bytestream_end;
44
45     int filter_type;
46
47     z_stream zstream;
48     uint8_t buf[IOBUF_SIZE];
49     int dpi;                     ///< Physical pixel density, in dots per inch, if set
50     int dpm;                     ///< Physical pixel density, in dots per meter, if set
51
52     int is_progressive;
53     int bit_depth;
54     int color_type;
55     int bits_per_pixel;
56 } PNGEncContext;
57
58 static void png_get_interlaced_row(uint8_t *dst, int row_size,
59                                    int bits_per_pixel, int pass,
60                                    const uint8_t *src, int width)
61 {
62     int x, mask, dst_x, j, b, bpp;
63     uint8_t *d;
64     const uint8_t *s;
65     static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
66
67     mask = masks[pass];
68     switch (bits_per_pixel) {
69     case 1:
70         memset(dst, 0, row_size);
71         dst_x = 0;
72         for (x = 0; x < width; x++) {
73             j = (x & 7);
74             if ((mask << j) & 0x80) {
75                 b = (src[x >> 3] >> (7 - j)) & 1;
76                 dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
77                 dst_x++;
78             }
79         }
80         break;
81     default:
82         bpp = bits_per_pixel >> 3;
83         d = dst;
84         s = src;
85         for (x = 0; x < width; x++) {
86             j = x & 7;
87             if ((mask << j) & 0x80) {
88                 memcpy(d, s, bpp);
89                 d += bpp;
90             }
91             s += bpp;
92         }
93         break;
94     }
95 }
96
97 static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
98                                      int w, int bpp)
99 {
100     int i;
101     for (i = 0; i < w; i++) {
102         int a, b, c, p, pa, pb, pc;
103
104         a = src[i - bpp];
105         b = top[i];
106         c = top[i - bpp];
107
108         p  = b - c;
109         pc = a - c;
110
111         pa = abs(p);
112         pb = abs(pc);
113         pc = abs(p + pc);
114
115         if (pa <= pb && pa <= pc)
116             p = a;
117         else if (pb <= pc)
118             p = b;
119         else
120             p = c;
121         dst[i] = src[i] - p;
122     }
123 }
124
125 static void sub_left_prediction(PNGEncContext *c, uint8_t *dst, const uint8_t *src, int bpp, int size)
126 {
127     const uint8_t *src1 = src + bpp;
128     const uint8_t *src2 = src;
129     int x, unaligned_w;
130
131     memcpy(dst, src, bpp);
132     dst += bpp;
133     size -= bpp;
134     unaligned_w = FFMIN(32 - bpp, size);
135     for (x = 0; x < unaligned_w; x++)
136         *dst++ = *src1++ - *src2++;
137     size -= unaligned_w;
138     c->hdsp.diff_bytes(dst, src1, src2, size);
139 }
140
141 static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type,
142                            uint8_t *src, uint8_t *top, int size, int bpp)
143 {
144     int i;
145
146     switch (filter_type) {
147     case PNG_FILTER_VALUE_NONE:
148         memcpy(dst, src, size);
149         break;
150     case PNG_FILTER_VALUE_SUB:
151         sub_left_prediction(c, dst, src, bpp, size);
152         break;
153     case PNG_FILTER_VALUE_UP:
154         c->hdsp.diff_bytes(dst, src, top, size);
155         break;
156     case PNG_FILTER_VALUE_AVG:
157         for (i = 0; i < bpp; i++)
158             dst[i] = src[i] - (top[i] >> 1);
159         for (; i < size; i++)
160             dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
161         break;
162     case PNG_FILTER_VALUE_PAETH:
163         for (i = 0; i < bpp; i++)
164             dst[i] = src[i] - top[i];
165         sub_png_paeth_prediction(dst + i, src + i, top + i, size - i, bpp);
166         break;
167     }
168 }
169
170 static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
171                                   uint8_t *src, uint8_t *top, int size, int bpp)
172 {
173     int pred = s->filter_type;
174     av_assert0(bpp || !pred);
175     if (!top && pred)
176         pred = PNG_FILTER_VALUE_SUB;
177     if (pred == PNG_FILTER_VALUE_MIXED) {
178         int i;
179         int cost, bcost = INT_MAX;
180         uint8_t *buf1 = dst, *buf2 = dst + size + 16;
181         for (pred = 0; pred < 5; pred++) {
182             png_filter_row(s, buf1 + 1, pred, src, top, size, bpp);
183             buf1[0] = pred;
184             cost = 0;
185             for (i = 0; i <= size; i++)
186                 cost += abs((int8_t) buf1[i]);
187             if (cost < bcost) {
188                 bcost = cost;
189                 FFSWAP(uint8_t *, buf1, buf2);
190             }
191         }
192         return buf2;
193     } else {
194         png_filter_row(s, dst + 1, pred, src, top, size, bpp);
195         dst[0] = pred;
196         return dst;
197     }
198 }
199
200 static void png_write_chunk(uint8_t **f, uint32_t tag,
201                             const uint8_t *buf, int length)
202 {
203     uint32_t crc;
204     uint8_t tagbuf[4];
205
206     bytestream_put_be32(f, length);
207     crc = crc32(0, Z_NULL, 0);
208     AV_WL32(tagbuf, tag);
209     crc = crc32(crc, tagbuf, 4);
210     bytestream_put_be32(f, av_bswap32(tag));
211     if (length > 0) {
212         crc = crc32(crc, buf, length);
213         memcpy(*f, buf, length);
214         *f += length;
215     }
216     bytestream_put_be32(f, crc);
217 }
218
219 /* XXX: do filtering */
220 static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
221 {
222     int ret;
223
224     s->zstream.avail_in = size;
225     s->zstream.next_in  = data;
226     while (s->zstream.avail_in > 0) {
227         ret = deflate(&s->zstream, Z_NO_FLUSH);
228         if (ret != Z_OK)
229             return -1;
230         if (s->zstream.avail_out == 0) {
231             if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
232                 png_write_chunk(&s->bytestream,
233                                 MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
234             s->zstream.avail_out = IOBUF_SIZE;
235             s->zstream.next_out  = s->buf;
236         }
237     }
238     return 0;
239 }
240
241 #define AV_WB32_PNG(buf, n) AV_WB32(buf, lrint((n) * 100000))
242 static int png_get_chrm(enum AVColorPrimaries prim,  uint8_t *buf)
243 {
244     double rx, ry, gx, gy, bx, by, wx = 0.3127, wy = 0.3290;
245     switch (prim) {
246         case AVCOL_PRI_BT709:
247             rx = 0.640; ry = 0.330;
248             gx = 0.300; gy = 0.600;
249             bx = 0.150; by = 0.060;
250             break;
251         case AVCOL_PRI_BT470M:
252             rx = 0.670; ry = 0.330;
253             gx = 0.210; gy = 0.710;
254             bx = 0.140; by = 0.080;
255             wx = 0.310; wy = 0.316;
256             break;
257         case AVCOL_PRI_BT470BG:
258             rx = 0.640; ry = 0.330;
259             gx = 0.290; gy = 0.600;
260             bx = 0.150; by = 0.060;
261             break;
262         case AVCOL_PRI_SMPTE170M:
263         case AVCOL_PRI_SMPTE240M:
264             rx = 0.630; ry = 0.340;
265             gx = 0.310; gy = 0.595;
266             bx = 0.155; by = 0.070;
267             break;
268         case AVCOL_PRI_BT2020:
269             rx = 0.708; ry = 0.292;
270             gx = 0.170; gy = 0.797;
271             bx = 0.131; by = 0.046;
272             break;
273         default:
274             return 0;
275     }
276
277     AV_WB32_PNG(buf     , wx); AV_WB32_PNG(buf + 4 , wy);
278     AV_WB32_PNG(buf + 8 , rx); AV_WB32_PNG(buf + 12, ry);
279     AV_WB32_PNG(buf + 16, gx); AV_WB32_PNG(buf + 20, gy);
280     AV_WB32_PNG(buf + 24, bx); AV_WB32_PNG(buf + 28, by);
281     return 1;
282 }
283
284 static int png_get_gama(enum AVColorTransferCharacteristic trc, uint8_t *buf)
285 {
286     double gamma = avpriv_get_gamma_from_trc(trc);
287     if (gamma <= 1e-6)
288         return 0;
289
290     AV_WB32_PNG(buf, 1.0 / gamma);
291     return 1;
292 }
293
294 static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
295 {
296     PNGEncContext *s = avctx->priv_data;
297
298     /* write png header */
299     AV_WB32(s->buf, avctx->width);
300     AV_WB32(s->buf + 4, avctx->height);
301     s->buf[8]  = s->bit_depth;
302     s->buf[9]  = s->color_type;
303     s->buf[10] = 0; /* compression type */
304     s->buf[11] = 0; /* filter type */
305     s->buf[12] = s->is_progressive; /* interlace type */
306     png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
307
308     /* write physical information */
309     if (s->dpm) {
310       AV_WB32(s->buf, s->dpm);
311       AV_WB32(s->buf + 4, s->dpm);
312       s->buf[8] = 1; /* unit specifier is meter */
313     } else {
314       AV_WB32(s->buf, avctx->sample_aspect_ratio.num);
315       AV_WB32(s->buf + 4, avctx->sample_aspect_ratio.den);
316       s->buf[8] = 0; /* unit specifier is unknown */
317     }
318     png_write_chunk(&s->bytestream, MKTAG('p', 'H', 'Y', 's'), s->buf, 9);
319
320     /* write colorspace information */
321     if (pict->color_primaries == AVCOL_PRI_BT709 &&
322         pict->color_trc == AVCOL_TRC_IEC61966_2_1) {
323         s->buf[0] = 1; /* rendering intent, relative colorimetric by default */
324         png_write_chunk(&s->bytestream, MKTAG('s', 'R', 'G', 'B'), s->buf, 1);
325     }
326
327     if (png_get_chrm(pict->color_primaries, s->buf))
328         png_write_chunk(&s->bytestream, MKTAG('c', 'H', 'R', 'M'), s->buf, 32);
329     if (png_get_gama(pict->color_trc, s->buf))
330         png_write_chunk(&s->bytestream, MKTAG('g', 'A', 'M', 'A'), s->buf, 4);
331
332     /* put the palette if needed */
333     if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
334         int has_alpha, alpha, i;
335         unsigned int v;
336         uint32_t *palette;
337         uint8_t *ptr, *alpha_ptr;
338
339         palette   = (uint32_t *)pict->data[1];
340         ptr       = s->buf;
341         alpha_ptr = s->buf + 256 * 3;
342         has_alpha = 0;
343         for (i = 0; i < 256; i++) {
344             v     = palette[i];
345             alpha = v >> 24;
346             if (alpha != 0xff)
347                 has_alpha = 1;
348             *alpha_ptr++ = alpha;
349             bytestream_put_be24(&ptr, v);
350         }
351         png_write_chunk(&s->bytestream,
352                         MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
353         if (has_alpha) {
354             png_write_chunk(&s->bytestream,
355                             MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
356         }
357     }
358
359     return 0;
360 }
361
362 static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
363 {
364     PNGEncContext *s       = avctx->priv_data;
365     const AVFrame *const p = pict;
366     int y, len, ret;
367     int row_size, pass_row_size;
368     uint8_t *ptr, *top, *crow_buf, *crow;
369     uint8_t *crow_base       = NULL;
370     uint8_t *progressive_buf = NULL;
371     uint8_t *top_buf         = NULL;
372
373     row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
374
375     crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
376     if (!crow_base) {
377         ret = AVERROR(ENOMEM);
378         goto the_end;
379     }
380     // pixel data should be aligned, but there's a control byte before it
381     crow_buf = crow_base + 15;
382     if (s->is_progressive) {
383         progressive_buf = av_malloc(row_size + 1);
384         top_buf = av_malloc(row_size + 1);
385         if (!progressive_buf || !top_buf) {
386             ret = AVERROR(ENOMEM);
387             goto the_end;
388         }
389     }
390
391     /* put each row */
392     s->zstream.avail_out = IOBUF_SIZE;
393     s->zstream.next_out  = s->buf;
394     if (s->is_progressive) {
395         int pass;
396
397         for (pass = 0; pass < NB_PASSES; pass++) {
398             /* NOTE: a pass is completely omitted if no pixels would be
399              * output */
400             pass_row_size = ff_png_pass_row_size(pass, s->bits_per_pixel, avctx->width);
401             if (pass_row_size > 0) {
402                 top = NULL;
403                 for (y = 0; y < avctx->height; y++)
404                     if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
405                         ptr = p->data[0] + y * p->linesize[0];
406                         FFSWAP(uint8_t *, progressive_buf, top_buf);
407                         png_get_interlaced_row(progressive_buf, pass_row_size,
408                                                s->bits_per_pixel, pass,
409                                                ptr, avctx->width);
410                         crow = png_choose_filter(s, crow_buf, progressive_buf,
411                                                  top, pass_row_size, s->bits_per_pixel >> 3);
412                         png_write_row(s, crow, pass_row_size + 1);
413                         top = progressive_buf;
414                     }
415             }
416         }
417     } else {
418         top = NULL;
419         for (y = 0; y < avctx->height; y++) {
420             ptr = p->data[0] + y * p->linesize[0];
421             crow = png_choose_filter(s, crow_buf, ptr, top,
422                                      row_size, s->bits_per_pixel >> 3);
423             png_write_row(s, crow, row_size + 1);
424             top = ptr;
425         }
426     }
427     /* compress last bytes */
428     for (;;) {
429         ret = deflate(&s->zstream, Z_FINISH);
430         if (ret == Z_OK || ret == Z_STREAM_END) {
431             len = IOBUF_SIZE - s->zstream.avail_out;
432             if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
433                 png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
434             }
435             s->zstream.avail_out = IOBUF_SIZE;
436             s->zstream.next_out  = s->buf;
437             if (ret == Z_STREAM_END)
438                 break;
439         } else {
440             ret = -1;
441             goto the_end;
442         }
443     }
444
445     ret = 0;
446
447 the_end:
448     av_freep(&crow_base);
449     av_freep(&progressive_buf);
450     av_freep(&top_buf);
451     deflateReset(&s->zstream);
452     return ret;
453 }
454
455 static int encode(AVCodecContext *avctx, AVPacket *pkt,
456                   const AVFrame *pict, int *got_packet)
457 {
458     PNGEncContext *s = avctx->priv_data;
459     int ret;
460     int enc_row_size;
461     size_t max_packet_size;
462
463     enc_row_size    = deflateBound(&s->zstream, (avctx->width * s->bits_per_pixel + 7) >> 3);
464     max_packet_size =
465         FF_MIN_BUFFER_SIZE + // headers
466         avctx->height * (
467             enc_row_size +
468             12 * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // IDAT * ceil(enc_row_size / IOBUF_SIZE)
469         );
470     if (max_packet_size > INT_MAX)
471         return AVERROR(ENOMEM);
472     ret = ff_alloc_packet2(avctx, pkt, max_packet_size);
473     if (ret < 0)
474         return ret;
475
476     s->bytestream_start =
477     s->bytestream       = pkt->data;
478     s->bytestream_end   = pkt->data + pkt->size;
479
480     AV_WB64(s->bytestream, PNGSIG);
481     s->bytestream += 8;
482
483     ret = encode_headers(avctx, pict);
484     if (ret < 0)
485         return ret;
486
487     ret = encode_frame(avctx, pict);
488     if (ret < 0)
489         return ret;
490
491     png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
492
493     pkt->size = s->bytestream - s->bytestream_start;
494     pkt->flags |= AV_PKT_FLAG_KEY;
495     *got_packet = 1;
496
497     return 0;
498 }
499
500 static av_cold int png_enc_init(AVCodecContext *avctx)
501 {
502     PNGEncContext *s = avctx->priv_data;
503     int compression_level;
504
505     switch (avctx->pix_fmt) {
506     case AV_PIX_FMT_RGBA:
507         avctx->bits_per_coded_sample = 32;
508         break;
509     case AV_PIX_FMT_RGB24:
510         avctx->bits_per_coded_sample = 24;
511         break;
512     case AV_PIX_FMT_GRAY8:
513         avctx->bits_per_coded_sample = 0x28;
514         break;
515     case AV_PIX_FMT_MONOBLACK:
516         avctx->bits_per_coded_sample = 1;
517         break;
518     case AV_PIX_FMT_PAL8:
519         avctx->bits_per_coded_sample = 8;
520     }
521
522     avctx->coded_frame = av_frame_alloc();
523     if (!avctx->coded_frame)
524         return AVERROR(ENOMEM);
525
526     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
527     avctx->coded_frame->key_frame = 1;
528
529     ff_huffyuvencdsp_init(&s->hdsp);
530
531     s->filter_type = av_clip(avctx->prediction_method,
532                              PNG_FILTER_VALUE_NONE,
533                              PNG_FILTER_VALUE_MIXED);
534     if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
535         s->filter_type = PNG_FILTER_VALUE_NONE;
536
537     if (s->dpi && s->dpm) {
538       av_log(avctx, AV_LOG_ERROR, "Only one of 'dpi' or 'dpm' options should be set\n");
539       return AVERROR(EINVAL);
540     } else if (s->dpi) {
541       s->dpm = s->dpi * 10000 / 254;
542     }
543
544     s->is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
545     switch (avctx->pix_fmt) {
546     case AV_PIX_FMT_RGBA64BE:
547         s->bit_depth = 16;
548         s->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
549         break;
550     case AV_PIX_FMT_RGB48BE:
551         s->bit_depth = 16;
552         s->color_type = PNG_COLOR_TYPE_RGB;
553         break;
554     case AV_PIX_FMT_RGBA:
555         s->bit_depth  = 8;
556         s->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
557         break;
558     case AV_PIX_FMT_RGB24:
559         s->bit_depth  = 8;
560         s->color_type = PNG_COLOR_TYPE_RGB;
561         break;
562     case AV_PIX_FMT_GRAY16BE:
563         s->bit_depth  = 16;
564         s->color_type = PNG_COLOR_TYPE_GRAY;
565         break;
566     case AV_PIX_FMT_GRAY8:
567         s->bit_depth  = 8;
568         s->color_type = PNG_COLOR_TYPE_GRAY;
569         break;
570     case AV_PIX_FMT_GRAY8A:
571         s->bit_depth = 8;
572         s->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
573         break;
574     case AV_PIX_FMT_YA16BE:
575         s->bit_depth = 16;
576         s->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
577         break;
578     case AV_PIX_FMT_MONOBLACK:
579         s->bit_depth  = 1;
580         s->color_type = PNG_COLOR_TYPE_GRAY;
581         break;
582     case AV_PIX_FMT_PAL8:
583         s->bit_depth  = 8;
584         s->color_type = PNG_COLOR_TYPE_PALETTE;
585         break;
586     default:
587         return -1;
588     }
589     s->bits_per_pixel = ff_png_get_nb_channels(s->color_type) * s->bit_depth;
590
591     s->zstream.zalloc = ff_png_zalloc;
592     s->zstream.zfree  = ff_png_zfree;
593     s->zstream.opaque = NULL;
594     compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
595                       ? Z_DEFAULT_COMPRESSION
596                       : av_clip(avctx->compression_level, 0, 9);
597     if (deflateInit2(&s->zstream, compression_level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY) != Z_OK)
598         return -1;
599
600     return 0;
601 }
602
603 static av_cold int png_enc_close(AVCodecContext *avctx)
604 {
605     PNGEncContext *s = avctx->priv_data;
606
607     deflateEnd(&s->zstream);
608     av_frame_free(&avctx->coded_frame);
609     return 0;
610 }
611
612 #define OFFSET(x) offsetof(PNGEncContext, x)
613 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
614 static const AVOption options[] = {
615     {"dpi", "Set image resolution (in dots per inch)",  OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE},
616     {"dpm", "Set image resolution (in dots per meter)", OFFSET(dpm), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0x10000, VE},
617     { NULL }
618 };
619
620 static const AVClass pngenc_class = {
621     .class_name = "PNG encoder",
622     .item_name  = av_default_item_name,
623     .option     = options,
624     .version    = LIBAVUTIL_VERSION_INT,
625 };
626
627 AVCodec ff_png_encoder = {
628     .name           = "png",
629     .long_name      = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
630     .type           = AVMEDIA_TYPE_VIDEO,
631     .id             = AV_CODEC_ID_PNG,
632     .priv_data_size = sizeof(PNGEncContext),
633     .init           = png_enc_init,
634     .close          = png_enc_close,
635     .encode2        = encode,
636     .capabilities   = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
637     .pix_fmts       = (const enum AVPixelFormat[]) {
638         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
639         AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
640         AV_PIX_FMT_PAL8,
641         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
642         AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE,
643         AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
644     },
645     .priv_class     = &pngenc_class,
646 };