[FFmpeg-devel] [PATCH 2/2] MxPEG decoder

Michael Niedermayer michaelni
Wed Nov 17 14:49:02 CET 2010


On Mon, Nov 15, 2010 at 03:27:07AM +0300, Anatoly Nenashev wrote:
> On 13.11.2010 04:24, Michael Niedermayer wrote:
>> On Thu, Nov 11, 2010 at 02:16:27AM +0300, Anatoly Nenashev wrote:
>>    
>>> On 10.11.2010 18:28, Michael Niedermayer wrote:
>>>      
>>>> On Wed, Nov 10, 2010 at 06:08:47PM +0300, Anatoly Nenashev wrote:
>>>>
>>>>        
>>>>> [...]
>>>>> If to read more than 16 byte at once then it is required additional
>>>>> operations of memcpy and memmove.
>>>>> For example. If I read buffer of 256 bytes in which audio packet
>>>>> available in position 10 and size 100 then I need
>>>>> to copy data of size 100-10=90 in new audio packet and move data of size
>>>>> 256-100=156 in internal buffer. I think this may reduce the performance.
>>>>>
>>>>>          
>>>> you dont need to move it.
>>>>
>>>> and the overhead of all your code executed once every 16 bytes will reduce
>>>> performance more than an occasional memcpy(). That said it can be done
>>>> without memcpy by accessing the internal buffer but iam not suggesting this
>>>> to be done.
>>>>
>>>>        
>> There are many ways to implement this, one simple one is
>> you have a internal buffer
>> you read into it
>> you return AVPackets that point into this buffer and have destruct=NULL
>>
>>    
>
> Implemented this way.
>

>  Makefile     |    1 
>  allformats.c |    1 
>  mxg.c        |  256 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 258 insertions(+)
> b2283573c132194317817cf27f165a3092faae71  mxg_v16.patch
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index e62a5ea..d3e3cd0 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -147,6 +147,7 @@ OBJS-$(CONFIG_MTV_DEMUXER)               += mtv.o
>  OBJS-$(CONFIG_MVI_DEMUXER)               += mvi.o
>  OBJS-$(CONFIG_MXF_DEMUXER)               += mxfdec.o mxf.o
>  OBJS-$(CONFIG_MXF_MUXER)                 += mxfenc.o mxf.o audiointerleave.o
> +OBJS-$(CONFIG_MXG_DEMUXER)               += mxg.o
>  OBJS-$(CONFIG_NC_DEMUXER)                += ncdec.o
>  OBJS-$(CONFIG_NSV_DEMUXER)               += nsvdec.o
>  OBJS-$(CONFIG_NULL_MUXER)                += nullenc.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index e35f4f8..4afbef5 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -142,6 +142,7 @@ void av_register_all(void)
>      REGISTER_DEMUXER  (MVI, mvi);
>      REGISTER_MUXDEMUX (MXF, mxf);
>      REGISTER_MUXER    (MXF_D10, mxf_d10);
> +    REGISTER_DEMUXER  (MXG, mxg);
>      REGISTER_DEMUXER  (NC, nc);
>      REGISTER_DEMUXER  (NSV, nsv);
>      REGISTER_MUXER    (NULL, null);
> diff --git a/libavformat/mxg.c b/libavformat/mxg.c
> new file mode 100644
> index 0000000..cdfb992
> --- /dev/null
> +++ b/libavformat/mxg.c
> @@ -0,0 +1,256 @@
> +/*
> + * MxPEG clip file demuxer
> + * Copyright (c) 2010 Anatoly Nenashev
> + *
> + * 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
> + */
> +
> +#include "libavutil/intreadwrite.h"
> +#include "libavcodec/mjpeg.h"
> +#include "avformat.h"
> +#include "avio.h"
> +
> +#define VIDEO_STREAM_INDEX 0
> +#define AUDIO_STREAM_INDEX 1
> +#define DEFAULT_PACKET_SIZE 1024
> +#define OVERREAD_SIZE 3
> +
> +typedef struct MXGContext {
> +    uint8_t *buffer;
> +    uint8_t *buffer_ptr;
> +    uint8_t *soi_ptr;
> +    unsigned int buffer_size;
> +    int64_t dts;
> +    unsigned int cache_size;
> +} MXGContext;
> +

> +static int mxg_read_header(AVFormatContext *s, AVFormatParameters *ap)
> +{
> +    AVStream *video_st = 0, *audio_st = 0;

the =0 is useless


[...]
> +static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> +    int ret;
> +    unsigned int size;
> +    uint8_t *startmarker_ptr, *end, *search_end, marker;
> +    MXGContext *mxg = s->priv_data;
> +
> +    while (!url_feof(s->pb) && !url_ferror(s->pb)){
> +        if (mxg->cache_size <= OVERREAD_SIZE) {
> +            /* reallocate internal buffer */
> +            ret = mxg_reallocate_buffer(mxg, DEFAULT_PACKET_SIZE + OVERREAD_SIZE +
> +                                        FF_INPUT_BUFFER_PADDING_SIZE);
> +            if (ret < 0)
> +                return ret;
> +
> +            /* read data */
> +            ret = get_buffer(s->pb, mxg->buffer_ptr + mxg->cache_size,
> +                             DEFAULT_PACKET_SIZE + OVERREAD_SIZE - mxg->cache_size);
> +            if (ret < 0)
> +                return ret;
> +
> +            end = mxg->buffer_ptr + ret + mxg->cache_size;
> +        } else {
> +            /* will search only in cached data */
> +            end = mxg->buffer_ptr + mxg->cache_size;
> +        }
> +
> +        /* find start marker - 0xff */
> +        if (mxg->buffer_ptr < end - OVERREAD_SIZE) {
> +            search_end = end - OVERREAD_SIZE;
> +            startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
> +        } else {
> +            search_end = end;
> +            startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
> +            if (startmarker_ptr >= search_end - 1 ||
> +                *(startmarker_ptr + 1) != EOI) break;
> +        }
> +
> +        if (startmarker_ptr != search_end) { /* start marker found */
> +            marker = *(startmarker_ptr + 1);
> +            mxg->buffer_ptr = startmarker_ptr + 2;
> +            mxg->cache_size = end - mxg->buffer_ptr;
> +
> +            if (marker == SOI) {
> +                mxg->soi_ptr = startmarker_ptr;
> +            } else if (marker == EOI) {
> +                if (!mxg->soi_ptr) {
> +                    av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
> +                    continue;
> +                }
> +
> +                pkt->pts = pkt->dts = mxg->dts;
> +                pkt->stream_index = VIDEO_STREAM_INDEX;
> +                pkt->destruct = NULL;
> +                pkt->size = mxg->buffer_ptr - mxg->soi_ptr;
> +                pkt->data = mxg->soi_ptr;
> +
> +                if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
> +                    if (mxg->cache_size > 0) {
> +                        memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
> +                    }
> +
> +                    mxg->buffer_ptr = mxg->buffer;
> +                }
> +                mxg->soi_ptr = 0;
> +
> +                return pkt->size;
> +            } else if ( (SOF0 <= marker && marker <= SOF15) ||
> +                        (SOS  <= marker && marker <= COM) ) {
> +                /* all other markers that start marker segment also contain
> +                   length value (see specification for JPEG Annex B.1) */
> +                size = AV_RB16(mxg->buffer_ptr);
> +                if (size < 2)
> +                    return AVERROR(EINVAL);
> +
> +                if (mxg->cache_size < size) {

> +                    ret = mxg_reallocate_buffer(mxg, size + FF_INPUT_BUFFER_PADDING_SIZE);
> +                    if (ret < 0)
> +                        return ret;
> +                    startmarker_ptr = mxg->buffer_ptr - 2;
> +
> +                    ret = get_buffer(s->pb, mxg->buffer_ptr + mxg->cache_size,
> +                                     size - mxg->cache_size);
> +                    if (ret < 0)
> +                        return ret;

i think the get_buffer() could also be moved into mxg_reallocate_buffer() and
it be renamed accordingly. That should simplify the code

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The misfortune of the wise is better than the prosperity of the fool.
-- Epicurus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20101117/82df68d2/attachment.pgp>



More information about the ffmpeg-devel mailing list