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