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.

335 lines
9.8KB

  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 "png.h"
  24. /* TODO:
  25. * - add 2, 4 and 16 bit depth support
  26. * - use filters when generating a png (better compression)
  27. */
  28. #include <zlib.h>
  29. //#define DEBUG
  30. #define IOBUF_SIZE 4096
  31. typedef struct PNGEncContext {
  32. uint8_t *bytestream;
  33. uint8_t *bytestream_start;
  34. uint8_t *bytestream_end;
  35. AVFrame picture;
  36. z_stream zstream;
  37. uint8_t buf[IOBUF_SIZE];
  38. } PNGEncContext;
  39. static void png_get_interlaced_row(uint8_t *dst, int row_size,
  40. int bits_per_pixel, int pass,
  41. const uint8_t *src, int width)
  42. {
  43. int x, mask, dst_x, j, b, bpp;
  44. uint8_t *d;
  45. const uint8_t *s;
  46. mask = ff_png_pass_mask[pass];
  47. switch(bits_per_pixel) {
  48. case 1:
  49. memset(dst, 0, row_size);
  50. dst_x = 0;
  51. for(x = 0; x < width; x++) {
  52. j = (x & 7);
  53. if ((mask << j) & 0x80) {
  54. b = (src[x >> 3] >> (7 - j)) & 1;
  55. dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
  56. dst_x++;
  57. }
  58. }
  59. break;
  60. default:
  61. bpp = bits_per_pixel >> 3;
  62. d = dst;
  63. s = src;
  64. for(x = 0; x < width; x++) {
  65. j = x & 7;
  66. if ((mask << j) & 0x80) {
  67. memcpy(d, s, bpp);
  68. d += bpp;
  69. }
  70. s += bpp;
  71. }
  72. break;
  73. }
  74. }
  75. static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
  76. {
  77. uint8_t *d;
  78. int j;
  79. unsigned int v;
  80. d = dst;
  81. for(j = 0; j < width; j++) {
  82. v = ((const uint32_t *)src)[j];
  83. d[0] = v >> 16;
  84. d[1] = v >> 8;
  85. d[2] = v;
  86. d[3] = v >> 24;
  87. d += 4;
  88. }
  89. }
  90. static void png_write_chunk(uint8_t **f, uint32_t tag,
  91. const uint8_t *buf, int length)
  92. {
  93. uint32_t crc;
  94. uint8_t tagbuf[4];
  95. bytestream_put_be32(f, length);
  96. crc = crc32(0, Z_NULL, 0);
  97. AV_WL32(tagbuf, tag);
  98. crc = crc32(crc, tagbuf, 4);
  99. bytestream_put_be32(f, bswap_32(tag));
  100. if (length > 0) {
  101. crc = crc32(crc, buf, length);
  102. memcpy(*f, buf, length);
  103. *f += length;
  104. }
  105. bytestream_put_be32(f, crc);
  106. }
  107. /* XXX: do filtering */
  108. static int png_write_row(PNGEncContext *s, const uint8_t *data, int size)
  109. {
  110. int ret;
  111. s->zstream.avail_in = size;
  112. s->zstream.next_in = (uint8_t *)data;
  113. while (s->zstream.avail_in > 0) {
  114. ret = deflate(&s->zstream, Z_NO_FLUSH);
  115. if (ret != Z_OK)
  116. return -1;
  117. if (s->zstream.avail_out == 0) {
  118. if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
  119. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  120. s->zstream.avail_out = IOBUF_SIZE;
  121. s->zstream.next_out = s->buf;
  122. }
  123. }
  124. return 0;
  125. }
  126. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  127. PNGEncContext *s = avctx->priv_data;
  128. AVFrame *pict = data;
  129. AVFrame * const p= (AVFrame*)&s->picture;
  130. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  131. int bits_per_pixel, pass_row_size;
  132. int compression_level;
  133. uint8_t *ptr;
  134. uint8_t *crow_buf = NULL;
  135. uint8_t *tmp_buf = NULL;
  136. *p = *pict;
  137. p->pict_type= FF_I_TYPE;
  138. p->key_frame= 1;
  139. s->bytestream_start=
  140. s->bytestream= buf;
  141. s->bytestream_end= buf+buf_size;
  142. is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  143. switch(avctx->pix_fmt) {
  144. case PIX_FMT_RGB32:
  145. bit_depth = 8;
  146. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  147. break;
  148. case PIX_FMT_RGB24:
  149. bit_depth = 8;
  150. color_type = PNG_COLOR_TYPE_RGB;
  151. break;
  152. case PIX_FMT_GRAY8:
  153. bit_depth = 8;
  154. color_type = PNG_COLOR_TYPE_GRAY;
  155. break;
  156. case PIX_FMT_MONOBLACK:
  157. bit_depth = 1;
  158. color_type = PNG_COLOR_TYPE_GRAY;
  159. break;
  160. case PIX_FMT_PAL8:
  161. bit_depth = 8;
  162. color_type = PNG_COLOR_TYPE_PALETTE;
  163. break;
  164. default:
  165. return -1;
  166. }
  167. bits_per_pixel = ff_png_get_nb_channels(color_type) * bit_depth;
  168. row_size = (avctx->width * bits_per_pixel + 7) >> 3;
  169. s->zstream.zalloc = ff_png_zalloc;
  170. s->zstream.zfree = ff_png_zfree;
  171. s->zstream.opaque = NULL;
  172. compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
  173. Z_DEFAULT_COMPRESSION :
  174. av_clip(avctx->compression_level, 0, 9);
  175. ret = deflateInit2(&s->zstream, compression_level,
  176. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  177. if (ret != Z_OK)
  178. return -1;
  179. crow_buf = av_malloc(row_size + 1);
  180. if (!crow_buf)
  181. goto fail;
  182. if (is_progressive) {
  183. tmp_buf = av_malloc(row_size + 1);
  184. if (!tmp_buf)
  185. goto fail;
  186. }
  187. /* write png header */
  188. memcpy(s->bytestream, ff_pngsig, 8);
  189. s->bytestream += 8;
  190. AV_WB32(s->buf, avctx->width);
  191. AV_WB32(s->buf + 4, avctx->height);
  192. s->buf[8] = bit_depth;
  193. s->buf[9] = color_type;
  194. s->buf[10] = 0; /* compression type */
  195. s->buf[11] = 0; /* filter type */
  196. s->buf[12] = is_progressive; /* interlace type */
  197. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  198. /* put the palette if needed */
  199. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  200. int has_alpha, alpha, i;
  201. unsigned int v;
  202. uint32_t *palette;
  203. uint8_t *alpha_ptr;
  204. palette = (uint32_t *)p->data[1];
  205. ptr = s->buf;
  206. alpha_ptr = s->buf + 256 * 3;
  207. has_alpha = 0;
  208. for(i = 0; i < 256; i++) {
  209. v = palette[i];
  210. alpha = v >> 24;
  211. if (alpha && alpha != 0xff)
  212. has_alpha = 1;
  213. *alpha_ptr++ = alpha;
  214. bytestream_put_be24(&ptr, v);
  215. }
  216. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  217. if (has_alpha) {
  218. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  219. }
  220. }
  221. /* now put each row */
  222. s->zstream.avail_out = IOBUF_SIZE;
  223. s->zstream.next_out = s->buf;
  224. if (is_progressive) {
  225. uint8_t *ptr1;
  226. int pass;
  227. for(pass = 0; pass < NB_PASSES; pass++) {
  228. /* NOTE: a pass is completely omited if no pixels would be
  229. output */
  230. pass_row_size = ff_png_pass_row_size(pass, bits_per_pixel, avctx->width);
  231. if (pass_row_size > 0) {
  232. for(y = 0; y < avctx->height; y++) {
  233. if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
  234. ptr = p->data[0] + y * p->linesize[0];
  235. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  236. convert_from_rgb32(tmp_buf, ptr, avctx->width);
  237. ptr1 = tmp_buf;
  238. } else {
  239. ptr1 = ptr;
  240. }
  241. png_get_interlaced_row(crow_buf + 1, pass_row_size,
  242. bits_per_pixel, pass,
  243. ptr1, avctx->width);
  244. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  245. png_write_row(s, crow_buf, pass_row_size + 1);
  246. }
  247. }
  248. }
  249. }
  250. } else {
  251. for(y = 0; y < avctx->height; y++) {
  252. ptr = p->data[0] + y * p->linesize[0];
  253. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  254. convert_from_rgb32(crow_buf + 1, ptr, avctx->width);
  255. else
  256. memcpy(crow_buf + 1, ptr, row_size);
  257. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  258. png_write_row(s, crow_buf, row_size + 1);
  259. }
  260. }
  261. /* compress last bytes */
  262. for(;;) {
  263. ret = deflate(&s->zstream, Z_FINISH);
  264. if (ret == Z_OK || ret == Z_STREAM_END) {
  265. len = IOBUF_SIZE - s->zstream.avail_out;
  266. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  267. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  268. }
  269. s->zstream.avail_out = IOBUF_SIZE;
  270. s->zstream.next_out = s->buf;
  271. if (ret == Z_STREAM_END)
  272. break;
  273. } else {
  274. goto fail;
  275. }
  276. }
  277. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  278. ret = s->bytestream - s->bytestream_start;
  279. the_end:
  280. av_free(crow_buf);
  281. av_free(tmp_buf);
  282. deflateEnd(&s->zstream);
  283. return ret;
  284. fail:
  285. ret = -1;
  286. goto the_end;
  287. }
  288. static int png_enc_init(AVCodecContext *avctx){
  289. PNGEncContext *s = avctx->priv_data;
  290. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  291. avctx->coded_frame= (AVFrame*)&s->picture;
  292. return 0;
  293. }
  294. AVCodec png_encoder = {
  295. "png",
  296. CODEC_TYPE_VIDEO,
  297. CODEC_ID_PNG,
  298. sizeof(PNGEncContext),
  299. png_enc_init,
  300. encode_frame,
  301. NULL, //encode_end,
  302. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
  303. };