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.

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