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.

662 lines
21KB

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