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.

902 lines
30KB

  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. //#define DEBUG
  22. #include "libavutil/bprint.h"
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "png.h"
  27. #include "pngdsp.h"
  28. /* TODO:
  29. * - add 16 bit depth support
  30. */
  31. #include <zlib.h>
  32. //#define DEBUG
  33. typedef struct PNGDecContext {
  34. PNGDSPContext dsp;
  35. AVCodecContext *avctx;
  36. GetByteContext gb;
  37. AVFrame picture1, picture2;
  38. AVFrame *current_picture, *last_picture;
  39. int state;
  40. int width, height;
  41. int bit_depth;
  42. int color_type;
  43. int compression_type;
  44. int interlace_type;
  45. int filter_type;
  46. int channels;
  47. int bits_per_pixel;
  48. int bpp;
  49. uint8_t *image_buf;
  50. int image_linesize;
  51. uint32_t palette[256];
  52. uint8_t *crow_buf;
  53. uint8_t *last_row;
  54. uint8_t *tmp_row;
  55. int pass;
  56. int crow_size; /* compressed row size (include filter type) */
  57. int row_size; /* decompressed row size */
  58. int pass_row_size; /* decompress row size of the current pass */
  59. int y;
  60. z_stream zstream;
  61. } PNGDecContext;
  62. /* Mask to determine which pixels are valid in a pass */
  63. static const uint8_t png_pass_mask[NB_PASSES] = {
  64. 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
  65. };
  66. /* Mask to determine which y pixels can be written in a pass */
  67. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  68. 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  69. };
  70. /* Mask to determine which pixels to overwrite while displaying */
  71. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  72. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  73. };
  74. /* NOTE: we try to construct a good looking image at each pass. width
  75. is the original image width. We also do pixel format conversion at
  76. this stage */
  77. static void png_put_interlaced_row(uint8_t *dst, int width,
  78. int bits_per_pixel, int pass,
  79. int color_type, const uint8_t *src)
  80. {
  81. int x, mask, dsp_mask, j, src_x, b, bpp;
  82. uint8_t *d;
  83. const uint8_t *s;
  84. mask = png_pass_mask[pass];
  85. dsp_mask = png_pass_dsp_mask[pass];
  86. switch(bits_per_pixel) {
  87. case 1:
  88. src_x = 0;
  89. for(x = 0; x < width; x++) {
  90. j = (x & 7);
  91. if ((dsp_mask << j) & 0x80) {
  92. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  93. dst[x >> 3] &= 0xFF7F>>j;
  94. dst[x >> 3] |= b << (7 - j);
  95. }
  96. if ((mask << j) & 0x80)
  97. src_x++;
  98. }
  99. break;
  100. case 2:
  101. src_x = 0;
  102. for(x = 0; x < width; x++) {
  103. int j2 = 2*(x&3);
  104. j = (x & 7);
  105. if ((dsp_mask << j) & 0x80) {
  106. b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
  107. dst[x >> 2] &= 0xFF3F>>j2;
  108. dst[x >> 2] |= b << (6 - j2);
  109. }
  110. if ((mask << j) & 0x80)
  111. src_x++;
  112. }
  113. break;
  114. case 4:
  115. src_x = 0;
  116. for(x = 0; x < width; x++) {
  117. int j2 = 4*(x&1);
  118. j = (x & 7);
  119. if ((dsp_mask << j) & 0x80) {
  120. b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
  121. dst[x >> 1] &= 0xFF0F>>j2;
  122. dst[x >> 1] |= b << (4 - j2);
  123. }
  124. if ((mask << j) & 0x80)
  125. src_x++;
  126. }
  127. break;
  128. default:
  129. bpp = bits_per_pixel >> 3;
  130. d = dst;
  131. s = src;
  132. for(x = 0; x < width; x++) {
  133. j = x & 7;
  134. if ((dsp_mask << j) & 0x80) {
  135. memcpy(d, s, bpp);
  136. }
  137. d += bpp;
  138. if ((mask << j) & 0x80)
  139. s += bpp;
  140. }
  141. break;
  142. }
  143. }
  144. void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
  145. {
  146. int i;
  147. for(i = 0; i < w; i++) {
  148. int a, b, c, p, pa, pb, pc;
  149. a = dst[i - bpp];
  150. b = top[i];
  151. c = top[i - bpp];
  152. p = b - c;
  153. pc = a - c;
  154. pa = abs(p);
  155. pb = abs(pc);
  156. pc = abs(p + pc);
  157. if (pa <= pb && pa <= pc)
  158. p = a;
  159. else if (pb <= pc)
  160. p = b;
  161. else
  162. p = c;
  163. dst[i] = p + src[i];
  164. }
  165. }
  166. #define UNROLL1(bpp, op) {\
  167. r = dst[0];\
  168. if(bpp >= 2) g = dst[1];\
  169. if(bpp >= 3) b = dst[2];\
  170. if(bpp >= 4) a = dst[3];\
  171. for(; i < size; i+=bpp) {\
  172. dst[i+0] = r = op(r, src[i+0], last[i+0]);\
  173. if(bpp == 1) continue;\
  174. dst[i+1] = g = op(g, src[i+1], last[i+1]);\
  175. if(bpp == 2) continue;\
  176. dst[i+2] = b = op(b, src[i+2], last[i+2]);\
  177. if(bpp == 3) continue;\
  178. dst[i+3] = a = op(a, src[i+3], last[i+3]);\
  179. }\
  180. }
  181. #define UNROLL_FILTER(op)\
  182. if(bpp == 1) UNROLL1(1, op)\
  183. else if(bpp == 2) UNROLL1(2, op)\
  184. else if(bpp == 3) UNROLL1(3, op)\
  185. else if(bpp == 4) UNROLL1(4, op)\
  186. else {\
  187. for (; i < size; i += bpp) {\
  188. int j;\
  189. for (j = 0; j < bpp; j++)\
  190. dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
  191. }\
  192. }
  193. /* NOTE: 'dst' can be equal to 'last' */
  194. static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
  195. uint8_t *src, uint8_t *last, int size, int bpp)
  196. {
  197. int i, p, r, g, b, a;
  198. switch(filter_type) {
  199. case PNG_FILTER_VALUE_NONE:
  200. memcpy(dst, src, size);
  201. break;
  202. case PNG_FILTER_VALUE_SUB:
  203. for(i = 0; i < bpp; i++) {
  204. dst[i] = src[i];
  205. }
  206. if(bpp == 4) {
  207. p = *(int*)dst;
  208. for(; i < size; i+=bpp) {
  209. int s = *(int*)(src+i);
  210. p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
  211. *(int*)(dst+i) = p;
  212. }
  213. } else {
  214. #define OP_SUB(x,s,l) x+s
  215. UNROLL_FILTER(OP_SUB);
  216. }
  217. break;
  218. case PNG_FILTER_VALUE_UP:
  219. dsp->add_bytes_l2(dst, src, last, size);
  220. break;
  221. case PNG_FILTER_VALUE_AVG:
  222. for(i = 0; i < bpp; i++) {
  223. p = (last[i] >> 1);
  224. dst[i] = p + src[i];
  225. }
  226. #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
  227. UNROLL_FILTER(OP_AVG);
  228. break;
  229. case PNG_FILTER_VALUE_PAETH:
  230. for(i = 0; i < bpp; i++) {
  231. p = last[i];
  232. dst[i] = p + src[i];
  233. }
  234. if(bpp > 2 && size > 4) {
  235. // would write off the end of the array if we let it process the last pixel with bpp=3
  236. int w = bpp==4 ? size : size-3;
  237. dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
  238. i = w;
  239. }
  240. ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
  241. break;
  242. }
  243. }
  244. /* This used to be called "deloco" in FFmpeg
  245. * and is actually an inverse reversible colorspace transformation */
  246. #define YUV2RGB(NAME, TYPE) \
  247. static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
  248. { \
  249. int i; \
  250. for (i = 0; i < size; i += 3 + alpha) { \
  251. int g = dst [i+1]; \
  252. dst[i+0] += g; \
  253. dst[i+2] += g; \
  254. } \
  255. }
  256. YUV2RGB(rgb8, uint8_t)
  257. YUV2RGB(rgb16, uint16_t)
  258. /* process exactly one decompressed row */
  259. static void png_handle_row(PNGDecContext *s)
  260. {
  261. uint8_t *ptr, *last_row;
  262. int got_line;
  263. if (!s->interlace_type) {
  264. ptr = s->image_buf + s->image_linesize * s->y;
  265. if (s->y == 0)
  266. last_row = s->last_row;
  267. else
  268. last_row = ptr - s->image_linesize;
  269. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  270. last_row, s->row_size, s->bpp);
  271. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  272. if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
  273. if (s->bit_depth == 16) {
  274. deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
  275. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  276. } else {
  277. deloco_rgb8(ptr - s->image_linesize, s->row_size,
  278. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  279. }
  280. }
  281. s->y++;
  282. if (s->y == s->height) {
  283. s->state |= PNG_ALLIMAGE;
  284. if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
  285. if (s->bit_depth == 16) {
  286. deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
  287. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  288. } else {
  289. deloco_rgb8(ptr, s->row_size,
  290. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  291. }
  292. }
  293. }
  294. } else {
  295. got_line = 0;
  296. for(;;) {
  297. ptr = s->image_buf + s->image_linesize * s->y;
  298. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  299. /* if we already read one row, it is time to stop to
  300. wait for the next one */
  301. if (got_line)
  302. break;
  303. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  304. s->last_row, s->pass_row_size, s->bpp);
  305. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  306. got_line = 1;
  307. }
  308. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  309. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  310. s->color_type, s->last_row);
  311. }
  312. s->y++;
  313. if (s->y == s->height) {
  314. memset(s->last_row, 0, s->row_size);
  315. for(;;) {
  316. if (s->pass == NB_PASSES - 1) {
  317. s->state |= PNG_ALLIMAGE;
  318. goto the_end;
  319. } else {
  320. s->pass++;
  321. s->y = 0;
  322. s->pass_row_size = ff_png_pass_row_size(s->pass,
  323. s->bits_per_pixel,
  324. s->width);
  325. s->crow_size = s->pass_row_size + 1;
  326. if (s->pass_row_size != 0)
  327. break;
  328. /* skip pass if empty row */
  329. }
  330. }
  331. }
  332. }
  333. the_end: ;
  334. }
  335. }
  336. static int png_decode_idat(PNGDecContext *s, int length)
  337. {
  338. int ret;
  339. s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
  340. s->zstream.next_in = (unsigned char *)s->gb.buffer;
  341. bytestream2_skip(&s->gb, length);
  342. /* decode one line if possible */
  343. while (s->zstream.avail_in > 0) {
  344. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  345. if (ret != Z_OK && ret != Z_STREAM_END) {
  346. av_log(s->avctx, AV_LOG_ERROR, "inflate returned %d\n", ret);
  347. return -1;
  348. }
  349. if (s->zstream.avail_out == 0) {
  350. if (!(s->state & PNG_ALLIMAGE)) {
  351. png_handle_row(s);
  352. }
  353. s->zstream.avail_out = s->crow_size;
  354. s->zstream.next_out = s->crow_buf;
  355. }
  356. }
  357. return 0;
  358. }
  359. static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
  360. const uint8_t *data_end)
  361. {
  362. z_stream zstream;
  363. unsigned char *buf;
  364. unsigned buf_size;
  365. int ret;
  366. zstream.zalloc = ff_png_zalloc;
  367. zstream.zfree = ff_png_zfree;
  368. zstream.opaque = NULL;
  369. if (inflateInit(&zstream) != Z_OK)
  370. return AVERROR_EXTERNAL;
  371. zstream.next_in = (unsigned char *)data;
  372. zstream.avail_in = data_end - data;
  373. av_bprint_init(bp, 0, -1);
  374. while (zstream.avail_in > 0) {
  375. av_bprint_get_buffer(bp, 1, &buf, &buf_size);
  376. if (!buf_size) {
  377. ret = AVERROR(ENOMEM);
  378. goto fail;
  379. }
  380. zstream.next_out = buf;
  381. zstream.avail_out = buf_size;
  382. ret = inflate(&zstream, Z_PARTIAL_FLUSH);
  383. if (ret != Z_OK && ret != Z_STREAM_END) {
  384. ret = AVERROR_EXTERNAL;
  385. goto fail;
  386. }
  387. bp->len += zstream.next_out - buf;
  388. if (ret == Z_STREAM_END)
  389. break;
  390. }
  391. inflateEnd(&zstream);
  392. bp->str[bp->len] = 0;
  393. return 0;
  394. fail:
  395. inflateEnd(&zstream);
  396. av_bprint_finalize(bp, NULL);
  397. return ret;
  398. }
  399. static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
  400. {
  401. size_t extra = 0, i;
  402. uint8_t *out, *q;
  403. for (i = 0; i < size_in; i++)
  404. extra += in[i] >= 0x80;
  405. if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
  406. return NULL;
  407. q = out = av_malloc(size_in + extra + 1);
  408. if (!out)
  409. return NULL;
  410. for (i = 0; i < size_in; i++) {
  411. if (in[i] >= 0x80) {
  412. *(q++) = 0xC0 | (in[i] >> 6);
  413. *(q++) = 0x80 | (in[i] & 0x3F);
  414. } else {
  415. *(q++) = in[i];
  416. }
  417. }
  418. *(q++) = 0;
  419. return out;
  420. }
  421. static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
  422. AVDictionary **dict)
  423. {
  424. int ret, method;
  425. const uint8_t *data = s->gb.buffer;
  426. const uint8_t *data_end = data + length;
  427. const uint8_t *keyword = data;
  428. const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
  429. uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
  430. unsigned text_len;
  431. AVBPrint bp;
  432. if (!keyword_end)
  433. return AVERROR_INVALIDDATA;
  434. data = keyword_end + 1;
  435. if (compressed) {
  436. if (data == data_end)
  437. return AVERROR_INVALIDDATA;
  438. method = *(data++);
  439. if (method)
  440. return AVERROR_INVALIDDATA;
  441. if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
  442. return ret;
  443. text_len = bp.len;
  444. av_bprint_finalize(&bp, (char **)&text);
  445. if (!text)
  446. return AVERROR(ENOMEM);
  447. } else {
  448. text = (uint8_t *)data;
  449. text_len = data_end - text;
  450. }
  451. kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
  452. txt_utf8 = iso88591_to_utf8(text, text_len);
  453. if (text != data)
  454. av_free(text);
  455. if (!(kw_utf8 && txt_utf8)) {
  456. av_free(kw_utf8);
  457. av_free(txt_utf8);
  458. return AVERROR(ENOMEM);
  459. }
  460. av_dict_set(dict, kw_utf8, txt_utf8,
  461. AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  462. return 0;
  463. }
  464. static int decode_frame(AVCodecContext *avctx,
  465. void *data, int *data_size,
  466. AVPacket *avpkt)
  467. {
  468. const uint8_t *buf = avpkt->data;
  469. int buf_size = avpkt->size;
  470. PNGDecContext * const s = avctx->priv_data;
  471. AVFrame *picture = data;
  472. AVFrame *p;
  473. AVDictionary *metadata = NULL;
  474. uint8_t *crow_buf_base = NULL;
  475. uint32_t tag, length;
  476. int64_t sig;
  477. int ret;
  478. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  479. avctx->coded_frame= s->current_picture;
  480. p = s->current_picture;
  481. bytestream2_init(&s->gb, buf, buf_size);
  482. /* check signature */
  483. sig = bytestream2_get_be64(&s->gb);
  484. if (sig != PNGSIG &&
  485. sig != MNGSIG) {
  486. av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
  487. return -1;
  488. }
  489. s->y=
  490. s->state=0;
  491. // memset(s, 0, sizeof(PNGDecContext));
  492. /* init the zlib */
  493. s->zstream.zalloc = ff_png_zalloc;
  494. s->zstream.zfree = ff_png_zfree;
  495. s->zstream.opaque = NULL;
  496. ret = inflateInit(&s->zstream);
  497. if (ret != Z_OK) {
  498. av_log(avctx, AV_LOG_ERROR, "inflateInit returned %d\n", ret);
  499. return -1;
  500. }
  501. for(;;) {
  502. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  503. av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
  504. goto fail;
  505. }
  506. length = bytestream2_get_be32(&s->gb);
  507. if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
  508. av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
  509. goto fail;
  510. }
  511. tag = bytestream2_get_le32(&s->gb);
  512. if (avctx->debug & FF_DEBUG_STARTCODE)
  513. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  514. (tag & 0xff),
  515. ((tag >> 8) & 0xff),
  516. ((tag >> 16) & 0xff),
  517. ((tag >> 24) & 0xff), length);
  518. switch(tag) {
  519. case MKTAG('I', 'H', 'D', 'R'):
  520. if (length != 13)
  521. goto fail;
  522. s->width = bytestream2_get_be32(&s->gb);
  523. s->height = bytestream2_get_be32(&s->gb);
  524. if(av_image_check_size(s->width, s->height, 0, avctx)){
  525. s->width= s->height= 0;
  526. av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
  527. goto fail;
  528. }
  529. s->bit_depth = bytestream2_get_byte(&s->gb);
  530. s->color_type = bytestream2_get_byte(&s->gb);
  531. s->compression_type = bytestream2_get_byte(&s->gb);
  532. s->filter_type = bytestream2_get_byte(&s->gb);
  533. s->interlace_type = bytestream2_get_byte(&s->gb);
  534. bytestream2_skip(&s->gb, 4); /* crc */
  535. s->state |= PNG_IHDR;
  536. if (avctx->debug & FF_DEBUG_PICT_INFO)
  537. 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",
  538. s->width, s->height, s->bit_depth, s->color_type,
  539. s->compression_type, s->filter_type, s->interlace_type);
  540. break;
  541. case MKTAG('p', 'H', 'Y', 's'):
  542. if (s->state & PNG_IDAT) {
  543. av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
  544. goto fail;
  545. }
  546. avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
  547. avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
  548. if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
  549. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  550. bytestream2_skip(&s->gb, 1); /* unit specifier */
  551. bytestream2_skip(&s->gb, 4); /* crc */
  552. break;
  553. case MKTAG('I', 'D', 'A', 'T'):
  554. if (!(s->state & PNG_IHDR)) {
  555. av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
  556. goto fail;
  557. }
  558. if (!(s->state & PNG_IDAT)) {
  559. /* init image info */
  560. avctx->width = s->width;
  561. avctx->height = s->height;
  562. s->channels = ff_png_get_nb_channels(s->color_type);
  563. s->bits_per_pixel = s->bit_depth * s->channels;
  564. s->bpp = (s->bits_per_pixel + 7) >> 3;
  565. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  566. if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  567. s->color_type == PNG_COLOR_TYPE_RGB) {
  568. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  569. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  570. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  571. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  572. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  573. s->color_type == PNG_COLOR_TYPE_GRAY) {
  574. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  575. } else if (s->bit_depth == 16 &&
  576. s->color_type == PNG_COLOR_TYPE_GRAY) {
  577. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  578. } else if (s->bit_depth == 16 &&
  579. s->color_type == PNG_COLOR_TYPE_RGB) {
  580. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  581. } else if (s->bit_depth == 16 &&
  582. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  583. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  584. } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
  585. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  586. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  587. } else if (s->bit_depth == 1) {
  588. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  589. } else if (s->bit_depth == 8 &&
  590. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  591. avctx->pix_fmt = AV_PIX_FMT_Y400A;
  592. } else {
  593. av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
  594. "and color type %d\n",
  595. s->bit_depth, s->color_type);
  596. goto fail;
  597. }
  598. if(p->data[0])
  599. avctx->release_buffer(avctx, p);
  600. p->reference= 3;
  601. if(avctx->get_buffer(avctx, p) < 0){
  602. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  603. goto fail;
  604. }
  605. p->pict_type= AV_PICTURE_TYPE_I;
  606. p->key_frame= 1;
  607. p->interlaced_frame = !!s->interlace_type;
  608. /* compute the compressed row size */
  609. if (!s->interlace_type) {
  610. s->crow_size = s->row_size + 1;
  611. } else {
  612. s->pass = 0;
  613. s->pass_row_size = ff_png_pass_row_size(s->pass,
  614. s->bits_per_pixel,
  615. s->width);
  616. s->crow_size = s->pass_row_size + 1;
  617. }
  618. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  619. s->row_size, s->crow_size);
  620. s->image_buf = p->data[0];
  621. s->image_linesize = p->linesize[0];
  622. /* copy the palette if needed */
  623. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  624. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  625. /* empty row is used if differencing to the first row */
  626. s->last_row = av_mallocz(s->row_size);
  627. if (!s->last_row)
  628. goto fail;
  629. if (s->interlace_type ||
  630. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  631. s->tmp_row = av_malloc(s->row_size);
  632. if (!s->tmp_row)
  633. goto fail;
  634. }
  635. /* compressed row */
  636. crow_buf_base = av_malloc(s->row_size + 16);
  637. if (!crow_buf_base)
  638. goto fail;
  639. /* we want crow_buf+1 to be 16-byte aligned */
  640. s->crow_buf = crow_buf_base + 15;
  641. s->zstream.avail_out = s->crow_size;
  642. s->zstream.next_out = s->crow_buf;
  643. }
  644. s->state |= PNG_IDAT;
  645. if (png_decode_idat(s, length) < 0)
  646. goto fail;
  647. bytestream2_skip(&s->gb, 4); /* crc */
  648. break;
  649. case MKTAG('P', 'L', 'T', 'E'):
  650. {
  651. int n, i, r, g, b;
  652. if ((length % 3) != 0 || length > 256 * 3)
  653. goto skip_tag;
  654. /* read the palette */
  655. n = length / 3;
  656. for(i=0;i<n;i++) {
  657. r = bytestream2_get_byte(&s->gb);
  658. g = bytestream2_get_byte(&s->gb);
  659. b = bytestream2_get_byte(&s->gb);
  660. s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  661. }
  662. for(;i<256;i++) {
  663. s->palette[i] = (0xFFU << 24);
  664. }
  665. s->state |= PNG_PLTE;
  666. bytestream2_skip(&s->gb, 4); /* crc */
  667. }
  668. break;
  669. case MKTAG('t', 'R', 'N', 'S'):
  670. {
  671. int v, i;
  672. /* read the transparency. XXX: Only palette mode supported */
  673. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  674. length > 256 ||
  675. !(s->state & PNG_PLTE))
  676. goto skip_tag;
  677. for(i=0;i<length;i++) {
  678. v = bytestream2_get_byte(&s->gb);
  679. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  680. }
  681. bytestream2_skip(&s->gb, 4); /* crc */
  682. }
  683. break;
  684. case MKTAG('t', 'E', 'X', 't'):
  685. if (decode_text_chunk(s, length, 0, &metadata) < 0)
  686. av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
  687. bytestream2_skip(&s->gb, length + 4);
  688. break;
  689. case MKTAG('z', 'T', 'X', 't'):
  690. if (decode_text_chunk(s, length, 1, &metadata) < 0)
  691. av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
  692. bytestream2_skip(&s->gb, length + 4);
  693. break;
  694. case MKTAG('I', 'E', 'N', 'D'):
  695. if (!(s->state & PNG_ALLIMAGE))
  696. av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
  697. if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
  698. goto fail;
  699. }
  700. bytestream2_skip(&s->gb, 4); /* crc */
  701. goto exit_loop;
  702. default:
  703. /* skip tag */
  704. skip_tag:
  705. bytestream2_skip(&s->gb, length + 4);
  706. break;
  707. }
  708. }
  709. exit_loop:
  710. if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
  711. int i, j;
  712. uint8_t *pd = s->current_picture->data[0];
  713. for(j=0; j < s->height; j++) {
  714. for(i=s->width/8-1; i>=0; i--) {
  715. pd[8*i+7]= pd[i] &1;
  716. pd[8*i+6]= (pd[i]>>1)&1;
  717. pd[8*i+5]= (pd[i]>>2)&1;
  718. pd[8*i+4]= (pd[i]>>3)&1;
  719. pd[8*i+3]= (pd[i]>>4)&1;
  720. pd[8*i+2]= (pd[i]>>5)&1;
  721. pd[8*i+1]= (pd[i]>>6)&1;
  722. pd[8*i+0]= pd[i]>>7;
  723. }
  724. pd += s->image_linesize;
  725. }
  726. }
  727. if(s->bits_per_pixel == 2){
  728. int i, j;
  729. uint8_t *pd = s->current_picture->data[0];
  730. for(j=0; j < s->height; j++) {
  731. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  732. for(i=s->width/4-1; i>=0; i--) {
  733. pd[4*i+3]= pd[i] &3;
  734. pd[4*i+2]= (pd[i]>>2)&3;
  735. pd[4*i+1]= (pd[i]>>4)&3;
  736. pd[4*i+0]= pd[i]>>6;
  737. }
  738. } else {
  739. for(i=s->width/4-1; i>=0; i--) {
  740. pd[4*i+3]= ( pd[i] &3)*0x55;
  741. pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
  742. pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
  743. pd[4*i+0]= ( pd[i]>>6 )*0x55;
  744. }
  745. }
  746. pd += s->image_linesize;
  747. }
  748. }
  749. if(s->bits_per_pixel == 4){
  750. int i, j;
  751. uint8_t *pd = s->current_picture->data[0];
  752. for(j=0; j < s->height; j++) {
  753. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  754. for(i=s->width/2-1; i>=0; i--) {
  755. pd[2*i+1]= pd[i]&15;
  756. pd[2*i+0]= pd[i]>>4;
  757. }
  758. } else {
  759. for(i=s->width/2-1; i>=0; i--) {
  760. pd[2*i+1]= (pd[i]&15)*0x11;
  761. pd[2*i+0]= (pd[i]>>4)*0x11;
  762. }
  763. }
  764. pd += s->image_linesize;
  765. }
  766. }
  767. /* handle p-frames only if a predecessor frame is available */
  768. if(s->last_picture->data[0] != NULL) {
  769. if( !(avpkt->flags & AV_PKT_FLAG_KEY)
  770. && s->last_picture->width == s->current_picture->width
  771. && s->last_picture->height== s->current_picture->height
  772. && s->last_picture->format== s->current_picture->format
  773. ) {
  774. int i, j;
  775. uint8_t *pd = s->current_picture->data[0];
  776. uint8_t *pd_last = s->last_picture->data[0];
  777. for(j=0; j < s->height; j++) {
  778. for(i=0; i < s->width * s->bpp; i++) {
  779. pd[i] += pd_last[i];
  780. }
  781. pd += s->image_linesize;
  782. pd_last += s->image_linesize;
  783. }
  784. }
  785. }
  786. s->current_picture->metadata = metadata;
  787. metadata = NULL;
  788. *picture= *s->current_picture;
  789. *data_size = sizeof(AVFrame);
  790. ret = bytestream2_tell(&s->gb);
  791. the_end:
  792. inflateEnd(&s->zstream);
  793. av_free(crow_buf_base);
  794. s->crow_buf = NULL;
  795. av_freep(&s->last_row);
  796. av_freep(&s->tmp_row);
  797. return ret;
  798. fail:
  799. av_dict_free(&metadata);
  800. ret = -1;
  801. goto the_end;
  802. }
  803. static av_cold int png_dec_init(AVCodecContext *avctx)
  804. {
  805. PNGDecContext *s = avctx->priv_data;
  806. s->current_picture = &s->picture1;
  807. s->last_picture = &s->picture2;
  808. avcodec_get_frame_defaults(&s->picture1);
  809. avcodec_get_frame_defaults(&s->picture2);
  810. ff_pngdsp_init(&s->dsp);
  811. s->avctx = avctx;
  812. return 0;
  813. }
  814. static av_cold int png_dec_end(AVCodecContext *avctx)
  815. {
  816. PNGDecContext *s = avctx->priv_data;
  817. if (s->picture1.data[0])
  818. avctx->release_buffer(avctx, &s->picture1);
  819. if (s->picture2.data[0])
  820. avctx->release_buffer(avctx, &s->picture2);
  821. return 0;
  822. }
  823. AVCodec ff_png_decoder = {
  824. .name = "png",
  825. .type = AVMEDIA_TYPE_VIDEO,
  826. .id = AV_CODEC_ID_PNG,
  827. .priv_data_size = sizeof(PNGDecContext),
  828. .init = png_dec_init,
  829. .close = png_dec_end,
  830. .decode = decode_frame,
  831. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  832. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  833. };