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.

456 lines
14KB

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