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.

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