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.

969 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 == 16 &&
  521. s->color_type == PNG_COLOR_TYPE_GRAY) {
  522. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  523. } else if (s->bit_depth == 1 &&
  524. s->color_type == PNG_COLOR_TYPE_GRAY) {
  525. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  526. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  527. avctx->pix_fmt = PIX_FMT_PAL8;
  528. } else {
  529. goto fail;
  530. }
  531. if(p->data[0])
  532. avctx->release_buffer(avctx, p);
  533. p->reference= 0;
  534. if(avctx->get_buffer(avctx, p) < 0){
  535. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  536. goto fail;
  537. }
  538. p->pict_type= FF_I_TYPE;
  539. p->key_frame= 1;
  540. p->interlaced_frame = !!s->interlace_type;
  541. /* compute the compressed row size */
  542. if (!s->interlace_type) {
  543. s->crow_size = s->row_size + 1;
  544. } else {
  545. s->pass = 0;
  546. s->pass_row_size = png_pass_row_size(s->pass,
  547. s->bits_per_pixel,
  548. s->width);
  549. s->crow_size = s->pass_row_size + 1;
  550. }
  551. #ifdef DEBUG
  552. av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
  553. s->row_size, s->crow_size);
  554. #endif
  555. s->image_buf = p->data[0];
  556. s->image_linesize = p->linesize[0];
  557. /* copy the palette if needed */
  558. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  559. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  560. /* empty row is used if differencing to the first row */
  561. s->last_row = av_mallocz(s->row_size);
  562. if (!s->last_row)
  563. goto fail;
  564. if (s->interlace_type ||
  565. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  566. s->tmp_row = av_malloc(s->row_size);
  567. if (!s->tmp_row)
  568. goto fail;
  569. }
  570. /* compressed row */
  571. s->crow_buf = av_malloc(s->row_size + 1);
  572. if (!s->crow_buf)
  573. goto fail;
  574. s->zstream.avail_out = s->crow_size;
  575. s->zstream.next_out = s->crow_buf;
  576. }
  577. s->state |= PNG_IDAT;
  578. if (png_decode_idat(s, length) < 0)
  579. goto fail;
  580. /* skip crc */
  581. crc = get32(&s->bytestream);
  582. break;
  583. case MKTAG('P', 'L', 'T', 'E'):
  584. {
  585. int n, i, r, g, b;
  586. if ((length % 3) != 0 || length > 256 * 3)
  587. goto skip_tag;
  588. /* read the palette */
  589. n = length / 3;
  590. for(i=0;i<n;i++) {
  591. r = *s->bytestream++;
  592. g = *s->bytestream++;
  593. b = *s->bytestream++;
  594. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  595. }
  596. for(;i<256;i++) {
  597. s->palette[i] = (0xff << 24);
  598. }
  599. s->state |= PNG_PLTE;
  600. crc = get32(&s->bytestream);
  601. }
  602. break;
  603. case MKTAG('t', 'R', 'N', 'S'):
  604. {
  605. int v, i;
  606. /* read the transparency. XXX: Only palette mode supported */
  607. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  608. length > 256 ||
  609. !(s->state & PNG_PLTE))
  610. goto skip_tag;
  611. for(i=0;i<length;i++) {
  612. v = *s->bytestream++;
  613. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  614. }
  615. crc = get32(&s->bytestream);
  616. }
  617. break;
  618. case MKTAG('I', 'E', 'N', 'D'):
  619. if (!(s->state & PNG_ALLIMAGE))
  620. goto fail;
  621. crc = get32(&s->bytestream);
  622. goto exit_loop;
  623. default:
  624. /* skip tag */
  625. skip_tag:
  626. s->bytestream += length + 4;
  627. break;
  628. }
  629. }
  630. exit_loop:
  631. *picture= *(AVFrame*)&s->picture;
  632. *data_size = sizeof(AVPicture);
  633. ret = s->bytestream - s->bytestream_start;
  634. the_end:
  635. inflateEnd(&s->zstream);
  636. av_freep(&s->crow_buf);
  637. av_freep(&s->last_row);
  638. av_freep(&s->tmp_row);
  639. return ret;
  640. fail:
  641. ret = -1;
  642. goto the_end;
  643. }
  644. #endif
  645. #ifdef CONFIG_ENCODERS
  646. static void png_write_chunk(uint8_t **f, uint32_t tag,
  647. const uint8_t *buf, int length)
  648. {
  649. uint32_t crc;
  650. uint8_t tagbuf[4];
  651. put32(f, length);
  652. crc = crc32(0, Z_NULL, 0);
  653. tagbuf[0] = tag;
  654. tagbuf[1] = tag >> 8;
  655. tagbuf[2] = tag >> 16;
  656. tagbuf[3] = tag >> 24;
  657. crc = crc32(crc, tagbuf, 4);
  658. put32(f, bswap_32(tag));
  659. if (length > 0) {
  660. crc = crc32(crc, buf, length);
  661. memcpy(*f, buf, length);
  662. *f += length;
  663. }
  664. put32(f, crc);
  665. }
  666. /* XXX: use avcodec generic function ? */
  667. static void to_be32(uint8_t *p, uint32_t v)
  668. {
  669. p[0] = v >> 24;
  670. p[1] = v >> 16;
  671. p[2] = v >> 8;
  672. p[3] = v;
  673. }
  674. /* XXX: do filtering */
  675. static int png_write_row(PNGContext *s, const uint8_t *data, int size)
  676. {
  677. int ret;
  678. s->zstream.avail_in = size;
  679. s->zstream.next_in = (uint8_t *)data;
  680. while (s->zstream.avail_in > 0) {
  681. ret = deflate(&s->zstream, Z_NO_FLUSH);
  682. if (ret != Z_OK)
  683. return -1;
  684. if (s->zstream.avail_out == 0) {
  685. if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
  686. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  687. s->zstream.avail_out = IOBUF_SIZE;
  688. s->zstream.next_out = s->buf;
  689. }
  690. }
  691. return 0;
  692. }
  693. #endif /* CONFIG_ENCODERS */
  694. static int common_init(AVCodecContext *avctx){
  695. PNGContext *s = avctx->priv_data;
  696. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  697. avctx->coded_frame= (AVFrame*)&s->picture;
  698. // s->avctx= avctx;
  699. return 0;
  700. }
  701. #ifdef CONFIG_ENCODERS
  702. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  703. PNGContext *s = avctx->priv_data;
  704. AVFrame *pict = data;
  705. AVFrame * const p= (AVFrame*)&s->picture;
  706. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  707. int bits_per_pixel, pass_row_size;
  708. uint8_t *ptr;
  709. uint8_t *crow_buf = NULL;
  710. uint8_t *tmp_buf = NULL;
  711. *p = *pict;
  712. p->pict_type= FF_I_TYPE;
  713. p->key_frame= 1;
  714. s->bytestream_start=
  715. s->bytestream= buf;
  716. s->bytestream_end= buf+buf_size;
  717. is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  718. switch(avctx->pix_fmt) {
  719. case PIX_FMT_RGBA32:
  720. bit_depth = 8;
  721. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  722. break;
  723. case PIX_FMT_RGB24:
  724. bit_depth = 8;
  725. color_type = PNG_COLOR_TYPE_RGB;
  726. break;
  727. case PIX_FMT_GRAY8:
  728. bit_depth = 8;
  729. color_type = PNG_COLOR_TYPE_GRAY;
  730. break;
  731. case PIX_FMT_MONOBLACK:
  732. bit_depth = 1;
  733. color_type = PNG_COLOR_TYPE_GRAY;
  734. break;
  735. case PIX_FMT_PAL8:
  736. bit_depth = 8;
  737. color_type = PNG_COLOR_TYPE_PALETTE;
  738. break;
  739. default:
  740. return -1;
  741. }
  742. bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
  743. row_size = (avctx->width * bits_per_pixel + 7) >> 3;
  744. s->zstream.zalloc = png_zalloc;
  745. s->zstream.zfree = png_zfree;
  746. s->zstream.opaque = NULL;
  747. ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
  748. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  749. if (ret != Z_OK)
  750. return -1;
  751. crow_buf = av_malloc(row_size + 1);
  752. if (!crow_buf)
  753. goto fail;
  754. if (is_progressive) {
  755. tmp_buf = av_malloc(row_size + 1);
  756. if (!tmp_buf)
  757. goto fail;
  758. }
  759. /* write png header */
  760. memcpy(s->bytestream, pngsig, 8);
  761. s->bytestream += 8;
  762. to_be32(s->buf, avctx->width);
  763. to_be32(s->buf + 4, avctx->height);
  764. s->buf[8] = bit_depth;
  765. s->buf[9] = color_type;
  766. s->buf[10] = 0; /* compression type */
  767. s->buf[11] = 0; /* filter type */
  768. s->buf[12] = is_progressive; /* interlace type */
  769. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  770. /* put the palette if needed */
  771. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  772. int has_alpha, alpha, i;
  773. unsigned int v;
  774. uint32_t *palette;
  775. uint8_t *alpha_ptr;
  776. palette = (uint32_t *)p->data[1];
  777. ptr = s->buf;
  778. alpha_ptr = s->buf + 256 * 3;
  779. has_alpha = 0;
  780. for(i = 0; i < 256; i++) {
  781. v = palette[i];
  782. alpha = v >> 24;
  783. if (alpha && alpha != 0xff)
  784. has_alpha = 1;
  785. *alpha_ptr++ = alpha;
  786. ptr[0] = v >> 16;
  787. ptr[1] = v >> 8;
  788. ptr[2] = v;
  789. ptr += 3;
  790. }
  791. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  792. if (has_alpha) {
  793. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  794. }
  795. }
  796. /* now put each row */
  797. s->zstream.avail_out = IOBUF_SIZE;
  798. s->zstream.next_out = s->buf;
  799. if (is_progressive) {
  800. uint8_t *ptr1;
  801. int pass;
  802. for(pass = 0; pass < NB_PASSES; pass++) {
  803. /* NOTE: a pass is completely omited if no pixels would be
  804. output */
  805. pass_row_size = png_pass_row_size(pass, bits_per_pixel, avctx->width);
  806. if (pass_row_size > 0) {
  807. for(y = 0; y < avctx->height; y++) {
  808. if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
  809. ptr = p->data[0] + y * p->linesize[0];
  810. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  811. convert_from_rgba32(tmp_buf, ptr, avctx->width);
  812. ptr1 = tmp_buf;
  813. } else {
  814. ptr1 = ptr;
  815. }
  816. png_get_interlaced_row(crow_buf + 1, pass_row_size,
  817. bits_per_pixel, pass,
  818. ptr1, avctx->width);
  819. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  820. png_write_row(s, crow_buf, pass_row_size + 1);
  821. }
  822. }
  823. }
  824. }
  825. } else {
  826. for(y = 0; y < avctx->height; y++) {
  827. ptr = p->data[0] + y * p->linesize[0];
  828. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  829. convert_from_rgba32(crow_buf + 1, ptr, avctx->width);
  830. else
  831. memcpy(crow_buf + 1, ptr, row_size);
  832. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  833. png_write_row(s, crow_buf, row_size + 1);
  834. }
  835. }
  836. /* compress last bytes */
  837. for(;;) {
  838. ret = deflate(&s->zstream, Z_FINISH);
  839. if (ret == Z_OK || ret == Z_STREAM_END) {
  840. len = IOBUF_SIZE - s->zstream.avail_out;
  841. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  842. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  843. }
  844. s->zstream.avail_out = IOBUF_SIZE;
  845. s->zstream.next_out = s->buf;
  846. if (ret == Z_STREAM_END)
  847. break;
  848. } else {
  849. goto fail;
  850. }
  851. }
  852. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  853. ret = s->bytestream - s->bytestream_start;
  854. the_end:
  855. av_free(crow_buf);
  856. av_free(tmp_buf);
  857. deflateEnd(&s->zstream);
  858. return ret;
  859. fail:
  860. ret = -1;
  861. goto the_end;
  862. }
  863. #endif
  864. #ifdef CONFIG_PNG_DECODER
  865. AVCodec png_decoder = {
  866. "png",
  867. CODEC_TYPE_VIDEO,
  868. CODEC_ID_PNG,
  869. sizeof(PNGContext),
  870. common_init,
  871. NULL,
  872. NULL, //decode_end,
  873. decode_frame,
  874. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  875. NULL
  876. };
  877. #endif
  878. #ifdef CONFIG_PNG_ENCODER
  879. AVCodec png_encoder = {
  880. "png",
  881. CODEC_TYPE_VIDEO,
  882. CODEC_ID_PNG,
  883. sizeof(PNGContext),
  884. common_init,
  885. encode_frame,
  886. NULL, //encode_end,
  887. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
  888. };
  889. #endif // CONFIG_PNG_ENCODER
  890. #endif