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.

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