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.

742 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. /* TODO:
  27. * - add 16 bit depth support
  28. */
  29. #include <zlib.h>
  30. /* Mask to determine which y pixels can be written in a pass */
  31. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  32. 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  33. };
  34. /* Mask to determine which pixels to overwrite while displaying */
  35. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  36. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  37. };
  38. /* NOTE: we try to construct a good looking image at each pass. width
  39. is the original image width. We also do pixel format conversion at
  40. this stage */
  41. static void png_put_interlaced_row(uint8_t *dst, int width,
  42. int bits_per_pixel, int pass,
  43. int color_type, const uint8_t *src)
  44. {
  45. int x, mask, dsp_mask, j, src_x, b, bpp;
  46. uint8_t *d;
  47. const uint8_t *s;
  48. mask = ff_png_pass_mask[pass];
  49. dsp_mask = png_pass_dsp_mask[pass];
  50. switch(bits_per_pixel) {
  51. case 1:
  52. src_x = 0;
  53. for(x = 0; x < width; x++) {
  54. j = (x & 7);
  55. if ((dsp_mask << j) & 0x80) {
  56. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  57. dst[x >> 3] &= 0xFF7F>>j;
  58. dst[x >> 3] |= b << (7 - j);
  59. }
  60. if ((mask << j) & 0x80)
  61. src_x++;
  62. }
  63. break;
  64. case 2:
  65. src_x = 0;
  66. for(x = 0; x < width; x++) {
  67. int j2 = 2*(x&3);
  68. j = (x & 7);
  69. if ((dsp_mask << j) & 0x80) {
  70. b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
  71. dst[x >> 2] &= 0xFF3F>>j2;
  72. dst[x >> 2] |= b << (6 - j2);
  73. }
  74. if ((mask << j) & 0x80)
  75. src_x++;
  76. }
  77. break;
  78. case 4:
  79. src_x = 0;
  80. for(x = 0; x < width; x++) {
  81. int j2 = 4*(x&1);
  82. j = (x & 7);
  83. if ((dsp_mask << j) & 0x80) {
  84. b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
  85. dst[x >> 1] &= 0xFF0F>>j2;
  86. dst[x >> 1] |= b << (4 - j2);
  87. }
  88. if ((mask << j) & 0x80)
  89. src_x++;
  90. }
  91. break;
  92. default:
  93. bpp = bits_per_pixel >> 3;
  94. d = dst;
  95. s = src;
  96. for(x = 0; x < width; x++) {
  97. j = x & 7;
  98. if ((dsp_mask << j) & 0x80) {
  99. memcpy(d, s, bpp);
  100. }
  101. d += bpp;
  102. if ((mask << j) & 0x80)
  103. s += bpp;
  104. }
  105. break;
  106. }
  107. }
  108. // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
  109. #define pb_7f (~0UL/255 * 0x7f)
  110. #define pb_80 (~0UL/255 * 0x80)
  111. static void add_bytes_l2_c(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
  112. {
  113. long i;
  114. for(i=0; i<=w-sizeof(long); i+=sizeof(long)){
  115. long a = *(long*)(src1+i);
  116. long b = *(long*)(src2+i);
  117. *(long*)(dst+i) = ((a&pb_7f) + (b&pb_7f)) ^ ((a^b)&pb_80);
  118. }
  119. for(; i<w; i++)
  120. dst[i] = src1[i]+src2[i];
  121. }
  122. static void add_paeth_prediction_c(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
  123. {
  124. int i;
  125. for(i = 0; i < w; i++) {
  126. int a, b, c, p, pa, pb, pc;
  127. a = dst[i - bpp];
  128. b = top[i];
  129. c = top[i - bpp];
  130. p = b - c;
  131. pc = a - c;
  132. pa = abs(p);
  133. pb = abs(pc);
  134. pc = abs(p + pc);
  135. if (pa <= pb && pa <= pc)
  136. p = a;
  137. else if (pb <= pc)
  138. p = b;
  139. else
  140. p = c;
  141. dst[i] = p + src[i];
  142. }
  143. }
  144. #define UNROLL1(bpp, op) {\
  145. r = dst[0];\
  146. if(bpp >= 2) g = dst[1];\
  147. if(bpp >= 3) b = dst[2];\
  148. if(bpp >= 4) a = dst[3];\
  149. for(; i < size; i+=bpp) {\
  150. dst[i+0] = r = op(r, src[i+0], last[i+0]);\
  151. if(bpp == 1) continue;\
  152. dst[i+1] = g = op(g, src[i+1], last[i+1]);\
  153. if(bpp == 2) continue;\
  154. dst[i+2] = b = op(b, src[i+2], last[i+2]);\
  155. if(bpp == 3) continue;\
  156. dst[i+3] = a = op(a, src[i+3], last[i+3]);\
  157. }\
  158. }
  159. #define UNROLL_FILTER(op)\
  160. if(bpp == 1) UNROLL1(1, op)\
  161. else if(bpp == 2) UNROLL1(2, op)\
  162. else if(bpp == 3) UNROLL1(3, op)\
  163. else if(bpp == 4) UNROLL1(4, op)\
  164. else {\
  165. for (; i < size; i += bpp) {\
  166. int j;\
  167. for (j = 0; j < bpp; j++)\
  168. dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
  169. }\
  170. }
  171. /* NOTE: 'dst' can be equal to 'last' */
  172. static void png_filter_row(PNGDecContext *s, uint8_t *dst, int filter_type,
  173. uint8_t *src, uint8_t *last, int size, int bpp)
  174. {
  175. int i, p, r, g, b, a;
  176. switch(filter_type) {
  177. case PNG_FILTER_VALUE_NONE:
  178. memcpy(dst, src, size);
  179. break;
  180. case PNG_FILTER_VALUE_SUB:
  181. for(i = 0; i < bpp; i++) {
  182. dst[i] = src[i];
  183. }
  184. if(bpp == 4) {
  185. p = *(int*)dst;
  186. for(; i < size; i+=bpp) {
  187. int s = *(int*)(src+i);
  188. p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
  189. *(int*)(dst+i) = p;
  190. }
  191. } else {
  192. #define OP_SUB(x,s,l) x+s
  193. UNROLL_FILTER(OP_SUB);
  194. }
  195. break;
  196. case PNG_FILTER_VALUE_UP:
  197. s->add_bytes_l2(dst, src, last, size);
  198. break;
  199. case PNG_FILTER_VALUE_AVG:
  200. for(i = 0; i < bpp; i++) {
  201. p = (last[i] >> 1);
  202. dst[i] = p + src[i];
  203. }
  204. #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
  205. UNROLL_FILTER(OP_AVG);
  206. break;
  207. case PNG_FILTER_VALUE_PAETH:
  208. for(i = 0; i < bpp; i++) {
  209. p = last[i];
  210. dst[i] = p + src[i];
  211. }
  212. if(bpp > 2 && size > 4) {
  213. // would write off the end of the array if we let it process the last pixel with bpp=3
  214. int w = bpp==4 ? size : size-3;
  215. s->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
  216. i = w;
  217. }
  218. add_paeth_prediction_c(dst+i, src+i, last+i, size-i, bpp);
  219. break;
  220. }
  221. }
  222. static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
  223. {
  224. int j;
  225. unsigned int r, g, b, a;
  226. for(j = 0;j < width; j++) {
  227. r = src[0];
  228. g = src[1];
  229. b = src[2];
  230. a = src[3];
  231. if(loco) {
  232. r = (r+g)&0xff;
  233. b = (b+g)&0xff;
  234. }
  235. dst[0] = r;
  236. dst[1] = g;
  237. dst[2] = b;
  238. dst[3] = a;
  239. dst += 4;
  240. src += 4;
  241. }
  242. }
  243. static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
  244. {
  245. if(loco)
  246. convert_to_rgb32_loco(dst, src, width, 1);
  247. else
  248. memcpy(dst, src, width * 4);
  249. }
  250. static void deloco_rgb24(uint8_t *dst, int size)
  251. {
  252. int i;
  253. for(i=0; i<size; i+=3) {
  254. int g = dst[i+1];
  255. dst[i+0] += g;
  256. dst[i+2] += g;
  257. }
  258. }
  259. /* process exactly one decompressed row */
  260. static void png_handle_row(PNGDecContext *s)
  261. {
  262. uint8_t *ptr, *last_row;
  263. int got_line;
  264. if (!s->interlace_type) {
  265. ptr = s->image_buf + s->image_linesize * s->y;
  266. /* need to swap bytes correctly for RGB_ALPHA */
  267. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  268. png_filter_row(s, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  269. s->last_row, s->row_size, s->bpp);
  270. convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
  271. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  272. } else {
  273. /* in normal case, we avoid one copy */
  274. if (s->y == 0)
  275. last_row = s->last_row;
  276. else
  277. last_row = ptr - s->image_linesize;
  278. png_filter_row(s, ptr, s->crow_buf[0], s->crow_buf + 1,
  279. last_row, s->row_size, s->bpp);
  280. }
  281. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  282. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  283. s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
  284. deloco_rgb24(ptr - s->image_linesize, s->row_size);
  285. s->y++;
  286. if (s->y == s->height) {
  287. s->state |= PNG_ALLIMAGE;
  288. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  289. s->color_type == PNG_COLOR_TYPE_RGB)
  290. deloco_rgb24(ptr, s->row_size);
  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, 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 = length;
  338. s->zstream.next_in = s->bytestream;
  339. s->bytestream += length;
  340. if(s->bytestream > s->bytestream_end)
  341. return -1;
  342. /* decode one line if possible */
  343. while (s->zstream.avail_in > 0) {
  344. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  345. if (ret != Z_OK && ret != Z_STREAM_END) {
  346. return -1;
  347. }
  348. if (s->zstream.avail_out == 0) {
  349. if (!(s->state & PNG_ALLIMAGE)) {
  350. png_handle_row(s);
  351. }
  352. s->zstream.avail_out = s->crow_size;
  353. s->zstream.next_out = s->crow_buf;
  354. }
  355. }
  356. return 0;
  357. }
  358. static int decode_frame(AVCodecContext *avctx,
  359. void *data, int *data_size,
  360. AVPacket *avpkt)
  361. {
  362. const uint8_t *buf = avpkt->data;
  363. int buf_size = avpkt->size;
  364. PNGDecContext * const s = avctx->priv_data;
  365. AVFrame *picture = data;
  366. AVFrame *p;
  367. uint8_t *crow_buf_base = NULL;
  368. uint32_t tag, length;
  369. int ret;
  370. FFSWAP(AVFrame *, s->current_picture, s->last_picture);
  371. avctx->coded_frame= s->current_picture;
  372. p = s->current_picture;
  373. s->bytestream_start=
  374. s->bytestream= buf;
  375. s->bytestream_end= buf + buf_size;
  376. /* check signature */
  377. if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
  378. memcmp(s->bytestream, ff_mngsig, 8) != 0)
  379. return -1;
  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->color_type == PNG_COLOR_TYPE_PALETTE) {
  455. avctx->pix_fmt = PIX_FMT_PAL8;
  456. } else if (s->bit_depth == 1) {
  457. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  458. } else if (s->bit_depth == 8 &&
  459. s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  460. avctx->pix_fmt = PIX_FMT_GRAY8A;
  461. } else {
  462. av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
  463. "and color type %d\n",
  464. s->bit_depth, s->color_type);
  465. goto fail;
  466. }
  467. if(p->data[0])
  468. avctx->release_buffer(avctx, p);
  469. p->reference= 3;
  470. if(avctx->get_buffer(avctx, p) < 0){
  471. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  472. goto fail;
  473. }
  474. p->pict_type= AV_PICTURE_TYPE_I;
  475. p->key_frame= 1;
  476. p->interlaced_frame = !!s->interlace_type;
  477. /* compute the compressed row size */
  478. if (!s->interlace_type) {
  479. s->crow_size = s->row_size + 1;
  480. } else {
  481. s->pass = 0;
  482. s->pass_row_size = ff_png_pass_row_size(s->pass,
  483. s->bits_per_pixel,
  484. s->width);
  485. s->crow_size = s->pass_row_size + 1;
  486. }
  487. av_dlog(avctx, "row_size=%d crow_size =%d\n",
  488. s->row_size, s->crow_size);
  489. s->image_buf = p->data[0];
  490. s->image_linesize = p->linesize[0];
  491. /* copy the palette if needed */
  492. if (avctx->pix_fmt == PIX_FMT_PAL8)
  493. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  494. /* empty row is used if differencing to the first row */
  495. s->last_row = av_mallocz(s->row_size);
  496. if (!s->last_row)
  497. goto fail;
  498. if (s->interlace_type ||
  499. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  500. s->tmp_row = av_malloc(s->row_size);
  501. if (!s->tmp_row)
  502. goto fail;
  503. }
  504. /* compressed row */
  505. crow_buf_base = av_malloc(s->row_size + 16);
  506. if (!crow_buf_base)
  507. goto fail;
  508. /* we want crow_buf+1 to be 16-byte aligned */
  509. s->crow_buf = crow_buf_base + 15;
  510. s->zstream.avail_out = s->crow_size;
  511. s->zstream.next_out = s->crow_buf;
  512. }
  513. s->state |= PNG_IDAT;
  514. if (png_decode_idat(s, length) < 0)
  515. goto fail;
  516. s->bytestream += 4; /* crc */
  517. break;
  518. case MKTAG('P', 'L', 'T', 'E'):
  519. {
  520. int n, i, r, g, b;
  521. if ((length % 3) != 0 || length > 256 * 3)
  522. goto skip_tag;
  523. /* read the palette */
  524. n = length / 3;
  525. for(i=0;i<n;i++) {
  526. r = *s->bytestream++;
  527. g = *s->bytestream++;
  528. b = *s->bytestream++;
  529. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  530. }
  531. for(;i<256;i++) {
  532. s->palette[i] = (0xff << 24);
  533. }
  534. s->state |= PNG_PLTE;
  535. s->bytestream += 4; /* crc */
  536. }
  537. break;
  538. case MKTAG('t', 'R', 'N', 'S'):
  539. {
  540. int v, i;
  541. /* read the transparency. XXX: Only palette mode supported */
  542. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  543. length > 256 ||
  544. !(s->state & PNG_PLTE))
  545. goto skip_tag;
  546. for(i=0;i<length;i++) {
  547. v = *s->bytestream++;
  548. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  549. }
  550. s->bytestream += 4; /* crc */
  551. }
  552. break;
  553. case MKTAG('I', 'E', 'N', 'D'):
  554. if (!(s->state & PNG_ALLIMAGE))
  555. goto fail;
  556. s->bytestream += 4; /* crc */
  557. goto exit_loop;
  558. default:
  559. /* skip tag */
  560. skip_tag:
  561. s->bytestream += length + 4;
  562. break;
  563. }
  564. }
  565. exit_loop:
  566. if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
  567. int i, j;
  568. uint8_t *pd = s->current_picture->data[0];
  569. for(j=0; j < s->height; j++) {
  570. for(i=s->width/8-1; i>=0; i--) {
  571. pd[8*i+7]= pd[i] &1;
  572. pd[8*i+6]= (pd[i]>>1)&1;
  573. pd[8*i+5]= (pd[i]>>2)&1;
  574. pd[8*i+4]= (pd[i]>>3)&1;
  575. pd[8*i+3]= (pd[i]>>4)&1;
  576. pd[8*i+2]= (pd[i]>>5)&1;
  577. pd[8*i+1]= (pd[i]>>6)&1;
  578. pd[8*i+0]= pd[i]>>7;
  579. }
  580. pd += s->image_linesize;
  581. }
  582. }
  583. if(s->bits_per_pixel == 2){
  584. int i, j;
  585. uint8_t *pd = s->current_picture->data[0];
  586. for(j=0; j < s->height; j++) {
  587. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  588. for(i=s->width/4-1; i>=0; i--) {
  589. pd[4*i+3]= pd[i] &3;
  590. pd[4*i+2]= (pd[i]>>2)&3;
  591. pd[4*i+1]= (pd[i]>>4)&3;
  592. pd[4*i+0]= pd[i]>>6;
  593. }
  594. } else {
  595. for(i=s->width/4-1; i>=0; i--) {
  596. pd[4*i+3]= ( pd[i] &3)*0x55;
  597. pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
  598. pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
  599. pd[4*i+0]= ( pd[i]>>6 )*0x55;
  600. }
  601. }
  602. pd += s->image_linesize;
  603. }
  604. }
  605. if(s->bits_per_pixel == 4){
  606. int i, j;
  607. uint8_t *pd = s->current_picture->data[0];
  608. for(j=0; j < s->height; j++) {
  609. if (s->color_type == PNG_COLOR_TYPE_PALETTE){
  610. for(i=s->width/2-1; i>=0; i--) {
  611. pd[2*i+1]= pd[i]&15;
  612. pd[2*i+0]= pd[i]>>4;
  613. }
  614. } else {
  615. for(i=s->width/2-1; i>=0; i--) {
  616. pd[2*i+1]= (pd[i]&15)*0x11;
  617. pd[2*i+0]= (pd[i]>>4)*0x11;
  618. }
  619. }
  620. pd += s->image_linesize;
  621. }
  622. }
  623. /* handle p-frames only if a predecessor frame is available */
  624. if(s->last_picture->data[0] != NULL) {
  625. if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
  626. int i, j;
  627. uint8_t *pd = s->current_picture->data[0];
  628. uint8_t *pd_last = s->last_picture->data[0];
  629. for(j=0; j < s->height; j++) {
  630. for(i=0; i < s->width * s->bpp; i++) {
  631. pd[i] += pd_last[i];
  632. }
  633. pd += s->image_linesize;
  634. pd_last += s->image_linesize;
  635. }
  636. }
  637. }
  638. *picture= *s->current_picture;
  639. *data_size = sizeof(AVFrame);
  640. ret = s->bytestream - s->bytestream_start;
  641. the_end:
  642. inflateEnd(&s->zstream);
  643. av_free(crow_buf_base);
  644. s->crow_buf = NULL;
  645. av_freep(&s->last_row);
  646. av_freep(&s->tmp_row);
  647. return ret;
  648. fail:
  649. ret = -1;
  650. goto the_end;
  651. }
  652. static av_cold int png_dec_init(AVCodecContext *avctx)
  653. {
  654. PNGDecContext *s = avctx->priv_data;
  655. s->current_picture = &s->picture1;
  656. s->last_picture = &s->picture2;
  657. avcodec_get_frame_defaults(&s->picture1);
  658. avcodec_get_frame_defaults(&s->picture2);
  659. #if HAVE_MMX
  660. ff_png_init_mmx(s);
  661. #endif
  662. if (!s->add_paeth_prediction)
  663. s->add_paeth_prediction = add_paeth_prediction_c;
  664. if (!s->add_bytes_l2)
  665. s->add_bytes_l2 = add_bytes_l2_c;
  666. return 0;
  667. }
  668. static av_cold int png_dec_end(AVCodecContext *avctx)
  669. {
  670. PNGDecContext *s = avctx->priv_data;
  671. if (s->picture1.data[0])
  672. avctx->release_buffer(avctx, &s->picture1);
  673. if (s->picture2.data[0])
  674. avctx->release_buffer(avctx, &s->picture2);
  675. return 0;
  676. }
  677. AVCodec ff_png_decoder = {
  678. .name = "png",
  679. .type = AVMEDIA_TYPE_VIDEO,
  680. .id = CODEC_ID_PNG,
  681. .priv_data_size = sizeof(PNGDecContext),
  682. .init = png_dec_init,
  683. .close = png_dec_end,
  684. .decode = decode_frame,
  685. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  686. .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
  687. };