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.

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