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.

661 lines
22KB

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