00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "bytestream.h"
00023 #include "png.h"
00024 #include "dsputil.h"
00025
00026
00027
00028
00029
00030 #include <zlib.h>
00031
00032
00033
00034 typedef struct PNGDecContext {
00035 DSPContext dsp;
00036
00037 const uint8_t *bytestream;
00038 const uint8_t *bytestream_start;
00039 const uint8_t *bytestream_end;
00040 AVFrame picture;
00041
00042 int state;
00043 int width, height;
00044 int bit_depth;
00045 int color_type;
00046 int compression_type;
00047 int interlace_type;
00048 int filter_type;
00049 int channels;
00050 int bits_per_pixel;
00051 int bpp;
00052
00053 uint8_t *image_buf;
00054 int image_linesize;
00055 uint32_t palette[256];
00056 uint8_t *crow_buf;
00057 uint8_t *last_row;
00058 uint8_t *tmp_row;
00059 int pass;
00060 int crow_size;
00061 int row_size;
00062 int pass_row_size;
00063 int y;
00064 z_stream zstream;
00065 } PNGDecContext;
00066
00067
00068 static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
00069 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
00070 };
00071
00072
00073 static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
00074 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
00075 };
00076
00077
00078
00079
00080 static void png_put_interlaced_row(uint8_t *dst, int width,
00081 int bits_per_pixel, int pass,
00082 int color_type, const uint8_t *src)
00083 {
00084 int x, mask, dsp_mask, j, src_x, b, bpp;
00085 uint8_t *d;
00086 const uint8_t *s;
00087
00088 mask = ff_png_pass_mask[pass];
00089 dsp_mask = png_pass_dsp_mask[pass];
00090 switch(bits_per_pixel) {
00091 case 1:
00092
00093 if (pass == 0)
00094 memset(dst, 0, (width + 7) >> 3);
00095 src_x = 0;
00096 for(x = 0; x < width; x++) {
00097 j = (x & 7);
00098 if ((dsp_mask << j) & 0x80) {
00099 b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
00100 dst[x >> 3] |= b << (7 - j);
00101 }
00102 if ((mask << j) & 0x80)
00103 src_x++;
00104 }
00105 break;
00106 default:
00107 bpp = bits_per_pixel >> 3;
00108 d = dst;
00109 s = src;
00110 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00111 for(x = 0; x < width; x++) {
00112 j = x & 7;
00113 if ((dsp_mask << j) & 0x80) {
00114 *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
00115 }
00116 d += bpp;
00117 if ((mask << j) & 0x80)
00118 s += bpp;
00119 }
00120 } else {
00121 for(x = 0; x < width; x++) {
00122 j = x & 7;
00123 if ((dsp_mask << j) & 0x80) {
00124 memcpy(d, s, bpp);
00125 }
00126 d += bpp;
00127 if ((mask << j) & 0x80)
00128 s += bpp;
00129 }
00130 }
00131 break;
00132 }
00133 }
00134
00135 void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
00136 {
00137 int i;
00138 for(i = 0; i < w; i++) {
00139 int a, b, c, p, pa, pb, pc;
00140
00141 a = dst[i - bpp];
00142 b = top[i];
00143 c = top[i - bpp];
00144
00145 p = b - c;
00146 pc = a - c;
00147
00148 pa = abs(p);
00149 pb = abs(pc);
00150 pc = abs(p + pc);
00151
00152 if (pa <= pb && pa <= pc)
00153 p = a;
00154 else if (pb <= pc)
00155 p = b;
00156 else
00157 p = c;
00158 dst[i] = p + src[i];
00159 }
00160 }
00161
00162 #define UNROLL1(bpp, op) {\
00163 r = dst[0];\
00164 if(bpp >= 2) g = dst[1];\
00165 if(bpp >= 3) b = dst[2];\
00166 if(bpp >= 4) a = dst[3];\
00167 for(; i < size; i+=bpp) {\
00168 dst[i+0] = r = op(r, src[i+0], last[i+0]);\
00169 if(bpp == 1) continue;\
00170 dst[i+1] = g = op(g, src[i+1], last[i+1]);\
00171 if(bpp == 2) continue;\
00172 dst[i+2] = b = op(b, src[i+2], last[i+2]);\
00173 if(bpp == 3) continue;\
00174 dst[i+3] = a = op(a, src[i+3], last[i+3]);\
00175 }\
00176 }
00177
00178 #define UNROLL_FILTER(op)\
00179 if(bpp == 1) UNROLL1(1, op)\
00180 else if(bpp == 2) UNROLL1(2, op)\
00181 else if(bpp == 3) UNROLL1(3, op)\
00182 else if(bpp == 4) UNROLL1(4, op)\
00183
00184
00185 static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
00186 uint8_t *src, uint8_t *last, int size, int bpp)
00187 {
00188 int i, p, r, g, b, a;
00189
00190 switch(filter_type) {
00191 case PNG_FILTER_VALUE_NONE:
00192 memcpy(dst, src, size);
00193 break;
00194 case PNG_FILTER_VALUE_SUB:
00195 for(i = 0; i < bpp; i++) {
00196 dst[i] = src[i];
00197 }
00198 if(bpp == 4) {
00199 p = *(int*)dst;
00200 for(; i < size; i+=bpp) {
00201 int s = *(int*)(src+i);
00202 p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
00203 *(int*)(dst+i) = p;
00204 }
00205 } else {
00206 #define OP_SUB(x,s,l) x+s
00207 UNROLL_FILTER(OP_SUB);
00208 }
00209 break;
00210 case PNG_FILTER_VALUE_UP:
00211 dsp->add_bytes_l2(dst, src, last, size);
00212 break;
00213 case PNG_FILTER_VALUE_AVG:
00214 for(i = 0; i < bpp; i++) {
00215 p = (last[i] >> 1);
00216 dst[i] = p + src[i];
00217 }
00218 #define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
00219 UNROLL_FILTER(OP_AVG);
00220 break;
00221 case PNG_FILTER_VALUE_PAETH:
00222 for(i = 0; i < bpp; i++) {
00223 p = last[i];
00224 dst[i] = p + src[i];
00225 }
00226 if(bpp > 1 && size > 4) {
00227
00228 int w = bpp==4 ? size : size-3;
00229 dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
00230 i = w;
00231 }
00232 ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
00233 break;
00234 }
00235 }
00236
00237 static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
00238 {
00239 int j;
00240 unsigned int r, g, b, a;
00241
00242 for(j = 0;j < width; j++) {
00243 r = src[0];
00244 g = src[1];
00245 b = src[2];
00246 a = src[3];
00247 if(loco) {
00248 r = (r+g)&0xff;
00249 b = (b+g)&0xff;
00250 }
00251 *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
00252 dst += 4;
00253 src += 4;
00254 }
00255 }
00256
00257 static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
00258 {
00259 if(loco)
00260 convert_to_rgb32_loco(dst, src, width, 1);
00261 else
00262 convert_to_rgb32_loco(dst, src, width, 0);
00263 }
00264
00265 static void deloco_rgb24(uint8_t *dst, int size)
00266 {
00267 int i;
00268 for(i=0; i<size; i+=3) {
00269 int g = dst[i+1];
00270 dst[i+0] += g;
00271 dst[i+2] += g;
00272 }
00273 }
00274
00275
00276 static void png_handle_row(PNGDecContext *s)
00277 {
00278 uint8_t *ptr, *last_row;
00279 int got_line;
00280
00281 if (!s->interlace_type) {
00282 ptr = s->image_buf + s->image_linesize * s->y;
00283
00284 if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00285 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
00286 s->last_row, s->row_size, s->bpp);
00287 convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
00288 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
00289 } else {
00290
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
00300 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
00301 s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
00302 deloco_rgb24(ptr - s->image_linesize, s->row_size);
00303 s->y++;
00304 if (s->y == s->height) {
00305 s->state |= PNG_ALLIMAGE;
00306 if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
00307 s->color_type == PNG_COLOR_TYPE_RGB)
00308 deloco_rgb24(ptr, s->row_size);
00309 }
00310 } else {
00311 got_line = 0;
00312 for(;;) {
00313 ptr = s->image_buf + s->image_linesize * s->y;
00314 if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
00315
00316
00317 if (got_line)
00318 break;
00319 png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
00320 s->last_row, s->pass_row_size, s->bpp);
00321 FFSWAP(uint8_t*, s->last_row, s->tmp_row);
00322 got_line = 1;
00323 }
00324 if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
00325
00326 png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
00327 s->color_type, s->last_row);
00328 }
00329 s->y++;
00330 if (s->y == s->height) {
00331 for(;;) {
00332 if (s->pass == NB_PASSES - 1) {
00333 s->state |= PNG_ALLIMAGE;
00334 goto the_end;
00335 } else {
00336 s->pass++;
00337 s->y = 0;
00338 s->pass_row_size = ff_png_pass_row_size(s->pass,
00339 s->bits_per_pixel,
00340 s->width);
00341 s->crow_size = s->pass_row_size + 1;
00342 if (s->pass_row_size != 0)
00343 break;
00344
00345 }
00346 }
00347 }
00348 }
00349 the_end: ;
00350 }
00351 }
00352
00353 static int png_decode_idat(PNGDecContext *s, int length)
00354 {
00355 int ret;
00356 s->zstream.avail_in = length;
00357 s->zstream.next_in = s->bytestream;
00358 s->bytestream += length;
00359
00360 if(s->bytestream > s->bytestream_end)
00361 return -1;
00362
00363
00364 while (s->zstream.avail_in > 0) {
00365 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
00366 if (ret != Z_OK && ret != Z_STREAM_END) {
00367 return -1;
00368 }
00369 if (s->zstream.avail_out == 0) {
00370 if (!(s->state & PNG_ALLIMAGE)) {
00371 png_handle_row(s);
00372 }
00373 s->zstream.avail_out = s->crow_size;
00374 s->zstream.next_out = s->crow_buf;
00375 }
00376 }
00377 return 0;
00378 }
00379
00380 static int decode_frame(AVCodecContext *avctx,
00381 void *data, int *data_size,
00382 const uint8_t *buf, int buf_size)
00383 {
00384 PNGDecContext * const s = avctx->priv_data;
00385 AVFrame *picture = data;
00386 AVFrame * const p= &s->picture;
00387 uint32_t tag, length;
00388 int ret, crc;
00389
00390 s->bytestream_start=
00391 s->bytestream= buf;
00392 s->bytestream_end= buf + buf_size;
00393
00394
00395 if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
00396 memcmp(s->bytestream, ff_mngsig, 8) != 0)
00397 return -1;
00398 s->bytestream+= 8;
00399 s->y=
00400 s->state=0;
00401
00402
00403 s->zstream.zalloc = ff_png_zalloc;
00404 s->zstream.zfree = ff_png_zfree;
00405 s->zstream.opaque = NULL;
00406 ret = inflateInit(&s->zstream);
00407 if (ret != Z_OK)
00408 return -1;
00409 for(;;) {
00410 int tag32;
00411 if (s->bytestream >= s->bytestream_end)
00412 goto fail;
00413 length = bytestream_get_be32(&s->bytestream);
00414 if (length > 0x7fffffff)
00415 goto fail;
00416 tag32 = bytestream_get_be32(&s->bytestream);
00417 tag = bswap_32(tag32);
00418 #ifdef DEBUG
00419 av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
00420 (tag & 0xff),
00421 ((tag >> 8) & 0xff),
00422 ((tag >> 16) & 0xff),
00423 ((tag >> 24) & 0xff), length);
00424 #endif
00425 switch(tag) {
00426 case MKTAG('I', 'H', 'D', 'R'):
00427 if (length != 13)
00428 goto fail;
00429 s->width = bytestream_get_be32(&s->bytestream);
00430 s->height = bytestream_get_be32(&s->bytestream);
00431 if(avcodec_check_dimensions(avctx, s->width, s->height)){
00432 s->width= s->height= 0;
00433 goto fail;
00434 }
00435 s->bit_depth = *s->bytestream++;
00436 s->color_type = *s->bytestream++;
00437 s->compression_type = *s->bytestream++;
00438 s->filter_type = *s->bytestream++;
00439 s->interlace_type = *s->bytestream++;
00440 crc = bytestream_get_be32(&s->bytestream);
00441 s->state |= PNG_IHDR;
00442 #ifdef DEBUG
00443 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",
00444 s->width, s->height, s->bit_depth, s->color_type,
00445 s->compression_type, s->filter_type, s->interlace_type);
00446 #endif
00447 break;
00448 case MKTAG('I', 'D', 'A', 'T'):
00449 if (!(s->state & PNG_IHDR))
00450 goto fail;
00451 if (!(s->state & PNG_IDAT)) {
00452
00453 avctx->width = s->width;
00454 avctx->height = s->height;
00455
00456 s->channels = ff_png_get_nb_channels(s->color_type);
00457 s->bits_per_pixel = s->bit_depth * s->channels;
00458 s->bpp = (s->bits_per_pixel + 7) >> 3;
00459 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
00460
00461 if (s->bit_depth == 8 &&
00462 s->color_type == PNG_COLOR_TYPE_RGB) {
00463 avctx->pix_fmt = PIX_FMT_RGB24;
00464 } else if (s->bit_depth == 8 &&
00465 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00466 avctx->pix_fmt = PIX_FMT_RGB32;
00467 } else if (s->bit_depth == 8 &&
00468 s->color_type == PNG_COLOR_TYPE_GRAY) {
00469 avctx->pix_fmt = PIX_FMT_GRAY8;
00470 } else if (s->bit_depth == 16 &&
00471 s->color_type == PNG_COLOR_TYPE_GRAY) {
00472 avctx->pix_fmt = PIX_FMT_GRAY16BE;
00473 } else if (s->bit_depth == 1 &&
00474 s->color_type == PNG_COLOR_TYPE_GRAY) {
00475 avctx->pix_fmt = PIX_FMT_MONOBLACK;
00476 } else if (s->bit_depth == 8 &&
00477 s->color_type == PNG_COLOR_TYPE_PALETTE) {
00478 avctx->pix_fmt = PIX_FMT_PAL8;
00479 } else {
00480 goto fail;
00481 }
00482 if(p->data[0])
00483 avctx->release_buffer(avctx, p);
00484
00485 p->reference= 0;
00486 if(avctx->get_buffer(avctx, p) < 0){
00487 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00488 goto fail;
00489 }
00490 p->pict_type= FF_I_TYPE;
00491 p->key_frame= 1;
00492 p->interlaced_frame = !!s->interlace_type;
00493
00494
00495 if (!s->interlace_type) {
00496 s->crow_size = s->row_size + 1;
00497 } else {
00498 s->pass = 0;
00499 s->pass_row_size = ff_png_pass_row_size(s->pass,
00500 s->bits_per_pixel,
00501 s->width);
00502 s->crow_size = s->pass_row_size + 1;
00503 }
00504 #ifdef DEBUG
00505 av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
00506 s->row_size, s->crow_size);
00507 #endif
00508 s->image_buf = p->data[0];
00509 s->image_linesize = p->linesize[0];
00510
00511 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
00512 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
00513
00514 s->last_row = av_mallocz(s->row_size);
00515 if (!s->last_row)
00516 goto fail;
00517 if (s->interlace_type ||
00518 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00519 s->tmp_row = av_malloc(s->row_size);
00520 if (!s->tmp_row)
00521 goto fail;
00522 }
00523
00524 s->crow_buf = av_malloc(s->row_size + 1);
00525 if (!s->crow_buf)
00526 goto fail;
00527 s->zstream.avail_out = s->crow_size;
00528 s->zstream.next_out = s->crow_buf;
00529 }
00530 s->state |= PNG_IDAT;
00531 if (png_decode_idat(s, length) < 0)
00532 goto fail;
00533
00534 crc = bytestream_get_be32(&s->bytestream);
00535 break;
00536 case MKTAG('P', 'L', 'T', 'E'):
00537 {
00538 int n, i, r, g, b;
00539
00540 if ((length % 3) != 0 || length > 256 * 3)
00541 goto skip_tag;
00542
00543 n = length / 3;
00544 for(i=0;i<n;i++) {
00545 r = *s->bytestream++;
00546 g = *s->bytestream++;
00547 b = *s->bytestream++;
00548 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
00549 }
00550 for(;i<256;i++) {
00551 s->palette[i] = (0xff << 24);
00552 }
00553 s->state |= PNG_PLTE;
00554 crc = bytestream_get_be32(&s->bytestream);
00555 }
00556 break;
00557 case MKTAG('t', 'R', 'N', 'S'):
00558 {
00559 int v, i;
00560
00561
00562 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
00563 length > 256 ||
00564 !(s->state & PNG_PLTE))
00565 goto skip_tag;
00566 for(i=0;i<length;i++) {
00567 v = *s->bytestream++;
00568 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
00569 }
00570 crc = bytestream_get_be32(&s->bytestream);
00571 }
00572 break;
00573 case MKTAG('I', 'E', 'N', 'D'):
00574 if (!(s->state & PNG_ALLIMAGE))
00575 goto fail;
00576 crc = bytestream_get_be32(&s->bytestream);
00577 goto exit_loop;
00578 default:
00579
00580 skip_tag:
00581 s->bytestream += length + 4;
00582 break;
00583 }
00584 }
00585 exit_loop:
00586 *picture= s->picture;
00587 *data_size = sizeof(AVFrame);
00588
00589 ret = s->bytestream - s->bytestream_start;
00590 the_end:
00591 inflateEnd(&s->zstream);
00592 av_freep(&s->crow_buf);
00593 av_freep(&s->last_row);
00594 av_freep(&s->tmp_row);
00595 return ret;
00596 fail:
00597 ret = -1;
00598 goto the_end;
00599 }
00600
00601 static av_cold int png_dec_init(AVCodecContext *avctx){
00602 PNGDecContext *s = avctx->priv_data;
00603
00604 avcodec_get_frame_defaults(&s->picture);
00605 avctx->coded_frame= &s->picture;
00606 dsputil_init(&s->dsp, avctx);
00607
00608 return 0;
00609 }
00610
00611 AVCodec png_decoder = {
00612 "png",
00613 CODEC_TYPE_VIDEO,
00614 CODEC_ID_PNG,
00615 sizeof(PNGDecContext),
00616 png_dec_init,
00617 NULL,
00618 NULL,
00619 decode_frame,
00620 0 ,
00621 NULL,
00622 .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
00623 };