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.

592 lines
19KB

  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. #include "avcodec.h"
  22. #include "bytestream.h"
  23. #include "png.h"
  24. #include "dsputil.h"
  25. /* TODO:
  26. * - add 2, 4 and 16 bit depth support
  27. */
  28. #include <zlib.h>
  29. //#define DEBUG
  30. typedef struct PNGDecContext {
  31. DSPContext dsp;
  32. const uint8_t *bytestream;
  33. const uint8_t *bytestream_start;
  34. const uint8_t *bytestream_end;
  35. AVFrame picture;
  36. int state;
  37. int width, height;
  38. int bit_depth;
  39. int color_type;
  40. int compression_type;
  41. int interlace_type;
  42. int filter_type;
  43. int channels;
  44. int bits_per_pixel;
  45. int bpp;
  46. uint8_t *image_buf;
  47. int image_linesize;
  48. uint32_t palette[256];
  49. uint8_t *crow_buf;
  50. uint8_t *last_row;
  51. uint8_t *tmp_row;
  52. int pass;
  53. int crow_size; /* compressed row size (include filter type) */
  54. int row_size; /* decompressed row size */
  55. int pass_row_size; /* decompress row size of the current pass */
  56. int y;
  57. z_stream zstream;
  58. } PNGDecContext;
  59. /* Mask to determine which y pixels can be written in a pass */
  60. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  61. 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
  62. };
  63. /* Mask to determine which pixels to overwrite while displaying */
  64. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  65. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  66. };
  67. /* NOTE: we try to construct a good looking image at each pass. width
  68. is the original image width. We also do pixel format conversion at
  69. this stage */
  70. static void png_put_interlaced_row(uint8_t *dst, int width,
  71. int bits_per_pixel, int pass,
  72. int color_type, const uint8_t *src)
  73. {
  74. int x, mask, dsp_mask, j, src_x, b, bpp;
  75. uint8_t *d;
  76. const uint8_t *s;
  77. mask = ff_png_pass_mask[pass];
  78. dsp_mask = png_pass_dsp_mask[pass];
  79. switch(bits_per_pixel) {
  80. case 1:
  81. /* we must initialize the line to zero before writing to it */
  82. if (pass == 0)
  83. memset(dst, 0, (width + 7) >> 3);
  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] |= b << (7 - j);
  90. }
  91. if ((mask << j) & 0x80)
  92. src_x++;
  93. }
  94. break;
  95. default:
  96. bpp = bits_per_pixel >> 3;
  97. d = dst;
  98. s = src;
  99. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  100. for(x = 0; x < width; x++) {
  101. j = x & 7;
  102. if ((dsp_mask << j) & 0x80) {
  103. *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
  104. }
  105. d += bpp;
  106. if ((mask << j) & 0x80)
  107. s += bpp;
  108. }
  109. } else {
  110. for(x = 0; x < width; x++) {
  111. j = x & 7;
  112. if ((dsp_mask << j) & 0x80) {
  113. memcpy(d, s, bpp);
  114. }
  115. d += bpp;
  116. if ((mask << j) & 0x80)
  117. s += bpp;
  118. }
  119. }
  120. break;
  121. }
  122. }
  123. void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
  124. {
  125. int i;
  126. for(i = 0; i < w; i++) {
  127. int a, b, c, p, pa, pb, pc;
  128. a = dst[i - bpp];
  129. b = top[i];
  130. c = top[i - bpp];
  131. p = b - c;
  132. pc = a - c;
  133. pa = abs(p);
  134. pb = abs(pc);
  135. pc = abs(p + pc);
  136. if (pa <= pb && pa <= pc)
  137. p = a;
  138. else if (pb <= pc)
  139. p = b;
  140. else
  141. p = c;
  142. dst[i] = p + src[i];
  143. }
  144. }
  145. #define UNROLL1(bpp, op) {\
  146. r = dst[0];\
  147. if(bpp >= 2) g = dst[1];\
  148. if(bpp >= 3) b = dst[2];\
  149. if(bpp >= 4) a = dst[3];\
  150. for(; i < size; i+=bpp) {\
  151. dst[i+0] = r = op(r, src[i+0], last[i+0]);\
  152. if(bpp == 1) continue;\
  153. dst[i+1] = g = op(g, src[i+1], last[i+1]);\
  154. if(bpp == 2) continue;\
  155. dst[i+2] = b = op(b, src[i+2], last[i+2]);\
  156. if(bpp == 3) continue;\
  157. dst[i+3] = a = op(a, src[i+3], last[i+3]);\
  158. }\
  159. }
  160. #define UNROLL_FILTER(op)\
  161. if(bpp == 1) UNROLL1(1, op)\
  162. else if(bpp == 2) UNROLL1(2, op)\
  163. else if(bpp == 3) UNROLL1(3, op)\
  164. else if(bpp == 4) UNROLL1(4, op)\
  165. /* NOTE: 'dst' can be equal to 'last' */
  166. static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
  167. uint8_t *src, uint8_t *last, int size, int bpp)
  168. {
  169. int i, p, r, g, b, a;
  170. switch(filter_type) {
  171. case PNG_FILTER_VALUE_NONE:
  172. memcpy(dst, src, size);
  173. break;
  174. case PNG_FILTER_VALUE_SUB:
  175. for(i = 0; i < bpp; i++) {
  176. dst[i] = src[i];
  177. }
  178. if(bpp == 4) {
  179. p = *(int*)dst;
  180. for(; i < size; i+=bpp) {
  181. int s = *(int*)(src+i);
  182. p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
  183. *(int*)(dst+i) = p;
  184. }
  185. } else {
  186. #define OP_SUB(x,s,l) x+s
  187. UNROLL_FILTER(OP_SUB);
  188. }
  189. break;
  190. case PNG_FILTER_VALUE_UP:
  191. dsp->add_bytes_l2(dst, src, last, size);
  192. break;
  193. case PNG_FILTER_VALUE_AVG:
  194. for(i = 0; i < bpp; i++) {
  195. p = (last[i] >> 1);
  196. dst[i] = p + src[i];
  197. }
  198. #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
  199. UNROLL_FILTER(OP_AVG);
  200. break;
  201. case PNG_FILTER_VALUE_PAETH:
  202. for(i = 0; i < bpp; i++) {
  203. p = last[i];
  204. dst[i] = p + src[i];
  205. }
  206. if(bpp > 1 && size > 4) {
  207. // would write off the end of the array if we let it process the last pixel with bpp=3
  208. int w = bpp==4 ? size : size-3;
  209. dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
  210. i = w;
  211. }
  212. ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
  213. break;
  214. }
  215. }
  216. static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width)
  217. {
  218. int j;
  219. unsigned int r, g, b, a;
  220. for(j = 0;j < width; j++) {
  221. r = src[0];
  222. g = src[1];
  223. b = src[2];
  224. a = src[3];
  225. *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
  226. dst += 4;
  227. src += 4;
  228. }
  229. }
  230. /* process exactly one decompressed row */
  231. static void png_handle_row(PNGDecContext *s)
  232. {
  233. uint8_t *ptr, *last_row;
  234. int got_line;
  235. if (!s->interlace_type) {
  236. ptr = s->image_buf + s->image_linesize * s->y;
  237. /* need to swap bytes correctly for RGB_ALPHA */
  238. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  239. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  240. s->last_row, s->row_size, s->bpp);
  241. convert_to_rgb32(ptr, s->tmp_row, s->width);
  242. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  243. } else {
  244. /* in normal case, we avoid one copy */
  245. if (s->y == 0)
  246. last_row = s->last_row;
  247. else
  248. last_row = ptr - s->image_linesize;
  249. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  250. last_row, s->row_size, s->bpp);
  251. }
  252. s->y++;
  253. if (s->y == s->height) {
  254. s->state |= PNG_ALLIMAGE;
  255. }
  256. } else {
  257. got_line = 0;
  258. for(;;) {
  259. ptr = s->image_buf + s->image_linesize * s->y;
  260. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  261. /* if we already read one row, it is time to stop to
  262. wait for the next one */
  263. if (got_line)
  264. break;
  265. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  266. s->last_row, s->pass_row_size, s->bpp);
  267. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  268. got_line = 1;
  269. }
  270. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  271. /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
  272. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  273. s->color_type, s->last_row);
  274. }
  275. s->y++;
  276. if (s->y == s->height) {
  277. for(;;) {
  278. if (s->pass == NB_PASSES - 1) {
  279. s->state |= PNG_ALLIMAGE;
  280. goto the_end;
  281. } else {
  282. s->pass++;
  283. s->y = 0;
  284. s->pass_row_size = ff_png_pass_row_size(s->pass,
  285. s->bits_per_pixel,
  286. s->width);
  287. s->crow_size = s->pass_row_size + 1;
  288. if (s->pass_row_size != 0)
  289. break;
  290. /* skip pass if empty row */
  291. }
  292. }
  293. }
  294. }
  295. the_end: ;
  296. }
  297. }
  298. static int png_decode_idat(PNGDecContext *s, int length)
  299. {
  300. int ret;
  301. s->zstream.avail_in = length;
  302. s->zstream.next_in = s->bytestream;
  303. s->bytestream += length;
  304. if(s->bytestream > s->bytestream_end)
  305. return -1;
  306. /* decode one line if possible */
  307. while (s->zstream.avail_in > 0) {
  308. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  309. if (ret != Z_OK && ret != Z_STREAM_END) {
  310. return -1;
  311. }
  312. if (s->zstream.avail_out == 0) {
  313. if (!(s->state & PNG_ALLIMAGE)) {
  314. png_handle_row(s);
  315. }
  316. s->zstream.avail_out = s->crow_size;
  317. s->zstream.next_out = s->crow_buf;
  318. }
  319. }
  320. return 0;
  321. }
  322. static int decode_frame(AVCodecContext *avctx,
  323. void *data, int *data_size,
  324. const uint8_t *buf, int buf_size)
  325. {
  326. PNGDecContext * const s = avctx->priv_data;
  327. AVFrame *picture = data;
  328. AVFrame * const p= (AVFrame*)&s->picture;
  329. uint32_t tag, length;
  330. int ret, crc;
  331. s->bytestream_start=
  332. s->bytestream= buf;
  333. s->bytestream_end= buf + buf_size;
  334. /* check signature */
  335. if (memcmp(s->bytestream, ff_pngsig, 8) != 0)
  336. return -1;
  337. s->bytestream+= 8;
  338. s->y=
  339. s->state=0;
  340. // memset(s, 0, sizeof(PNGDecContext));
  341. /* init the zlib */
  342. s->zstream.zalloc = ff_png_zalloc;
  343. s->zstream.zfree = ff_png_zfree;
  344. s->zstream.opaque = NULL;
  345. ret = inflateInit(&s->zstream);
  346. if (ret != Z_OK)
  347. return -1;
  348. for(;;) {
  349. int tag32;
  350. if (s->bytestream >= s->bytestream_end)
  351. goto fail;
  352. length = bytestream_get_be32(&s->bytestream);
  353. if (length > 0x7fffffff)
  354. goto fail;
  355. tag32 = bytestream_get_be32(&s->bytestream);
  356. tag = bswap_32(tag32);
  357. #ifdef DEBUG
  358. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  359. (tag & 0xff),
  360. ((tag >> 8) & 0xff),
  361. ((tag >> 16) & 0xff),
  362. ((tag >> 24) & 0xff), length);
  363. #endif
  364. switch(tag) {
  365. case MKTAG('I', 'H', 'D', 'R'):
  366. if (length != 13)
  367. goto fail;
  368. s->width = bytestream_get_be32(&s->bytestream);
  369. s->height = bytestream_get_be32(&s->bytestream);
  370. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  371. s->width= s->height= 0;
  372. goto fail;
  373. }
  374. s->bit_depth = *s->bytestream++;
  375. s->color_type = *s->bytestream++;
  376. s->compression_type = *s->bytestream++;
  377. s->filter_type = *s->bytestream++;
  378. s->interlace_type = *s->bytestream++;
  379. crc = bytestream_get_be32(&s->bytestream);
  380. s->state |= PNG_IHDR;
  381. #ifdef DEBUG
  382. 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",
  383. s->width, s->height, s->bit_depth, s->color_type,
  384. s->compression_type, s->filter_type, s->interlace_type);
  385. #endif
  386. break;
  387. case MKTAG('I', 'D', 'A', 'T'):
  388. if (!(s->state & PNG_IHDR))
  389. goto fail;
  390. if (!(s->state & PNG_IDAT)) {
  391. /* init image info */
  392. avctx->width = s->width;
  393. avctx->height = s->height;
  394. s->channels = ff_png_get_nb_channels(s->color_type);
  395. s->bits_per_pixel = s->bit_depth * s->channels;
  396. s->bpp = (s->bits_per_pixel + 7) >> 3;
  397. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  398. if (s->bit_depth == 8 &&
  399. s->color_type == PNG_COLOR_TYPE_RGB) {
  400. avctx->pix_fmt = PIX_FMT_RGB24;
  401. } else if (s->bit_depth == 8 &&
  402. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  403. avctx->pix_fmt = PIX_FMT_RGB32;
  404. } else if (s->bit_depth == 8 &&
  405. s->color_type == PNG_COLOR_TYPE_GRAY) {
  406. avctx->pix_fmt = PIX_FMT_GRAY8;
  407. } else if (s->bit_depth == 16 &&
  408. s->color_type == PNG_COLOR_TYPE_GRAY) {
  409. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  410. } else if (s->bit_depth == 1 &&
  411. s->color_type == PNG_COLOR_TYPE_GRAY) {
  412. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  413. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  414. avctx->pix_fmt = PIX_FMT_PAL8;
  415. } else {
  416. goto fail;
  417. }
  418. if(p->data[0])
  419. avctx->release_buffer(avctx, p);
  420. p->reference= 0;
  421. if(avctx->get_buffer(avctx, p) < 0){
  422. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  423. goto fail;
  424. }
  425. p->pict_type= FF_I_TYPE;
  426. p->key_frame= 1;
  427. p->interlaced_frame = !!s->interlace_type;
  428. /* compute the compressed row size */
  429. if (!s->interlace_type) {
  430. s->crow_size = s->row_size + 1;
  431. } else {
  432. s->pass = 0;
  433. s->pass_row_size = ff_png_pass_row_size(s->pass,
  434. s->bits_per_pixel,
  435. s->width);
  436. s->crow_size = s->pass_row_size + 1;
  437. }
  438. #ifdef DEBUG
  439. av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
  440. s->row_size, s->crow_size);
  441. #endif
  442. s->image_buf = p->data[0];
  443. s->image_linesize = p->linesize[0];
  444. /* copy the palette if needed */
  445. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  446. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  447. /* empty row is used if differencing to the first row */
  448. s->last_row = av_mallocz(s->row_size);
  449. if (!s->last_row)
  450. goto fail;
  451. if (s->interlace_type ||
  452. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  453. s->tmp_row = av_malloc(s->row_size);
  454. if (!s->tmp_row)
  455. goto fail;
  456. }
  457. /* compressed row */
  458. s->crow_buf = av_malloc(s->row_size + 1);
  459. if (!s->crow_buf)
  460. goto fail;
  461. s->zstream.avail_out = s->crow_size;
  462. s->zstream.next_out = s->crow_buf;
  463. }
  464. s->state |= PNG_IDAT;
  465. if (png_decode_idat(s, length) < 0)
  466. goto fail;
  467. /* skip crc */
  468. crc = bytestream_get_be32(&s->bytestream);
  469. break;
  470. case MKTAG('P', 'L', 'T', 'E'):
  471. {
  472. int n, i, r, g, b;
  473. if ((length % 3) != 0 || length > 256 * 3)
  474. goto skip_tag;
  475. /* read the palette */
  476. n = length / 3;
  477. for(i=0;i<n;i++) {
  478. r = *s->bytestream++;
  479. g = *s->bytestream++;
  480. b = *s->bytestream++;
  481. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  482. }
  483. for(;i<256;i++) {
  484. s->palette[i] = (0xff << 24);
  485. }
  486. s->state |= PNG_PLTE;
  487. crc = bytestream_get_be32(&s->bytestream);
  488. }
  489. break;
  490. case MKTAG('t', 'R', 'N', 'S'):
  491. {
  492. int v, i;
  493. /* read the transparency. XXX: Only palette mode supported */
  494. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  495. length > 256 ||
  496. !(s->state & PNG_PLTE))
  497. goto skip_tag;
  498. for(i=0;i<length;i++) {
  499. v = *s->bytestream++;
  500. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  501. }
  502. crc = bytestream_get_be32(&s->bytestream);
  503. }
  504. break;
  505. case MKTAG('I', 'E', 'N', 'D'):
  506. if (!(s->state & PNG_ALLIMAGE))
  507. goto fail;
  508. crc = bytestream_get_be32(&s->bytestream);
  509. goto exit_loop;
  510. default:
  511. /* skip tag */
  512. skip_tag:
  513. s->bytestream += length + 4;
  514. break;
  515. }
  516. }
  517. exit_loop:
  518. *picture= *(AVFrame*)&s->picture;
  519. *data_size = sizeof(AVPicture);
  520. ret = s->bytestream - s->bytestream_start;
  521. the_end:
  522. inflateEnd(&s->zstream);
  523. av_freep(&s->crow_buf);
  524. av_freep(&s->last_row);
  525. av_freep(&s->tmp_row);
  526. return ret;
  527. fail:
  528. ret = -1;
  529. goto the_end;
  530. }
  531. static int png_dec_init(AVCodecContext *avctx){
  532. PNGDecContext *s = avctx->priv_data;
  533. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  534. avctx->coded_frame= (AVFrame*)&s->picture;
  535. dsputil_init(&s->dsp, avctx);
  536. return 0;
  537. }
  538. AVCodec png_decoder = {
  539. "png",
  540. CODEC_TYPE_VIDEO,
  541. CODEC_ID_PNG,
  542. sizeof(PNGDecContext),
  543. png_dec_init,
  544. NULL,
  545. NULL, //decode_end,
  546. decode_frame,
  547. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  548. NULL
  549. };