📄 pngdec.c
字号:
}
}
}
}
the_end: ;
}
}
static int png_decode_idat(PNGDecContext *s, int length)
{
int ret;
s->zstream.avail_in = length;
s->zstream.next_in = s->bytestream;
s->bytestream += length;
if(s->bytestream > s->bytestream_end)
return -1;
/* decode one line if possible */
while (s->zstream.avail_in > 0) {
ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
return -1;
}
if (s->zstream.avail_out == 0) {
if (!(s->state & PNG_ALLIMAGE)) {
png_handle_row(s);
}
s->zstream.avail_out = s->crow_size;
s->zstream.next_out = s->crow_buf;
}
}
return 0;
}
static int decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
uint8_t *buf, int buf_size)
{
PNGDecContext * const s = avctx->priv_data;
AVFrame *picture = data;
AVFrame * const p= (AVFrame*)&s->picture;
uint32_t tag, length;
int ret, crc;
s->bytestream_start=
s->bytestream= buf;
s->bytestream_end= buf + buf_size;
/* check signature */
if (memcmp(s->bytestream, ff_pngsig, 8) != 0)
return -1;
s->bytestream+= 8;
s->y=
s->state=0;
// memset(s, 0, sizeof(PNGDecContext));
/* init the zlib */
s->zstream.zalloc = ff_png_zalloc;
s->zstream.zfree = ff_png_zfree;
s->zstream.opaque = NULL;
ret = inflateInit(&s->zstream);
if (ret != Z_OK)
return -1;
for(;;) {
int tag32;
if (s->bytestream >= s->bytestream_end)
goto fail;
length = bytestream_get_be32(&s->bytestream);
if (length > 0x7fffffff)
goto fail;
tag32 = bytestream_get_be32(&s->bytestream);
tag = bswap_32(tag32);
#ifdef DEBUG
av_log(avctx, AV_LOG_DEBUG, "png: tag=%c%c%c%c length=%u\n",
(tag & 0xff),
((tag >> 8) & 0xff),
((tag >> 16) & 0xff),
((tag >> 24) & 0xff), length);
#endif
switch(tag) {
case MKTAG('I', 'H', 'D', 'R'):
if (length != 13)
goto fail;
s->width = bytestream_get_be32(&s->bytestream);
s->height = bytestream_get_be32(&s->bytestream);
if(avcodec_check_dimensions(avctx, s->width, s->height)){
s->width= s->height= 0;
goto fail;
}
s->bit_depth = *s->bytestream++;
s->color_type = *s->bytestream++;
s->compression_type = *s->bytestream++;
s->filter_type = *s->bytestream++;
s->interlace_type = *s->bytestream++;
crc = bytestream_get_be32(&s->bytestream);
s->state |= PNG_IHDR;
#ifdef DEBUG
av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
s->width, s->height, s->bit_depth, s->color_type,
s->compression_type, s->filter_type, s->interlace_type);
#endif
break;
case MKTAG('I', 'D', 'A', 'T'):
if (!(s->state & PNG_IHDR))
goto fail;
if (!(s->state & PNG_IDAT)) {
/* init image info */
avctx->width = s->width;
avctx->height = s->height;
s->channels = ff_png_get_nb_channels(s->color_type);
s->bits_per_pixel = s->bit_depth * s->channels;
s->bpp = (s->bits_per_pixel + 7) >> 3;
s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_RGB) {
avctx->pix_fmt = PIX_FMT_RGB24;
} else if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
avctx->pix_fmt = PIX_FMT_RGB32;
} else if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_GRAY8;
} else if (s->bit_depth == 16 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_GRAY16BE;
} else if (s->bit_depth == 1 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_MONOBLACK;
} else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
avctx->pix_fmt = PIX_FMT_PAL8;
} else {
goto fail;
}
if(p->data[0])
avctx->release_buffer(avctx, p);
p->reference= 0;
if(avctx->get_buffer(avctx, p) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
goto fail;
}
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
p->interlaced_frame = !!s->interlace_type;
/* compute the compressed row size */
if (!s->interlace_type) {
s->crow_size = s->row_size + 1;
} else {
s->pass = 0;
s->pass_row_size = ff_png_pass_row_size(s->pass,
s->bits_per_pixel,
s->width);
s->crow_size = s->pass_row_size + 1;
}
#ifdef DEBUG
av_log(avctx, AV_LOG_DEBUG, "row_size=%d crow_size =%d\n",
s->row_size, s->crow_size);
#endif
s->image_buf = p->data[0];
s->image_linesize = p->linesize[0];
/* copy the palette if needed */
if (s->color_type == PNG_COLOR_TYPE_PALETTE)
memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
/* empty row is used if differencing to the first row */
s->last_row = av_mallocz(s->row_size);
if (!s->last_row)
goto fail;
if (s->interlace_type ||
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
s->tmp_row = av_malloc(s->row_size);
if (!s->tmp_row)
goto fail;
}
/* compressed row */
s->crow_buf = av_malloc(s->row_size + 1);
if (!s->crow_buf)
goto fail;
s->zstream.avail_out = s->crow_size;
s->zstream.next_out = s->crow_buf;
}
s->state |= PNG_IDAT;
if (png_decode_idat(s, length) < 0)
goto fail;
/* skip crc */
crc = bytestream_get_be32(&s->bytestream);
break;
case MKTAG('P', 'L', 'T', 'E'):
{
int n, i, r, g, b;
if ((length % 3) != 0 || length > 256 * 3)
goto skip_tag;
/* read the palette */
n = length / 3;
for(i=0;i<n;i++) {
r = *s->bytestream++;
g = *s->bytestream++;
b = *s->bytestream++;
s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
}
for(;i<256;i++) {
s->palette[i] = (0xff << 24);
}
s->state |= PNG_PLTE;
crc = bytestream_get_be32(&s->bytestream);
}
break;
case MKTAG('t', 'R', 'N', 'S'):
{
int v, i;
/* read the transparency. XXX: Only palette mode supported */
if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
length > 256 ||
!(s->state & PNG_PLTE))
goto skip_tag;
for(i=0;i<length;i++) {
v = *s->bytestream++;
s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
}
crc = bytestream_get_be32(&s->bytestream);
}
break;
case MKTAG('I', 'E', 'N', 'D'):
if (!(s->state & PNG_ALLIMAGE))
goto fail;
crc = bytestream_get_be32(&s->bytestream);
goto exit_loop;
default:
/* skip tag */
skip_tag:
s->bytestream += length + 4;
break;
}
}
exit_loop:
*picture= *(AVFrame*)&s->picture;
*data_size = sizeof(AVPicture);
ret = s->bytestream - s->bytestream_start;
the_end:
inflateEnd(&s->zstream);
av_freep(&s->crow_buf);
av_freep(&s->last_row);
av_freep(&s->tmp_row);
return ret;
fail:
ret = -1;
goto the_end;
}
static int png_dec_init(AVCodecContext *avctx){
PNGDecContext *s = avctx->priv_data;
avcodec_get_frame_defaults((AVFrame*)&s->picture);
avctx->coded_frame= (AVFrame*)&s->picture;
return 0;
}
AVCodec png_decoder = {
"png",
CODEC_TYPE_VIDEO,
CODEC_ID_PNG,
sizeof(PNGDecContext),
png_dec_init,
NULL,
NULL, //decode_end,
decode_frame,
0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
NULL
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -