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.

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