From 3fc5a2087d6fe72dd09b588d16b6c66ccd0ba9c8 Mon Sep 17 00:00:00 2001 From: Alex Loiko Date: Tue, 2 Oct 2018 14:09:46 +0200 Subject: [PATCH] Add support for many channels in push_resampler. The PushResampler has a SincResampler per channel. Before this CL, it was hard-coded to handle up to 2 channels. In this CL I made it handle arbitrarily many. Bug: webrtc:8649 Change-Id: Ia2f33e45535f8bbda59090f8a0847546ff7edd75 Reviewed-on: https://webrtc-review.googlesource.com/103000 Commit-Queue: Alex Loiko Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#24928} --- common_audio/BUILD.gn | 2 + .../resampler/include/push_resampler.h | 16 ++--- common_audio/resampler/push_resampler.cc | 62 ++++++++++--------- .../resampler/push_resampler_unittest.cc | 5 +- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/common_audio/BUILD.gn b/common_audio/BUILD.gn index 4ff7986e82..abcfe9ad16 100644 --- a/common_audio/BUILD.gn +++ b/common_audio/BUILD.gn @@ -57,6 +57,8 @@ rtc_static_library("common_audio") { "../system_wrappers", "../system_wrappers:cpu_features_api", "third_party/fft4g:fft4g", + "//third_party/abseil-cpp/absl/container:inlined_vector", + "//third_party/abseil-cpp/absl/memory:memory", "//third_party/abseil-cpp/absl/types:optional", ] diff --git a/common_audio/resampler/include/push_resampler.h b/common_audio/resampler/include/push_resampler.h index 082cdc6284..232ad2a79f 100644 --- a/common_audio/resampler/include/push_resampler.h +++ b/common_audio/resampler/include/push_resampler.h @@ -12,6 +12,7 @@ #define COMMON_AUDIO_RESAMPLER_INCLUDE_PUSH_RESAMPLER_H_ #include +#include namespace webrtc { @@ -36,17 +37,18 @@ class PushResampler { int Resample(const T* src, size_t src_length, T* dst, size_t dst_capacity); private: - std::unique_ptr sinc_resampler_; - std::unique_ptr sinc_resampler_right_; int src_sample_rate_hz_; int dst_sample_rate_hz_; size_t num_channels_; - std::unique_ptr src_left_; - std::unique_ptr src_right_; - std::unique_ptr dst_left_; - std::unique_ptr dst_right_; -}; + struct ChannelResampler { + std::unique_ptr resampler; + std::vector source; + std::vector destination; + }; + + std::vector channel_resamplers_; +}; } // namespace webrtc #endif // COMMON_AUDIO_RESAMPLER_INCLUDE_PUSH_RESAMPLER_H_ diff --git a/common_audio/resampler/push_resampler.cc b/common_audio/resampler/push_resampler.cc index cc24c4b3f6..318d97b72c 100644 --- a/common_audio/resampler/push_resampler.cc +++ b/common_audio/resampler/push_resampler.cc @@ -12,6 +12,8 @@ #include +#include "absl/container/inlined_vector.h" +#include "absl/memory/memory.h" #include "common_audio/include/audio_util.h" #include "common_audio/resampler/include/resampler.h" #include "common_audio/resampler/push_sinc_resampler.h" @@ -34,7 +36,6 @@ void CheckValidInitParams(int src_sample_rate_hz, RTC_DCHECK_GT(src_sample_rate_hz, 0); RTC_DCHECK_GT(dst_sample_rate_hz, 0); RTC_DCHECK_GT(num_channels, 0); - RTC_DCHECK_LE(num_channels, 2); #endif } @@ -76,8 +77,7 @@ int PushResampler::InitializeIfNeeded(int src_sample_rate_hz, return 0; } - if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0 || - num_channels > 2) { + if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0) { return -1; } @@ -89,15 +89,14 @@ int PushResampler::InitializeIfNeeded(int src_sample_rate_hz, static_cast(src_sample_rate_hz / 100); const size_t dst_size_10ms_mono = static_cast(dst_sample_rate_hz / 100); - sinc_resampler_.reset( - new PushSincResampler(src_size_10ms_mono, dst_size_10ms_mono)); - if (num_channels_ == 2) { - src_left_.reset(new T[src_size_10ms_mono]); - src_right_.reset(new T[src_size_10ms_mono]); - dst_left_.reset(new T[dst_size_10ms_mono]); - dst_right_.reset(new T[dst_size_10ms_mono]); - sinc_resampler_right_.reset( - new PushSincResampler(src_size_10ms_mono, dst_size_10ms_mono)); + channel_resamplers_.clear(); + for (size_t i = 0; i < num_channels; ++i) { + channel_resamplers_.push_back(ChannelResampler()); + auto channel_resampler = channel_resamplers_.rbegin(); + channel_resampler->resampler = absl::make_unique( + src_size_10ms_mono, dst_size_10ms_mono); + channel_resampler->source.resize(src_size_10ms_mono); + channel_resampler->destination.resize(dst_size_10ms_mono); } return 0; @@ -117,25 +116,32 @@ int PushResampler::Resample(const T* src, memcpy(dst, src, src_length * sizeof(T)); return static_cast(src_length); } - if (num_channels_ == 2) { - const size_t src_length_mono = src_length / num_channels_; - const size_t dst_capacity_mono = dst_capacity / num_channels_; - T* deinterleaved[] = {src_left_.get(), src_right_.get()}; - Deinterleave(src, src_length_mono, num_channels_, deinterleaved); - size_t dst_length_mono = sinc_resampler_->Resample( - src_left_.get(), src_length_mono, dst_left_.get(), dst_capacity_mono); - sinc_resampler_right_->Resample(src_right_.get(), src_length_mono, - dst_right_.get(), dst_capacity_mono); + const size_t src_length_mono = src_length / num_channels_; + const size_t dst_capacity_mono = dst_capacity / num_channels_; - deinterleaved[0] = dst_left_.get(); - deinterleaved[1] = dst_right_.get(); - Interleave(deinterleaved, dst_length_mono, num_channels_, dst); - return static_cast(dst_length_mono * num_channels_); - } else { - return static_cast( - sinc_resampler_->Resample(src, src_length, dst, dst_capacity)); + absl::InlinedVector source_pointers; + for (auto& resampler : channel_resamplers_) { + source_pointers.push_back(resampler.source.data()); } + + Deinterleave(src, src_length_mono, num_channels_, source_pointers.data()); + + size_t dst_length_mono = 0; + + for (auto& resampler : channel_resamplers_) { + dst_length_mono = resampler.resampler->Resample( + resampler.source.data(), src_length_mono, resampler.destination.data(), + dst_capacity_mono); + } + + absl::InlinedVector destination_pointers; + for (auto& resampler : channel_resamplers_) { + destination_pointers.push_back(resampler.destination.data()); + } + + Interleave(destination_pointers.data(), dst_length_mono, num_channels_, dst); + return static_cast(dst_length_mono * num_channels_); } // Explictly generate required instantiations. diff --git a/common_audio/resampler/push_resampler_unittest.cc b/common_audio/resampler/push_resampler_unittest.cc index 6a0c60a734..3a1d5c551a 100644 --- a/common_audio/resampler/push_resampler_unittest.cc +++ b/common_audio/resampler/push_resampler_unittest.cc @@ -25,6 +25,7 @@ TEST(PushResamplerTest, VerifiesInputParameters) { PushResampler resampler; EXPECT_EQ(0, resampler.InitializeIfNeeded(16000, 16000, 1)); EXPECT_EQ(0, resampler.InitializeIfNeeded(16000, 16000, 2)); + EXPECT_EQ(0, resampler.InitializeIfNeeded(16000, 16000, 8)); } #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) @@ -45,10 +46,6 @@ TEST(PushResamplerTest, VerifiesBadInputParameters3) { EXPECT_DEATH(resampler.InitializeIfNeeded(16000, 16000, 0), "num_channels"); } -TEST(PushResamplerTest, VerifiesBadInputParameters4) { - PushResampler resampler; - EXPECT_DEATH(resampler.InitializeIfNeeded(16000, 16000, 3), "num_channels"); -} #endif #endif