From patchwork Mon Mar 16 03:31:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zane van Iperen X-Patchwork-Id: 18228 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1FAFE44B5DE for ; Mon, 16 Mar 2020 05:32:01 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F37CF68B3D0; Mon, 16 Mar 2020 05:32:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail2.protonmail.ch (mail2.protonmail.ch [185.70.40.22]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9CCB768B3D0 for ; Mon, 16 Mar 2020 05:31:54 +0200 (EET) Date: Mon, 16 Mar 2020 03:31:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=protonmail; t=1584329513; bh=aybDpVV1DpKz7+Smpv6pNpZ+hBR2KG5ub4u9js2uUAM=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References: Feedback-ID:From; b=uPG0P2Ic9GZ1cVBAsuq0vVLcUwqxxuj62+jAZwah8uYP/HODtAaVfoII3c25GfPBM XJkCMx52ujFLemi9OYPM5fivY899bCN8GJnyVGC5GteYW0rynJ6nlpebpVK3u+229Z XsWmPnRzke+XOW21HF5Ks5sU9MmNZwLqm8a9Hg8k= To: ffmpeg-devel@ffmpeg.org From: Zane van Iperen Message-ID: <20200316033110.3958-3-zane@zanevaniperen.com> In-Reply-To: <20200316033110.3958-1-zane@zanevaniperen.com> References: <20200316033110.3958-1-zane@zanevaniperen.com> Feedback-ID: xylLYHwBzJW7F28tHN-oL9EBm6h5yqtCqG6YwPpJ2oMwBYJT6-HxTnFTF6p18axLiSywX61iUfj4CElBGg8-GA==:Ext:ProtonMail MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mail.protonmail.ch Subject: [FFmpeg-devel] [PATCH 2/2] avformat: add demuxer for Pro Pinball Series' Soundbanks X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Zane van Iperen Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Zane van Iperen --- 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 --git a/Changelog b/Changelog index eeaa643e88..d84621f7d8 100644 --- a/Changelog +++ b/Changelog @@ -49,6 +49,7 @@ version : - 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, \