png.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. /* TODO:
  23. * - add 2, 4 and 16 bit depth support
  24. * - use filters when generating a png (better compression)
  25. */
  26. #include <zlib.h>
  27. //#define DEBUG
  28. #define PNG_COLOR_MASK_PALETTE 1
  29. #define PNG_COLOR_MASK_COLOR 2
  30. #define PNG_COLOR_MASK_ALPHA 4
  31. #define PNG_COLOR_TYPE_GRAY 0
  32. #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
  33. #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
  34. #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
  35. #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
  36. #define PNG_FILTER_VALUE_NONE 0
  37. #define PNG_FILTER_VALUE_SUB 1
  38. #define PNG_FILTER_VALUE_UP 2
  39. #define PNG_FILTER_VALUE_AVG 3
  40. #define PNG_FILTER_VALUE_PAETH 4
  41. #define PNG_IHDR 0x0001
  42. #define PNG_IDAT 0x0002
  43. #define PNG_ALLIMAGE 0x0004
  44. #define PNG_PLTE 0x0008
  45. #define NB_PASSES 7
  46. #define IOBUF_SIZE 4096
  47. typedef struct PNGContext {
  48. uint8_t *bytestream;
  49. uint8_t *bytestream_start;
  50. uint8_t *bytestream_end;
  51. AVFrame picture;
  52. int state;
  53. int width, height;
  54. int bit_depth;
  55. int color_type;
  56. int compression_type;
  57. int interlace_type;
  58. int filter_type;
  59. int channels;
  60. int bits_per_pixel;
  61. int bpp;
  62. uint8_t *image_buf;
  63. int image_linesize;
  64. uint32_t palette[256];
  65. uint8_t *crow_buf;
  66. uint8_t *last_row;
  67. uint8_t *tmp_row;
  68. int pass;
  69. int crow_size; /* compressed row size (include filter type) */
  70. int row_size; /* decompressed row size */
  71. int pass_row_size; /* decompress row size of the current pass */
  72. int y;
  73. z_stream zstream;
  74. uint8_t buf[IOBUF_SIZE];
  75. } PNGContext;
  76. static unsigned int get32(uint8_t **b){
  77. (*b) += 4;
  78. return ((*b)[-4]<<24) + ((*b)[-3]<<16) + ((*b)[-2]<<8) + (*b)[-1];
  79. }
  80. #ifdef CONFIG_ENCODERS
  81. static void put32(uint8_t **b, unsigned int v){
  82. *(*b)++= v>>24;
  83. *(*b)++= v>>16;
  84. *(*b)++= v>>8;
  85. *(*b)++= v;
  86. }
  87. #endif
  88. static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  89. /* Mask to determine which y pixels are valid in a pass */
  90. static const uint8_t png_pass_ymask[NB_PASSES] = {
  91. 0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
  92. };
  93. /* Mask to determine which y pixels can be written in a pass */
  94. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  95. 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
  96. };
  97. /* minimum x value */
  98. static const uint8_t png_pass_xmin[NB_PASSES] = {
  99. 0, 4, 0, 2, 0, 1, 0
  100. };
  101. /* x shift to get row width */
  102. static const uint8_t png_pass_xshift[NB_PASSES] = {
  103. 3, 3, 2, 2, 1, 1, 0
  104. };
  105. /* Mask to determine which pixels are valid in a pass */
  106. static const uint8_t png_pass_mask[NB_PASSES] = {
  107. 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
  108. };
  109. /* Mask to determine which pixels to overwrite while displaying */
  110. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  111. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  112. };
  113. #if 0
  114. static int png_probe(AVProbeData *pd)
  115. {
  116. if (pd->buf_size >= 8 &&
  117. memcmp(pd->buf, pngsig, 8) == 0)
  118. return AVPROBE_SCORE_MAX;
  119. else
  120. return 0;
  121. }
  122. #endif
  123. static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
  124. {
  125. if(items >= UINT_MAX / size)
  126. return NULL;
  127. return av_malloc(items * size);
  128. }
  129. static void png_zfree(void *opaque, void *ptr)
  130. {
  131. av_free(ptr);
  132. }
  133. static int png_get_nb_channels(int color_type)
  134. {
  135. int channels;
  136. channels = 1;
  137. if ((color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
  138. PNG_COLOR_MASK_COLOR)
  139. channels = 3;
  140. if (color_type & PNG_COLOR_MASK_ALPHA)
  141. channels++;
  142. return channels;
  143. }
  144. /* compute the row size of an interleaved pass */
  145. static int png_pass_row_size(int pass, int bits_per_pixel, int width)
  146. {
  147. int shift, xmin, pass_width;
  148. xmin = png_pass_xmin[pass];
  149. if (width <= xmin)
  150. return 0;
  151. shift = png_pass_xshift[pass];
  152. pass_width = (width - xmin + (1 << shift) - 1) >> shift;
  153. return (pass_width * bits_per_pixel + 7) >> 3;
  154. }
  155. /* NOTE: we try to construct a good looking image at each pass. width
  156. is the original image width. We also do pixel format convertion at
  157. this stage */
  158. static void png_put_interlaced_row(uint8_t *dst, int width,
  159. int bits_per_pixel, int pass,
  160. int color_type, const uint8_t *src)
  161. {
  162. int x, mask, dsp_mask, j, src_x, b, bpp;
  163. uint8_t *d;
  164. const uint8_t *s;
  165. mask = png_pass_mask[pass];
  166. dsp_mask = png_pass_dsp_mask[pass];
  167. switch(bits_per_pixel) {
  168. case 1:
  169. /* we must intialize the line to zero before writing to it */
  170. if (pass == 0)
  171. memset(dst, 0, (width + 7) >> 3);
  172. src_x = 0;
  173. for(x = 0; x < width; x++) {
  174. j = (x & 7);
  175. if ((dsp_mask << j) & 0x80) {
  176. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  177. dst[x >> 3] |= b << (7 - j);
  178. }
  179. if ((mask << j) & 0x80)
  180. src_x++;
  181. }
  182. break;
  183. default:
  184. bpp = bits_per_pixel >> 3;
  185. d = dst;
  186. s = src;
  187. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  188. for(x = 0; x < width; x++) {
  189. j = x & 7;
  190. if ((dsp_mask << j) & 0x80) {
  191. *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
  192. }
  193. d += bpp;
  194. if ((mask << j) & 0x80)
  195. s += bpp;
  196. }
  197. } else {
  198. for(x = 0; x < width; x++) {
  199. j = x & 7;
  200. if ((dsp_mask << j) & 0x80) {
  201. memcpy(d, s, bpp);
  202. }
  203. d += bpp;
  204. if ((mask << j) & 0x80)
  205. s += bpp;
  206. }
  207. }
  208. break;
  209. }
  210. }
  211. #ifdef CONFIG_ENCODERS
  212. static void png_get_interlaced_row(uint8_t *dst, int row_size,
  213. int bits_per_pixel, int pass,
  214. const uint8_t *src, int width)
  215. {
  216. int x, mask, dst_x, j, b, bpp;
  217. uint8_t *d;
  218. const uint8_t *s;
  219. mask = png_pass_mask[pass];
  220. switch(bits_per_pixel) {
  221. case 1:
  222. memset(dst, 0, row_size);
  223. dst_x = 0;
  224. for(x = 0; x < width; x++) {
  225. j = (x & 7);
  226. if ((mask << j) & 0x80) {
  227. b = (src[x >> 3] >> (7 - j)) & 1;
  228. dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
  229. dst_x++;
  230. }
  231. }
  232. break;
  233. default:
  234. bpp = bits_per_pixel >> 3;
  235. d = dst;
  236. s = src;
  237. for(x = 0; x < width; x++) {
  238. j = x & 7;
  239. if ((mask << j) & 0x80) {
  240. memcpy(d, s, bpp);
  241. d += bpp;
  242. }
  243. s += bpp;
  244. }
  245. break;
  246. }
  247. }
  248. #endif
  249. /* XXX: optimize */
  250. /* NOTE: 'dst' can be equal to 'last' */
  251. static void png_filter_row(uint8_t *dst, int filter_type,
  252. uint8_t *src, uint8_t *last, int size, int bpp)
  253. {
  254. int i, p;
  255. switch(filter_type) {
  256. case PNG_FILTER_VALUE_NONE:
  257. memcpy(dst, src, size);
  258. break;
  259. case PNG_FILTER_VALUE_SUB:
  260. for(i = 0; i < bpp; i++) {
  261. dst[i] = src[i];
  262. }
  263. for(i = bpp; i < size; i++) {
  264. p = dst[i - bpp];
  265. dst[i] = p + src[i];
  266. }
  267. break;
  268. case PNG_FILTER_VALUE_UP:
  269. for(i = 0; i < size; i++) {
  270. p = last[i];
  271. dst[i] = p + src[i];
  272. }
  273. break;
  274. case PNG_FILTER_VALUE_AVG:
  275. for(i = 0; i < bpp; i++) {
  276. p = (last[i] >> 1);
  277. dst[i] = p + src[i];
  278. }
  279. for(i = bpp; i < size; i++) {
  280. p = ((dst[i - bpp] + last[i]) >> 1);
  281. dst[i] = p + src[i];
  282. }
  283. break;
  284. case PNG_FILTER_VALUE_PAETH:
  285. for(i = 0; i < bpp; i++) {
  286. p = last[i];
  287. dst[i] = p + src[i];
  288. }
  289. for(i = bpp; i < size; i++) {
  290. int a, b, c, pa, pb, pc;
  291. a = dst[i - bpp];
  292. b = last[i];
  293. c = last[i - bpp];
  294. p = b - c;
  295. pc = a - c;
  296. pa = abs(p);
  297. pb = abs(pc);
  298. pc = abs(p + pc);
  299. if (pa <= pb && pa <= pc)
  300. p = a;
  301. else if (pb <= pc)
  302. p = b;
  303. else
  304. p = c;
  305. dst[i] = p + src[i];
  306. }
  307. break;
  308. }
  309. }
  310. #ifdef CONFIG_ENCODERS
  311. static void convert_from_rgb32(uint8_t *dst, const uint8_t *src, int width)
  312. {
  313. uint8_t *d;
  314. int j;
  315. unsigned int v;
  316. d = dst;
  317. for(j = 0; j < width; j++) {
  318. v = ((const uint32_t *)src)[j];
  319. d[0] = v >> 16;
  320. d[1] = v >> 8;
  321. d[2] = v;
  322. d[3] = v >> 24;
  323. d += 4;
  324. }
  325. }
  326. #endif
  327. #ifdef CONFIG_DECODERS
  328. static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width)
  329. {
  330. int j;
  331. unsigned int r, g, b, a;
  332. for(j = 0;j < width; j++) {
  333. r = src[0];
  334. g = src[1];
  335. b = src[2];
  336. a = src[3];
  337. *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
  338. dst += 4;
  339. src += 4;
  340. }
  341. }
  342. /* process exactly one decompressed row */
  343. static void png_handle_row(PNGContext *s)
  344. {
  345. uint8_t *ptr, *last_row;
  346. int got_line;
  347. if (!s->interlace_type) {
  348. ptr = s->image_buf + s->image_linesize * s->y;
  349. /* need to swap bytes correctly for RGB_ALPHA */
  350. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  351. png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  352. s->last_row, s->row_size, s->bpp);
  353. memcpy(s->last_row, s->tmp_row, s->row_size);
  354. convert_to_rgb32(ptr, s->tmp_row, s->width);
  355. } else {
  356. /* in normal case, we avoid one copy */
  357. if (s->y == 0)
  358. last_row = s->last_row;
  359. else
  360. last_row = ptr - s->image_linesize;
  361. png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
  362. last_row, s->row_size, s->bpp);
  363. }
  364. s->y++;
  365. if (s->y == s->height) {
  366. s->state |= PNG_ALLIMAGE;
  367. }
  368. } else {
  369. got_line = 0;
  370. for(;;) {
  371. ptr = s->image_buf + s->image_linesize * s->y;
  372. if ((png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  373. /* if we already read one row, it is time to stop to
  374. wait for the next one */
  375. if (got_line)
  376. break;
  377. png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  378. s->last_row, s->pass_row_size, s->bpp);
  379. memcpy(s->last_row, s->tmp_row, s->pass_row_size);
  380. got_line = 1;
  381. }
  382. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  383. /* NOTE: RGB32 is handled directly in png_put_interlaced_row */
  384. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  385. s->color_type, s->last_row);
  386. }
  387. s->y++;
  388. if (s->y == s->height) {
  389. for(;;) {
  390. if (s->pass == NB_PASSES - 1) {
  391. s->state |= PNG_ALLIMAGE;
  392. goto the_end;
  393. } else {
  394. s->pass++;
  395. s->y = 0;
  396. s->pass_row_size = png_pass_row_size(s->pass,
  397. s->bits_per_pixel,
  398. s->width);
  399. s->crow_size = s->pass_row_size + 1;
  400. if (s->pass_row_size != 0)
  401. break;
  402. /* skip pass if empty row */
  403. }
  404. }
  405. }
  406. }
  407. the_end: ;
  408. }
  409. }
  410. static int png_decode_idat(PNGContext *s, int length)
  411. {
  412. int ret;
  413. s->zstream.avail_in = length;
  414. s->zstream.next_in = s->bytestream;
  415. s->bytestream += length;
  416. if(s->bytestream > s->bytestream_end)
  417. return -1;
  418. /* decode one line if possible */
  419. while (s->zstream.avail_in > 0) {
  420. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  421. if (ret != Z_OK && ret != Z_STREAM_END) {
  422. return -1;
  423. }
  424. if (s->zstream.avail_out == 0) {
  425. if (!(s->state & PNG_ALLIMAGE)) {
  426. png_handle_row(s);
  427. }
  428. s->zstream.avail_out = s->crow_size;
  429. s->zstream.next_out = s->crow_buf;
  430. }
  431. }
  432. return 0;
  433. }
  434. static int decode_frame(AVCodecContext *avctx,
  435. void *data, int *data_size,
  436. uint8_t *buf, int buf_size)
  437. {
  438. PNGContext * const s = avctx->priv_data;
  439. AVFrame *picture = data;
  440. AVFrame * const p= (AVFrame*)&s->picture;
  441. uint32_t tag, length;
  442. int ret, crc;
  443. s->bytestream_start=
  444. s->bytestream= buf;
  445. s->bytestream_end= buf + buf_size;
  446. /* check signature */
  447. if (memcmp(s->bytestream, pngsig, 8) != 0)
  448. return -1;
  449. s->bytestream+= 8;
  450. s->y=
  451. s->state=0;
  452. // memset(s, 0, sizeof(PNGContext));
  453. /* init the zlib */
  454. s->zstream.zalloc = png_zalloc;
  455. s->zstream.zfree = png_zfree;
  456. s->zstream.opaque = NULL;
  457. ret = inflateInit(&s->zstream);
  458. if (ret != Z_OK)
  459. return -1;
  460. for(;;) {
  461. int tag32;
  462. if (s->bytestream >= s->bytestream_end)
  463. goto fail;
  464. length = get32(&s->bytestream);
  465. if (length > 0x7fffffff)
  466. goto fail;
  467. tag32 = get32(&s->bytestream);
  468. tag = bswap_32(tag32);
  469. #ifdef DEBUG
  470. av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
  471. (tag & 0xff),
  472. ((tag >> 8) & 0xff),
  473. ((tag >> 16) & 0xff),
  474. ((tag >> 24) & 0xff), length);
  475. #endif
  476. switch(tag) {
  477. case MKTAG('I', 'H', 'D', 'R'):
  478. if (length != 13)
  479. goto fail;
  480. s->width = get32(&s->bytestream);
  481. s->height = get32(&s->bytestream);
  482. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  483. s->width= s->height= 0;
  484. goto fail;
  485. }
  486. s->bit_depth = *s->bytestream++;
  487. s->color_type = *s->bytestream++;
  488. s->compression_type = *s->bytestream++;
  489. s->filter_type = *s->bytestream++;
  490. s->interlace_type = *s->bytestream++;
  491. crc = get32(&s->bytestream);
  492. s->state |= PNG_IHDR;
  493. #ifdef DEBUG
  494. 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",
  495. s->width, s->height, s->bit_depth, s->color_type,
  496. s->compression_type, s->filter_type, s->interlace_type);
  497. #endif
  498. break;
  499. case MKTAG('I', 'D', 'A', 'T'):
  500. if (!(s->state & PNG_IHDR))
  501. goto fail;
  502. if (!(s->state & PNG_IDAT)) {
  503. /* init image info */
  504. avctx->width = s->width;
  505. avctx->height = s->height;
  506. s->channels = png_get_nb_channels(s->color_type);
  507. s->bits_per_pixel = s->bit_depth * s->channels;
  508. s->bpp = (s->bits_per_pixel + 7) >> 3;
  509. s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
  510. if (s->bit_depth == 8 &&
  511. s->color_type == PNG_COLOR_TYPE_RGB) {
  512. avctx->pix_fmt = PIX_FMT_RGB24;
  513. } else if (s->bit_depth == 8 &&
  514. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  515. avctx->pix_fmt = PIX_FMT_RGB32;
  516. } else if (s->bit_depth == 8 &&
  517. s->color_type == PNG_COLOR_TYPE_GRAY) {
  518. avctx->pix_fmt = PIX_FMT_GRAY8;
  519. } else if (s->bit_depth == 16 &&
  520. s->color_type == PNG_COLOR_TYPE_GRAY) {
  521. avctx->pix_fmt = PIX_FMT_GRAY16BE;
  522. } else if (s->bit_depth == 1 &&
  523. s->color_type == PNG_COLOR_TYPE_GRAY) {
  524. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  525. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  526. avctx->pix_fmt = PIX_FMT_PAL8;
  527. } else {
  528. goto fail;
  529. }
  530. if(p->data[0])
  531. avctx->release_buffer(avctx, p);
  532. p->reference= 0;
  533. if(avctx->get_buffer(avctx, p) < 0){
  534. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  535. goto fail;
  536. }
  537. p->pict_type= FF_I_TYPE;
  538. p->key_frame= 1;
  539. p->interlaced_frame = !!s->interlace_type;
  540. /* compute the compressed row size */
  541. if (!s->interlace_type) {
  542. s->crow_size = s->row_size + 1;
  543. } else {
  544. s->pass = 0;
  545. s->pass_row_size = png_pass_row_size(s->pass,
  546. s->bits_per_pixel,
  547. s->width);
  548. s->crow_size = s->pass_row_size + 1;
  549. }
  550. #ifdef DEBUG
  551. av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
  552. s->row_size, s->crow_size);
  553. #endif
  554. s->image_buf = p->data[0];
  555. s->image_linesize = p->linesize[0];
  556. /* copy the palette if needed */
  557. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  558. memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
  559. /* empty row is used if differencing to the first row */
  560. s->last_row = av_mallocz(s->row_size);
  561. if (!s->last_row)
  562. goto fail;
  563. if (s->interlace_type ||
  564. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  565. s->tmp_row = av_malloc(s->row_size);
  566. if (!s->tmp_row)
  567. goto fail;
  568. }
  569. /* compressed row */
  570. s->crow_buf = av_malloc(s->row_size + 1);
  571. if (!s->crow_buf)
  572. goto fail;
  573. s->zstream.avail_out = s->crow_size;
  574. s->zstream.next_out = s->crow_buf;
  575. }
  576. s->state |= PNG_IDAT;
  577. if (png_decode_idat(s, length) < 0)
  578. goto fail;
  579. /* skip crc */
  580. crc = get32(&s->bytestream);
  581. break;
  582. case MKTAG('P', 'L', 'T', 'E'):
  583. {
  584. int n, i, r, g, b;
  585. if ((length % 3) != 0 || length > 256 * 3)
  586. goto skip_tag;
  587. /* read the palette */
  588. n = length / 3;
  589. for(i=0;i<n;i++) {
  590. r = *s->bytestream++;
  591. g = *s->bytestream++;
  592. b = *s->bytestream++;
  593. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  594. }
  595. for(;i<256;i++) {
  596. s->palette[i] = (0xff << 24);
  597. }
  598. s->state |= PNG_PLTE;
  599. crc = get32(&s->bytestream);
  600. }
  601. break;
  602. case MKTAG('t', 'R', 'N', 'S'):
  603. {
  604. int v, i;
  605. /* read the transparency. XXX: Only palette mode supported */
  606. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  607. length > 256 ||
  608. !(s->state & PNG_PLTE))
  609. goto skip_tag;
  610. for(i=0;i<length;i++) {
  611. v = *s->bytestream++;
  612. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  613. }
  614. crc = get32(&s->bytestream);
  615. }
  616. break;
  617. case MKTAG('I', 'E', 'N', 'D'):
  618. if (!(s->state & PNG_ALLIMAGE))
  619. goto fail;
  620. crc = get32(&s->bytestream);
  621. goto exit_loop;
  622. default:
  623. /* skip tag */
  624. skip_tag:
  625. s->bytestream += length + 4;
  626. break;
  627. }
  628. }
  629. exit_loop:
  630. *picture= *(AVFrame*)&s->picture;
  631. *data_size = sizeof(AVPicture);
  632. ret = s->bytestream - s->bytestream_start;
  633. the_end:
  634. inflateEnd(&s->zstream);
  635. av_freep(&s->crow_buf);
  636. av_freep(&s->last_row);
  637. av_freep(&s->tmp_row);
  638. return ret;
  639. fail:
  640. ret = -1;
  641. goto the_end;
  642. }
  643. #endif
  644. #ifdef CONFIG_ENCODERS
  645. static void png_write_chunk(uint8_t **f, uint32_t tag,
  646. const uint8_t *buf, int length)
  647. {
  648. uint32_t crc;
  649. uint8_t tagbuf[4];
  650. put32(f, length);
  651. crc = crc32(0, Z_NULL, 0);
  652. tagbuf[0] = tag;
  653. tagbuf[1] = tag >> 8;
  654. tagbuf[2] = tag >> 16;
  655. tagbuf[3] = tag >> 24;
  656. crc = crc32(crc, tagbuf, 4);
  657. put32(f, bswap_32(tag));
  658. if (length > 0) {
  659. crc = crc32(crc, buf, length);
  660. memcpy(*f, buf, length);
  661. *f += length;
  662. }
  663. put32(f, crc);
  664. }
  665. /* XXX: use avcodec generic function ? */
  666. static void to_be32(uint8_t *p, uint32_t v)
  667. {
  668. p[0] = v >> 24;
  669. p[1] = v >> 16;
  670. p[2] = v >> 8;
  671. p[3] = v;
  672. }
  673. /* XXX: do filtering */
  674. static int png_write_row(PNGContext *s, const uint8_t *data, int size)
  675. {
  676. int ret;
  677. s->zstream.avail_in = size;
  678. s->zstream.next_in = (uint8_t *)data;
  679. while (s->zstream.avail_in > 0) {
  680. ret = deflate(&s->zstream, Z_NO_FLUSH);
  681. if (ret != Z_OK)
  682. return -1;
  683. if (s->zstream.avail_out == 0) {
  684. if(s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
  685. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  686. s->zstream.avail_out = IOBUF_SIZE;
  687. s->zstream.next_out = s->buf;
  688. }
  689. }
  690. return 0;
  691. }
  692. #endif /* CONFIG_ENCODERS */
  693. static int common_init(AVCodecContext *avctx){
  694. PNGContext *s = avctx->priv_data;
  695. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  696. avctx->coded_frame= (AVFrame*)&s->picture;
  697. // s->avctx= avctx;
  698. return 0;
  699. }
  700. #ifdef CONFIG_ENCODERS
  701. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  702. PNGContext *s = avctx->priv_data;
  703. AVFrame *pict = data;
  704. AVFrame * const p= (AVFrame*)&s->picture;
  705. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  706. int bits_per_pixel, pass_row_size;
  707. uint8_t *ptr;
  708. uint8_t *crow_buf = NULL;
  709. uint8_t *tmp_buf = NULL;
  710. *p = *pict;
  711. p->pict_type= FF_I_TYPE;
  712. p->key_frame= 1;
  713. s->bytestream_start=
  714. s->bytestream= buf;
  715. s->bytestream_end= buf+buf_size;
  716. is_progressive = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  717. switch(avctx->pix_fmt) {
  718. case PIX_FMT_RGB32:
  719. bit_depth = 8;
  720. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  721. break;
  722. case PIX_FMT_RGB24:
  723. bit_depth = 8;
  724. color_type = PNG_COLOR_TYPE_RGB;
  725. break;
  726. case PIX_FMT_GRAY8:
  727. bit_depth = 8;
  728. color_type = PNG_COLOR_TYPE_GRAY;
  729. break;
  730. case PIX_FMT_MONOBLACK:
  731. bit_depth = 1;
  732. color_type = PNG_COLOR_TYPE_GRAY;
  733. break;
  734. case PIX_FMT_PAL8:
  735. bit_depth = 8;
  736. color_type = PNG_COLOR_TYPE_PALETTE;
  737. break;
  738. default:
  739. return -1;
  740. }
  741. bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
  742. row_size = (avctx->width * bits_per_pixel + 7) >> 3;
  743. s->zstream.zalloc = png_zalloc;
  744. s->zstream.zfree = png_zfree;
  745. s->zstream.opaque = NULL;
  746. ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
  747. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  748. if (ret != Z_OK)
  749. return -1;
  750. crow_buf = av_malloc(row_size + 1);
  751. if (!crow_buf)
  752. goto fail;
  753. if (is_progressive) {
  754. tmp_buf = av_malloc(row_size + 1);
  755. if (!tmp_buf)
  756. goto fail;
  757. }
  758. /* write png header */
  759. memcpy(s->bytestream, pngsig, 8);
  760. s->bytestream += 8;
  761. to_be32(s->buf, avctx->width);
  762. to_be32(s->buf + 4, avctx->height);
  763. s->buf[8] = bit_depth;
  764. s->buf[9] = color_type;
  765. s->buf[10] = 0; /* compression type */
  766. s->buf[11] = 0; /* filter type */
  767. s->buf[12] = is_progressive; /* interlace type */
  768. png_write_chunk(&s->bytestream, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  769. /* put the palette if needed */
  770. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  771. int has_alpha, alpha, i;
  772. unsigned int v;
  773. uint32_t *palette;
  774. uint8_t *alpha_ptr;
  775. palette = (uint32_t *)p->data[1];
  776. ptr = s->buf;
  777. alpha_ptr = s->buf + 256 * 3;
  778. has_alpha = 0;
  779. for(i = 0; i < 256; i++) {
  780. v = palette[i];
  781. alpha = v >> 24;
  782. if (alpha && alpha != 0xff)
  783. has_alpha = 1;
  784. *alpha_ptr++ = alpha;
  785. ptr[0] = v >> 16;
  786. ptr[1] = v >> 8;
  787. ptr[2] = v;
  788. ptr += 3;
  789. }
  790. png_write_chunk(&s->bytestream, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  791. if (has_alpha) {
  792. png_write_chunk(&s->bytestream, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  793. }
  794. }
  795. /* now put each row */
  796. s->zstream.avail_out = IOBUF_SIZE;
  797. s->zstream.next_out = s->buf;
  798. if (is_progressive) {
  799. uint8_t *ptr1;
  800. int pass;
  801. for(pass = 0; pass < NB_PASSES; pass++) {
  802. /* NOTE: a pass is completely omited if no pixels would be
  803. output */
  804. pass_row_size = png_pass_row_size(pass, bits_per_pixel, avctx->width);
  805. if (pass_row_size > 0) {
  806. for(y = 0; y < avctx->height; y++) {
  807. if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
  808. ptr = p->data[0] + y * p->linesize[0];
  809. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  810. convert_from_rgb32(tmp_buf, ptr, avctx->width);
  811. ptr1 = tmp_buf;
  812. } else {
  813. ptr1 = ptr;
  814. }
  815. png_get_interlaced_row(crow_buf + 1, pass_row_size,
  816. bits_per_pixel, pass,
  817. ptr1, avctx->width);
  818. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  819. png_write_row(s, crow_buf, pass_row_size + 1);
  820. }
  821. }
  822. }
  823. }
  824. } else {
  825. for(y = 0; y < avctx->height; y++) {
  826. ptr = p->data[0] + y * p->linesize[0];
  827. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  828. convert_from_rgb32(crow_buf + 1, ptr, avctx->width);
  829. else
  830. memcpy(crow_buf + 1, ptr, row_size);
  831. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  832. png_write_row(s, crow_buf, row_size + 1);
  833. }
  834. }
  835. /* compress last bytes */
  836. for(;;) {
  837. ret = deflate(&s->zstream, Z_FINISH);
  838. if (ret == Z_OK || ret == Z_STREAM_END) {
  839. len = IOBUF_SIZE - s->zstream.avail_out;
  840. if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
  841. png_write_chunk(&s->bytestream, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  842. }
  843. s->zstream.avail_out = IOBUF_SIZE;
  844. s->zstream.next_out = s->buf;
  845. if (ret == Z_STREAM_END)
  846. break;
  847. } else {
  848. goto fail;
  849. }
  850. }
  851. png_write_chunk(&s->bytestream, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  852. ret = s->bytestream - s->bytestream_start;
  853. the_end:
  854. av_free(crow_buf);
  855. av_free(tmp_buf);
  856. deflateEnd(&s->zstream);
  857. return ret;
  858. fail:
  859. ret = -1;
  860. goto the_end;
  861. }
  862. #endif
  863. #ifdef CONFIG_PNG_DECODER
  864. AVCodec png_decoder = {
  865. "png",
  866. CODEC_TYPE_VIDEO,
  867. CODEC_ID_PNG,
  868. sizeof(PNGContext),
  869. common_init,
  870. NULL,
  871. NULL, //decode_end,
  872. decode_frame,
  873. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  874. NULL
  875. };
  876. #endif
  877. #ifdef CONFIG_PNG_ENCODER
  878. AVCodec png_encoder = {
  879. "png",
  880. CODEC_TYPE_VIDEO,
  881. CODEC_ID_PNG,
  882. sizeof(PNGContext),
  883. common_init,
  884. encode_frame,
  885. NULL, //encode_end,
  886. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, -1},
  887. };
  888. #endif // CONFIG_PNG_ENCODER