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.

719 lines
23KB

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