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.

680 lines
24KB

  1. /*
  2. * PNG image format
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/imgutils.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. #include "png.h"
  26. #include "pngdsp.h"
  27. /* TODO:
  28. * - add 2, 4 and 16 bit depth support
  29. */
  30. #include <zlib.h>
  31. typedef struct PNGDecContext {
  32. PNGDSPContext dsp;
  33. GetByteContext gb;
  34. AVFrame *prev;
  35. int state;
  36. int width, height;
  37. int bit_depth;
  38. int color_type;
  39. int compression_type;
  40. int interlace_type;
  41. int filter_type;
  42. int channels;
  43. int bits_per_pixel;
  44. int bpp;
  45. uint8_t *image_buf;
  46. int image_linesize;
  47. uint32_t palette[256];
  48. uint8_t *crow_buf;
  49. uint8_t *last_row;
  50. uint8_t *tmp_row;
  51. int pass;
  52. int crow_size; /* compressed row size (include filter type) */
  53. int row_size; /* decompressed row size */
  54. int pass_row_size; /* decompress row size of the current pass */
  55. int y;
  56. z_stream zstream;
  57. } PNGDecContext;
  58. /* Mask to determine which y pixels can be written in a pass */
  59. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  60. 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
  61. };
  62. /* Mask to determine which pixels to overwrite while displaying */
  63. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  64. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  65. };
  66. /* NOTE: we try to construct a good looking image at each pass. width
  67. * is the original image width. We also do pixel format conversion at
  68. * this stage */
  69. static void png_put_interlaced_row(uint8_t *dst, int width,
  70. int bits_per_pixel, int pass,
  71. int color_type, const uint8_t *src)
  72. {
  73. int x, mask, dsp_mask, j, src_x, b, bpp;
  74. uint8_t *d;
  75. const uint8_t *s;
  76. mask = ff_png_pass_mask[pass];
  77. dsp_mask = png_pass_dsp_mask[pass];
  78. switch (bits_per_pixel) {
  79. case 1:
  80. /* we must initialize the line to zero before writing to it */
  81. if (pass == 0)
  82. memset(dst, 0, (width + 7) >> 3);
  83. src_x = 0;
  84. for (x = 0; x < width; x++) {
  85. j = (x & 7);
  86. if ((dsp_mask << j) & 0x80) {
  87. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  88. dst[x >> 3] |= b << (7 - j);
  89. }
  90. if ((mask << j) & 0x80)
  91. src_x++;
  92. }
  93. break;
  94. default:
  95. bpp = bits_per_pixel >> 3;
  96. d = dst;
  97. s = src;
  98. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  99. for (x = 0; x < width; x++) {
  100. j = x & 7;
  101. if ((dsp_mask << j) & 0x80) {
  102. *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
  103. }
  104. d += bpp;
  105. if ((mask << j) & 0x80)
  106. s += bpp;
  107. }
  108. } else {
  109. for (x = 0; x < width; x++) {
  110. j = x & 7;
  111. if ((dsp_mask << j) & 0x80) {
  112. memcpy(d, s, bpp);
  113. }
  114. d += bpp;
  115. if ((mask << j) & 0x80)
  116. s += bpp;
  117. }
  118. }
  119. break;
  120. }
  121. }
  122. void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
  123. int w, int bpp)
  124. {
  125. int i;
  126. for (i = 0; i < w; i++) {
  127. int a, b, c, p, pa, pb, pc;
  128. a = dst[i - bpp];
  129. b = top[i];
  130. c = top[i - bpp];
  131. p = b - c;
  132. pc = a - c;
  133. pa = abs(p);
  134. pb = abs(pc);
  135. pc = abs(p + pc);
  136. if (pa <= pb && pa <= pc)
  137. p = a;
  138. else if (pb <= pc)
  139. p = b;
  140. else
  141. p = c;
  142. dst[i] = p + src[i];
  143. }
  144. }
  145. #define UNROLL1(bpp, op) \
  146. { \
  147. r = dst[0]; \
  148. if (bpp >= 2) \
  149. g = dst[1]; \
  150. if (bpp >= 3) \
  151. b = dst[2]; \
  152. if (bpp >= 4) \
  153. a = dst[3]; \
  154. for (; i < size; i += bpp) { \
  155. dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
  156. if (bpp == 1) \
  157. continue; \
  158. dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
  159. if (bpp == 2) \
  160. continue; \
  161. dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
  162. if (bpp == 3) \
  163. continue; \
  164. dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
  165. } \
  166. }
  167. #define UNROLL_FILTER(op) \
  168. if (bpp == 1) { \
  169. UNROLL1(1, op) \
  170. } else if (bpp == 2) { \
  171. UNROLL1(2, op) \
  172. } else if (bpp == 3) { \
  173. UNROLL1(3, op) \
  174. } else if (bpp == 4) { \
  175. UNROLL1(4, op) \
  176. } else { \
  177. for (; i < size; i += bpp) { \
  178. int j; \
  179. for (j = 0; j < bpp; j++) \
  180. dst[i + j] = op(dst[i + j - bpp], src[i + j], last[i + j]); \
  181. } \
  182. }
  183. /* NOTE: 'dst' can be equal to 'last' */
  184. static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
  185. uint8_t *src, uint8_t *last, int size, int bpp)
  186. {
  187. int i, p, r, g, b, a;
  188. switch (filter_type) {
  189. case PNG_FILTER_VALUE_NONE:
  190. memcpy(dst, src, size);
  191. break;
  192. case PNG_FILTER_VALUE_SUB:
  193. for (i = 0; i < bpp; i++)
  194. dst[i] = src[i];
  195. if (bpp == 4) {
  196. p = *(int *)dst;
  197. for (; i < size; i += bpp) {
  198. int s = *(int *)(src + i);
  199. p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
  200. *(int *)(dst + i) = p;
  201. }
  202. } else {
  203. #define OP_SUB(x, s, l) x + s
  204. UNROLL_FILTER(OP_SUB);
  205. }
  206. break;
  207. case PNG_FILTER_VALUE_UP:
  208. dsp->add_bytes_l2(dst, src, last, size);
  209. break;
  210. case PNG_FILTER_VALUE_AVG:
  211. for (i = 0; i < bpp; i++) {
  212. p = (last[i] >> 1);
  213. dst[i] = p + src[i];
  214. }
  215. #define OP_AVG(x, s, l) (((x + l) >> 1) + s) & 0xff
  216. UNROLL_FILTER(OP_AVG);
  217. break;
  218. case PNG_FILTER_VALUE_PAETH:
  219. for (i = 0; i < bpp; i++) {
  220. p = last[i];
  221. dst[i] = p + src[i];
  222. }
  223. if (bpp > 2 && size > 4) {
  224. /* would write off the end of the array if we let it process
  225. * the last pixel with bpp=3 */
  226. int w = bpp == 4 ? size : size - 3;
  227. dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
  228. i = w;
  229. }
  230. ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
  231. break;
  232. }
  233. }
  234. static av_always_inline void convert_to_rgb32_loco(uint8_t *dst,
  235. const uint8_t *src,
  236. int width, int loco)
  237. {
  238. int j;
  239. unsigned int r, g, b, a;
  240. for (j = 0; j < width; j++) {
  241. r = src[0];
  242. g = src[1];
  243. b = src[2];
  244. a = src[3];
  245. if (loco) {
  246. r = (r + g) & 0xff;
  247. b = (b + g) & 0xff;
  248. }
  249. *(uint32_t *) dst = (a << 24) | (r << 16) | (g << 8) | b;
  250. dst += 4;
  251. src += 4;
  252. }
  253. }
  254. static void convert_to_rgb32(uint8_t *dst, const uint8_t *src,
  255. int width, int loco)
  256. {
  257. if (loco)
  258. convert_to_rgb32_loco(dst, src, width, 1);
  259. else
  260. convert_to_rgb32_loco(dst, src, width, 0);
  261. }
  262. static void deloco_rgb24(uint8_t *dst, int size)
  263. {
  264. int i;
  265. for (i = 0; i < size; i += 3) {
  266. int g = dst[i + 1];
  267. dst[i + 0] += g;
  268. dst[i + 2] += g;
  269. }
  270. }
  271. /* process exactly one decompressed row */
  272. static void png_handle_row(PNGDecContext *s)
  273. {
  274. uint8_t *ptr, *last_row;
  275. int got_line;
  276. if (!s->interlace_type) {
  277. ptr = s->image_buf + s->image_linesize * s->y;
  278. /* need to swap bytes correctly for RGB_ALPHA */
  279. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  280. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  281. s->last_row, s->row_size, s->bpp);
  282. convert_to_rgb32(ptr, s->tmp_row, s->width,
  283. s->filter_type == PNG_FILTER_TYPE_LOCO);
  284. FFSWAP(uint8_t *, s->last_row, s->tmp_row);
  285. } else {
  286. /* in normal case, we avoid one copy */
  287. if (s->y == 0)
  288. last_row = s->last_row;
  289. else
  290. last_row = ptr - s->image_linesize;
  291. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  292. last_row, s->row_size, s->bpp);
  293. }
  294. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  295. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  296. s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
  297. deloco_rgb24(ptr - s->image_linesize, s->row_size);
  298. s->y++;
  299. if (s->y == s->height) {
  300. s->state |= PNG_ALLIMAGE;
  301. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  302. s->color_type == PNG_COLOR_TYPE_RGB)
  303. deloco_rgb24(ptr, s->row_size);
  304. }
  305. } else {
  306. got_line = 0;
  307. for (;;) {
  308. ptr = s->image_buf + s->image_linesize * s->y;
  309. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  310. /* if we already read one row, it is time to stop to
  311. * wait for the next one */
  312. if (got_line)
  313. break;
  314. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  315. s->last_row, s->pass_row_size, s->bpp);
  316. FFSWAP(uint8_t *, s->last_row, s->tmp_row);
  317. got_line = 1;
  318. }
  319. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  320. /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
  321. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  322. s->color_type, s->last_row);
  323. }
  324. s->y++;
  325. if (s->y == s->height) {
  326. for (;;) {
  327. if (s->pass == NB_PASSES - 1) {
  328. s->state |= PNG_ALLIMAGE;
  329. goto the_end;
  330. } else {
  331. s->pass++;
  332. s->y = 0;
  333. s->pass_row_size = ff_png_pass_row_size(s->pass,
  334. s->bits_per_pixel,
  335. s->width);
  336. s->crow_size = s->pass_row_size + 1;
  337. if (s->pass_row_size != 0)
  338. break;
  339. /* skip pass if empty row */
  340. }
  341. }
  342. }
  343. }
  344. the_end:;
  345. }
  346. }
  347. static int png_decode_idat(PNGDecContext *s, int length)
  348. {
  349. int ret;
  350. s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
  351. s->zstream.next_in = s->gb.buffer;
  352. bytestream2_skip(&s->gb, length);
  353. /* decode one line if possible */
  354. while (s->zstream.avail_in > 0) {
  355. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  356. if (ret != Z_OK && ret != Z_STREAM_END) {
  357. return -1;
  358. }
  359. if (s->zstream.avail_out == 0) {
  360. if (!(s->state & PNG_ALLIMAGE)) {
  361. png_handle_row(s);
  362. }
  363. s->zstream.avail_out = s->crow_size;
  364. s->zstream.next_out = s->crow_buf;
  365. }
  366. if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
  367. av_log(NULL, AV_LOG_WARNING,
  368. "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
  369. return 0;
  370. }
  371. }
  372. return 0;
  373. }
  374. static int decode_frame(AVCodecContext *avctx,
  375. void *data, int *got_frame,
  376. AVPacket *avpkt)
  377. {
  378. PNGDecContext *const s = avctx->priv_data;
  379. const uint8_t *buf = avpkt->data;
  380. int buf_size = avpkt->size;
  381. AVFrame *p = data;
  382. uint8_t *crow_buf_base = NULL;
  383. uint32_t tag, length;
  384. int ret;
  385. /* check signature */
  386. if (buf_size < 8 ||
  387. (memcmp(buf, ff_pngsig, 8) != 0 && memcmp(buf, ff_mngsig, 8) != 0)) {
  388. av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature (%d).\n", buf_size);
  389. return AVERROR_INVALIDDATA;
  390. }
  391. bytestream2_init(&s->gb, buf + 8, buf_size - 8);
  392. s->y = s->state = 0;
  393. /* init the zlib */
  394. s->zstream.zalloc = ff_png_zalloc;
  395. s->zstream.zfree = ff_png_zfree;
  396. s->zstream.opaque = NULL;
  397. ret = inflateInit(&s->zstream);
  398. if (ret != Z_OK)
  399. return -1;
  400. for (;;) {
  401. if (bytestream2_get_bytes_left(&s->gb) <= 0)
  402. goto fail;
  403. length = bytestream2_get_be32(&s->gb);
  404. if (length > 0x7fffffff)
  405. goto fail;
  406. tag = bytestream2_get_le32(&s->gb);
  407. ff_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
  408. (tag & 0xff),
  409. ((tag >> 8) & 0xff),
  410. ((tag >> 16) & 0xff),
  411. ((tag >> 24) & 0xff), length);
  412. switch (tag) {
  413. case MKTAG('I', 'H', 'D', 'R'):
  414. if (length != 13)
  415. goto fail;
  416. s->width = bytestream2_get_be32(&s->gb);
  417. s->height = bytestream2_get_be32(&s->gb);
  418. if (av_image_check_size(s->width, s->height, 0, avctx)) {
  419. s->width = s->height = 0;
  420. goto fail;
  421. }
  422. s->bit_depth = bytestream2_get_byte(&s->gb);
  423. s->color_type = bytestream2_get_byte(&s->gb);
  424. s->compression_type = bytestream2_get_byte(&s->gb);
  425. s->filter_type = bytestream2_get_byte(&s->gb);
  426. s->interlace_type = bytestream2_get_byte(&s->gb);
  427. bytestream2_skip(&s->gb, 4); /* crc */
  428. s->state |= PNG_IHDR;
  429. ff_dlog(avctx, "width=%d height=%d depth=%d color_type=%d "
  430. "compression_type=%d filter_type=%d interlace_type=%d\n",
  431. s->width, s->height, s->bit_depth, s->color_type,
  432. s->compression_type, s->filter_type, s->interlace_type);
  433. break;
  434. case MKTAG('I', 'D', 'A', 'T'):
  435. if (!(s->state & PNG_IHDR))
  436. goto fail;
  437. if (!(s->state & PNG_IDAT)) {
  438. /* init image info */
  439. avctx->width = s->width;
  440. avctx->height = s->height;
  441. s->channels = ff_png_get_nb_channels(s->color_type);
  442. s->bits_per_pixel = s->bit_depth * s->channels;
  443. s->bpp = (s->bits_per_pixel + 7) >> 3;
  444. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  445. if (s->bit_depth == 8 &&
  446. s->color_type == PNG_COLOR_TYPE_RGB) {
  447. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  448. } else if (s->bit_depth == 8 &&
  449. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  450. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  451. } else if (s->bit_depth == 8 &&
  452. s->color_type == PNG_COLOR_TYPE_GRAY) {
  453. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  454. } else if (s->bit_depth == 16 &&
  455. s->color_type == PNG_COLOR_TYPE_GRAY) {
  456. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  457. } else if (s->bit_depth == 16 &&
  458. s->color_type == PNG_COLOR_TYPE_RGB) {
  459. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  460. } else if (s->bit_depth == 1 &&
  461. s->color_type == PNG_COLOR_TYPE_GRAY) {
  462. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  463. } else if (s->bit_depth == 8 &&
  464. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  465. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  466. } else if (s->bit_depth == 8 &&
  467. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  468. avctx->pix_fmt = AV_PIX_FMT_YA8;
  469. } else if (s->bit_depth == 16 &&
  470. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  471. avctx->pix_fmt = AV_PIX_FMT_YA16BE;
  472. } else {
  473. goto fail;
  474. }
  475. if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0) {
  476. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  477. goto fail;
  478. }
  479. p->pict_type = AV_PICTURE_TYPE_I;
  480. p->key_frame = 1;
  481. p->interlaced_frame = !!s->interlace_type;
  482. /* compute the compressed row size */
  483. if (!s->interlace_type) {
  484. s->crow_size = s->row_size + 1;
  485. } else {
  486. s->pass = 0;
  487. s->pass_row_size = ff_png_pass_row_size(s->pass,
  488. s->bits_per_pixel,
  489. s->width);
  490. s->crow_size = s->pass_row_size + 1;
  491. }
  492. ff_dlog(avctx, "row_size=%d crow_size =%d\n",
  493. s->row_size, s->crow_size);
  494. s->image_buf = p->data[0];
  495. s->image_linesize = p->linesize[0];
  496. /* copy the palette if needed */
  497. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  498. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  499. /* empty row is used if differencing to the first row */
  500. s->last_row = av_mallocz(s->row_size);
  501. if (!s->last_row)
  502. goto fail;
  503. if (s->interlace_type ||
  504. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  505. s->tmp_row = av_malloc(s->row_size);
  506. if (!s->tmp_row)
  507. goto fail;
  508. }
  509. /* compressed row */
  510. crow_buf_base = av_malloc(s->row_size + 16);
  511. if (!crow_buf_base)
  512. goto fail;
  513. /* we want crow_buf+1 to be 16-byte aligned */
  514. s->crow_buf = crow_buf_base + 15;
  515. s->zstream.avail_out = s->crow_size;
  516. s->zstream.next_out = s->crow_buf;
  517. }
  518. s->state |= PNG_IDAT;
  519. if (png_decode_idat(s, length) < 0)
  520. goto fail;
  521. bytestream2_skip(&s->gb, 4); /* crc */
  522. break;
  523. case MKTAG('P', 'L', 'T', 'E'):
  524. {
  525. int n, i, r, g, b;
  526. if ((length % 3) != 0 || length > 256 * 3)
  527. goto skip_tag;
  528. /* read the palette */
  529. n = length / 3;
  530. for (i = 0; i < n; i++) {
  531. r = bytestream2_get_byte(&s->gb);
  532. g = bytestream2_get_byte(&s->gb);
  533. b = bytestream2_get_byte(&s->gb);
  534. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  535. }
  536. for (; i < 256; i++)
  537. s->palette[i] = (0xff << 24);
  538. s->state |= PNG_PLTE;
  539. bytestream2_skip(&s->gb, 4); /* crc */
  540. }
  541. break;
  542. case MKTAG('t', 'R', 'N', 'S'):
  543. {
  544. int v, i;
  545. /* read the transparency. XXX: Only palette mode supported */
  546. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  547. length > 256 ||
  548. !(s->state & PNG_PLTE))
  549. goto skip_tag;
  550. for (i = 0; i < length; i++) {
  551. v = bytestream2_get_byte(&s->gb);
  552. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  553. }
  554. bytestream2_skip(&s->gb, 4); /* crc */
  555. }
  556. break;
  557. case MKTAG('I', 'E', 'N', 'D'):
  558. if (!(s->state & PNG_ALLIMAGE))
  559. goto fail;
  560. bytestream2_skip(&s->gb, 4); /* crc */
  561. goto exit_loop;
  562. default:
  563. /* skip tag */
  564. skip_tag:
  565. bytestream2_skip(&s->gb, length + 4);
  566. break;
  567. }
  568. }
  569. exit_loop:
  570. /* handle p-frames only if a predecessor frame is available */
  571. if (s->prev->data[0]) {
  572. if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
  573. int i, j;
  574. uint8_t *pd = p->data[0];
  575. uint8_t *pd_last = s->prev->data[0];
  576. for (j = 0; j < s->height; j++) {
  577. for (i = 0; i < s->width * s->bpp; i++)
  578. pd[i] += pd_last[i];
  579. pd += s->image_linesize;
  580. pd_last += s->image_linesize;
  581. }
  582. }
  583. }
  584. av_frame_unref(s->prev);
  585. if ((ret = av_frame_ref(s->prev, p)) < 0)
  586. goto fail;
  587. *got_frame = 1;
  588. ret = bytestream2_tell(&s->gb);
  589. the_end:
  590. inflateEnd(&s->zstream);
  591. av_free(crow_buf_base);
  592. s->crow_buf = NULL;
  593. av_freep(&s->last_row);
  594. av_freep(&s->tmp_row);
  595. return ret;
  596. fail:
  597. ret = -1;
  598. goto the_end;
  599. }
  600. static av_cold int png_dec_init(AVCodecContext *avctx)
  601. {
  602. PNGDecContext *s = avctx->priv_data;
  603. avctx->color_range = AVCOL_RANGE_JPEG;
  604. s->prev = av_frame_alloc();
  605. if (!s->prev)
  606. return AVERROR(ENOMEM);
  607. ff_pngdsp_init(&s->dsp);
  608. return 0;
  609. }
  610. static av_cold int png_dec_end(AVCodecContext *avctx)
  611. {
  612. PNGDecContext *s = avctx->priv_data;
  613. av_frame_free(&s->prev);
  614. return 0;
  615. }
  616. AVCodec ff_png_decoder = {
  617. .name = "png",
  618. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  619. .type = AVMEDIA_TYPE_VIDEO,
  620. .id = AV_CODEC_ID_PNG,
  621. .priv_data_size = sizeof(PNGDecContext),
  622. .init = png_dec_init,
  623. .close = png_dec_end,
  624. .decode = decode_frame,
  625. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  626. };