diff mbox series

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

Message ID 20200316033110.3958-3-zane@zanevaniperen.com
State Superseded
Headers show
Series Pro Pinball Series Soundbank demuxer + decoder.
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork warning New warnings during build
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Zane van Iperen March 16, 2020, 3:31 a.m. UTC
Signed-off-by: Zane van Iperen <zane@zanevaniperen.com>
---
 Changelog                |   1 +
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/pp_bnk.c     | 213 +++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |   2 +-
 5 files changed, 217 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/pp_bnk.c
diff mbox series

Patch

diff --git a/Changelog b/Changelog
index eeaa643e88..d84621f7d8 100644
--- a/Changelog
+++ b/Changelog
@@ -49,6 +49,7 @@  version <next>:
 - Vulkan support
 - avgblur_vulkan, overlay_vulkan, scale_vulkan and chromaber_vulkan filters
 - Cunning Developments ADPCM decoder
+- Pro Pinball Series Soundbank demuxer
 
 
 version 4.2:
diff --git a/libavformat/Makefile b/libavformat/Makefile
index f84becd30a..1a9655ff40 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -425,6 +425,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 08012ea208..04aab18687 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -338,6 +338,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..ba490f3084
--- /dev/null
+++ b/libavformat/pp_bnk.c
@@ -0,0 +1,213 @@ 
+/*
+ * 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"
+
+#define FJBNK_MAX_READ_SIZE     4096
+#define FJBNK_FILE_HEADER_SIZE  20
+#define FJBNK_ENTRY_SIZE        20
+
+typedef struct PPBnkHeader {
+    uint32_t        bank_id;
+    uint32_t        sample_rate;
+    uint32_t        always1;
+    uint32_t        entry_count;
+    uint32_t        unknown;        /*< Possibly flags. 2 == music, 0 == sfx? */
+} PPBnkHeader;
+
+typedef struct PPBnkEntry {
+    uint32_t        id;             /*< Entry ID. Usually entry[i].id == entry[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. */
+} PPBnkEntry;
+
+typedef struct PPBnkCtxEntry {
+    int64_t         data_offset;
+    uint32_t        data_size;
+} PPBnkCtxEntry;
+
+typedef struct PPBnkCtx {
+    int             entry_count;
+    PPBnkCtxEntry   *entries;
+    uint32_t        i;
+    uint32_t        bytes_read;
+} PPBnkCtx;
+
+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->entry_count    = AV_RL32(buf + 12);
+    hdr->unknown        = AV_RL32(buf + 16);
+}
+
+static void pp_bnk_parse_entry(PPBnkEntry *e, const uint8_t *buf)
+{
+    e->id               = AV_RL32(buf +  0);
+    e->size             = AV_RL32(buf +  4);
+    e->sample_rate      = AV_RL32(buf +  8);
+    e->always1_1        = AV_RL32(buf + 12);
+    e->always1_2        = AV_RL32(buf + 16);
+}
+
+static int pp_bnk_read_header(AVFormatContext *s)
+{
+    int ret;
+    AVStream *st;
+    AVCodecParameters *par;
+    PPBnkCtx *ctx = s->priv_data;
+    uint8_t buf[FFMAX(FJBNK_FILE_HEADER_SIZE, FJBNK_ENTRY_SIZE)];
+    PPBnkHeader hdr;
+
+    if ((ret = avio_read(s->pb, buf, FJBNK_FILE_HEADER_SIZE)) < 0)
+        return ret;
+    else if (ret != FJBNK_FILE_HEADER_SIZE)
+        return AVERROR(EIO);
+
+    pp_bnk_parse_header(&hdr, buf);
+
+    if (hdr.entry_count == 0)
+        return AVERROR_INVALIDDATA;
+
+    ctx->entry_count = hdr.entry_count;
+    if ((ret = av_reallocp_array(&ctx->entries, hdr.entry_count, sizeof(PPBnkCtxEntry))))
+        return ret;
+
+    /* Parse and validate each track. */
+    for (int i = 0; i < hdr.entry_count; i++) {
+        PPBnkEntry e;
+
+        if ((ret = avio_read(s->pb, buf, FJBNK_ENTRY_SIZE)) < 0)
+            return ret;
+        else if (ret != FJBNK_ENTRY_SIZE)
+            return AVERROR(EIO);
+
+        pp_bnk_parse_entry(&e, buf);
+
+        /* The individual sample rates of all tracks must match that of the file header. */
+        if (e.sample_rate != hdr.sample_rate) {
+            return AVERROR_INVALIDDATA;
+        }
+
+        ctx->entries[i].data_offset = avio_tell(s->pb);
+        ctx->entries[i].data_size   = e.size;
+
+        /* Skip over the data to the next stream header. */
+        avio_skip(s->pb, e.size);
+    }
+
+    /* Build the streams. */
+    for (int i = 0; i < hdr.entry_count; i++) {
+
+        if (!(st = avformat_new_stream(s, NULL)))
+            return AVERROR(ENOMEM);
+
+        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->channels *
+                                      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->entries[i].data_size * 2;
+    }
+
+    /* Seek to the start of the first stream. */
+    if ((ret = avio_seek(s->pb, ctx->entries[0].data_offset, SEEK_SET)) < 0)
+        return ret;
+    else if (ret != ctx->entries[0].data_offset)
+        return AVERROR(EIO);
+
+    return 0;
+}
+
+static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    int ret, size;
+    PPBnkCtx *ctx = s->priv_data;
+    PPBnkCtxEntry *e = ctx->entries + ctx->i;
+    AVCodecParameters *par;
+
+    av_assert0(ctx->bytes_read <= e->data_size);
+
+    if (ctx->bytes_read == e->data_size) {
+        ctx->bytes_read  = 0;
+        ctx->i          += 1;
+        e               += 1;
+
+        if ((ret = avio_seek(s->pb, e->data_offset, SEEK_SET)) < 0)
+            return ret;
+        else if (ret != e->data_offset)
+            return AVERROR(EIO);
+    }
+
+    if (ctx->i == ctx->entry_count)
+        return AVERROR_EOF;
+
+    par  = s->streams[ctx->i]->codecpar;
+    size = FFMIN(e->data_size - ctx->bytes_read, FJBNK_MAX_READ_SIZE);
+
+    if ((ret = av_get_packet(s->pb, pkt, size)) < 0)
+        return ret;
+    else if (ret != size)
+        return AVERROR_INVALIDDATA;
+
+    ctx->bytes_read     += ret;
+
+    pkt->stream_index   = ctx->i;
+    pkt->duration       = ret * 2;
+    return 0;
+}
+
+static int pp_bnk_read_close(AVFormatContext *s)
+{
+    PPBnkCtx *ctx = s->priv_data;
+
+    if (ctx->entries)
+        av_freep(&ctx->entries);
+
+    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_header    = pp_bnk_read_header,
+    .read_packet    = pp_bnk_read_packet,
+    .read_close     = pp_bnk_read_close,
+    .extensions     = "5c,11c,22c,44c"
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index e815c1f3c4..18c2f5fec2 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@ 
 // 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  41
+#define LIBAVFORMAT_VERSION_MINOR  42
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \