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.

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