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.

450 lines
13KB

  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 "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 png_write_chunk(uint8_t **f, uint32_t tag,
  157. const uint8_t *buf, int length)
  158. {
  159. uint32_t crc;
  160. uint8_t tagbuf[4];
  161. bytestream_put_be32(f, length);
  162. crc = crc32(0, Z_NULL, 0);
  163. AV_WL32(tagbuf, tag);
  164. crc = crc32(crc, tagbuf, 4);
  165. bytestream_put_be32(f, av_bswap32(tag));
  166. if (length > 0) {
  167. crc = crc32(crc, buf, length);
  168. memcpy(*f, buf, length);
  169. *f += length;
  170. }
  171. bytestream_put_be32(f, crc);
  172. }
  173. /* XXX: do filtering */
  174. static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
  175. {
  176. int ret;
  177. s->zstream.avail_in = size;
  178. s->zstream.next_in = (uint8_t *)data;
  179. while (s->zstream.avail_in > 0) {
  180. ret = deflate(&s->zstream, Z_NO_FLUSH);
  181. if (ret != Z_OK)
  182. return -1;
  183. if (s->zstream.avail_out == 0) {
  184. if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
  185. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  186. s->zstream.avail_out = IOBUF_SIZE;
  187. s->zstream.next_out = s->buf;
  188. }
  189. }
  190. return 0;
  191. }
  192. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  193. const AVFrame *pict, int *got_packet)
  194. {
  195. PNGEncContext *s = avctx->priv_data;
  196. AVFrame * const p= &s->picture;
  197. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  198. int bits_per_pixel, pass_row_size, max_packet_size;
  199. int compression_level;
  200. uint8_t *ptr, *top;
  201. uint8_t *crow_base = NULL, *crow_buf, *crow;
  202. uint8_t *progressive_buf = NULL;
  203. uint8_t *top_buf = NULL;
  204. *p = *pict;
  205. p->pict_type= AV_PICTURE_TYPE_I;
  206. p->key_frame= 1;
  207. max_packet_size = IOBUF_SIZE*avctx->height + FF_MIN_BUFFER_SIZE;
  208. if (!pkt->data &&
  209. (ret = av_new_packet(pkt, max_packet_size)) < 0) {
  210. av_log(avctx, AV_LOG_ERROR, "Could not allocate output packet of size %d.\n",
  211. max_packet_size);
  212. return ret;
  213. }
  214. s->bytestream_start =
  215. s->bytestream = pkt->data;
  216. s->bytestream_end = pkt->data + pkt->size;
  217. is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  218. switch(avctx->pix_fmt) {
  219. case PIX_FMT_RGBA64BE:
  220. bit_depth = 16;
  221. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  222. break;
  223. case PIX_FMT_RGB48BE:
  224. bit_depth = 16;
  225. color_type = PNG_COLOR_TYPE_RGB;
  226. break;
  227. case PIX_FMT_RGBA:
  228. bit_depth = 8;
  229. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  230. break;
  231. case PIX_FMT_RGB24:
  232. bit_depth = 8;
  233. color_type = PNG_COLOR_TYPE_RGB;
  234. break;
  235. case PIX_FMT_GRAY16BE:
  236. bit_depth = 16;
  237. color_type = PNG_COLOR_TYPE_GRAY;
  238. break;
  239. case PIX_FMT_GRAY8:
  240. bit_depth = 8;
  241. color_type = PNG_COLOR_TYPE_GRAY;
  242. break;
  243. case PIX_FMT_GRAY8A:
  244. bit_depth = 8;
  245. color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  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 (is_progressive) {
  280. top_buf = av_malloc(row_size + 1);
  281. if (!top_buf)
  282. goto fail;
  283. }
  284. /* write png header */
  285. memcpy(s->bytestream, ff_pngsig, 8);
  286. s->bytestream += 8;
  287. AV_WB32(s->buf, avctx->width);
  288. AV_WB32(s->buf + 4, avctx->height);
  289. s->buf[8] = bit_depth;
  290. s->buf[9] = color_type;
  291. s->buf[10] = 0; /* compression type */
  292. s->buf[11] = 0; /* filter type */
  293. s->buf[12] = is_progressive; /* interlace type */
  294. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  295. /* put the palette if needed */
  296. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  297. int has_alpha, alpha, i;
  298. unsigned int v;
  299. uint32_t *palette;
  300. uint8_t *alpha_ptr;
  301. palette = (uint32_t *)p->data[1];
  302. ptr = s->buf;
  303. alpha_ptr = s->buf + 256 * 3;
  304. has_alpha = 0;
  305. for(i = 0; i < 256; i++) {
  306. v = palette[i];
  307. alpha = v >> 24;
  308. if (alpha != 0xff)
  309. has_alpha = 1;
  310. *alpha_ptr++ = alpha;
  311. bytestream_put_be24(&ptr, v);
  312. }
  313. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  314. if (has_alpha) {
  315. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  316. }
  317. }
  318. /* now put each row */
  319. s->zstream.avail_out = IOBUF_SIZE;
  320. s->zstream.next_out = s->buf;
  321. if (is_progressive) {
  322. int pass;
  323. for(pass = 0; pass < NB_PASSES; pass++) {
  324. /* NOTE: a pass is completely omited if no pixels would be
  325. output */
  326. pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
  327. if (pass_row_size > 0) {
  328. top = NULL;
  329. for(y = 0; y < avctx->height; y++) {
  330. if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
  331. ptr = p->data[0] + y * p->linesize[0];
  332. FFSWAP(uint8_t*, progressive_buf, top_buf);
  333. png_get_interlaced_row(progressive_buf, pass_row_size,
  334. bits_per_pixel, pass,
  335. ptr, avctx->width);
  336. crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
  337. png_write_row(s, crow, pass_row_size + 1);
  338. top = progressive_buf;
  339. }
  340. }
  341. }
  342. }
  343. } else {
  344. top = NULL;
  345. for(y = 0; y < avctx->height; y++) {
  346. ptr = p->data[0] + y * p->linesize[0];
  347. crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
  348. png_write_row(s, crow, row_size + 1);
  349. top = ptr;
  350. }
  351. }
  352. /* compress last bytes */
  353. for(;;) {
  354. ret = deflate(&s->zstream, Z_FINISH);
  355. if (ret == Z_OK || ret == Z_STREAM_END) {
  356. len = IOBUF_SIZE - s->zstream.avail_out;
  357. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  358. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  359. }
  360. s->zstream.avail_out = IOBUF_SIZE;
  361. s->zstream.next_out = s->buf;
  362. if (ret == Z_STREAM_END)
  363. break;
  364. } else {
  365. goto fail;
  366. }
  367. }
  368. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  369. pkt->size = s->bytestream - s->bytestream_start;
  370. pkt->flags |= AV_PKT_FLAG_KEY;
  371. *got_packet = 1;
  372. ret = 0;
  373. the_end:
  374. av_free(crow_base);
  375. av_free(progressive_buf);
  376. av_free(top_buf);
  377. deflateEnd(&s->zstream);
  378. return ret;
  379. fail:
  380. ret = -1;
  381. goto the_end;
  382. }
  383. static av_cold int png_enc_init(AVCodecContext *avctx){
  384. PNGEncContext *s = avctx->priv_data;
  385. avcodec_get_frame_defaults(&s->picture);
  386. avctx->coded_frame= &s->picture;
  387. ff_dsputil_init(&s->dsp, avctx);
  388. s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
  389. if(avctx->pix_fmt == PIX_FMT_MONOBLACK)
  390. s->filter_type = PNG_FILTER_VALUE_NONE;
  391. return 0;
  392. }
  393. AVCodec ff_png_encoder = {
  394. .name = "png",
  395. .type = AVMEDIA_TYPE_VIDEO,
  396. .id = CODEC_ID_PNG,
  397. .priv_data_size = sizeof(PNGEncContext),
  398. .init = png_enc_init,
  399. .encode2 = encode_frame,
  400. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA,
  401. PIX_FMT_RGB48BE, PIX_FMT_RGBA64BE,
  402. PIX_FMT_PAL8,
  403. PIX_FMT_GRAY8, PIX_FMT_GRAY8A,
  404. PIX_FMT_GRAY16BE,
  405. PIX_FMT_MONOBLACK, PIX_FMT_NONE},
  406. .long_name= NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  407. };