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.

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