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.

479 lines
14KB

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