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.

918 lines
31KB

  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 "internal.h"
  27. #include "png.h"
  28. #include "pngdsp.h"
  29. /* TODO:
  30. * - add 16 bit depth support
  31. */
  32. #include <zlib.h>
  33. //#define DEBUG
  34. typedef struct PNGDecContext {
  35. PNGDSPContext dsp;
  36. AVCodecContext *avctx;
  37. GetByteContext gb;
  38. AVFrame picture1, picture2;
  39. AVFrame *current_picture, *last_picture;
  40. int state;
  41. int width, height;
  42. int bit_depth;
  43. int color_type;
  44. int compression_type;
  45. int interlace_type;
  46. int filter_type;
  47. int channels;
  48. int bits_per_pixel;
  49. int bpp;
  50. uint8_t *image_buf;
  51. int image_linesize;
  52. uint32_t palette[256];
  53. uint8_t *crow_buf;
  54. uint8_t *last_row;
  55. uint8_t *tmp_row;
  56. int pass;
  57. int crow_size; /* compressed row size (include filter type) */
  58. int row_size; /* decompressed row size */
  59. int pass_row_size; /* decompress row size of the current pass */
  60. int y;
  61. z_stream zstream;
  62. } PNGDecContext;
  63. /* Mask to determine which pixels are valid in a pass */
  64. static const uint8_t png_pass_mask[NB_PASSES] = {
  65. 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
  66. };
  67. /* Mask to determine which y pixels can be written in a pass */
  68. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  69. 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  70. };
  71. /* Mask to determine which pixels to overwrite while displaying */
  72. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  73. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  74. };
  75. /* NOTE: we try to construct a good looking image at each pass. width
  76. is the original image width. We also do pixel format conversion at
  77. this stage */
  78. static void png_put_interlaced_row(uint8_t *dst, int width,
  79. int bits_per_pixel, int pass,
  80. int color_type, const uint8_t *src)
  81. {
  82. int x, mask, dsp_mask, j, src_x, b, bpp;
  83. uint8_t *d;
  84. const uint8_t *s;
  85. mask = png_pass_mask[pass];
  86. dsp_mask = png_pass_dsp_mask[pass];
  87. switch (bits_per_pixel) {
  88. case 1:
  89. src_x = 0;
  90. for (x = 0; x < width; x++) {
  91. j = (x & 7);
  92. if ((dsp_mask << j) & 0x80) {
  93. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  94. dst[x >> 3] &= 0xFF7F>>j;
  95. dst[x >> 3] |= b << (7 - j);
  96. }
  97. if ((mask << j) & 0x80)
  98. src_x++;
  99. }
  100. break;
  101. case 2:
  102. src_x = 0;
  103. for (x = 0; x < width; x++) {
  104. int j2 = 2 * (x & 3);
  105. j = (x & 7);
  106. if ((dsp_mask << j) & 0x80) {
  107. b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
  108. dst[x >> 2] &= 0xFF3F>>j2;
  109. dst[x >> 2] |= b << (6 - j2);
  110. }
  111. if ((mask << j) & 0x80)
  112. src_x++;
  113. }
  114. break;
  115. case 4:
  116. src_x = 0;
  117. for (x = 0; x < width; x++) {
  118. int j2 = 4*(x&1);
  119. j = (x & 7);
  120. if ((dsp_mask << j) & 0x80) {
  121. b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
  122. dst[x >> 1] &= 0xFF0F>>j2;
  123. dst[x >> 1] |= b << (4 - j2);
  124. }
  125. if ((mask << j) & 0x80)
  126. src_x++;
  127. }
  128. break;
  129. default:
  130. bpp = bits_per_pixel >> 3;
  131. d = dst;
  132. s = src;
  133. for (x = 0; x < width; x++) {
  134. j = x & 7;
  135. if ((dsp_mask << j) & 0x80) {
  136. memcpy(d, s, bpp);
  137. }
  138. d += bpp;
  139. if ((mask << j) & 0x80)
  140. s += bpp;
  141. }
  142. break;
  143. }
  144. }
  145. void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
  146. {
  147. int i;
  148. for (i = 0; i < w; i++) {
  149. int a, b, c, p, pa, pb, pc;
  150. a = dst[i - bpp];
  151. b = top[i];
  152. c = top[i - bpp];
  153. p = b - c;
  154. pc = a - c;
  155. pa = abs(p);
  156. pb = abs(pc);
  157. pc = abs(p + pc);
  158. if (pa <= pb && pa <= pc)
  159. p = a;
  160. else if (pb <= pc)
  161. p = b;
  162. else
  163. p = c;
  164. dst[i] = p + src[i];
  165. }
  166. }
  167. #define UNROLL1(bpp, op) {\
  168. r = dst[0];\
  169. if(bpp >= 2) g = dst[1];\
  170. if(bpp >= 3) b = dst[2];\
  171. if(bpp >= 4) a = dst[3];\
  172. for(; i < size; i+=bpp) {\
  173. dst[i+0] = r = op(r, src[i+0], last[i+0]);\
  174. if(bpp == 1) continue;\
  175. dst[i+1] = g = op(g, src[i+1], last[i+1]);\
  176. if(bpp == 2) continue;\
  177. dst[i+2] = b = op(b, src[i+2], last[i+2]);\
  178. if(bpp == 3) continue;\
  179. dst[i+3] = a = op(a, src[i+3], last[i+3]);\
  180. }\
  181. }
  182. #define UNROLL_FILTER(op)\
  183. if(bpp == 1) UNROLL1(1, op)\
  184. else if(bpp == 2) UNROLL1(2, op)\
  185. else if(bpp == 3) UNROLL1(3, op)\
  186. else if(bpp == 4) UNROLL1(4, op)\
  187. else {\
  188. for (; i < size; i += bpp) {\
  189. int j;\
  190. for (j = 0; j < bpp; j++)\
  191. dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
  192. }\
  193. }
  194. /* NOTE: 'dst' can be equal to 'last' */
  195. static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
  196. uint8_t *src, uint8_t *last, int size, int bpp)
  197. {
  198. int i, p, r, g, b, a;
  199. switch (filter_type) {
  200. case PNG_FILTER_VALUE_NONE:
  201. memcpy(dst, src, size);
  202. break;
  203. case PNG_FILTER_VALUE_SUB:
  204. for (i = 0; i < bpp; i++) {
  205. dst[i] = src[i];
  206. }
  207. if (bpp == 4) {
  208. p = *(int*)dst;
  209. for (; i < size; i += bpp) {
  210. int s = *(int*)(src + i);
  211. p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
  212. *(int*)(dst + i) = p;
  213. }
  214. } else {
  215. #define OP_SUB(x,s,l) x+s
  216. UNROLL_FILTER(OP_SUB);
  217. }
  218. break;
  219. case PNG_FILTER_VALUE_UP:
  220. dsp->add_bytes_l2(dst, src, last, size);
  221. break;
  222. case PNG_FILTER_VALUE_AVG:
  223. for (i = 0; i < bpp; i++) {
  224. p = (last[i] >> 1);
  225. dst[i] = p + src[i];
  226. }
  227. #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
  228. UNROLL_FILTER(OP_AVG);
  229. break;
  230. case PNG_FILTER_VALUE_PAETH:
  231. for (i = 0; i < bpp; i++) {
  232. p = last[i];
  233. dst[i] = p + src[i];
  234. }
  235. if (bpp > 2 && size > 4) {
  236. // would write off the end of the array if we let it process the last pixel with bpp=3
  237. int w = bpp == 4 ? size : size - 3;
  238. dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
  239. i = w;
  240. }
  241. ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
  242. break;
  243. }
  244. }
  245. /* This used to be called "deloco" in FFmpeg
  246. * and is actually an inverse reversible colorspace transformation */
  247. #define YUV2RGB(NAME, TYPE) \
  248. static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
  249. { \
  250. int i; \
  251. for (i = 0; i < size; i += 3 + alpha) { \
  252. int g = dst [i+1]; \
  253. dst[i+0] += g; \
  254. dst[i+2] += g; \
  255. } \
  256. }
  257. YUV2RGB(rgb8, uint8_t)
  258. YUV2RGB(rgb16, uint16_t)
  259. /* process exactly one decompressed row */
  260. static void png_handle_row(PNGDecContext *s)
  261. {
  262. uint8_t *ptr, *last_row;
  263. int got_line;
  264. if (!s->interlace_type) {
  265. ptr = s->image_buf + s->image_linesize * s->y;
  266. if (s->y == 0)
  267. last_row = s->last_row;
  268. else
  269. last_row = ptr - s->image_linesize;
  270. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  271. last_row, s->row_size, s->bpp);
  272. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  273. if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
  274. if (s->bit_depth == 16) {
  275. deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
  276. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  277. } else {
  278. deloco_rgb8(ptr - s->image_linesize, s->row_size,
  279. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  280. }
  281. }
  282. s->y++;
  283. if (s->y == s->height) {
  284. s->state |= PNG_ALLIMAGE;
  285. if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
  286. if (s->bit_depth == 16) {
  287. deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
  288. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  289. } else {
  290. deloco_rgb8(ptr, s->row_size,
  291. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  292. }
  293. }
  294. }
  295. } else {
  296. got_line = 0;
  297. for (;;) {
  298. ptr = s->image_buf + s->image_linesize * s->y;
  299. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  300. /* if we already read one row, it is time to stop to
  301. wait for the next one */
  302. if (got_line)
  303. break;
  304. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  305. s->last_row, s->pass_row_size, s->bpp);
  306. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  307. got_line = 1;
  308. }
  309. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  310. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  311. s->color_type, s->last_row);
  312. }
  313. s->y++;
  314. if (s->y == s->height) {
  315. memset(s->last_row, 0, s->row_size);
  316. for (;;) {
  317. if (s->pass == NB_PASSES - 1) {
  318. s->state |= PNG_ALLIMAGE;
  319. goto the_end;
  320. } else {
  321. s->pass++;
  322. s->y = 0;
  323. s->pass_row_size = ff_png_pass_row_size(s->pass,
  324. s->bits_per_pixel,
  325. s->width);
  326. s->crow_size = s->pass_row_size + 1;
  327. if (s->pass_row_size != 0)
  328. break;
  329. /* skip pass if empty row */
  330. }
  331. }
  332. }
  333. }
  334. the_end: ;
  335. }
  336. }
  337. static int png_decode_idat(PNGDecContext *s, int length)
  338. {
  339. int ret;
  340. s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
  341. s->zstream.next_in = (unsigned char *)s->gb.buffer;
  342. bytestream2_skip(&s->gb, length);
  343. /* decode one line if possible */
  344. while (s->zstream.avail_in > 0) {
  345. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  346. if (ret != Z_OK && ret != Z_STREAM_END) {
  347. av_log(s->avctx, AV_LOG_ERROR, "inflate returned %d\n", ret);
  348. return -1;
  349. }
  350. if (s->zstream.avail_out == 0) {
  351. if (!(s->state & PNG_ALLIMAGE)) {
  352. png_handle_row(s);
  353. }
  354. s->zstream.avail_out = s->crow_size;
  355. s->zstream.next_out = s->crow_buf;
  356. }
  357. }
  358. return 0;
  359. }
  360. static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
  361. const uint8_t *data_end)
  362. {
  363. z_stream zstream;
  364. unsigned char *buf;
  365. unsigned buf_size;
  366. int ret;
  367. zstream.zalloc = ff_png_zalloc;
  368. zstream.zfree = ff_png_zfree;
  369. zstream.opaque = NULL;
  370. if (inflateInit(&zstream) != Z_OK)
  371. return AVERROR_EXTERNAL;
  372. zstream.next_in = (unsigned char *)data;
  373. zstream.avail_in = data_end - data;
  374. av_bprint_init(bp, 0, -1);
  375. while (zstream.avail_in > 0) {
  376. av_bprint_get_buffer(bp, 1, &buf, &buf_size);
  377. if (!buf_size) {
  378. ret = AVERROR(ENOMEM);
  379. goto fail;
  380. }
  381. zstream.next_out = buf;
  382. zstream.avail_out = buf_size;
  383. ret = inflate(&zstream, Z_PARTIAL_FLUSH);
  384. if (ret != Z_OK && ret != Z_STREAM_END) {
  385. ret = AVERROR_EXTERNAL;
  386. goto fail;
  387. }
  388. bp->len += zstream.next_out - buf;
  389. if (ret == Z_STREAM_END)
  390. break;
  391. }
  392. inflateEnd(&zstream);
  393. bp->str[bp->len] = 0;
  394. return 0;
  395. fail:
  396. inflateEnd(&zstream);
  397. av_bprint_finalize(bp, NULL);
  398. return ret;
  399. }
  400. static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
  401. {
  402. size_t extra = 0, i;
  403. uint8_t *out, *q;
  404. for (i = 0; i < size_in; i++)
  405. extra += in[i] >= 0x80;
  406. if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
  407. return NULL;
  408. q = out = av_malloc(size_in + extra + 1);
  409. if (!out)
  410. return NULL;
  411. for (i = 0; i < size_in; i++) {
  412. if (in[i] >= 0x80) {
  413. *(q++) = 0xC0 | (in[i] >> 6);
  414. *(q++) = 0x80 | (in[i] & 0x3F);
  415. } else {
  416. *(q++) = in[i];
  417. }
  418. }
  419. *(q++) = 0;
  420. return out;
  421. }
  422. static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
  423. AVDictionary **dict)
  424. {
  425. int ret, method;
  426. const uint8_t *data = s->gb.buffer;
  427. const uint8_t *data_end = data + length;
  428. const uint8_t *keyword = data;
  429. const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
  430. uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
  431. unsigned text_len;
  432. AVBPrint bp;
  433. if (!keyword_end)
  434. return AVERROR_INVALIDDATA;
  435. data = keyword_end + 1;
  436. if (compressed) {
  437. if (data == data_end)
  438. return AVERROR_INVALIDDATA;
  439. method = *(data++);
  440. if (method)
  441. return AVERROR_INVALIDDATA;
  442. if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
  443. return ret;
  444. text_len = bp.len;
  445. av_bprint_finalize(&bp, (char **)&text);
  446. if (!text)
  447. return AVERROR(ENOMEM);
  448. } else {
  449. text = (uint8_t *)data;
  450. text_len = data_end - text;
  451. }
  452. kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
  453. txt_utf8 = iso88591_to_utf8(text, text_len);
  454. if (text != data)
  455. av_free(text);
  456. if (!(kw_utf8 && txt_utf8)) {
  457. av_free(kw_utf8);
  458. av_free(txt_utf8);
  459. return AVERROR(ENOMEM);
  460. }
  461. av_dict_set(dict, kw_utf8, txt_utf8,
  462. AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  463. return 0;
  464. }
  465. static int decode_frame(AVCodecContext *avctx,
  466. void *data, int *got_frame,
  467. AVPacket *avpkt)
  468. {
  469. PNGDecContext * const s = avctx->priv_data;
  470. const uint8_t *buf = avpkt->data;
  471. int buf_size = avpkt->size;
  472. AVFrame *picture = data;
  473. AVDictionary *metadata = NULL;
  474. uint8_t *crow_buf_base = NULL;
  475. AVFrame *p;
  476. uint32_t tag, length;
  477. int64_t sig;
  478. int ret;
  479. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  480. avctx->coded_frame = s->current_picture;
  481. p = s->current_picture;
  482. bytestream2_init(&s->gb, buf, buf_size);
  483. /* check signature */
  484. sig = bytestream2_get_be64(&s->gb);
  485. if (sig != PNGSIG &&
  486. sig != MNGSIG) {
  487. av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
  488. return -1;
  489. }
  490. s->y = s->state = 0;
  491. /* init the zlib */
  492. s->zstream.zalloc = ff_png_zalloc;
  493. s->zstream.zfree = ff_png_zfree;
  494. s->zstream.opaque = NULL;
  495. ret = inflateInit(&s->zstream);
  496. if (ret != Z_OK) {
  497. av_log(avctx, AV_LOG_ERROR, "inflateInit returned %d\n", ret);
  498. return -1;
  499. }
  500. for (;;) {
  501. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  502. av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
  503. goto fail;
  504. }
  505. length = bytestream2_get_be32(&s->gb);
  506. if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
  507. av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
  508. goto fail;
  509. }
  510. tag = bytestream2_get_le32(&s->gb);
  511. if (avctx->debug & FF_DEBUG_STARTCODE)
  512. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  513. (tag & 0xff),
  514. ((tag >> 8) & 0xff),
  515. ((tag >> 16) & 0xff),
  516. ((tag >> 24) & 0xff), length);
  517. switch (tag) {
  518. case MKTAG('I', 'H', 'D', 'R'):
  519. if (length != 13)
  520. goto fail;
  521. s->width = bytestream2_get_be32(&s->gb);
  522. s->height = bytestream2_get_be32(&s->gb);
  523. if (av_image_check_size(s->width, s->height, 0, avctx)) {
  524. s->width = s->height = 0;
  525. av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
  526. goto fail;
  527. }
  528. s->bit_depth = bytestream2_get_byte(&s->gb);
  529. s->color_type = bytestream2_get_byte(&s->gb);
  530. s->compression_type = bytestream2_get_byte(&s->gb);
  531. s->filter_type = bytestream2_get_byte(&s->gb);
  532. s->interlace_type = bytestream2_get_byte(&s->gb);
  533. bytestream2_skip(&s->gb, 4); /* crc */
  534. s->state |= PNG_IHDR;
  535. if (avctx->debug & FF_DEBUG_PICT_INFO)
  536. av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
  537. "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 (ff_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, k;
  712. uint8_t *pd = s->current_picture->data[0];
  713. for (j = 0; j < s->height; j++) {
  714. i = s->width / 8;
  715. for (k = 7; k >= 1; k--)
  716. if ((s->width&7) >= k)
  717. pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
  718. for (i--; i >= 0; i--) {
  719. pd[8*i + 7]= pd[i] & 1;
  720. pd[8*i + 6]= (pd[i]>>1) & 1;
  721. pd[8*i + 5]= (pd[i]>>2) & 1;
  722. pd[8*i + 4]= (pd[i]>>3) & 1;
  723. pd[8*i + 3]= (pd[i]>>4) & 1;
  724. pd[8*i + 2]= (pd[i]>>5) & 1;
  725. pd[8*i + 1]= (pd[i]>>6) & 1;
  726. pd[8*i + 0]= pd[i]>>7;
  727. }
  728. pd += s->image_linesize;
  729. }
  730. }
  731. if (s->bits_per_pixel == 2){
  732. int i, j;
  733. uint8_t *pd = s->current_picture->data[0];
  734. for (j = 0; j < s->height; j++) {
  735. i = s->width / 4;
  736. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  737. if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
  738. if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
  739. if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
  740. for (i--; i >= 0; i--) {
  741. pd[4*i + 3]= pd[i] & 3;
  742. pd[4*i + 2]= (pd[i]>>2) & 3;
  743. pd[4*i + 1]= (pd[i]>>4) & 3;
  744. pd[4*i + 0]= pd[i]>>6;
  745. }
  746. } else {
  747. if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
  748. if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
  749. if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
  750. for (i--; i >= 0; i--) {
  751. pd[4*i + 3]= ( pd[i] & 3)*0x55;
  752. pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
  753. pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
  754. pd[4*i + 0]= ( pd[i]>>6 )*0x55;
  755. }
  756. }
  757. pd += s->image_linesize;
  758. }
  759. }
  760. if (s->bits_per_pixel == 4){
  761. int i, j;
  762. uint8_t *pd = s->current_picture->data[0];
  763. for (j = 0; j < s->height; j++) {
  764. i = s->width/2;
  765. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  766. if (s->width&1) pd[2*i+0]= pd[i]>>4;
  767. for (i--; i >= 0; i--) {
  768. pd[2*i + 1] = pd[i] & 15;
  769. pd[2*i + 0] = pd[i] >> 4;
  770. }
  771. } else {
  772. if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
  773. for (i--; i >= 0; i--) {
  774. pd[2*i + 1] = (pd[i] & 15) * 0x11;
  775. pd[2*i + 0] = (pd[i] >> 4) * 0x11;
  776. }
  777. }
  778. pd += s->image_linesize;
  779. }
  780. }
  781. /* handle p-frames only if a predecessor frame is available */
  782. if (s->last_picture->data[0] != NULL) {
  783. if ( !(avpkt->flags & AV_PKT_FLAG_KEY)
  784. && s->last_picture->width == s->current_picture->width
  785. && s->last_picture->height== s->current_picture->height
  786. && s->last_picture->format== s->current_picture->format
  787. ) {
  788. int i, j;
  789. uint8_t *pd = s->current_picture->data[0];
  790. uint8_t *pd_last = s->last_picture->data[0];
  791. for (j = 0; j < s->height; j++) {
  792. for (i = 0; i < s->width * s->bpp; i++) {
  793. pd[i] += pd_last[i];
  794. }
  795. pd += s->image_linesize;
  796. pd_last += s->image_linesize;
  797. }
  798. }
  799. }
  800. s->current_picture->metadata = metadata;
  801. metadata = NULL;
  802. *picture = *s->current_picture;
  803. *got_frame = 1;
  804. ret = bytestream2_tell(&s->gb);
  805. the_end:
  806. inflateEnd(&s->zstream);
  807. av_free(crow_buf_base);
  808. s->crow_buf = NULL;
  809. av_freep(&s->last_row);
  810. av_freep(&s->tmp_row);
  811. return ret;
  812. fail:
  813. av_dict_free(&metadata);
  814. ret = -1;
  815. goto the_end;
  816. }
  817. static av_cold int png_dec_init(AVCodecContext *avctx)
  818. {
  819. PNGDecContext *s = avctx->priv_data;
  820. s->current_picture = &s->picture1;
  821. s->last_picture = &s->picture2;
  822. avcodec_get_frame_defaults(&s->picture1);
  823. avcodec_get_frame_defaults(&s->picture2);
  824. ff_pngdsp_init(&s->dsp);
  825. s->avctx = avctx;
  826. return 0;
  827. }
  828. static av_cold int png_dec_end(AVCodecContext *avctx)
  829. {
  830. PNGDecContext *s = avctx->priv_data;
  831. if (s->picture1.data[0])
  832. avctx->release_buffer(avctx, &s->picture1);
  833. if (s->picture2.data[0])
  834. avctx->release_buffer(avctx, &s->picture2);
  835. return 0;
  836. }
  837. AVCodec ff_png_decoder = {
  838. .name = "png",
  839. .type = AVMEDIA_TYPE_VIDEO,
  840. .id = AV_CODEC_ID_PNG,
  841. .priv_data_size = sizeof(PNGDecContext),
  842. .init = png_dec_init,
  843. .close = png_dec_end,
  844. .decode = decode_frame,
  845. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  846. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  847. };