You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1181 lines
39KB

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