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.

472 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. AVFrame picture;
  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, 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(DSPContext *dsp, 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. dsp->diff_bytes(dst, src, src-bpp, size);
  108. memcpy(dst, src, bpp);
  109. break;
  110. case PNG_FILTER_VALUE_UP:
  111. dsp->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->dsp, 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->dsp, 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 = (uint8_t *)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, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  200. s->zstream.avail_out = IOBUF_SIZE;
  201. s->zstream.next_out = s->buf;
  202. }
  203. }
  204. return 0;
  205. }
  206. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  207. const AVFrame *pict, int *got_packet)
  208. {
  209. PNGEncContext *s = avctx->priv_data;
  210. AVFrame * const p= &s->picture;
  211. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  212. int bits_per_pixel, pass_row_size, enc_row_size, max_packet_size;
  213. int compression_level;
  214. uint8_t *ptr, *top;
  215. uint8_t *crow_base = NULL, *crow_buf, *crow;
  216. uint8_t *progressive_buf = NULL;
  217. uint8_t *rgba_buf = NULL;
  218. uint8_t *top_buf = NULL;
  219. *p = *pict;
  220. p->pict_type= AV_PICTURE_TYPE_I;
  221. p->key_frame= 1;
  222. is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  223. switch(avctx->pix_fmt) {
  224. case AV_PIX_FMT_RGB32:
  225. bit_depth = 8;
  226. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  227. break;
  228. case AV_PIX_FMT_RGB24:
  229. bit_depth = 8;
  230. color_type = PNG_COLOR_TYPE_RGB;
  231. break;
  232. case AV_PIX_FMT_GRAY16BE:
  233. bit_depth = 16;
  234. color_type = PNG_COLOR_TYPE_GRAY;
  235. break;
  236. case AV_PIX_FMT_GRAY8:
  237. bit_depth = 8;
  238. color_type = PNG_COLOR_TYPE_GRAY;
  239. break;
  240. case AV_PIX_FMT_MONOBLACK:
  241. bit_depth = 1;
  242. color_type = PNG_COLOR_TYPE_GRAY;
  243. break;
  244. case AV_PIX_FMT_PAL8:
  245. bit_depth = 8;
  246. color_type = PNG_COLOR_TYPE_PALETTE;
  247. break;
  248. default:
  249. return -1;
  250. }
  251. bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
  252. row_size = (avctx->width * bits_per_pixel + 7) >> 3;
  253. s->zstream.zalloc = ff_png_zalloc;
  254. s->zstream.zfree = ff_png_zfree;
  255. s->zstream.opaque = NULL;
  256. compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
  257. Z_DEFAULT_COMPRESSION :
  258. av_clip(avctx->compression_level, 0, 9);
  259. ret = deflateInit2(&s->zstream, compression_level,
  260. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  261. if (ret != Z_OK)
  262. return -1;
  263. enc_row_size = deflateBound(&s->zstream, row_size);
  264. max_packet_size = avctx->height * (enc_row_size +
  265. ((enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) * 12)
  266. + FF_MIN_BUFFER_SIZE;
  267. if (!pkt->data &&
  268. (ret = av_new_packet(pkt, max_packet_size)) < 0) {
  269. av_log(avctx, AV_LOG_ERROR, "Could not allocate output packet of size %d.\n",
  270. max_packet_size);
  271. return ret;
  272. }
  273. s->bytestream_start =
  274. s->bytestream = pkt->data;
  275. s->bytestream_end = pkt->data + pkt->size;
  276. crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
  277. if (!crow_base)
  278. goto fail;
  279. crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
  280. if (is_progressive) {
  281. progressive_buf = av_malloc(row_size + 1);
  282. if (!progressive_buf)
  283. goto fail;
  284. }
  285. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  286. rgba_buf = av_malloc(row_size + 1);
  287. if (!rgba_buf)
  288. goto fail;
  289. }
  290. if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  291. top_buf = av_malloc(row_size + 1);
  292. if (!top_buf)
  293. goto fail;
  294. }
  295. /* write png header */
  296. memcpy(s->bytestream, ff_pngsig, 8);
  297. s->bytestream += 8;
  298. AV_WB32(s->buf, avctx->width);
  299. AV_WB32(s->buf + 4, avctx->height);
  300. s->buf[8] = bit_depth;
  301. s->buf[9] = color_type;
  302. s->buf[10] = 0; /* compression type */
  303. s->buf[11] = 0; /* filter type */
  304. s->buf[12] = is_progressive; /* interlace type */
  305. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  306. /* put the palette if needed */
  307. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  308. int has_alpha, alpha, i;
  309. unsigned int v;
  310. uint32_t *palette;
  311. uint8_t *alpha_ptr;
  312. palette = (uint32_t *)p->data[1];
  313. ptr = s->buf;
  314. alpha_ptr = s->buf + 256 * 3;
  315. has_alpha = 0;
  316. for(i = 0; i < 256; i++) {
  317. v = palette[i];
  318. alpha = v >> 24;
  319. if (alpha && alpha != 0xff)
  320. has_alpha = 1;
  321. *alpha_ptr++ = alpha;
  322. bytestream_put_be24(&ptr, v);
  323. }
  324. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  325. if (has_alpha) {
  326. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  327. }
  328. }
  329. /* now put each row */
  330. s->zstream.avail_out = IOBUF_SIZE;
  331. s->zstream.next_out = s->buf;
  332. if (is_progressive) {
  333. int pass;
  334. for(pass = 0; pass < NB_PASSES; pass++) {
  335. /* NOTE: a pass is completely omitted if no pixels would be
  336. output */
  337. pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
  338. if (pass_row_size > 0) {
  339. top = NULL;
  340. for(y = 0; y < avctx->height; y++) {
  341. if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
  342. ptr = p->data[0] + y * p->linesize[0];
  343. FFSWAP(uint8_t*, progressive_buf, top_buf);
  344. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  345. convert_from_rgb32(rgba_buf, ptr, avctx->width);
  346. ptr = rgba_buf;
  347. }
  348. png_get_interlaced_row(progressive_buf, pass_row_size,
  349. bits_per_pixel, pass,
  350. ptr, avctx->width);
  351. crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
  352. png_write_row(s, crow, pass_row_size + 1);
  353. top = progressive_buf;
  354. }
  355. }
  356. }
  357. }
  358. } else {
  359. top = NULL;
  360. for(y = 0; y < avctx->height; y++) {
  361. ptr = p->data[0] + y * p->linesize[0];
  362. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  363. FFSWAP(uint8_t*, rgba_buf, top_buf);
  364. convert_from_rgb32(rgba_buf, ptr, avctx->width);
  365. ptr = rgba_buf;
  366. }
  367. crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
  368. png_write_row(s, crow, row_size + 1);
  369. top = ptr;
  370. }
  371. }
  372. /* compress last bytes */
  373. for(;;) {
  374. ret = deflate(&s->zstream, Z_FINISH);
  375. if (ret == Z_OK || ret == Z_STREAM_END) {
  376. len = IOBUF_SIZE - s->zstream.avail_out;
  377. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  378. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  379. }
  380. s->zstream.avail_out = IOBUF_SIZE;
  381. s->zstream.next_out = s->buf;
  382. if (ret == Z_STREAM_END)
  383. break;
  384. } else {
  385. goto fail;
  386. }
  387. }
  388. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  389. pkt->size = s->bytestream - s->bytestream_start;
  390. pkt->flags |= AV_PKT_FLAG_KEY;
  391. *got_packet = 1;
  392. ret = 0;
  393. the_end:
  394. av_free(crow_base);
  395. av_free(progressive_buf);
  396. av_free(rgba_buf);
  397. av_free(top_buf);
  398. deflateEnd(&s->zstream);
  399. return ret;
  400. fail:
  401. ret = -1;
  402. goto the_end;
  403. }
  404. static av_cold int png_enc_init(AVCodecContext *avctx){
  405. PNGEncContext *s = avctx->priv_data;
  406. avcodec_get_frame_defaults(&s->picture);
  407. avctx->coded_frame= &s->picture;
  408. ff_dsputil_init(&s->dsp, avctx);
  409. s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
  410. if(avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
  411. s->filter_type = PNG_FILTER_VALUE_NONE;
  412. return 0;
  413. }
  414. AVCodec ff_png_encoder = {
  415. .name = "png",
  416. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  417. .type = AVMEDIA_TYPE_VIDEO,
  418. .id = AV_CODEC_ID_PNG,
  419. .priv_data_size = sizeof(PNGEncContext),
  420. .init = png_enc_init,
  421. .encode2 = encode_frame,
  422. .pix_fmts = (const enum AVPixelFormat[]){
  423. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
  424. AV_PIX_FMT_GRAY16BE,
  425. AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE
  426. },
  427. };