diff mbox series

[FFmpeg-devel,v11] avformat: add demuxer for Pro Pinball Series' Soundbanks

Message ID 20200428121025.179367-1-zane@zanevaniperen.com
State Superseded
Headers show
Series [FFmpeg-devel,v11] avformat: add demuxer for Pro Pinball Series' Soundbanks | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Zane van Iperen April 28, 2020, 12:10 p.m. UTC
Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
---
 Changelog                |   1 +
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/pp_bnk.c     | 289 +++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |   4 +-
 5 files changed, 294 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/pp_bnk.c

Comments

Zane van Iperen April 30, 2020, 12:21 p.m. UTC | #1
On Tue, 28 Apr 2020 12:10:33 +0000
"Zane van Iperen" <zane@zanevaniperen.com> wrote:

> Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
> ---
>  Changelog                |   1 +
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/pp_bnk.c     | 289
> +++++++++++++++++++++++++++++++++++++++ libavformat/version.h    |
> 4 +- 5 files changed, 294 insertions(+), 2 deletions(-)
>  create mode 100644 libavformat/pp_bnk.c
> 

Ping.

Btw, Michael, this has the probe change we discussed.

Zane
Michael Niedermayer April 30, 2020, 11:09 p.m. UTC | #2
On Tue, Apr 28, 2020 at 12:10:33PM +0000, Zane van Iperen wrote:
> Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
> ---
>  Changelog                |   1 +
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/pp_bnk.c     | 289 +++++++++++++++++++++++++++++++++++++++
>  libavformat/version.h    |   4 +-
>  5 files changed, 294 insertions(+), 2 deletions(-)
>  create mode 100644 libavformat/pp_bnk.c
> 
> diff --git a/Changelog b/Changelog
> index 83b8a4a46e..4cd324ffc2 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -63,6 +63,7 @@ version <next>:
>  - maskedthreshold filter
>  - Support for muxing pcm and pgs in m2ts
>  - Cunning Developments ADPCM decoder
> +- Pro Pinball Series Soundbank demuxer
>  
>  
>  version 4.2:
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index d4bed3c113..b744eb69b2 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -428,6 +428,7 @@ OBJS-$(CONFIG_PCM_VIDC_DEMUXER)          += pcmdec.o pcm.o
>  OBJS-$(CONFIG_PCM_VIDC_MUXER)            += pcmenc.o rawenc.o
>  OBJS-$(CONFIG_PJS_DEMUXER)               += pjsdec.o subtitles.o
>  OBJS-$(CONFIG_PMP_DEMUXER)               += pmpdec.o
> +OBJS-$(CONFIG_PP_BNK_DEMUXER)            += pp_bnk.o
>  OBJS-$(CONFIG_PVA_DEMUXER)               += pva.o
>  OBJS-$(CONFIG_PVF_DEMUXER)               += pvfdec.o pcm.o
>  OBJS-$(CONFIG_QCP_DEMUXER)               += qcp.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index 39d2c352f5..3919c9e4c1 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -341,6 +341,7 @@ extern AVInputFormat  ff_pcm_u8_demuxer;
>  extern AVOutputFormat ff_pcm_u8_muxer;
>  extern AVInputFormat  ff_pjs_demuxer;
>  extern AVInputFormat  ff_pmp_demuxer;
> +extern AVInputFormat  ff_pp_bnk_demuxer;
>  extern AVOutputFormat ff_psp_muxer;
>  extern AVInputFormat  ff_pva_demuxer;
>  extern AVInputFormat  ff_pvf_demuxer;
> diff --git a/libavformat/pp_bnk.c b/libavformat/pp_bnk.c
> new file mode 100644
> index 0000000000..5f9fc2d373
> --- /dev/null
> +++ b/libavformat/pp_bnk.c
> @@ -0,0 +1,289 @@
> +/*
> + * Pro Pinball Series Soundbank (44c, 22c, 11c, 5c) 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"
> +#include "libavutil/avassert.h"
> +#include "libavutil/internal.h"
> +
> +#define PP_BNK_MAX_READ_SIZE    4096
> +#define PP_BNK_FILE_HEADER_SIZE 20
> +#define PP_BNK_TRACK_SIZE       20
> +
> +typedef struct PPBnkHeader {
> +    uint32_t        bank_id;        /*< Bank ID, useless for our purposes. */
> +    uint32_t        sample_rate;    /*< Sample rate of the contained tracks. */
> +    uint32_t        always1;        /*< Unknown, always seems to be 1. */
> +    uint32_t        track_count;    /*< Number of tracks in the file. */
> +    uint32_t        flags;          /*< Flags. */
> +} PPBnkHeader;
> +
> +typedef struct PPBnkTrack {
> +    uint32_t        id;             /*< Track ID. Usually track[i].id == track[i-1].id + 1, but not always */
> +    uint32_t        size;           /*< Size of the data in bytes. */
> +    uint32_t        sample_rate;    /*< Sample rate. */
> +    uint32_t        always1_1;      /*< Unknown, always seems to be 1. */
> +    uint32_t        always1_2;      /*< Unknown, always seems to be 1. */
> +} PPBnkTrack;
> +
> +typedef struct PPBnkCtxTrack {
> +    int64_t         data_offset;
> +    uint32_t        data_size;
> +} PPBnkCtxTrack;
> +
> +typedef struct PPBnkCtx {
> +    int             track_count;
> +    PPBnkCtxTrack   *tracks;
> +    uint32_t        current_track;
> +    uint32_t        bytes_read;
> +} PPBnkCtx;
> +
> +enum {
> +    PP_BNK_FLAG_PERSIST = (1 << 0), /*< This is a large file, keep in memory. */
> +    PP_BNK_FLAG_MUSIC   = (1 << 1), /*< This is music. */
> +    PP_BNK_FLAG_MASK    = (PP_BNK_FLAG_PERSIST | PP_BNK_FLAG_MUSIC)
> +};
> +
> +static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
> +{
> +    hdr->bank_id        = AV_RL32(buf +  0);
> +    hdr->sample_rate    = AV_RL32(buf +  4);
> +    hdr->always1        = AV_RL32(buf +  8);
> +    hdr->track_count    = AV_RL32(buf + 12);
> +    hdr->flags          = AV_RL32(buf + 16);
> +}
> +
> +static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
> +{
> +    trk->id             = AV_RL32(buf +  0);
> +    trk->size           = AV_RL32(buf +  4);
> +    trk->sample_rate    = AV_RL32(buf +  8);
> +    trk->always1_1      = AV_RL32(buf + 12);
> +    trk->always1_2      = AV_RL32(buf + 16);
> +}
> +
> +static int pp_bnk_probe(const AVProbeData *p)
> +{
> +    uint32_t sample_rate = AV_RL32(p->buf +  4);
> +    uint32_t track_count = AV_RL32(p->buf + 12);
> +    uint32_t flags       = AV_RL32(p->buf + 16);
> +
> +    if (track_count == 0 || track_count > INT_MAX)
> +        return 0;
> +
> +    if ((sample_rate !=  5512) && (sample_rate != 11025) &&
> +        (sample_rate != 22050) && (sample_rate != 44100))
> +        return 0;
> +
> +    /* Check the first track header. */
> +    if (AV_RL32(p->buf + 28) != sample_rate)
> +        return 0;
> +
> +    if ((flags & ~PP_BNK_FLAG_MASK) != 0)
> +        return 0;
> +
> +    return AVPROBE_SCORE_MAX / 4 + 1;
> +}
> +
> +static int pp_bnk_read_header(AVFormatContext *s)
> +{
> +    int64_t ret;
> +    AVStream *st;
> +    AVCodecParameters *par;
> +    PPBnkCtx *ctx = s->priv_data;
> +    uint8_t buf[FFMAX(PP_BNK_FILE_HEADER_SIZE, PP_BNK_TRACK_SIZE)];
> +    PPBnkHeader hdr;
> +
> +    if ((ret = avio_read(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
> +        return ret;
> +    else if (ret != PP_BNK_FILE_HEADER_SIZE)
> +        return AVERROR(EIO);
> +
> +    pp_bnk_parse_header(&hdr, buf);
> +
> +    if (hdr.track_count == 0 || hdr.track_count > INT_MAX)
> +        return AVERROR_INVALIDDATA;
> +
> +    if (hdr.sample_rate == 0 || hdr.sample_rate > INT_MAX)
> +        return AVERROR_INVALIDDATA;
> +
> +    if (hdr.always1 != 1) {
> +        avpriv_request_sample(s, "Non-one header value");
> +        return AVERROR_PATCHWELCOME;
> +    }
> +
> +    ctx->track_count = hdr.track_count;
> +
> +    if (!(ctx->tracks = av_malloc_array(hdr.track_count, sizeof(PPBnkCtxTrack))))
> +        return AVERROR(ENOMEM);
> +
> +    /* Parse and validate each track. */
> +    for (int i = 0; i < hdr.track_count; i++) {
> +        PPBnkTrack e;
> +        PPBnkCtxTrack *trk = ctx->tracks + i;
> +
> +        ret = avio_read(s->pb, buf, PP_BNK_TRACK_SIZE);
> +        if (ret < 0 && ret != AVERROR_EOF)
> +            goto fail;
> +
> +        /* Short byte-count or EOF, we have a truncated file. */
> +        if (ret != PP_BNK_TRACK_SIZE) {
> +            av_log(s, AV_LOG_WARNING, "File truncated at %d/%u track(s)\n",
> +                   i, hdr.track_count);
> +            ctx->track_count = i;
> +            break;
> +        }
> +
> +        pp_bnk_parse_track(&e, buf);
> +
> +        /* The individual sample rates of all tracks must match that of the file header. */
> +        if (e.sample_rate != hdr.sample_rate) {
> +            ret = AVERROR_INVALIDDATA;
> +            goto fail;
> +        }
> +
> +        if (e.always1_1 != 1 || e.always1_2 != 1) {
> +            avpriv_request_sample(s, "Non-one track header values");
> +            ret = AVERROR_PATCHWELCOME;
> +            goto fail;
> +        }
> +
> +        trk->data_offset = avio_tell(s->pb);
> +        trk->data_size   = e.size;
> +
> +        /*
> +         * Skip over the data to the next stream header.
> +         * Sometimes avio_skip() doesn't detect EOF. If it doesn't, either:
> +         *   - the avio_read() above will, or
> +         *   - pp_bnk_read_packet() will read a truncated last track.
> +         */
> +        if ((ret = avio_skip(s->pb, e.size)) == AVERROR_EOF) {
> +            ctx->track_count = i + 1;
> +            av_log(s, AV_LOG_WARNING,
> +                   "Track %d has truncated data, assuming track count == %d\n",
> +                   i, ctx->track_count);
> +            break;
> +        } else if (ret < 0) {
> +            goto fail;
> +        }
> +    }
> +
> +    /* File is only a header. */
> +    if (ctx->track_count == 0) {
> +        ret = AVERROR_INVALIDDATA;
> +        goto fail;
> +    }
> +
> +    /* Build the streams. */
> +    for (int i = 0; i < ctx->track_count; i++) {
> +        if (!(st = avformat_new_stream(s, NULL))) {
> +            ret = AVERROR(ENOMEM);
> +            goto fail;
> +        }
> +
> +        par                         = st->codecpar;
> +        par->codec_type             = AVMEDIA_TYPE_AUDIO;
> +        par->codec_id               = AV_CODEC_ID_ADPCM_IMA_CUNNING;
> +        par->format                 = AV_SAMPLE_FMT_S16;
> +        par->channel_layout         = AV_CH_LAYOUT_MONO;
> +        par->channels               = 1;
> +        par->sample_rate            = hdr.sample_rate;
> +        par->bits_per_coded_sample  = 4;
> +        par->bits_per_raw_sample    = 16;
> +        par->block_align            = 1;
> +        par->bit_rate               = par->sample_rate * par->bits_per_coded_sample;
> +
> +        avpriv_set_pts_info(st, 64, 1, par->sample_rate);
> +        st->start_time              = 0;
> +        st->duration                = ctx->tracks[i].data_size * 2;
> +    }
> +
> +    /* Seek to the start of the first stream. */
> +    if ((ret = avio_seek(s->pb, ctx->tracks[0].data_offset, SEEK_SET)) < 0) {
> +        goto fail;
> +    } else if (ret != ctx->tracks[0].data_offset) {
> +        ret = AVERROR(EIO);
> +        goto fail;
> +    }
> +
> +    return 0;
> +
> +fail:
> +    av_freep(&ctx->tracks);
> +    return ret;
> +}
> +

> +static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> +    int64_t ret;
> +    int size;
> +    PPBnkCtx *ctx = s->priv_data;
> +    PPBnkCtxTrack *trk = ctx->tracks + ctx->current_track;
> +
> +    av_assert0(ctx->bytes_read <= trk->data_size);
> +
> +    if (ctx->bytes_read == trk->data_size) {
> +        if (ctx->current_track == ctx->track_count - 1)
> +            return AVERROR_EOF;
> +
> +        trk++;
> +
> +        if ((ret = avio_seek(s->pb, trk->data_offset, SEEK_SET)) < 0)
> +            return ret;
> +        else if (ret != trk->data_offset)
> +            return AVERROR(EIO);
> +
> +        ctx->bytes_read = 0;
> +        ctx->current_track++;
> +    }
> +
> +    size = FFMIN(trk->data_size - ctx->bytes_read, PP_BNK_MAX_READ_SIZE);
> +
> +    if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
> +        return ret;
> +
> +    ctx->bytes_read    += ret;
> +    pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
> +    pkt->stream_index   = ctx->current_track;
> +    pkt->duration       = ret * 2;

With this each stream would be returned completely before the next
such non interleaved output is a bit odd.

also where can i find such a file ?

thx

[...]
Zane van Iperen May 1, 2020, 12:36 a.m. UTC | #3
On Fri, 1 May 2020 01:09:17 +0200
"Michael Niedermayer" <michael@niedermayer.cc> wrote:

> > +    size = FFMIN(trk->data_size - ctx->bytes_read,
> > PP_BNK_MAX_READ_SIZE); +
> > +    if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
> > +        return ret;
> > +
> > +    ctx->bytes_read    += ret;
> > +    pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
> > +    pkt->stream_index   = ctx->current_track;
> > +    pkt->duration       = ret * 2;  
> 
> With this each stream would be returned completely before the next
> such non interleaved output is a bit odd.
> 

Yep, it's an odd format.  Some files are meant to be stereo, but I
can't present them as such. I have to present them as separate mono
streams and merge them with a filter.

> also where can i find such a file ?
> 

Here's a file I trimmed for FATE:

https://0x0.st/ie7O.11c


> thx
> 
> [...]
> --
> Michael     GnuPG fingerprint:
> 9FF2128B147EF6730BADF133611EC787040B0FAB
> 
> There will always be a question for which you do not know the correct
> answer. _______________________________________________
> 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 van Iperen May 1, 2020, 12:55 a.m. UTC | #4
On Fri, 01 May 2020 00:36:57 +0000
"Zane van Iperen" <zane@zanevaniperen.com> wrote:

> On Fri, 1 May 2020 01:09:17 +0200
> "Michael Niedermayer" <michael@niedermayer.cc> wrote:
> 
> > > +    size = FFMIN(trk->data_size - ctx->bytes_read,
> > > PP_BNK_MAX_READ_SIZE); +
> > > +    if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
> > > +        return ret;
> > > +
> > > +    ctx->bytes_read    += ret;
> > > +    pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
> > > +    pkt->stream_index   = ctx->current_track;
> > > +    pkt->duration       = ret * 2;  
> >
> > With this each stream would be returned completely before the next
> > such non interleaved output is a bit odd.
> >  
> 
> Yep, it's an odd format.  Some files are meant to be stereo, but I
> can't present them as such. I have to present them as separate mono
> streams and merge them with a filter.
> 
> > also where can i find such a file ?
> >  
> 
> Here's a file I trimmed for FATE:
> 
> https://0x0.st/ie7O.11c
> 
Probably should have mentioned that first file is meant to be stereo
music.

Here's one that's meant to be just a bunch of mono tracks:
https://0x0.st/ie74.5C
Michael Niedermayer May 3, 2020, 1:23 p.m. UTC | #5
On Fri, May 01, 2020 at 12:55:18AM +0000, Zane van Iperen wrote:
> On Fri, 01 May 2020 00:36:57 +0000
> "Zane van Iperen" <zane@zanevaniperen.com> wrote:
> 
> > On Fri, 1 May 2020 01:09:17 +0200
> > "Michael Niedermayer" <michael@niedermayer.cc> wrote:
> > 
> > > > +    size = FFMIN(trk->data_size - ctx->bytes_read,
> > > > PP_BNK_MAX_READ_SIZE); +
> > > > +    if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
> > > > +        return ret;
> > > > +
> > > > +    ctx->bytes_read    += ret;
> > > > +    pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
> > > > +    pkt->stream_index   = ctx->current_track;
> > > > +    pkt->duration       = ret * 2;  
> > >
> > > With this each stream would be returned completely before the next
> > > such non interleaved output is a bit odd.
> > >  
> > 
> > Yep, it's an odd format.  Some files are meant to be stereo, but I
> > can't present them as such. I have to present them as separate mono
> > streams and merge them with a filter.
> > 
> > > also where can i find such a file ?
> > >  
> > 
> > Here's a file I trimmed for FATE:
> > 
> > https://0x0.st/ie7O.11c
> > 
> Probably should have mentioned that first file is meant to be stereo
> music.
> 
> Here's one that's meant to be just a bunch of mono tracks:
> https://0x0.st/ie74.5C

just tried, and as expected this doesnt work

./ffmpeg -i pinball/ie74.5C -map 0 test.nut
...
Press [q] to stop, [?] for help
Too many packets buffered for output stream 0:1.
[libvorbis @ 0x557f49f3da40] 16 frames left in the queue on closing
[libvorbis @ 0x557f49f3f340] 14 frames left in the queue on closing
Conversion failed!


[...]
Zane van Iperen May 3, 2020, 1:43 p.m. UTC | #6
On Sun, 3 May 2020 15:23:19 +0200
"Michael Niedermayer" <michael@niedermayer.cc> wrote:

> On Fri, May 01, 2020 at 12:55:18AM +0000, Zane van Iperen wrote:
> > On Fri, 01 May 2020 00:36:57 +0000
> > "Zane van Iperen" <zane@zanevaniperen.com> wrote:
> >  
> > > On Fri, 1 May 2020 01:09:17 +0200
> > > "Michael Niedermayer" <michael@niedermayer.cc> wrote:
> > >  
> > > > > +    size = FFMIN(trk->data_size - ctx->bytes_read,
> > > > > PP_BNK_MAX_READ_SIZE); +
> > > > > +    if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
> > > > > +        return ret;
> > > > > +
> > > > > +    ctx->bytes_read    += ret;
> > > > > +    pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
> > > > > +    pkt->stream_index   = ctx->current_track;
> > > > > +    pkt->duration       = ret * 2;  
> > > >
> > > > With this each stream would be returned completely before the
> > > > next such non interleaved output is a bit odd.
> > > >  
> > >
> > > Yep, it's an odd format.  Some files are meant to be stereo, but I
> > > can't present them as such. I have to present them as separate
> > > mono streams and merge them with a filter.
> > >  
> > > > also where can i find such a file ?
> > > >  
> > >
> > > Here's a file I trimmed for FATE:
> > >
> > > https://0x0.st/ie7O.11c
> > >  
> > Probably should have mentioned that first file is meant to be stereo
> > music.
> >
> > Here's one that's meant to be just a bunch of mono tracks:
> > https://0x0.st/ie74.5C  
> 
> just tried, and as expected this doesnt work
> 
> ./ffmpeg -i pinball/ie74.5C -map 0 test.nut
> ...
> Press [q] to stop, [?] for help
> Too many packets buffered for output stream 0:1.
> [libvorbis @ 0x557f49f3da40] 16 frames left in the queue on closing
> [libvorbis @ 0x557f49f3f340] 14 frames left in the queue on closing
> Conversion failed!
> 

Interesting... that worked for me :/

./ffmpeg -y -i pp/ie74.5C -map 0 test.nut
...
[nut @ 0x55615988d700] Multiple keyframes with same PTS
    Last message repeated 5 times
size=     632kB time=00:00:06.37 bitrate= 812.2kbits/s speed=59.8x    
video:0kB audio:630kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.366753%


This isn't a valid use case for this file format regardless, as they're soundbanks.
Each stream should be split into a separate file, and it's almost guaranteed that they're all different lengths.

If this is required, then I'm not sure of the best course of action is.
Unless I constantly seek back and forth in the file, but that seems messy...

Zane
Michael Niedermayer May 3, 2020, 5:10 p.m. UTC | #7
On Sun, May 03, 2020 at 01:43:24PM +0000, Zane van Iperen wrote:
> On Sun, 3 May 2020 15:23:19 +0200
> "Michael Niedermayer" <michael@niedermayer.cc> wrote:
> 
> > On Fri, May 01, 2020 at 12:55:18AM +0000, Zane van Iperen wrote:
> > > On Fri, 01 May 2020 00:36:57 +0000
> > > "Zane van Iperen" <zane@zanevaniperen.com> wrote:
> > >  
> > > > On Fri, 1 May 2020 01:09:17 +0200
> > > > "Michael Niedermayer" <michael@niedermayer.cc> wrote:
> > > >  
> > > > > > +    size = FFMIN(trk->data_size - ctx->bytes_read,
> > > > > > PP_BNK_MAX_READ_SIZE); +
> > > > > > +    if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
> > > > > > +        return ret;
> > > > > > +
> > > > > > +    ctx->bytes_read    += ret;
> > > > > > +    pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
> > > > > > +    pkt->stream_index   = ctx->current_track;
> > > > > > +    pkt->duration       = ret * 2;  
> > > > >
> > > > > With this each stream would be returned completely before the
> > > > > next such non interleaved output is a bit odd.
> > > > >  
> > > >
> > > > Yep, it's an odd format.  Some files are meant to be stereo, but I
> > > > can't present them as such. I have to present them as separate
> > > > mono streams and merge them with a filter.
> > > >  
> > > > > also where can i find such a file ?
> > > > >  
> > > >
> > > > Here's a file I trimmed for FATE:
> > > >
> > > > https://0x0.st/ie7O.11c
> > > >  
> > > Probably should have mentioned that first file is meant to be stereo
> > > music.
> > >
> > > Here's one that's meant to be just a bunch of mono tracks:
> > > https://0x0.st/ie74.5C  
> > 
> > just tried, and as expected this doesnt work
> > 
> > ./ffmpeg -i pinball/ie74.5C -map 0 test.nut
> > ...
> > Press [q] to stop, [?] for help
> > Too many packets buffered for output stream 0:1.
> > [libvorbis @ 0x557f49f3da40] 16 frames left in the queue on closing
> > [libvorbis @ 0x557f49f3f340] 14 frames left in the queue on closing
> > Conversion failed!
> > 
> 
> Interesting... that worked for me :/
> 
> ./ffmpeg -y -i pp/ie74.5C -map 0 test.nut
> ...
> [nut @ 0x55615988d700] Multiple keyframes with same PTS
>     Last message repeated 5 times
> size=     632kB time=00:00:06.37 bitrate= 812.2kbits/s speed=59.8x    
> video:0kB audio:630kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.366753%

Interresting, but the problem with 100% uninterleaved streams remains
applications will have issues with this, and the more the longer these are


> 
> 
> This isn't a valid use case for this file format regardless, as they're soundbanks.

Well, if -map 0 is not a valid use case then how would you
convert such a pinball file to a new pinball file ?
(maybe with some audio filter adding echo or whatever)
Theres no muxer currently but if there was one that seems a valid use
case to me


> Each stream should be split into a separate file, and it's almost guaranteed that they're all different lengths.
> 

> If this is required, then I'm not sure of the best course of action is.

iam also not sure but maybe looking at what exists could lead to some
inspiration
audio streams with concatenated songs, chapters, slide shows, mpeg ts which
stores several independant programms, ...


> Unless I constantly seek back and forth in the file, but that seems messy...

streams can be set to AVDISCARD_ALL when they are unneeded. So at least this
should not happen when parts are unused. Still not sure thats a solution

thx

[...]
Zane van Iperen May 4, 2020, 12:34 p.m. UTC | #8
On Sun, 3 May 2020 19:10:51 +0200
"Michael Niedermayer" <michael@niedermayer.cc> wrote:

> > > just tried, and as expected this doesnt work
> > >
> > > ./ffmpeg -i pinball/ie74.5C -map 0 test.nut
> > > ...
> > > Press [q] to stop, [?] for help
> > > Too many packets buffered for output stream 0:1.
> > > [libvorbis @ 0x557f49f3da40] 16 frames left in the queue on
> > > closing [libvorbis @ 0x557f49f3f340] 14 frames left in the queue
> > > on closing Conversion failed!
> > >  
> >
> > Interesting... that worked for me :/
> >
> > ./ffmpeg -y -i pp/ie74.5C -map 0 test.nut
> > ...
> > [nut @ 0x55615988d700] Multiple keyframes with same PTS
> >     Last message repeated 5 times
> > size=     632kB time=00:00:06.37 bitrate= 812.2kbits/s speed=59.8x
> > video:0kB audio:630kB subtitle:0kB other streams:0kB global
> > headers:0kB muxing overhead: 0.366753%  
> 
> Interresting, but the problem with 100% uninterleaved streams remains
> applications will have issues with this, and the more the longer
> these are
> 

Yeah, I was able to reproduce this with a longer file.

Have fixed it by reading packets from the streams in a round-robin
manner. A bit nasty, but I don't see any nicer way of doing it.

Zane
diff mbox series

Patch

diff --git a/Changelog b/Changelog
index 83b8a4a46e..4cd324ffc2 100644
--- a/Changelog
+++ b/Changelog
@@ -63,6 +63,7 @@  version <next>:
 - maskedthreshold filter
 - Support for muxing pcm and pgs in m2ts
 - Cunning Developments ADPCM decoder
+- Pro Pinball Series Soundbank demuxer
 
 
 version 4.2:
diff --git a/libavformat/Makefile b/libavformat/Makefile
index d4bed3c113..b744eb69b2 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -428,6 +428,7 @@  OBJS-$(CONFIG_PCM_VIDC_DEMUXER)          += pcmdec.o pcm.o
 OBJS-$(CONFIG_PCM_VIDC_MUXER)            += pcmenc.o rawenc.o
 OBJS-$(CONFIG_PJS_DEMUXER)               += pjsdec.o subtitles.o
 OBJS-$(CONFIG_PMP_DEMUXER)               += pmpdec.o
+OBJS-$(CONFIG_PP_BNK_DEMUXER)            += pp_bnk.o
 OBJS-$(CONFIG_PVA_DEMUXER)               += pva.o
 OBJS-$(CONFIG_PVF_DEMUXER)               += pvfdec.o pcm.o
 OBJS-$(CONFIG_QCP_DEMUXER)               += qcp.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 39d2c352f5..3919c9e4c1 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -341,6 +341,7 @@  extern AVInputFormat  ff_pcm_u8_demuxer;
 extern AVOutputFormat ff_pcm_u8_muxer;
 extern AVInputFormat  ff_pjs_demuxer;
 extern AVInputFormat  ff_pmp_demuxer;
+extern AVInputFormat  ff_pp_bnk_demuxer;
 extern AVOutputFormat ff_psp_muxer;
 extern AVInputFormat  ff_pva_demuxer;
 extern AVInputFormat  ff_pvf_demuxer;
diff --git a/libavformat/pp_bnk.c b/libavformat/pp_bnk.c
new file mode 100644
index 0000000000..5f9fc2d373
--- /dev/null
+++ b/libavformat/pp_bnk.c
@@ -0,0 +1,289 @@ 
+/*
+ * Pro Pinball Series Soundbank (44c, 22c, 11c, 5c) 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"
+#include "libavutil/avassert.h"
+#include "libavutil/internal.h"
+
+#define PP_BNK_MAX_READ_SIZE    4096
+#define PP_BNK_FILE_HEADER_SIZE 20
+#define PP_BNK_TRACK_SIZE       20
+
+typedef struct PPBnkHeader {
+    uint32_t        bank_id;        /*< Bank ID, useless for our purposes. */
+    uint32_t        sample_rate;    /*< Sample rate of the contained tracks. */
+    uint32_t        always1;        /*< Unknown, always seems to be 1. */
+    uint32_t        track_count;    /*< Number of tracks in the file. */
+    uint32_t        flags;          /*< Flags. */
+} PPBnkHeader;
+
+typedef struct PPBnkTrack {
+    uint32_t        id;             /*< Track ID. Usually track[i].id == track[i-1].id + 1, but not always */
+    uint32_t        size;           /*< Size of the data in bytes. */
+    uint32_t        sample_rate;    /*< Sample rate. */
+    uint32_t        always1_1;      /*< Unknown, always seems to be 1. */
+    uint32_t        always1_2;      /*< Unknown, always seems to be 1. */
+} PPBnkTrack;
+
+typedef struct PPBnkCtxTrack {
+    int64_t         data_offset;
+    uint32_t        data_size;
+} PPBnkCtxTrack;
+
+typedef struct PPBnkCtx {
+    int             track_count;
+    PPBnkCtxTrack   *tracks;
+    uint32_t        current_track;
+    uint32_t        bytes_read;
+} PPBnkCtx;
+
+enum {
+    PP_BNK_FLAG_PERSIST = (1 << 0), /*< This is a large file, keep in memory. */
+    PP_BNK_FLAG_MUSIC   = (1 << 1), /*< This is music. */
+    PP_BNK_FLAG_MASK    = (PP_BNK_FLAG_PERSIST | PP_BNK_FLAG_MUSIC)
+};
+
+static void pp_bnk_parse_header(PPBnkHeader *hdr, const uint8_t *buf)
+{
+    hdr->bank_id        = AV_RL32(buf +  0);
+    hdr->sample_rate    = AV_RL32(buf +  4);
+    hdr->always1        = AV_RL32(buf +  8);
+    hdr->track_count    = AV_RL32(buf + 12);
+    hdr->flags          = AV_RL32(buf + 16);
+}
+
+static void pp_bnk_parse_track(PPBnkTrack *trk, const uint8_t *buf)
+{
+    trk->id             = AV_RL32(buf +  0);
+    trk->size           = AV_RL32(buf +  4);
+    trk->sample_rate    = AV_RL32(buf +  8);
+    trk->always1_1      = AV_RL32(buf + 12);
+    trk->always1_2      = AV_RL32(buf + 16);
+}
+
+static int pp_bnk_probe(const AVProbeData *p)
+{
+    uint32_t sample_rate = AV_RL32(p->buf +  4);
+    uint32_t track_count = AV_RL32(p->buf + 12);
+    uint32_t flags       = AV_RL32(p->buf + 16);
+
+    if (track_count == 0 || track_count > INT_MAX)
+        return 0;
+
+    if ((sample_rate !=  5512) && (sample_rate != 11025) &&
+        (sample_rate != 22050) && (sample_rate != 44100))
+        return 0;
+
+    /* Check the first track header. */
+    if (AV_RL32(p->buf + 28) != sample_rate)
+        return 0;
+
+    if ((flags & ~PP_BNK_FLAG_MASK) != 0)
+        return 0;
+
+    return AVPROBE_SCORE_MAX / 4 + 1;
+}
+
+static int pp_bnk_read_header(AVFormatContext *s)
+{
+    int64_t ret;
+    AVStream *st;
+    AVCodecParameters *par;
+    PPBnkCtx *ctx = s->priv_data;
+    uint8_t buf[FFMAX(PP_BNK_FILE_HEADER_SIZE, PP_BNK_TRACK_SIZE)];
+    PPBnkHeader hdr;
+
+    if ((ret = avio_read(s->pb, buf, PP_BNK_FILE_HEADER_SIZE)) < 0)
+        return ret;
+    else if (ret != PP_BNK_FILE_HEADER_SIZE)
+        return AVERROR(EIO);
+
+    pp_bnk_parse_header(&hdr, buf);
+
+    if (hdr.track_count == 0 || hdr.track_count > INT_MAX)
+        return AVERROR_INVALIDDATA;
+
+    if (hdr.sample_rate == 0 || hdr.sample_rate > INT_MAX)
+        return AVERROR_INVALIDDATA;
+
+    if (hdr.always1 != 1) {
+        avpriv_request_sample(s, "Non-one header value");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    ctx->track_count = hdr.track_count;
+
+    if (!(ctx->tracks = av_malloc_array(hdr.track_count, sizeof(PPBnkCtxTrack))))
+        return AVERROR(ENOMEM);
+
+    /* Parse and validate each track. */
+    for (int i = 0; i < hdr.track_count; i++) {
+        PPBnkTrack e;
+        PPBnkCtxTrack *trk = ctx->tracks + i;
+
+        ret = avio_read(s->pb, buf, PP_BNK_TRACK_SIZE);
+        if (ret < 0 && ret != AVERROR_EOF)
+            goto fail;
+
+        /* Short byte-count or EOF, we have a truncated file. */
+        if (ret != PP_BNK_TRACK_SIZE) {
+            av_log(s, AV_LOG_WARNING, "File truncated at %d/%u track(s)\n",
+                   i, hdr.track_count);
+            ctx->track_count = i;
+            break;
+        }
+
+        pp_bnk_parse_track(&e, buf);
+
+        /* The individual sample rates of all tracks must match that of the file header. */
+        if (e.sample_rate != hdr.sample_rate) {
+            ret = AVERROR_INVALIDDATA;
+            goto fail;
+        }
+
+        if (e.always1_1 != 1 || e.always1_2 != 1) {
+            avpriv_request_sample(s, "Non-one track header values");
+            ret = AVERROR_PATCHWELCOME;
+            goto fail;
+        }
+
+        trk->data_offset = avio_tell(s->pb);
+        trk->data_size   = e.size;
+
+        /*
+         * Skip over the data to the next stream header.
+         * Sometimes avio_skip() doesn't detect EOF. If it doesn't, either:
+         *   - the avio_read() above will, or
+         *   - pp_bnk_read_packet() will read a truncated last track.
+         */
+        if ((ret = avio_skip(s->pb, e.size)) == AVERROR_EOF) {
+            ctx->track_count = i + 1;
+            av_log(s, AV_LOG_WARNING,
+                   "Track %d has truncated data, assuming track count == %d\n",
+                   i, ctx->track_count);
+            break;
+        } else if (ret < 0) {
+            goto fail;
+        }
+    }
+
+    /* File is only a header. */
+    if (ctx->track_count == 0) {
+        ret = AVERROR_INVALIDDATA;
+        goto fail;
+    }
+
+    /* Build the streams. */
+    for (int i = 0; i < ctx->track_count; i++) {
+        if (!(st = avformat_new_stream(s, NULL))) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+
+        par                         = st->codecpar;
+        par->codec_type             = AVMEDIA_TYPE_AUDIO;
+        par->codec_id               = AV_CODEC_ID_ADPCM_IMA_CUNNING;
+        par->format                 = AV_SAMPLE_FMT_S16;
+        par->channel_layout         = AV_CH_LAYOUT_MONO;
+        par->channels               = 1;
+        par->sample_rate            = hdr.sample_rate;
+        par->bits_per_coded_sample  = 4;
+        par->bits_per_raw_sample    = 16;
+        par->block_align            = 1;
+        par->bit_rate               = par->sample_rate * par->bits_per_coded_sample;
+
+        avpriv_set_pts_info(st, 64, 1, par->sample_rate);
+        st->start_time              = 0;
+        st->duration                = ctx->tracks[i].data_size * 2;
+    }
+
+    /* Seek to the start of the first stream. */
+    if ((ret = avio_seek(s->pb, ctx->tracks[0].data_offset, SEEK_SET)) < 0) {
+        goto fail;
+    } else if (ret != ctx->tracks[0].data_offset) {
+        ret = AVERROR(EIO);
+        goto fail;
+    }
+
+    return 0;
+
+fail:
+    av_freep(&ctx->tracks);
+    return ret;
+}
+
+static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    int64_t ret;
+    int size;
+    PPBnkCtx *ctx = s->priv_data;
+    PPBnkCtxTrack *trk = ctx->tracks + ctx->current_track;
+
+    av_assert0(ctx->bytes_read <= trk->data_size);
+
+    if (ctx->bytes_read == trk->data_size) {
+        if (ctx->current_track == ctx->track_count - 1)
+            return AVERROR_EOF;
+
+        trk++;
+
+        if ((ret = avio_seek(s->pb, trk->data_offset, SEEK_SET)) < 0)
+            return ret;
+        else if (ret != trk->data_offset)
+            return AVERROR(EIO);
+
+        ctx->bytes_read = 0;
+        ctx->current_track++;
+    }
+
+    size = FFMIN(trk->data_size - ctx->bytes_read, PP_BNK_MAX_READ_SIZE);
+
+    if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
+        return ret;
+
+    ctx->bytes_read    += ret;
+    pkt->flags         &= ~AV_PKT_FLAG_CORRUPT;
+    pkt->stream_index   = ctx->current_track;
+    pkt->duration       = ret * 2;
+
+    return 0;
+}
+
+static int pp_bnk_read_close(AVFormatContext *s)
+{
+    PPBnkCtx *ctx = s->priv_data;
+
+    av_freep(&ctx->tracks);
+
+    return 0;
+}
+
+AVInputFormat ff_pp_bnk_demuxer = {
+    .name           = "pp_bnk",
+    .long_name      = NULL_IF_CONFIG_SMALL("Pro Pinball Series Soundbank"),
+    .priv_data_size = sizeof(PPBnkCtx),
+    .read_probe     = pp_bnk_probe,
+    .read_header    = pp_bnk_read_header,
+    .read_packet    = pp_bnk_read_packet,
+    .read_close     = pp_bnk_read_close
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 719cda6b98..493a0b337f 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  42
-#define LIBAVFORMAT_VERSION_MICRO 101
+#define LIBAVFORMAT_VERSION_MINOR  43
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \