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.

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