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.

946 lines
28KB

  1. /*
  2. * PNG image format
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. /* TODO:
  21. * - add 2, 4 and 16 bit depth support
  22. * - use filters when generating a png (better compression)
  23. */
  24. #ifdef CONFIG_ZLIB
  25. #include <zlib.h>
  26. //#define DEBUG
  27. #define PNG_COLOR_MASK_PALETTE 1
  28. #define PNG_COLOR_MASK_COLOR 2
  29. #define PNG_COLOR_MASK_ALPHA 4
  30. #define PNG_COLOR_TYPE_GRAY 0
  31. #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
  32. #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
  33. #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
  34. #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
  35. #define PNG_FILTER_VALUE_NONE 0
  36. #define PNG_FILTER_VALUE_SUB 1
  37. #define PNG_FILTER_VALUE_UP 2
  38. #define PNG_FILTER_VALUE_AVG 3
  39. #define PNG_FILTER_VALUE_PAETH 4
  40. #define PNG_IHDR 0x0001
  41. #define PNG_IDAT 0x0002
  42. #define PNG_ALLIMAGE 0x0004
  43. #define PNG_PLTE 0x0008
  44. #define NB_PASSES 7
  45. #define IOBUF_SIZE 4096
  46. typedef struct PNGContext {
  47. uint8_t *bytestream;
  48. uint8_t *bytestream_start;
  49. uint8_t *bytestream_end;
  50. AVFrame picture;
  51. int state;
  52. int width, height;
  53. int bit_depth;
  54. int color_type;
  55. int compression_type;
  56. int interlace_type;
  57. int filter_type;
  58. int channels;
  59. int bits_per_pixel;
  60. int bpp;
  61. uint8_t *image_buf;
  62. int image_linesize;
  63. uint32_t palette[256];
  64. uint8_t *crow_buf;
  65. uint8_t *last_row;
  66. uint8_t *tmp_row;
  67. int pass;
  68. int crow_size; /* compressed row size (include filter type) */
  69. int row_size; /* decompressed row size */
  70. int pass_row_size; /* decompress row size of the current pass */
  71. int y;
  72. z_stream zstream;
  73. uint8_t buf[IOBUF_SIZE];
  74. } PNGContext;
  75. static unsigned int get32(uint8_t **b){
  76. (*b) += 4;
  77. return ((*b)[-4]<<24) + ((*b)[-3]<<16) + ((*b)[-2]<<8) + (*b)[-1];
  78. }
  79. static void put32(uint8_t **b, unsigned int v){
  80. *(*b)++= v>>24;
  81. *(*b)++= v>>16;
  82. *(*b)++= v>>8;
  83. *(*b)++= v;
  84. }
  85. static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  86. /* Mask to determine which y pixels are valid in a pass */
  87. static const uint8_t png_pass_ymask[NB_PASSES] = {
  88. 0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
  89. };
  90. /* Mask to determine which y pixels can be written in a pass */
  91. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  92. 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
  93. };
  94. /* minimum x value */
  95. static const uint8_t png_pass_xmin[NB_PASSES] = {
  96. 0, 4, 0, 2, 0, 1, 0
  97. };
  98. /* x shift to get row width */
  99. static const uint8_t png_pass_xshift[NB_PASSES] = {
  100. 3, 3, 2, 2, 1, 1, 0
  101. };
  102. /* Mask to determine which pixels are valid in a pass */
  103. static const uint8_t png_pass_mask[NB_PASSES] = {
  104. 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
  105. };
  106. /* Mask to determine which pixels to overwrite while displaying */
  107. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  108. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  109. };
  110. #if 0
  111. static int png_probe(AVProbeData *pd)
  112. {
  113. if (pd->buf_size >= 8 &&
  114. memcmp(pd->buf, pngsig, 8) == 0)
  115. return AVPROBE_SCORE_MAX;
  116. else
  117. return 0;
  118. }
  119. #endif
  120. static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
  121. {
  122. return av_malloc(items * size);
  123. }
  124. static void png_zfree(void *opaque, void *ptr)
  125. {
  126. av_free(ptr);
  127. }
  128. static int png_get_nb_channels(int color_type)
  129. {
  130. int channels;
  131. channels = 1;
  132. if ((color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
  133. PNG_COLOR_MASK_COLOR)
  134. channels = 3;
  135. if (color_type & PNG_COLOR_MASK_ALPHA)
  136. channels++;
  137. return channels;
  138. }
  139. /* compute the row size of an interleaved pass */
  140. static int png_pass_row_size(int pass, int bits_per_pixel, int width)
  141. {
  142. int shift, xmin, pass_width;
  143. xmin = png_pass_xmin[pass];
  144. if (width <= xmin)
  145. return 0;
  146. shift = png_pass_xshift[pass];
  147. pass_width = (width - xmin + (1 << shift) - 1) >> shift;
  148. return (pass_width * bits_per_pixel + 7) >> 3;
  149. }
  150. /* NOTE: we try to construct a good looking image at each pass. width
  151. is the original image width. We also do pixel format convertion at
  152. this stage */
  153. static void png_put_interlaced_row(uint8_t *dst, int width,
  154. int bits_per_pixel, int pass,
  155. int color_type, const uint8_t *src)
  156. {
  157. int x, mask, dsp_mask, j, src_x, b, bpp;
  158. uint8_t *d;
  159. const uint8_t *s;
  160. mask = png_pass_mask[pass];
  161. dsp_mask = png_pass_dsp_mask[pass];
  162. switch(bits_per_pixel) {
  163. case 1:
  164. /* we must intialize the line to zero before writing to it */
  165. if (pass == 0)
  166. memset(dst, 0, (width + 7) >> 3);
  167. src_x = 0;
  168. for(x = 0; x < width; x++) {
  169. j = (x & 7);
  170. if ((dsp_mask << j) & 0x80) {
  171. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  172. dst[x >> 3] |= b << (7 - j);
  173. }
  174. if ((mask << j) & 0x80)
  175. src_x++;
  176. }
  177. break;
  178. default:
  179. bpp = bits_per_pixel >> 3;
  180. d = dst;
  181. s = src;
  182. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  183. for(x = 0; x < width; x++) {
  184. j = x & 7;
  185. if ((dsp_mask << j) & 0x80) {
  186. *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
  187. }
  188. d += bpp;
  189. if ((mask << j) & 0x80)
  190. s += bpp;
  191. }
  192. } else {
  193. for(x = 0; x < width; x++) {
  194. j = x & 7;
  195. if ((dsp_mask << j) & 0x80) {
  196. memcpy(d, s, bpp);
  197. }
  198. d += bpp;
  199. if ((mask << j) & 0x80)
  200. s += bpp;
  201. }
  202. }
  203. break;
  204. }
  205. }
  206. static void png_get_interlaced_row(uint8_t *dst, int row_size,
  207. int bits_per_pixel, int pass,
  208. const uint8_t *src, int width)
  209. {
  210. int x, mask, dst_x, j, b, bpp;
  211. uint8_t *d;
  212. const uint8_t *s;
  213. mask = png_pass_mask[pass];
  214. switch(bits_per_pixel) {
  215. case 1:
  216. memset(dst, 0, row_size);
  217. dst_x = 0;
  218. for(x = 0; x < width; x++) {
  219. j = (x & 7);
  220. if ((mask << j) & 0x80) {
  221. b = (src[x >> 3] >> (7 - j)) & 1;
  222. dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
  223. dst_x++;
  224. }
  225. }
  226. break;
  227. default:
  228. bpp = bits_per_pixel >> 3;
  229. d = dst;
  230. s = src;
  231. for(x = 0; x < width; x++) {
  232. j = x & 7;
  233. if ((mask << j) & 0x80) {
  234. memcpy(d, s, bpp);
  235. d += bpp;
  236. }
  237. s += bpp;
  238. }
  239. break;
  240. }
  241. }
  242. /* XXX: optimize */
  243. /* NOTE: 'dst' can be equal to 'last' */
  244. static void png_filter_row(uint8_t *dst, int filter_type,
  245. uint8_t *src, uint8_t *last, int size, int bpp)
  246. {
  247. int i, p;
  248. switch(filter_type) {
  249. case PNG_FILTER_VALUE_NONE:
  250. memcpy(dst, src, size);
  251. break;
  252. case PNG_FILTER_VALUE_SUB:
  253. for(i = 0; i < bpp; i++) {
  254. dst[i] = src[i];
  255. }
  256. for(i = bpp; i < size; i++) {
  257. p = dst[i - bpp];
  258. dst[i] = p + src[i];
  259. }
  260. break;
  261. case PNG_FILTER_VALUE_UP:
  262. for(i = 0; i < size; i++) {
  263. p = last[i];
  264. dst[i] = p + src[i];
  265. }
  266. break;
  267. case PNG_FILTER_VALUE_AVG:
  268. for(i = 0; i < bpp; i++) {
  269. p = (last[i] >> 1);
  270. dst[i] = p + src[i];
  271. }
  272. for(i = bpp; i < size; i++) {
  273. p = ((dst[i - bpp] + last[i]) >> 1);
  274. dst[i] = p + src[i];
  275. }
  276. break;
  277. case PNG_FILTER_VALUE_PAETH:
  278. for(i = 0; i < bpp; i++) {
  279. p = last[i];
  280. dst[i] = p + src[i];
  281. }
  282. for(i = bpp; i < size; i++) {
  283. int a, b, c, pa, pb, pc;
  284. a = dst[i - bpp];
  285. b = last[i];
  286. c = last[i - bpp];
  287. p = b - c;
  288. pc = a - c;
  289. pa = abs(p);
  290. pb = abs(pc);
  291. pc = abs(p + pc);
  292. if (pa <= pb && pa <= pc)
  293. p = a;
  294. else if (pb <= pc)
  295. p = b;
  296. else
  297. p = c;
  298. dst[i] = p + src[i];
  299. }
  300. break;
  301. }
  302. }
  303. static void convert_from_rgba32(uint8_t *dst, const uint8_t *src, int width)
  304. {
  305. uint8_t *d;
  306. int j;
  307. unsigned int v;
  308. d = dst;
  309. for(j = 0; j < width; j++) {
  310. v = ((uint32_t *)src)[j];
  311. d[0] = v >> 16;
  312. d[1] = v >> 8;
  313. d[2] = v;
  314. d[3] = v >> 24;
  315. d += 4;
  316. }
  317. }
  318. static void convert_to_rgba32(uint8_t *dst, const uint8_t *src, int width)
  319. {
  320. int j;
  321. unsigned int r, g, b, a;
  322. for(j = 0;j < width; j++) {
  323. r = src[0];
  324. g = src[1];
  325. b = src[2];
  326. a = src[3];
  327. *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
  328. dst += 4;
  329. src += 4;
  330. }
  331. }
  332. /* process exactly one decompressed row */
  333. static void png_handle_row(PNGContext *s)
  334. {
  335. uint8_t *ptr, *last_row;
  336. int got_line;
  337. if (!s->interlace_type) {
  338. ptr = s->image_buf + s->image_linesize * s->y;
  339. /* need to swap bytes correctly for RGB_ALPHA */
  340. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  341. png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  342. s->last_row, s->row_size, s->bpp);
  343. memcpy(s->last_row, s->tmp_row, s->row_size);
  344. convert_to_rgba32(ptr, s->tmp_row, s->width);
  345. } else {
  346. /* in normal case, we avoid one copy */
  347. if (s->y == 0)
  348. last_row = s->last_row;
  349. else
  350. last_row = ptr - s->image_linesize;
  351. png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
  352. last_row, s->row_size, s->bpp);
  353. }
  354. s->y++;
  355. if (s->y == s->height) {
  356. s->state |= PNG_ALLIMAGE;
  357. }
  358. } else {
  359. got_line = 0;
  360. for(;;) {
  361. ptr = s->image_buf + s->image_linesize * s->y;
  362. if ((png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  363. /* if we already read one row, it is time to stop to
  364. wait for the next one */
  365. if (got_line)
  366. break;
  367. png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  368. s->last_row, s->pass_row_size, s->bpp);
  369. memcpy(s->last_row, s->tmp_row, s->pass_row_size);
  370. got_line = 1;
  371. }
  372. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  373. /* NOTE: rgba32 is handled directly in png_put_interlaced_row */
  374. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  375. s->color_type, s->last_row);
  376. }
  377. s->y++;
  378. if (s->y == s->height) {
  379. for(;;) {
  380. if (s->pass == NB_PASSES - 1) {
  381. s->state |= PNG_ALLIMAGE;
  382. goto the_end;
  383. } else {
  384. s->pass++;
  385. s->y = 0;
  386. s->pass_row_size = png_pass_row_size(s->pass,
  387. s->bits_per_pixel,
  388. s->width);
  389. s->crow_size = s->pass_row_size + 1;
  390. if (s->pass_row_size != 0)
  391. break;
  392. /* skip pass if empty row */
  393. }
  394. }
  395. }
  396. }
  397. the_end: ;
  398. }
  399. }
  400. static int png_decode_idat(PNGContext *s, int length)
  401. {
  402. int ret;
  403. s->zstream.avail_in = length;
  404. s->zstream.next_in = s->bytestream;
  405. s->bytestream += length;
  406. if(s->bytestream > s->bytestream_end)
  407. return -1;
  408. /* decode one line if possible */
  409. while (s->zstream.avail_in > 0) {
  410. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  411. if (ret != Z_OK && ret != Z_STREAM_END) {
  412. return -1;
  413. }
  414. if (s->zstream.avail_out == 0) {
  415. if (!(s->state & PNG_ALLIMAGE)) {
  416. png_handle_row(s);
  417. }
  418. s->zstream.avail_out = s->crow_size;
  419. s->zstream.next_out = s->crow_buf;
  420. }
  421. }
  422. return 0;
  423. }
  424. static int decode_frame(AVCodecContext *avctx,
  425. void *data, int *data_size,
  426. uint8_t *buf, int buf_size)
  427. {
  428. PNGContext * const s = avctx->priv_data;
  429. AVFrame *picture = data;
  430. AVFrame * const p= (AVFrame*)&s->picture;
  431. uint32_t tag, length;
  432. int ret, crc;
  433. /* special case for last picture */
  434. if (buf_size == 0) {
  435. return 0;
  436. }
  437. s->bytestream_start=
  438. s->bytestream= buf;
  439. s->bytestream_end= buf + buf_size;
  440. /* check signature */
  441. if (memcmp(s->bytestream, pngsig, 8) != 0)
  442. return -1;
  443. s->bytestream+= 8;
  444. s->y=
  445. s->state=0;
  446. // memset(s, 0, sizeof(PNGContext));
  447. /* init the zlib */
  448. s->zstream.zalloc = png_zalloc;
  449. s->zstream.zfree = png_zfree;
  450. s->zstream.opaque = NULL;
  451. ret = inflateInit(&s->zstream);
  452. if (ret != Z_OK)
  453. return -1;
  454. for(;;) {
  455. int tag32;
  456. if (s->bytestream >= s->bytestream_end)
  457. goto fail;
  458. length = get32(&s->bytestream);
  459. if (length > 0x7fffffff)
  460. goto fail;
  461. tag32 = get32(&s->bytestream);
  462. tag = bswap_32(tag32);
  463. #ifdef DEBUG
  464. printf("png: tag=%c%c%c%c length=%u\n",
  465. (tag & 0xff),
  466. ((tag >> 8) & 0xff),
  467. ((tag >> 16) & 0xff),
  468. ((tag >> 24) & 0xff), length);
  469. #endif
  470. switch(tag) {
  471. case MKTAG('I', 'H', 'D', 'R'):
  472. if (length != 13)
  473. goto fail;
  474. s->width = get32(&s->bytestream);
  475. s->height = get32(&s->bytestream);
  476. s->bit_depth = *s->bytestream++;
  477. s->color_type = *s->bytestream++;
  478. s->compression_type = *s->bytestream++;
  479. s->filter_type = *s->bytestream++;
  480. s->interlace_type = *s->bytestream++;
  481. crc = get32(&s->bytestream);
  482. s->state |= PNG_IHDR;
  483. #ifdef DEBUG
  484. printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
  485. s->width, s->height, s->bit_depth, s->color_type,
  486. s->compression_type, s->filter_type, s->interlace_type);
  487. #endif
  488. break;
  489. case MKTAG('I', 'D', 'A', 'T'):
  490. if (!(s->state & PNG_IHDR))
  491. goto fail;
  492. if (!(s->state & PNG_IDAT)) {
  493. /* init image info */
  494. avctx->width = s->width;
  495. avctx->height = s->height;
  496. s->channels = png_get_nb_channels(s->color_type);
  497. s->bits_per_pixel = s->bit_depth * s->channels;
  498. s->bpp = (s->bits_per_pixel + 7) >> 3;
  499. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  500. if (s->bit_depth == 8 &&
  501. s->color_type == PNG_COLOR_TYPE_RGB) {
  502. avctx->pix_fmt = PIX_FMT_RGB24;
  503. } else if (s->bit_depth == 8 &&
  504. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  505. avctx->pix_fmt = PIX_FMT_RGBA32;
  506. } else if (s->bit_depth == 8 &&
  507. s->color_type == PNG_COLOR_TYPE_GRAY) {
  508. avctx->pix_fmt = PIX_FMT_GRAY8;
  509. } else if (s->bit_depth == 1 &&
  510. s->color_type == PNG_COLOR_TYPE_GRAY) {
  511. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  512. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  513. avctx->pix_fmt = PIX_FMT_PAL8;
  514. } else {
  515. goto fail;
  516. }
  517. if(p->data[0])
  518. avctx->release_buffer(avctx, p);
  519. p->reference= 0;
  520. if(avctx->get_buffer(avctx, p) < 0){
  521. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  522. goto fail;
  523. }
  524. p->pict_type= FF_I_TYPE;
  525. p->key_frame= 1;
  526. p->interlaced_frame = !!s->interlace_type;
  527. /* compute the compressed row size */
  528. if (!s->interlace_type) {
  529. s->crow_size = s->row_size + 1;
  530. } else {
  531. s->pass = 0;
  532. s->pass_row_size = png_pass_row_size(s->pass,
  533. s->bits_per_pixel,
  534. s->width);
  535. s->crow_size = s->pass_row_size + 1;
  536. }
  537. #ifdef DEBUG
  538. printf("row_size=%d crow_size =%d\n",
  539. s->row_size, s->crow_size);
  540. #endif
  541. s->image_buf = p->data[0];
  542. s->image_linesize = p->linesize[0];
  543. /* copy the palette if needed */
  544. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  545. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  546. /* empty row is used if differencing to the first row */
  547. s->last_row = av_mallocz(s->row_size);
  548. if (!s->last_row)
  549. goto fail;
  550. if (s->interlace_type ||
  551. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  552. s->tmp_row = av_malloc(s->row_size);
  553. if (!s->tmp_row)
  554. goto fail;
  555. }
  556. /* compressed row */
  557. s->crow_buf = av_malloc(s->row_size + 1);
  558. if (!s->crow_buf)
  559. goto fail;
  560. s->zstream.avail_out = s->crow_size;
  561. s->zstream.next_out = s->crow_buf;
  562. }
  563. s->state |= PNG_IDAT;
  564. if (png_decode_idat(s, length) < 0)
  565. goto fail;
  566. /* skip crc */
  567. crc = get32(&s->bytestream);
  568. break;
  569. case MKTAG('P', 'L', 'T', 'E'):
  570. {
  571. int n, i, r, g, b;
  572. if ((length % 3) != 0 || length > 256 * 3)
  573. goto skip_tag;
  574. /* read the palette */
  575. n = length / 3;
  576. for(i=0;i<n;i++) {
  577. r = *s->bytestream++;
  578. g = *s->bytestream++;
  579. b = *s->bytestream++;
  580. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  581. }
  582. for(;i<256;i++) {
  583. s->palette[i] = (0xff << 24);
  584. }
  585. s->state |= PNG_PLTE;
  586. crc = get32(&s->bytestream);
  587. }
  588. break;
  589. case MKTAG('t', 'R', 'N', 'S'):
  590. {
  591. int v, i;
  592. /* read the transparency. XXX: Only palette mode supported */
  593. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  594. length > 256 ||
  595. !(s->state & PNG_PLTE))
  596. goto skip_tag;
  597. for(i=0;i<length;i++) {
  598. v = *s->bytestream++;
  599. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  600. }
  601. crc = get32(&s->bytestream);
  602. }
  603. break;
  604. case MKTAG('I', 'E', 'N', 'D'):
  605. if (!(s->state & PNG_ALLIMAGE))
  606. goto fail;
  607. crc = get32(&s->bytestream);
  608. goto exit_loop;
  609. default:
  610. /* skip tag */
  611. skip_tag:
  612. s->bytestream += length + 4;
  613. break;
  614. }
  615. }
  616. exit_loop:
  617. *picture= *(AVFrame*)&s->picture;
  618. *data_size = sizeof(AVPicture);
  619. ret = s->bytestream - s->bytestream_start;
  620. the_end:
  621. inflateEnd(&s->zstream);
  622. av_freep(&s->crow_buf);
  623. av_freep(&s->last_row);
  624. av_freep(&s->tmp_row);
  625. return ret;
  626. fail:
  627. ret = -1;
  628. goto the_end;
  629. }
  630. static void png_write_chunk(uint8_t **f, uint32_t tag,
  631. const uint8_t *buf, int length)
  632. {
  633. uint32_t crc;
  634. uint8_t tagbuf[4];
  635. put32(f, length);
  636. crc = crc32(0, Z_NULL, 0);
  637. tagbuf[0] = tag;
  638. tagbuf[1] = tag >> 8;
  639. tagbuf[2] = tag >> 16;
  640. tagbuf[3] = tag >> 24;
  641. crc = crc32(crc, tagbuf, 4);
  642. put32(f, bswap_32(tag));
  643. if (length > 0) {
  644. crc = crc32(crc, buf, length);
  645. memcpy(*f, buf, length);
  646. *f += length;
  647. }
  648. put32(f, crc);
  649. }
  650. /* XXX: use avcodec generic function ? */
  651. static void to_be32(uint8_t *p, uint32_t v)
  652. {
  653. p[0] = v >> 24;
  654. p[1] = v >> 16;
  655. p[2] = v >> 8;
  656. p[3] = v;
  657. }
  658. /* XXX: do filtering */
  659. static int png_write_row(PNGContext *s, const uint8_t *data, int size)
  660. {
  661. int ret;
  662. s->zstream.avail_in = size;
  663. s->zstream.next_in = (uint8_t *)data;
  664. while (s->zstream.avail_in > 0) {
  665. ret = deflate(&s->zstream, Z_NO_FLUSH);
  666. if (ret != Z_OK)
  667. return -1;
  668. if (s->zstream.avail_out == 0) {
  669. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  670. s->zstream.avail_out = IOBUF_SIZE;
  671. s->zstream.next_out = s->buf;
  672. }
  673. }
  674. return 0;
  675. }
  676. static int common_init(AVCodecContext *avctx){
  677. PNGContext *s = avctx->priv_data;
  678. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  679. avctx->coded_frame= (AVFrame*)&s->picture;
  680. // s->avctx= avctx;
  681. return 0;
  682. }
  683. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  684. PNGContext *s = avctx->priv_data;
  685. AVFrame *pict = data;
  686. AVFrame * const p= (AVFrame*)&s->picture;
  687. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  688. int bits_per_pixel, pass_row_size;
  689. uint8_t *ptr;
  690. uint8_t *crow_buf = NULL;
  691. uint8_t *tmp_buf = NULL;
  692. *p = *pict;
  693. p->pict_type= FF_I_TYPE;
  694. p->key_frame= 1;
  695. s->bytestream_start=
  696. s->bytestream= buf;
  697. s->bytestream_end= buf+buf_size;
  698. is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  699. switch(avctx->pix_fmt) {
  700. case PIX_FMT_RGBA32:
  701. bit_depth = 8;
  702. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  703. break;
  704. case PIX_FMT_RGB24:
  705. bit_depth = 8;
  706. color_type = PNG_COLOR_TYPE_RGB;
  707. break;
  708. case PIX_FMT_GRAY8:
  709. bit_depth = 8;
  710. color_type = PNG_COLOR_TYPE_GRAY;
  711. break;
  712. case PIX_FMT_MONOBLACK:
  713. bit_depth = 1;
  714. color_type = PNG_COLOR_TYPE_GRAY;
  715. break;
  716. case PIX_FMT_PAL8:
  717. bit_depth = 8;
  718. color_type = PNG_COLOR_TYPE_PALETTE;
  719. break;
  720. default:
  721. return -1;
  722. }
  723. bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
  724. row_size = (avctx->width * bits_per_pixel + 7) >> 3;
  725. s->zstream.zalloc = png_zalloc;
  726. s->zstream.zfree = png_zfree;
  727. s->zstream.opaque = NULL;
  728. ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
  729. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  730. if (ret != Z_OK)
  731. return -1;
  732. crow_buf = av_malloc(row_size + 1);
  733. if (!crow_buf)
  734. goto fail;
  735. if (is_progressive) {
  736. tmp_buf = av_malloc(row_size + 1);
  737. if (!tmp_buf)
  738. goto fail;
  739. }
  740. /* write png header */
  741. memcpy(s->bytestream, pngsig, 8);
  742. s->bytestream += 8;
  743. to_be32(s->buf, avctx->width);
  744. to_be32(s->buf + 4, avctx->height);
  745. s->buf[8] = bit_depth;
  746. s->buf[9] = color_type;
  747. s->buf[10] = 0; /* compression type */
  748. s->buf[11] = 0; /* filter type */
  749. s->buf[12] = is_progressive; /* interlace type */
  750. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  751. /* put the palette if needed */
  752. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  753. int has_alpha, alpha, i;
  754. unsigned int v;
  755. uint32_t *palette;
  756. uint8_t *alpha_ptr;
  757. palette = (uint32_t *)p->data[1];
  758. ptr = s->buf;
  759. alpha_ptr = s->buf + 256 * 3;
  760. has_alpha = 0;
  761. for(i = 0; i < 256; i++) {
  762. v = palette[i];
  763. alpha = v >> 24;
  764. if (alpha != 0xff)
  765. has_alpha = 1;
  766. *alpha_ptr++ = alpha;
  767. ptr[0] = v >> 16;
  768. ptr[1] = v >> 8;
  769. ptr[2] = v;
  770. ptr += 3;
  771. }
  772. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  773. if (has_alpha) {
  774. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  775. }
  776. }
  777. /* now put each row */
  778. s->zstream.avail_out = IOBUF_SIZE;
  779. s->zstream.next_out = s->buf;
  780. if (is_progressive) {
  781. uint8_t *ptr1;
  782. int pass;
  783. for(pass = 0; pass < NB_PASSES; pass++) {
  784. /* NOTE: a pass is completely omited if no pixels would be
  785. output */
  786. pass_row_size = png_pass_row_size(pass, bits_per_pixel, avctx->width);
  787. if (pass_row_size > 0) {
  788. for(y = 0; y < avctx->height; y++) {
  789. if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
  790. ptr = p->data[0] + y * p->linesize[0];
  791. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  792. convert_from_rgba32(tmp_buf, ptr, avctx->width);
  793. ptr1 = tmp_buf;
  794. } else {
  795. ptr1 = ptr;
  796. }
  797. png_get_interlaced_row(crow_buf + 1, pass_row_size,
  798. bits_per_pixel, pass,
  799. ptr1, avctx->width);
  800. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  801. png_write_row(s, crow_buf, pass_row_size + 1);
  802. }
  803. }
  804. }
  805. }
  806. } else {
  807. for(y = 0; y < avctx->height; y++) {
  808. ptr = p->data[0] + y * p->linesize[0];
  809. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  810. convert_from_rgba32(crow_buf + 1, ptr, avctx->width);
  811. else
  812. memcpy(crow_buf + 1, ptr, row_size);
  813. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  814. png_write_row(s, crow_buf, row_size + 1);
  815. }
  816. }
  817. /* compress last bytes */
  818. for(;;) {
  819. ret = deflate(&s->zstream, Z_FINISH);
  820. if (ret == Z_OK || ret == Z_STREAM_END) {
  821. len = IOBUF_SIZE - s->zstream.avail_out;
  822. if (len > 0) {
  823. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  824. }
  825. s->zstream.avail_out = IOBUF_SIZE;
  826. s->zstream.next_out = s->buf;
  827. if (ret == Z_STREAM_END)
  828. break;
  829. } else {
  830. goto fail;
  831. }
  832. }
  833. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  834. ret = s->bytestream - s->bytestream_start;
  835. the_end:
  836. av_free(crow_buf);
  837. av_free(tmp_buf);
  838. deflateEnd(&s->zstream);
  839. return ret;
  840. fail:
  841. ret = -1;
  842. goto the_end;
  843. }
  844. AVCodec png_decoder = {
  845. "png",
  846. CODEC_TYPE_VIDEO,
  847. CODEC_ID_PNG,
  848. sizeof(PNGContext),
  849. common_init,
  850. NULL,
  851. NULL, //decode_end,
  852. decode_frame,
  853. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  854. NULL
  855. };
  856. AVCodec png_encoder = {
  857. "png",
  858. CODEC_TYPE_VIDEO,
  859. CODEC_ID_PNG,
  860. sizeof(PNGContext),
  861. common_init,
  862. encode_frame,
  863. NULL, //encode_end,
  864. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
  865. };
  866. #endif