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