Use MonoView for deinterleaved channels in AudioFrameView

Allow skipping the deinterleaving steps in PushResampler
before resampling when deinterleaved buffers already exist.

Bug: chromium:335805780
Change-Id: I2080ce2624636cb743beef78f6f08887db01120f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/352202
Reviewed-by: Per Åhgren <peah@webrtc.org>
Auto-Submit: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42438}
This commit is contained in:
Tommi 2024-06-04 20:08:27 +02:00 committed by WebRTC LUCI CQ
parent 6431a64f02
commit 67fd83eae2
6 changed files with 28 additions and 17 deletions

View File

@ -243,7 +243,7 @@ void CopySamples(D& destination, const S& source) {
// "Incompatible view types"); // "Incompatible view types");
RTC_DCHECK_EQ(NumChannels(destination), NumChannels(source)); RTC_DCHECK_EQ(NumChannels(destination), NumChannels(source));
RTC_DCHECK_EQ(SamplesPerChannel(destination), SamplesPerChannel(source)); RTC_DCHECK_EQ(SamplesPerChannel(destination), SamplesPerChannel(source));
RTC_DCHECK_GE(destination.data().size(), source.data().size()); RTC_DCHECK_GE(destination.size(), source.size());
memcpy(&destination[0], &source[0], memcpy(&destination[0], &source[0],
source.size() * sizeof(typename S::value_type)); source.size() * sizeof(typename S::value_type));
} }

View File

@ -37,8 +37,12 @@ class PushResampler {
// Returns the total number of samples provided in destination (e.g. 32 kHz, // Returns the total number of samples provided in destination (e.g. 32 kHz,
// 2 channel audio gives 640 samples). // 2 channel audio gives 640 samples).
int Resample(InterleavedView<const T> src, InterleavedView<T> dst); int Resample(InterleavedView<const T> src, InterleavedView<T> dst);
// For when a deinterleaved/mono channel already exists and we can skip the
// deinterleaved operation.
int Resample(MonoView<const T> src, MonoView<T> dst);
private: private:
// Buffers used for when a deinterleaving step is necessary.
std::unique_ptr<T[]> source_; std::unique_ptr<T[]> source_;
std::unique_ptr<T[]> destination_; std::unique_ptr<T[]> destination_;
DeinterleavedView<T> source_view_; DeinterleavedView<T> source_view_;

View File

@ -95,6 +95,20 @@ int PushResampler<T>::Resample(InterleavedView<const T> src,
return static_cast<int>(dst.size()); return static_cast<int>(dst.size());
} }
template <typename T>
int PushResampler<T>::Resample(MonoView<const T> src, MonoView<T> dst) {
RTC_DCHECK_EQ(resamplers_.size(), 1);
RTC_DCHECK_EQ(SamplesPerChannel(src), SamplesPerChannel(source_view_));
RTC_DCHECK_EQ(SamplesPerChannel(dst), SamplesPerChannel(destination_view_));
if (SamplesPerChannel(src) == SamplesPerChannel(dst)) {
CopySamples(dst, src);
return static_cast<int>(src.size());
}
return resamplers_[0]->Resample(src, dst);
}
// Explictly generate required instantiations. // Explictly generate required instantiations.
template class PushResampler<int16_t>; template class PushResampler<int16_t>;
template class PushResampler<float>; template class PushResampler<float>;

View File

@ -269,7 +269,7 @@ rtc_library("audio_processing_statistics") {
rtc_source_set("audio_frame_view") { rtc_source_set("audio_frame_view") {
sources = [ "include/audio_frame_view.h" ] sources = [ "include/audio_frame_view.h" ]
deps = [ "../../api:array_view" ] deps = [ "../../api/audio:audio_frame_api" ]
} }
if (rtc_enable_protobuf) { if (rtc_enable_protobuf) {

View File

@ -102,18 +102,11 @@ float VoiceActivityDetectorWrapper::Analyze(AudioFrameView<const float> frame) {
vad_->Reset(); vad_->Reset();
time_to_vad_reset_ = vad_reset_period_frames_; time_to_vad_reset_ = vad_reset_period_frames_;
} }
// Resample the first channel of `frame`. // Resample the first channel of `frame`.
RTC_DCHECK_EQ(frame.samples_per_channel(), frame_size_); RTC_DCHECK_EQ(frame.samples_per_channel(), frame_size_);
MonoView<float> dst(resampled_buffer_.data(), resampled_buffer_.size());
// TODO: b/335805780 - channel() should return a MonoView<> which there resampler_.Resample(frame.channel(0), dst);
// should be a Resample() implementation for. There's no need to
// "deinterleave" a mono buffer, which is what Resample() currently does,
// so here we should be able to directly resample the channel buffer.
auto channel = frame.channel(0);
InterleavedView<const float> src(channel.data(), channel.size(), 1);
InterleavedView<float> dst(resampled_buffer_.data(), resampled_buffer_.size(),
1);
resampler_.Resample(src, dst);
return vad_->Analyze(resampled_buffer_); return vad_->Analyze(resampled_buffer_);
} }

View File

@ -11,7 +11,7 @@
#ifndef MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_ #ifndef MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_
#define MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_ #define MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_
#include "api/array_view.h" #include "api/audio/audio_view.h"
namespace webrtc { namespace webrtc {
@ -44,16 +44,16 @@ class AudioFrameView {
int samples_per_channel() const { return channel_size_; } int samples_per_channel() const { return channel_size_; }
rtc::ArrayView<T> channel(int idx) { MonoView<T> channel(int idx) {
RTC_DCHECK_LE(0, idx); RTC_DCHECK_LE(0, idx);
RTC_DCHECK_LE(idx, num_channels_); RTC_DCHECK_LE(idx, num_channels_);
return rtc::ArrayView<T>(audio_samples_[idx], channel_size_); return MonoView<T>(audio_samples_[idx], channel_size_);
} }
rtc::ArrayView<const T> channel(int idx) const { MonoView<const T> channel(int idx) const {
RTC_DCHECK_LE(0, idx); RTC_DCHECK_LE(0, idx);
RTC_DCHECK_LE(idx, num_channels_); RTC_DCHECK_LE(idx, num_channels_);
return rtc::ArrayView<const T>(audio_samples_[idx], channel_size_); return MonoView<const T>(audio_samples_[idx], channel_size_);
} }
T* const* data() { return audio_samples_; } T* const* data() { return audio_samples_; }