[FFmpeg-devel] [PATCH 2/2] MxPEG decoder
Anatoly Nenashev
anatoly.nenashev
Tue Nov 2 04:25:41 CET 2010
On 02.11.2010 03:23, Stefano Sabatini wrote:
> On date Tuesday 2010-11-02 03:16:04 +0300, Anatoly Nenashev encoded:
>
>> On 02.11.2010 01:32, Michael Niedermayer wrote:
>>
> [...]
>
>> diff --git a/libavformat/Makefile b/libavformat/Makefile
>> index 40adb53..ae6e05b 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..917cc47
>> --- /dev/null
>> +++ b/libavformat/mxg.c
>> @@ -0,0 +1,195 @@
>> +/*
>> + * 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 "avformat.h"
>> +#include "avio.h"
>>
>
>> +#include<libavutil/intreadwrite.h>
>>
> "libavutil/intreadwrite.h"
>
> also should be listed before the local header (standard headers,
> FFmpeg headers starting from libavutil... current dir headers)
>
>
>> +
>> +#define VIDEO_STREAM_INDEX 0
>> +#define AUDIO_STREAM_INDEX 1
>> +#define DEFAULT_PACKET_SIZE 1024
>>
>
>> +
>> +
>>
> nit++: one empty line
>
>
>> +typedef struct MXGContext {
>> + uint8_t *buffer;
>> + unsigned int buffer_size;
>> + unsigned int current_pos;
>> + uint16_t state;
>> + int vop_found;
>> + 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;
>> +
>> + mxg->buffer = av_malloc(DEFAULT_PACKET_SIZE);
>>
>
>> + if (!mxg->buffer) {
>> + av_log(s, AV_LOG_ERROR, "mxg demuxer error in av_malloc()\n");
>> + return AVERROR(ENOMEM);
>> + }
>>
> for all these memory error you can skip the error message, and simply
> return ENOMEM
>
>
>> + mxg->buffer_size = DEFAULT_PACKET_SIZE;
>> + mxg->current_pos = 0;
>> + mxg->state = 0;
>> + mxg->vop_found = 0;
>> + mxg->dts = AV_NOPTS_VALUE;
>> +
>> + return 0;
>> +}
>> +
>> +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)) {
>>
> nit++: while_(
>
>
>> + const uint8_t data = get_byte(s->pb);
>> + unsigned int found_frame_end = 0;
>> + mxg->state = (mxg->state<< 8) | data;
>> + mxg->buffer[mxg->current_pos++] = data;
>> +
>> + if (mxg->state == 0xffd9) {
>> + found_frame_end = mxg->current_pos;
>> + } else if (mxg->state == 0xffd8) {
>> + if (mxg->vop_found) {
>> + //emulating frame end
>> + found_frame_end = mxg->current_pos - 2;
>> + url_fseek(s->pb, -2, SEEK_CUR);
>> + } else {
>> + mxg->vop_found = 1;
>> + }
>> + } else if (mxg->state == 0xfffe) {
>> + size = get_be16(s->pb);
>> + if (url_feof(s->pb) || url_ferror(s->pb))
>> + return AVERROR_EOF;
>> +
>>
>
>> + if (mxg->current_pos> mxg->current_pos + size)
>> + {
>>
> nit++: if (...)_{
>
>
>> + av_log(s, AV_LOG_ERROR, "buffer overflow\n");
>> + return AVERROR(ENOMEM);
>> + }
>> + mxg->buffer = av_fast_realloc(mxg->buffer,&mxg->buffer_size,
>> + mxg->current_pos + size);
>>
>
>> + if (!mxg->buffer) {
>> + av_log(s, AV_LOG_ERROR, "mxg demuxer error in av_fast_realloc()\n");
>> + return AVERROR(ENOMEM);
>> + }
>>
> ditto
>
>
>> + if (size< 2) {
>> + av_log(s, AV_LOG_ERROR, "wrong comment buffer size\n");
>> + return AVERROR(EINVAL);
>> + }
>> + mxg->buffer[mxg->current_pos++] = size>> 8;
>> + mxg->buffer[mxg->current_pos++] = size& 0xff;
>> +
>> + ret = get_buffer(s->pb, mxg->buffer + mxg->current_pos, size - 2);
>> + if (ret< 0)
>> + return ret;
>> + if (ret>= 16&& !strncmp(mxg->buffer + mxg->current_pos, "MXF", 3)) {
>> + mxg->dts = AV_RL64(mxg->buffer + mxg->current_pos + 8);
>> + }
>> + mxg->current_pos += ret;
>> + mxg->state = 0;
>> + } else if (mxg->state == 0xffed) {
>> + mxg->current_pos -= 2;
>> + size = get_be16(s->pb);
>> + if (url_feof(s->pb) || url_ferror(s->pb))
>> + return AVERROR_EOF;
>> + if (size<= 14)
>> + return AVERROR(EINVAL);
>> + url_fskip(s->pb, 12);
>> + if (url_feof(s->pb) || url_ferror(s->pb))
>> + return AVERROR_EOF;
>> + size -= 14;
>> + ret = av_get_packet(s->pb, pkt, size);
>> + if (ret< 0)
>> + return ret;
>> +
>> + pkt->stream_index = AUDIO_STREAM_INDEX;
>> + mxg->state = 0;
>> +
>> + return pkt->size;
>> + }
>> +
>> + if (found_frame_end) {
>> + mxg->vop_found = 0;
>> + mxg->current_pos = 0;
>> + ret = av_new_packet(pkt, found_frame_end);
>> + if (ret< 0)
>> + return ret;
>> + memcpy(pkt->data, mxg->buffer, found_frame_end);
>> + pkt->stream_index = VIDEO_STREAM_INDEX;
>> + pkt->dts = mxg->dts;
>> + return pkt->size;
>> + }
>> +
>> + if (mxg->current_pos>= mxg->buffer_size) {
>> + if (mxg->current_pos> mxg->current_pos + DEFAULT_PACKET_SIZE) {
>> + av_log(s, AV_LOG_ERROR, "buffer overflow\n");
>> + return AVERROR(ENOMEM);
>> + }
>>
>
>> + mxg->buffer = av_fast_realloc(mxg->buffer,&mxg->buffer_size,
>> + mxg->current_pos + DEFAULT_PACKET_SIZE);
>> + if (!mxg->buffer) {
>> + av_log(s, AV_LOG_ERROR, "mxg demuxer error in av_fast_realloc()\n");
>> + return AVERROR(ENOMEM);
>>
> ditto
>
>
>> + }
>> + }
>> + }
>> +
>> + return AVERROR_EOF;
>> +}
>> +
>> +static int mxg_close(struct AVFormatContext *s)
>> +{
>> + MXGContext *mxg = s->priv_data;
>> + av_freep(&mxg->buffer);
>> + return 0;
>> +}
>> +
>>
>
>> +AVInputFormat mxg_demuxer = {
>> + "mxg",
>> + NULL_IF_CONFIG_SMALL("MxPEG Clip File Format"),
>> + sizeof(MXGContext),
>> + NULL,
>> + mxg_read_header,
>> + mxg_read_packet,
>> + mxg_close,
>> + .extensions = "mxg"
>>
> please use named fields, helps legibility and ease ABI break updates.
>
All remarks are considered in the attached patch.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mxg_v5.patch
Type: text/x-patch
Size: 7456 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20101102/cf9c4bf8/attachment.bin>
More information about the ffmpeg-devel
mailing list