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.

770 lines
26KB

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