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