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.

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