00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "libavutil/imgutils.h"
00025 #include "avcodec.h"
00026 #include "bytestream.h"
00027 #include "png.h"
00028 #include "pngdsp.h"
00029
00030
00031
00032
00033
00034 #include <zlib.h>
00035
00036
00037
00038 typedef struct PNGDecContext {
00039 PNGDSPContext dsp;
00040 AVCodecContext *avctx;
00041
00042 GetByteContext gb;
00043 AVFrame picture1, picture2;
00044 AVFrame *current_picture, *last_picture;
00045
00046 int state;
00047 int width, height;
00048 int bit_depth;
00049 int color_type;
00050 int compression_type;
00051 int interlace_type;
00052 int filter_type;
00053 int channels;
00054 int bits_per_pixel;
00055 int bpp;
00056
00057 uint8_t *image_buf;
00058 int image_linesize;
00059 uint32_t palette[256];
00060 uint8_t *crow_buf;
00061 uint8_t *last_row;
00062 uint8_t *tmp_row;
00063 int pass;
00064 int crow_size;
00065 int row_size;
00066 int pass_row_size;
00067 int y;
00068 z_stream zstream;
00069 } PNGDecContext;
00070
00071
00072 static const uint8_t png_pass_mask[NB_PASSES] = {
00073 0x01, 0x01, 0x11, 0x11, 0x55, 0x55, 0xff,
00074 };
00075
00076
00077 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
00078 0xff, 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
00079 };
00080
00081
00082 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
00083 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
00084 };
00085
00086
00087
00088
00089 static void png_put_interlaced_row(uint8_t *dst, int width,
00090 int bits_per_pixel, int pass,
00091 int color_type, const uint8_t *src)
00092 {
00093 int x, mask, dsp_mask, j, src_x, b, bpp;
00094 uint8_t *d;
00095 const uint8_t *s;
00096
00097 mask = png_pass_mask[pass];
00098 dsp_mask = png_pass_dsp_mask[pass];
00099 switch(bits_per_pixel) {
00100 case 1:
00101 src_x = 0;
00102 for(x = 0; x < width; x++) {
00103 j = (x & 7);
00104 if ((dsp_mask << j) & 0x80) {
00105 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
00106 dst[x >> 3] &= 0xFF7F>>j;
00107 dst[x >> 3] |= b << (7 - j);
00108 }
00109 if ((mask << j) & 0x80)
00110 src_x++;
00111 }
00112 break;
00113 case 2:
00114 src_x = 0;
00115 for(x = 0; x < width; x++) {
00116 int j2 = 2*(x&3);
00117 j = (x & 7);
00118 if ((dsp_mask << j) & 0x80) {
00119 b = (src[src_x >> 2] >> (6 - 2*(src_x & 3))) & 3;
00120 dst[x >> 2] &= 0xFF3F>>j2;
00121 dst[x >> 2] |= b << (6 - j2);
00122 }
00123 if ((mask << j) & 0x80)
00124 src_x++;
00125 }
00126 break;
00127 case 4:
00128 src_x = 0;
00129 for(x = 0; x < width; x++) {
00130 int j2 = 4*(x&1);
00131 j = (x & 7);
00132 if ((dsp_mask << j) & 0x80) {
00133 b = (src[src_x >> 1] >> (4 - 4*(src_x & 1))) & 15;
00134 dst[x >> 1] &= 0xFF0F>>j2;
00135 dst[x >> 1] |= b << (4 - j2);
00136 }
00137 if ((mask << j) & 0x80)
00138 src_x++;
00139 }
00140 break;
00141 default:
00142 bpp = bits_per_pixel >> 3;
00143 d = dst;
00144 s = src;
00145 for(x = 0; x < width; x++) {
00146 j = x & 7;
00147 if ((dsp_mask << j) & 0x80) {
00148 memcpy(d, s, bpp);
00149 }
00150 d += bpp;
00151 if ((mask << j) & 0x80)
00152 s += bpp;
00153 }
00154 break;
00155 }
00156 }
00157
00158 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
00159 {
00160 int i;
00161 for(i = 0; i < w; i++) {
00162 int a, b, c, p, pa, pb, pc;
00163
00164 a = dst[i - bpp];
00165 b = top[i];
00166 c = top[i - bpp];
00167
00168 p = b - c;
00169 pc = a - c;
00170
00171 pa = abs(p);
00172 pb = abs(pc);
00173 pc = abs(p + pc);
00174
00175 if (pa <= pb && pa <= pc)
00176 p = a;
00177 else if (pb <= pc)
00178 p = b;
00179 else
00180 p = c;
00181 dst[i] = p + src[i];
00182 }
00183 }
00184
00185 #define UNROLL1(bpp, op) {\
00186 r = dst[0];\
00187 if(bpp >= 2) g = dst[1];\
00188 if(bpp >= 3) b = dst[2];\
00189 if(bpp >= 4) a = dst[3];\
00190 for(; i < size; i+=bpp) {\
00191 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
00192 if(bpp == 1) continue;\
00193 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
00194 if(bpp == 2) continue;\
00195 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
00196 if(bpp == 3) continue;\
00197 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
00198 }\
00199 }
00200
00201 #define UNROLL_FILTER(op)\
00202 if(bpp == 1) UNROLL1(1, op)\
00203 else if(bpp == 2) UNROLL1(2, op)\
00204 else if(bpp == 3) UNROLL1(3, op)\
00205 else if(bpp == 4) UNROLL1(4, op)\
00206 else {\
00207 for (; i < size; i += bpp) {\
00208 int j;\
00209 for (j = 0; j < bpp; j++)\
00210 dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
00211 }\
00212 }
00213
00214
00215 static void png_filter_row(PNGDSPContext *dsp, uint8_t *dst, int filter_type,
00216 uint8_t *src, uint8_t *last, int size, int bpp)
00217 {
00218 int i, p, r, g, b, a;
00219
00220 switch(filter_type) {
00221 case PNG_FILTER_VALUE_NONE:
00222 memcpy(dst, src, size);
00223 break;
00224 case PNG_FILTER_VALUE_SUB:
00225 for(i = 0; i < bpp; i++) {
00226 dst[i] = src[i];
00227 }
00228 if(bpp == 4) {
00229 p = *(int*)dst;
00230 for(; i < size; i+=bpp) {
00231 int s = *(int*)(src+i);
00232 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
00233 *(int*)(dst+i) = p;
00234 }
00235 } else {
00236 #define OP_SUB(x,s,l) x+s
00237 UNROLL_FILTER(OP_SUB);
00238 }
00239 break;
00240 case PNG_FILTER_VALUE_UP:
00241 dsp->add_bytes_l2(dst, src, last, size);
00242 break;
00243 case PNG_FILTER_VALUE_AVG:
00244 for(i = 0; i < bpp; i++) {
00245 p = (last[i] >> 1);
00246 dst[i] = p + src[i];
00247 }
00248 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
00249 UNROLL_FILTER(OP_AVG);
00250 break;
00251 case PNG_FILTER_VALUE_PAETH:
00252 for(i = 0; i < bpp; i++) {
00253 p = last[i];
00254 dst[i] = p + src[i];
00255 }
00256 if(bpp > 2 && size > 4) {
00257
00258 int w = bpp==4 ? size : size-3;
00259 dsp->add_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
00260 i = w;
00261 }
00262 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
00263 break;
00264 }
00265 }
00266
00267
00268
00269 #define YUV2RGB(NAME, TYPE) \
00270 static void deloco_ ## NAME(TYPE *dst, int size, int alpha) \
00271 { \
00272 int i; \
00273 for (i = 0; i < size; i += 3 + alpha) { \
00274 int g = dst [i+1]; \
00275 dst[i+0] += g; \
00276 dst[i+2] += g; \
00277 } \
00278 }
00279
00280 YUV2RGB(rgb8, uint8_t)
00281 YUV2RGB(rgb16, uint16_t)
00282
00283
00284 static void png_handle_row(PNGDecContext *s)
00285 {
00286 uint8_t *ptr, *last_row;
00287 int got_line;
00288
00289 if (!s->interlace_type) {
00290 ptr = s->image_buf + s->image_linesize * s->y;
00291 if (s->y == 0)
00292 last_row = s->last_row;
00293 else
00294 last_row = ptr - s->image_linesize;
00295
00296 png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
00297 last_row, s->row_size, s->bpp);
00298
00299 if (s->filter_type == PNG_FILTER_TYPE_LOCO && s->y > 0) {
00300 if (s->bit_depth == 16) {
00301 deloco_rgb16((uint16_t *)(ptr - s->image_linesize), s->row_size / 2,
00302 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
00303 } else {
00304 deloco_rgb8(ptr - s->image_linesize, s->row_size,
00305 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
00306 }
00307 }
00308 s->y++;
00309 if (s->y == s->height) {
00310 s->state |= PNG_ALLIMAGE;
00311 if (s->filter_type == PNG_FILTER_TYPE_LOCO) {
00312 if (s->bit_depth == 16) {
00313 deloco_rgb16((uint16_t *)ptr, s->row_size / 2,
00314 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
00315 } else {
00316 deloco_rgb8(ptr, s->row_size,
00317 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA);
00318 }
00319 }
00320 }
00321 } else {
00322 got_line = 0;
00323 for(;;) {
00324 ptr = s->image_buf + s->image_linesize * s->y;
00325 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
00326
00327
00328 if (got_line)
00329 break;
00330 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
00331 s->last_row, s->pass_row_size, s->bpp);
00332 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
00333 got_line = 1;
00334 }
00335 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
00336 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
00337 s->color_type, s->last_row);
00338 }
00339 s->y++;
00340 if (s->y == s->height) {
00341 memset(s->last_row, 0, s->row_size);
00342 for(;;) {
00343 if (s->pass == NB_PASSES - 1) {
00344 s->state |= PNG_ALLIMAGE;
00345 goto the_end;
00346 } else {
00347 s->pass++;
00348 s->y = 0;
00349 s->pass_row_size = ff_png_pass_row_size(s->pass,
00350 s->bits_per_pixel,
00351 s->width);
00352 s->crow_size = s->pass_row_size + 1;
00353 if (s->pass_row_size != 0)
00354 break;
00355
00356 }
00357 }
00358 }
00359 }
00360 the_end: ;
00361 }
00362 }
00363
00364 static int png_decode_idat(PNGDecContext *s, int length)
00365 {
00366 int ret;
00367 s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
00368 s->zstream.next_in = (unsigned char *)s->gb.buffer;
00369 bytestream2_skip(&s->gb, length);
00370
00371
00372 while (s->zstream.avail_in > 0) {
00373 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
00374 if (ret != Z_OK && ret != Z_STREAM_END) {
00375 av_log(s->avctx, AV_LOG_ERROR, "inflate returned %d\n", ret);
00376 return -1;
00377 }
00378 if (s->zstream.avail_out == 0) {
00379 if (!(s->state & PNG_ALLIMAGE)) {
00380 png_handle_row(s);
00381 }
00382 s->zstream.avail_out = s->crow_size;
00383 s->zstream.next_out = s->crow_buf;
00384 }
00385 }
00386 return 0;
00387 }
00388
00389 static int decode_frame(AVCodecContext *avctx,
00390 void *data, int *data_size,
00391 AVPacket *avpkt)
00392 {
00393 const uint8_t *buf = avpkt->data;
00394 int buf_size = avpkt->size;
00395 PNGDecContext * const s = avctx->priv_data;
00396 AVFrame *picture = data;
00397 AVFrame *p;
00398 uint8_t *crow_buf_base = NULL;
00399 uint32_t tag, length;
00400 int64_t sig;
00401 int ret;
00402
00403 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
00404 avctx->coded_frame= s->current_picture;
00405 p = s->current_picture;
00406
00407 bytestream2_init(&s->gb, buf, buf_size);
00408
00409
00410 sig = bytestream2_get_be64(&s->gb);
00411 if (sig != PNGSIG &&
00412 sig != MNGSIG) {
00413 av_log(avctx, AV_LOG_ERROR, "Missing png signature\n");
00414 return -1;
00415 }
00416
00417 s->y=
00418 s->state=0;
00419
00420
00421 s->zstream.zalloc = ff_png_zalloc;
00422 s->zstream.zfree = ff_png_zfree;
00423 s->zstream.opaque = NULL;
00424 ret = inflateInit(&s->zstream);
00425 if (ret != Z_OK) {
00426 av_log(avctx, AV_LOG_ERROR, "inflateInit returned %d\n", ret);
00427 return -1;
00428 }
00429 for(;;) {
00430 if (bytestream2_get_bytes_left(&s->gb) <= 0) {
00431 av_log(avctx, AV_LOG_ERROR, "No bytes left\n");
00432 goto fail;
00433 }
00434
00435 length = bytestream2_get_be32(&s->gb);
00436 if (length > 0x7fffffff || length > bytestream2_get_bytes_left(&s->gb)) {
00437 av_log(avctx, AV_LOG_ERROR, "chunk too big\n");
00438 goto fail;
00439 }
00440 tag = bytestream2_get_le32(&s->gb);
00441 if (avctx->debug & FF_DEBUG_STARTCODE)
00442 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
00443 (tag & 0xff),
00444 ((tag >> 8) & 0xff),
00445 ((tag >> 16) & 0xff),
00446 ((tag >> 24) & 0xff), length);
00447 switch(tag) {
00448 case MKTAG('I', 'H', 'D', 'R'):
00449 if (length != 13)
00450 goto fail;
00451 s->width = bytestream2_get_be32(&s->gb);
00452 s->height = bytestream2_get_be32(&s->gb);
00453 if(av_image_check_size(s->width, s->height, 0, avctx)){
00454 s->width= s->height= 0;
00455 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
00456 goto fail;
00457 }
00458 s->bit_depth = bytestream2_get_byte(&s->gb);
00459 s->color_type = bytestream2_get_byte(&s->gb);
00460 s->compression_type = bytestream2_get_byte(&s->gb);
00461 s->filter_type = bytestream2_get_byte(&s->gb);
00462 s->interlace_type = bytestream2_get_byte(&s->gb);
00463 bytestream2_skip(&s->gb, 4);
00464 s->state |= PNG_IHDR;
00465 if (avctx->debug & FF_DEBUG_PICT_INFO)
00466 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",
00467 s->width, s->height, s->bit_depth, s->color_type,
00468 s->compression_type, s->filter_type, s->interlace_type);
00469 break;
00470 case MKTAG('I', 'D', 'A', 'T'):
00471 if (!(s->state & PNG_IHDR)) {
00472 av_log(avctx, AV_LOG_ERROR, "IDAT without IHDR\n");
00473 goto fail;
00474 }
00475 if (!(s->state & PNG_IDAT)) {
00476
00477 avctx->width = s->width;
00478 avctx->height = s->height;
00479
00480 s->channels = ff_png_get_nb_channels(s->color_type);
00481 s->bits_per_pixel = s->bit_depth * s->channels;
00482 s->bpp = (s->bits_per_pixel + 7) >> 3;
00483 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
00484
00485 if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
00486 s->color_type == PNG_COLOR_TYPE_RGB) {
00487 avctx->pix_fmt = PIX_FMT_RGB24;
00488 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
00489 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00490 avctx->pix_fmt = PIX_FMT_RGBA;
00491 } else if ((s->bit_depth == 2 || s->bit_depth == 4 || s->bit_depth == 8) &&
00492 s->color_type == PNG_COLOR_TYPE_GRAY) {
00493 avctx->pix_fmt = PIX_FMT_GRAY8;
00494 } else if (s->bit_depth == 16 &&
00495 s->color_type == PNG_COLOR_TYPE_GRAY) {
00496 avctx->pix_fmt = PIX_FMT_GRAY16BE;
00497 } else if (s->bit_depth == 16 &&
00498 s->color_type == PNG_COLOR_TYPE_RGB) {
00499 avctx->pix_fmt = PIX_FMT_RGB48BE;
00500 } else if (s->bit_depth == 16 &&
00501 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00502 avctx->pix_fmt = PIX_FMT_RGBA64BE;
00503 } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) &&
00504 s->color_type == PNG_COLOR_TYPE_PALETTE) {
00505 avctx->pix_fmt = PIX_FMT_PAL8;
00506 } else if (s->bit_depth == 1) {
00507 avctx->pix_fmt = PIX_FMT_MONOBLACK;
00508 } else if (s->bit_depth == 8 &&
00509 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
00510 avctx->pix_fmt = PIX_FMT_Y400A;
00511 } else {
00512 av_log(avctx, AV_LOG_ERROR, "unsupported bit depth %d "
00513 "and color type %d\n",
00514 s->bit_depth, s->color_type);
00515 goto fail;
00516 }
00517 if(p->data[0])
00518 avctx->release_buffer(avctx, p);
00519
00520 p->reference= 3;
00521 if(avctx->get_buffer(avctx, p) < 0){
00522 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00523 goto fail;
00524 }
00525 p->pict_type= AV_PICTURE_TYPE_I;
00526 p->key_frame= 1;
00527 p->interlaced_frame = !!s->interlace_type;
00528
00529
00530 if (!s->interlace_type) {
00531 s->crow_size = s->row_size + 1;
00532 } else {
00533 s->pass = 0;
00534 s->pass_row_size = ff_png_pass_row_size(s->pass,
00535 s->bits_per_pixel,
00536 s->width);
00537 s->crow_size = s->pass_row_size + 1;
00538 }
00539 av_dlog(avctx, "row_size=%d crow_size =%d\n",
00540 s->row_size, s->crow_size);
00541 s->image_buf = p->data[0];
00542 s->image_linesize = p->linesize[0];
00543
00544 if (avctx->pix_fmt == PIX_FMT_PAL8)
00545 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
00546
00547 s->last_row = av_mallocz(s->row_size);
00548 if (!s->last_row)
00549 goto fail;
00550 if (s->interlace_type ||
00551 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00552 s->tmp_row = av_malloc(s->row_size);
00553 if (!s->tmp_row)
00554 goto fail;
00555 }
00556
00557 crow_buf_base = av_malloc(s->row_size + 16);
00558 if (!crow_buf_base)
00559 goto fail;
00560
00561
00562 s->crow_buf = crow_buf_base + 15;
00563 s->zstream.avail_out = s->crow_size;
00564 s->zstream.next_out = s->crow_buf;
00565 }
00566 s->state |= PNG_IDAT;
00567 if (png_decode_idat(s, length) < 0)
00568 goto fail;
00569 bytestream2_skip(&s->gb, 4);
00570 break;
00571 case MKTAG('P', 'L', 'T', 'E'):
00572 {
00573 int n, i, r, g, b;
00574
00575 if ((length % 3) != 0 || length > 256 * 3)
00576 goto skip_tag;
00577
00578 n = length / 3;
00579 for(i=0;i<n;i++) {
00580 r = bytestream2_get_byte(&s->gb);
00581 g = bytestream2_get_byte(&s->gb);
00582 b = bytestream2_get_byte(&s->gb);
00583 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
00584 }
00585 for(;i<256;i++) {
00586 s->palette[i] = (0xff << 24);
00587 }
00588 s->state |= PNG_PLTE;
00589 bytestream2_skip(&s->gb, 4);
00590 }
00591 break;
00592 case MKTAG('t', 'R', 'N', 'S'):
00593 {
00594 int v, i;
00595
00596
00597 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
00598 length > 256 ||
00599 !(s->state & PNG_PLTE))
00600 goto skip_tag;
00601 for(i=0;i<length;i++) {
00602 v = bytestream2_get_byte(&s->gb);
00603 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
00604 }
00605 bytestream2_skip(&s->gb, 4);
00606 }
00607 break;
00608 case MKTAG('I', 'E', 'N', 'D'):
00609 if (!(s->state & PNG_ALLIMAGE))
00610 av_log(avctx, AV_LOG_ERROR, "IEND without all image\n");
00611 if (!(s->state & (PNG_ALLIMAGE|PNG_IDAT))) {
00612 goto fail;
00613 }
00614 bytestream2_skip(&s->gb, 4);
00615 goto exit_loop;
00616 default:
00617
00618 skip_tag:
00619 bytestream2_skip(&s->gb, length + 4);
00620 break;
00621 }
00622 }
00623 exit_loop:
00624
00625 if(s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
00626 int i, j;
00627 uint8_t *pd = s->current_picture->data[0];
00628 for(j=0; j < s->height; j++) {
00629 for(i=s->width/8-1; i>=0; i--) {
00630 pd[8*i+7]= pd[i] &1;
00631 pd[8*i+6]= (pd[i]>>1)&1;
00632 pd[8*i+5]= (pd[i]>>2)&1;
00633 pd[8*i+4]= (pd[i]>>3)&1;
00634 pd[8*i+3]= (pd[i]>>4)&1;
00635 pd[8*i+2]= (pd[i]>>5)&1;
00636 pd[8*i+1]= (pd[i]>>6)&1;
00637 pd[8*i+0]= pd[i]>>7;
00638 }
00639 pd += s->image_linesize;
00640 }
00641 }
00642 if(s->bits_per_pixel == 2){
00643 int i, j;
00644 uint8_t *pd = s->current_picture->data[0];
00645 for(j=0; j < s->height; j++) {
00646 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
00647 for(i=s->width/4-1; i>=0; i--) {
00648 pd[4*i+3]= pd[i] &3;
00649 pd[4*i+2]= (pd[i]>>2)&3;
00650 pd[4*i+1]= (pd[i]>>4)&3;
00651 pd[4*i+0]= pd[i]>>6;
00652 }
00653 } else {
00654 for(i=s->width/4-1; i>=0; i--) {
00655 pd[4*i+3]= ( pd[i] &3)*0x55;
00656 pd[4*i+2]= ((pd[i]>>2)&3)*0x55;
00657 pd[4*i+1]= ((pd[i]>>4)&3)*0x55;
00658 pd[4*i+0]= ( pd[i]>>6 )*0x55;
00659 }
00660 }
00661 pd += s->image_linesize;
00662 }
00663 }
00664 if(s->bits_per_pixel == 4){
00665 int i, j;
00666 uint8_t *pd = s->current_picture->data[0];
00667 for(j=0; j < s->height; j++) {
00668 if (s->color_type == PNG_COLOR_TYPE_PALETTE){
00669 for(i=s->width/2-1; i>=0; i--) {
00670 pd[2*i+1]= pd[i]&15;
00671 pd[2*i+0]= pd[i]>>4;
00672 }
00673 } else {
00674 for(i=s->width/2-1; i>=0; i--) {
00675 pd[2*i+1]= (pd[i]&15)*0x11;
00676 pd[2*i+0]= (pd[i]>>4)*0x11;
00677 }
00678 }
00679 pd += s->image_linesize;
00680 }
00681 }
00682
00683
00684 if(s->last_picture->data[0] != NULL) {
00685 if( !(avpkt->flags & AV_PKT_FLAG_KEY)
00686 && s->last_picture->width == s->current_picture->width
00687 && s->last_picture->height== s->current_picture->height
00688 ) {
00689 int i, j;
00690 uint8_t *pd = s->current_picture->data[0];
00691 uint8_t *pd_last = s->last_picture->data[0];
00692
00693 for(j=0; j < s->height; j++) {
00694 for(i=0; i < s->width * s->bpp; i++) {
00695 pd[i] += pd_last[i];
00696 }
00697 pd += s->image_linesize;
00698 pd_last += s->image_linesize;
00699 }
00700 }
00701 }
00702
00703 *picture= *s->current_picture;
00704 *data_size = sizeof(AVFrame);
00705
00706 ret = bytestream2_tell(&s->gb);
00707 the_end:
00708 inflateEnd(&s->zstream);
00709 av_free(crow_buf_base);
00710 s->crow_buf = NULL;
00711 av_freep(&s->last_row);
00712 av_freep(&s->tmp_row);
00713 return ret;
00714 fail:
00715 ret = -1;
00716 goto the_end;
00717 }
00718
00719 static av_cold int png_dec_init(AVCodecContext *avctx)
00720 {
00721 PNGDecContext *s = avctx->priv_data;
00722
00723 s->current_picture = &s->picture1;
00724 s->last_picture = &s->picture2;
00725 avcodec_get_frame_defaults(&s->picture1);
00726 avcodec_get_frame_defaults(&s->picture2);
00727
00728 ff_pngdsp_init(&s->dsp);
00729
00730 s->avctx = avctx;
00731
00732 return 0;
00733 }
00734
00735 static av_cold int png_dec_end(AVCodecContext *avctx)
00736 {
00737 PNGDecContext *s = avctx->priv_data;
00738
00739 if (s->picture1.data[0])
00740 avctx->release_buffer(avctx, &s->picture1);
00741 if (s->picture2.data[0])
00742 avctx->release_buffer(avctx, &s->picture2);
00743
00744 return 0;
00745 }
00746
00747 AVCodec ff_png_decoder = {
00748 .name = "png",
00749 .type = AVMEDIA_TYPE_VIDEO,
00750 .id = AV_CODEC_ID_PNG,
00751 .priv_data_size = sizeof(PNGDecContext),
00752 .init = png_dec_init,
00753 .close = png_dec_end,
00754 .decode = decode_frame,
00755 .capabilities = CODEC_CAP_DR1 ,
00756 .long_name = NULL_IF_CONFIG_SMALL("PNG (Portable Network Graphics) image"),
00757 };