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.

672 lines
22KB

  1. /*
  2. * PNG image format
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "bytestream.h"
  23. #include "png.h"
  24. #include "dsputil.h"
  25. /* TODO:
  26. * - add 2, 4 and 16 bit depth support
  27. */
  28. #include <zlib.h>
  29. //#define DEBUG
  30. typedef struct PNGDecContext {
  31. DSPContext dsp;
  32. const uint8_t *bytestream;
  33. const uint8_t *bytestream_start;
  34. const uint8_t *bytestream_end;
  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(DSPContext *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_png_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, const uint8_t *src, 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 = length;
  337. s->zstream.next_in = s->bytestream;
  338. s->bytestream += length;
  339. if(s->bytestream > s->bytestream_end)
  340. return -1;
  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 *data_size,
  359. AVPacket *avpkt)
  360. {
  361. const uint8_t *buf = avpkt->data;
  362. int buf_size = avpkt->size;
  363. PNGDecContext * const s = avctx->priv_data;
  364. AVFrame *picture = data;
  365. AVFrame *p;
  366. uint8_t *crow_buf_base = NULL;
  367. uint32_t tag, length;
  368. int ret, crc;
  369. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  370. avctx->coded_frame= s->current_picture;
  371. p = s->current_picture;
  372. s->bytestream_start=
  373. s->bytestream= buf;
  374. s->bytestream_end= buf + buf_size;
  375. /* check signature */
  376. if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
  377. memcmp(s->bytestream, ff_mngsig, 8) != 0)
  378. return -1;
  379. s->bytestream+= 8;
  380. s->y=
  381. s->state=0;
  382. // memset(s, 0, sizeof(PNGDecContext));
  383. /* init the zlib */
  384. s->zstream.zalloc = ff_png_zalloc;
  385. s->zstream.zfree = ff_png_zfree;
  386. s->zstream.opaque = NULL;
  387. ret = inflateInit(&s->zstream);
  388. if (ret != Z_OK)
  389. return -1;
  390. for(;;) {
  391. int tag32;
  392. if (s->bytestream >= s->bytestream_end)
  393. goto fail;
  394. length = bytestream_get_be32(&s->bytestream);
  395. if (length > 0x7fffffff)
  396. goto fail;
  397. tag32 = bytestream_get_be32(&s->bytestream);
  398. tag = bswap_32(tag32);
  399. dprintf(avctx, "png: tag=%c%c%c%c length=%u\n",
  400. (tag & 0xff),
  401. ((tag >> 8) & 0xff),
  402. ((tag >> 16) & 0xff),
  403. ((tag >> 24) & 0xff), length);
  404. switch(tag) {
  405. case MKTAG('I', 'H', 'D', 'R'):
  406. if (length != 13)
  407. goto fail;
  408. s->width = bytestream_get_be32(&s->bytestream);
  409. s->height = bytestream_get_be32(&s->bytestream);
  410. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  411. s->width= s->height= 0;
  412. goto fail;
  413. }
  414. s->bit_depth = *s->bytestream++;
  415. s->color_type = *s->bytestream++;
  416. s->compression_type = *s->bytestream++;
  417. s->filter_type = *s->bytestream++;
  418. s->interlace_type = *s->bytestream++;
  419. crc = bytestream_get_be32(&s->bytestream);
  420. s->state |= PNG_IHDR;
  421. dprintf(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
  422. s->width, s->height, s->bit_depth, s->color_type,
  423. s->compression_type, s->filter_type, s->interlace_type);
  424. break;
  425. case MKTAG('I', 'D', 'A', 'T'):
  426. if (!(s->state & PNG_IHDR))
  427. goto fail;
  428. if (!(s->state & PNG_IDAT)) {
  429. /* init image info */
  430. avctx->width = s->width;
  431. avctx->height = s->height;
  432. s->channels = ff_png_get_nb_channels(s->color_type);
  433. s->bits_per_pixel = s->bit_depth * s->channels;
  434. s->bpp = (s->bits_per_pixel + 7) >> 3;
  435. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  436. if (s->bit_depth == 8 &&
  437. s->color_type == PNG_COLOR_TYPE_RGB) {
  438. avctx->pix_fmt = PIX_FMT_RGB24;
  439. } else if (s->bit_depth == 8 &&
  440. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  441. avctx->pix_fmt = PIX_FMT_RGB32;
  442. } else if (s->bit_depth == 8 &&
  443. s->color_type == PNG_COLOR_TYPE_GRAY) {
  444. avctx->pix_fmt = PIX_FMT_GRAY8;
  445. } else if (s->bit_depth == 16 &&
  446. s->color_type == PNG_COLOR_TYPE_GRAY) {
  447. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  448. } else if (s->bit_depth == 16 &&
  449. s->color_type == PNG_COLOR_TYPE_RGB) {
  450. avctx->pix_fmt = PIX_FMT_RGB48BE;
  451. } else if (s->bit_depth == 1 &&
  452. s->color_type == PNG_COLOR_TYPE_GRAY) {
  453. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  454. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  455. avctx->pix_fmt = PIX_FMT_PAL8;
  456. } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  457. avctx->pix_fmt = PIX_FMT_Y400A;
  458. } else {
  459. goto fail;
  460. }
  461. if(p->data[0])
  462. avctx->release_buffer(avctx, p);
  463. p->reference= 0;
  464. if(avctx->get_buffer(avctx, p) < 0){
  465. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  466. goto fail;
  467. }
  468. p->pict_type= FF_I_TYPE;
  469. p->key_frame= 1;
  470. p->interlaced_frame = !!s->interlace_type;
  471. /* compute the compressed row size */
  472. if (!s->interlace_type) {
  473. s->crow_size = s->row_size + 1;
  474. } else {
  475. s->pass = 0;
  476. s->pass_row_size = ff_png_pass_row_size(s->pass,
  477. s->bits_per_pixel,
  478. s->width);
  479. s->crow_size = s->pass_row_size + 1;
  480. }
  481. dprintf(avctx, "row_size=%d crow_size =%d\n",
  482. s->row_size, s->crow_size);
  483. s->image_buf = p->data[0];
  484. s->image_linesize = p->linesize[0];
  485. /* copy the palette if needed */
  486. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  487. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  488. /* empty row is used if differencing to the first row */
  489. s->last_row = av_mallocz(s->row_size);
  490. if (!s->last_row)
  491. goto fail;
  492. if (s->interlace_type ||
  493. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  494. s->tmp_row = av_malloc(s->row_size);
  495. if (!s->tmp_row)
  496. goto fail;
  497. }
  498. /* compressed row */
  499. crow_buf_base = av_malloc(s->row_size + 16);
  500. if (!crow_buf_base)
  501. goto fail;
  502. /* we want crow_buf+1 to be 16-byte aligned */
  503. s->crow_buf = crow_buf_base + 15;
  504. s->zstream.avail_out = s->crow_size;
  505. s->zstream.next_out = s->crow_buf;
  506. }
  507. s->state |= PNG_IDAT;
  508. if (png_decode_idat(s, length) < 0)
  509. goto fail;
  510. /* skip crc */
  511. crc = bytestream_get_be32(&s->bytestream);
  512. break;
  513. case MKTAG('P', 'L', 'T', 'E'):
  514. {
  515. int n, i, r, g, b;
  516. if ((length % 3) != 0 || length > 256 * 3)
  517. goto skip_tag;
  518. /* read the palette */
  519. n = length / 3;
  520. for(i=0;i<n;i++) {
  521. r = *s->bytestream++;
  522. g = *s->bytestream++;
  523. b = *s->bytestream++;
  524. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  525. }
  526. for(;i<256;i++) {
  527. s->palette[i] = (0xff << 24);
  528. }
  529. s->state |= PNG_PLTE;
  530. crc = bytestream_get_be32(&s->bytestream);
  531. }
  532. break;
  533. case MKTAG('t', 'R', 'N', 'S'):
  534. {
  535. int v, i;
  536. /* read the transparency. XXX: Only palette mode supported */
  537. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  538. length > 256 ||
  539. !(s->state & PNG_PLTE))
  540. goto skip_tag;
  541. for(i=0;i<length;i++) {
  542. v = *s->bytestream++;
  543. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  544. }
  545. crc = bytestream_get_be32(&s->bytestream);
  546. }
  547. break;
  548. case MKTAG('I', 'E', 'N', 'D'):
  549. if (!(s->state & PNG_ALLIMAGE))
  550. goto fail;
  551. crc = bytestream_get_be32(&s->bytestream);
  552. goto exit_loop;
  553. default:
  554. /* skip tag */
  555. skip_tag:
  556. s->bytestream += length + 4;
  557. break;
  558. }
  559. }
  560. exit_loop:
  561. /* handle p-frames only if a predecessor frame is available */
  562. if(s->last_picture->data[0] != NULL) {
  563. if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
  564. int i, j;
  565. uint8_t *pd = s->current_picture->data[0];
  566. uint8_t *pd_last = s->last_picture->data[0];
  567. for(j=0; j < s->height; j++) {
  568. for(i=0; i < s->width * s->bpp; i++) {
  569. pd[i] += pd_last[i];
  570. }
  571. pd += s->image_linesize;
  572. pd_last += s->image_linesize;
  573. }
  574. }
  575. }
  576. *picture= *s->current_picture;
  577. *data_size = sizeof(AVFrame);
  578. ret = s->bytestream - s->bytestream_start;
  579. the_end:
  580. inflateEnd(&s->zstream);
  581. av_free(crow_buf_base);
  582. s->crow_buf = NULL;
  583. av_freep(&s->last_row);
  584. av_freep(&s->tmp_row);
  585. return ret;
  586. fail:
  587. ret = -1;
  588. goto the_end;
  589. }
  590. static av_cold int png_dec_init(AVCodecContext *avctx){
  591. PNGDecContext *s = avctx->priv_data;
  592. s->current_picture = &s->picture1;
  593. s->last_picture = &s->picture2;
  594. avcodec_get_frame_defaults(&s->picture1);
  595. avcodec_get_frame_defaults(&s->picture2);
  596. dsputil_init(&s->dsp, avctx);
  597. return 0;
  598. }
  599. static av_cold int png_dec_end(AVCodecContext *avctx)
  600. {
  601. PNGDecContext *s = avctx->priv_data;
  602. if (s->picture1.data[0])
  603. avctx->release_buffer(avctx, &s->picture1);
  604. if (s->picture2.data[0])
  605. avctx->release_buffer(avctx, &s->picture2);
  606. return 0;
  607. }
  608. AVCodec png_decoder = {
  609. "png",
  610. AVMEDIA_TYPE_VIDEO,
  611. CODEC_ID_PNG,
  612. sizeof(PNGDecContext),
  613. png_dec_init,
  614. NULL,
  615. png_dec_end,
  616. decode_frame,
  617. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  618. NULL,
  619. .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
  620. };