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.

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