[FFmpeg-devel] [PATCH] png parser
Peter Holik
peter
Sat Jun 13 13:54:46 CEST 2009
> On Thu, Jun 11, 2009 at 09:35:58AM +0200, Peter Holik wrote:
>> > cOn Wed, Jun 10, 2009 at 04:33:48PM +0200, Peter Holik wrote:
>> >> >> On Wed, Jun 10, 2009 at 01:27:10PM +0200, Peter Holik wrote:
>> >> >>> > On Wed, Jun 10, 2009 at 11:52:34AM +0200, Peter Holik wrote:
>> >> >>> >> >>> On Thu, Jun 04, 2009 at 10:40:55AM +0200, Peter Holik wrote:
>> >> >>> >> >>>> > On Mon, Jun 01, 2009 at 12:27:52PM +0200, Peter Holik wrote:
>> >> >>> >> >>>> >> Hi!
>> >> >>> >> >>>> >>
>> >> >>> >> >>>> >> I've now reworked my patch to really parse a png for end of frame.
>> >> >>> >> >>>> >>
>> >> >>> >> >>>> >> Now you can filter out png's of any stream like
>> >> >>> >> >>>> >>
>> >> >>> >> >>>> >> cat garbage a.png garbage b.png garbage | ffmpeg -f image2pipe -vcodec png -i
>> -
>> >> >>> c%d.png
>> >> >>> >> >>>> >>
>> >> >>> >> >>>> >> cu Peter
>> >> >>> >> >>>> >> Makefile | 1
>> >> >>> >> >>>> >> allcodecs.c | 1
>> >> >>> >> >>>> >> png_parser.c | 124
>> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> >> 3
>> >> >>> files
>> >> >>> >> >> changed, 126 insertions(+)
>> >> >>> >> >>>> >> 63f7cd1bb826726f1d2a9e7254410cd7ec98a81c png-parser.patch
>> >> >>> >> >>>> >> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
>> >> >>> >> >>>> >> index 14ef9f4..189231b 100644
>> >> >>> >> >>>> >> --- a/libavcodec/Makefile
>> >> >>> >> >>>> >> +++ b/libavcodec/Makefile
>> >> >>> >> >>>> >> @@ -411,6 +411,7 @@ OBJS-$(CONFIG_MLP_PARSER) += mlp_parser.o
>> mlp.o
>> >> >>> >> >>>> >> OBJS-$(CONFIG_MPEG4VIDEO_PARSER) += mpeg4video_parser.o h263.o
>> >> mpeg12data.o
>> >> >>> >> >>>> mpegvideo.o
>> >> >>> >> >>>> >> error_resilience.o
>> >> >>> >> >>>> >> OBJS-$(CONFIG_MPEGAUDIO_PARSER) += mpegaudio_parser.o
>> >> mpegaudiodecheader.o
>> >> >>> >> >>>> >> mpegaudiodata.o
>> >> >>> >> >>>> >> OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o mpeg12.o
>> >> mpeg12data.o
>> >> >>> >> >>>> mpegvideo.o
>> >> >>> >> >>>> >> error_resilience.o
>> >> >>> >> >>>> >> +OBJS-$(CONFIG_PNG_PARSER) += png_parser.o
>> >> >>> >> >>>> >> OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o
>> >> >>> OBJS-$(CONFIG_VC1_PARSER)
>> >> >>> >> >> += vc1_parser.o
>> >> >>> >> >>>> >> OBJS-$(CONFIG_VP3_PARSER) += vp3_parser.o
>> >> >>> >> >>>> >> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index
>> >> e8d5d27..362021f
>> >> >>> >> 100644
>> >> >>> >> >>>> >> --- a/libavcodec/allcodecs.c
>> >> >>> >> >>>> >> +++ b/libavcodec/allcodecs.c
>> >> >>> >> >>>> >> @@ -334,6 +334,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 (VC1, vc1);
>> >> >>> >> >>>> >> REGISTER_PARSER (VP3, vp3);
>> >> >>> >> >>>> >> diff --git a/libavcodec/png_parser.c b/libavcodec/png_parser.c new file mode
>> >> 100644
>> >> >>> >> >>>> >> index 0000000..64f2356
>> >> >>> >> >>>> >> --- /dev/null
>> >> >>> >> >>>> >> +++ b/libavcodec/png_parser.c
>> >> >>> >> >>>> >> @@ -0,0 +1,124 @@
>> >> >>> >> >>>> >> +/*
>> >> >>> >> >>>> >> + * PNG parser
>> >> >>> >> >>>> >> + * Copyright (c) 2009 Peter Holik
>> >> >>> >> >>>> >> + *
>> >> >>> >> >>>> >> + * This file is part of FFmpeg.
>> >> >>> >> >>>> >> + *
>> >> >>> >> >>>> >> + * FFmpeg 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. + *
>> >> >>> >> >>>> >> + * FFmpeg 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
>> >> >>> >> >> FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin
>> Street,
>> >> >>> Fifth
>> >> >>> >> >> Floor, Boston, MA 02110-1301 USA + */
>> >> >>> >> >>>> >> +
>> >> >>> >> >>>> >> +/**
>> >> >>> >> >>>> >> + * @file libavcodec/png_parser.c
>> >> >>> >> >>>> >> + * PNG parser
>> >> >>> >> >>>> >> + */
>> >> >>> >> >>>> >> +
>> >> >>> >> >>>> >> +#include "parser.h"
>> >> >>> >> >>>> >> +#include "bytestream.h"
>> >> >>> >> >>>> >> +#include "png.h"
>> >> >>> >> >>>> >> +
>> >> >>> >> >>>> >> +static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx, +
>> >> >>> >> >> const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t
>> *buf,
>> >> int
>> >> >>> >> >> buf_size)
>> >> >>> >> >>>> >> +{
>> >> >>> >> >>>> >> + ParseContext *pc = s->priv_data;
>> >> >>> >> >>>> >> + uint8_t *bytestream;
>> >> >>> >> >>>> >> + uint8_t *bytestream_start;
>> >> >>> >> >>>> >> + uint8_t *bytestream_end;
>> >> >>> >> >>>> >> +
>> >> >>> >> >>>> >> + /* EOF considered as end of frame */
>> >> >>> >> >>>> >> + if (buf_size == 0)
>> >> >>> >> >>>> >> + goto flush;
>> >> >>> >> >>>> >> +
>> >> >>> >> >>>> >> + *poutbuf = NULL;
>> >> >>> >> >>>> >> + *poutbuf_size = 0;
>> >> >>> >> >>>> >> +
>> >> >>> >> >>>> >> + /* first add new buffer to parsebuffer */
>> >> >>> >> >>>> >> + bytestream_start = av_fast_realloc(pc->buffer, &pc->buffer_size, +
>> >> >>> >> >> pc->index + buf_size + FF_INPUT_BUFFER_PADDING_SIZE); +
>> if(!bytestream_start)
>> >> >>> >> >>>> >> + goto fail;
>> >> >>> >> >>>> >> + pc->buffer = bytestream_start;
>> >> >>> >> >>>> >> + memcpy(&pc->buffer[pc->index], buf, buf_size);
>> >> >>> >> >>>> >
>> >> >>> >> >>>> > mandatory copying is unacceptable
>> >> >>> >> >>>> > the case where each input is exactly a png frame for example should be free of
>> any
>> >> >>> >> >> timeconsuming operation
>> >> >>> >> >>>> >
>> >> >>> >> >>>> > maybe ff_combine_frame() would be usefull
>> >> >>> >> >>>>
>> >> >>> >> >>>> But ff_combine_frame also does memcpy, so there is no difference to my approach.
>> >> >>> >> >>>
>> >> >>> >> >>> ff_combine_frame() does not always memcpy, your code does always
>> >> >>> >> >>
>> >> >>> >> >> change my patch to only memcpy when input is not exactly a png frame.
>> >> >>> >> >>
>> >> >>> >> >> cu Peter
>> >> >>> >>
>> >> >>> >> ping
>> >> >>> >
>> >> >>> > does your code do something else than ff_combine_frame() ?
>> >> >>> > if no its duplicate code
>> >> >>> > if yes its either worse or ff_combine_frame() should be improved
>> >> >>> >
>> >> >>>
>> >> >>> 1. ff_combine_frame is overhead
>> >> >>
>> >> >> elaborate
>> >> >
>> >> > there is more done than i need (pc->overread, pc->state,...)
>> >> >
>> >> >>> 2. i use a variable from PasteContext also altered by ff_combine_frame
>> >> >>
>> >> >> which variable is how used by you so that it conflicts with ff_combine_frame?
>> >> >
>> >> > * i use pc->last_index to remember where to parse from, because i do not like
>> >> > to parse every time from the beginning.
>> >> >
>> >> > * to return a packet starting with a png signature i skip the garbage if any
>> >> > and need this information in my combine_frame version.
>> >> >
>> >> >
>> >> > well, i will try to use ff_combine_frame()
>> >>
>> >> now with ff_combine_frame()
>> >
>> > well yes, but ive meant it should be used like also in the other parsers,
>> > but lets rather look at actual problems of your approuch ... if you can
>> > fix them and your code is simpler i wouldnt mind it but i doubt it
>> >
>> > example:
>> > 2 packets
>> > packet 1: frame 0, frame 1, fra
>> > packet 2: me 2, frame 3 frame 4
>> >
>> > your approuch will memcpy the whole 2nd packet, the normal ff_combine_frame
>> > code will copy only "frame 2"
>>
>> ff_combine_frame copy as much as you tell (ff_combine_frame don't know where frame 2 ends)
>>
>>
>> you are right, but i don't think that this situation with your 2 packets ever happens.
>>
>> usually there is the opposite situation:
>>
>> packet 1: frame 0(part 1)
>> packet 2: frame 0(last part), frame 1(part 1)
>>
>> because image2pipe uses only a 4kByte Buffer.
>
> thats all fine as long as the parser is just used with image2pipe and
> image2pipe buffer is not changed ...
> i think these are not reasonable assumtions
>
>
>>
>>
>> My main problem with png is, that i never know the total size of the picture.
>> I read chunks (with a length)
>>
>> And most troubles are if for example the length (32bits) of the chunk-header
>> is across two packets so i decided to read/copy ahead (of course copy more than i really would
>> need).
>
> some rough idea:
>
> uint64_t state= state_from_context;
>
> for(i=0; i<buf_size; i++){
> state= (state<<8) + buf[i];
> if(state == startcode){
> we have a new png start
> }
> }
>
>
> for(; i<buf_size; i++){
> int skip= FFMIN(remaining-8, buf_size-i-1);
> if(skip>0){
> i += skip;
> remaining -= skip;
> }
> state= (state<<8) + buf[i];
> remaining--;
> if(remaining == 0){
> remaining= state&0xFFFFFFFF;
> if(state>>32 == end)
> png end
> }
> }
Thanks for inspiration
Here now my rework of png_parser
cu Peter
-------------- next part --------------
A non-text attachment was scrubbed...
Name: png-parser.patch
Type: text/x-diff
Size: 5768 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090613/849c2871/attachment.patch>
More information about the ffmpeg-devel
mailing list