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