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.

1310 lines
44KB

  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/bprint.h"
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "apng.h"
  28. #include "png.h"
  29. #include "pngdsp.h"
  30. #include "thread.h"
  31. #include <zlib.h>
  32. typedef struct PNGDecContext {
  33. PNGDSPContext dsp;
  34. AVCodecContext *avctx;
  35. GetByteContext gb;
  36. ThreadFrame previous_picture;
  37. ThreadFrame last_picture;
  38. ThreadFrame picture;
  39. int state;
  40. int width, height;
  41. int cur_w, cur_h;
  42. int x_offset, y_offset;
  43. uint8_t dispose_op, blend_op;
  44. int bit_depth;
  45. int color_type;
  46. int compression_type;
  47. int interlace_type;
  48. int filter_type;
  49. int channels;
  50. int bits_per_pixel;
  51. int bpp;
  52. int frame_id;
  53. uint8_t *image_buf;
  54. int image_linesize;
  55. uint32_t palette[256];
  56. uint8_t *crow_buf;
  57. uint8_t *last_row;
  58. unsigned int last_row_size;
  59. uint8_t *tmp_row;
  60. unsigned int tmp_row_size;
  61. uint8_t *buffer;
  62. int buffer_size;
  63. int pass;
  64. int crow_size; /* compressed row size (include filter type) */
  65. int row_size; /* decompressed row size */
  66. int pass_row_size; /* decompress row size of the current pass */
  67. int y;
  68. z_stream zstream;
  69. } PNGDecContext;
  70. /* Mask to determine which pixels are valid in a pass */
  71. static const uint8_t png_pass_mask[NB_PASSES] = {
  72. 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
  73. };
  74. /* Mask to determine which y pixels can be written in a pass */
  75. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  76. 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  77. };
  78. /* Mask to determine which pixels to overwrite while displaying */
  79. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  80. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  81. };
  82. /* NOTE: we try to construct a good looking image at each pass. width
  83. * is the original image width. We also do pixel format conversion at
  84. * this stage */
  85. static void png_put_interlaced_row(uint8_t *dst, int width,
  86. int bits_per_pixel, int pass,
  87. int color_type, const uint8_t *src)
  88. {
  89. int x, mask, dsp_mask, j, src_x, b, bpp;
  90. uint8_t *d;
  91. const uint8_t *s;
  92. mask = png_pass_mask[pass];
  93. dsp_mask = png_pass_dsp_mask[pass];
  94. switch (bits_per_pixel) {
  95. case 1:
  96. src_x = 0;
  97. for (x = 0; x < width; x++) {
  98. j = (x & 7);
  99. if ((dsp_mask << j) & 0x80) {
  100. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  101. dst[x >> 3] &= 0xFF7F>>j;
  102. dst[x >> 3] |= b << (7 - j);
  103. }
  104. if ((mask << j) & 0x80)
  105. src_x++;
  106. }
  107. break;
  108. case 2:
  109. src_x = 0;
  110. for (x = 0; x < width; x++) {
  111. int j2 = 2 * (x & 3);
  112. j = (x & 7);
  113. if ((dsp_mask << j) & 0x80) {
  114. b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
  115. dst[x >> 2] &= 0xFF3F>>j2;
  116. dst[x >> 2] |= b << (6 - j2);
  117. }
  118. if ((mask << j) & 0x80)
  119. src_x++;
  120. }
  121. break;
  122. case 4:
  123. src_x = 0;
  124. for (x = 0; x < width; x++) {
  125. int j2 = 4*(x&1);
  126. j = (x & 7);
  127. if ((dsp_mask << j) & 0x80) {
  128. b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
  129. dst[x >> 1] &= 0xFF0F>>j2;
  130. dst[x >> 1] |= b << (4 - j2);
  131. }
  132. if ((mask << j) & 0x80)
  133. src_x++;
  134. }
  135. break;
  136. default:
  137. bpp = bits_per_pixel >> 3;
  138. d = dst;
  139. s = src;
  140. for (x = 0; x < width; x++) {
  141. j = x & 7;
  142. if ((dsp_mask << j) & 0x80) {
  143. memcpy(d, s, bpp);
  144. }
  145. d += bpp;
  146. if ((mask << j) & 0x80)
  147. s += bpp;
  148. }
  149. break;
  150. }
  151. }
  152. void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top,
  153. int w, int bpp)
  154. {
  155. int i;
  156. for (i = 0; i < w; i++) {
  157. int a, b, c, p, pa, pb, pc;
  158. a = dst[i - bpp];
  159. b = top[i];
  160. c = top[i - bpp];
  161. p = b - c;
  162. pc = a - c;
  163. pa = abs(p);
  164. pb = abs(pc);
  165. pc = abs(p + pc);
  166. if (pa <= pb && pa <= pc)
  167. p = a;
  168. else if (pb <= pc)
  169. p = b;
  170. else
  171. p = c;
  172. dst[i] = p + src[i];
  173. }
  174. }
  175. #define UNROLL1(bpp, op) \
  176. { \
  177. r = dst[0]; \
  178. if (bpp >= 2) \
  179. g = dst[1]; \
  180. if (bpp >= 3) \
  181. b = dst[2]; \
  182. if (bpp >= 4) \
  183. a = dst[3]; \
  184. for (; i <= size - bpp; i += bpp) { \
  185. dst[i + 0] = r = op(r, src[i + 0], last[i + 0]); \
  186. if (bpp == 1) \
  187. continue; \
  188. dst[i + 1] = g = op(g, src[i + 1], last[i + 1]); \
  189. if (bpp == 2) \
  190. continue; \
  191. dst[i + 2] = b = op(b, src[i + 2], last[i + 2]); \
  192. if (bpp == 3) \
  193. continue; \
  194. dst[i + 3] = a = op(a, src[i + 3], last[i + 3]); \
  195. } \
  196. }
  197. #define UNROLL_FILTER(op) \
  198. if (bpp == 1) { \
  199. UNROLL1(1, op) \
  200. } else if (bpp == 2) { \
  201. UNROLL1(2, op) \
  202. } else if (bpp == 3) { \
  203. UNROLL1(3, op) \
  204. } else if (bpp == 4) { \
  205. UNROLL1(4, op) \
  206. } \
  207. for (; i < size; i++) { \
  208. dst[i] = op(dst[i - bpp], src[i], last[i]); \
  209. }
  210. /* NOTE: 'dst' can be equal to 'last' */
  211. static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
  212. uint8_t *src, uint8_t *last, int size, int bpp)
  213. {
  214. int i, p, r, g, b, a;
  215. switch (filter_type) {
  216. case PNG_FILTER_VALUE_NONE:
  217. memcpy(dst, src, size);
  218. break;
  219. case PNG_FILTER_VALUE_SUB:
  220. for (i = 0; i < bpp; i++)
  221. dst[i] = src[i];
  222. if (bpp == 4) {
  223. p = *(int *)dst;
  224. for (; i < size; i += bpp) {
  225. unsigned s = *(int *)(src + i);
  226. p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
  227. *(int *)(dst + i) = p;
  228. }
  229. } else {
  230. #define OP_SUB(x, s, l) ((x) + (s))
  231. UNROLL_FILTER(OP_SUB);
  232. }
  233. break;
  234. case PNG_FILTER_VALUE_UP:
  235. dsp->add_bytes_l2(dst, src, last, size);
  236. break;
  237. case PNG_FILTER_VALUE_AVG:
  238. for (i = 0; i < bpp; i++) {
  239. p = (last[i] >> 1);
  240. dst[i] = p + src[i];
  241. }
  242. #define OP_AVG(x, s, l) (((((x) + (l)) >> 1) + (s)) & 0xff)
  243. UNROLL_FILTER(OP_AVG);
  244. break;
  245. case PNG_FILTER_VALUE_PAETH:
  246. for (i = 0; i < bpp; i++) {
  247. p = last[i];
  248. dst[i] = p + src[i];
  249. }
  250. if (bpp > 2 && size > 4) {
  251. /* would write off the end of the array if we let it process
  252. * the last pixel with bpp=3 */
  253. int w = bpp == 4 ? size : size - 3;
  254. if (w > i) {
  255. dsp->add_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
  256. i = w;
  257. }
  258. }
  259. ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
  260. break;
  261. }
  262. }
  263. /* This used to be called "deloco" in FFmpeg
  264. * and is actually an inverse reversible colorspace transformation */
  265. #define YUV2RGB(NAME, TYPE) \
  266. static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
  267. { \
  268. int i; \
  269. for (i = 0; i < size; i += 3 + alpha) { \
  270. int g = dst [i + 1]; \
  271. dst[i + 0] += g; \
  272. dst[i + 2] += g; \
  273. } \
  274. }
  275. YUV2RGB(rgb8, uint8_t)
  276. YUV2RGB(rgb16, uint16_t)
  277. /* process exactly one decompressed row */
  278. static void png_handle_row(PNGDecContext *s)
  279. {
  280. uint8_t *ptr, *last_row;
  281. int got_line;
  282. if (!s->interlace_type) {
  283. ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
  284. if (s->y == 0)
  285. last_row = s->last_row;
  286. else
  287. last_row = ptr - s->image_linesize;
  288. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  289. last_row, s->row_size, s->bpp);
  290. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  291. if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
  292. if (s->bit_depth == 16) {
  293. deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
  294. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  295. } else {
  296. deloco_rgb8(ptr - s->image_linesize, s->row_size,
  297. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  298. }
  299. }
  300. s->y++;
  301. if (s->y == s->cur_h) {
  302. s->state |= PNG_ALLIMAGE;
  303. if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
  304. if (s->bit_depth == 16) {
  305. deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
  306. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  307. } else {
  308. deloco_rgb8(ptr, s->row_size,
  309. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  310. }
  311. }
  312. }
  313. } else {
  314. got_line = 0;
  315. for (;;) {
  316. ptr = s->image_buf + s->image_linesize * (s->y + s->y_offset) + s->x_offset * s->bpp;
  317. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  318. /* if we already read one row, it is time to stop to
  319. * wait for the next one */
  320. if (got_line)
  321. break;
  322. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  323. s->last_row, s->pass_row_size, s->bpp);
  324. FFSWAP(uint8_t *, s->last_row, s->tmp_row);
  325. FFSWAP(unsigned int, s->last_row_size, s->tmp_row_size);
  326. got_line = 1;
  327. }
  328. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  329. png_put_interlaced_row(ptr, s->cur_w, s->bits_per_pixel, s->pass,
  330. s->color_type, s->last_row);
  331. }
  332. s->y++;
  333. if (s->y == s->cur_h) {
  334. memset(s->last_row, 0, s->row_size);
  335. for (;;) {
  336. if (s->pass == NB_PASSES - 1) {
  337. s->state |= PNG_ALLIMAGE;
  338. goto the_end;
  339. } else {
  340. s->pass++;
  341. s->y = 0;
  342. s->pass_row_size = ff_png_pass_row_size(s->pass,
  343. s->bits_per_pixel,
  344. s->cur_w);
  345. s->crow_size = s->pass_row_size + 1;
  346. if (s->pass_row_size != 0)
  347. break;
  348. /* skip pass if empty row */
  349. }
  350. }
  351. }
  352. }
  353. the_end:;
  354. }
  355. }
  356. static int png_decode_idat(PNGDecContext *s, int length)
  357. {
  358. int ret;
  359. s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
  360. s->zstream.next_in = (unsigned char *)s->gb.buffer;
  361. bytestream2_skip(&s->gb, length);
  362. /* decode one line if possible */
  363. while (s->zstream.avail_in > 0) {
  364. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  365. if (ret != Z_OK && ret != Z_STREAM_END) {
  366. av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
  367. return AVERROR_EXTERNAL;
  368. }
  369. if (s->zstream.avail_out == 0) {
  370. if (!(s->state & PNG_ALLIMAGE)) {
  371. png_handle_row(s);
  372. }
  373. s->zstream.avail_out = s->crow_size;
  374. s->zstream.next_out = s->crow_buf;
  375. }
  376. if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
  377. av_log(NULL, AV_LOG_WARNING,
  378. "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
  379. return 0;
  380. }
  381. }
  382. return 0;
  383. }
  384. static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
  385. const uint8_t *data_end)
  386. {
  387. z_stream zstream;
  388. unsigned char *buf;
  389. unsigned buf_size;
  390. int ret;
  391. zstream.zalloc = ff_png_zalloc;
  392. zstream.zfree = ff_png_zfree;
  393. zstream.opaque = NULL;
  394. if (inflateInit(&zstream) != Z_OK)
  395. return AVERROR_EXTERNAL;
  396. zstream.next_in = (unsigned char *)data;
  397. zstream.avail_in = data_end - data;
  398. av_bprint_init(bp, 0, -1);
  399. while (zstream.avail_in > 0) {
  400. av_bprint_get_buffer(bp, 1, &buf, &buf_size);
  401. if (!buf_size) {
  402. ret = AVERROR(ENOMEM);
  403. goto fail;
  404. }
  405. zstream.next_out = buf;
  406. zstream.avail_out = buf_size;
  407. ret = inflate(&zstream, Z_PARTIAL_FLUSH);
  408. if (ret != Z_OK && ret != Z_STREAM_END) {
  409. ret = AVERROR_EXTERNAL;
  410. goto fail;
  411. }
  412. bp->len += zstream.next_out - buf;
  413. if (ret == Z_STREAM_END)
  414. break;
  415. }
  416. inflateEnd(&zstream);
  417. bp->str[bp->len] = 0;
  418. return 0;
  419. fail:
  420. inflateEnd(&zstream);
  421. av_bprint_finalize(bp, NULL);
  422. return ret;
  423. }
  424. static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
  425. {
  426. size_t extra = 0, i;
  427. uint8_t *out, *q;
  428. for (i = 0; i < size_in; i++)
  429. extra += in[i] >= 0x80;
  430. if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
  431. return NULL;
  432. q = out = av_malloc(size_in + extra + 1);
  433. if (!out)
  434. return NULL;
  435. for (i = 0; i < size_in; i++) {
  436. if (in[i] >= 0x80) {
  437. *(q++) = 0xC0 | (in[i] >> 6);
  438. *(q++) = 0x80 | (in[i] & 0x3F);
  439. } else {
  440. *(q++) = in[i];
  441. }
  442. }
  443. *(q++) = 0;
  444. return out;
  445. }
  446. static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
  447. AVDictionary **dict)
  448. {
  449. int ret, method;
  450. const uint8_t *data = s->gb.buffer;
  451. const uint8_t *data_end = data + length;
  452. const uint8_t *keyword = data;
  453. const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
  454. uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
  455. unsigned text_len;
  456. AVBPrint bp;
  457. if (!keyword_end)
  458. return AVERROR_INVALIDDATA;
  459. data = keyword_end + 1;
  460. if (compressed) {
  461. if (data == data_end)
  462. return AVERROR_INVALIDDATA;
  463. method = *(data++);
  464. if (method)
  465. return AVERROR_INVALIDDATA;
  466. if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
  467. return ret;
  468. text_len = bp.len;
  469. av_bprint_finalize(&bp, (char **)&text);
  470. if (!text)
  471. return AVERROR(ENOMEM);
  472. } else {
  473. text = (uint8_t *)data;
  474. text_len = data_end - text;
  475. }
  476. kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
  477. txt_utf8 = iso88591_to_utf8(text, text_len);
  478. if (text != data)
  479. av_free(text);
  480. if (!(kw_utf8 && txt_utf8)) {
  481. av_free(kw_utf8);
  482. av_free(txt_utf8);
  483. return AVERROR(ENOMEM);
  484. }
  485. av_dict_set(dict, kw_utf8, txt_utf8,
  486. AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  487. return 0;
  488. }
  489. static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
  490. uint32_t length)
  491. {
  492. if (length != 13)
  493. return AVERROR_INVALIDDATA;
  494. if (s->state & PNG_IDAT) {
  495. av_log(avctx, AV_LOG_ERROR, "IHDR after IDAT\n");
  496. return AVERROR_INVALIDDATA;
  497. }
  498. s->width = s->cur_w = bytestream2_get_be32(&s->gb);
  499. s->height = s->cur_h = bytestream2_get_be32(&s->gb);
  500. if (av_image_check_size(s->width, s->height, 0, avctx)) {
  501. s->width = s->height = 0;
  502. av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
  503. return AVERROR_INVALIDDATA;
  504. }
  505. s->bit_depth = bytestream2_get_byte(&s->gb);
  506. s->color_type = bytestream2_get_byte(&s->gb);
  507. s->compression_type = bytestream2_get_byte(&s->gb);
  508. s->filter_type = bytestream2_get_byte(&s->gb);
  509. s->interlace_type = bytestream2_get_byte(&s->gb);
  510. bytestream2_skip(&s->gb, 4); /* crc */
  511. s->state |= PNG_IHDR;
  512. if (avctx->debug & FF_DEBUG_PICT_INFO)
  513. av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
  514. "compression_type=%d filter_type=%d interlace_type=%d\n",
  515. s->width, s->height, s->bit_depth, s->color_type,
  516. s->compression_type, s->filter_type, s->interlace_type);
  517. return 0;
  518. }
  519. static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
  520. {
  521. if (s->state & PNG_IDAT) {
  522. av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
  523. return AVERROR_INVALIDDATA;
  524. }
  525. avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
  526. avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
  527. if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
  528. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  529. bytestream2_skip(&s->gb, 1); /* unit specifier */
  530. bytestream2_skip(&s->gb, 4); /* crc */
  531. return 0;
  532. }
  533. static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
  534. uint32_t length, AVFrame *p)
  535. {
  536. int ret;
  537. if (!(s->state & PNG_IHDR)) {
  538. av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
  539. return AVERROR_INVALIDDATA;
  540. }
  541. if (!(s->state & PNG_IDAT)) {
  542. /* init image info */
  543. avctx->width = s->width;
  544. avctx->height = s->height;
  545. s->channels = ff_png_get_nb_channels(s->color_type);
  546. s->bits_per_pixel = s->bit_depth * s->channels;
  547. s->bpp = (s->bits_per_pixel + 7) >> 3;
  548. s->row_size = (s->cur_w * s->bits_per_pixel + 7) >> 3;
  549. if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  550. s->color_type == PNG_COLOR_TYPE_RGB) {
  551. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  552. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  553. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  554. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  555. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  556. s->color_type == PNG_COLOR_TYPE_GRAY) {
  557. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  558. } else if (s->bit_depth == 16 &&
  559. s->color_type == PNG_COLOR_TYPE_GRAY) {
  560. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  561. } else if (s->bit_depth == 16 &&
  562. s->color_type == PNG_COLOR_TYPE_RGB) {
  563. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  564. } else if (s->bit_depth == 16 &&
  565. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  566. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  567. } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
  568. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  569. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  570. } else if (s->bit_depth == 1 && s->bits_per_pixel == 1) {
  571. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  572. } else if (s->bit_depth == 8 &&
  573. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  574. avctx->pix_fmt = AV_PIX_FMT_YA8;
  575. } else if (s->bit_depth == 16 &&
  576. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  577. avctx->pix_fmt = AV_PIX_FMT_YA16BE;
  578. } else {
  579. av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
  580. "and color type %d\n",
  581. s->bit_depth, s->color_type);
  582. return AVERROR_INVALIDDATA;
  583. }
  584. if ((ret = ff_thread_get_buffer(avctx, &s->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
  585. return ret;
  586. ff_thread_finish_setup(avctx);
  587. p->pict_type = AV_PICTURE_TYPE_I;
  588. p->key_frame = 1;
  589. p->interlaced_frame = !!s->interlace_type;
  590. /* compute the compressed row size */
  591. if (!s->interlace_type) {
  592. s->crow_size = s->row_size + 1;
  593. } else {
  594. s->pass = 0;
  595. s->pass_row_size = ff_png_pass_row_size(s->pass,
  596. s->bits_per_pixel,
  597. s->cur_w);
  598. s->crow_size = s->pass_row_size + 1;
  599. }
  600. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  601. s->row_size, s->crow_size);
  602. s->image_buf = p->data[0];
  603. s->image_linesize = p->linesize[0];
  604. /* copy the palette if needed */
  605. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  606. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  607. /* empty row is used if differencing to the first row */
  608. av_fast_padded_mallocz(&s->last_row, &s->last_row_size, s->row_size);
  609. if (!s->last_row)
  610. return AVERROR_INVALIDDATA;
  611. if (s->interlace_type ||
  612. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  613. av_fast_padded_malloc(&s->tmp_row, &s->tmp_row_size, s->row_size);
  614. if (!s->tmp_row)
  615. return AVERROR_INVALIDDATA;
  616. }
  617. /* compressed row */
  618. av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
  619. if (!s->buffer)
  620. return AVERROR(ENOMEM);
  621. /* we want crow_buf+1 to be 16-byte aligned */
  622. s->crow_buf = s->buffer + 15;
  623. s->zstream.avail_out = s->crow_size;
  624. s->zstream.next_out = s->crow_buf;
  625. }
  626. s->state |= PNG_IDAT;
  627. if ((ret = png_decode_idat(s, length)) < 0)
  628. return ret;
  629. bytestream2_skip(&s->gb, 4); /* crc */
  630. return 0;
  631. }
  632. static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
  633. uint32_t length)
  634. {
  635. int n, i, r, g, b;
  636. if ((length % 3) != 0 || length > 256 * 3)
  637. return AVERROR_INVALIDDATA;
  638. /* read the palette */
  639. n = length / 3;
  640. for (i = 0; i < n; i++) {
  641. r = bytestream2_get_byte(&s->gb);
  642. g = bytestream2_get_byte(&s->gb);
  643. b = bytestream2_get_byte(&s->gb);
  644. s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  645. }
  646. for (; i < 256; i++)
  647. s->palette[i] = (0xFFU << 24);
  648. s->state |= PNG_PLTE;
  649. bytestream2_skip(&s->gb, 4); /* crc */
  650. return 0;
  651. }
  652. static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
  653. uint32_t length)
  654. {
  655. int v, i;
  656. /* read the transparency. XXX: Only palette mode supported */
  657. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  658. length > 256 ||
  659. !(s->state & PNG_PLTE))
  660. return AVERROR_INVALIDDATA;
  661. for (i = 0; i < length; i++) {
  662. v = bytestream2_get_byte(&s->gb);
  663. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  664. }
  665. bytestream2_skip(&s->gb, 4); /* crc */
  666. return 0;
  667. }
  668. static void handle_small_bpp(PNGDecContext *s, AVFrame *p)
  669. {
  670. if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE) {
  671. int i, j, k;
  672. uint8_t *pd = p->data[0];
  673. for (j = 0; j < s->height; j++) {
  674. i = s->width / 8;
  675. for (k = 7; k >= 1; k--)
  676. if ((s->width&7) >= k)
  677. pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
  678. for (i--; i >= 0; i--) {
  679. pd[8*i + 7]= pd[i] & 1;
  680. pd[8*i + 6]= (pd[i]>>1) & 1;
  681. pd[8*i + 5]= (pd[i]>>2) & 1;
  682. pd[8*i + 4]= (pd[i]>>3) & 1;
  683. pd[8*i + 3]= (pd[i]>>4) & 1;
  684. pd[8*i + 2]= (pd[i]>>5) & 1;
  685. pd[8*i + 1]= (pd[i]>>6) & 1;
  686. pd[8*i + 0]= pd[i]>>7;
  687. }
  688. pd += s->image_linesize;
  689. }
  690. } else if (s->bits_per_pixel == 2) {
  691. int i, j;
  692. uint8_t *pd = p->data[0];
  693. for (j = 0; j < s->height; j++) {
  694. i = s->width / 4;
  695. if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  696. if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
  697. if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
  698. if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
  699. for (i--; i >= 0; i--) {
  700. pd[4*i + 3]= pd[i] & 3;
  701. pd[4*i + 2]= (pd[i]>>2) & 3;
  702. pd[4*i + 1]= (pd[i]>>4) & 3;
  703. pd[4*i + 0]= pd[i]>>6;
  704. }
  705. } else {
  706. if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
  707. if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
  708. if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
  709. for (i--; i >= 0; i--) {
  710. pd[4*i + 3]= ( pd[i] & 3)*0x55;
  711. pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
  712. pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
  713. pd[4*i + 0]= ( pd[i]>>6 )*0x55;
  714. }
  715. }
  716. pd += s->image_linesize;
  717. }
  718. } else if (s->bits_per_pixel == 4) {
  719. int i, j;
  720. uint8_t *pd = p->data[0];
  721. for (j = 0; j < s->height; j++) {
  722. i = s->width/2;
  723. if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  724. if (s->width&1) pd[2*i+0]= pd[i]>>4;
  725. for (i--; i >= 0; i--) {
  726. pd[2*i + 1] = pd[i] & 15;
  727. pd[2*i + 0] = pd[i] >> 4;
  728. }
  729. } else {
  730. if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
  731. for (i--; i >= 0; i--) {
  732. pd[2*i + 1] = (pd[i] & 15) * 0x11;
  733. pd[2*i + 0] = (pd[i] >> 4) * 0x11;
  734. }
  735. }
  736. pd += s->image_linesize;
  737. }
  738. }
  739. }
  740. static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
  741. uint32_t length)
  742. {
  743. uint32_t sequence_number;
  744. if (length != 26)
  745. return AVERROR_INVALIDDATA;
  746. sequence_number = bytestream2_get_be32(&s->gb);
  747. s->cur_w = bytestream2_get_be32(&s->gb);
  748. s->cur_h = bytestream2_get_be32(&s->gb);
  749. s->x_offset = bytestream2_get_be32(&s->gb);
  750. s->y_offset = bytestream2_get_be32(&s->gb);
  751. bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
  752. s->dispose_op = bytestream2_get_byte(&s->gb);
  753. s->blend_op = bytestream2_get_byte(&s->gb);
  754. bytestream2_skip(&s->gb, 4); /* crc */
  755. if (sequence_number == 0 &&
  756. (s->cur_w != s->width ||
  757. s->cur_h != s->height ||
  758. s->x_offset != 0 ||
  759. s->y_offset != 0) ||
  760. s->cur_w <= 0 || s->cur_h <= 0 ||
  761. s->x_offset < 0 || s->y_offset < 0 ||
  762. s->cur_w > s->width - s->x_offset|| s->cur_h > s->height - s->y_offset)
  763. return AVERROR_INVALIDDATA;
  764. /* always (re)start with a clean frame */
  765. if (sequence_number == 0) {
  766. s->dispose_op = APNG_DISPOSE_OP_BACKGROUND;
  767. s->frame_id = 0;
  768. } else {
  769. s->frame_id++;
  770. if (s->frame_id == 1 && s->dispose_op == APNG_DISPOSE_OP_PREVIOUS)
  771. /* previous for the second frame is the first frame */
  772. s->dispose_op = APNG_DISPOSE_OP_NONE;
  773. }
  774. return 0;
  775. }
  776. static void handle_p_frame_png(PNGDecContext *s, AVFrame *p)
  777. {
  778. int i, j;
  779. uint8_t *pd = p->data[0];
  780. uint8_t *pd_last = s->last_picture.f->data[0];
  781. int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
  782. ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
  783. for (j = 0; j < s->height; j++) {
  784. for (i = 0; i < ls; i++)
  785. pd[i] += pd_last[i];
  786. pd += s->image_linesize;
  787. pd_last += s->image_linesize;
  788. }
  789. }
  790. // divide by 255 and round to nearest
  791. // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
  792. #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
  793. static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s,
  794. AVFrame *p)
  795. {
  796. int i, j;
  797. uint8_t *pd = p->data[0];
  798. uint8_t *pd_last = s->last_picture.f->data[0];
  799. uint8_t *pd_last_region = s->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
  800. s->previous_picture.f->data[0] : s->last_picture.f->data[0];
  801. int ls = FFMIN(av_image_get_linesize(p->format, s->width, 0), s->width * s->bpp);
  802. if (ls < 0)
  803. return ls;
  804. if (s->blend_op == APNG_BLEND_OP_OVER &&
  805. avctx->pix_fmt != AV_PIX_FMT_RGBA && avctx->pix_fmt != AV_PIX_FMT_ARGB) {
  806. avpriv_request_sample(avctx, "Blending with pixel format %s",
  807. av_get_pix_fmt_name(avctx->pix_fmt));
  808. return AVERROR_PATCHWELCOME;
  809. }
  810. ff_thread_await_progress(&s->last_picture, INT_MAX, 0);
  811. if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS)
  812. ff_thread_await_progress(&s->previous_picture, INT_MAX, 0);
  813. for (j = 0; j < s->y_offset; j++) {
  814. memcpy(pd, pd_last, ls);
  815. pd += s->image_linesize;
  816. pd_last += s->image_linesize;
  817. }
  818. if (s->dispose_op != APNG_DISPOSE_OP_BACKGROUND && s->blend_op == APNG_BLEND_OP_OVER) {
  819. uint8_t ri, gi, bi, ai;
  820. pd_last_region += s->y_offset * s->image_linesize;
  821. if (avctx->pix_fmt == AV_PIX_FMT_RGBA) {
  822. ri = 0;
  823. gi = 1;
  824. bi = 2;
  825. ai = 3;
  826. } else {
  827. ri = 3;
  828. gi = 2;
  829. bi = 1;
  830. ai = 0;
  831. }
  832. for (j = s->y_offset; j < s->y_offset + s->cur_h; j++) {
  833. i = s->x_offset * s->bpp;
  834. if (i)
  835. memcpy(pd, pd_last, i);
  836. for (; i < (s->x_offset + s->cur_w) * s->bpp; i += s->bpp) {
  837. uint8_t alpha = pd[i+ai];
  838. /* output = alpha * foreground + (1-alpha) * background */
  839. switch (alpha) {
  840. case 0:
  841. pd[i+ri] = pd_last_region[i+ri];
  842. pd[i+gi] = pd_last_region[i+gi];
  843. pd[i+bi] = pd_last_region[i+bi];
  844. pd[i+ai] = 0xff;
  845. break;
  846. case 255:
  847. break;
  848. default:
  849. pd[i+ri] = FAST_DIV255(alpha * pd[i+ri] + (255 - alpha) * pd_last_region[i+ri]);
  850. pd[i+gi] = FAST_DIV255(alpha * pd[i+gi] + (255 - alpha) * pd_last_region[i+gi]);
  851. pd[i+bi] = FAST_DIV255(alpha * pd[i+bi] + (255 - alpha) * pd_last_region[i+bi]);
  852. pd[i+ai] = 0xff;
  853. break;
  854. }
  855. }
  856. if (ls - i)
  857. memcpy(pd+i, pd_last+i, ls - i);
  858. pd += s->image_linesize;
  859. pd_last += s->image_linesize;
  860. pd_last_region += s->image_linesize;
  861. }
  862. } else {
  863. for (j = s->y_offset; j < s->y_offset + s->cur_h; j++) {
  864. int end_offset = (s->x_offset + s->cur_w) * s->bpp;
  865. int end_len = ls - end_offset;
  866. if (s->x_offset)
  867. memcpy(pd, pd_last, s->x_offset * s->bpp);
  868. if (end_len)
  869. memcpy(pd+end_offset, pd_last+end_offset, end_len);
  870. pd += s->image_linesize;
  871. pd_last += s->image_linesize;
  872. }
  873. }
  874. for (j = s->y_offset + s->cur_h; j < s->height; j++) {
  875. memcpy(pd, pd_last, ls);
  876. pd += s->image_linesize;
  877. pd_last += s->image_linesize;
  878. }
  879. return 0;
  880. }
  881. static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
  882. AVFrame *p, AVPacket *avpkt)
  883. {
  884. AVDictionary *metadata = NULL;
  885. uint32_t tag, length;
  886. int decode_next_dat = 0;
  887. int ret = AVERROR_INVALIDDATA;
  888. AVFrame *ref;
  889. for (;;) {
  890. length = bytestream2_get_bytes_left(&s->gb);
  891. if (length <= 0) {
  892. if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
  893. if (!(s->state & PNG_IDAT))
  894. return 0;
  895. else
  896. goto exit_loop;
  897. }
  898. av_log(avctx, AV_LOG_ERROR, "%d bytes left\n", length);
  899. if ( s->state & PNG_ALLIMAGE
  900. && avctx->strict_std_compliance <= FF_COMPLIANCE_NORMAL)
  901. goto exit_loop;
  902. goto fail;
  903. }
  904. length = bytestream2_get_be32(&s->gb);
  905. if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
  906. av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
  907. goto fail;
  908. }
  909. tag = bytestream2_get_le32(&s->gb);
  910. if (avctx->debug & FF_DEBUG_STARTCODE)
  911. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  912. (tag & 0xff),
  913. ((tag >> 8) & 0xff),
  914. ((tag >> 16) & 0xff),
  915. ((tag >> 24) & 0xff), length);
  916. switch (tag) {
  917. case MKTAG('I', 'H', 'D', 'R'):
  918. if (decode_ihdr_chunk(avctx, s, length) < 0)
  919. goto fail;
  920. break;
  921. case MKTAG('p', 'H', 'Y', 's'):
  922. if (decode_phys_chunk(avctx, s) < 0)
  923. goto fail;
  924. break;
  925. case MKTAG('f', 'c', 'T', 'L'):
  926. if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
  927. goto skip_tag;
  928. if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
  929. goto fail;
  930. decode_next_dat = 1;
  931. break;
  932. case MKTAG('f', 'd', 'A', 'T'):
  933. if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
  934. goto skip_tag;
  935. if (!decode_next_dat)
  936. goto fail;
  937. bytestream2_get_be32(&s->gb);
  938. length -= 4;
  939. /* fallthrough */
  940. case MKTAG('I', 'D', 'A', 'T'):
  941. if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
  942. goto skip_tag;
  943. if (decode_idat_chunk(avctx, s, length, p) < 0)
  944. goto fail;
  945. break;
  946. case MKTAG('P', 'L', 'T', 'E'):
  947. if (decode_plte_chunk(avctx, s, length) < 0)
  948. goto skip_tag;
  949. break;
  950. case MKTAG('t', 'R', 'N', 'S'):
  951. if (decode_trns_chunk(avctx, s, length) < 0)
  952. goto skip_tag;
  953. break;
  954. case MKTAG('t', 'E', 'X', 't'):
  955. if (decode_text_chunk(s, length, 0, &metadata) < 0)
  956. av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
  957. bytestream2_skip(&s->gb, length + 4);
  958. break;
  959. case MKTAG('z', 'T', 'X', 't'):
  960. if (decode_text_chunk(s, length, 1, &metadata) < 0)
  961. av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
  962. bytestream2_skip(&s->gb, length + 4);
  963. break;
  964. case MKTAG('I', 'E', 'N', 'D'):
  965. if (!(s->state & PNG_ALLIMAGE))
  966. av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
  967. if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
  968. goto fail;
  969. }
  970. bytestream2_skip(&s->gb, 4); /* crc */
  971. goto exit_loop;
  972. default:
  973. /* skip tag */
  974. skip_tag:
  975. bytestream2_skip(&s->gb, length + 4);
  976. break;
  977. }
  978. }
  979. exit_loop:
  980. if (s->bits_per_pixel <= 4)
  981. handle_small_bpp(s, p);
  982. /* handle p-frames only if a predecessor frame is available */
  983. ref = s->dispose_op == APNG_DISPOSE_OP_PREVIOUS ?
  984. s->previous_picture.f : s->last_picture.f;
  985. if (ref->data[0]) {
  986. if ( !(avpkt->flags & AV_PKT_FLAG_KEY) && avctx->codec_tag != AV_RL32("MPNG")
  987. && ref->width == p->width
  988. && ref->height== p->height
  989. && ref->format== p->format
  990. ) {
  991. if (CONFIG_PNG_DECODER && avctx->codec_id != AV_CODEC_ID_APNG)
  992. handle_p_frame_png(s, p);
  993. else if (CONFIG_APNG_DECODER &&
  994. avctx->codec_id == AV_CODEC_ID_APNG &&
  995. (ret = handle_p_frame_apng(avctx, s, p)) < 0)
  996. goto fail;
  997. }
  998. }
  999. ff_thread_report_progress(&s->picture, INT_MAX, 0);
  1000. av_frame_set_metadata(p, metadata);
  1001. metadata = NULL;
  1002. return 0;
  1003. fail:
  1004. av_dict_free(&metadata);
  1005. ff_thread_report_progress(&s->picture, INT_MAX, 0);
  1006. return ret;
  1007. }
  1008. #if CONFIG_PNG_DECODER
  1009. static int decode_frame_png(AVCodecContext *avctx,
  1010. void *data, int *got_frame,
  1011. AVPacket *avpkt)
  1012. {
  1013. PNGDecContext *const s = avctx->priv_data;
  1014. const uint8_t *buf = avpkt->data;
  1015. int buf_size = avpkt->size;
  1016. AVFrame *p;
  1017. int64_t sig;
  1018. int ret;
  1019. ff_thread_release_buffer(avctx, &s->last_picture);
  1020. FFSWAP(ThreadFrame, s->picture, s->last_picture);
  1021. p = s->picture.f;
  1022. bytestream2_init(&s->gb, buf, buf_size);
  1023. /* check signature */
  1024. sig = bytestream2_get_be64(&s->gb);
  1025. if (sig != PNGSIG &&
  1026. sig != MNGSIG) {
  1027. av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
  1028. return AVERROR_INVALIDDATA;
  1029. }
  1030. s->y = s->state = 0;
  1031. /* init the zlib */
  1032. s->zstream.zalloc = ff_png_zalloc;
  1033. s->zstream.zfree = ff_png_zfree;
  1034. s->zstream.opaque = NULL;
  1035. ret = inflateInit(&s->zstream);
  1036. if (ret != Z_OK) {
  1037. av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
  1038. return AVERROR_EXTERNAL;
  1039. }
  1040. if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
  1041. goto the_end;
  1042. if ((ret = av_frame_ref(data, s->picture.f)) < 0)
  1043. return ret;
  1044. *got_frame = 1;
  1045. ret = bytestream2_tell(&s->gb);
  1046. the_end:
  1047. inflateEnd(&s->zstream);
  1048. s->crow_buf = NULL;
  1049. return ret;
  1050. }
  1051. #endif
  1052. #if CONFIG_APNG_DECODER
  1053. static int decode_frame_apng(AVCodecContext *avctx,
  1054. void *data, int *got_frame,
  1055. AVPacket *avpkt)
  1056. {
  1057. PNGDecContext *const s = avctx->priv_data;
  1058. int ret;
  1059. AVFrame *p;
  1060. ThreadFrame tmp;
  1061. ff_thread_release_buffer(avctx, &s->previous_picture);
  1062. tmp = s->previous_picture;
  1063. s->previous_picture = s->last_picture;
  1064. s->last_picture = s->picture;
  1065. s->picture = tmp;
  1066. p = s->picture.f;
  1067. if (!(s->state & PNG_IHDR)) {
  1068. if (!avctx->extradata_size)
  1069. return AVERROR_INVALIDDATA;
  1070. /* only init fields, there is no zlib use in extradata */
  1071. s->zstream.zalloc = ff_png_zalloc;
  1072. s->zstream.zfree = ff_png_zfree;
  1073. bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
  1074. if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
  1075. goto end;
  1076. }
  1077. /* reset state for a new frame */
  1078. if ((ret = inflateInit(&s->zstream)) != Z_OK) {
  1079. av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
  1080. ret = AVERROR_EXTERNAL;
  1081. goto end;
  1082. }
  1083. s->y = 0;
  1084. s->state &= ~(PNG_IDAT | PNG_ALLIMAGE);
  1085. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  1086. if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
  1087. goto end;
  1088. if (!(s->state & PNG_ALLIMAGE))
  1089. av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
  1090. if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
  1091. ret = AVERROR_INVALIDDATA;
  1092. goto end;
  1093. }
  1094. if ((ret = av_frame_ref(data, s->picture.f)) < 0)
  1095. goto end;
  1096. *got_frame = 1;
  1097. ret = bytestream2_tell(&s->gb);
  1098. end:
  1099. inflateEnd(&s->zstream);
  1100. return ret;
  1101. }
  1102. #endif
  1103. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1104. {
  1105. PNGDecContext *psrc = src->priv_data;
  1106. PNGDecContext *pdst = dst->priv_data;
  1107. int ret;
  1108. if (dst == src)
  1109. return 0;
  1110. pdst->frame_id = psrc->frame_id;
  1111. ff_thread_release_buffer(dst, &pdst->picture);
  1112. if (psrc->picture.f->data[0] &&
  1113. (ret = ff_thread_ref_frame(&pdst->picture, &psrc->picture)) < 0)
  1114. return ret;
  1115. if (CONFIG_APNG_DECODER && dst->codec_id == AV_CODEC_ID_APNG) {
  1116. ff_thread_release_buffer(dst, &pdst->last_picture);
  1117. if (psrc->last_picture.f->data[0])
  1118. return ff_thread_ref_frame(&pdst->last_picture, &psrc->last_picture);
  1119. }
  1120. return 0;
  1121. }
  1122. static av_cold int png_dec_init(AVCodecContext *avctx)
  1123. {
  1124. PNGDecContext *s = avctx->priv_data;
  1125. s->avctx = avctx;
  1126. s->previous_picture.f = av_frame_alloc();
  1127. s->last_picture.f = av_frame_alloc();
  1128. s->picture.f = av_frame_alloc();
  1129. if (!s->previous_picture.f || !s->last_picture.f || !s->picture.f) {
  1130. av_frame_free(&s->previous_picture.f);
  1131. av_frame_free(&s->last_picture.f);
  1132. av_frame_free(&s->picture.f);
  1133. return AVERROR(ENOMEM);
  1134. }
  1135. if (!avctx->internal->is_copy) {
  1136. avctx->internal->allocate_progress = 1;
  1137. ff_pngdsp_init(&s->dsp);
  1138. }
  1139. return 0;
  1140. }
  1141. static av_cold int png_dec_end(AVCodecContext *avctx)
  1142. {
  1143. PNGDecContext *s = avctx->priv_data;
  1144. ff_thread_release_buffer(avctx, &s->previous_picture);
  1145. av_frame_free(&s->previous_picture.f);
  1146. ff_thread_release_buffer(avctx, &s->last_picture);
  1147. av_frame_free(&s->last_picture.f);
  1148. ff_thread_release_buffer(avctx, &s->picture);
  1149. av_frame_free(&s->picture.f);
  1150. av_freep(&s->buffer);
  1151. s->buffer_size = 0;
  1152. av_freep(&s->last_row);
  1153. s->last_row_size = 0;
  1154. av_freep(&s->tmp_row);
  1155. s->tmp_row_size = 0;
  1156. return 0;
  1157. }
  1158. #if CONFIG_APNG_DECODER
  1159. AVCodec ff_apng_decoder = {
  1160. .name = "apng",
  1161. .long_name = NULL_IF_CONFIG_SMALL("APNG (Animated Portable Network Graphics) image"),
  1162. .type = AVMEDIA_TYPE_VIDEO,
  1163. .id = AV_CODEC_ID_APNG,
  1164. .priv_data_size = sizeof(PNGDecContext),
  1165. .init = png_dec_init,
  1166. .close = png_dec_end,
  1167. .decode = decode_frame_apng,
  1168. .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
  1169. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  1170. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1171. };
  1172. #endif
  1173. #if CONFIG_PNG_DECODER
  1174. AVCodec ff_png_decoder = {
  1175. .name = "png",
  1176. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  1177. .type = AVMEDIA_TYPE_VIDEO,
  1178. .id = AV_CODEC_ID_PNG,
  1179. .priv_data_size = sizeof(PNGDecContext),
  1180. .init = png_dec_init,
  1181. .close = png_dec_end,
  1182. .decode = decode_frame_png,
  1183. .init_thread_copy = ONLY_IF_THREADS_ENABLED(png_dec_init),
  1184. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  1185. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1186. };
  1187. #endif