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.

623 lines
20KB

  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 av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
  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. if(loco) {
  226. r = (r+g)&0xff;
  227. b = (b+g)&0xff;
  228. }
  229. *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
  230. dst += 4;
  231. src += 4;
  232. }
  233. }
  234. static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
  235. {
  236. if(loco)
  237. convert_to_rgb32_loco(dst, src, width, 1);
  238. else
  239. convert_to_rgb32_loco(dst, src, width, 0);
  240. }
  241. static void deloco_rgb24(uint8_t *dst, int size)
  242. {
  243. int i;
  244. for(i=0; i<size; i+=3) {
  245. int g = dst[i+1];
  246. dst[i+0] += g;
  247. dst[i+2] += g;
  248. }
  249. }
  250. /* process exactly one decompressed row */
  251. static void png_handle_row(PNGDecContext *s)
  252. {
  253. uint8_t *ptr, *last_row;
  254. int got_line;
  255. if (!s->interlace_type) {
  256. ptr = s->image_buf + s->image_linesize * s->y;
  257. /* need to swap bytes correctly for RGB_ALPHA */
  258. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  259. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  260. s->last_row, s->row_size, s->bpp);
  261. convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
  262. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  263. } else {
  264. /* in normal case, we avoid one copy */
  265. if (s->y == 0)
  266. last_row = s->last_row;
  267. else
  268. last_row = ptr - s->image_linesize;
  269. png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
  270. last_row, s->row_size, s->bpp);
  271. }
  272. /* loco lags by 1 row so that it doesn't interfere with top prediction */
  273. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  274. s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
  275. deloco_rgb24(ptr - s->image_linesize, s->row_size);
  276. s->y++;
  277. if (s->y == s->height) {
  278. s->state |= PNG_ALLIMAGE;
  279. if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
  280. s->color_type == PNG_COLOR_TYPE_RGB)
  281. deloco_rgb24(ptr, s->row_size);
  282. }
  283. } else {
  284. got_line = 0;
  285. for(;;) {
  286. ptr = s->image_buf + s->image_linesize * s->y;
  287. if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  288. /* if we already read one row, it is time to stop to
  289. wait for the next one */
  290. if (got_line)
  291. break;
  292. png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  293. s->last_row, s->pass_row_size, s->bpp);
  294. FFSWAP(uint8_t*, s->last_row, s->tmp_row);
  295. got_line = 1;
  296. }
  297. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  298. /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
  299. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  300. s->color_type, s->last_row);
  301. }
  302. s->y++;
  303. if (s->y == s->height) {
  304. for(;;) {
  305. if (s->pass == NB_PASSES - 1) {
  306. s->state |= PNG_ALLIMAGE;
  307. goto the_end;
  308. } else {
  309. s->pass++;
  310. s->y = 0;
  311. s->pass_row_size = ff_png_pass_row_size(s->pass,
  312. s->bits_per_pixel,
  313. s->width);
  314. s->crow_size = s->pass_row_size + 1;
  315. if (s->pass_row_size != 0)
  316. break;
  317. /* skip pass if empty row */
  318. }
  319. }
  320. }
  321. }
  322. the_end: ;
  323. }
  324. }
  325. static int png_decode_idat(PNGDecContext *s, int length)
  326. {
  327. int ret;
  328. s->zstream.avail_in = length;
  329. s->zstream.next_in = s->bytestream;
  330. s->bytestream += length;
  331. if(s->bytestream > s->bytestream_end)
  332. return -1;
  333. /* decode one line if possible */
  334. while (s->zstream.avail_in > 0) {
  335. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  336. if (ret != Z_OK && ret != Z_STREAM_END) {
  337. return -1;
  338. }
  339. if (s->zstream.avail_out == 0) {
  340. if (!(s->state & PNG_ALLIMAGE)) {
  341. png_handle_row(s);
  342. }
  343. s->zstream.avail_out = s->crow_size;
  344. s->zstream.next_out = s->crow_buf;
  345. }
  346. }
  347. return 0;
  348. }
  349. static int decode_frame(AVCodecContext *avctx,
  350. void *data, int *data_size,
  351. const uint8_t *buf, int buf_size)
  352. {
  353. PNGDecContext * const s = avctx->priv_data;
  354. AVFrame *picture = data;
  355. AVFrame * const p= &s->picture;
  356. uint32_t tag, length;
  357. int ret, crc;
  358. s->bytestream_start=
  359. s->bytestream= buf;
  360. s->bytestream_end= buf + buf_size;
  361. /* check signature */
  362. if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
  363. memcmp(s->bytestream, ff_mngsig, 8) != 0)
  364. return -1;
  365. s->bytestream+= 8;
  366. s->y=
  367. s->state=0;
  368. // memset(s, 0, sizeof(PNGDecContext));
  369. /* init the zlib */
  370. s->zstream.zalloc = ff_png_zalloc;
  371. s->zstream.zfree = ff_png_zfree;
  372. s->zstream.opaque = NULL;
  373. ret = inflateInit(&s->zstream);
  374. if (ret != Z_OK)
  375. return -1;
  376. for(;;) {
  377. int tag32;
  378. if (s->bytestream >= s->bytestream_end)
  379. goto fail;
  380. length = bytestream_get_be32(&s->bytestream);
  381. if (length > 0x7fffffff)
  382. goto fail;
  383. tag32 = bytestream_get_be32(&s->bytestream);
  384. tag = bswap_32(tag32);
  385. #ifdef DEBUG
  386. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  387. (tag & 0xff),
  388. ((tag >> 8) & 0xff),
  389. ((tag >> 16) & 0xff),
  390. ((tag >> 24) & 0xff), length);
  391. #endif
  392. switch(tag) {
  393. case MKTAG('I', 'H', 'D', 'R'):
  394. if (length != 13)
  395. goto fail;
  396. s->width = bytestream_get_be32(&s->bytestream);
  397. s->height = bytestream_get_be32(&s->bytestream);
  398. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  399. s->width= s->height= 0;
  400. goto fail;
  401. }
  402. s->bit_depth = *s->bytestream++;
  403. s->color_type = *s->bytestream++;
  404. s->compression_type = *s->bytestream++;
  405. s->filter_type = *s->bytestream++;
  406. s->interlace_type = *s->bytestream++;
  407. crc = bytestream_get_be32(&s->bytestream);
  408. s->state |= PNG_IHDR;
  409. #ifdef DEBUG
  410. 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",
  411. s->width, s->height, s->bit_depth, s->color_type,
  412. s->compression_type, s->filter_type, s->interlace_type);
  413. #endif
  414. break;
  415. case MKTAG('I', 'D', 'A', 'T'):
  416. if (!(s->state & PNG_IHDR))
  417. goto fail;
  418. if (!(s->state & PNG_IDAT)) {
  419. /* init image info */
  420. avctx->width = s->width;
  421. avctx->height = s->height;
  422. s->channels = ff_png_get_nb_channels(s->color_type);
  423. s->bits_per_pixel = s->bit_depth * s->channels;
  424. s->bpp = (s->bits_per_pixel + 7) >> 3;
  425. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  426. if (s->bit_depth == 8 &&
  427. s->color_type == PNG_COLOR_TYPE_RGB) {
  428. avctx->pix_fmt = PIX_FMT_RGB24;
  429. } else if (s->bit_depth == 8 &&
  430. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  431. avctx->pix_fmt = PIX_FMT_RGB32;
  432. } else if (s->bit_depth == 8 &&
  433. s->color_type == PNG_COLOR_TYPE_GRAY) {
  434. avctx->pix_fmt = PIX_FMT_GRAY8;
  435. } else if (s->bit_depth == 16 &&
  436. s->color_type == PNG_COLOR_TYPE_GRAY) {
  437. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  438. } else if (s->bit_depth == 1 &&
  439. s->color_type == PNG_COLOR_TYPE_GRAY) {
  440. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  441. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  442. avctx->pix_fmt = PIX_FMT_PAL8;
  443. } else {
  444. goto fail;
  445. }
  446. if(p->data[0])
  447. avctx->release_buffer(avctx, p);
  448. p->reference= 0;
  449. if(avctx->get_buffer(avctx, p) < 0){
  450. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  451. goto fail;
  452. }
  453. p->pict_type= FF_I_TYPE;
  454. p->key_frame= 1;
  455. p->interlaced_frame = !!s->interlace_type;
  456. /* compute the compressed row size */
  457. if (!s->interlace_type) {
  458. s->crow_size = s->row_size + 1;
  459. } else {
  460. s->pass = 0;
  461. s->pass_row_size = ff_png_pass_row_size(s->pass,
  462. s->bits_per_pixel,
  463. s->width);
  464. s->crow_size = s->pass_row_size + 1;
  465. }
  466. #ifdef DEBUG
  467. av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
  468. s->row_size, s->crow_size);
  469. #endif
  470. s->image_buf = p->data[0];
  471. s->image_linesize = p->linesize[0];
  472. /* copy the palette if needed */
  473. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  474. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  475. /* empty row is used if differencing to the first row */
  476. s->last_row = av_mallocz(s->row_size);
  477. if (!s->last_row)
  478. goto fail;
  479. if (s->interlace_type ||
  480. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  481. s->tmp_row = av_malloc(s->row_size);
  482. if (!s->tmp_row)
  483. goto fail;
  484. }
  485. /* compressed row */
  486. s->crow_buf = av_malloc(s->row_size + 1);
  487. if (!s->crow_buf)
  488. goto fail;
  489. s->zstream.avail_out = s->crow_size;
  490. s->zstream.next_out = s->crow_buf;
  491. }
  492. s->state |= PNG_IDAT;
  493. if (png_decode_idat(s, length) < 0)
  494. goto fail;
  495. /* skip crc */
  496. crc = bytestream_get_be32(&s->bytestream);
  497. break;
  498. case MKTAG('P', 'L', 'T', 'E'):
  499. {
  500. int n, i, r, g, b;
  501. if ((length % 3) != 0 || length > 256 * 3)
  502. goto skip_tag;
  503. /* read the palette */
  504. n = length / 3;
  505. for(i=0;i<n;i++) {
  506. r = *s->bytestream++;
  507. g = *s->bytestream++;
  508. b = *s->bytestream++;
  509. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  510. }
  511. for(;i<256;i++) {
  512. s->palette[i] = (0xff << 24);
  513. }
  514. s->state |= PNG_PLTE;
  515. crc = bytestream_get_be32(&s->bytestream);
  516. }
  517. break;
  518. case MKTAG('t', 'R', 'N', 'S'):
  519. {
  520. int v, i;
  521. /* read the transparency. XXX: Only palette mode supported */
  522. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  523. length > 256 ||
  524. !(s->state & PNG_PLTE))
  525. goto skip_tag;
  526. for(i=0;i<length;i++) {
  527. v = *s->bytestream++;
  528. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  529. }
  530. crc = bytestream_get_be32(&s->bytestream);
  531. }
  532. break;
  533. case MKTAG('I', 'E', 'N', 'D'):
  534. if (!(s->state & PNG_ALLIMAGE))
  535. goto fail;
  536. crc = bytestream_get_be32(&s->bytestream);
  537. goto exit_loop;
  538. default:
  539. /* skip tag */
  540. skip_tag:
  541. s->bytestream += length + 4;
  542. break;
  543. }
  544. }
  545. exit_loop:
  546. *picture= s->picture;
  547. *data_size = sizeof(AVFrame);
  548. ret = s->bytestream - s->bytestream_start;
  549. the_end:
  550. inflateEnd(&s->zstream);
  551. av_freep(&s->crow_buf);
  552. av_freep(&s->last_row);
  553. av_freep(&s->tmp_row);
  554. return ret;
  555. fail:
  556. ret = -1;
  557. goto the_end;
  558. }
  559. static av_cold int png_dec_init(AVCodecContext *avctx){
  560. PNGDecContext *s = avctx->priv_data;
  561. avcodec_get_frame_defaults(&s->picture);
  562. avctx->coded_frame= &s->picture;
  563. dsputil_init(&s->dsp, avctx);
  564. return 0;
  565. }
  566. AVCodec png_decoder = {
  567. "png",
  568. CODEC_TYPE_VIDEO,
  569. CODEC_ID_PNG,
  570. sizeof(PNGDecContext),
  571. png_dec_init,
  572. NULL,
  573. NULL, //decode_end,
  574. decode_frame,
  575. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  576. NULL,
  577. .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
  578. };