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.

663 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 "png.h"
  25. #include "pngdsp.h"
  26. /* TODO:
  27. * - add 2, 4 and 16 bit depth support
  28. */
  29. #include <zlib.h>
  30. //#define DEBUG
  31. typedef struct PNGDecContext {
  32. PNGDSPContext dsp;
  33. GetByteContext gb;
  34. AVFrame picture1, picture2;
  35. AVFrame *current_picture, *last_picture;
  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, const uint8_t *src, int width, int loco)
  224. {
  225. int j;
  226. unsigned int r, g, b, a;
  227. for(j = 0;j < width; j++) {
  228. r = src[0];
  229. g = src[1];
  230. b = src[2];
  231. a = src[3];
  232. if(loco) {
  233. r = (r+g)&0xff;
  234. b = (b+g)&0xff;
  235. }
  236. *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
  237. dst += 4;
  238. src += 4;
  239. }
  240. }
  241. static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
  242. {
  243. if(loco)
  244. convert_to_rgb32_loco(dst, src, width, 1);
  245. else
  246. convert_to_rgb32_loco(dst, src, width, 0);
  247. }
  248. static void deloco_rgb24(uint8_t *dst, int size)
  249. {
  250. int i;
  251. for(i=0; i<size; i+=3) {
  252. int g = dst[i+1];
  253. dst[i+0] += g;
  254. dst[i+2] += g;
  255. }
  256. }
  257. /* process exactly one decompressed row */
  258. static void png_handle_row(PNGDecContext *s)
  259. {
  260. uint8_t *ptr, *last_row;
  261. int got_line;
  262. if (!s->interlace_type) {
  263. ptr = s->image_buf + s->image_linesize * s->y;
  264. /* need to swap bytes correctly for RGB_ALPHA */
  265. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  266. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  267. s->last_row, s->row_size, s->bpp);
  268. convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
  269. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  270. } else {
  271. /* in normal case, we avoid one copy */
  272. if (s->y == 0)
  273. last_row = s->last_row;
  274. else
  275. last_row = ptr - s->image_linesize;
  276. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  277. last_row, s->row_size, s->bpp);
  278. }
  279. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  280. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  281. s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
  282. deloco_rgb24(ptr - s->image_linesize, s->row_size);
  283. s->y++;
  284. if (s->y == s->height) {
  285. s->state |= PNG_ALLIMAGE;
  286. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  287. s->color_type == PNG_COLOR_TYPE_RGB)
  288. deloco_rgb24(ptr, s->row_size);
  289. }
  290. } else {
  291. got_line = 0;
  292. for(;;) {
  293. ptr = s->image_buf + s->image_linesize * s->y;
  294. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  295. /* if we already read one row, it is time to stop to
  296. wait for the next one */
  297. if (got_line)
  298. break;
  299. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  300. s->last_row, s->pass_row_size, s->bpp);
  301. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  302. got_line = 1;
  303. }
  304. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  305. /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
  306. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  307. s->color_type, s->last_row);
  308. }
  309. s->y++;
  310. if (s->y == s->height) {
  311. for(;;) {
  312. if (s->pass == NB_PASSES - 1) {
  313. s->state |= PNG_ALLIMAGE;
  314. goto the_end;
  315. } else {
  316. s->pass++;
  317. s->y = 0;
  318. s->pass_row_size = ff_png_pass_row_size(s->pass,
  319. s->bits_per_pixel,
  320. s->width);
  321. s->crow_size = s->pass_row_size + 1;
  322. if (s->pass_row_size != 0)
  323. break;
  324. /* skip pass if empty row */
  325. }
  326. }
  327. }
  328. }
  329. the_end: ;
  330. }
  331. }
  332. static int png_decode_idat(PNGDecContext *s, int length)
  333. {
  334. int ret;
  335. s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
  336. s->zstream.next_in = s->gb.buffer;
  337. bytestream2_skip(&s->gb, length);
  338. /* decode one line if possible */
  339. while (s->zstream.avail_in > 0) {
  340. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  341. if (ret != Z_OK && ret != Z_STREAM_END) {
  342. return -1;
  343. }
  344. if (s->zstream.avail_out == 0) {
  345. if (!(s->state & PNG_ALLIMAGE)) {
  346. png_handle_row(s);
  347. }
  348. s->zstream.avail_out = s->crow_size;
  349. s->zstream.next_out = s->crow_buf;
  350. }
  351. }
  352. return 0;
  353. }
  354. static int decode_frame(AVCodecContext *avctx,
  355. void *data, int *data_size,
  356. AVPacket *avpkt)
  357. {
  358. const uint8_t *buf = avpkt->data;
  359. int buf_size = avpkt->size;
  360. PNGDecContext * const s = avctx->priv_data;
  361. AVFrame *picture = data;
  362. AVFrame *p;
  363. uint8_t *crow_buf_base = NULL;
  364. uint32_t tag, length;
  365. int ret;
  366. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  367. avctx->coded_frame= s->current_picture;
  368. p = s->current_picture;
  369. /* check signature */
  370. if (buf_size < 8 ||
  371. memcmp(buf, ff_pngsig, 8) != 0 &&
  372. memcmp(buf, ff_mngsig, 8) != 0)
  373. return -1;
  374. bytestream2_init(&s->gb, buf + 8, buf_size - 8);
  375. s->y=
  376. s->state=0;
  377. // memset(s, 0, sizeof(PNGDecContext));
  378. /* init the zlib */
  379. s->zstream.zalloc = ff_png_zalloc;
  380. s->zstream.zfree = ff_png_zfree;
  381. s->zstream.opaque = NULL;
  382. ret = inflateInit(&s->zstream);
  383. if (ret != Z_OK)
  384. return -1;
  385. for(;;) {
  386. if (bytestream2_get_bytes_left(&s->gb) <= 0)
  387. goto fail;
  388. length = bytestream2_get_be32(&s->gb);
  389. if (length > 0x7fffffff)
  390. goto fail;
  391. tag = bytestream2_get_le32(&s->gb);
  392. av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
  393. (tag & 0xff),
  394. ((tag >> 8) & 0xff),
  395. ((tag >> 16) & 0xff),
  396. ((tag >> 24) & 0xff), length);
  397. switch(tag) {
  398. case MKTAG('I', 'H', 'D', 'R'):
  399. if (length != 13)
  400. goto fail;
  401. s->width = bytestream2_get_be32(&s->gb);
  402. s->height = bytestream2_get_be32(&s->gb);
  403. if(av_image_check_size(s->width, s->height, 0, avctx)){
  404. s->width= s->height= 0;
  405. goto fail;
  406. }
  407. s->bit_depth = bytestream2_get_byte(&s->gb);
  408. s->color_type = bytestream2_get_byte(&s->gb);
  409. s->compression_type = bytestream2_get_byte(&s->gb);
  410. s->filter_type = bytestream2_get_byte(&s->gb);
  411. s->interlace_type = bytestream2_get_byte(&s->gb);
  412. bytestream2_skip(&s->gb, 4); /* crc */
  413. s->state |= PNG_IHDR;
  414. av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d 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 = PIX_FMT_RGB24;
  432. } else if (s->bit_depth == 8 &&
  433. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  434. avctx->pix_fmt = PIX_FMT_RGB32;
  435. } else if (s->bit_depth == 8 &&
  436. s->color_type == PNG_COLOR_TYPE_GRAY) {
  437. avctx->pix_fmt = PIX_FMT_GRAY8;
  438. } else if (s->bit_depth == 16 &&
  439. s->color_type == PNG_COLOR_TYPE_GRAY) {
  440. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  441. } else if (s->bit_depth == 16 &&
  442. s->color_type == PNG_COLOR_TYPE_RGB) {
  443. avctx->pix_fmt = PIX_FMT_RGB48BE;
  444. } else if (s->bit_depth == 1 &&
  445. s->color_type == PNG_COLOR_TYPE_GRAY) {
  446. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  447. } else if (s->bit_depth == 8 &&
  448. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  449. avctx->pix_fmt = PIX_FMT_PAL8;
  450. } else if (s->bit_depth == 8 &&
  451. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  452. avctx->pix_fmt = PIX_FMT_Y400A;
  453. } else {
  454. goto fail;
  455. }
  456. if(p->data[0])
  457. avctx->release_buffer(avctx, p);
  458. p->reference= 0;
  459. if(avctx->get_buffer(avctx, p) < 0){
  460. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  461. goto fail;
  462. }
  463. p->pict_type= AV_PICTURE_TYPE_I;
  464. p->key_frame= 1;
  465. p->interlaced_frame = !!s->interlace_type;
  466. /* compute the compressed row size */
  467. if (!s->interlace_type) {
  468. s->crow_size = s->row_size + 1;
  469. } else {
  470. s->pass = 0;
  471. s->pass_row_size = ff_png_pass_row_size(s->pass,
  472. s->bits_per_pixel,
  473. s->width);
  474. s->crow_size = s->pass_row_size + 1;
  475. }
  476. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  477. s->row_size, s->crow_size);
  478. s->image_buf = p->data[0];
  479. s->image_linesize = p->linesize[0];
  480. /* copy the palette if needed */
  481. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  482. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  483. /* empty row is used if differencing to the first row */
  484. s->last_row = av_mallocz(s->row_size);
  485. if (!s->last_row)
  486. goto fail;
  487. if (s->interlace_type ||
  488. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  489. s->tmp_row = av_malloc(s->row_size);
  490. if (!s->tmp_row)
  491. goto fail;
  492. }
  493. /* compressed row */
  494. crow_buf_base = av_malloc(s->row_size + 16);
  495. if (!crow_buf_base)
  496. goto fail;
  497. /* we want crow_buf+1 to be 16-byte aligned */
  498. s->crow_buf = crow_buf_base + 15;
  499. s->zstream.avail_out = s->crow_size;
  500. s->zstream.next_out = s->crow_buf;
  501. }
  502. s->state |= PNG_IDAT;
  503. if (png_decode_idat(s, length) < 0)
  504. goto fail;
  505. bytestream2_skip(&s->gb, 4); /* crc */
  506. break;
  507. case MKTAG('P', 'L', 'T', 'E'):
  508. {
  509. int n, i, r, g, b;
  510. if ((length % 3) != 0 || length > 256 * 3)
  511. goto skip_tag;
  512. /* read the palette */
  513. n = length / 3;
  514. for(i=0;i<n;i++) {
  515. r = bytestream2_get_byte(&s->gb);
  516. g = bytestream2_get_byte(&s->gb);
  517. b = bytestream2_get_byte(&s->gb);
  518. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  519. }
  520. for(;i<256;i++) {
  521. s->palette[i] = (0xff << 24);
  522. }
  523. s->state |= PNG_PLTE;
  524. bytestream2_skip(&s->gb, 4); /* crc */
  525. }
  526. break;
  527. case MKTAG('t', 'R', 'N', 'S'):
  528. {
  529. int v, i;
  530. /* read the transparency. XXX: Only palette mode supported */
  531. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  532. length > 256 ||
  533. !(s->state & PNG_PLTE))
  534. goto skip_tag;
  535. for(i=0;i<length;i++) {
  536. v = bytestream2_get_byte(&s->gb);
  537. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  538. }
  539. bytestream2_skip(&s->gb, 4); /* crc */
  540. }
  541. break;
  542. case MKTAG('I', 'E', 'N', 'D'):
  543. if (!(s->state & PNG_ALLIMAGE))
  544. goto fail;
  545. bytestream2_skip(&s->gb, 4); /* crc */
  546. goto exit_loop;
  547. default:
  548. /* skip tag */
  549. skip_tag:
  550. bytestream2_skip(&s->gb, length + 4);
  551. break;
  552. }
  553. }
  554. exit_loop:
  555. /* handle p-frames only if a predecessor frame is available */
  556. if(s->last_picture->data[0] != NULL) {
  557. if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
  558. int i, j;
  559. uint8_t *pd = s->current_picture->data[0];
  560. uint8_t *pd_last = s->last_picture->data[0];
  561. for(j=0; j < s->height; j++) {
  562. for(i=0; i < s->width * s->bpp; i++) {
  563. pd[i] += pd_last[i];
  564. }
  565. pd += s->image_linesize;
  566. pd_last += s->image_linesize;
  567. }
  568. }
  569. }
  570. *picture= *s->current_picture;
  571. *data_size = sizeof(AVFrame);
  572. ret = bytestream2_tell(&s->gb);
  573. the_end:
  574. inflateEnd(&s->zstream);
  575. av_free(crow_buf_base);
  576. s->crow_buf = NULL;
  577. av_freep(&s->last_row);
  578. av_freep(&s->tmp_row);
  579. return ret;
  580. fail:
  581. ret = -1;
  582. goto the_end;
  583. }
  584. static av_cold int png_dec_init(AVCodecContext *avctx){
  585. PNGDecContext *s = avctx->priv_data;
  586. s->current_picture = &s->picture1;
  587. s->last_picture = &s->picture2;
  588. avcodec_get_frame_defaults(&s->picture1);
  589. avcodec_get_frame_defaults(&s->picture2);
  590. ff_pngdsp_init(&s->dsp);
  591. return 0;
  592. }
  593. static av_cold int png_dec_end(AVCodecContext *avctx)
  594. {
  595. PNGDecContext *s = avctx->priv_data;
  596. if (s->picture1.data[0])
  597. avctx->release_buffer(avctx, &s->picture1);
  598. if (s->picture2.data[0])
  599. avctx->release_buffer(avctx, &s->picture2);
  600. return 0;
  601. }
  602. AVCodec ff_png_decoder = {
  603. .name = "png",
  604. .type = AVMEDIA_TYPE_VIDEO,
  605. .id = CODEC_ID_PNG,
  606. .priv_data_size = sizeof(PNGDecContext),
  607. .init = png_dec_init,
  608. .close = png_dec_end,
  609. .decode = decode_frame,
  610. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  611. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  612. };