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.

437 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 = (int[]){0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}[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, unsigned char *buf, int buf_size, void *data){
  193. PNGEncContext *s = avctx->priv_data;
  194. AVFrame *pict = data;
  195. AVFrame * const p= &s->picture;
  196. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  197. int bits_per_pixel, pass_row_size;
  198. int compression_level;
  199. uint8_t *ptr, *top;
  200. uint8_t *crow_base = NULL, *crow_buf, *crow;
  201. uint8_t *progressive_buf = NULL;
  202. uint8_t *top_buf = NULL;
  203. *p = *pict;
  204. p->pict_type= AV_PICTURE_TYPE_I;
  205. p->key_frame= 1;
  206. s->bytestream_start=
  207. s->bytestream= buf;
  208. s->bytestream_end= buf+buf_size;
  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. crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
  263. if (!crow_base)
  264. goto fail;
  265. crow_buf = crow_base + 15; // pixel data should be aligned, but there's a control byte before it
  266. if (is_progressive) {
  267. progressive_buf = av_malloc(row_size + 1);
  268. if (!progressive_buf)
  269. goto fail;
  270. }
  271. if (is_progressive) {
  272. top_buf = av_malloc(row_size + 1);
  273. if (!top_buf)
  274. goto fail;
  275. }
  276. /* write png header */
  277. memcpy(s->bytestream, ff_pngsig, 8);
  278. s->bytestream += 8;
  279. AV_WB32(s->buf, avctx->width);
  280. AV_WB32(s->buf + 4, avctx->height);
  281. s->buf[8] = bit_depth;
  282. s->buf[9] = color_type;
  283. s->buf[10] = 0; /* compression type */
  284. s->buf[11] = 0; /* filter type */
  285. s->buf[12] = is_progressive; /* interlace type */
  286. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  287. /* put the palette if needed */
  288. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  289. int has_alpha, alpha, i;
  290. unsigned int v;
  291. uint32_t *palette;
  292. uint8_t *alpha_ptr;
  293. palette = (uint32_t *)p->data[1];
  294. ptr = s->buf;
  295. alpha_ptr = s->buf + 256 * 3;
  296. has_alpha = 0;
  297. for(i = 0; i < 256; i++) {
  298. v = palette[i];
  299. alpha = v >> 24;
  300. if (alpha != 0xff)
  301. has_alpha = 1;
  302. *alpha_ptr++ = alpha;
  303. bytestream_put_be24(&ptr, v);
  304. }
  305. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  306. if (has_alpha) {
  307. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  308. }
  309. }
  310. /* now put each row */
  311. s->zstream.avail_out = IOBUF_SIZE;
  312. s->zstream.next_out = s->buf;
  313. if (is_progressive) {
  314. int pass;
  315. for(pass = 0; pass < NB_PASSES; pass++) {
  316. /* NOTE: a pass is completely omited if no pixels would be
  317. output */
  318. pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
  319. if (pass_row_size > 0) {
  320. top = NULL;
  321. for(y = 0; y < avctx->height; y++) {
  322. if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
  323. ptr = p->data[0] + y * p->linesize[0];
  324. FFSWAP(uint8_t*, progressive_buf, top_buf);
  325. png_get_interlaced_row(progressive_buf, pass_row_size,
  326. bits_per_pixel, pass,
  327. ptr, avctx->width);
  328. crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
  329. png_write_row(s, crow, pass_row_size + 1);
  330. top = progressive_buf;
  331. }
  332. }
  333. }
  334. }
  335. } else {
  336. top = NULL;
  337. for(y = 0; y < avctx->height; y++) {
  338. ptr = p->data[0] + y * p->linesize[0];
  339. crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
  340. png_write_row(s, crow, row_size + 1);
  341. top = ptr;
  342. }
  343. }
  344. /* compress last bytes */
  345. for(;;) {
  346. ret = deflate(&s->zstream, Z_FINISH);
  347. if (ret == Z_OK || ret == Z_STREAM_END) {
  348. len = IOBUF_SIZE - s->zstream.avail_out;
  349. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  350. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  351. }
  352. s->zstream.avail_out = IOBUF_SIZE;
  353. s->zstream.next_out = s->buf;
  354. if (ret == Z_STREAM_END)
  355. break;
  356. } else {
  357. goto fail;
  358. }
  359. }
  360. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  361. ret = s->bytestream - s->bytestream_start;
  362. the_end:
  363. av_free(crow_base);
  364. av_free(progressive_buf);
  365. av_free(top_buf);
  366. deflateEnd(&s->zstream);
  367. return ret;
  368. fail:
  369. ret = -1;
  370. goto the_end;
  371. }
  372. static av_cold int png_enc_init(AVCodecContext *avctx){
  373. PNGEncContext *s = avctx->priv_data;
  374. avcodec_get_frame_defaults(&s->picture);
  375. avctx->coded_frame= &s->picture;
  376. dsputil_init(&s->dsp, avctx);
  377. s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
  378. if(avctx->pix_fmt == PIX_FMT_MONOBLACK)
  379. s->filter_type = PNG_FILTER_VALUE_NONE;
  380. return 0;
  381. }
  382. AVCodec ff_png_encoder = {
  383. .name = "png",
  384. .type = AVMEDIA_TYPE_VIDEO,
  385. .id = CODEC_ID_PNG,
  386. .priv_data_size = sizeof(PNGEncContext),
  387. .init = png_enc_init,
  388. .encode = encode_frame,
  389. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA,
  390. PIX_FMT_RGB48BE, PIX_FMT_RGBA64BE,
  391. PIX_FMT_PAL8,
  392. PIX_FMT_GRAY8, PIX_FMT_GRAY8A,
  393. PIX_FMT_GRAY16BE,
  394. PIX_FMT_MONOBLACK, PIX_FMT_NONE},
  395. .long_name= NULL_IF_CONFIG_SMALL("PNG image"),
  396. };