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.

964 lines
29KB

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