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.

752 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. int64_t sig;
  368. int ret;
  369. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  370. avctx->coded_frame= s->current_picture;
  371. p = s->current_picture;
  372. bytestream2_init(&s->gb, buf, buf_size);
  373. /* check signature */
  374. sig = bytestream2_get_be64(&s->gb);
  375. if (sig != PNGSIG &&
  376. sig != MNGSIG) {
  377. av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
  378. return -1;
  379. }
  380. s->y=
  381. s->state=0;
  382. // memset(s, 0, sizeof(PNGDecContext));
  383. /* init the zlib */
  384. s->zstream.zalloc = ff_png_zalloc;
  385. s->zstream.zfree = ff_png_zfree;
  386. s->zstream.opaque = NULL;
  387. ret = inflateInit(&s->zstream);
  388. if (ret != Z_OK)
  389. return -1;
  390. for(;;) {
  391. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  392. av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
  393. goto fail;
  394. }
  395. length = bytestream2_get_be32(&s->gb);
  396. if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
  397. av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
  398. goto fail;
  399. }
  400. tag = bytestream2_get_le32(&s->gb);
  401. if (avctx->debug & FF_DEBUG_STARTCODE)
  402. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  403. (tag & 0xff),
  404. ((tag >> 8) & 0xff),
  405. ((tag >> 16) & 0xff),
  406. ((tag >> 24) & 0xff), length);
  407. switch(tag) {
  408. case MKTAG('I', 'H', 'D', 'R'):
  409. if (length != 13)
  410. goto fail;
  411. s->width = bytestream2_get_be32(&s->gb);
  412. s->height = bytestream2_get_be32(&s->gb);
  413. if(av_image_check_size(s->width, s->height, 0, avctx)){
  414. s->width= s->height= 0;
  415. av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
  416. goto fail;
  417. }
  418. s->bit_depth = bytestream2_get_byte(&s->gb);
  419. s->color_type = bytestream2_get_byte(&s->gb);
  420. s->compression_type = bytestream2_get_byte(&s->gb);
  421. s->filter_type = bytestream2_get_byte(&s->gb);
  422. s->interlace_type = bytestream2_get_byte(&s->gb);
  423. bytestream2_skip(&s->gb, 4); /* crc */
  424. s->state |= PNG_IHDR;
  425. if (avctx->debug & FF_DEBUG_PICT_INFO)
  426. 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",
  427. s->width, s->height, s->bit_depth, s->color_type,
  428. s->compression_type, s->filter_type, s->interlace_type);
  429. break;
  430. case MKTAG('I', 'D', 'A', 'T'):
  431. if (!(s->state & PNG_IHDR)) {
  432. av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
  433. goto fail;
  434. }
  435. if (!(s->state & PNG_IDAT)) {
  436. /* init image info */
  437. avctx->width = s->width;
  438. avctx->height = s->height;
  439. s->channels = ff_png_get_nb_channels(s->color_type);
  440. s->bits_per_pixel = s->bit_depth * s->channels;
  441. s->bpp = (s->bits_per_pixel + 7) >> 3;
  442. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  443. if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  444. s->color_type == PNG_COLOR_TYPE_RGB) {
  445. avctx->pix_fmt = PIX_FMT_RGB24;
  446. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  447. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  448. avctx->pix_fmt = PIX_FMT_RGBA;
  449. } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
  450. s->color_type == PNG_COLOR_TYPE_GRAY) {
  451. avctx->pix_fmt = PIX_FMT_GRAY8;
  452. } else if (s->bit_depth == 16 &&
  453. s->color_type == PNG_COLOR_TYPE_GRAY) {
  454. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  455. } else if (s->bit_depth == 16 &&
  456. s->color_type == PNG_COLOR_TYPE_RGB) {
  457. avctx->pix_fmt = PIX_FMT_RGB48BE;
  458. } else if (s->bit_depth == 16 &&
  459. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  460. avctx->pix_fmt = PIX_FMT_RGBA64BE;
  461. } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
  462. s->color_type == PNG_COLOR_TYPE_PALETTE) {
  463. avctx->pix_fmt = PIX_FMT_PAL8;
  464. } else if (s->bit_depth == 1) {
  465. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  466. } else if (s->bit_depth == 8 &&
  467. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  468. avctx->pix_fmt = PIX_FMT_Y400A;
  469. } else {
  470. av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
  471. "and color type %d\n",
  472. s->bit_depth, s->color_type);
  473. goto fail;
  474. }
  475. if(p->data[0])
  476. avctx->release_buffer(avctx, p);
  477. p->reference= 3;
  478. if(avctx->get_buffer(avctx, p) < 0){
  479. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  480. goto fail;
  481. }
  482. p->pict_type= AV_PICTURE_TYPE_I;
  483. p->key_frame= 1;
  484. p->interlaced_frame = !!s->interlace_type;
  485. /* compute the compressed row size */
  486. if (!s->interlace_type) {
  487. s->crow_size = s->row_size + 1;
  488. } else {
  489. s->pass = 0;
  490. s->pass_row_size = ff_png_pass_row_size(s->pass,
  491. s->bits_per_pixel,
  492. s->width);
  493. s->crow_size = s->pass_row_size + 1;
  494. }
  495. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  496. s->row_size, s->crow_size);
  497. s->image_buf = p->data[0];
  498. s->image_linesize = p->linesize[0];
  499. /* copy the palette if needed */
  500. if (avctx->pix_fmt == PIX_FMT_PAL8)
  501. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  502. /* empty row is used if differencing to the first row */
  503. s->last_row = av_mallocz(s->row_size);
  504. if (!s->last_row)
  505. goto fail;
  506. if (s->interlace_type ||
  507. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  508. s->tmp_row = av_malloc(s->row_size);
  509. if (!s->tmp_row)
  510. goto fail;
  511. }
  512. /* compressed row */
  513. crow_buf_base = av_malloc(s->row_size + 16);
  514. if (!crow_buf_base)
  515. goto fail;
  516. /* we want crow_buf+1 to be 16-byte aligned */
  517. s->crow_buf = crow_buf_base + 15;
  518. s->zstream.avail_out = s->crow_size;
  519. s->zstream.next_out = s->crow_buf;
  520. }
  521. s->state |= PNG_IDAT;
  522. if (png_decode_idat(s, length) < 0)
  523. goto fail;
  524. bytestream2_skip(&s->gb, 4); /* crc */
  525. break;
  526. case MKTAG('P', 'L', 'T', 'E'):
  527. {
  528. int n, i, r, g, b;
  529. if ((length % 3) != 0 || length > 256 * 3)
  530. goto skip_tag;
  531. /* read the palette */
  532. n = length / 3;
  533. for(i=0;i<n;i++) {
  534. r = bytestream2_get_byte(&s->gb);
  535. g = bytestream2_get_byte(&s->gb);
  536. b = bytestream2_get_byte(&s->gb);
  537. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  538. }
  539. for(;i<256;i++) {
  540. s->palette[i] = (0xff << 24);
  541. }
  542. s->state |= PNG_PLTE;
  543. bytestream2_skip(&s->gb, 4); /* crc */
  544. }
  545. break;
  546. case MKTAG('t', 'R', 'N', 'S'):
  547. {
  548. int v, i;
  549. /* read the transparency. XXX: Only palette mode supported */
  550. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  551. length > 256 ||
  552. !(s->state & PNG_PLTE))
  553. goto skip_tag;
  554. for(i=0;i<length;i++) {
  555. v = bytestream2_get_byte(&s->gb);
  556. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  557. }
  558. bytestream2_skip(&s->gb, 4); /* crc */
  559. }
  560. break;
  561. case MKTAG('I', 'E', 'N', 'D'):
  562. if (!(s->state & PNG_ALLIMAGE))
  563. av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
  564. if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
  565. goto fail;
  566. }
  567. bytestream2_skip(&s->gb, 4); /* crc */
  568. goto exit_loop;
  569. default:
  570. /* skip tag */
  571. skip_tag:
  572. bytestream2_skip(&s->gb, length + 4);
  573. break;
  574. }
  575. }
  576. exit_loop:
  577. if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
  578. int i, j;
  579. uint8_t *pd = s->current_picture->data[0];
  580. for(j=0; j < s->height; j++) {
  581. for(i=s->width/8-1; i>=0; i--) {
  582. pd[8*i+7]= pd[i] &1;
  583. pd[8*i+6]= (pd[i]>>1)&1;
  584. pd[8*i+5]= (pd[i]>>2)&1;
  585. pd[8*i+4]= (pd[i]>>3)&1;
  586. pd[8*i+3]= (pd[i]>>4)&1;
  587. pd[8*i+2]= (pd[i]>>5)&1;
  588. pd[8*i+1]= (pd[i]>>6)&1;
  589. pd[8*i+0]= pd[i]>>7;
  590. }
  591. pd += s->image_linesize;
  592. }
  593. }
  594. if(s->bits_per_pixel == 2){
  595. int i, j;
  596. uint8_t *pd = s->current_picture->data[0];
  597. for(j=0; j < s->height; j++) {
  598. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  599. for(i=s->width/4-1; i>=0; i--) {
  600. pd[4*i+3]= pd[i] &3;
  601. pd[4*i+2]= (pd[i]>>2)&3;
  602. pd[4*i+1]= (pd[i]>>4)&3;
  603. pd[4*i+0]= pd[i]>>6;
  604. }
  605. } else {
  606. for(i=s->width/4-1; i>=0; i--) {
  607. pd[4*i+3]= ( pd[i] &3)*0x55;
  608. pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
  609. pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
  610. pd[4*i+0]= ( pd[i]>>6 )*0x55;
  611. }
  612. }
  613. pd += s->image_linesize;
  614. }
  615. }
  616. if(s->bits_per_pixel == 4){
  617. int i, j;
  618. uint8_t *pd = s->current_picture->data[0];
  619. for(j=0; j < s->height; j++) {
  620. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  621. for(i=s->width/2-1; i>=0; i--) {
  622. pd[2*i+1]= pd[i]&15;
  623. pd[2*i+0]= pd[i]>>4;
  624. }
  625. } else {
  626. for(i=s->width/2-1; i>=0; i--) {
  627. pd[2*i+1]= (pd[i]&15)*0x11;
  628. pd[2*i+0]= (pd[i]>>4)*0x11;
  629. }
  630. }
  631. pd += s->image_linesize;
  632. }
  633. }
  634. /* handle p-frames only if a predecessor frame is available */
  635. if(s->last_picture->data[0] != NULL) {
  636. if( !(avpkt->flags & AV_PKT_FLAG_KEY)
  637. && s->last_picture->width == s->current_picture->width
  638. && s->last_picture->height== s->current_picture->height
  639. ) {
  640. int i, j;
  641. uint8_t *pd = s->current_picture->data[0];
  642. uint8_t *pd_last = s->last_picture->data[0];
  643. for(j=0; j < s->height; j++) {
  644. for(i=0; i < s->width * s->bpp; i++) {
  645. pd[i] += pd_last[i];
  646. }
  647. pd += s->image_linesize;
  648. pd_last += s->image_linesize;
  649. }
  650. }
  651. }
  652. *picture= *s->current_picture;
  653. *data_size = sizeof(AVFrame);
  654. ret = bytestream2_tell(&s->gb);
  655. the_end:
  656. inflateEnd(&s->zstream);
  657. av_free(crow_buf_base);
  658. s->crow_buf = NULL;
  659. av_freep(&s->last_row);
  660. av_freep(&s->tmp_row);
  661. return ret;
  662. fail:
  663. ret = -1;
  664. goto the_end;
  665. }
  666. static av_cold int png_dec_init(AVCodecContext *avctx)
  667. {
  668. PNGDecContext *s = avctx->priv_data;
  669. s->current_picture = &s->picture1;
  670. s->last_picture = &s->picture2;
  671. avcodec_get_frame_defaults(&s->picture1);
  672. avcodec_get_frame_defaults(&s->picture2);
  673. ff_pngdsp_init(&s->dsp);
  674. return 0;
  675. }
  676. static av_cold int png_dec_end(AVCodecContext *avctx)
  677. {
  678. PNGDecContext *s = avctx->priv_data;
  679. if (s->picture1.data[0])
  680. avctx->release_buffer(avctx, &s->picture1);
  681. if (s->picture2.data[0])
  682. avctx->release_buffer(avctx, &s->picture2);
  683. return 0;
  684. }
  685. AVCodec ff_png_decoder = {
  686. .name = "png",
  687. .type = AVMEDIA_TYPE_VIDEO,
  688. .id = AV_CODEC_ID_PNG,
  689. .priv_data_size = sizeof(PNGDecContext),
  690. .init = png_dec_init,
  691. .close = png_dec_end,
  692. .decode = decode_frame,
  693. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  694. .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
  695. };