[FFmpeg-devel] [PATCH 2/2] MxPEG decoder
Michael Niedermayer
michaelni
Sun Nov 7 14:28:00 CET 2010
On Sat, Nov 06, 2010 at 05:10:41AM +0300, Anatoly Nenashev wrote:
> On 06.11.2010 01:50, Michael Niedermayer wrote:
>> On Fri, Nov 05, 2010 at 09:50:36PM +0300, Anatoly Nenashev wrote:
>>
>>
>>> +static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
>>> +{
>>> + int ret, size;
>>> + MXGContext *mxg = s->priv_data;
>>> +
>>> + while (!url_feof(s->pb)&& !url_ferror(s->pb)) {
>>> + uint8_t data = 0;
>>> + unsigned int current_pad;
>>> +
>>> + /* search for start marker - 0xff */
>>> + while (mxg->current_pos + FF_INPUT_BUFFER_PADDING_SIZE< mxg->buffer_size) {
>>> + data = get_byte(s->pb);
>>> + mxg->buffer[mxg->current_pos++] = data;
>>> + if (data == 0xff) break;
>>> + }
>>>
>> you can check 4 bytes at a time with something like:
>> (x&~(x+0x01010101))& 0x80808080
>> this should be faster
>>
>>
>
> Ok, thanks for your improvements, reimplemented this way. Also marker
> segment size parsing is added.
> Makefile | 1
> allformats.c | 1
> mxg.c | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 227 insertions(+)
> 7f1d8269bff394c81c62c3d5b59d47a3cc97032c mxg_v12.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..5260086
> --- /dev/null
> +++ b/libavformat/mxg.c
> @@ -0,0 +1,225 @@
> +/*
> + * 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
> +
> +typedef struct MXGContext {
> + uint8_t *buffer;
> + unsigned int buffer_size;
> + unsigned int current_pos;
> + int64_t dts;
> +} MXGContext;
> +
> +static int mxg_read_header(AVFormatContext *s, AVFormatParameters *ap)
> +{
> + AVStream *video_st = 0, *audio_st = 0;
> + MXGContext *mxg = s->priv_data;
> +
> + /* video parameters will be extracted from the compressed bitstream */
> + video_st = av_new_stream(s, VIDEO_STREAM_INDEX);
> + if (!video_st)
> + return AVERROR(ENOMEM);
> + video_st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
> + video_st->codec->codec_id = CODEC_ID_MXPEG;
> + av_set_pts_info(video_st, 64, 1, 1000000);
> +
> + audio_st = av_new_stream(s, AUDIO_STREAM_INDEX);
> + if (!audio_st)
> + return AVERROR(ENOMEM);
> + audio_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
> + audio_st->codec->codec_id = CODEC_ID_PCM_ALAW;
> + audio_st->codec->channels = 1;
> + audio_st->codec->sample_rate = 8000;
> + audio_st->codec->bits_per_coded_sample = 8;
> + audio_st->codec->block_align = 1;
> + av_set_pts_info(audio_st, 64, 1, 1000000);
> +
> + mxg->buffer = av_malloc(DEFAULT_PACKET_SIZE);
> + if (!mxg->buffer)
> + return AVERROR(ENOMEM);
> +
> + mxg->buffer_size = DEFAULT_PACKET_SIZE;
> + mxg->current_pos = 0;
> + mxg->dts = AV_NOPTS_VALUE;
> +
> + return 0;
> +}
> +
> +static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> + int ret, size;
> + unsigned int current_pad;
> + MXGContext *mxg = s->priv_data;
> +
> + while (!url_feof(s->pb) && !url_ferror(s->pb)) {
> + uint8_t marker = 0;
> +
> + /* search for start marker - 0xff */
> + while (mxg->current_pos + FF_INPUT_BUFFER_PADDING_SIZE < mxg->buffer_size) {
> + uint32_t x;
> + uint8_t *p = mxg->buffer + mxg->current_pos;
> +
> + ret = get_partial_buffer(s->pb, p, 4);
calling this every 4 bytes is slow
> + if (ret < 0)
> + return ret;
> +
> + x = *(uint32_t*)p;
> + if (x & (~(x+0x01010101)) & 0x80808080) {
> + if (p[0] == 0xff && ret > 0) {
> + mxg->current_pos += 1;
> + url_fseek(s->pb, 1 - ret, SEEK_CUR);
these seek back calls can fail
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Dictatorship naturally arises out of democracy, and the most aggravated
form of tyranny and slavery out of the most extreme liberty. -- Plato
-------------- 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/20101107/11bceaf3/attachment.pgp>
More information about the ffmpeg-devel
mailing list