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.

694 lines
24KB

  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/avstring.h"
  22. #include "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "internal.h"
  26. #include "png.h"
  27. #include "pngdsp.h"
  28. /* TODO:
  29. * - add 2, 4 and 16 bit depth support
  30. */
  31. #include <zlib.h>
  32. typedef struct PNGDecContext {
  33. PNGDSPContext dsp;
  34. GetByteContext gb;
  35. AVFrame *prev;
  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,
  124. 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. { \
  148. r = dst[0]; \
  149. if (bpp >= 2) \
  150. g = dst[1]; \
  151. if (bpp >= 3) \
  152. b = dst[2]; \
  153. if (bpp >= 4) \
  154. a = dst[3]; \
  155. for (; i < size; i += bpp) { \
  156. dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
  157. if (bpp == 1) \
  158. continue; \
  159. dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
  160. if (bpp == 2) \
  161. continue; \
  162. dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
  163. if (bpp == 3) \
  164. continue; \
  165. dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
  166. } \
  167. }
  168. #define UNROLL_FILTER(op) \
  169. if (bpp == 1) { \
  170. UNROLL1(1, op) \
  171. } else if (bpp == 2) { \
  172. UNROLL1(2, op) \
  173. } else if (bpp == 3) { \
  174. UNROLL1(3, op) \
  175. } else if (bpp == 4) { \
  176. UNROLL1(4, op) \
  177. } else { \
  178. for (; i < size; i += bpp) { \
  179. int j; \
  180. for (j = 0; j < bpp; j++) \
  181. dst[i + j] = op(dst[i + j - bpp], src[i + j], last[i + j]); \
  182. } \
  183. }
  184. /* NOTE: 'dst' can be equal to 'last' */
  185. static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
  186. uint8_t *src, uint8_t *last, int size, int bpp)
  187. {
  188. int i, p, r, g, b, a;
  189. switch (filter_type) {
  190. case PNG_FILTER_VALUE_NONE:
  191. memcpy(dst, src, size);
  192. break;
  193. case PNG_FILTER_VALUE_SUB:
  194. for (i = 0; i < bpp; i++)
  195. dst[i] = src[i];
  196. if (bpp == 4) {
  197. p = *(int *)dst;
  198. for (; i < size; i += bpp) {
  199. int s = *(int *)(src + i);
  200. p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
  201. *(int *)(dst + i) = p;
  202. }
  203. } else {
  204. #define OP_SUB(x, s, l) x + s
  205. UNROLL_FILTER(OP_SUB);
  206. }
  207. break;
  208. case PNG_FILTER_VALUE_UP:
  209. dsp->add_bytes_l2(dst, src, last, size);
  210. break;
  211. case PNG_FILTER_VALUE_AVG:
  212. for (i = 0; i < bpp; i++) {
  213. p = (last[i] >> 1);
  214. dst[i] = p + src[i];
  215. }
  216. #define OP_AVG(x, s, l) (((x + l) >> 1) + s) & 0xff
  217. UNROLL_FILTER(OP_AVG);
  218. break;
  219. case PNG_FILTER_VALUE_PAETH:
  220. for (i = 0; i < bpp; i++) {
  221. p = last[i];
  222. dst[i] = p + src[i];
  223. }
  224. if (bpp > 2 && size > 4) {
  225. /* would write off the end of the array if we let it process
  226. * the last pixel with bpp=3 */
  227. int w = bpp == 4 ? size : size - 3;
  228. dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
  229. i = w;
  230. }
  231. ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
  232. break;
  233. }
  234. }
  235. static av_always_inline void convert_to_rgb32_loco(uint8_t *dst,
  236. const uint8_t *src,
  237. int width, int loco)
  238. {
  239. int j;
  240. unsigned int r, g, b, a;
  241. for (j = 0; j < width; j++) {
  242. r = src[0];
  243. g = src[1];
  244. b = src[2];
  245. a = src[3];
  246. if (loco) {
  247. r = (r + g) & 0xff;
  248. b = (b + g) & 0xff;
  249. }
  250. *(uint32_t *) dst = (a << 24) | (r << 16) | (g << 8) | b;
  251. dst += 4;
  252. src += 4;
  253. }
  254. }
  255. static void convert_to_rgb32(uint8_t *dst, const uint8_t *src,
  256. int width, int loco)
  257. {
  258. if (loco)
  259. convert_to_rgb32_loco(dst, src, width, 1);
  260. else
  261. convert_to_rgb32_loco(dst, src, width, 0);
  262. }
  263. static void deloco_rgb24(uint8_t *dst, int size)
  264. {
  265. int i;
  266. for (i = 0; i < size; i += 3) {
  267. int g = dst[i + 1];
  268. dst[i + 0] += g;
  269. dst[i + 2] += g;
  270. }
  271. }
  272. /* process exactly one decompressed row */
  273. static void png_handle_row(PNGDecContext *s)
  274. {
  275. uint8_t *ptr, *last_row;
  276. int got_line;
  277. if (!s->interlace_type) {
  278. ptr = s->image_buf + s->image_linesize * s->y;
  279. /* need to swap bytes correctly for RGB_ALPHA */
  280. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  281. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  282. s->last_row, s->row_size, s->bpp);
  283. convert_to_rgb32(ptr, s->tmp_row, s->width,
  284. s->filter_type == PNG_FILTER_TYPE_LOCO);
  285. FFSWAP(uint8_t *, s->last_row, s->tmp_row);
  286. } else {
  287. /* in normal case, we avoid one copy */
  288. if (s->y == 0)
  289. last_row = s->last_row;
  290. else
  291. last_row = ptr - s->image_linesize;
  292. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  293. last_row, s->row_size, s->bpp);
  294. }
  295. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  296. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  297. s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
  298. deloco_rgb24(ptr - s->image_linesize, s->row_size);
  299. s->y++;
  300. if (s->y == s->height) {
  301. s->state |= PNG_ALLIMAGE;
  302. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  303. s->color_type == PNG_COLOR_TYPE_RGB)
  304. deloco_rgb24(ptr, s->row_size);
  305. }
  306. } else {
  307. got_line = 0;
  308. for (;;) {
  309. ptr = s->image_buf + s->image_linesize * s->y;
  310. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  311. /* if we already read one row, it is time to stop to
  312. * wait for the next one */
  313. if (got_line)
  314. break;
  315. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  316. s->last_row, s->pass_row_size, s->bpp);
  317. FFSWAP(uint8_t *, s->last_row, s->tmp_row);
  318. got_line = 1;
  319. }
  320. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  321. /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
  322. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  323. s->color_type, s->last_row);
  324. }
  325. s->y++;
  326. if (s->y == s->height) {
  327. for (;;) {
  328. if (s->pass == NB_PASSES - 1) {
  329. s->state |= PNG_ALLIMAGE;
  330. goto the_end;
  331. } else {
  332. s->pass++;
  333. s->y = 0;
  334. s->pass_row_size = ff_png_pass_row_size(s->pass,
  335. s->bits_per_pixel,
  336. s->width);
  337. s->crow_size = s->pass_row_size + 1;
  338. if (s->pass_row_size != 0)
  339. break;
  340. /* skip pass if empty row */
  341. }
  342. }
  343. }
  344. }
  345. the_end:;
  346. }
  347. }
  348. static int png_decode_idat(PNGDecContext *s, int length)
  349. {
  350. int ret;
  351. s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
  352. s->zstream.next_in = s->gb.buffer;
  353. bytestream2_skip(&s->gb, length);
  354. /* decode one line if possible */
  355. while (s->zstream.avail_in > 0) {
  356. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  357. if (ret != Z_OK && ret != Z_STREAM_END) {
  358. return -1;
  359. }
  360. if (s->zstream.avail_out == 0) {
  361. if (!(s->state & PNG_ALLIMAGE)) {
  362. png_handle_row(s);
  363. }
  364. s->zstream.avail_out = s->crow_size;
  365. s->zstream.next_out = s->crow_buf;
  366. }
  367. if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
  368. av_log(NULL, AV_LOG_WARNING,
  369. "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
  370. return 0;
  371. }
  372. }
  373. return 0;
  374. }
  375. static int decode_frame(AVCodecContext *avctx,
  376. void *data, int *got_frame,
  377. AVPacket *avpkt)
  378. {
  379. PNGDecContext *const s = avctx->priv_data;
  380. const uint8_t *buf = avpkt->data;
  381. int buf_size = avpkt->size;
  382. AVFrame *p = data;
  383. uint8_t *crow_buf_base = NULL;
  384. uint32_t tag, length;
  385. int ret;
  386. /* check signature */
  387. if (buf_size < 8) {
  388. av_log(avctx, AV_LOG_ERROR, "Not enough data %d\n",
  389. buf_size);
  390. return AVERROR_INVALIDDATA;
  391. }
  392. if (memcmp(buf, ff_pngsig, 8) != 0 &&
  393. memcmp(buf, ff_mngsig, 8) != 0) {
  394. char signature[5 * 8 + 1] = { 0 };
  395. int i;
  396. for (i = 0; i < 8; i++) {
  397. av_strlcatf(signature + i * 5, sizeof(signature) - i * 5,
  398. " 0x%02x", buf[i]);
  399. }
  400. av_log(avctx, AV_LOG_ERROR, "Invalid PNG signature %s\n",
  401. signature);
  402. return AVERROR_INVALIDDATA;
  403. }
  404. bytestream2_init(&s->gb, buf + 8, buf_size - 8);
  405. s->y = s->state = 0;
  406. /* init the zlib */
  407. s->zstream.zalloc = ff_png_zalloc;
  408. s->zstream.zfree = ff_png_zfree;
  409. s->zstream.opaque = NULL;
  410. ret = inflateInit(&s->zstream);
  411. if (ret != Z_OK)
  412. return -1;
  413. for (;;) {
  414. if (bytestream2_get_bytes_left(&s->gb) <= 0)
  415. goto fail;
  416. length = bytestream2_get_be32(&s->gb);
  417. if (length > 0x7fffffff)
  418. goto fail;
  419. tag = bytestream2_get_le32(&s->gb);
  420. ff_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
  421. (tag & 0xff),
  422. ((tag >> 8) & 0xff),
  423. ((tag >> 16) & 0xff),
  424. ((tag >> 24) & 0xff), length);
  425. switch (tag) {
  426. case MKTAG('I', 'H', 'D', 'R'):
  427. if (length != 13)
  428. goto fail;
  429. s->width = bytestream2_get_be32(&s->gb);
  430. s->height = bytestream2_get_be32(&s->gb);
  431. if (av_image_check_size(s->width, s->height, 0, avctx)) {
  432. s->width = s->height = 0;
  433. goto fail;
  434. }
  435. s->bit_depth = bytestream2_get_byte(&s->gb);
  436. s->color_type = bytestream2_get_byte(&s->gb);
  437. s->compression_type = bytestream2_get_byte(&s->gb);
  438. s->filter_type = bytestream2_get_byte(&s->gb);
  439. s->interlace_type = bytestream2_get_byte(&s->gb);
  440. bytestream2_skip(&s->gb, 4); /* crc */
  441. s->state |= PNG_IHDR;
  442. ff_dlog(avctx, "width=%d height=%d depth=%d color_type=%d "
  443. "compression_type=%d filter_type=%d interlace_type=%d\n",
  444. s->width, s->height, s->bit_depth, s->color_type,
  445. s->compression_type, s->filter_type, s->interlace_type);
  446. break;
  447. case MKTAG('I', 'D', 'A', 'T'):
  448. if (!(s->state & PNG_IHDR))
  449. goto fail;
  450. if (!(s->state & PNG_IDAT)) {
  451. /* init image info */
  452. avctx->width = s->width;
  453. avctx->height = s->height;
  454. s->channels = ff_png_get_nb_channels(s->color_type);
  455. s->bits_per_pixel = s->bit_depth * s->channels;
  456. s->bpp = (s->bits_per_pixel + 7) >> 3;
  457. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  458. if (s->bit_depth == 8 &&
  459. s->color_type == PNG_COLOR_TYPE_RGB) {
  460. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  461. } else if (s->bit_depth == 8 &&
  462. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  463. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  464. } else if (s->bit_depth == 8 &&
  465. s->color_type == PNG_COLOR_TYPE_GRAY) {
  466. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  467. } else if (s->bit_depth == 16 &&
  468. s->color_type == PNG_COLOR_TYPE_GRAY) {
  469. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  470. } else if (s->bit_depth == 16 &&
  471. s->color_type == PNG_COLOR_TYPE_RGB) {
  472. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  473. } else if (s->bit_depth == 1 &&
  474. s->color_type == PNG_COLOR_TYPE_GRAY) {
  475. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  476. } else if (s->bit_depth == 8 &&
  477. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  478. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  479. } else if (s->bit_depth == 8 &&
  480. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  481. avctx->pix_fmt = AV_PIX_FMT_YA8;
  482. } else if (s->bit_depth == 16 &&
  483. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  484. avctx->pix_fmt = AV_PIX_FMT_YA16BE;
  485. } else {
  486. goto fail;
  487. }
  488. if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0) {
  489. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  490. goto fail;
  491. }
  492. p->pict_type = AV_PICTURE_TYPE_I;
  493. p->key_frame = 1;
  494. p->interlaced_frame = !!s->interlace_type;
  495. /* compute the compressed row size */
  496. if (!s->interlace_type) {
  497. s->crow_size = s->row_size + 1;
  498. } else {
  499. s->pass = 0;
  500. s->pass_row_size = ff_png_pass_row_size(s->pass,
  501. s->bits_per_pixel,
  502. s->width);
  503. s->crow_size = s->pass_row_size + 1;
  504. }
  505. ff_dlog(avctx, "row_size=%d crow_size =%d\n",
  506. s->row_size, s->crow_size);
  507. s->image_buf = p->data[0];
  508. s->image_linesize = p->linesize[0];
  509. /* copy the palette if needed */
  510. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  511. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  512. /* empty row is used if differencing to the first row */
  513. s->last_row = av_mallocz(s->row_size);
  514. if (!s->last_row)
  515. goto fail;
  516. if (s->interlace_type ||
  517. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  518. s->tmp_row = av_malloc(s->row_size);
  519. if (!s->tmp_row)
  520. goto fail;
  521. }
  522. /* compressed row */
  523. crow_buf_base = av_malloc(s->row_size + 16);
  524. if (!crow_buf_base)
  525. goto fail;
  526. /* we want crow_buf+1 to be 16-byte aligned */
  527. s->crow_buf = crow_buf_base + 15;
  528. s->zstream.avail_out = s->crow_size;
  529. s->zstream.next_out = s->crow_buf;
  530. }
  531. s->state |= PNG_IDAT;
  532. if (png_decode_idat(s, length) < 0)
  533. goto fail;
  534. bytestream2_skip(&s->gb, 4); /* crc */
  535. break;
  536. case MKTAG('P', 'L', 'T', 'E'):
  537. {
  538. int n, i, r, g, b;
  539. if ((length % 3) != 0 || length > 256 * 3)
  540. goto skip_tag;
  541. /* read the palette */
  542. n = length / 3;
  543. for (i = 0; i < n; i++) {
  544. r = bytestream2_get_byte(&s->gb);
  545. g = bytestream2_get_byte(&s->gb);
  546. b = bytestream2_get_byte(&s->gb);
  547. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  548. }
  549. for (; i < 256; i++)
  550. s->palette[i] = (0xff << 24);
  551. s->state |= PNG_PLTE;
  552. bytestream2_skip(&s->gb, 4); /* crc */
  553. }
  554. break;
  555. case MKTAG('t', 'R', 'N', 'S'):
  556. {
  557. int v, i;
  558. /* read the transparency. XXX: Only palette mode supported */
  559. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  560. length > 256 ||
  561. !(s->state & PNG_PLTE))
  562. goto skip_tag;
  563. for (i = 0; i < length; i++) {
  564. v = bytestream2_get_byte(&s->gb);
  565. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  566. }
  567. bytestream2_skip(&s->gb, 4); /* crc */
  568. }
  569. break;
  570. case MKTAG('I', 'E', 'N', 'D'):
  571. if (!(s->state & PNG_ALLIMAGE))
  572. goto fail;
  573. bytestream2_skip(&s->gb, 4); /* crc */
  574. goto exit_loop;
  575. default:
  576. /* skip tag */
  577. skip_tag:
  578. bytestream2_skip(&s->gb, length + 4);
  579. break;
  580. }
  581. }
  582. exit_loop:
  583. /* handle p-frames only if a predecessor frame is available */
  584. if (s->prev->data[0]) {
  585. if (!(avpkt->flags & AV_PKT_FLAG_KEY)) {
  586. int i, j;
  587. uint8_t *pd = p->data[0];
  588. uint8_t *pd_last = s->prev->data[0];
  589. for (j = 0; j < s->height; j++) {
  590. for (i = 0; i < s->width * s->bpp; i++)
  591. pd[i] += pd_last[i];
  592. pd += s->image_linesize;
  593. pd_last += s->image_linesize;
  594. }
  595. }
  596. }
  597. av_frame_unref(s->prev);
  598. if ((ret = av_frame_ref(s->prev, p)) < 0)
  599. goto fail;
  600. *got_frame = 1;
  601. ret = bytestream2_tell(&s->gb);
  602. the_end:
  603. inflateEnd(&s->zstream);
  604. av_free(crow_buf_base);
  605. s->crow_buf = NULL;
  606. av_freep(&s->last_row);
  607. av_freep(&s->tmp_row);
  608. return ret;
  609. fail:
  610. ret = -1;
  611. goto the_end;
  612. }
  613. static av_cold int png_dec_init(AVCodecContext *avctx)
  614. {
  615. PNGDecContext *s = avctx->priv_data;
  616. avctx->color_range = AVCOL_RANGE_JPEG;
  617. s->prev = av_frame_alloc();
  618. if (!s->prev)
  619. return AVERROR(ENOMEM);
  620. ff_pngdsp_init(&s->dsp);
  621. return 0;
  622. }
  623. static av_cold int png_dec_end(AVCodecContext *avctx)
  624. {
  625. PNGDecContext *s = avctx->priv_data;
  626. av_frame_free(&s->prev);
  627. return 0;
  628. }
  629. AVCodec ff_png_decoder = {
  630. .name = "png",
  631. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  632. .type = AVMEDIA_TYPE_VIDEO,
  633. .id = AV_CODEC_ID_PNG,
  634. .priv_data_size = sizeof(PNGDecContext),
  635. .init = png_dec_init,
  636. .close = png_dec_end,
  637. .decode = decode_frame,
  638. .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/,
  639. };