From 6fd99e78def3c795bdd0bc31f3ae0998d24bc94c Mon Sep 17 00:00:00 2001 From: Peter Holik Date: Thu, 12 Dec 2013 16:11:35 +0100 Subject: png: add a standalone parser Useful for reading png images from a pipe. --- Changelog | 1 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/png_parser.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 4 +- 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 libavcodec/png_parser.c diff --git a/Changelog b/Changelog index e7a8e440fa..6ec0357945 100644 --- a/Changelog +++ b/Changelog @@ -51,6 +51,7 @@ version 10: - support for decoding through VDPAU in avconv (the -hwaccel option) - remove mp3_header_(de)compress bitstream filters - stereoscopic 3d metadata handling +- png standalone parser version 9: diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 6dfb9a0e9f..fb79c3276b 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -649,6 +649,7 @@ OBJS-$(CONFIG_MPEGAUDIO_PARSER) += mpegaudio_parser.o \ mpegaudiodecheader.o mpegaudiodata.o OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o \ mpeg12.o mpeg12data.o +OBJS-$(CONFIG_PNG_PARSER) += png_parser.o OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o OBJS-$(CONFIG_RV40_PARSER) += rv34_parser.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 044f5850dd..8b2bdc12ca 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -464,6 +464,7 @@ void avcodec_register_all(void) REGISTER_PARSER(MPEG4VIDEO, mpeg4video); REGISTER_PARSER(MPEGAUDIO, mpegaudio); REGISTER_PARSER(MPEGVIDEO, mpegvideo); + REGISTER_PARSER(PNG, png); REGISTER_PARSER(PNM, pnm); REGISTER_PARSER(RV30, rv30); REGISTER_PARSER(RV40, rv40); diff --git a/libavcodec/png_parser.c b/libavcodec/png_parser.c new file mode 100644 index 0000000000..00cbb82071 --- /dev/null +++ b/libavcodec/png_parser.c @@ -0,0 +1,124 @@ +/* + * PNG parser + * Copyright (c) 2009 Peter Holik + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * PNG parser + */ + +#include "libavutil/intreadwrite.h" +#include "libavutil/common.h" + +#include "parser.h" + +#define PNG_SIGNATURE UINT64_C(0x89504e470d0a1a0a) +#define MNG_SIGNATURE UINT64_C(0x8a4d4e470d0a1a0a) + +typedef struct PNGParseContext { + ParseContext pc; + + int chunk_pos; ///< position inside current chunk + int chunk_length; ///< length of the current chunk + int remaining_size; ///< remaining size of the current chunk +} PNGParseContext; + +static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + PNGParseContext *ppc = s->priv_data; + int next = END_NOT_FOUND; + int i = 0; + + *poutbuf_size = 0; + if (buf_size == 0) + return 0; + + if (!ppc->pc.frame_start_found) { + uint64_t state64 = ppc->pc.state64; + for (; i < buf_size; i++) { + state64 = (state64 << 8) | buf[i]; + if (state64 == PNG_SIGNATURE || + state64 == MNG_SIGNATURE) { + i++; + ppc->pc.frame_start_found = 1; + break; + } + } + ppc->pc.state64 = state64; + } else if (ppc->remaining_size) { + i = FFMIN(ppc->remaining_size, buf_size); + ppc->remaining_size -= i; + if (ppc->remaining_size) + goto flush; + if (ppc->chunk_pos == -1) { + next = i; + goto flush; + } + } + + for (; ppc->pc.frame_start_found && i < buf_size; i++) { + ppc->pc.state = (ppc->pc.state << 8) | buf[i]; + if (ppc->chunk_pos == 3) { + ppc->chunk_length = AV_RL32(&ppc->pc.state); + if (ppc->chunk_length > 0x7fffffff) { + ppc->chunk_pos = ppc->pc.frame_start_found = 0; + goto flush; + } + ppc->chunk_length += 4; + } else if (ppc->chunk_pos == 7) { + if (ppc->chunk_length >= buf_size - i) + ppc->remaining_size = ppc->chunk_length - buf_size + i + 1; + if (AV_RB32(&ppc->pc.state) == MKTAG('I', 'E', 'N', 'D')) { + if (ppc->remaining_size) + ppc->chunk_pos = -1; + else + next = ppc->chunk_length + i + 1; + break; + } else { + ppc->chunk_pos = 0; + if (ppc->remaining_size) + break; + else + i += ppc->chunk_length; + continue; + } + } + ppc->chunk_pos++; + } + +flush: + if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0) + return buf_size; + + ppc->chunk_pos = ppc->pc.frame_start_found = 0; + + *poutbuf = buf; + *poutbuf_size = buf_size; + return next; +} + +AVCodecParser ff_png_parser = { + .codec_ids = { AV_CODEC_ID_PNG }, + .priv_data_size = sizeof(PNGParseContext), + .parser_parse = png_parse, + .parser_close = ff_parse_close, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index 2c3f2f214a..c5a777bffd 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -27,8 +27,8 @@ */ #define LIBAVCODEC_VERSION_MAJOR 55 -#define LIBAVCODEC_VERSION_MINOR 28 -#define LIBAVCODEC_VERSION_MICRO 1 +#define LIBAVCODEC_VERSION_MINOR 29 +#define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \ -- cgit v1.2.3