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.

512 lines
15KB

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