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 if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
00381 av_log(NULL, AV_LOG_WARNING, "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
00382 return 0;
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;
00400
00401 FFSWAP(AVFrame *, s->current_picture, s->last_picture);
00402 avctx->coded_frame= s->current_picture;
00403 p = s->current_picture;
00404
00405
00406 if (buf_size < 8 ||
00407 memcmp(buf, ff_pngsig, 8) != 0 &&
00408 memcmp(buf, ff_mngsig, 8) != 0)
00409 return -1;
00410
00411 bytestream2_init(&s->gb, buf + 8, buf_size - 8);
00412 s->y=
00413 s->state=0;
00414
00415
00416 s->zstream.zalloc = ff_png_zalloc;
00417 s->zstream.zfree = ff_png_zfree;
00418 s->zstream.opaque = NULL;
00419 ret = inflateInit(&s->zstream);
00420 if (ret != Z_OK)
00421 return -1;
00422 for(;;) {
00423 if (bytestream2_get_bytes_left(&s->gb) <= 0)
00424 goto fail;
00425 length = bytestream2_get_be32(&s->gb);
00426 if (length > 0x7fffffff)
00427 goto fail;
00428 tag = bytestream2_get_le32(&s->gb);
00429 av_dlog(avctx, "png: tag=%c%c%c%c length=%u\n",
00430 (tag & 0xff),
00431 ((tag >> 8) & 0xff),
00432 ((tag >> 16) & 0xff),
00433 ((tag >> 24) & 0xff), length);
00434 switch(tag) {
00435 case MKTAG('I', 'H', 'D', 'R'):
00436 if (length != 13)
00437 goto fail;
00438 s->width = bytestream2_get_be32(&s->gb);
00439 s->height = bytestream2_get_be32(&s->gb);
00440 if(av_image_check_size(s->width, s->height, 0, avctx)){
00441 s->width= s->height= 0;
00442 goto fail;
00443 }
00444 s->bit_depth = bytestream2_get_byte(&s->gb);
00445 s->color_type = bytestream2_get_byte(&s->gb);
00446 s->compression_type = bytestream2_get_byte(&s->gb);
00447 s->filter_type = bytestream2_get_byte(&s->gb);
00448 s->interlace_type = bytestream2_get_byte(&s->gb);
00449 bytestream2_skip(&s->gb, 4);
00450 s->state |= PNG_IHDR;
00451 av_dlog(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
00452 s->width, s->height, s->bit_depth, s->color_type,
00453 s->compression_type, s->filter_type, s->interlace_type);
00454 break;
00455 case MKTAG('I', 'D', 'A', 'T'):
00456 if (!(s->state & PNG_IHDR))
00457 goto fail;
00458 if (!(s->state & PNG_IDAT)) {
00459
00460 avctx->width = s->width;
00461 avctx->height = s->height;
00462
00463 s->channels = ff_png_get_nb_channels(s->color_type);
00464 s->bits_per_pixel = s->bit_depth * s->channels;
00465 s->bpp = (s->bits_per_pixel + 7) >> 3;
00466 s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
00467
00468 if (s->bit_depth == 8 &&
00469 s->color_type == PNG_COLOR_TYPE_RGB) {
00470 avctx->pix_fmt = PIX_FMT_RGB24;
00471 } else if (s->bit_depth == 8 &&
00472 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00473 avctx->pix_fmt = PIX_FMT_RGB32;
00474 } else if (s->bit_depth == 8 &&
00475 s->color_type == PNG_COLOR_TYPE_GRAY) {
00476 avctx->pix_fmt = PIX_FMT_GRAY8;
00477 } else if (s->bit_depth == 16 &&
00478 s->color_type == PNG_COLOR_TYPE_GRAY) {
00479 avctx->pix_fmt = PIX_FMT_GRAY16BE;
00480 } else if (s->bit_depth == 16 &&
00481 s->color_type == PNG_COLOR_TYPE_RGB) {
00482 avctx->pix_fmt = PIX_FMT_RGB48BE;
00483 } else if (s->bit_depth == 1 &&
00484 s->color_type == PNG_COLOR_TYPE_GRAY) {
00485 avctx->pix_fmt = PIX_FMT_MONOBLACK;
00486 } else if (s->bit_depth == 8 &&
00487 s->color_type == PNG_COLOR_TYPE_PALETTE) {
00488 avctx->pix_fmt = PIX_FMT_PAL8;
00489 } else if (s->bit_depth == 8 &&
00490 s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
00491 avctx->pix_fmt = PIX_FMT_Y400A;
00492 } else {
00493 goto fail;
00494 }
00495 if(p->data[0])
00496 avctx->release_buffer(avctx, p);
00497
00498 p->reference= 0;
00499 if(avctx->get_buffer(avctx, p) < 0){
00500 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00501 goto fail;
00502 }
00503 p->pict_type= AV_PICTURE_TYPE_I;
00504 p->key_frame= 1;
00505 p->interlaced_frame = !!s->interlace_type;
00506
00507
00508 if (!s->interlace_type) {
00509 s->crow_size = s->row_size + 1;
00510 } else {
00511 s->pass = 0;
00512 s->pass_row_size = ff_png_pass_row_size(s->pass,
00513 s->bits_per_pixel,
00514 s->width);
00515 s->crow_size = s->pass_row_size + 1;
00516 }
00517 av_dlog(avctx, "row_size=%d crow_size =%d\n",
00518 s->row_size, s->crow_size);
00519 s->image_buf = p->data[0];
00520 s->image_linesize = p->linesize[0];
00521
00522 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
00523 memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
00524
00525 s->last_row = av_mallocz(s->row_size);
00526 if (!s->last_row)
00527 goto fail;
00528 if (s->interlace_type ||
00529 s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
00530 s->tmp_row = av_malloc(s->row_size);
00531 if (!s->tmp_row)
00532 goto fail;
00533 }
00534
00535 crow_buf_base = av_malloc(s->row_size + 16);
00536 if (!crow_buf_base)
00537 goto fail;
00538
00539
00540 s->crow_buf = crow_buf_base + 15;
00541 s->zstream.avail_out = s->crow_size;
00542 s->zstream.next_out = s->crow_buf;
00543 }
00544 s->state |= PNG_IDAT;
00545 if (png_decode_idat(s, length) < 0)
00546 goto fail;
00547 bytestream2_skip(&s->gb, 4);
00548 break;
00549 case MKTAG('P', 'L', 'T', 'E'):
00550 {
00551 int n, i, r, g, b;
00552
00553 if ((length % 3) != 0 || length > 256 * 3)
00554 goto skip_tag;
00555
00556 n = length / 3;
00557 for(i=0;i<n;i++) {
00558 r = bytestream2_get_byte(&s->gb);
00559 g = bytestream2_get_byte(&s->gb);
00560 b = bytestream2_get_byte(&s->gb);
00561 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
00562 }
00563 for(;i<256;i++) {
00564 s->palette[i] = (0xff << 24);
00565 }
00566 s->state |= PNG_PLTE;
00567 bytestream2_skip(&s->gb, 4);
00568 }
00569 break;
00570 case MKTAG('t', 'R', 'N', 'S'):
00571 {
00572 int v, i;
00573
00574
00575 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
00576 length > 256 ||
00577 !(s->state & PNG_PLTE))
00578 goto skip_tag;
00579 for(i=0;i<length;i++) {
00580 v = bytestream2_get_byte(&s->gb);
00581 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
00582 }
00583 bytestream2_skip(&s->gb, 4);
00584 }
00585 break;
00586 case MKTAG('I', 'E', 'N', 'D'):
00587 if (!(s->state & PNG_ALLIMAGE))
00588 goto fail;
00589 bytestream2_skip(&s->gb, 4);
00590 goto exit_loop;
00591 default:
00592
00593 skip_tag:
00594 bytestream2_skip(&s->gb, length + 4);
00595 break;
00596 }
00597 }
00598 exit_loop:
00599
00600 if(s->last_picture->data[0] != NULL) {
00601 if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
00602 int i, j;
00603 uint8_t *pd = s->current_picture->data[0];
00604 uint8_t *pd_last = s->last_picture->data[0];
00605
00606 for(j=0; j < s->height; j++) {
00607 for(i=0; i < s->width * s->bpp; i++) {
00608 pd[i] += pd_last[i];
00609 }
00610 pd += s->image_linesize;
00611 pd_last += s->image_linesize;
00612 }
00613 }
00614 }
00615
00616 *picture= *s->current_picture;
00617 *data_size = sizeof(AVFrame);
00618
00619 ret = bytestream2_tell(&s->gb);
00620 the_end:
00621 inflateEnd(&s->zstream);
00622 av_free(crow_buf_base);
00623 s->crow_buf = NULL;
00624 av_freep(&s->last_row);
00625 av_freep(&s->tmp_row);
00626 return ret;
00627 fail:
00628 ret = -1;
00629 goto the_end;
00630 }
00631
00632 static av_cold int png_dec_init(AVCodecContext *avctx){
00633 PNGDecContext *s = avctx->priv_data;
00634
00635 s->current_picture = &s->picture1;
00636 s->last_picture = &s->picture2;
00637 avcodec_get_frame_defaults(&s->picture1);
00638 avcodec_get_frame_defaults(&s->picture2);
00639 dsputil_init(&s->dsp, avctx);
00640
00641 return 0;
00642 }
00643
00644 static av_cold int png_dec_end(AVCodecContext *avctx)
00645 {
00646 PNGDecContext *s = avctx->priv_data;
00647
00648 if (s->picture1.data[0])
00649 avctx->release_buffer(avctx, &s->picture1);
00650 if (s->picture2.data[0])
00651 avctx->release_buffer(avctx, &s->picture2);
00652
00653 return 0;
00654 }
00655
00656 AVCodec ff_png_decoder = {
00657 .name = "png",
00658 .type = AVMEDIA_TYPE_VIDEO,
00659 .id = CODEC_ID_PNG,
00660 .priv_data_size = sizeof(PNGDecContext),
00661 .init = png_dec_init,
00662 .close = png_dec_end,
00663 .decode = decode_frame,
00664 .capabilities = CODEC_CAP_DR1 ,
00665 .long_name = NULL_IF_CONFIG_SMALL("PNG image"),
00666 };