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.

734 lines
24KB

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