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.

553 lines
18KB

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