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