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.

945 lines
29KB

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