pngenc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 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, bswap_32(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, unsigned char *buf, int buf_size, void *data){
  208. PNGEncContext *s = avctx->priv_data;
  209. AVFrame *pict = data;
  210. AVFrame * const p= &s->picture;
  211. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  212. int bits_per_pixel, pass_row_size;
  213. int compression_level;
  214. uint8_t *ptr, *top;
  215. uint8_t *crow_base = NULL, *crow_buf, *crow;
  216. uint8_t *progressive_buf = NULL;
  217. uint8_t *rgba_buf = NULL;
  218. uint8_t *top_buf = NULL;
  219. *p = *pict;
  220. p->pict_type= FF_I_TYPE;
  221. p->key_frame= 1;
  222. s->bytestream_start=
  223. s->bytestream= buf;
  224. s->bytestream_end= buf+buf_size;
  225. is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  226. switch(avctx->pix_fmt) {
  227. case PIX_FMT_RGB32:
  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_GRAY8:
  236. bit_depth = 8;
  237. color_type = PNG_COLOR_TYPE_GRAY;
  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 (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  272. rgba_buf = av_malloc(row_size + 1);
  273. if (!rgba_buf)
  274. goto fail;
  275. }
  276. if (is_progressive || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  277. top_buf = av_malloc(row_size + 1);
  278. if (!top_buf)
  279. goto fail;
  280. }
  281. /* write png header */
  282. memcpy(s->bytestream, ff_pngsig, 8);
  283. s->bytestream += 8;
  284. AV_WB32(s->buf, avctx->width);
  285. AV_WB32(s->buf + 4, avctx->height);
  286. s->buf[8] = bit_depth;
  287. s->buf[9] = color_type;
  288. s->buf[10] = 0; /* compression type */
  289. s->buf[11] = 0; /* filter type */
  290. s->buf[12] = is_progressive; /* interlace type */
  291. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  292. /* put the palette if needed */
  293. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  294. int has_alpha, alpha, i;
  295. unsigned int v;
  296. uint32_t *palette;
  297. uint8_t *alpha_ptr;
  298. palette = (uint32_t *)p->data[1];
  299. ptr = s->buf;
  300. alpha_ptr = s->buf + 256 * 3;
  301. has_alpha = 0;
  302. for(i = 0; i < 256; i++) {
  303. v = palette[i];
  304. alpha = v >> 24;
  305. if (alpha && alpha != 0xff)
  306. has_alpha = 1;
  307. *alpha_ptr++ = alpha;
  308. bytestream_put_be24(&ptr, v);
  309. }
  310. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  311. if (has_alpha) {
  312. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  313. }
  314. }
  315. /* now put each row */
  316. s->zstream.avail_out = IOBUF_SIZE;
  317. s->zstream.next_out = s->buf;
  318. if (is_progressive) {
  319. int pass;
  320. for(pass = 0; pass < NB_PASSES; pass++) {
  321. /* NOTE: a pass is completely omited if no pixels would be
  322. output */
  323. pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
  324. if (pass_row_size > 0) {
  325. top = NULL;
  326. for(y = 0; y < avctx->height; y++) {
  327. if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
  328. ptr = p->data[0] + y * p->linesize[0];
  329. FFSWAP(uint8_t*, progressive_buf, top_buf);
  330. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  331. convert_from_rgb32(rgba_buf, ptr, avctx->width);
  332. ptr = rgba_buf;
  333. }
  334. png_get_interlaced_row(progressive_buf, pass_row_size,
  335. bits_per_pixel, pass,
  336. ptr, avctx->width);
  337. crow = png_choose_filter(s, crow_buf, progressive_buf, top, pass_row_size, bits_per_pixel>>3);
  338. png_write_row(s, crow, pass_row_size + 1);
  339. top = progressive_buf;
  340. }
  341. }
  342. }
  343. }
  344. } else {
  345. top = NULL;
  346. for(y = 0; y < avctx->height; y++) {
  347. ptr = p->data[0] + y * p->linesize[0];
  348. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  349. FFSWAP(uint8_t*, rgba_buf, top_buf);
  350. convert_from_rgb32(rgba_buf, ptr, avctx->width);
  351. ptr = rgba_buf;
  352. }
  353. crow = png_choose_filter(s, crow_buf, ptr, top, row_size, bits_per_pixel>>3);
  354. png_write_row(s, crow, row_size + 1);
  355. top = ptr;
  356. }
  357. }
  358. /* compress last bytes */
  359. for(;;) {
  360. ret = deflate(&s->zstream, Z_FINISH);
  361. if (ret == Z_OK || ret == Z_STREAM_END) {
  362. len = IOBUF_SIZE - s->zstream.avail_out;
  363. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  364. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  365. }
  366. s->zstream.avail_out = IOBUF_SIZE;
  367. s->zstream.next_out = s->buf;
  368. if (ret == Z_STREAM_END)
  369. break;
  370. } else {
  371. goto fail;
  372. }
  373. }
  374. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  375. ret = s->bytestream - s->bytestream_start;
  376. the_end:
  377. av_free(crow_base);
  378. av_free(progressive_buf);
  379. av_free(rgba_buf);
  380. av_free(top_buf);
  381. deflateEnd(&s->zstream);
  382. return ret;
  383. fail:
  384. ret = -1;
  385. goto the_end;
  386. }
  387. static av_cold int png_enc_init(AVCodecContext *avctx){
  388. PNGEncContext *s = avctx->priv_data;
  389. avcodec_get_frame_defaults(&s->picture);
  390. avctx->coded_frame= &s->picture;
  391. dsputil_init(&s->dsp, avctx);
  392. s->filter_type = av_clip(avctx->prediction_method, PNG_FILTER_VALUE_NONE, PNG_FILTER_VALUE_MIXED);
  393. if(avctx->pix_fmt == PIX_FMT_MONOBLACK)
  394. s->filter_type = PNG_FILTER_VALUE_NONE;
  395. return 0;
  396. }
  397. AVCodec png_encoder = {
  398. "png",
  399. CODEC_TYPE_VIDEO,
  400. CODEC_ID_PNG,
  401. sizeof(PNGEncContext),
  402. png_enc_init,
  403. encode_frame,
  404. NULL, //encode_end,
  405. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, PIX_FMT_NONE},
  406. .long_name= NULL_IF_CONFIG_SMALL("PNG image"),
  407. };