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.

712 lines
25KB

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