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.

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