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.

950 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. s->bytestream_start=
  436. s->bytestream= buf;
  437. s->bytestream_end= buf + buf_size;
  438. /* check signature */
  439. if (memcmp(s->bytestream, pngsig, 8) != 0)
  440. return -1;
  441. s->bytestream+= 8;
  442. s->y=
  443. s->state=0;
  444. // memset(s, 0, sizeof(PNGContext));
  445. /* init the zlib */
  446. s->zstream.zalloc = png_zalloc;
  447. s->zstream.zfree = png_zfree;
  448. s->zstream.opaque = NULL;
  449. ret = inflateInit(&s->zstream);
  450. if (ret != Z_OK)
  451. return -1;
  452. for(;;) {
  453. int tag32;
  454. if (s->bytestream >= s->bytestream_end)
  455. goto fail;
  456. length = get32(&s->bytestream);
  457. if (length > 0x7fffffff)
  458. goto fail;
  459. tag32 = get32(&s->bytestream);
  460. tag = bswap_32(tag32);
  461. #ifdef DEBUG
  462. printf("png: tag=%c%c%c%c length=%u\n",
  463. (tag & 0xff),
  464. ((tag >> 8) & 0xff),
  465. ((tag >> 16) & 0xff),
  466. ((tag >> 24) & 0xff), length);
  467. #endif
  468. switch(tag) {
  469. case MKTAG('I', 'H', 'D', 'R'):
  470. if (length != 13)
  471. goto fail;
  472. s->width = get32(&s->bytestream);
  473. s->height = get32(&s->bytestream);
  474. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  475. s->width= s->height= 0;
  476. goto fail;
  477. }
  478. s->bit_depth = *s->bytestream++;
  479. s->color_type = *s->bytestream++;
  480. s->compression_type = *s->bytestream++;
  481. s->filter_type = *s->bytestream++;
  482. s->interlace_type = *s->bytestream++;
  483. crc = get32(&s->bytestream);
  484. s->state |= PNG_IHDR;
  485. #ifdef DEBUG
  486. printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
  487. s->width, s->height, s->bit_depth, s->color_type,
  488. s->compression_type, s->filter_type, s->interlace_type);
  489. #endif
  490. break;
  491. case MKTAG('I', 'D', 'A', 'T'):
  492. if (!(s->state & PNG_IHDR))
  493. goto fail;
  494. if (!(s->state & PNG_IDAT)) {
  495. /* init image info */
  496. avctx->width = s->width;
  497. avctx->height = s->height;
  498. s->channels = png_get_nb_channels(s->color_type);
  499. s->bits_per_pixel = s->bit_depth * s->channels;
  500. s->bpp = (s->bits_per_pixel + 7) >> 3;
  501. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  502. if (s->bit_depth == 8 &&
  503. s->color_type == PNG_COLOR_TYPE_RGB) {
  504. avctx->pix_fmt = PIX_FMT_RGB24;
  505. } else if (s->bit_depth == 8 &&
  506. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  507. avctx->pix_fmt = PIX_FMT_RGBA32;
  508. } else if (s->bit_depth == 8 &&
  509. s->color_type == PNG_COLOR_TYPE_GRAY) {
  510. avctx->pix_fmt = PIX_FMT_GRAY8;
  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. printf("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 = get32(&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 = get32(&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 = get32(&s->bytestream);
  604. }
  605. break;
  606. case MKTAG('I', 'E', 'N', 'D'):
  607. if (!(s->state & PNG_ALLIMAGE))
  608. goto fail;
  609. crc = get32(&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. static void png_write_chunk(uint8_t **f, uint32_t tag,
  633. const uint8_t *buf, int length)
  634. {
  635. uint32_t crc;
  636. uint8_t tagbuf[4];
  637. put32(f, length);
  638. crc = crc32(0, Z_NULL, 0);
  639. tagbuf[0] = tag;
  640. tagbuf[1] = tag >> 8;
  641. tagbuf[2] = tag >> 16;
  642. tagbuf[3] = tag >> 24;
  643. crc = crc32(crc, tagbuf, 4);
  644. put32(f, bswap_32(tag));
  645. if (length > 0) {
  646. crc = crc32(crc, buf, length);
  647. memcpy(*f, buf, length);
  648. *f += length;
  649. }
  650. put32(f, crc);
  651. }
  652. /* XXX: use avcodec generic function ? */
  653. static void to_be32(uint8_t *p, uint32_t v)
  654. {
  655. p[0] = v >> 24;
  656. p[1] = v >> 16;
  657. p[2] = v >> 8;
  658. p[3] = v;
  659. }
  660. /* XXX: do filtering */
  661. static int png_write_row(PNGContext *s, const uint8_t *data, int size)
  662. {
  663. int ret;
  664. s->zstream.avail_in = size;
  665. s->zstream.next_in = (uint8_t *)data;
  666. while (s->zstream.avail_in > 0) {
  667. ret = deflate(&s->zstream, Z_NO_FLUSH);
  668. if (ret != Z_OK)
  669. return -1;
  670. if (s->zstream.avail_out == 0) {
  671. if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
  672. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  673. s->zstream.avail_out = IOBUF_SIZE;
  674. s->zstream.next_out = s->buf;
  675. }
  676. }
  677. return 0;
  678. }
  679. static int common_init(AVCodecContext *avctx){
  680. PNGContext *s = avctx->priv_data;
  681. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  682. avctx->coded_frame= (AVFrame*)&s->picture;
  683. // s->avctx= avctx;
  684. return 0;
  685. }
  686. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  687. PNGContext *s = avctx->priv_data;
  688. AVFrame *pict = data;
  689. AVFrame * const p= (AVFrame*)&s->picture;
  690. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  691. int bits_per_pixel, pass_row_size;
  692. uint8_t *ptr;
  693. uint8_t *crow_buf = NULL;
  694. uint8_t *tmp_buf = NULL;
  695. *p = *pict;
  696. p->pict_type= FF_I_TYPE;
  697. p->key_frame= 1;
  698. s->bytestream_start=
  699. s->bytestream= buf;
  700. s->bytestream_end= buf+buf_size;
  701. is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  702. switch(avctx->pix_fmt) {
  703. case PIX_FMT_RGBA32:
  704. bit_depth = 8;
  705. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  706. break;
  707. case PIX_FMT_RGB24:
  708. bit_depth = 8;
  709. color_type = PNG_COLOR_TYPE_RGB;
  710. break;
  711. case PIX_FMT_GRAY8:
  712. bit_depth = 8;
  713. color_type = PNG_COLOR_TYPE_GRAY;
  714. break;
  715. case PIX_FMT_MONOBLACK:
  716. bit_depth = 1;
  717. color_type = PNG_COLOR_TYPE_GRAY;
  718. break;
  719. case PIX_FMT_PAL8:
  720. bit_depth = 8;
  721. color_type = PNG_COLOR_TYPE_PALETTE;
  722. break;
  723. default:
  724. return -1;
  725. }
  726. bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
  727. row_size = (avctx->width * bits_per_pixel + 7) >> 3;
  728. s->zstream.zalloc = png_zalloc;
  729. s->zstream.zfree = png_zfree;
  730. s->zstream.opaque = NULL;
  731. ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
  732. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  733. if (ret != Z_OK)
  734. return -1;
  735. crow_buf = av_malloc(row_size + 1);
  736. if (!crow_buf)
  737. goto fail;
  738. if (is_progressive) {
  739. tmp_buf = av_malloc(row_size + 1);
  740. if (!tmp_buf)
  741. goto fail;
  742. }
  743. /* write png header */
  744. memcpy(s->bytestream, pngsig, 8);
  745. s->bytestream += 8;
  746. to_be32(s->buf, avctx->width);
  747. to_be32(s->buf + 4, avctx->height);
  748. s->buf[8] = bit_depth;
  749. s->buf[9] = color_type;
  750. s->buf[10] = 0; /* compression type */
  751. s->buf[11] = 0; /* filter type */
  752. s->buf[12] = is_progressive; /* interlace type */
  753. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  754. /* put the palette if needed */
  755. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  756. int has_alpha, alpha, i;
  757. unsigned int v;
  758. uint32_t *palette;
  759. uint8_t *alpha_ptr;
  760. palette = (uint32_t *)p->data[1];
  761. ptr = s->buf;
  762. alpha_ptr = s->buf + 256 * 3;
  763. has_alpha = 0;
  764. for(i = 0; i < 256; i++) {
  765. v = palette[i];
  766. alpha = v >> 24;
  767. if (alpha != 0xff)
  768. has_alpha = 1;
  769. *alpha_ptr++ = alpha;
  770. ptr[0] = v >> 16;
  771. ptr[1] = v >> 8;
  772. ptr[2] = v;
  773. ptr += 3;
  774. }
  775. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  776. if (has_alpha) {
  777. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  778. }
  779. }
  780. /* now put each row */
  781. s->zstream.avail_out = IOBUF_SIZE;
  782. s->zstream.next_out = s->buf;
  783. if (is_progressive) {
  784. uint8_t *ptr1;
  785. int pass;
  786. for(pass = 0; pass < NB_PASSES; pass++) {
  787. /* NOTE: a pass is completely omited if no pixels would be
  788. output */
  789. pass_row_size = png_pass_row_size(pass, bits_per_pixel, avctx->width);
  790. if (pass_row_size > 0) {
  791. for(y = 0; y < avctx->height; y++) {
  792. if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
  793. ptr = p->data[0] + y * p->linesize[0];
  794. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  795. convert_from_rgba32(tmp_buf, ptr, avctx->width);
  796. ptr1 = tmp_buf;
  797. } else {
  798. ptr1 = ptr;
  799. }
  800. png_get_interlaced_row(crow_buf + 1, pass_row_size,
  801. bits_per_pixel, pass,
  802. ptr1, avctx->width);
  803. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  804. png_write_row(s, crow_buf, pass_row_size + 1);
  805. }
  806. }
  807. }
  808. }
  809. } else {
  810. for(y = 0; y < avctx->height; y++) {
  811. ptr = p->data[0] + y * p->linesize[0];
  812. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  813. convert_from_rgba32(crow_buf + 1, ptr, avctx->width);
  814. else
  815. memcpy(crow_buf + 1, ptr, row_size);
  816. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  817. png_write_row(s, crow_buf, row_size + 1);
  818. }
  819. }
  820. /* compress last bytes */
  821. for(;;) {
  822. ret = deflate(&s->zstream, Z_FINISH);
  823. if (ret == Z_OK || ret == Z_STREAM_END) {
  824. len = IOBUF_SIZE - s->zstream.avail_out;
  825. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  826. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  827. }
  828. s->zstream.avail_out = IOBUF_SIZE;
  829. s->zstream.next_out = s->buf;
  830. if (ret == Z_STREAM_END)
  831. break;
  832. } else {
  833. goto fail;
  834. }
  835. }
  836. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  837. ret = s->bytestream - s->bytestream_start;
  838. the_end:
  839. av_free(crow_buf);
  840. av_free(tmp_buf);
  841. deflateEnd(&s->zstream);
  842. return ret;
  843. fail:
  844. ret = -1;
  845. goto the_end;
  846. }
  847. AVCodec png_decoder = {
  848. "png",
  849. CODEC_TYPE_VIDEO,
  850. CODEC_ID_PNG,
  851. sizeof(PNGContext),
  852. common_init,
  853. NULL,
  854. NULL, //decode_end,
  855. decode_frame,
  856. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  857. NULL
  858. };
  859. #ifdef CONFIG_PNG_ENCODER
  860. AVCodec png_encoder = {
  861. "png",
  862. CODEC_TYPE_VIDEO,
  863. CODEC_ID_PNG,
  864. sizeof(PNGContext),
  865. common_init,
  866. encode_frame,
  867. NULL, //encode_end,
  868. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
  869. };
  870. #endif // CONFIG_PNG_ENCODER
  871. #endif