| 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "modules/audio_processing/aec3/subtractor.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "api/array_view.h" |
| 17 | #include "modules/audio_processing/aec3/adaptive_fir_filter_erl.h" |
| 18 | #include "modules/audio_processing/aec3/fft_data.h" |
| 19 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/numerics/safe_minmax.h" |
| 22 | #include "system_wrappers/include/field_trial.h" |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | bool UseCoarseFilterResetHangover() { |
| 29 | return !field_trial::IsEnabled( |
| 30 | "WebRTC-Aec3CoarseFilterResetHangoverKillSwitch"); |
| 31 | } |
| 32 | |
| 33 | void PredictionError(const Aec3Fft& fft, |
| 34 | const FftData& S, |
| 35 | rtc::ArrayView<const float> y, |
| 36 | std::array<float, kBlockSize>* e, |
| 37 | std::array<float, kBlockSize>* s) { |
| 38 | std::array<float, kFftLength> tmp; |
| 39 | fft.Ifft(S, &tmp); |
| 40 | constexpr float kScale = 1.0f / kFftLengthBy2; |
| 41 | std::transform(y.begin(), y.end(), tmp.begin() + kFftLengthBy2, e->begin(), |
| 42 | [&](float a, float b) { return a - b * kScale; }); |
| 43 | |
| 44 | if (s) { |
| 45 | for (size_t k = 0; k < s->size(); ++k) { |
| 46 | (*s)[k] = kScale * tmp[k + kFftLengthBy2]; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void ScaleFilterOutput(rtc::ArrayView<const float> y, |
| 52 | float factor, |
| 53 | rtc::ArrayView<float> e, |
| 54 | rtc::ArrayView<float> s) { |
| 55 | RTC_DCHECK_EQ(y.size(), e.size()); |
| 56 | RTC_DCHECK_EQ(y.size(), s.size()); |
| 57 | for (size_t k = 0; k < y.size(); ++k) { |
| 58 | s[k] *= factor; |
| 59 | e[k] = y[k] - s[k]; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | } // namespace |
| 64 | |
| 65 | Subtractor::Subtractor(const EchoCanceller3Config& config, |
| 66 | size_t num_render_channels, |
| 67 | size_t num_capture_channels, |
| 68 | ApmDataDumper* data_dumper, |
| 69 | Aec3Optimization optimization) |
| 70 | : fft_(), |
| 71 | data_dumper_(data_dumper), |
| 72 | optimization_(optimization), |
| 73 | config_(config), |
| 74 | num_capture_channels_(num_capture_channels), |
| 75 | use_coarse_filter_reset_hangover_(UseCoarseFilterResetHangover()), |
| 76 | refined_filters_(num_capture_channels_), |
| 77 | coarse_filter_(num_capture_channels_), |
| 78 | refined_gains_(num_capture_channels_), |
| 79 | coarse_gains_(num_capture_channels_), |
| 80 | filter_misadjustment_estimators_(num_capture_channels_), |
| 81 | poor_coarse_filter_counters_(num_capture_channels_, 0), |
| 82 | coarse_filter_reset_hangover_(num_capture_channels_, 0), |
| 83 | refined_frequency_responses_( |
| 84 | num_capture_channels_, |
| 85 | std::vector<std::array<float, kFftLengthBy2Plus1>>( |
| 86 | std::max(config_.filter.refined_initial.length_blocks, |
| 87 | config_.filter.refined.length_blocks), |
| 88 | std::array<float, kFftLengthBy2Plus1>())), |
| 89 | refined_impulse_responses_( |
| 90 | num_capture_channels_, |
| 91 | std::vector<float>(GetTimeDomainLength(std::max( |
| 92 | config_.filter.refined_initial.length_blocks, |
| 93 | config_.filter.refined.length_blocks)), |
| 94 | 0.f)) { |
| 95 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 96 | refined_filters_[ch] = std::make_unique<AdaptiveFirFilter>( |
| 97 | config_.filter.refined.length_blocks, |
| 98 | config_.filter.refined_initial.length_blocks, |
| 99 | config.filter.config_change_duration_blocks, num_render_channels, |
| 100 | optimization, data_dumper_); |
| 101 | |
| 102 | coarse_filter_[ch] = std::make_unique<AdaptiveFirFilter>( |
| 103 | config_.filter.coarse.length_blocks, |
| 104 | config_.filter.coarse_initial.length_blocks, |
| 105 | config.filter.config_change_duration_blocks, num_render_channels, |
| 106 | optimization, data_dumper_); |
| 107 | refined_gains_[ch] = std::make_unique<RefinedFilterUpdateGain>( |
| 108 | config_.filter.refined_initial, |
| 109 | config_.filter.config_change_duration_blocks); |
| 110 | coarse_gains_[ch] = std::make_unique<CoarseFilterUpdateGain>( |
| 111 | config_.filter.coarse_initial, |
| 112 | config.filter.config_change_duration_blocks); |
| 113 | } |
| 114 | |
| 115 | RTC_DCHECK(data_dumper_); |
| 116 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 117 | for (auto& H2_k : refined_frequency_responses_[ch]) { |
| 118 | H2_k.fill(0.f); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | Subtractor::~Subtractor() = default; |
| 124 | |
| 125 | void Subtractor::HandleEchoPathChange( |
| 126 | const EchoPathVariability& echo_path_variability) { |
| 127 | const auto full_reset = [&]() { |
| 128 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 129 | refined_filters_[ch]->HandleEchoPathChange(); |
| 130 | coarse_filter_[ch]->HandleEchoPathChange(); |
| 131 | refined_gains_[ch]->HandleEchoPathChange(echo_path_variability); |
| 132 | coarse_gains_[ch]->HandleEchoPathChange(); |
| 133 | refined_gains_[ch]->SetConfig(config_.filter.refined_initial, true); |
| 134 | coarse_gains_[ch]->SetConfig(config_.filter.coarse_initial, true); |
| 135 | refined_filters_[ch]->SetSizePartitions( |
| 136 | config_.filter.refined_initial.length_blocks, true); |
| 137 | coarse_filter_[ch]->SetSizePartitions( |
| 138 | config_.filter.coarse_initial.length_blocks, true); |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | if (echo_path_variability.delay_change != |
| 143 | EchoPathVariability::DelayAdjustment::kNone) { |
| 144 | full_reset(); |
| 145 | } |
| 146 | |
| 147 | if (echo_path_variability.gain_change) { |
| 148 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 149 | refined_gains_[ch]->HandleEchoPathChange(echo_path_variability); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void Subtractor::ExitInitialState() { |
| 155 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 156 | refined_gains_[ch]->SetConfig(config_.filter.refined, false); |
| 157 | coarse_gains_[ch]->SetConfig(config_.filter.coarse, false); |
| 158 | refined_filters_[ch]->SetSizePartitions( |
| 159 | config_.filter.refined.length_blocks, false); |
| 160 | coarse_filter_[ch]->SetSizePartitions(config_.filter.coarse.length_blocks, |
| 161 | false); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void Subtractor::Process(const RenderBuffer& render_buffer, |
| 166 | const std::vector<std::vector<float>>& capture, |
| 167 | const RenderSignalAnalyzer& render_signal_analyzer, |
| 168 | const AecState& aec_state, |
| 169 | rtc::ArrayView<SubtractorOutput> outputs) { |
| 170 | RTC_DCHECK_EQ(num_capture_channels_, capture.size()); |
| 171 | |
| 172 | // Compute the render powers. |
| 173 | const bool same_filter_sizes = refined_filters_[0]->SizePartitions() == |
| 174 | coarse_filter_[0]->SizePartitions(); |
| 175 | std::array<float, kFftLengthBy2Plus1> X2_refined; |
| 176 | std::array<float, kFftLengthBy2Plus1> X2_coarse_data; |
| 177 | auto& X2_coarse = same_filter_sizes ? X2_refined : X2_coarse_data; |
| 178 | if (same_filter_sizes) { |
| 179 | render_buffer.SpectralSum(refined_filters_[0]->SizePartitions(), |
| 180 | &X2_refined); |
| 181 | } else if (refined_filters_[0]->SizePartitions() > |
| 182 | coarse_filter_[0]->SizePartitions()) { |
| 183 | render_buffer.SpectralSums(coarse_filter_[0]->SizePartitions(), |
| 184 | refined_filters_[0]->SizePartitions(), |
| 185 | &X2_coarse, &X2_refined); |
| 186 | } else { |
| 187 | render_buffer.SpectralSums(refined_filters_[0]->SizePartitions(), |
| 188 | coarse_filter_[0]->SizePartitions(), &X2_refined, |
| 189 | &X2_coarse); |
| 190 | } |
| 191 | |
| 192 | // Process all capture channels |
| 193 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 194 | RTC_DCHECK_EQ(kBlockSize, capture[ch].size()); |
| 195 | SubtractorOutput& output = outputs[ch]; |
| 196 | rtc::ArrayView<const float> y = capture[ch]; |
| 197 | FftData& E_refined = output.E_refined; |
| 198 | FftData E_coarse; |
| 199 | std::array<float, kBlockSize>& e_refined = output.e_refined; |
| 200 | std::array<float, kBlockSize>& e_coarse = output.e_coarse; |
| 201 | |
| 202 | FftData S; |
| 203 | FftData& G = S; |
| 204 | |
| 205 | // Form the outputs of the refined and coarse filters. |
| 206 | refined_filters_[ch]->Filter(render_buffer, &S); |
| 207 | PredictionError(fft_, S, y, &e_refined, &output.s_refined); |
| 208 | |
| 209 | coarse_filter_[ch]->Filter(render_buffer, &S); |
| 210 | PredictionError(fft_, S, y, &e_coarse, &output.s_coarse); |
| 211 | |
| 212 | // Compute the signal powers in the subtractor output. |
| 213 | output.ComputeMetrics(y); |
| 214 | |
| 215 | // Adjust the filter if needed. |
| 216 | bool refined_filters_adjusted = false; |
| 217 | filter_misadjustment_estimators_[ch].Update(output); |
| 218 | if (filter_misadjustment_estimators_[ch].IsAdjustmentNeeded()) { |
| 219 | float scale = filter_misadjustment_estimators_[ch].GetMisadjustment(); |
| 220 | refined_filters_[ch]->ScaleFilter(scale); |
| 221 | for (auto& h_k : refined_impulse_responses_[ch]) { |
| 222 | h_k *= scale; |
| 223 | } |
| 224 | ScaleFilterOutput(y, scale, e_refined, output.s_refined); |
| 225 | filter_misadjustment_estimators_[ch].Reset(); |
| 226 | refined_filters_adjusted = true; |
| 227 | } |
| 228 | |
| 229 | // Compute the FFts of the refined and coarse filter outputs. |
| 230 | fft_.ZeroPaddedFft(e_refined, Aec3Fft::Window::kHanning, &E_refined); |
| 231 | fft_.ZeroPaddedFft(e_coarse, Aec3Fft::Window::kHanning, &E_coarse); |
| 232 | |
| 233 | // Compute spectra for future use. |
| 234 | E_coarse.Spectrum(optimization_, output.E2_coarse); |
| 235 | E_refined.Spectrum(optimization_, output.E2_refined); |
| 236 | |
| 237 | // Update the refined filter. |
| 238 | if (!refined_filters_adjusted) { |
| 239 | // Do not allow the performance of the coarse filter to affect the |
| 240 | // adaptation speed of the refined filter just after the coarse filter has |
| 241 | // been reset. |
| 242 | const bool disallow_leakage_diverged = |
| 243 | coarse_filter_reset_hangover_[ch] > 0 && |
| 244 | use_coarse_filter_reset_hangover_; |
| 245 | |
| 246 | std::array<float, kFftLengthBy2Plus1> erl; |
| 247 | ComputeErl(optimization_, refined_frequency_responses_[ch], erl); |
| 248 | refined_gains_[ch]->Compute(X2_refined, render_signal_analyzer, output, |
| 249 | erl, refined_filters_[ch]->SizePartitions(), |
| 250 | aec_state.SaturatedCapture(), |
| 251 | disallow_leakage_diverged, &G); |
| 252 | } else { |
| 253 | G.re.fill(0.f); |
| 254 | G.im.fill(0.f); |
| 255 | } |
| 256 | refined_filters_[ch]->Adapt(render_buffer, G, |
| 257 | &refined_impulse_responses_[ch]); |
| 258 | refined_filters_[ch]->ComputeFrequencyResponse( |
| 259 | &refined_frequency_responses_[ch]); |
| 260 | |
| 261 | if (ch == 0) { |
| 262 | data_dumper_->DumpRaw("aec3_subtractor_G_refined", G.re); |
| 263 | data_dumper_->DumpRaw("aec3_subtractor_G_refined", G.im); |
| 264 | } |
| 265 | |
| 266 | // Update the coarse filter. |
| 267 | poor_coarse_filter_counters_[ch] = |
| 268 | output.e2_refined < output.e2_coarse |
| 269 | ? poor_coarse_filter_counters_[ch] + 1 |
| 270 | : 0; |
| 271 | if (poor_coarse_filter_counters_[ch] < 5) { |
| 272 | coarse_gains_[ch]->Compute(X2_coarse, render_signal_analyzer, E_coarse, |
| 273 | coarse_filter_[ch]->SizePartitions(), |
| 274 | aec_state.SaturatedCapture(), &G); |
| 275 | coarse_filter_reset_hangover_[ch] = |
| 276 | std::max(coarse_filter_reset_hangover_[ch] - 1, 0); |
| 277 | } else { |
| 278 | poor_coarse_filter_counters_[ch] = 0; |
| 279 | coarse_filter_[ch]->SetFilter(refined_filters_[ch]->SizePartitions(), |
| 280 | refined_filters_[ch]->GetFilter()); |
| 281 | coarse_gains_[ch]->Compute(X2_coarse, render_signal_analyzer, E_refined, |
| 282 | coarse_filter_[ch]->SizePartitions(), |
| 283 | aec_state.SaturatedCapture(), &G); |
| 284 | coarse_filter_reset_hangover_[ch] = |
| 285 | config_.filter.coarse_reset_hangover_blocks; |
| 286 | } |
| 287 | |
| 288 | coarse_filter_[ch]->Adapt(render_buffer, G); |
| 289 | if (ch == 0) { |
| 290 | data_dumper_->DumpRaw("aec3_subtractor_G_coarse", G.re); |
| 291 | data_dumper_->DumpRaw("aec3_subtractor_G_coarse", G.im); |
| 292 | filter_misadjustment_estimators_[ch].Dump(data_dumper_); |
| 293 | DumpFilters(); |
| 294 | } |
| 295 | |
| 296 | std::for_each(e_refined.begin(), e_refined.end(), |
| 297 | [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); }); |
| 298 | |
| 299 | if (ch == 0) { |
| 300 | data_dumper_->DumpWav("aec3_refined_filters_output", kBlockSize, |
| 301 | &e_refined[0], 16000, 1); |
| 302 | data_dumper_->DumpWav("aec3_coarse_filter_output", kBlockSize, |
| 303 | &e_coarse[0], 16000, 1); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void Subtractor::FilterMisadjustmentEstimator::Update( |
| 309 | const SubtractorOutput& output) { |
| 310 | e2_acum_ += output.e2_refined; |
| 311 | y2_acum_ += output.y2; |
| 312 | if (++n_blocks_acum_ == n_blocks_) { |
| 313 | if (y2_acum_ > n_blocks_ * 200.f * 200.f * kBlockSize) { |
| 314 | float update = (e2_acum_ / y2_acum_); |
| 315 | if (e2_acum_ > n_blocks_ * 7500.f * 7500.f * kBlockSize) { |
| 316 | // Duration equal to blockSizeMs * n_blocks_ * 4. |
| 317 | overhang_ = 4; |
| 318 | } else { |
| 319 | overhang_ = std::max(overhang_ - 1, 0); |
| 320 | } |
| 321 | |
| 322 | if ((update < inv_misadjustment_) || (overhang_ > 0)) { |
| 323 | inv_misadjustment_ += 0.1f * (update - inv_misadjustment_); |
| 324 | } |
| 325 | } |
| 326 | e2_acum_ = 0.f; |
| 327 | y2_acum_ = 0.f; |
| 328 | n_blocks_acum_ = 0; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void Subtractor::FilterMisadjustmentEstimator::Reset() { |
| 333 | e2_acum_ = 0.f; |
| 334 | y2_acum_ = 0.f; |
| 335 | n_blocks_acum_ = 0; |
| 336 | inv_misadjustment_ = 0.f; |
| 337 | overhang_ = 0.f; |
| 338 | } |
| 339 | |
| 340 | void Subtractor::FilterMisadjustmentEstimator::Dump( |
| 341 | ApmDataDumper* data_dumper) const { |
| 342 | data_dumper->DumpRaw("aec3_inv_misadjustment_factor", inv_misadjustment_); |
| 343 | } |
| 344 | |
| 345 | } // namespace webrtc |