diff mbox series

[FFmpeg-devel,v2,2/2] avformat: add demuxer for argonaut games' ASF format

Message ID 20200119083325.3377-3-zane@zanevaniperen.com
State Superseded
Headers show
Series Argonaut Games ASF and ADPCM decoding support
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork fail Failed to apply patch

Commit Message

Zane van Iperen Jan. 19, 2020, 8:33 a.m. UTC
Adds support for the custom ASF container used by some Argonaut Games'
games, such as 'Croc! Legend of the Gobbos', and 'Croc 2'.

Can also handle the sample files in:
https://samples.ffmpeg.org/game-formats/brender/part2.zip

Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
Reviewed-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 Changelog                |   1 +
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/argo_asf.c   | 260 +++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |   4 +-
 5 files changed, 265 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/argo_asf.c

Comments

Moritz Barsnick Jan. 19, 2020, 4:34 p.m. UTC | #1
Just one more:

On Sun, Jan 19, 2020 at 08:33:45 +0000, Zane van Iperen wrote:

> +    if (av_match_ext(p->filename, "asf"))
> +        score += AVPROBE_SCORE_EXTENSION;

Just a remark: The asf demuxer (for Microsoft's "Advanced System
Format") also considers ".asf" as a valid extension. I don't know
whether this clashes in any way though. You'd probably need to throw
both types of "ASF" files at these probes.

Cheers,
Moritz
Carl Eugen Hoyos Jan. 19, 2020, 6:52 p.m. UTC | #2
Am So., 19. Jan. 2020 um 09:34 Uhr schrieb Zane van Iperen
<zane@zanevaniperen.com>:

> +static int argo_asf_probe(const AVProbeData *p)
> +{
> +    int score;
> +    ArgoASFFileHeader hdr;
> +
> +    if (p->buf_size < ASF_FILE_HEADER_SIZE)
> +        return AVPROBE_SCORE_RETRY;

Either assert that AVPROBE_PADDING_SIZE >=
ASF_FILE_HEADER_SIZE or (better) remove this hunk.

> +
> +    score = 0;
> +    if (av_match_ext(p->filename, "asf"))
> +        score += AVPROBE_SCORE_EXTENSION;

Remove this hunk, we only match the extension if no
other way of recognizing a format works (tga).

> +
> +    argo_asf_parse_file_header(&hdr, p->buf);
> +
> +    if(hdr.magic != ASF_TAG)
> +        return 0;
> +
> +    if (argo_asf_is_known_version(&hdr))
> +        score += 25;

Return MAX/2 + 1 in this case, feel free to ignore the other fields.

I wonder if "argo" isn't a better name for the format than "argo_asf".

Carl Eugen
Zane van Iperen Jan. 19, 2020, 9:42 p.m. UTC | #3
On 20/1/20 2:34 am, Moritz Barsnick wrote:
> 
> Just one more:
> 
> On Sun, Jan 19, 2020 at 08:33:45 +0000, Zane van Iperen wrote:
> 
>> +    if (av_match_ext(p->filename, "asf"))
>> +        score += AVPROBE_SCORE_EXTENSION;
> 
> Just a remark: The asf demuxer (for Microsoft's "Advanced System
> Format") also considers ".asf" as a valid extension. I don't know
> whether this clashes in any way though. You'd probably need to throw
> both types of "ASF" files at these probes.
> 
> Cheers,
> Moritz
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
> 

They might have the same extension, but that's where the similarities
end. I just had a look at the Microsoft ASF spec and they start with a
GUID, so a "proper" ASF will hit the `hdr.magic != ASF_TAG` check.

The probing code in asfdec_f.c seems to confirm this as well.


Zane
Zane van Iperen Jan. 19, 2020, 10:44 p.m. UTC | #4
20/1/20 4:52 am, Carl Eugen Hoyos пишет:
> 
> Am So., 19. Jan. 2020 um 09:34 Uhr schrieb Zane van Iperen
> <zane@zanevaniperen.com>:
> 
>> +static int argo_asf_probe(const AVProbeData *p)
>> +{
>> +    int score;
>> +    ArgoASFFileHeader hdr;
>> +
>> +    if (p->buf_size < ASF_FILE_HEADER_SIZE)
>> +        return AVPROBE_SCORE_RETRY;
> 
> Either assert that AVPROBE_PADDING_SIZE >=
> ASF_FILE_HEADER_SIZE or (better) remove this hunk.
> 

I'd feel safer with an av_assert() than no check at the very least.
However, if there's actually no need for it and AVPROBE_PADDING_SIZE is
always guaranteed to be >= 24 then I'll remove it and leave a comment.

>> +
>> +    score = 0;
>> +    if (av_match_ext(p->filename, "asf"))
>> +        score += AVPROBE_SCORE_EXTENSION;
> 
> Remove this hunk, we only match the extension if no
> other way of recognizing a format works (tga).
> 

Done.

>> +
>> +    argo_asf_parse_file_header(&hdr, p->buf);
>> +
>> +    if(hdr.magic != ASF_TAG)
>> +        return 0;
>> +
>> +    if (argo_asf_is_known_version(&hdr))
>> +        score += 25;
> 
> Return MAX/2 + 1 in this case, feel free to ignore the other fields.
> 

Isn't that still a relatively low score?
I check the other fields so I can be more certain it's actually an ASF file.

The `hdr.chunk_offset` check was specifically suggested by Andreas.

> I wonder if "argo" isn't a better name for the format than "argo_asf".
> 

Well, there are other Argonaut formats I wanted have a crack at after
this, hence the "argo_" prefix:
- argo_brp: Container for intro videos, uses ADPCM for audio
- argo_cvg: Used for SFX on the PSX version of Croc 1.

> Carl Eugen
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
> 

Zane
Carl Eugen Hoyos Jan. 19, 2020, 11:47 p.m. UTC | #5
Am So., 19. Jan. 2020 um 23:44 Uhr schrieb Zane van Iperen
<zane@zanevaniperen.com>:
>
> 20/1/20 4:52 am, Carl Eugen Hoyos пишет:

> >> +    argo_asf_parse_file_header(&hdr, p->buf);
> >> +
> >> +    if(hdr.magic != ASF_TAG)
> >> +        return 0;
> >> +
> >> +    if (argo_asf_is_known_version(&hdr))
> >> +        score += 25;
> >
> > Return MAX/2 + 1 in this case, feel free to ignore the other fields.
>
> Isn't that still a relatively low score?

If (!) an mpeg stream succeeds with a higher score, it is most likely
correct not to detect argo_asf but feel free to add more checks.
Since you check nearly exclusively for zeros (except for the first 32 bits),
please don't make it >75.

Carl Eugen
Zane van Iperen Jan. 20, 2020, 12:22 a.m. UTC | #6
20/1/20 9:47 am, Carl Eugen Hoyos пишет:
> 
> Am So., 19. Jan. 2020 um 23:44 Uhr schrieb Zane van Iperen
> <zane@zanevaniperen.com>:
>>
>> 20/1/20 4:52 am, Carl Eugen Hoyos пишет:
> 
>>>> +    argo_asf_parse_file_header(&hdr, p->buf);
>>>> +
>>>> +    if(hdr.magic != ASF_TAG)
>>>> +        return 0;
>>>> +
>>>> +    if (argo_asf_is_known_version(&hdr))
>>>> +        score += 25;
>>>
>>> Return MAX/2 + 1 in this case, feel free to ignore the other fields.
>>
>> Isn't that still a relatively low score?
> 
> If (!) an mpeg stream succeeds with a higher score, it is most likely
> correct not to detect argo_asf but feel free to add more checks.
> Since you check nearly exclusively for zeros (except for the first 32 bits),
> please don't make it >75.
> 
> Carl Eugen
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
> 

Makes sense, fixed. The maximum possible score now is 61.

Zane
diff mbox series

Patch

diff --git a/Changelog b/Changelog
index e26320c0ce..bc1593bfd1 100644
--- a/Changelog
+++ b/Changelog
@@ -31,6 +31,7 @@  version <next>:
 - thistogram filter
 - freezeframes filter
 - Argonaut Games ADPCM decoder
+- Argonaut Games ASF demuxer
 
 version 4.2:
 - tpad filter
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 52f29b1a6d..ba6ea8c4a6 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -99,6 +99,7 @@  OBJS-$(CONFIG_APTX_MUXER)                += rawenc.o
 OBJS-$(CONFIG_APTX_HD_DEMUXER)           += aptxdec.o rawdec.o
 OBJS-$(CONFIG_APTX_HD_MUXER)             += rawenc.o
 OBJS-$(CONFIG_AQTITLE_DEMUXER)           += aqtitledec.o subtitles.o
+OBJS-$(CONFIG_ARGO_ASF_DEMUXER)          += argo_asf.o
 OBJS-$(CONFIG_ASF_DEMUXER)               += asfdec_f.o asf.o asfcrypt.o \
                                             avlanguage.o
 OBJS-$(CONFIG_ASF_O_DEMUXER)             += asfdec_o.o asf.o asfcrypt.o \
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index ff9bdb906f..fe74a32e47 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -60,6 +60,7 @@  extern AVOutputFormat ff_aptx_muxer;
 extern AVInputFormat  ff_aptx_hd_demuxer;
 extern AVOutputFormat ff_aptx_hd_muxer;
 extern AVInputFormat  ff_aqtitle_demuxer;
+extern AVInputFormat  ff_argo_asf_demuxer;
 extern AVInputFormat  ff_asf_demuxer;
 extern AVOutputFormat ff_asf_muxer;
 extern AVInputFormat  ff_asf_o_demuxer;
diff --git a/libavformat/argo_asf.c b/libavformat/argo_asf.c
new file mode 100644
index 0000000000..d8f2d71009
--- /dev/null
+++ b/libavformat/argo_asf.c
@@ -0,0 +1,260 @@ 
+/*
+ * Argonaut Games ASF demuxer
+ *
+ * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
+ *
+ * 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 "internal.h"
+#include "libavutil/intreadwrite.h"
+
+#define ASF_TAG                 MKTAG('A', 'S', 'F', '\0')
+#define ASF_FILE_HEADER_SIZE    (24)
+#define ASF_CHUNK_HEADER_SIZE   (20)
+
+typedef struct ArgoASFFileHeader {
+    uint32_t    magic;          /*< Magic Number, {'A', 'S', 'F', '\0'} */
+    uint16_t    version_major;  /*< File Major Version. */
+    uint16_t    version_minor;  /*< File Minor Version. */
+    uint32_t    num_chunks;     /*< No. chunks in the file. */
+    uint32_t    chunk_offset;   /*< Offset to the first chunk from the start of the file. */
+    int8_t      name[8];        /*< Name. */
+} ArgoASFFileHeader;
+
+typedef struct ArgoASFChunkHeader {
+    uint32_t    num_blocks;     /*< No. blocks in the chunk. */
+    uint32_t    num_samples;    /*< No. samples per channel in a block. */
+    uint32_t    unk1;           /*< Unknown */
+    uint16_t    sample_rate;    /*< Sample rate. */
+    uint16_t    unk2;           /*< Unknown. */
+    uint32_t    flags;          /*< Stream flags. */
+} ArgoASFChunkHeader;
+
+enum {
+    ASF_CF_BITS_PER_SAMPLE  = (1 << 0), /*< 16-bit if set, 8 otherwise.      */
+    ASF_CF_STEREO           = (1 << 1), /*< Stereo if set, mono otherwise.   */
+    ASF_CF_ALWAYS1_1        = (1 << 2), /*< Unknown, always seems to be set. */
+    ASF_CF_ALWAYS1_2        = (1 << 3), /*< Unknown, always seems to be set. */
+
+    ASF_CF_ALWAYS1          = ASF_CF_ALWAYS1_1 | ASF_CF_ALWAYS1_2,
+    ASF_CF_ALWAYS0          = ~(ASF_CF_BITS_PER_SAMPLE | ASF_CF_STEREO | ASF_CF_ALWAYS1)
+};
+
+typedef struct ArgoASFDemuxContext
+{
+    ArgoASFFileHeader   fhdr;
+    ArgoASFChunkHeader  ckhdr;
+    uint32_t            blocks_read;
+} ArgoASFDemuxContext;
+
+static void argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf)
+{
+    hdr->magic           = AV_RL32(buf + 0);
+    hdr->version_major   = AV_RL16(buf + 4);
+    hdr->version_minor   = AV_RL16(buf + 6);
+    hdr->num_chunks      = AV_RL32(buf + 8);
+    hdr->chunk_offset    = AV_RL32(buf + 12);
+    for(int i = 0; i < 8; ++i)
+        hdr->name[i]     = AV_RL8(buf + 16 + i);
+}
+
+static void argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf)
+{
+    hdr->num_blocks     = AV_RL32(buf + 0);
+    hdr->num_samples    = AV_RL32(buf + 4);
+    hdr->unk1           = AV_RL32(buf + 8);
+    hdr->sample_rate    = AV_RL16(buf + 12);
+    hdr->unk2           = AV_RL16(buf + 14);
+    hdr->flags          = AV_RL32(buf + 16);
+}
+
+/*
+ * Known versions:
+ * 1.1: The sample files in /game-formats/brender/part2.zip
+ * 1.2: Croc! Legend of the Gobbos
+ * 2.1: Croc 2
+ */
+static int argo_asf_is_known_version(const ArgoASFFileHeader *hdr)
+{
+    return (hdr->version_major == 1 && hdr->version_minor == 1) ||
+           (hdr->version_major == 1 && hdr->version_minor == 2) ||
+           (hdr->version_major == 2 && hdr->version_minor == 1);
+}
+
+static int argo_asf_probe(const AVProbeData *p)
+{
+    int score;
+    ArgoASFFileHeader hdr;
+
+    if (p->buf_size < ASF_FILE_HEADER_SIZE)
+        return AVPROBE_SCORE_RETRY;
+
+    score = 0;
+    if (av_match_ext(p->filename, "asf"))
+        score += AVPROBE_SCORE_EXTENSION;
+
+    argo_asf_parse_file_header(&hdr, p->buf);
+
+    if(hdr.magic != ASF_TAG)
+        return 0;
+
+    if (argo_asf_is_known_version(&hdr))
+        score += 25;
+
+    /* Have only ever seen these with 1 chunk. */
+    if (hdr.num_chunks == 1)
+        score += 25;
+
+    if (hdr.chunk_offset > INT_MAX)
+        score -= 50;
+
+    if (score > AVPROBE_SCORE_MAX)
+        score = AVPROBE_SCORE_MAX;
+
+    return score;
+}
+
+
+
+static int argo_asf_read_header(AVFormatContext *s)
+{
+    int64_t ret;
+    AVIOContext *pb = s->pb;
+    AVStream *st;
+    ArgoASFDemuxContext *asf = s->priv_data;
+    uint8_t buf[FFMAX(ASF_FILE_HEADER_SIZE, ASF_CHUNK_HEADER_SIZE)];
+
+    if (!(st = avformat_new_stream(s, NULL)))
+        return AVERROR(ENOMEM);
+
+    if ((ret = avio_read(pb, buf, ASF_FILE_HEADER_SIZE)) < 0)
+        return ret;
+    else if (ret != ASF_FILE_HEADER_SIZE)
+        return AVERROR(EIO);
+
+    argo_asf_parse_file_header(&asf->fhdr, buf);
+
+    if (!argo_asf_is_known_version(&asf->fhdr)) {
+        avpriv_request_sample(s, "Version %hu.%hu",
+            asf->fhdr.version_major, asf->fhdr.version_minor
+        );
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (asf->fhdr.chunk_offset < ASF_FILE_HEADER_SIZE)
+        return AVERROR_INVALIDDATA;
+
+    if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 0)
+        return ret;
+
+    if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
+        return ret;
+    else if (ret != ASF_CHUNK_HEADER_SIZE)
+        return AVERROR(EIO);
+
+    argo_asf_parse_chunk_header(&asf->ckhdr, buf);
+
+    if ((asf->ckhdr.flags & ASF_CF_ALWAYS1) != ASF_CF_ALWAYS1 || (asf->ckhdr.flags & ASF_CF_ALWAYS0) != 0) {
+        avpriv_request_sample(s, "Nonstandard flags (0x%08X)", asf->ckhdr.flags);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    st->codecpar->codec_type                = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id                  = AV_CODEC_ID_ADPCM_ARGO;
+    st->codecpar->format                    = AV_SAMPLE_FMT_S16;
+
+    if (asf->ckhdr.flags & ASF_CF_STEREO) {
+        st->codecpar->channel_layout        = AV_CH_LAYOUT_STEREO;
+        st->codecpar->channels              = 2;
+    } else {
+        st->codecpar->channel_layout        = AV_CH_LAYOUT_MONO;
+        st->codecpar->channels              = 1;
+    }
+
+    st->codecpar->sample_rate               = asf->ckhdr.sample_rate;
+
+    st->codecpar->bits_per_coded_sample     = 4;
+
+    if (asf->ckhdr.flags & ASF_CF_BITS_PER_SAMPLE)
+        st->codecpar->bits_per_raw_sample   = 16;
+    else
+        st->codecpar->bits_per_raw_sample   = 8;
+
+    if (st->codecpar->bits_per_raw_sample != 16) {
+        /* The header allows for these, but I've never seen any files with them. */
+        avpriv_request_sample(s, "Non 16-bit samples");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    /*
+     * (nchannel control bytes) + ((bytes_per_channel) * nchannel)
+     * For mono, this is 17. For stereo, this is 34.
+     */
+    st->codecpar->frame_size            = st->codecpar->channels +
+                                          (asf->ckhdr.num_samples / 2) *
+                                          st->codecpar->channels;
+
+    st->codecpar->block_align           = st->codecpar->frame_size;
+
+    st->codecpar->bit_rate              = st->codecpar->channels *
+                                          st->codecpar->sample_rate *
+                                          st->codecpar->bits_per_coded_sample;
+
+    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
+    st->start_time      = 0;
+    st->duration        = asf->ckhdr.num_blocks * asf->ckhdr.num_samples;
+    st->nb_frames       = asf->ckhdr.num_blocks;
+    return 0;
+}
+
+static int argo_asf_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    ArgoASFDemuxContext *asf = s->priv_data;
+
+    AVStream *st = s->streams[0];
+    AVIOContext *pb = s->pb;
+    int ret;
+
+    if (asf->blocks_read >= asf->ckhdr.num_blocks)
+        return AVERROR_EOF;
+
+    if ((ret = av_get_packet(pb, pkt, st->codecpar->frame_size)) < 0)
+        return ret;
+    else if (ret != st->codecpar->frame_size)
+        return AVERROR_INVALIDDATA;
+
+    pkt->stream_index   = st->index;
+    pkt->duration       = asf->ckhdr.num_samples;
+
+    ++asf->blocks_read;
+    return 0;
+}
+
+/*
+ * Not actually sure what ASF stands for.
+ * - Argonaut Sound File?
+ * - Audio Stream File?
+ */
+AVInputFormat ff_argo_asf_demuxer = {
+    .name           = "argo_asf",
+    .long_name      = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
+    .priv_data_size = sizeof(ArgoASFDemuxContext),
+    .read_probe     = argo_asf_probe,
+    .read_header    = argo_asf_read_header,
+    .read_packet    = argo_asf_read_packet
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 0a79868663..f72fb9478a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,8 +32,8 @@ 
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  35
-#define LIBAVFORMAT_VERSION_MICRO 103
+#define LIBAVFORMAT_VERSION_MINOR  36
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \