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.

914 lines
31KB

  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 "png.h"
  28. #include "pngdsp.h"
  29. /* TODO:
  30. * - add 16 bit depth support
  31. */
  32. #include <zlib.h>
  33. //#define DEBUG
  34. typedef struct PNGDecContext {
  35. PNGDSPContext dsp;
  36. AVCodecContext *avctx;
  37. GetByteContext gb;
  38. AVFrame picture1, picture2;
  39. AVFrame *current_picture, *last_picture;
  40. int state;
  41. int width, height;
  42. int bit_depth;
  43. int color_type;
  44. int compression_type;
  45. int interlace_type;
  46. int filter_type;
  47. int channels;
  48. int bits_per_pixel;
  49. int bpp;
  50. uint8_t *image_buf;
  51. int image_linesize;
  52. uint32_t palette[256];
  53. uint8_t *crow_buf;
  54. uint8_t *last_row;
  55. uint8_t *tmp_row;
  56. int pass;
  57. int crow_size; /* compressed row size (include filter type) */
  58. int row_size; /* decompressed row size */
  59. int pass_row_size; /* decompress row size of the current pass */
  60. int y;
  61. z_stream zstream;
  62. } PNGDecContext;
  63. /* Mask to determine which pixels are valid in a pass */
  64. static const uint8_t png_pass_mask[NB_PASSES] = {
  65. 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
  66. };
  67. /* Mask to determine which y pixels can be written in a pass */
  68. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  69. 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  70. };
  71. /* Mask to determine which pixels to overwrite while displaying */
  72. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  73. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  74. };
  75. /* NOTE: we try to construct a good looking image at each pass. width
  76. is the original image width. We also do pixel format conversion at
  77. this stage */
  78. static void png_put_interlaced_row(uint8_t *dst, int width,
  79. int bits_per_pixel, int pass,
  80. int color_type, const uint8_t *src)
  81. {
  82. int x, mask, dsp_mask, j, src_x, b, bpp;
  83. uint8_t *d;
  84. const uint8_t *s;
  85. mask = png_pass_mask[pass];
  86. dsp_mask = png_pass_dsp_mask[pass];
  87. switch (bits_per_pixel) {
  88. case 1:
  89. src_x = 0;
  90. for (x = 0; x < width; x++) {
  91. j = (x & 7);
  92. if ((dsp_mask << j) & 0x80) {
  93. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  94. dst[x >> 3] &= 0xFF7F>>j;
  95. dst[x >> 3] |= b << (7 - j);
  96. }
  97. if ((mask << j) & 0x80)
  98. src_x++;
  99. }
  100. break;
  101. case 2:
  102. src_x = 0;
  103. for (x = 0; x < width; x++) {
  104. int j2 = 2 * (x & 3);
  105. j = (x & 7);
  106. if ((dsp_mask << j) & 0x80) {
  107. b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
  108. dst[x >> 2] &= 0xFF3F>>j2;
  109. dst[x >> 2] |= b << (6 - j2);
  110. }
  111. if ((mask << j) & 0x80)
  112. src_x++;
  113. }
  114. break;
  115. case 4:
  116. src_x = 0;
  117. for (x = 0; x < width; x++) {
  118. int j2 = 4*(x&1);
  119. j = (x & 7);
  120. if ((dsp_mask << j) & 0x80) {
  121. b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
  122. dst[x >> 1] &= 0xFF0F>>j2;
  123. dst[x >> 1] |= b << (4 - j2);
  124. }
  125. if ((mask << j) & 0x80)
  126. src_x++;
  127. }
  128. break;
  129. default:
  130. bpp = bits_per_pixel >> 3;
  131. d = dst;
  132. s = src;
  133. for (x = 0; x < width; x++) {
  134. j = x & 7;
  135. if ((dsp_mask << j) & 0x80) {
  136. memcpy(d, s, bpp);
  137. }
  138. d += bpp;
  139. if ((mask << j) & 0x80)
  140. s += bpp;
  141. }
  142. break;
  143. }
  144. }
  145. void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
  146. {
  147. int i;
  148. for (i = 0; i < w; i++) {
  149. int a, b, c, p, pa, pb, pc;
  150. a = dst[i - bpp];
  151. b = top[i];
  152. c = top[i - bpp];
  153. p = b - c;
  154. pc = a - c;
  155. pa = abs(p);
  156. pb = abs(pc);
  157. pc = abs(p + pc);
  158. if (pa <= pb && pa <= pc)
  159. p = a;
  160. else if (pb <= pc)
  161. p = b;
  162. else
  163. p = c;
  164. dst[i] = p + src[i];
  165. }
  166. }
  167. #define UNROLL1(bpp, op) {\
  168. r = dst[0];\
  169. if(bpp >= 2) g = dst[1];\
  170. if(bpp >= 3) b = dst[2];\
  171. if(bpp >= 4) a = dst[3];\
  172. for(; i <= size - bpp; i+=bpp) {\
  173. dst[i+0] = r = op(r, src[i+0], last[i+0]);\
  174. if(bpp == 1) continue;\
  175. dst[i+1] = g = op(g, src[i+1], last[i+1]);\
  176. if(bpp == 2) continue;\
  177. dst[i+2] = b = op(b, src[i+2], last[i+2]);\
  178. if(bpp == 3) continue;\
  179. dst[i+3] = a = op(a, src[i+3], last[i+3]);\
  180. }\
  181. }
  182. #define UNROLL_FILTER(op)\
  183. if(bpp == 1) UNROLL1(1, op)\
  184. else if(bpp == 2) UNROLL1(2, op)\
  185. else if(bpp == 3) UNROLL1(3, op)\
  186. else if(bpp == 4) UNROLL1(4, op)\
  187. for (; i < size; i++) {\
  188. dst[i] = op(dst[i-bpp], src[i], last[i]);\
  189. }\
  190. /* NOTE: 'dst' can be equal to 'last' */
  191. static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
  192. uint8_t *src, uint8_t *last, int size, int bpp)
  193. {
  194. int i, p, r, g, b, a;
  195. switch (filter_type) {
  196. case PNG_FILTER_VALUE_NONE:
  197. memcpy(dst, src, size);
  198. break;
  199. case PNG_FILTER_VALUE_SUB:
  200. for (i = 0; i < bpp; i++) {
  201. dst[i] = src[i];
  202. }
  203. if (bpp == 4) {
  204. p = *(int*)dst;
  205. for (; i < size; i += bpp) {
  206. int s = *(int*)(src + i);
  207. p = ((s & 0x7f7f7f7f) + (p & 0x7f7f7f7f)) ^ ((s ^ p) & 0x80808080);
  208. *(int*)(dst + i) = p;
  209. }
  210. } else {
  211. #define OP_SUB(x,s,l) x+s
  212. UNROLL_FILTER(OP_SUB);
  213. }
  214. break;
  215. case PNG_FILTER_VALUE_UP:
  216. dsp->add_bytes_l2(dst, src, last, size);
  217. break;
  218. case PNG_FILTER_VALUE_AVG:
  219. for (i = 0; i < bpp; i++) {
  220. p = (last[i] >> 1);
  221. dst[i] = p + src[i];
  222. }
  223. #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
  224. UNROLL_FILTER(OP_AVG);
  225. break;
  226. case PNG_FILTER_VALUE_PAETH:
  227. for (i = 0; i < bpp; i++) {
  228. p = last[i];
  229. dst[i] = p + src[i];
  230. }
  231. if (bpp > 2 && size > 4) {
  232. // would write off the end of the array if we let it process the last pixel with bpp=3
  233. int w = bpp == 4 ? size : size - 3;
  234. dsp->add_paeth_prediction(dst + i, src + i, last + i, w - i, bpp);
  235. i = w;
  236. }
  237. ff_add_png_paeth_prediction(dst + i, src + i, last + i, size - i, bpp);
  238. break;
  239. }
  240. }
  241. /* This used to be called "deloco" in FFmpeg
  242. * and is actually an inverse reversible colorspace transformation */
  243. #define YUV2RGB(NAME, TYPE) \
  244. static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
  245. { \
  246. int i; \
  247. for (i = 0; i < size; i += 3 + alpha) { \
  248. int g = dst [i+1]; \
  249. dst[i+0] += g; \
  250. dst[i+2] += g; \
  251. } \
  252. }
  253. YUV2RGB(rgb8, uint8_t)
  254. YUV2RGB(rgb16, uint16_t)
  255. /* process exactly one decompressed row */
  256. static void png_handle_row(PNGDecContext *s)
  257. {
  258. uint8_t *ptr, *last_row;
  259. int got_line;
  260. if (!s->interlace_type) {
  261. ptr = s->image_buf + s->image_linesize * s->y;
  262. if (s->y == 0)
  263. last_row = s->last_row;
  264. else
  265. last_row = ptr - s->image_linesize;
  266. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  267. last_row, s->row_size, s->bpp);
  268. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  269. if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
  270. if (s->bit_depth == 16) {
  271. deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
  272. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  273. } else {
  274. deloco_rgb8(ptr - s->image_linesize, s->row_size,
  275. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  276. }
  277. }
  278. s->y++;
  279. if (s->y == s->height) {
  280. s->state |= PNG_ALLIMAGE;
  281. if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
  282. if (s->bit_depth == 16) {
  283. deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
  284. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  285. } else {
  286. deloco_rgb8(ptr, s->row_size,
  287. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  288. }
  289. }
  290. }
  291. } else {
  292. got_line = 0;
  293. for (;;) {
  294. ptr = s->image_buf + s->image_linesize * s->y;
  295. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  296. /* if we already read one row, it is time to stop to
  297. wait for the next one */
  298. if (got_line)
  299. break;
  300. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  301. s->last_row, s->pass_row_size, s->bpp);
  302. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  303. got_line = 1;
  304. }
  305. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  306. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  307. s->color_type, s->last_row);
  308. }
  309. s->y++;
  310. if (s->y == s->height) {
  311. memset(s->last_row, 0, s->row_size);
  312. for (;;) {
  313. if (s->pass == NB_PASSES - 1) {
  314. s->state |= PNG_ALLIMAGE;
  315. goto the_end;
  316. } else {
  317. s->pass++;
  318. s->y = 0;
  319. s->pass_row_size = ff_png_pass_row_size(s->pass,
  320. s->bits_per_pixel,
  321. s->width);
  322. s->crow_size = s->pass_row_size + 1;
  323. if (s->pass_row_size != 0)
  324. break;
  325. /* skip pass if empty row */
  326. }
  327. }
  328. }
  329. }
  330. the_end: ;
  331. }
  332. }
  333. static int png_decode_idat(PNGDecContext *s, int length)
  334. {
  335. int ret;
  336. s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
  337. s->zstream.next_in = (unsigned char *)s->gb.buffer;
  338. bytestream2_skip(&s->gb, length);
  339. /* decode one line if possible */
  340. while (s->zstream.avail_in > 0) {
  341. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  342. if (ret != Z_OK && ret != Z_STREAM_END) {
  343. av_log(s->avctx, AV_LOG_ERROR, "inflate returned %d\n", ret);
  344. return -1;
  345. }
  346. if (s->zstream.avail_out == 0) {
  347. if (!(s->state & PNG_ALLIMAGE)) {
  348. png_handle_row(s);
  349. }
  350. s->zstream.avail_out = s->crow_size;
  351. s->zstream.next_out = s->crow_buf;
  352. }
  353. }
  354. return 0;
  355. }
  356. static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
  357. const uint8_t *data_end)
  358. {
  359. z_stream zstream;
  360. unsigned char *buf;
  361. unsigned buf_size;
  362. int ret;
  363. zstream.zalloc = ff_png_zalloc;
  364. zstream.zfree = ff_png_zfree;
  365. zstream.opaque = NULL;
  366. if (inflateInit(&zstream) != Z_OK)
  367. return AVERROR_EXTERNAL;
  368. zstream.next_in = (unsigned char *)data;
  369. zstream.avail_in = data_end - data;
  370. av_bprint_init(bp, 0, -1);
  371. while (zstream.avail_in > 0) {
  372. av_bprint_get_buffer(bp, 1, &buf, &buf_size);
  373. if (!buf_size) {
  374. ret = AVERROR(ENOMEM);
  375. goto fail;
  376. }
  377. zstream.next_out = buf;
  378. zstream.avail_out = buf_size;
  379. ret = inflate(&zstream, Z_PARTIAL_FLUSH);
  380. if (ret != Z_OK && ret != Z_STREAM_END) {
  381. ret = AVERROR_EXTERNAL;
  382. goto fail;
  383. }
  384. bp->len += zstream.next_out - buf;
  385. if (ret == Z_STREAM_END)
  386. break;
  387. }
  388. inflateEnd(&zstream);
  389. bp->str[bp->len] = 0;
  390. return 0;
  391. fail:
  392. inflateEnd(&zstream);
  393. av_bprint_finalize(bp, NULL);
  394. return ret;
  395. }
  396. static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in)
  397. {
  398. size_t extra = 0, i;
  399. uint8_t *out, *q;
  400. for (i = 0; i < size_in; i++)
  401. extra += in[i] >= 0x80;
  402. if (size_in == SIZE_MAX || extra > SIZE_MAX - size_in - 1)
  403. return NULL;
  404. q = out = av_malloc(size_in + extra + 1);
  405. if (!out)
  406. return NULL;
  407. for (i = 0; i < size_in; i++) {
  408. if (in[i] >= 0x80) {
  409. *(q++) = 0xC0 | (in[i] >> 6);
  410. *(q++) = 0x80 | (in[i] & 0x3F);
  411. } else {
  412. *(q++) = in[i];
  413. }
  414. }
  415. *(q++) = 0;
  416. return out;
  417. }
  418. static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed,
  419. AVDictionary **dict)
  420. {
  421. int ret, method;
  422. const uint8_t *data = s->gb.buffer;
  423. const uint8_t *data_end = data + length;
  424. const uint8_t *keyword = data;
  425. const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
  426. uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
  427. unsigned text_len;
  428. AVBPrint bp;
  429. if (!keyword_end)
  430. return AVERROR_INVALIDDATA;
  431. data = keyword_end + 1;
  432. if (compressed) {
  433. if (data == data_end)
  434. return AVERROR_INVALIDDATA;
  435. method = *(data++);
  436. if (method)
  437. return AVERROR_INVALIDDATA;
  438. if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
  439. return ret;
  440. text_len = bp.len;
  441. av_bprint_finalize(&bp, (char **)&text);
  442. if (!text)
  443. return AVERROR(ENOMEM);
  444. } else {
  445. text = (uint8_t *)data;
  446. text_len = data_end - text;
  447. }
  448. kw_utf8 = iso88591_to_utf8(keyword, keyword_end - keyword);
  449. txt_utf8 = iso88591_to_utf8(text, text_len);
  450. if (text != data)
  451. av_free(text);
  452. if (!(kw_utf8 && txt_utf8)) {
  453. av_free(kw_utf8);
  454. av_free(txt_utf8);
  455. return AVERROR(ENOMEM);
  456. }
  457. av_dict_set(dict, kw_utf8, txt_utf8,
  458. AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  459. return 0;
  460. }
  461. static int decode_frame(AVCodecContext *avctx,
  462. void *data, int *got_frame,
  463. AVPacket *avpkt)
  464. {
  465. PNGDecContext * const s = avctx->priv_data;
  466. const uint8_t *buf = avpkt->data;
  467. int buf_size = avpkt->size;
  468. AVFrame *picture = data;
  469. AVDictionary *metadata = NULL;
  470. uint8_t *crow_buf_base = NULL;
  471. AVFrame *p;
  472. uint32_t tag, length;
  473. int64_t sig;
  474. int ret;
  475. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  476. avctx->coded_frame = s->current_picture;
  477. p = s->current_picture;
  478. bytestream2_init(&s->gb, buf, buf_size);
  479. /* check signature */
  480. sig = bytestream2_get_be64(&s->gb);
  481. if (sig != PNGSIG &&
  482. sig != MNGSIG) {
  483. av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
  484. return -1;
  485. }
  486. s->y = s->state = 0;
  487. /* init the zlib */
  488. s->zstream.zalloc = ff_png_zalloc;
  489. s->zstream.zfree = ff_png_zfree;
  490. s->zstream.opaque = NULL;
  491. ret = inflateInit(&s->zstream);
  492. if (ret != Z_OK) {
  493. av_log(avctx, AV_LOG_ERROR, "inflateInit returned %d\n", ret);
  494. return -1;
  495. }
  496. for (;;) {
  497. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  498. av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
  499. goto fail;
  500. }
  501. length = bytestream2_get_be32(&s->gb);
  502. if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
  503. av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
  504. goto fail;
  505. }
  506. tag = bytestream2_get_le32(&s->gb);
  507. if (avctx->debug & FF_DEBUG_STARTCODE)
  508. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  509. (tag & 0xff),
  510. ((tag >> 8) & 0xff),
  511. ((tag >> 16) & 0xff),
  512. ((tag >> 24) & 0xff), length);
  513. switch (tag) {
  514. case MKTAG('I', 'H', 'D', 'R'):
  515. if (length != 13)
  516. goto fail;
  517. s->width = bytestream2_get_be32(&s->gb);
  518. s->height = bytestream2_get_be32(&s->gb);
  519. if (av_image_check_size(s->width, s->height, 0, avctx)) {
  520. s->width = s->height = 0;
  521. av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
  522. goto fail;
  523. }
  524. s->bit_depth = bytestream2_get_byte(&s->gb);
  525. s->color_type = bytestream2_get_byte(&s->gb);
  526. s->compression_type = bytestream2_get_byte(&s->gb);
  527. s->filter_type = bytestream2_get_byte(&s->gb);
  528. s->interlace_type = bytestream2_get_byte(&s->gb);
  529. bytestream2_skip(&s->gb, 4); /* crc */
  530. s->state |= PNG_IHDR;
  531. if (avctx->debug & FF_DEBUG_PICT_INFO)
  532. av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
  533. "compression_type=%d filter_type=%d interlace_type=%d\n",
  534. s->width, s->height, s->bit_depth, s->color_type,
  535. s->compression_type, s->filter_type, s->interlace_type);
  536. break;
  537. case MKTAG('p', 'H', 'Y', 's'):
  538. if (s->state & PNG_IDAT) {
  539. av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
  540. goto fail;
  541. }
  542. avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
  543. avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
  544. if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
  545. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  546. bytestream2_skip(&s->gb, 1); /* unit specifier */
  547. bytestream2_skip(&s->gb, 4); /* crc */
  548. break;
  549. case MKTAG('I', 'D', 'A', 'T'):
  550. if (!(s->state & PNG_IHDR)) {
  551. av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
  552. goto fail;
  553. }
  554. if (!(s->state & PNG_IDAT)) {
  555. /* init image info */
  556. avctx->width = s->width;
  557. avctx->height = s->height;
  558. s->channels = ff_png_get_nb_channels(s->color_type);
  559. s->bits_per_pixel = s->bit_depth * s->channels;
  560. s->bpp = (s->bits_per_pixel + 7) >> 3;
  561. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  562. if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  563. s->color_type == PNG_COLOR_TYPE_RGB) {
  564. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  565. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  566. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  567. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  568. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  569. s->color_type == PNG_COLOR_TYPE_GRAY) {
  570. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  571. } else if (s->bit_depth == 16 &&
  572. s->color_type == PNG_COLOR_TYPE_GRAY) {
  573. avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
  574. } else if (s->bit_depth == 16 &&
  575. s->color_type == PNG_COLOR_TYPE_RGB) {
  576. avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
  577. } else if (s->bit_depth == 16 &&
  578. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  579. avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
  580. } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
  581. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  582. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  583. } else if (s->bit_depth == 1) {
  584. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  585. } else if (s->bit_depth == 8 &&
  586. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  587. avctx->pix_fmt = AV_PIX_FMT_Y400A;
  588. } else {
  589. av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
  590. "and color type %d\n",
  591. s->bit_depth, s->color_type);
  592. goto fail;
  593. }
  594. if (p->data[0])
  595. avctx->release_buffer(avctx, p);
  596. p->reference = 3;
  597. if (ff_get_buffer(avctx, p) < 0) {
  598. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  599. goto fail;
  600. }
  601. p->pict_type = AV_PICTURE_TYPE_I;
  602. p->key_frame = 1;
  603. p->interlaced_frame = !!s->interlace_type;
  604. /* compute the compressed row size */
  605. if (!s->interlace_type) {
  606. s->crow_size = s->row_size + 1;
  607. } else {
  608. s->pass = 0;
  609. s->pass_row_size = ff_png_pass_row_size(s->pass,
  610. s->bits_per_pixel,
  611. s->width);
  612. s->crow_size = s->pass_row_size + 1;
  613. }
  614. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  615. s->row_size, s->crow_size);
  616. s->image_buf = p->data[0];
  617. s->image_linesize = p->linesize[0];
  618. /* copy the palette if needed */
  619. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  620. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  621. /* empty row is used if differencing to the first row */
  622. s->last_row = av_mallocz(s->row_size);
  623. if (!s->last_row)
  624. goto fail;
  625. if (s->interlace_type ||
  626. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  627. s->tmp_row = av_malloc(s->row_size);
  628. if (!s->tmp_row)
  629. goto fail;
  630. }
  631. /* compressed row */
  632. crow_buf_base = av_malloc(s->row_size + 16);
  633. if (!crow_buf_base)
  634. goto fail;
  635. /* we want crow_buf+1 to be 16-byte aligned */
  636. s->crow_buf = crow_buf_base + 15;
  637. s->zstream.avail_out = s->crow_size;
  638. s->zstream.next_out = s->crow_buf;
  639. }
  640. s->state |= PNG_IDAT;
  641. if (png_decode_idat(s, length) < 0)
  642. goto fail;
  643. bytestream2_skip(&s->gb, 4); /* crc */
  644. break;
  645. case MKTAG('P', 'L', 'T', 'E'):
  646. {
  647. int n, i, r, g, b;
  648. if ((length % 3) != 0 || length > 256 * 3)
  649. goto skip_tag;
  650. /* read the palette */
  651. n = length / 3;
  652. for (i = 0; i < n; i++) {
  653. r = bytestream2_get_byte(&s->gb);
  654. g = bytestream2_get_byte(&s->gb);
  655. b = bytestream2_get_byte(&s->gb);
  656. s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  657. }
  658. for (; i < 256; i++) {
  659. s->palette[i] = (0xFFU << 24);
  660. }
  661. s->state |= PNG_PLTE;
  662. bytestream2_skip(&s->gb, 4); /* crc */
  663. }
  664. break;
  665. case MKTAG('t', 'R', 'N', 'S'):
  666. {
  667. int v, i;
  668. /* read the transparency. XXX: Only palette mode supported */
  669. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  670. length > 256 ||
  671. !(s->state & PNG_PLTE))
  672. goto skip_tag;
  673. for (i = 0; i < length; i++) {
  674. v = bytestream2_get_byte(&s->gb);
  675. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  676. }
  677. bytestream2_skip(&s->gb, 4); /* crc */
  678. }
  679. break;
  680. case MKTAG('t', 'E', 'X', 't'):
  681. if (decode_text_chunk(s, length, 0, &metadata) < 0)
  682. av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
  683. bytestream2_skip(&s->gb, length + 4);
  684. break;
  685. case MKTAG('z', 'T', 'X', 't'):
  686. if (decode_text_chunk(s, length, 1, &metadata) < 0)
  687. av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
  688. bytestream2_skip(&s->gb, length + 4);
  689. break;
  690. case MKTAG('I', 'E', 'N', 'D'):
  691. if (!(s->state & PNG_ALLIMAGE))
  692. av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
  693. if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
  694. goto fail;
  695. }
  696. bytestream2_skip(&s->gb, 4); /* crc */
  697. goto exit_loop;
  698. default:
  699. /* skip tag */
  700. skip_tag:
  701. bytestream2_skip(&s->gb, length + 4);
  702. break;
  703. }
  704. }
  705. exit_loop:
  706. if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
  707. int i, j, k;
  708. uint8_t *pd = s->current_picture->data[0];
  709. for (j = 0; j < s->height; j++) {
  710. i = s->width / 8;
  711. for (k = 7; k >= 1; k--)
  712. if ((s->width&7) >= k)
  713. pd[8*i + k - 1] = (pd[i]>>8-k) & 1;
  714. for (i--; i >= 0; i--) {
  715. pd[8*i + 7]= pd[i] & 1;
  716. pd[8*i + 6]= (pd[i]>>1) & 1;
  717. pd[8*i + 5]= (pd[i]>>2) & 1;
  718. pd[8*i + 4]= (pd[i]>>3) & 1;
  719. pd[8*i + 3]= (pd[i]>>4) & 1;
  720. pd[8*i + 2]= (pd[i]>>5) & 1;
  721. pd[8*i + 1]= (pd[i]>>6) & 1;
  722. pd[8*i + 0]= pd[i]>>7;
  723. }
  724. pd += s->image_linesize;
  725. }
  726. }
  727. if (s->bits_per_pixel == 2){
  728. int i, j;
  729. uint8_t *pd = s->current_picture->data[0];
  730. for (j = 0; j < s->height; j++) {
  731. i = s->width / 4;
  732. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  733. if ((s->width&3) >= 3) pd[4*i + 2]= (pd[i] >> 2) & 3;
  734. if ((s->width&3) >= 2) pd[4*i + 1]= (pd[i] >> 4) & 3;
  735. if ((s->width&3) >= 1) pd[4*i + 0]= pd[i] >> 6;
  736. for (i--; i >= 0; i--) {
  737. pd[4*i + 3]= pd[i] & 3;
  738. pd[4*i + 2]= (pd[i]>>2) & 3;
  739. pd[4*i + 1]= (pd[i]>>4) & 3;
  740. pd[4*i + 0]= pd[i]>>6;
  741. }
  742. } else {
  743. if ((s->width&3) >= 3) pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
  744. if ((s->width&3) >= 2) pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
  745. if ((s->width&3) >= 1) pd[4*i + 0]= ( pd[i]>>6 )*0x55;
  746. for (i--; i >= 0; i--) {
  747. pd[4*i + 3]= ( pd[i] & 3)*0x55;
  748. pd[4*i + 2]= ((pd[i]>>2) & 3)*0x55;
  749. pd[4*i + 1]= ((pd[i]>>4) & 3)*0x55;
  750. pd[4*i + 0]= ( pd[i]>>6 )*0x55;
  751. }
  752. }
  753. pd += s->image_linesize;
  754. }
  755. }
  756. if (s->bits_per_pixel == 4){
  757. int i, j;
  758. uint8_t *pd = s->current_picture->data[0];
  759. for (j = 0; j < s->height; j++) {
  760. i = s->width/2;
  761. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  762. if (s->width&1) pd[2*i+0]= pd[i]>>4;
  763. for (i--; i >= 0; i--) {
  764. pd[2*i + 1] = pd[i] & 15;
  765. pd[2*i + 0] = pd[i] >> 4;
  766. }
  767. } else {
  768. if (s->width & 1) pd[2*i + 0]= (pd[i] >> 4) * 0x11;
  769. for (i--; i >= 0; i--) {
  770. pd[2*i + 1] = (pd[i] & 15) * 0x11;
  771. pd[2*i + 0] = (pd[i] >> 4) * 0x11;
  772. }
  773. }
  774. pd += s->image_linesize;
  775. }
  776. }
  777. /* handle p-frames only if a predecessor frame is available */
  778. if (s->last_picture->data[0] != NULL) {
  779. if ( !(avpkt->flags & AV_PKT_FLAG_KEY)
  780. && s->last_picture->width == s->current_picture->width
  781. && s->last_picture->height== s->current_picture->height
  782. && s->last_picture->format== s->current_picture->format
  783. ) {
  784. int i, j;
  785. uint8_t *pd = s->current_picture->data[0];
  786. uint8_t *pd_last = s->last_picture->data[0];
  787. for (j = 0; j < s->height; j++) {
  788. for (i = 0; i < s->width * s->bpp; i++) {
  789. pd[i] += pd_last[i];
  790. }
  791. pd += s->image_linesize;
  792. pd_last += s->image_linesize;
  793. }
  794. }
  795. }
  796. s->current_picture->metadata = metadata;
  797. metadata = NULL;
  798. *picture = *s->current_picture;
  799. *got_frame = 1;
  800. ret = bytestream2_tell(&s->gb);
  801. the_end:
  802. inflateEnd(&s->zstream);
  803. av_free(crow_buf_base);
  804. s->crow_buf = NULL;
  805. av_freep(&s->last_row);
  806. av_freep(&s->tmp_row);
  807. return ret;
  808. fail:
  809. av_dict_free(&metadata);
  810. ret = -1;
  811. goto the_end;
  812. }
  813. static av_cold int png_dec_init(AVCodecContext *avctx)
  814. {
  815. PNGDecContext *s = avctx->priv_data;
  816. s->current_picture = &s->picture1;
  817. s->last_picture = &s->picture2;
  818. avcodec_get_frame_defaults(&s->picture1);
  819. avcodec_get_frame_defaults(&s->picture2);
  820. ff_pngdsp_init(&s->dsp);
  821. s->avctx = avctx;
  822. return 0;
  823. }
  824. static av_cold int png_dec_end(AVCodecContext *avctx)
  825. {
  826. PNGDecContext *s = avctx->priv_data;
  827. if (s->picture1.data[0])
  828. avctx->release_buffer(avctx, &s->picture1);
  829. if (s->picture2.data[0])
  830. avctx->release_buffer(avctx, &s->picture2);
  831. return 0;
  832. }
  833. AVCodec ff_png_decoder = {
  834. .name = "png",
  835. .type = AVMEDIA_TYPE_VIDEO,
  836. .id = AV_CODEC_ID_PNG,
  837. .priv_data_size = sizeof(PNGDecContext),
  838. .init = png_dec_init,
  839. .close = png_dec_end,
  840. .decode = decode_frame,
  841. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  842. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  843. };