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.

674 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. r = dst[0]; \
  147. if (bpp >= 2) \
  148. g = dst[1]; \
  149. if (bpp >= 3) \
  150. b = dst[2]; \
  151. if (bpp >= 4) \
  152. a = dst[3]; \
  153. for (; i < size; i += bpp) { \
  154. dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
  155. if (bpp == 1) \
  156. continue; \
  157. dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
  158. if (bpp == 2) \
  159. continue; \
  160. dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
  161. if (bpp == 3) \
  162. continue; \
  163. dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
  164. } \
  165. }
  166. #define UNROLL_FILTER(op) \
  167. if (bpp == 1) \
  168. UNROLL1(1, op) \
  169. else if (bpp == 2) \
  170. UNROLL1(2, op) \
  171. else if (bpp == 3) \
  172. UNROLL1(3, op) \
  173. else if (bpp == 4) \
  174. UNROLL1(4, op) \
  175. else { \
  176. for (; i < size; i += bpp) { \
  177. int j; \
  178. for (j = 0; j < bpp; j++) \
  179. dst[i + j] = op(dst[i + j - bpp], \
  180. 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 > 1 && 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 &&
  388. memcmp(buf, ff_mngsig, 8) != 0)
  389. return -1;
  390. bytestream2_init(&s->gb, buf + 8, buf_size - 8);
  391. s->y = s->state = 0;
  392. /* init the zlib */
  393. s->zstream.zalloc = ff_png_zalloc;
  394. s->zstream.zfree = ff_png_zfree;
  395. s->zstream.opaque = NULL;
  396. ret = inflateInit(&s->zstream);
  397. if (ret != Z_OK)
  398. return -1;
  399. for (;;) {
  400. if (bytestream2_get_bytes_left(&s->gb) <= 0)
  401. goto fail;
  402. length = bytestream2_get_be32(&s->gb);
  403. if (length > 0x7fffffff)
  404. goto fail;
  405. tag = bytestream2_get_le32(&s->gb);
  406. av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
  407. (tag & 0xff),
  408. ((tag >> 8) & 0xff),
  409. ((tag >> 16) & 0xff),
  410. ((tag >> 24) & 0xff), length);
  411. switch (tag) {
  412. case MKTAG('I', 'H', 'D', 'R'):
  413. if (length != 13)
  414. goto fail;
  415. s->width = bytestream2_get_be32(&s->gb);
  416. s->height = bytestream2_get_be32(&s->gb);
  417. if (av_image_check_size(s->width, s->height, 0, avctx)) {
  418. s->width = s->height = 0;
  419. goto fail;
  420. }
  421. s->bit_depth = bytestream2_get_byte(&s->gb);
  422. s->color_type = bytestream2_get_byte(&s->gb);
  423. s->compression_type = bytestream2_get_byte(&s->gb);
  424. s->filter_type = bytestream2_get_byte(&s->gb);
  425. s->interlace_type = bytestream2_get_byte(&s->gb);
  426. bytestream2_skip(&s->gb, 4); /* crc */
  427. s->state |= PNG_IHDR;
  428. av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d "
  429. "compression_type=%d filter_type=%d interlace_type=%d\n",
  430. s->width, s->height, s->bit_depth, s->color_type,
  431. s->compression_type, s->filter_type, s->interlace_type);
  432. break;
  433. case MKTAG('I', 'D', 'A', 'T'):
  434. if (!(s->state & PNG_IHDR))
  435. goto fail;
  436. if (!(s->state & PNG_IDAT)) {
  437. /* init image info */
  438. avctx->width = s->width;
  439. avctx->height = s->height;
  440. s->channels = ff_png_get_nb_channels(s->color_type);
  441. s->bits_per_pixel = s->bit_depth * s->channels;
  442. s->bpp = (s->bits_per_pixel + 7) >> 3;
  443. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  444. if (s->bit_depth == 8 &&
  445. s->color_type == PNG_COLOR_TYPE_RGB) {
  446. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  447. } else if (s->bit_depth == 8 &&
  448. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  449. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  450. } else if (s->bit_depth == 8 &&
  451. s->color_type == PNG_COLOR_TYPE_GRAY) {
  452. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  453. } else if (s->bit_depth == 16 &&
  454. s->color_type == PNG_COLOR_TYPE_GRAY) {
  455. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  456. } else if (s->bit_depth == 16 &&
  457. s->color_type == PNG_COLOR_TYPE_RGB) {
  458. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  459. } else if (s->bit_depth == 1 &&
  460. s->color_type == PNG_COLOR_TYPE_GRAY) {
  461. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  462. } else if (s->bit_depth == 8 &&
  463. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  464. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  465. } else if (s->bit_depth == 8 &&
  466. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  467. avctx->pix_fmt = AV_PIX_FMT_Y400A;
  468. } else {
  469. goto fail;
  470. }
  471. if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0) {
  472. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  473. goto fail;
  474. }
  475. p->pict_type = AV_PICTURE_TYPE_I;
  476. p->key_frame = 1;
  477. p->interlaced_frame = !!s->interlace_type;
  478. /* compute the compressed row size */
  479. if (!s->interlace_type) {
  480. s->crow_size = s->row_size + 1;
  481. } else {
  482. s->pass = 0;
  483. s->pass_row_size = ff_png_pass_row_size(s->pass,
  484. s->bits_per_pixel,
  485. s->width);
  486. s->crow_size = s->pass_row_size + 1;
  487. }
  488. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  489. s->row_size, s->crow_size);
  490. s->image_buf = p->data[0];
  491. s->image_linesize = p->linesize[0];
  492. /* copy the palette if needed */
  493. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  494. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  495. /* empty row is used if differencing to the first row */
  496. s->last_row = av_mallocz(s->row_size);
  497. if (!s->last_row)
  498. goto fail;
  499. if (s->interlace_type ||
  500. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  501. s->tmp_row = av_malloc(s->row_size);
  502. if (!s->tmp_row)
  503. goto fail;
  504. }
  505. /* compressed row */
  506. crow_buf_base = av_malloc(s->row_size + 16);
  507. if (!crow_buf_base)
  508. goto fail;
  509. /* we want crow_buf+1 to be 16-byte aligned */
  510. s->crow_buf = crow_buf_base + 15;
  511. s->zstream.avail_out = s->crow_size;
  512. s->zstream.next_out = s->crow_buf;
  513. }
  514. s->state |= PNG_IDAT;
  515. if (png_decode_idat(s, length) < 0)
  516. goto fail;
  517. bytestream2_skip(&s->gb, 4); /* crc */
  518. break;
  519. case MKTAG('P', 'L', 'T', 'E'):
  520. {
  521. int n, i, r, g, b;
  522. if ((length % 3) != 0 || length > 256 * 3)
  523. goto skip_tag;
  524. /* read the palette */
  525. n = length / 3;
  526. for (i = 0; i < n; i++) {
  527. r = bytestream2_get_byte(&s->gb);
  528. g = bytestream2_get_byte(&s->gb);
  529. b = bytestream2_get_byte(&s->gb);
  530. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  531. }
  532. for (; i < 256; i++)
  533. s->palette[i] = (0xff << 24);
  534. s->state |= PNG_PLTE;
  535. bytestream2_skip(&s->gb, 4); /* crc */
  536. }
  537. break;
  538. case MKTAG('t', 'R', 'N', 'S'):
  539. {
  540. int v, i;
  541. /* read the transparency. XXX: Only palette mode supported */
  542. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  543. length > 256 ||
  544. !(s->state & PNG_PLTE))
  545. goto skip_tag;
  546. for (i = 0; i < length; i++) {
  547. v = bytestream2_get_byte(&s->gb);
  548. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  549. }
  550. bytestream2_skip(&s->gb, 4); /* crc */
  551. }
  552. break;
  553. case MKTAG('I', 'E', 'N', 'D'):
  554. if (!(s->state & PNG_ALLIMAGE))
  555. goto fail;
  556. bytestream2_skip(&s->gb, 4); /* crc */
  557. goto exit_loop;
  558. default:
  559. /* skip tag */
  560. skip_tag:
  561. bytestream2_skip(&s->gb, length + 4);
  562. break;
  563. }
  564. }
  565. exit_loop:
  566. /* handle p-frames only if a predecessor frame is available */
  567. if (s->prev->data[0]) {
  568. if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
  569. int i, j;
  570. uint8_t *pd = p->data[0];
  571. uint8_t *pd_last = s->prev->data[0];
  572. for (j = 0; j < s->height; j++) {
  573. for (i = 0; i < s->width * s->bpp; i++)
  574. pd[i] += pd_last[i];
  575. pd += s->image_linesize;
  576. pd_last += s->image_linesize;
  577. }
  578. }
  579. }
  580. av_frame_unref(s->prev);
  581. if ((ret = av_frame_ref(s->prev, p)) < 0)
  582. goto fail;
  583. *got_frame = 1;
  584. ret = bytestream2_tell(&s->gb);
  585. the_end:
  586. inflateEnd(&s->zstream);
  587. av_free(crow_buf_base);
  588. s->crow_buf = NULL;
  589. av_freep(&s->last_row);
  590. av_freep(&s->tmp_row);
  591. return ret;
  592. fail:
  593. ret = -1;
  594. goto the_end;
  595. }
  596. static av_cold int png_dec_init(AVCodecContext *avctx)
  597. {
  598. PNGDecContext *s = avctx->priv_data;
  599. s->prev = av_frame_alloc();
  600. if (!s->prev)
  601. return AVERROR(ENOMEM);
  602. ff_pngdsp_init(&s->dsp);
  603. return 0;
  604. }
  605. static av_cold int png_dec_end(AVCodecContext *avctx)
  606. {
  607. PNGDecContext *s = avctx->priv_data;
  608. av_frame_free(&s->prev);
  609. return 0;
  610. }
  611. AVCodec ff_png_decoder = {
  612. .name = "png",
  613. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  614. .type = AVMEDIA_TYPE_VIDEO,
  615. .id = AV_CODEC_ID_PNG,
  616. .priv_data_size = sizeof(PNGDecContext),
  617. .init = png_dec_init,
  618. .close = png_dec_end,
  619. .decode = decode_frame,
  620. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  621. };