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.

674 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 "libavcore/imgutils.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "png.h"
  25. #include "dsputil.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. DSPContext dsp;
  33. const uint8_t *bytestream;
  34. const uint8_t *bytestream_start;
  35. const uint8_t *bytestream_end;
  36. AVFrame picture1, picture2;
  37. AVFrame *current_picture, *last_picture;
  38. int state;
  39. int width, height;
  40. int bit_depth;
  41. int color_type;
  42. int compression_type;
  43. int interlace_type;
  44. int filter_type;
  45. int channels;
  46. int bits_per_pixel;
  47. int bpp;
  48. uint8_t *image_buf;
  49. int image_linesize;
  50. uint32_t palette[256];
  51. uint8_t *crow_buf;
  52. uint8_t *last_row;
  53. uint8_t *tmp_row;
  54. int pass;
  55. int crow_size; /* compressed row size (include filter type) */
  56. int row_size; /* decompressed row size */
  57. int pass_row_size; /* decompress row size of the current pass */
  58. int y;
  59. z_stream zstream;
  60. } PNGDecContext;
  61. /* Mask to determine which y pixels can be written in a pass */
  62. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  63. 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
  64. };
  65. /* Mask to determine which pixels to overwrite while displaying */
  66. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  67. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  68. };
  69. /* NOTE: we try to construct a good looking image at each pass. width
  70. is the original image width. We also do pixel format conversion at
  71. this stage */
  72. static void png_put_interlaced_row(uint8_t *dst, int width,
  73. int bits_per_pixel, int pass,
  74. int color_type, const uint8_t *src)
  75. {
  76. int x, mask, dsp_mask, j, src_x, b, bpp;
  77. uint8_t *d;
  78. const uint8_t *s;
  79. mask = ff_png_pass_mask[pass];
  80. dsp_mask = png_pass_dsp_mask[pass];
  81. switch(bits_per_pixel) {
  82. case 1:
  83. /* we must initialize the line to zero before writing to it */
  84. if (pass == 0)
  85. memset(dst, 0, (width + 7) >> 3);
  86. src_x = 0;
  87. for(x = 0; x < width; x++) {
  88. j = (x & 7);
  89. if ((dsp_mask << j) & 0x80) {
  90. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  91. dst[x >> 3] |= b << (7 - j);
  92. }
  93. if ((mask << j) & 0x80)
  94. src_x++;
  95. }
  96. break;
  97. default:
  98. bpp = bits_per_pixel >> 3;
  99. d = dst;
  100. s = src;
  101. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  102. for(x = 0; x < width; x++) {
  103. j = x & 7;
  104. if ((dsp_mask << j) & 0x80) {
  105. *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
  106. }
  107. d += bpp;
  108. if ((mask << j) & 0x80)
  109. s += bpp;
  110. }
  111. } else {
  112. for(x = 0; x < width; x++) {
  113. j = x & 7;
  114. if ((dsp_mask << j) & 0x80) {
  115. memcpy(d, s, bpp);
  116. }
  117. d += bpp;
  118. if ((mask << j) & 0x80)
  119. s += bpp;
  120. }
  121. }
  122. break;
  123. }
  124. }
  125. void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
  126. {
  127. int i;
  128. for(i = 0; i < w; i++) {
  129. int a, b, c, p, pa, pb, pc;
  130. a = dst[i - bpp];
  131. b = top[i];
  132. c = top[i - bpp];
  133. p = b - c;
  134. pc = a - c;
  135. pa = abs(p);
  136. pb = abs(pc);
  137. pc = abs(p + pc);
  138. if (pa <= pb && pa <= pc)
  139. p = a;
  140. else if (pb <= pc)
  141. p = b;
  142. else
  143. p = c;
  144. dst[i] = p + src[i];
  145. }
  146. }
  147. #define UNROLL1(bpp, op) {\
  148. r = dst[0];\
  149. if(bpp >= 2) g = dst[1];\
  150. if(bpp >= 3) b = dst[2];\
  151. if(bpp >= 4) a = dst[3];\
  152. for(; i < size; i+=bpp) {\
  153. dst[i+0] = r = op(r, src[i+0], last[i+0]);\
  154. if(bpp == 1) continue;\
  155. dst[i+1] = g = op(g, src[i+1], last[i+1]);\
  156. if(bpp == 2) continue;\
  157. dst[i+2] = b = op(b, src[i+2], last[i+2]);\
  158. if(bpp == 3) continue;\
  159. dst[i+3] = a = op(a, src[i+3], last[i+3]);\
  160. }\
  161. }
  162. #define UNROLL_FILTER(op)\
  163. if(bpp == 1) UNROLL1(1, op)\
  164. else if(bpp == 2) UNROLL1(2, op)\
  165. else if(bpp == 3) UNROLL1(3, op)\
  166. else if(bpp == 4) UNROLL1(4, op)\
  167. else {\
  168. for (; i < size; i += bpp) {\
  169. int j;\
  170. for (j = 0; j < bpp; j++)\
  171. dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
  172. }\
  173. }
  174. /* NOTE: 'dst' can be equal to 'last' */
  175. static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
  176. uint8_t *src, uint8_t *last, int size, int bpp)
  177. {
  178. int i, p, r, g, b, a;
  179. switch(filter_type) {
  180. case PNG_FILTER_VALUE_NONE:
  181. memcpy(dst, src, size);
  182. break;
  183. case PNG_FILTER_VALUE_SUB:
  184. for(i = 0; i < bpp; i++) {
  185. dst[i] = src[i];
  186. }
  187. if(bpp == 4) {
  188. p = *(int*)dst;
  189. for(; i < size; i+=bpp) {
  190. int s = *(int*)(src+i);
  191. p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
  192. *(int*)(dst+i) = p;
  193. }
  194. } else {
  195. #define OP_SUB(x,s,l) x+s
  196. UNROLL_FILTER(OP_SUB);
  197. }
  198. break;
  199. case PNG_FILTER_VALUE_UP:
  200. dsp->add_bytes_l2(dst, src, last, size);
  201. break;
  202. case PNG_FILTER_VALUE_AVG:
  203. for(i = 0; i < bpp; i++) {
  204. p = (last[i] >> 1);
  205. dst[i] = p + src[i];
  206. }
  207. #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
  208. UNROLL_FILTER(OP_AVG);
  209. break;
  210. case PNG_FILTER_VALUE_PAETH:
  211. for(i = 0; i < bpp; i++) {
  212. p = last[i];
  213. dst[i] = p + src[i];
  214. }
  215. if(bpp > 1 && size > 4) {
  216. // would write off the end of the array if we let it process the last pixel with bpp=3
  217. int w = bpp==4 ? size : size-3;
  218. dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
  219. i = w;
  220. }
  221. ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
  222. break;
  223. }
  224. }
  225. static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, 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 = length;
  338. s->zstream.next_in = s->bytestream;
  339. s->bytestream += length;
  340. if(s->bytestream > s->bytestream_end)
  341. return -1;
  342. /* decode one line if possible */
  343. while (s->zstream.avail_in > 0) {
  344. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  345. if (ret != Z_OK && ret != Z_STREAM_END) {
  346. return -1;
  347. }
  348. if (s->zstream.avail_out == 0) {
  349. if (!(s->state & PNG_ALLIMAGE)) {
  350. png_handle_row(s);
  351. }
  352. s->zstream.avail_out = s->crow_size;
  353. s->zstream.next_out = s->crow_buf;
  354. }
  355. }
  356. return 0;
  357. }
  358. static int decode_frame(AVCodecContext *avctx,
  359. void *data, int *data_size,
  360. AVPacket *avpkt)
  361. {
  362. const uint8_t *buf = avpkt->data;
  363. int buf_size = avpkt->size;
  364. PNGDecContext * const s = avctx->priv_data;
  365. AVFrame *picture = data;
  366. AVFrame *p;
  367. uint8_t *crow_buf_base = NULL;
  368. uint32_t tag, length;
  369. int ret, crc;
  370. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  371. avctx->coded_frame= s->current_picture;
  372. p = s->current_picture;
  373. s->bytestream_start=
  374. s->bytestream= buf;
  375. s->bytestream_end= buf + buf_size;
  376. /* check signature */
  377. if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
  378. memcmp(s->bytestream, ff_mngsig, 8) != 0)
  379. return -1;
  380. s->bytestream+= 8;
  381. s->y=
  382. s->state=0;
  383. // memset(s, 0, sizeof(PNGDecContext));
  384. /* init the zlib */
  385. s->zstream.zalloc = ff_png_zalloc;
  386. s->zstream.zfree = ff_png_zfree;
  387. s->zstream.opaque = NULL;
  388. ret = inflateInit(&s->zstream);
  389. if (ret != Z_OK)
  390. return -1;
  391. for(;;) {
  392. int tag32;
  393. if (s->bytestream >= s->bytestream_end)
  394. goto fail;
  395. length = bytestream_get_be32(&s->bytestream);
  396. if (length > 0x7fffffff)
  397. goto fail;
  398. tag32 = bytestream_get_be32(&s->bytestream);
  399. tag = av_bswap32(tag32);
  400. dprintf(avctx, "png: tag=%c%c%c%c length=%u\n",
  401. (tag & 0xff),
  402. ((tag >> 8) & 0xff),
  403. ((tag >> 16) & 0xff),
  404. ((tag >> 24) & 0xff), length);
  405. switch(tag) {
  406. case MKTAG('I', 'H', 'D', 'R'):
  407. if (length != 13)
  408. goto fail;
  409. s->width = bytestream_get_be32(&s->bytestream);
  410. s->height = bytestream_get_be32(&s->bytestream);
  411. if(av_check_image_size(s->width, s->height, 0, avctx)){
  412. s->width= s->height= 0;
  413. goto fail;
  414. }
  415. s->bit_depth = *s->bytestream++;
  416. s->color_type = *s->bytestream++;
  417. s->compression_type = *s->bytestream++;
  418. s->filter_type = *s->bytestream++;
  419. s->interlace_type = *s->bytestream++;
  420. crc = bytestream_get_be32(&s->bytestream);
  421. s->state |= PNG_IHDR;
  422. dprintf(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
  423. s->width, s->height, s->bit_depth, s->color_type,
  424. s->compression_type, s->filter_type, s->interlace_type);
  425. break;
  426. case MKTAG('I', 'D', 'A', 'T'):
  427. if (!(s->state & PNG_IHDR))
  428. goto fail;
  429. if (!(s->state & PNG_IDAT)) {
  430. /* init image info */
  431. avctx->width = s->width;
  432. avctx->height = s->height;
  433. s->channels = ff_png_get_nb_channels(s->color_type);
  434. s->bits_per_pixel = s->bit_depth * s->channels;
  435. s->bpp = (s->bits_per_pixel + 7) >> 3;
  436. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  437. if (s->bit_depth == 8 &&
  438. s->color_type == PNG_COLOR_TYPE_RGB) {
  439. avctx->pix_fmt = PIX_FMT_RGB24;
  440. } else if (s->bit_depth == 8 &&
  441. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  442. avctx->pix_fmt = PIX_FMT_RGB32;
  443. } else if (s->bit_depth == 8 &&
  444. s->color_type == PNG_COLOR_TYPE_GRAY) {
  445. avctx->pix_fmt = PIX_FMT_GRAY8;
  446. } else if (s->bit_depth == 16 &&
  447. s->color_type == PNG_COLOR_TYPE_GRAY) {
  448. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  449. } else if (s->bit_depth == 16 &&
  450. s->color_type == PNG_COLOR_TYPE_RGB) {
  451. avctx->pix_fmt = PIX_FMT_RGB48BE;
  452. } else if (s->bit_depth == 1 &&
  453. s->color_type == PNG_COLOR_TYPE_GRAY) {
  454. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  455. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  456. avctx->pix_fmt = PIX_FMT_PAL8;
  457. } else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  458. avctx->pix_fmt = PIX_FMT_Y400A;
  459. } else {
  460. goto fail;
  461. }
  462. if(p->data[0])
  463. avctx->release_buffer(avctx, p);
  464. p->reference= 0;
  465. if(avctx->get_buffer(avctx, p) < 0){
  466. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  467. goto fail;
  468. }
  469. p->pict_type= FF_I_TYPE;
  470. p->key_frame= 1;
  471. p->interlaced_frame = !!s->interlace_type;
  472. /* compute the compressed row size */
  473. if (!s->interlace_type) {
  474. s->crow_size = s->row_size + 1;
  475. } else {
  476. s->pass = 0;
  477. s->pass_row_size = ff_png_pass_row_size(s->pass,
  478. s->bits_per_pixel,
  479. s->width);
  480. s->crow_size = s->pass_row_size + 1;
  481. }
  482. dprintf(avctx, "row_size=%d crow_size =%d\n",
  483. s->row_size, s->crow_size);
  484. s->image_buf = p->data[0];
  485. s->image_linesize = p->linesize[0];
  486. /* copy the palette if needed */
  487. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  488. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  489. /* empty row is used if differencing to the first row */
  490. s->last_row = av_mallocz(s->row_size);
  491. if (!s->last_row)
  492. goto fail;
  493. if (s->interlace_type ||
  494. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  495. s->tmp_row = av_malloc(s->row_size);
  496. if (!s->tmp_row)
  497. goto fail;
  498. }
  499. /* compressed row */
  500. crow_buf_base = av_malloc(s->row_size + 16);
  501. if (!crow_buf_base)
  502. goto fail;
  503. /* we want crow_buf+1 to be 16-byte aligned */
  504. s->crow_buf = crow_buf_base + 15;
  505. s->zstream.avail_out = s->crow_size;
  506. s->zstream.next_out = s->crow_buf;
  507. }
  508. s->state |= PNG_IDAT;
  509. if (png_decode_idat(s, length) < 0)
  510. goto fail;
  511. /* skip crc */
  512. crc = bytestream_get_be32(&s->bytestream);
  513. break;
  514. case MKTAG('P', 'L', 'T', 'E'):
  515. {
  516. int n, i, r, g, b;
  517. if ((length % 3) != 0 || length > 256 * 3)
  518. goto skip_tag;
  519. /* read the palette */
  520. n = length / 3;
  521. for(i=0;i<n;i++) {
  522. r = *s->bytestream++;
  523. g = *s->bytestream++;
  524. b = *s->bytestream++;
  525. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  526. }
  527. for(;i<256;i++) {
  528. s->palette[i] = (0xff << 24);
  529. }
  530. s->state |= PNG_PLTE;
  531. crc = bytestream_get_be32(&s->bytestream);
  532. }
  533. break;
  534. case MKTAG('t', 'R', 'N', 'S'):
  535. {
  536. int v, i;
  537. /* read the transparency. XXX: Only palette mode supported */
  538. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  539. length > 256 ||
  540. !(s->state & PNG_PLTE))
  541. goto skip_tag;
  542. for(i=0;i<length;i++) {
  543. v = *s->bytestream++;
  544. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  545. }
  546. crc = bytestream_get_be32(&s->bytestream);
  547. }
  548. break;
  549. case MKTAG('I', 'E', 'N', 'D'):
  550. if (!(s->state & PNG_ALLIMAGE))
  551. goto fail;
  552. crc = bytestream_get_be32(&s->bytestream);
  553. goto exit_loop;
  554. default:
  555. /* skip tag */
  556. skip_tag:
  557. s->bytestream += length + 4;
  558. break;
  559. }
  560. }
  561. exit_loop:
  562. /* handle p-frames only if a predecessor frame is available */
  563. if(s->last_picture->data[0] != NULL) {
  564. if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
  565. int i, j;
  566. uint8_t *pd = s->current_picture->data[0];
  567. uint8_t *pd_last = s->last_picture->data[0];
  568. for(j=0; j < s->height; j++) {
  569. for(i=0; i < s->width * s->bpp; i++) {
  570. pd[i] += pd_last[i];
  571. }
  572. pd += s->image_linesize;
  573. pd_last += s->image_linesize;
  574. }
  575. }
  576. }
  577. *picture= *s->current_picture;
  578. *data_size = sizeof(AVFrame);
  579. ret = s->bytestream - s->bytestream_start;
  580. the_end:
  581. inflateEnd(&s->zstream);
  582. av_free(crow_buf_base);
  583. s->crow_buf = NULL;
  584. av_freep(&s->last_row);
  585. av_freep(&s->tmp_row);
  586. return ret;
  587. fail:
  588. ret = -1;
  589. goto the_end;
  590. }
  591. static av_cold int png_dec_init(AVCodecContext *avctx){
  592. PNGDecContext *s = avctx->priv_data;
  593. s->current_picture = &s->picture1;
  594. s->last_picture = &s->picture2;
  595. avcodec_get_frame_defaults(&s->picture1);
  596. avcodec_get_frame_defaults(&s->picture2);
  597. dsputil_init(&s->dsp, avctx);
  598. return 0;
  599. }
  600. static av_cold int png_dec_end(AVCodecContext *avctx)
  601. {
  602. PNGDecContext *s = avctx->priv_data;
  603. if (s->picture1.data[0])
  604. avctx->release_buffer(avctx, &s->picture1);
  605. if (s->picture2.data[0])
  606. avctx->release_buffer(avctx, &s->picture2);
  607. return 0;
  608. }
  609. AVCodec png_decoder = {
  610. "png",
  611. AVMEDIA_TYPE_VIDEO,
  612. CODEC_ID_PNG,
  613. sizeof(PNGDecContext),
  614. png_dec_init,
  615. NULL,
  616. png_dec_end,
  617. decode_frame,
  618. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  619. NULL,
  620. .max_lowres = 5,
  621. .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
  622. };