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.

750 lines
25KB

  1. /*
  2. * PNG image format
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. //#define DEBUG
  22. #include "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "png.h"
  26. #include "pngdsp.h"
  27. /* TODO:
  28. * - add 16 bit depth support
  29. */
  30. #include <zlib.h>
  31. //#define DEBUG
  32. typedef struct PNGDecContext {
  33. PNGDSPContext dsp;
  34. GetByteContext gb;
  35. AVFrame picture1, picture2;
  36. AVFrame *current_picture, *last_picture;
  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 pixels are valid in a pass */
  61. static const uint8_t png_pass_mask[NB_PASSES] = {
  62. 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
  63. };
  64. /* Mask to determine which y pixels can be written in a pass */
  65. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  66. 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  67. };
  68. /* Mask to determine which pixels to overwrite while displaying */
  69. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  70. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  71. };
  72. /* NOTE: we try to construct a good looking image at each pass. width
  73. is the original image width. We also do pixel format conversion at
  74. this stage */
  75. static void png_put_interlaced_row(uint8_t *dst, int width,
  76. int bits_per_pixel, int pass,
  77. int color_type, const uint8_t *src)
  78. {
  79. int x, mask, dsp_mask, j, src_x, b, bpp;
  80. uint8_t *d;
  81. const uint8_t *s;
  82. mask = png_pass_mask[pass];
  83. dsp_mask = png_pass_dsp_mask[pass];
  84. switch(bits_per_pixel) {
  85. case 1:
  86. src_x = 0;
  87. for(x = 0; x < width; x++) {
  88. j = (x & 7);
  89. if ((dsp_mask << j) & 0x80) {
  90. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  91. dst[x >> 3] &= 0xFF7F>>j;
  92. dst[x >> 3] |= b << (7 - j);
  93. }
  94. if ((mask << j) & 0x80)
  95. src_x++;
  96. }
  97. break;
  98. case 2:
  99. src_x = 0;
  100. for(x = 0; x < width; x++) {
  101. int j2 = 2*(x&3);
  102. j = (x & 7);
  103. if ((dsp_mask << j) & 0x80) {
  104. b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
  105. dst[x >> 2] &= 0xFF3F>>j2;
  106. dst[x >> 2] |= b << (6 - j2);
  107. }
  108. if ((mask << j) & 0x80)
  109. src_x++;
  110. }
  111. break;
  112. case 4:
  113. src_x = 0;
  114. for(x = 0; x < width; x++) {
  115. int j2 = 4*(x&1);
  116. j = (x & 7);
  117. if ((dsp_mask << j) & 0x80) {
  118. b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
  119. dst[x >> 1] &= 0xFF0F>>j2;
  120. dst[x >> 1] |= b << (4 - j2);
  121. }
  122. if ((mask << j) & 0x80)
  123. src_x++;
  124. }
  125. break;
  126. default:
  127. bpp = bits_per_pixel >> 3;
  128. d = dst;
  129. s = src;
  130. for(x = 0; x < width; x++) {
  131. j = x & 7;
  132. if ((dsp_mask << j) & 0x80) {
  133. memcpy(d, s, bpp);
  134. }
  135. d += bpp;
  136. if ((mask << j) & 0x80)
  137. s += bpp;
  138. }
  139. break;
  140. }
  141. }
  142. void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
  143. {
  144. int i;
  145. for(i = 0; i < w; i++) {
  146. int a, b, c, p, pa, pb, pc;
  147. a = dst[i - bpp];
  148. b = top[i];
  149. c = top[i - bpp];
  150. p = b - c;
  151. pc = a - c;
  152. pa = abs(p);
  153. pb = abs(pc);
  154. pc = abs(p + pc);
  155. if (pa <= pb && pa <= pc)
  156. p = a;
  157. else if (pb <= pc)
  158. p = b;
  159. else
  160. p = c;
  161. dst[i] = p + src[i];
  162. }
  163. }
  164. #define UNROLL1(bpp, op) {\
  165. r = dst[0];\
  166. if(bpp >= 2) g = dst[1];\
  167. if(bpp >= 3) b = dst[2];\
  168. if(bpp >= 4) a = dst[3];\
  169. for(; i < size; i+=bpp) {\
  170. dst[i+0] = r = op(r, src[i+0], last[i+0]);\
  171. if(bpp == 1) continue;\
  172. dst[i+1] = g = op(g, src[i+1], last[i+1]);\
  173. if(bpp == 2) continue;\
  174. dst[i+2] = b = op(b, src[i+2], last[i+2]);\
  175. if(bpp == 3) continue;\
  176. dst[i+3] = a = op(a, src[i+3], last[i+3]);\
  177. }\
  178. }
  179. #define UNROLL_FILTER(op)\
  180. if(bpp == 1) UNROLL1(1, op)\
  181. else if(bpp == 2) UNROLL1(2, op)\
  182. else if(bpp == 3) UNROLL1(3, op)\
  183. else if(bpp == 4) UNROLL1(4, op)\
  184. else {\
  185. for (; i < size; i += bpp) {\
  186. int j;\
  187. for (j = 0; j < bpp; j++)\
  188. dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
  189. }\
  190. }
  191. /* NOTE: 'dst' can be equal to 'last' */
  192. static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
  193. uint8_t *src, uint8_t *last, int size, int bpp)
  194. {
  195. int i, p, r, g, b, a;
  196. switch(filter_type) {
  197. case PNG_FILTER_VALUE_NONE:
  198. memcpy(dst, src, size);
  199. break;
  200. case PNG_FILTER_VALUE_SUB:
  201. for(i = 0; i < bpp; i++) {
  202. dst[i] = src[i];
  203. }
  204. if(bpp == 4) {
  205. p = *(int*)dst;
  206. for(; i < size; i+=bpp) {
  207. int s = *(int*)(src+i);
  208. p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
  209. *(int*)(dst+i) = p;
  210. }
  211. } else {
  212. #define OP_SUB(x,s,l) x+s
  213. UNROLL_FILTER(OP_SUB);
  214. }
  215. break;
  216. case PNG_FILTER_VALUE_UP:
  217. dsp->add_bytes_l2(dst, src, last, size);
  218. break;
  219. case PNG_FILTER_VALUE_AVG:
  220. for(i = 0; i < bpp; i++) {
  221. p = (last[i] >> 1);
  222. dst[i] = p + src[i];
  223. }
  224. #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
  225. UNROLL_FILTER(OP_AVG);
  226. break;
  227. case PNG_FILTER_VALUE_PAETH:
  228. for(i = 0; i < bpp; i++) {
  229. p = last[i];
  230. dst[i] = p + src[i];
  231. }
  232. if(bpp > 2 && size > 4) {
  233. // would write off the end of the array if we let it process the last pixel with bpp=3
  234. int w = bpp==4 ? size : size-3;
  235. dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
  236. i = w;
  237. }
  238. ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
  239. break;
  240. }
  241. }
  242. /* This used to be called "deloco" in FFmpeg
  243. * and is actually an inverse reversible colorspace transformation */
  244. #define YUV2RGB(NAME, TYPE) \
  245. static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
  246. { \
  247. int i; \
  248. for (i = 0; i < size; i += 3 + alpha) { \
  249. int g = dst [i+1]; \
  250. dst[i+0] += g; \
  251. dst[i+2] += g; \
  252. } \
  253. }
  254. YUV2RGB(rgb8, uint8_t)
  255. YUV2RGB(rgb16, uint16_t)
  256. /* process exactly one decompressed row */
  257. static void png_handle_row(PNGDecContext *s)
  258. {
  259. uint8_t *ptr, *last_row;
  260. int got_line;
  261. if (!s->interlace_type) {
  262. ptr = s->image_buf + s->image_linesize * s->y;
  263. if (s->y == 0)
  264. last_row = s->last_row;
  265. else
  266. last_row = ptr - s->image_linesize;
  267. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  268. last_row, s->row_size, s->bpp);
  269. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  270. if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
  271. if (s->bit_depth == 16) {
  272. deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
  273. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  274. } else {
  275. deloco_rgb8(ptr - s->image_linesize, s->row_size,
  276. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  277. }
  278. }
  279. s->y++;
  280. if (s->y == s->height) {
  281. s->state |= PNG_ALLIMAGE;
  282. if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
  283. if (s->bit_depth == 16) {
  284. deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
  285. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  286. } else {
  287. deloco_rgb8(ptr, s->row_size,
  288. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
  289. }
  290. }
  291. }
  292. } else {
  293. got_line = 0;
  294. for(;;) {
  295. ptr = s->image_buf + s->image_linesize * s->y;
  296. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  297. /* if we already read one row, it is time to stop to
  298. wait for the next one */
  299. if (got_line)
  300. break;
  301. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  302. s->last_row, s->pass_row_size, s->bpp);
  303. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  304. got_line = 1;
  305. }
  306. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  307. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  308. s->color_type, s->last_row);
  309. }
  310. s->y++;
  311. if (s->y == s->height) {
  312. memset(s->last_row, 0, s->row_size);
  313. for(;;) {
  314. if (s->pass == NB_PASSES - 1) {
  315. s->state |= PNG_ALLIMAGE;
  316. goto the_end;
  317. } else {
  318. s->pass++;
  319. s->y = 0;
  320. s->pass_row_size = ff_png_pass_row_size(s->pass,
  321. s->bits_per_pixel,
  322. s->width);
  323. s->crow_size = s->pass_row_size + 1;
  324. if (s->pass_row_size != 0)
  325. break;
  326. /* skip pass if empty row */
  327. }
  328. }
  329. }
  330. }
  331. the_end: ;
  332. }
  333. }
  334. static int png_decode_idat(PNGDecContext *s, int length)
  335. {
  336. int ret;
  337. s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
  338. s->zstream.next_in = (unsigned char *)s->gb.buffer;
  339. bytestream2_skip(&s->gb, length);
  340. /* decode one line if possible */
  341. while (s->zstream.avail_in > 0) {
  342. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  343. if (ret != Z_OK && ret != Z_STREAM_END) {
  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_frame(AVCodecContext *avctx,
  357. void *data, int *data_size,
  358. AVPacket *avpkt)
  359. {
  360. const uint8_t *buf = avpkt->data;
  361. int buf_size = avpkt->size;
  362. PNGDecContext * const s = avctx->priv_data;
  363. AVFrame *picture = data;
  364. AVFrame *p;
  365. uint8_t *crow_buf_base = NULL;
  366. uint32_t tag, length;
  367. int ret;
  368. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  369. avctx->coded_frame= s->current_picture;
  370. p = s->current_picture;
  371. /* check signature */
  372. if (buf_size < 8 ||
  373. memcmp(buf, ff_pngsig, 8) != 0 &&
  374. memcmp(buf, ff_mngsig, 8) != 0) {
  375. av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
  376. return -1;
  377. }
  378. bytestream2_init(&s->gb, buf + 8, buf_size - 8);
  379. s->y=
  380. s->state=0;
  381. // memset(s, 0, sizeof(PNGDecContext));
  382. /* init the zlib */
  383. s->zstream.zalloc = ff_png_zalloc;
  384. s->zstream.zfree = ff_png_zfree;
  385. s->zstream.opaque = NULL;
  386. ret = inflateInit(&s->zstream);
  387. if (ret != Z_OK)
  388. return -1;
  389. for(;;) {
  390. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  391. av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
  392. goto fail;
  393. }
  394. length = bytestream2_get_be32(&s->gb);
  395. if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
  396. av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
  397. goto fail;
  398. }
  399. tag = bytestream2_get_le32(&s->gb);
  400. if (avctx->debug & FF_DEBUG_STARTCODE)
  401. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  402. (tag & 0xff),
  403. ((tag >> 8) & 0xff),
  404. ((tag >> 16) & 0xff),
  405. ((tag >> 24) & 0xff), length);
  406. switch(tag) {
  407. case MKTAG('I', 'H', 'D', 'R'):
  408. if (length != 13)
  409. goto fail;
  410. s->width = bytestream2_get_be32(&s->gb);
  411. s->height = bytestream2_get_be32(&s->gb);
  412. if(av_image_check_size(s->width, s->height, 0, avctx)){
  413. s->width= s->height= 0;
  414. av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
  415. goto fail;
  416. }
  417. s->bit_depth = bytestream2_get_byte(&s->gb);
  418. s->color_type = bytestream2_get_byte(&s->gb);
  419. s->compression_type = bytestream2_get_byte(&s->gb);
  420. s->filter_type = bytestream2_get_byte(&s->gb);
  421. s->interlace_type = bytestream2_get_byte(&s->gb);
  422. bytestream2_skip(&s->gb, 4); /* crc */
  423. s->state |= PNG_IHDR;
  424. if (avctx->debug & FF_DEBUG_PICT_INFO)
  425. av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
  426. s->width, s->height, s->bit_depth, s->color_type,
  427. s->compression_type, s->filter_type, s->interlace_type);
  428. break;
  429. case MKTAG('I', 'D', 'A', 'T'):
  430. if (!(s->state & PNG_IHDR)) {
  431. av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
  432. goto fail;
  433. }
  434. if (!(s->state & PNG_IDAT)) {
  435. /* init image info */
  436. avctx->width = s->width;
  437. avctx->height = s->height;
  438. s->channels = ff_png_get_nb_channels(s->color_type);
  439. s->bits_per_pixel = s->bit_depth * s->channels;
  440. s->bpp = (s->bits_per_pixel + 7) >> 3;
  441. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  442. if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  443. s->color_type == PNG_COLOR_TYPE_RGB) {
  444. avctx->pix_fmt = PIX_FMT_RGB24;
  445. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  446. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  447. avctx->pix_fmt = PIX_FMT_RGBA;
  448. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  449. s->color_type == PNG_COLOR_TYPE_GRAY) {
  450. avctx->pix_fmt = PIX_FMT_GRAY8;
  451. } else if (s->bit_depth == 16 &&
  452. s->color_type == PNG_COLOR_TYPE_GRAY) {
  453. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  454. } else if (s->bit_depth == 16 &&
  455. s->color_type == PNG_COLOR_TYPE_RGB) {
  456. avctx->pix_fmt = PIX_FMT_RGB48BE;
  457. } else if (s->bit_depth == 16 &&
  458. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  459. avctx->pix_fmt = PIX_FMT_RGBA64BE;
  460. } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
  461. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  462. avctx->pix_fmt = PIX_FMT_PAL8;
  463. } else if (s->bit_depth == 1) {
  464. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  465. } else if (s->bit_depth == 8 &&
  466. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  467. avctx->pix_fmt = PIX_FMT_GRAY8A;
  468. } else {
  469. av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
  470. "and color type %d\n",
  471. s->bit_depth, s->color_type);
  472. goto fail;
  473. }
  474. if(p->data[0])
  475. avctx->release_buffer(avctx, p);
  476. p->reference= 3;
  477. if(avctx->get_buffer(avctx, p) < 0){
  478. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  479. goto fail;
  480. }
  481. p->pict_type= AV_PICTURE_TYPE_I;
  482. p->key_frame= 1;
  483. p->interlaced_frame = !!s->interlace_type;
  484. /* compute the compressed row size */
  485. if (!s->interlace_type) {
  486. s->crow_size = s->row_size + 1;
  487. } else {
  488. s->pass = 0;
  489. s->pass_row_size = ff_png_pass_row_size(s->pass,
  490. s->bits_per_pixel,
  491. s->width);
  492. s->crow_size = s->pass_row_size + 1;
  493. }
  494. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  495. s->row_size, s->crow_size);
  496. s->image_buf = p->data[0];
  497. s->image_linesize = p->linesize[0];
  498. /* copy the palette if needed */
  499. if (avctx->pix_fmt == PIX_FMT_PAL8)
  500. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  501. /* empty row is used if differencing to the first row */
  502. s->last_row = av_mallocz(s->row_size);
  503. if (!s->last_row)
  504. goto fail;
  505. if (s->interlace_type ||
  506. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  507. s->tmp_row = av_malloc(s->row_size);
  508. if (!s->tmp_row)
  509. goto fail;
  510. }
  511. /* compressed row */
  512. crow_buf_base = av_malloc(s->row_size + 16);
  513. if (!crow_buf_base)
  514. goto fail;
  515. /* we want crow_buf+1 to be 16-byte aligned */
  516. s->crow_buf = crow_buf_base + 15;
  517. s->zstream.avail_out = s->crow_size;
  518. s->zstream.next_out = s->crow_buf;
  519. }
  520. s->state |= PNG_IDAT;
  521. if (png_decode_idat(s, length) < 0)
  522. goto fail;
  523. bytestream2_skip(&s->gb, 4); /* crc */
  524. break;
  525. case MKTAG('P', 'L', 'T', 'E'):
  526. {
  527. int n, i, r, g, b;
  528. if ((length % 3) != 0 || length > 256 * 3)
  529. goto skip_tag;
  530. /* read the palette */
  531. n = length / 3;
  532. for(i=0;i<n;i++) {
  533. r = bytestream2_get_byte(&s->gb);
  534. g = bytestream2_get_byte(&s->gb);
  535. b = bytestream2_get_byte(&s->gb);
  536. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  537. }
  538. for(;i<256;i++) {
  539. s->palette[i] = (0xff << 24);
  540. }
  541. s->state |= PNG_PLTE;
  542. bytestream2_skip(&s->gb, 4); /* crc */
  543. }
  544. break;
  545. case MKTAG('t', 'R', 'N', 'S'):
  546. {
  547. int v, i;
  548. /* read the transparency. XXX: Only palette mode supported */
  549. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  550. length > 256 ||
  551. !(s->state & PNG_PLTE))
  552. goto skip_tag;
  553. for(i=0;i<length;i++) {
  554. v = bytestream2_get_byte(&s->gb);
  555. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  556. }
  557. bytestream2_skip(&s->gb, 4); /* crc */
  558. }
  559. break;
  560. case MKTAG('I', 'E', 'N', 'D'):
  561. if (!(s->state & PNG_ALLIMAGE))
  562. av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
  563. if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
  564. goto fail;
  565. }
  566. bytestream2_skip(&s->gb, 4); /* crc */
  567. goto exit_loop;
  568. default:
  569. /* skip tag */
  570. skip_tag:
  571. bytestream2_skip(&s->gb, length + 4);
  572. break;
  573. }
  574. }
  575. exit_loop:
  576. if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
  577. int i, j;
  578. uint8_t *pd = s->current_picture->data[0];
  579. for(j=0; j < s->height; j++) {
  580. for(i=s->width/8-1; i>=0; i--) {
  581. pd[8*i+7]= pd[i] &1;
  582. pd[8*i+6]= (pd[i]>>1)&1;
  583. pd[8*i+5]= (pd[i]>>2)&1;
  584. pd[8*i+4]= (pd[i]>>3)&1;
  585. pd[8*i+3]= (pd[i]>>4)&1;
  586. pd[8*i+2]= (pd[i]>>5)&1;
  587. pd[8*i+1]= (pd[i]>>6)&1;
  588. pd[8*i+0]= pd[i]>>7;
  589. }
  590. pd += s->image_linesize;
  591. }
  592. }
  593. if(s->bits_per_pixel == 2){
  594. int i, j;
  595. uint8_t *pd = s->current_picture->data[0];
  596. for(j=0; j < s->height; j++) {
  597. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  598. for(i=s->width/4-1; i>=0; i--) {
  599. pd[4*i+3]= pd[i] &3;
  600. pd[4*i+2]= (pd[i]>>2)&3;
  601. pd[4*i+1]= (pd[i]>>4)&3;
  602. pd[4*i+0]= pd[i]>>6;
  603. }
  604. } else {
  605. for(i=s->width/4-1; i>=0; i--) {
  606. pd[4*i+3]= ( pd[i] &3)*0x55;
  607. pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
  608. pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
  609. pd[4*i+0]= ( pd[i]>>6 )*0x55;
  610. }
  611. }
  612. pd += s->image_linesize;
  613. }
  614. }
  615. if(s->bits_per_pixel == 4){
  616. int i, j;
  617. uint8_t *pd = s->current_picture->data[0];
  618. for(j=0; j < s->height; j++) {
  619. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  620. for(i=s->width/2-1; i>=0; i--) {
  621. pd[2*i+1]= pd[i]&15;
  622. pd[2*i+0]= pd[i]>>4;
  623. }
  624. } else {
  625. for(i=s->width/2-1; i>=0; i--) {
  626. pd[2*i+1]= (pd[i]&15)*0x11;
  627. pd[2*i+0]= (pd[i]>>4)*0x11;
  628. }
  629. }
  630. pd += s->image_linesize;
  631. }
  632. }
  633. /* handle p-frames only if a predecessor frame is available */
  634. if(s->last_picture->data[0] != NULL) {
  635. if( !(avpkt->flags & AV_PKT_FLAG_KEY)
  636. && s->last_picture->width == s->current_picture->width
  637. && s->last_picture->height== s->current_picture->height
  638. ) {
  639. int i, j;
  640. uint8_t *pd = s->current_picture->data[0];
  641. uint8_t *pd_last = s->last_picture->data[0];
  642. for(j=0; j < s->height; j++) {
  643. for(i=0; i < s->width * s->bpp; i++) {
  644. pd[i] += pd_last[i];
  645. }
  646. pd += s->image_linesize;
  647. pd_last += s->image_linesize;
  648. }
  649. }
  650. }
  651. *picture= *s->current_picture;
  652. *data_size = sizeof(AVFrame);
  653. ret = bytestream2_tell(&s->gb);
  654. the_end:
  655. inflateEnd(&s->zstream);
  656. av_free(crow_buf_base);
  657. s->crow_buf = NULL;
  658. av_freep(&s->last_row);
  659. av_freep(&s->tmp_row);
  660. return ret;
  661. fail:
  662. ret = -1;
  663. goto the_end;
  664. }
  665. static av_cold int png_dec_init(AVCodecContext *avctx)
  666. {
  667. PNGDecContext *s = avctx->priv_data;
  668. s->current_picture = &s->picture1;
  669. s->last_picture = &s->picture2;
  670. avcodec_get_frame_defaults(&s->picture1);
  671. avcodec_get_frame_defaults(&s->picture2);
  672. ff_pngdsp_init(&s->dsp);
  673. return 0;
  674. }
  675. static av_cold int png_dec_end(AVCodecContext *avctx)
  676. {
  677. PNGDecContext *s = avctx->priv_data;
  678. if (s->picture1.data[0])
  679. avctx->release_buffer(avctx, &s->picture1);
  680. if (s->picture2.data[0])
  681. avctx->release_buffer(avctx, &s->picture2);
  682. return 0;
  683. }
  684. AVCodec ff_png_decoder = {
  685. .name = "png",
  686. .type = AVMEDIA_TYPE_VIDEO,
  687. .id = CODEC_ID_PNG,
  688. .priv_data_size = sizeof(PNGDecContext),
  689. .init = png_dec_init,
  690. .close = png_dec_end,
  691. .decode = decode_frame,
  692. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  693. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  694. };