[FFmpeg-devel] [PATCH 3/3] DSD Stream File (DSF) demuxer

Michael Niedermayer michaelni at gmx.at
Mon Apr 14 16:09:33 CEST 2014


On Mon, Apr 14, 2014 at 06:22:54PM +1000, Peter Ross wrote:
> Signed-off-by: Peter Ross <pross at xvid.org>
> ---
>  doc/general.texi         |   1 +
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/dsfdec.c     | 143 +++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 146 insertions(+)
>  create mode 100644 libavformat/dsfdec.c
> 
> diff --git a/doc/general.texi b/doc/general.texi
> index b1118b9..8bf0379 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -259,6 +259,7 @@ library:
>  @item Deluxe Paint Animation    @tab   @tab X
>  @item DFA                       @tab   @tab X
>      @tab This format is used in Chronomaster game
> + at item DSD Stream File (DSF)     @tab   @tab X
>  @item DV video                  @tab X @tab X
>  @item DXA                       @tab   @tab X
>      @tab This format is used in the non-Windows version of the Feeble Files
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 18b8817..b1e29fe 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -118,6 +118,7 @@ OBJS-$(CONFIG_DIRAC_DEMUXER)             += diracdec.o rawdec.o
>  OBJS-$(CONFIG_DIRAC_MUXER)               += rawenc.o
>  OBJS-$(CONFIG_DNXHD_DEMUXER)             += dnxhddec.o rawdec.o
>  OBJS-$(CONFIG_DNXHD_MUXER)               += rawenc.o
> +OBJS-$(CONFIG_DSF_DEMUXER)               += dsfdec.o
>  OBJS-$(CONFIG_DSICIN_DEMUXER)            += dsicin.o
>  OBJS-$(CONFIG_DTSHD_DEMUXER)             += dtshddec.o
>  OBJS-$(CONFIG_DTS_DEMUXER)               += dtsdec.o rawdec.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index b5cbbef..f6d78ae 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -106,6 +106,7 @@ void av_register_all(void)
>      REGISTER_DEMUXER (DFA,              dfa);
>      REGISTER_MUXDEMUX(DIRAC,            dirac);
>      REGISTER_MUXDEMUX(DNXHD,            dnxhd);
> +    REGISTER_DEMUXER (DSF,              dsf);
>      REGISTER_DEMUXER (DSICIN,           dsicin);
>      REGISTER_MUXDEMUX(DTS,              dts);
>      REGISTER_DEMUXER (DTSHD,            dtshd);
> diff --git a/libavformat/dsfdec.c b/libavformat/dsfdec.c
> new file mode 100644
> index 0000000..c62a013
> --- /dev/null
> +++ b/libavformat/dsfdec.c
> @@ -0,0 +1,143 @@
> +/*
> + * DSD Stream File (DSF) demuxer
> + * Copyright (c) 2014 Peter Ross
> + *
> + * 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 "avformat.h"
> +#include "internal.h"
> +#include "id3v2.h"
> +
> +typedef struct {
> +    uint64_t data_end;
> +} DSFContext;
> +
> +static int dsf_probe(AVProbeData *p)
> +{
> +    if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
> +        return 0;
> +    return AVPROBE_SCORE_EXTENSION;
> +}
> +
> +static const uint64_t dsf_channel_layout[] = {
> +    0,
> +    AV_CH_LAYOUT_MONO,
> +    AV_CH_LAYOUT_STEREO,
> +    AV_CH_LAYOUT_SURROUND,
> +    AV_CH_LAYOUT_QUAD,
> +    AV_CH_LAYOUT_4POINT0,
> +    AV_CH_LAYOUT_5POINT0_BACK,
> +    AV_CH_LAYOUT_5POINT1_BACK,
> +};
> +
> +static void read_id3(AVFormatContext *s, uint64_t id3pos)
> +{
> +    ID3v2ExtraMeta *id3v2_extra_meta = NULL;
> +    if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
> +        return;
> +
> +    ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
> +    if (id3v2_extra_meta)
> +        ff_id3v2_parse_apic(s, &id3v2_extra_meta);
> +    ff_id3v2_free_extra_meta(&id3v2_extra_meta);
> +}
> +
> +static int dsf_read_header(AVFormatContext *s)
> +{
> +    DSFContext *dsf = s->priv_data;
> +    AVIOContext *pb = s->pb;
> +    AVStream *st;
> +    uint64_t id3pos;
> +    int channel_type;
> +
> +    avio_skip(pb, 4);
> +    if (avio_rl64(pb) != 28)
> +        return AVERROR_INVALIDDATA;
> +
> +    avio_skip(pb, 8);
> +    id3pos = avio_rl64(pb);
> +    if (pb->seekable) {
> +        read_id3(s, id3pos);
> +        avio_seek(pb, 28, SEEK_SET);
> +    }
> +
> +    /* fmt chunk */
> +
> +    if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52 ||
> +        avio_rl32(pb) != 1 || avio_rl32(pb))
> +        return AVERROR_INVALIDDATA;
> +
> +    st = avformat_new_stream(s, NULL);
> +    if (!st)
> +        return AVERROR(ENOMEM);
> +

> +    channel_type = avio_rl32(pb);
> +    if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
> +        st->codec->channel_layout = dsf_channel_layout[channel_type];

channel_type is int so it could be negative and this would then
segfault


> +    if (!st->codec->channel_layout)
> +        avpriv_request_sample(s, "channel type %i", channel_type);
> +
> +    st->codec->codec_type   = AVMEDIA_TYPE_AUDIO;
> +    st->codec->channels     = avio_rl32(pb);
> +    st->codec->sample_rate  = avio_rl32(pb) / 8;
> +
> +    switch(avio_rl32(pb)) {
> +    case 1: st->codec->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR;break;
> +    case 8: st->codec->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;

> +    default:
> +        return AVERROR_INVALIDDATA;

should use avpriv_request_sample() or print some error mesage


> +    }
> +
> +    avio_skip(pb, 8);

> +    st->codec->block_align = avio_rl32(pb) * st->codec->channels;

possible integer overflow

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

I am the wisest man alive, for I know one thing, and that is that I know
nothing. -- Socrates
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140414/aae3830e/attachment.asc>


More information about the ffmpeg-devel mailing list