pngdec.c 21 KB

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