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.

541 lines
17KB

  1. /*
  2. * PNG image format
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/opt.h"
  22. #include "libavutil/stereo3d.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "huffyuvencdsp.h"
  26. #include "png.h"
  27. /* TODO:
  28. * - add 2, 4 and 16 bit depth support
  29. */
  30. #include <zlib.h>
  31. #define IOBUF_SIZE 4096
  32. typedef struct PNGEncContext {
  33. AVClass *class;
  34. HuffYUVEncDSPContext hdsp;
  35. uint8_t *bytestream;
  36. uint8_t *bytestream_start;
  37. uint8_t *bytestream_end;
  38. int filter_type;
  39. z_stream zstream;
  40. uint8_t buf[IOBUF_SIZE];
  41. } PNGEncContext;
  42. static void png_get_interlaced_row(uint8_t *dst, int row_size,
  43. int bits_per_pixel, int pass,
  44. const uint8_t *src, int width)
  45. {
  46. int x, mask, dst_x, j, b, bpp;
  47. uint8_t *d;
  48. const uint8_t *s;
  49. mask = ff_png_pass_mask[pass];
  50. switch (bits_per_pixel) {
  51. case 1:
  52. memset(dst, 0, row_size);
  53. dst_x = 0;
  54. for (x = 0; x < width; x++) {
  55. j = (x & 7);
  56. if ((mask << j) & 0x80) {
  57. b = (src[x >> 3] >> (7 - j)) & 1;
  58. dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
  59. dst_x++;
  60. }
  61. }
  62. break;
  63. default:
  64. bpp = bits_per_pixel >> 3;
  65. d = dst;
  66. s = src;
  67. for (x = 0; x < width; x++) {
  68. j = x & 7;
  69. if ((mask << j) & 0x80) {
  70. memcpy(d, s, bpp);
  71. d += bpp;
  72. }
  73. s += bpp;
  74. }
  75. break;
  76. }
  77. }
  78. static void sub_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
  79. int w, int bpp)
  80. {
  81. int i;
  82. for (i = 0; i < w; i++) {
  83. int a, b, c, p, pa, pb, pc;
  84. a = src[i - bpp];
  85. b = top[i];
  86. c = top[i - bpp];
  87. p = b - c;
  88. pc = a - c;
  89. pa = abs(p);
  90. pb = abs(pc);
  91. pc = abs(p + pc);
  92. if (pa <= pb && pa <= pc)
  93. p = a;
  94. else if (pb <= pc)
  95. p = b;
  96. else
  97. p = c;
  98. dst[i] = src[i] - p;
  99. }
  100. }
  101. static void png_filter_row(PNGEncContext *c, uint8_t *dst, int filter_type,
  102. uint8_t *src, uint8_t *top, int size, int bpp)
  103. {
  104. int i;
  105. switch (filter_type) {
  106. case PNG_FILTER_VALUE_NONE:
  107. memcpy(dst, src, size);
  108. break;
  109. case PNG_FILTER_VALUE_SUB:
  110. c->hdsp.diff_bytes(dst, src, src - bpp, size);
  111. memcpy(dst, src, bpp);
  112. break;
  113. case PNG_FILTER_VALUE_UP:
  114. c->hdsp.diff_bytes(dst, src, top, size);
  115. break;
  116. case PNG_FILTER_VALUE_AVG:
  117. for (i = 0; i < bpp; i++)
  118. dst[i] = src[i] - (top[i] >> 1);
  119. for (; i < size; i++)
  120. dst[i] = src[i] - ((src[i - bpp] + top[i]) >> 1);
  121. break;
  122. case PNG_FILTER_VALUE_PAETH:
  123. for (i = 0; i < bpp; i++)
  124. dst[i] = src[i] - top[i];
  125. sub_png_paeth_prediction(dst + i, src + i, top + i, size - i, bpp);
  126. break;
  127. }
  128. }
  129. static uint8_t *png_choose_filter(PNGEncContext *s, uint8_t *dst,
  130. uint8_t *src, uint8_t *top, int size, int bpp)
  131. {
  132. int pred = s->filter_type;
  133. assert(bpp || !pred);
  134. if (!top && pred)
  135. pred = PNG_FILTER_VALUE_SUB;
  136. if (pred == PNG_FILTER_VALUE_MIXED) {
  137. int i;
  138. int cost, bcost = INT_MAX;
  139. uint8_t *buf1 = dst, *buf2 = dst + size + 16;
  140. for (pred = 0; pred < 5; pred++) {
  141. png_filter_row(s, buf1 + 1, pred, src, top, size, bpp);
  142. buf1[0] = pred;
  143. cost = 0;
  144. for (i = 0; i <= size; i++)
  145. cost += abs((int8_t) buf1[i]);
  146. if (cost < bcost) {
  147. bcost = cost;
  148. FFSWAP(uint8_t *, buf1, buf2);
  149. }
  150. }
  151. return buf2;
  152. } else {
  153. png_filter_row(s, dst + 1, pred, src, top, size, bpp);
  154. dst[0] = pred;
  155. return dst;
  156. }
  157. }
  158. static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
  159. {
  160. uint8_t *d;
  161. int j;
  162. unsigned int v;
  163. d = dst;
  164. for (j = 0; j < width; j++) {
  165. v = ((const uint32_t *) src)[j];
  166. d[0] = v >> 16;
  167. d[1] = v >> 8;
  168. d[2] = v;
  169. d[3] = v >> 24;
  170. d += 4;
  171. }
  172. }
  173. static void png_write_chunk(uint8_t **f, uint32_t tag,
  174. const uint8_t *buf, int length)
  175. {
  176. uint32_t crc;
  177. uint8_t tagbuf[4];
  178. bytestream_put_be32(f, length);
  179. crc = crc32(0, Z_NULL, 0);
  180. AV_WL32(tagbuf, tag);
  181. crc = crc32(crc, tagbuf, 4);
  182. bytestream_put_be32(f, av_bswap32(tag));
  183. if (length > 0) {
  184. crc = crc32(crc, buf, length);
  185. memcpy(*f, buf, length);
  186. *f += length;
  187. }
  188. bytestream_put_be32(f, crc);
  189. }
  190. /* XXX: do filtering */
  191. static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
  192. {
  193. int ret;
  194. s->zstream.avail_in = size;
  195. s->zstream.next_in = data;
  196. while (s->zstream.avail_in > 0) {
  197. ret = deflate(&s->zstream, Z_NO_FLUSH);
  198. if (ret != Z_OK)
  199. return -1;
  200. if (s->zstream.avail_out == 0) {
  201. if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
  202. png_write_chunk(&s->bytestream,
  203. MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  204. s->zstream.avail_out = IOBUF_SIZE;
  205. s->zstream.next_out = s->buf;
  206. }
  207. }
  208. return 0;
  209. }
  210. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  211. const AVFrame *pict, int *got_packet)
  212. {
  213. PNGEncContext *s = avctx->priv_data;
  214. AVFrameSideData *side_data;
  215. const AVFrame *const p = pict;
  216. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  217. int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
  218. int compression_level;
  219. uint8_t *ptr, *top, *crow_buf, *crow;
  220. uint8_t *crow_base = NULL;
  221. uint8_t *progressive_buf = NULL;
  222. uint8_t *rgba_buf = NULL;
  223. uint8_t *top_buf = NULL;
  224. is_progressive = !!(avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT);
  225. switch (avctx->pix_fmt) {
  226. case AV_PIX_FMT_RGBA64BE:
  227. bit_depth = 16;
  228. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  229. break;
  230. case AV_PIX_FMT_RGB48BE:
  231. bit_depth = 16;
  232. color_type = PNG_COLOR_TYPE_RGB;
  233. break;
  234. case AV_PIX_FMT_RGB32:
  235. bit_depth = 8;
  236. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  237. break;
  238. case AV_PIX_FMT_RGB24:
  239. bit_depth = 8;
  240. color_type = PNG_COLOR_TYPE_RGB;
  241. break;
  242. case AV_PIX_FMT_GRAY16BE:
  243. bit_depth = 16;
  244. color_type = PNG_COLOR_TYPE_GRAY;
  245. break;
  246. case AV_PIX_FMT_GRAY8:
  247. bit_depth = 8;
  248. color_type = PNG_COLOR_TYPE_GRAY;
  249. break;
  250. case AV_PIX_FMT_MONOBLACK:
  251. bit_depth = 1;
  252. color_type = PNG_COLOR_TYPE_GRAY;
  253. break;
  254. case AV_PIX_FMT_PAL8:
  255. bit_depth = 8;
  256. color_type = PNG_COLOR_TYPE_PALETTE;
  257. break;
  258. default:
  259. return -1;
  260. }
  261. bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
  262. row_size = (avctx->width * bits_per_pixel + 7) >> 3;
  263. s->zstream.zalloc = ff_png_zalloc;
  264. s->zstream.zfree = ff_png_zfree;
  265. s->zstream.opaque = NULL;
  266. compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
  267. ? Z_DEFAULT_COMPRESSION
  268. : av_clip(avctx->compression_level, 0, 9);
  269. ret = deflateInit2(&s->zstream, compression_level,
  270. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  271. if (ret != Z_OK)
  272. return -1;
  273. enc_row_size = deflateBound(&s->zstream, row_size);
  274. max_packet_size = avctx->height * (enc_row_size +
  275. ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
  276. + AV_INPUT_BUFFER_MIN_SIZE;
  277. if (!pkt->data &&
  278. (ret = av_new_packet(pkt, max_packet_size)) < 0) {
  279. av_log(avctx, AV_LOG_ERROR, "Could not allocate output packet of size %d.\n",
  280. max_packet_size);
  281. return ret;
  282. }
  283. s->bytestream_start =
  284. s->bytestream = pkt->data;
  285. s->bytestream_end = pkt->data + pkt->size;
  286. crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
  287. if (!crow_base)
  288. goto fail;
  289. // pixel data should be aligned, but there's a control byte before it
  290. crow_buf = crow_base + 15;
  291. if (is_progressive) {
  292. progressive_buf = av_malloc(row_size + 1);
  293. if (!progressive_buf)
  294. goto fail;
  295. }
  296. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  297. rgba_buf = av_malloc(row_size + 1);
  298. if (!rgba_buf)
  299. goto fail;
  300. }
  301. if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  302. top_buf = av_malloc(row_size + 1);
  303. if (!top_buf)
  304. goto fail;
  305. }
  306. /* write png header */
  307. memcpy(s->bytestream, ff_pngsig, 8);
  308. s->bytestream += 8;
  309. AV_WB32(s->buf, avctx->width);
  310. AV_WB32(s->buf + 4, avctx->height);
  311. s->buf[8] = bit_depth;
  312. s->buf[9] = color_type;
  313. s->buf[10] = 0; /* compression type */
  314. s->buf[11] = 0; /* filter type */
  315. s->buf[12] = is_progressive; /* interlace type */
  316. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  317. /* put the palette if needed */
  318. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  319. int has_alpha, alpha, i;
  320. unsigned int v;
  321. uint32_t *palette;
  322. uint8_t *alpha_ptr;
  323. palette = (uint32_t *)p->data[1];
  324. ptr = s->buf;
  325. alpha_ptr = s->buf + 256 * 3;
  326. has_alpha = 0;
  327. for (i = 0; i < 256; i++) {
  328. v = palette[i];
  329. alpha = v >> 24;
  330. if (alpha && alpha != 0xff)
  331. has_alpha = 1;
  332. *alpha_ptr++ = alpha;
  333. bytestream_put_be24(&ptr, v);
  334. }
  335. png_write_chunk(&s->bytestream,
  336. MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  337. if (has_alpha) {
  338. png_write_chunk(&s->bytestream,
  339. MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  340. }
  341. }
  342. /* write stereoscopic information */
  343. side_data = av_frame_get_side_data(pict, AV_FRAME_DATA_STEREO3D);
  344. if (side_data) {
  345. AVStereo3D *stereo3d = (AVStereo3D *)side_data->data;
  346. uint8_t sm;
  347. switch (stereo3d->type) {
  348. case AV_STEREO3D_SIDEBYSIDE:
  349. sm = !(stereo3d->flags & AV_STEREO3D_FLAG_INVERT);
  350. png_write_chunk(&s->bytestream, MKTAG('s', 'T', 'E', 'R'), &sm, 1);
  351. break;
  352. case AV_STEREO3D_2D:
  353. break;
  354. default:
  355. av_log(avctx, AV_LOG_WARNING,
  356. "Only side-by-side stereo3d flag can be defined within sTER chunk\n");
  357. break;
  358. }
  359. }
  360. /* now put each row */
  361. s->zstream.avail_out = IOBUF_SIZE;
  362. s->zstream.next_out = s->buf;
  363. if (is_progressive) {
  364. int pass;
  365. for (pass = 0; pass < NB_PASSES; pass++) {
  366. /* NOTE: a pass is completely omitted if no pixels would be
  367. * output */
  368. pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
  369. if (pass_row_size > 0) {
  370. top = NULL;
  371. for (y = 0; y < avctx->height; y++)
  372. if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
  373. ptr = p->data[0] + y * p->linesize[0];
  374. FFSWAP(uint8_t *, progressive_buf, top_buf);
  375. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  376. convert_from_rgb32(rgba_buf, ptr, avctx->width);
  377. ptr = rgba_buf;
  378. }
  379. png_get_interlaced_row(progressive_buf, pass_row_size,
  380. bits_per_pixel, pass,
  381. ptr, avctx->width);
  382. crow = png_choose_filter(s, crow_buf, progressive_buf,
  383. top, pass_row_size, bits_per_pixel >> 3);
  384. png_write_row(s, crow, pass_row_size + 1);
  385. top = progressive_buf;
  386. }
  387. }
  388. }
  389. } else {
  390. top = NULL;
  391. for (y = 0; y < avctx->height; y++) {
  392. ptr = p->data[0] + y * p->linesize[0];
  393. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  394. FFSWAP(uint8_t *, rgba_buf, top_buf);
  395. convert_from_rgb32(rgba_buf, ptr, avctx->width);
  396. ptr = rgba_buf;
  397. }
  398. crow = png_choose_filter(s, crow_buf, ptr, top,
  399. row_size, bits_per_pixel >> 3);
  400. png_write_row(s, crow, row_size + 1);
  401. top = ptr;
  402. }
  403. }
  404. /* compress last bytes */
  405. for (;;) {
  406. ret = deflate(&s->zstream, Z_FINISH);
  407. if (ret == Z_OK || ret == Z_STREAM_END) {
  408. len = IOBUF_SIZE - s->zstream.avail_out;
  409. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  410. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  411. }
  412. s->zstream.avail_out = IOBUF_SIZE;
  413. s->zstream.next_out = s->buf;
  414. if (ret == Z_STREAM_END)
  415. break;
  416. } else {
  417. goto fail;
  418. }
  419. }
  420. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  421. pkt->size = s->bytestream - s->bytestream_start;
  422. pkt->flags |= AV_PKT_FLAG_KEY;
  423. *got_packet = 1;
  424. ret = 0;
  425. the_end:
  426. av_free(crow_base);
  427. av_free(progressive_buf);
  428. av_free(rgba_buf);
  429. av_free(top_buf);
  430. deflateEnd(&s->zstream);
  431. return ret;
  432. fail:
  433. ret = -1;
  434. goto the_end;
  435. }
  436. static av_cold int png_enc_init(AVCodecContext *avctx)
  437. {
  438. PNGEncContext *s = avctx->priv_data;
  439. #if FF_API_CODED_FRAME
  440. FF_DISABLE_DEPRECATION_WARNINGS
  441. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  442. avctx->coded_frame->key_frame = 1;
  443. FF_ENABLE_DEPRECATION_WARNINGS
  444. #endif
  445. ff_huffyuvencdsp_init(&s->hdsp);
  446. #if FF_API_PRIVATE_OPT
  447. FF_DISABLE_DEPRECATION_WARNINGS
  448. if (avctx->prediction_method)
  449. s->filter_type = av_clip(avctx->prediction_method,
  450. PNG_FILTER_VALUE_NONE,
  451. PNG_FILTER_VALUE_MIXED);
  452. FF_ENABLE_DEPRECATION_WARNINGS
  453. #endif
  454. if (avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
  455. s->filter_type = PNG_FILTER_VALUE_NONE;
  456. return 0;
  457. }
  458. #define OFFSET(x) offsetof(PNGEncContext, x)
  459. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  460. static const AVOption options[] = {
  461. { "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" },
  462. { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_NONE }, INT_MIN, INT_MAX, VE, "pred" },
  463. { "sub", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_SUB }, INT_MIN, INT_MAX, VE, "pred" },
  464. { "up", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_UP }, INT_MIN, INT_MAX, VE, "pred" },
  465. { "avg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_AVG }, INT_MIN, INT_MAX, VE, "pred" },
  466. { "paeth", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_PAETH }, INT_MIN, INT_MAX, VE, "pred" },
  467. { "mixed", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PNG_FILTER_VALUE_MIXED }, INT_MIN, INT_MAX, VE, "pred" },
  468. { NULL},
  469. };
  470. static const AVClass png_class = {
  471. .class_name = "png",
  472. .item_name = av_default_item_name,
  473. .option = options,
  474. .version = LIBAVUTIL_VERSION_INT,
  475. };
  476. AVCodec ff_png_encoder = {
  477. .name = "png",
  478. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  479. .type = AVMEDIA_TYPE_VIDEO,
  480. .id = AV_CODEC_ID_PNG,
  481. .priv_data_size = sizeof(PNGEncContext),
  482. .priv_class = &png_class,
  483. .init = png_enc_init,
  484. .encode2 = encode_frame,
  485. .pix_fmts = (const enum AVPixelFormat[]) {
  486. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
  487. AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_GRAY16BE,
  488. AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
  489. },
  490. };