From dabd9f699f4c4b6e3402ecb7c06f5b9518f08937 Mon Sep 17 00:00:00 2001 From: wangqr Date: Thu, 26 Dec 2019 17:31:59 -0500 Subject: [PATCH] Add option to downmix FFMS audio When enabled, restore FFMS to old behavior, downmixing auduo to S16 mono. This can reduce cache usage. Fix wangqr/Aegisub#31 --- src/audio_provider_ffmpegsource.cpp | 22 ++++++++++++---------- src/libresrc/default_config.json | 3 ++- src/preferences.cpp | 1 + src/project.cpp | 1 + 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/audio_provider_ffmpegsource.cpp b/src/audio_provider_ffmpegsource.cpp index 0a201855a..176a0ad81 100644 --- a/src/audio_provider_ffmpegsource.cpp +++ b/src/audio_provider_ffmpegsource.cpp @@ -165,17 +165,19 @@ void FFmpegSourceAudioProvider::LoadAudio(agi::fs::path const& filename) { throw agi::AudioProviderError("unknown or unsupported sample format"); } - if (channels > 1 || bytes_per_sample != 2) { - std::unique_ptr - opt(FFMS_CreateResampleOptions(AudioSource), FFMS_DestroyResampleOptions); - opt->ChannelLayout = FFMS_CH_FRONT_CENTER; - opt->SampleFormat = FFMS_FMT_S16; + if (OPT_GET("Provider/Audio/FFmpegSource/Downmix")->GetBool()) { + if (channels > 1 || bytes_per_sample != 2 || float_samples) { + std::unique_ptr + opt(FFMS_CreateResampleOptions(AudioSource), FFMS_DestroyResampleOptions); + opt->ChannelLayout = FFMS_CH_FRONT_CENTER; + opt->SampleFormat = FFMS_FMT_S16; - // Might fail if FFMS2 wasn't built with libavresample - if (!FFMS_SetOutputFormatA(AudioSource, opt.get(), nullptr)) { - channels = 1; - bytes_per_sample = 2; - float_samples = false; + // Might fail if FFMS2 wasn't built with libavresample + if (!FFMS_SetOutputFormatA(AudioSource, opt.get(), nullptr)) { + channels = 1; + bytes_per_sample = 2; + float_samples = false; + } } } } diff --git a/src/libresrc/default_config.json b/src/libresrc/default_config.json index 318f8d3ee..f5d26761b 100644 --- a/src/libresrc/default_config.json +++ b/src/libresrc/default_config.json @@ -331,7 +331,8 @@ "Sample Rate" : 0 }, "FFmpegSource" : { - "Decode Error Handling" : "ignore" + "Decode Error Handling" : "ignore", + "Downmix" : false } }, "Avisynth" : { diff --git a/src/preferences.cpp b/src/preferences.cpp index f6320dd75..91ebacee7 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -401,6 +401,7 @@ void Advanced_Audio(wxTreebook *book, Preferences *parent) { p->OptionChoice(ffms, _("Audio indexing error handling mode"), error_modes_choice, "Provider/Audio/FFmpegSource/Decode Error Handling"); p->OptionAdd(ffms, _("Always index all audio tracks"), "Provider/FFmpegSource/Index All Tracks"); + p->OptionAdd(ffms, _("Downmix to 16bit mono audio"), "Provider/Audio/FFmpegSource/Downmix"); #endif #ifdef WITH_PORTAUDIO diff --git a/src/project.cpp b/src/project.cpp index e01dd3749..b6230af23 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -53,6 +53,7 @@ Project::Project(agi::Context *c) : context(c) { OPT_SUB("Audio/Cache/Type", &Project::ReloadAudio, this); OPT_SUB("Audio/Provider", &Project::ReloadAudio, this); OPT_SUB("Provider/Audio/FFmpegSource/Decode Error Handling", &Project::ReloadAudio, this); + OPT_SUB("Provider/Audio/FFmpegSource/Downmix", &Project::ReloadAudio, this); OPT_SUB("Provider/Avisynth/Allow Ancient", &Project::ReloadVideo, this); OPT_SUB("Provider/Avisynth/Memory Max", &Project::ReloadVideo, this); OPT_SUB("Provider/Video/FFmpegSource/Decoding Threads", &Project::ReloadVideo, this);