diff mbox

[FFmpeg-devel] add Falcom Xanadu demuxer

Message ID 20170415133606.21049-1-james.darnley@gmail.com
State New
Headers show

Commit Message

James Darnley April 15, 2017, 1:36 p.m. UTC
---
 libavformat/Makefile     |  1 +
 libavformat/allformats.c |  1 +
 libavformat/falcom_xa.c  | 98 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 libavformat/falcom_xa.c

Comments

James Darnley April 15, 2017, 3:04 p.m. UTC | #1
On 2017-04-15 15:36, James Darnley wrote:
> add Falcom Xanadu demuxer

I mean Xanadu Next, not the original one.
James Almer April 15, 2017, 3:56 p.m. UTC | #2
On 4/15/2017 10:36 AM, James Darnley wrote:
> ---
>  libavformat/Makefile     |  1 +
>  libavformat/allformats.c |  1 +
>  libavformat/falcom_xa.c  | 98 ++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 100 insertions(+)
>  create mode 100644 libavformat/falcom_xa.c

Changelog/version.h/etc before you push.

> 
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 6bdfbe6789..06b6b5da57 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -158,6 +158,7 @@ OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
>  OBJS-$(CONFIG_EAC3_DEMUXER)              += ac3dec.o rawdec.o
>  OBJS-$(CONFIG_EAC3_MUXER)                += rawenc.o
>  OBJS-$(CONFIG_EPAF_DEMUXER)              += epafdec.o pcm.o
> +OBJS-$(CONFIG_FALCOM_XA_DEMUXER)         += falcom_xa.o
>  OBJS-$(CONFIG_FFM_DEMUXER)               += ffmdec.o
>  OBJS-$(CONFIG_FFM_MUXER)                 += ffmenc.o
>  OBJS-$(CONFIG_FFMETADATA_DEMUXER)        += ffmetadec.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index 09e62c3cfc..0c23ea9df0 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -117,6 +117,7 @@ static void register_all(void)
>      REGISTER_MUXDEMUX(EAC3,             eac3);
>      REGISTER_DEMUXER (EPAF,             epaf);
>      REGISTER_MUXER   (F4V,              f4v);
> +    REGISTER_DEMUXER (FALCOM_XA,        falcom_xa);

Maybe just Xanadu?

>      REGISTER_MUXDEMUX(FFM,              ffm);
>      REGISTER_MUXDEMUX(FFMETADATA,       ffmetadata);
>      REGISTER_MUXER   (FIFO,             fifo);
> diff --git a/libavformat/falcom_xa.c b/libavformat/falcom_xa.c
> new file mode 100644
> index 0000000000..4c5f32a1b6
> --- /dev/null
> +++ b/libavformat/falcom_xa.c
> @@ -0,0 +1,98 @@
> +/*
> + * Falcom Xanadu demuxer
> + * Copyright (c) 2016 James Darnley

2017?

> + *
> + * 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"
> +
> +typedef struct FalcomXADemuxContext {
> +    const AVClass *class;
> +    unsigned frame_size;
> +} FalcomXADemuxContext;
> +
> +static av_cold
> +int falcom_xa_read_header(AVFormatContext *s)

A probe function should be added if possible.

> +{
> +    int sample_rate, bit_depth, channels;
> +    AVStream *st;
> +    AVIOContext *pb = s->pb;
> +
> +    avio_seek(pb, 22, SEEK_SET);

avio_skip() is more in line with most demuxers.

What are those first 22 bytes? I assume header magic (which you should
check in a probe function) and maybe some version byte?
If the latter, better check it here.

> +    channels = avio_rl16(pb);
> +    if (channels != 2)
> +        return AVERROR_INVALIDDATA;

If there are real world files with more than two channels, you could
instead return PATCHWELCOME and even ask for a sample.

> +
> +    sample_rate = avio_rl32(pb);
> +    if (sample_rate != 44100)
> +        return AVERROR_INVALIDDATA;

Same.

> +
> +    avio_seek(pb, 34, SEEK_SET);

avio_skip().

> +    bit_depth = avio_rl16(pb);
> +    if (bit_depth != 16)
> +        return AVERROR_INVALIDDATA;
> +
> +    avio_seek(pb, 44, SEEK_SET);

Same.

> +
> +    st = avformat_new_stream(s, NULL);
> +    if (!st)
> +        return AVERROR(ENOMEM);
> +
> +    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
> +    st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS;
> +    st->codecpar->sample_rate = sample_rate;
> +    st->codecpar->channels = channels;
> +    st->codecpar->block_align = 2048;
> +
> +    return 0;
> +}
> +
> +static
> +int falcom_xa_read_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> +    FalcomXADemuxContext *ctx = s->priv_data;
> +
> +    if (ctx->frame_size == 0) {
> +        unsigned tell = avio_tell(s->pb);

avio_tell() returns int64_t.

> +        unsigned size = avio_rl32(s->pb);
> +        av_log(ctx, AV_LOG_DEBUG, "read size %u (0x%x) at offset %u (0x%x)\n",
> +                size, size, tell, tell);
> +        if (size % 2048)

Nit: size & 2047.

> +            return AVERROR_INVALIDDATA;
> +        ctx->frame_size = size;
> +        avio_skip(s->pb, 4); // unknown bytes
> +    }
> +
> +    ctx->frame_size -= 2048;
> +    return av_get_packet(s->pb, pkt, 2048);
> +}
> +
> +static const AVClass falcom_xa_demux_class = {
> +    .class_name = "Falcom Xanadu demuxer",
> +    .item_name  = av_default_item_name,
> +    .version    = LIBAVUTIL_VERSION_INT,
> +};
> +
> +AVInputFormat ff_falcom_xa_demuxer = {
> +    .name           = "falcom_xa",
> +    .long_name      = NULL_IF_CONFIG_SMALL("Falcom Xanadu demuxer"),
> +    .priv_data_size = sizeof(FalcomXADemuxContext),
> +    .read_header    = falcom_xa_read_header,
> +    .read_packet    = falcom_xa_read_packet,
> +    .priv_class     = &falcom_xa_demux_class,

No known extension/s? And did you try using AVFMT_GENERIC_INDEX flag
to see if it enables seeking without a custom read_seek() function?

> +};
>
James Darnley April 15, 2017, 4:05 p.m. UTC | #3
On 2017-04-15 17:56, James Almer wrote:
> On 4/15/2017 10:36 AM, James Darnley wrote:
>> ---
>>  libavformat/Makefile     |  1 +
>>  libavformat/allformats.c |  1 +
>>  libavformat/falcom_xa.c  | 98 ++++++++++++++++++++++++++++++++++++++++++++++++
>>  3 files changed, 100 insertions(+)
>>  create mode 100644 libavformat/falcom_xa.c
> 
> Changelog/version.h/etc before you push.

Sure.

>>
>> diff --git a/libavformat/Makefile b/libavformat/Makefile
>> index 6bdfbe6789..06b6b5da57 100644
>> --- a/libavformat/Makefile
>> +++ b/libavformat/Makefile
>> @@ -158,6 +158,7 @@ OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
>>  OBJS-$(CONFIG_EAC3_DEMUXER)              += ac3dec.o rawdec.o
>>  OBJS-$(CONFIG_EAC3_MUXER)                += rawenc.o
>>  OBJS-$(CONFIG_EPAF_DEMUXER)              += epafdec.o pcm.o
>> +OBJS-$(CONFIG_FALCOM_XA_DEMUXER)         += falcom_xa.o
>>  OBJS-$(CONFIG_FFM_DEMUXER)               += ffmdec.o
>>  OBJS-$(CONFIG_FFM_MUXER)                 += ffmenc.o
>>  OBJS-$(CONFIG_FFMETADATA_DEMUXER)        += ffmetadec.o
>> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
>> index 09e62c3cfc..0c23ea9df0 100644
>> --- a/libavformat/allformats.c
>> +++ b/libavformat/allformats.c
>> @@ -117,6 +117,7 @@ static void register_all(void)
>>      REGISTER_MUXDEMUX(EAC3,             eac3);
>>      REGISTER_DEMUXER (EPAF,             epaf);
>>      REGISTER_MUXER   (F4V,              f4v);
>> +    REGISTER_DEMUXER (FALCOM_XA,        falcom_xa);
> 
> Maybe just Xanadu?
> 
>>      REGISTER_MUXDEMUX(FFM,              ffm);
>>      REGISTER_MUXDEMUX(FFMETADATA,       ffmetadata);
>>      REGISTER_MUXER   (FIFO,             fifo);
>> diff --git a/libavformat/falcom_xa.c b/libavformat/falcom_xa.c
>> new file mode 100644
>> index 0000000000..4c5f32a1b6
>> --- /dev/null
>> +++ b/libavformat/falcom_xa.c
>> @@ -0,0 +1,98 @@
>> +/*
>> + * Falcom Xanadu demuxer
>> + * Copyright (c) 2016 James Darnley
> 
> 2017?

I wrote it in November 2016 and have only just remembered about it.

>> + *
>> + * 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"
>> +
>> +typedef struct FalcomXADemuxContext {
>> +    const AVClass *class;
>> +    unsigned frame_size;
>> +} FalcomXADemuxContext;
>> +
>> +static av_cold
>> +int falcom_xa_read_header(AVFormatContext *s)
> 
> A probe function should be added if possible.
> 
>> +{
>> +    int sample_rate, bit_depth, channels;
>> +    AVStream *st;
>> +    AVIOContext *pb = s->pb;
>> +
>> +    avio_seek(pb, 22, SEEK_SET);
> 
> avio_skip() is more in line with most demuxers.
> 
> What are those first 22 bytes? I assume header magic (which you should
> check in a probe function) and maybe some version byte?
> If the latter, better check it here.

It is a riff or wave header with incorrect data.  I can probe for the
commonalities for all the files but I couldn't be bothered at the time.

>> +    channels = avio_rl16(pb);
>> +    if (channels != 2)
>> +        return AVERROR_INVALIDDATA;
> 
> If there are real world files with more than two channels, you could
> instead return PATCHWELCOME and even ask for a sample.

I wonder.  I only wanted it to decode the 24 files from the game.

>> +
>> +    sample_rate = avio_rl32(pb);
>> +    if (sample_rate != 44100)
>> +        return AVERROR_INVALIDDATA;
> 
> Same.
> 
>> +
>> +    avio_seek(pb, 34, SEEK_SET);
> 
> avio_skip().
> 
>> +    bit_depth = avio_rl16(pb);
>> +    if (bit_depth != 16)
>> +        return AVERROR_INVALIDDATA;
>> +
>> +    avio_seek(pb, 44, SEEK_SET);
> 
> Same.

All noted.

>> +
>> +    st = avformat_new_stream(s, NULL);
>> +    if (!st)
>> +        return AVERROR(ENOMEM);
>> +
>> +    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
>> +    st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS;
>> +    st->codecpar->sample_rate = sample_rate;
>> +    st->codecpar->channels = channels;
>> +    st->codecpar->block_align = 2048;
>> +
>> +    return 0;
>> +}
>> +
>> +static
>> +int falcom_xa_read_packet(AVFormatContext *s, AVPacket *pkt)
>> +{
>> +    FalcomXADemuxContext *ctx = s->priv_data;
>> +
>> +    if (ctx->frame_size == 0) {
>> +        unsigned tell = avio_tell(s->pb);
> 
> avio_tell() returns int64_t.

I know but none of the files are larger than 12 MiB.

>> +        unsigned size = avio_rl32(s->pb);
>> +        av_log(ctx, AV_LOG_DEBUG, "read size %u (0x%x) at offset %u (0x%x)\n",
>> +                size, size, tell, tell);
>> +        if (size % 2048)
> 
> Nit: size & 2047.

Also noted.

>> +            return AVERROR_INVALIDDATA;
>> +        ctx->frame_size = size;
>> +        avio_skip(s->pb, 4); // unknown bytes
>> +    }
>> +
>> +    ctx->frame_size -= 2048;
>> +    return av_get_packet(s->pb, pkt, 2048);
>> +}
>> +
>> +static const AVClass falcom_xa_demux_class = {
>> +    .class_name = "Falcom Xanadu demuxer",
>> +    .item_name  = av_default_item_name,
>> +    .version    = LIBAVUTIL_VERSION_INT,
>> +};
>> +
>> +AVInputFormat ff_falcom_xa_demuxer = {
>> +    .name           = "falcom_xa",
>> +    .long_name      = NULL_IF_CONFIG_SMALL("Falcom Xanadu demuxer"),
>> +    .priv_data_size = sizeof(FalcomXADemuxContext),
>> +    .read_header    = falcom_xa_read_header,
>> +    .read_packet    = falcom_xa_read_packet,
>> +    .priv_class     = &falcom_xa_demux_class,
> 
> No known extension/s? And did you try using AVFMT_GENERIC_INDEX flag
> to see if it enables seeking without a custom read_seek() function?

The files are all .dec so I guess I could add that at the least.  And
I'll try the seeking.
diff mbox

Patch

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 6bdfbe6789..06b6b5da57 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -158,6 +158,7 @@  OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
 OBJS-$(CONFIG_EAC3_DEMUXER)              += ac3dec.o rawdec.o
 OBJS-$(CONFIG_EAC3_MUXER)                += rawenc.o
 OBJS-$(CONFIG_EPAF_DEMUXER)              += epafdec.o pcm.o
+OBJS-$(CONFIG_FALCOM_XA_DEMUXER)         += falcom_xa.o
 OBJS-$(CONFIG_FFM_DEMUXER)               += ffmdec.o
 OBJS-$(CONFIG_FFM_MUXER)                 += ffmenc.o
 OBJS-$(CONFIG_FFMETADATA_DEMUXER)        += ffmetadec.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 09e62c3cfc..0c23ea9df0 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -117,6 +117,7 @@  static void register_all(void)
     REGISTER_MUXDEMUX(EAC3,             eac3);
     REGISTER_DEMUXER (EPAF,             epaf);
     REGISTER_MUXER   (F4V,              f4v);
+    REGISTER_DEMUXER (FALCOM_XA,        falcom_xa);
     REGISTER_MUXDEMUX(FFM,              ffm);
     REGISTER_MUXDEMUX(FFMETADATA,       ffmetadata);
     REGISTER_MUXER   (FIFO,             fifo);
diff --git a/libavformat/falcom_xa.c b/libavformat/falcom_xa.c
new file mode 100644
index 0000000000..4c5f32a1b6
--- /dev/null
+++ b/libavformat/falcom_xa.c
@@ -0,0 +1,98 @@ 
+/*
+ * Falcom Xanadu demuxer
+ * Copyright (c) 2016 James Darnley
+ *
+ * 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"
+
+typedef struct FalcomXADemuxContext {
+    const AVClass *class;
+    unsigned frame_size;
+} FalcomXADemuxContext;
+
+static av_cold
+int falcom_xa_read_header(AVFormatContext *s)
+{
+    int sample_rate, bit_depth, channels;
+    AVStream *st;
+    AVIOContext *pb = s->pb;
+
+    avio_seek(pb, 22, SEEK_SET);
+    channels = avio_rl16(pb);
+    if (channels != 2)
+        return AVERROR_INVALIDDATA;
+
+    sample_rate = avio_rl32(pb);
+    if (sample_rate != 44100)
+        return AVERROR_INVALIDDATA;
+
+    avio_seek(pb, 34, SEEK_SET);
+    bit_depth = avio_rl16(pb);
+    if (bit_depth != 16)
+        return AVERROR_INVALIDDATA;
+
+    avio_seek(pb, 44, SEEK_SET);
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS;
+    st->codecpar->sample_rate = sample_rate;
+    st->codecpar->channels = channels;
+    st->codecpar->block_align = 2048;
+
+    return 0;
+}
+
+static
+int falcom_xa_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    FalcomXADemuxContext *ctx = s->priv_data;
+
+    if (ctx->frame_size == 0) {
+        unsigned tell = avio_tell(s->pb);
+        unsigned size = avio_rl32(s->pb);
+        av_log(ctx, AV_LOG_DEBUG, "read size %u (0x%x) at offset %u (0x%x)\n",
+                size, size, tell, tell);
+        if (size % 2048)
+            return AVERROR_INVALIDDATA;
+        ctx->frame_size = size;
+        avio_skip(s->pb, 4); // unknown bytes
+    }
+
+    ctx->frame_size -= 2048;
+    return av_get_packet(s->pb, pkt, 2048);
+}
+
+static const AVClass falcom_xa_demux_class = {
+    .class_name = "Falcom Xanadu demuxer",
+    .item_name  = av_default_item_name,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVInputFormat ff_falcom_xa_demuxer = {
+    .name           = "falcom_xa",
+    .long_name      = NULL_IF_CONFIG_SMALL("Falcom Xanadu demuxer"),
+    .priv_data_size = sizeof(FalcomXADemuxContext),
+    .read_header    = falcom_xa_read_header,
+    .read_packet    = falcom_xa_read_packet,
+    .priv_class     = &falcom_xa_demux_class,
+};