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.

746 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->color_type == PNG_COLOR_TYPE_PALETTE) {
  461. avctx->pix_fmt = PIX_FMT_PAL8;
  462. } else if (s->bit_depth == 1) {
  463. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  464. } else if (s->bit_depth == 8 &&
  465. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  466. avctx->pix_fmt = PIX_FMT_GRAY8A;
  467. } else {
  468. av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
  469. "and color type %d\n",
  470. s->bit_depth, s->color_type);
  471. goto fail;
  472. }
  473. if(p->data[0])
  474. avctx->release_buffer(avctx, p);
  475. p->reference= 3;
  476. if(avctx->get_buffer(avctx, p) < 0){
  477. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  478. goto fail;
  479. }
  480. p->pict_type= AV_PICTURE_TYPE_I;
  481. p->key_frame= 1;
  482. p->interlaced_frame = !!s->interlace_type;
  483. /* compute the compressed row size */
  484. if (!s->interlace_type) {
  485. s->crow_size = s->row_size + 1;
  486. } else {
  487. s->pass = 0;
  488. s->pass_row_size = ff_png_pass_row_size(s->pass,
  489. s->bits_per_pixel,
  490. s->width);
  491. s->crow_size = s->pass_row_size + 1;
  492. }
  493. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  494. s->row_size, s->crow_size);
  495. s->image_buf = p->data[0];
  496. s->image_linesize = p->linesize[0];
  497. /* copy the palette if needed */
  498. if (avctx->pix_fmt == PIX_FMT_PAL8)
  499. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  500. /* empty row is used if differencing to the first row */
  501. s->last_row = av_mallocz(s->row_size);
  502. if (!s->last_row)
  503. goto fail;
  504. if (s->interlace_type ||
  505. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  506. s->tmp_row = av_malloc(s->row_size);
  507. if (!s->tmp_row)
  508. goto fail;
  509. }
  510. /* compressed row */
  511. crow_buf_base = av_malloc(s->row_size + 16);
  512. if (!crow_buf_base)
  513. goto fail;
  514. /* we want crow_buf+1 to be 16-byte aligned */
  515. s->crow_buf = crow_buf_base + 15;
  516. s->zstream.avail_out = s->crow_size;
  517. s->zstream.next_out = s->crow_buf;
  518. }
  519. s->state |= PNG_IDAT;
  520. if (png_decode_idat(s, length) < 0)
  521. goto fail;
  522. bytestream2_skip(&s->gb, 4); /* crc */
  523. break;
  524. case MKTAG('P', 'L', 'T', 'E'):
  525. {
  526. int n, i, r, g, b;
  527. if ((length % 3) != 0 || length > 256 * 3)
  528. goto skip_tag;
  529. /* read the palette */
  530. n = length / 3;
  531. for(i=0;i<n;i++) {
  532. r = bytestream2_get_byte(&s->gb);
  533. g = bytestream2_get_byte(&s->gb);
  534. b = bytestream2_get_byte(&s->gb);
  535. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  536. }
  537. for(;i<256;i++) {
  538. s->palette[i] = (0xff << 24);
  539. }
  540. s->state |= PNG_PLTE;
  541. bytestream2_skip(&s->gb, 4); /* crc */
  542. }
  543. break;
  544. case MKTAG('t', 'R', 'N', 'S'):
  545. {
  546. int v, i;
  547. /* read the transparency. XXX: Only palette mode supported */
  548. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  549. length > 256 ||
  550. !(s->state & PNG_PLTE))
  551. goto skip_tag;
  552. for(i=0;i<length;i++) {
  553. v = bytestream2_get_byte(&s->gb);
  554. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  555. }
  556. bytestream2_skip(&s->gb, 4); /* crc */
  557. }
  558. break;
  559. case MKTAG('I', 'E', 'N', 'D'):
  560. if (!(s->state & PNG_ALLIMAGE))
  561. av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
  562. if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
  563. goto fail;
  564. }
  565. bytestream2_skip(&s->gb, 4); /* crc */
  566. goto exit_loop;
  567. default:
  568. /* skip tag */
  569. skip_tag:
  570. bytestream2_skip(&s->gb, length + 4);
  571. break;
  572. }
  573. }
  574. exit_loop:
  575. if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
  576. int i, j;
  577. uint8_t *pd = s->current_picture->data[0];
  578. for(j=0; j < s->height; j++) {
  579. for(i=s->width/8-1; i>=0; i--) {
  580. pd[8*i+7]= pd[i] &1;
  581. pd[8*i+6]= (pd[i]>>1)&1;
  582. pd[8*i+5]= (pd[i]>>2)&1;
  583. pd[8*i+4]= (pd[i]>>3)&1;
  584. pd[8*i+3]= (pd[i]>>4)&1;
  585. pd[8*i+2]= (pd[i]>>5)&1;
  586. pd[8*i+1]= (pd[i]>>6)&1;
  587. pd[8*i+0]= pd[i]>>7;
  588. }
  589. pd += s->image_linesize;
  590. }
  591. }
  592. if(s->bits_per_pixel == 2){
  593. int i, j;
  594. uint8_t *pd = s->current_picture->data[0];
  595. for(j=0; j < s->height; j++) {
  596. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  597. for(i=s->width/4-1; i>=0; i--) {
  598. pd[4*i+3]= pd[i] &3;
  599. pd[4*i+2]= (pd[i]>>2)&3;
  600. pd[4*i+1]= (pd[i]>>4)&3;
  601. pd[4*i+0]= pd[i]>>6;
  602. }
  603. } else {
  604. for(i=s->width/4-1; i>=0; i--) {
  605. pd[4*i+3]= ( pd[i] &3)*0x55;
  606. pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
  607. pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
  608. pd[4*i+0]= ( pd[i]>>6 )*0x55;
  609. }
  610. }
  611. pd += s->image_linesize;
  612. }
  613. }
  614. if(s->bits_per_pixel == 4){
  615. int i, j;
  616. uint8_t *pd = s->current_picture->data[0];
  617. for(j=0; j < s->height; j++) {
  618. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  619. for(i=s->width/2-1; i>=0; i--) {
  620. pd[2*i+1]= pd[i]&15;
  621. pd[2*i+0]= pd[i]>>4;
  622. }
  623. } else {
  624. for(i=s->width/2-1; i>=0; i--) {
  625. pd[2*i+1]= (pd[i]&15)*0x11;
  626. pd[2*i+0]= (pd[i]>>4)*0x11;
  627. }
  628. }
  629. pd += s->image_linesize;
  630. }
  631. }
  632. /* handle p-frames only if a predecessor frame is available */
  633. if(s->last_picture->data[0] != NULL) {
  634. if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
  635. int i, j;
  636. uint8_t *pd = s->current_picture->data[0];
  637. uint8_t *pd_last = s->last_picture->data[0];
  638. for(j=0; j < s->height; j++) {
  639. for(i=0; i < s->width * s->bpp; i++) {
  640. pd[i] += pd_last[i];
  641. }
  642. pd += s->image_linesize;
  643. pd_last += s->image_linesize;
  644. }
  645. }
  646. }
  647. *picture= *s->current_picture;
  648. *data_size = sizeof(AVFrame);
  649. ret = bytestream2_tell(&s->gb);
  650. the_end:
  651. inflateEnd(&s->zstream);
  652. av_free(crow_buf_base);
  653. s->crow_buf = NULL;
  654. av_freep(&s->last_row);
  655. av_freep(&s->tmp_row);
  656. return ret;
  657. fail:
  658. ret = -1;
  659. goto the_end;
  660. }
  661. static av_cold int png_dec_init(AVCodecContext *avctx)
  662. {
  663. PNGDecContext *s = avctx->priv_data;
  664. s->current_picture = &s->picture1;
  665. s->last_picture = &s->picture2;
  666. avcodec_get_frame_defaults(&s->picture1);
  667. avcodec_get_frame_defaults(&s->picture2);
  668. ff_pngdsp_init(&s->dsp);
  669. return 0;
  670. }
  671. static av_cold int png_dec_end(AVCodecContext *avctx)
  672. {
  673. PNGDecContext *s = avctx->priv_data;
  674. if (s->picture1.data[0])
  675. avctx->release_buffer(avctx, &s->picture1);
  676. if (s->picture2.data[0])
  677. avctx->release_buffer(avctx, &s->picture2);
  678. return 0;
  679. }
  680. AVCodec ff_png_decoder = {
  681. .name = "png",
  682. .type = AVMEDIA_TYPE_VIDEO,
  683. .id = CODEC_ID_PNG,
  684. .priv_data_size = sizeof(PNGDecContext),
  685. .init = png_dec_init,
  686. .close = png_dec_end,
  687. .decode = decode_frame,
  688. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  689. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  690. };