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.

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