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