blob: b1eba18fbcc53361b16d48ec8fe7fea5e07ba98f [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/subtractor.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <algorithm>
Per Åhgren1b4059e2017-10-15 20:19:21 +020014#include <numeric>
peah522d71b2017-02-23 05:16:26 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/array_view.h"
17#include "modules/audio_processing/logging/apm_data_dumper.h"
18#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010019#include "rtc_base/numerics/safe_minmax.h"
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020020#include "system_wrappers/include/field_trial.h"
peah522d71b2017-02-23 05:16:26 -080021
22namespace webrtc {
23
24namespace {
25
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020026bool EnableAdaptationDuringSaturation() {
27 return !field_trial::IsEnabled("WebRTC-Aec3RapidAgcGainRecoveryKillSwitch");
28}
29
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020030bool EnableMisadjustmentEstimator() {
31 return !field_trial::IsEnabled("WebRTC-Aec3MisadjustmentEstimatorKillSwitch");
32}
33
peah86afe9d2017-04-06 15:45:32 -070034void PredictionError(const Aec3Fft& fft,
35 const FftData& S,
36 rtc::ArrayView<const float> y,
37 std::array<float, kBlockSize>* e,
Per Åhgren9845a672018-01-15 13:09:02 +010038 std::array<float, kBlockSize>* s,
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020039 bool adaptation_during_saturation,
Per Åhgren9845a672018-01-15 13:09:02 +010040 bool* saturation) {
Per Åhgren7634c162017-12-18 15:45:49 +010041 std::array<float, kFftLength> tmp;
42 fft.Ifft(S, &tmp);
peah522d71b2017-02-23 05:16:26 -080043 constexpr float kScale = 1.0f / kFftLengthBy2;
Per Åhgren7634c162017-12-18 15:45:49 +010044 std::transform(y.begin(), y.end(), tmp.begin() + kFftLengthBy2, e->begin(),
45 [&](float a, float b) { return a - b * kScale; });
peah29103572017-07-11 02:54:02 -070046
Per Åhgren9845a672018-01-15 13:09:02 +010047 *saturation = false;
48
peah29103572017-07-11 02:54:02 -070049 if (s) {
50 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010051 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070052 }
Per Åhgren9845a672018-01-15 13:09:02 +010053 auto result = std::minmax_element(s->begin(), s->end());
54 *saturation = *result.first <= -32768 || *result.first >= 32767;
55 }
56 if (!(*saturation)) {
57 auto result = std::minmax_element(e->begin(), e->end());
58 *saturation = *result.first <= -32768 || *result.first >= 32767;
peah29103572017-07-11 02:54:02 -070059 }
Per Åhgren7634c162017-12-18 15:45:49 +010060
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020061 if (!adaptation_during_saturation) {
62 std::for_each(e->begin(), e->end(),
63 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
64 } else {
65 *saturation = false;
66 }
peah522d71b2017-02-23 05:16:26 -080067}
Per Åhgrenec22e3f2017-12-20 15:20:37 +010068
peah522d71b2017-02-23 05:16:26 -080069} // namespace
70
Per Åhgren09a718a2017-12-11 22:28:45 +010071Subtractor::Subtractor(const EchoCanceller3Config& config,
72 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080073 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080074 : fft_(),
75 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080076 optimization_(optimization),
Per Åhgrena98c8072018-01-15 19:17:16 +010077 config_(config),
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020078 adaptation_during_saturation_(EnableAdaptationDuringSaturation()),
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020079 enable_misadjustment_estimator_(EnableMisadjustmentEstimator()),
Per Åhgrena98c8072018-01-15 19:17:16 +010080 main_filter_(config_.filter.main.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010081 config_.filter.main_initial.length_blocks,
82 config.filter.config_change_duration_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +010083 optimization,
84 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +010085 shadow_filter_(config_.filter.shadow.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +010086 config_.filter.shadow_initial.length_blocks,
87 config.filter.config_change_duration_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +010088 optimization,
89 data_dumper_),
Per Åhgren5f1a31c2018-03-08 15:54:41 +010090 G_main_(config_.filter.main_initial,
91 config_.filter.config_change_duration_blocks),
92 G_shadow_(config_.filter.shadow_initial,
93 config.filter.config_change_duration_blocks) {
peah522d71b2017-02-23 05:16:26 -080094 RTC_DCHECK(data_dumper_);
Per Åhgren08ea5892018-01-15 08:07:41 +010095 // Currently, the rest of AEC3 requires the main and shadow filter lengths to
96 // be identical.
Per Åhgrena98c8072018-01-15 19:17:16 +010097 RTC_DCHECK_EQ(config_.filter.main.length_blocks,
98 config_.filter.shadow.length_blocks);
99 RTC_DCHECK_EQ(config_.filter.main_initial.length_blocks,
100 config_.filter.shadow_initial.length_blocks);
peah522d71b2017-02-23 05:16:26 -0800101}
102
peah29103572017-07-11 02:54:02 -0700103Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -0800104
105void Subtractor::HandleEchoPathChange(
106 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100107 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -0800108 main_filter_.HandleEchoPathChange();
109 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +0100110 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -0700111 G_shadow_.HandleEchoPathChange();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100112 G_main_.SetConfig(config_.filter.main_initial, true);
113 G_shadow_.SetConfig(config_.filter.shadow_initial, true);
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100114 main_filter_converged_ = false;
115 shadow_filter_converged_ = false;
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100116 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks,
117 true);
Per Åhgren5c532d32018-03-22 00:29:25 +0100118 main_filter_once_converged_ = false;
Per Åhgrena98c8072018-01-15 19:17:16 +0100119 shadow_filter_.SetSizePartitions(
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100120 config_.filter.shadow_initial.length_blocks, true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100121 };
122
123 // TODO(peah): Add delay-change specific reset behavior.
124 if ((echo_path_variability.delay_change ==
125 EchoPathVariability::DelayAdjustment::kBufferFlush) ||
126 (echo_path_variability.delay_change ==
127 EchoPathVariability::DelayAdjustment::kDelayReset)) {
128 full_reset();
129 } else if (echo_path_variability.delay_change ==
130 EchoPathVariability::DelayAdjustment::kNewDetectedDelay) {
131 full_reset();
132 } else if (echo_path_variability.delay_change ==
133 EchoPathVariability::DelayAdjustment::kBufferReadjustment) {
134 full_reset();
peah522d71b2017-02-23 05:16:26 -0800135 }
136}
137
Per Åhgrena98c8072018-01-15 19:17:16 +0100138void Subtractor::ExitInitialState() {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100139 G_main_.SetConfig(config_.filter.main, false);
140 G_shadow_.SetConfig(config_.filter.shadow, false);
141 main_filter_.SetSizePartitions(config_.filter.main.length_blocks, false);
142 shadow_filter_.SetSizePartitions(config_.filter.shadow.length_blocks, false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100143}
144
peahcf02cf12017-04-05 14:18:07 -0700145void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800146 const rtc::ArrayView<const float> capture,
147 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700148 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800149 SubtractorOutput* output) {
150 RTC_DCHECK_EQ(kBlockSize, capture.size());
151 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800152 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -0700153 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800154 std::array<float, kBlockSize>& e_main = output->e_main;
155 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
156
157 FftData S;
158 FftData& G = S;
159
peah86afe9d2017-04-06 15:45:32 -0700160 // Form the output of the main filter.
161 main_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100162 bool main_saturation = false;
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200163 PredictionError(fft_, S, y, &e_main, &output->s_main,
164 adaptation_during_saturation_, &main_saturation);
Per Åhgrend20639f2018-01-11 10:29:49 +0100165 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kHanning, &E_main);
peah522d71b2017-02-23 05:16:26 -0800166
peah86afe9d2017-04-06 15:45:32 -0700167 // Form the output of the shadow filter.
168 shadow_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100169 bool shadow_saturation = false;
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200170 PredictionError(fft_, S, y, &e_shadow, nullptr, adaptation_during_saturation_,
171 &shadow_saturation);
Per Åhgrend20639f2018-01-11 10:29:49 +0100172 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kHanning, &E_shadow);
peah522d71b2017-02-23 05:16:26 -0800173
Per Åhgren5c532d32018-03-22 00:29:25 +0100174 // Check for filter convergence.
175 const auto sum_of_squares = [](float a, float b) { return a + b * b; };
176 const float y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
177 const float e2_main =
178 std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
179 const float e2_shadow =
180 std::accumulate(e_shadow.begin(), e_shadow.end(), 0.f, sum_of_squares);
Per Åhgren63b494d2017-12-06 11:32:38 +0100181
Per Åhgren971bf032018-03-29 12:55:30 +0200182 constexpr float kConvergenceThreshold = 50 * 50 * kBlockSize;
Per Åhgrend18e87e2018-05-09 12:07:26 +0200183 main_filter_converged_ = e2_main < 0.5f * y2 && y2 > kConvergenceThreshold;
Per Åhgren5c532d32018-03-22 00:29:25 +0100184 shadow_filter_converged_ =
185 e2_shadow < 0.05 * y2 && y2 > kConvergenceThreshold;
186 main_filter_once_converged_ =
187 main_filter_once_converged_ || main_filter_converged_;
188 main_filter_diverged_ = e2_main > 1.5f * y2 && y2 > 30.f * 30.f * kBlockSize;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200189
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200190 if (enable_misadjustment_estimator_) {
191 filter_misadjustment_estimator_.Update(e2_main, y2);
192 if (filter_misadjustment_estimator_.IsAdjustmentNeeded()) {
193 float scale = filter_misadjustment_estimator_.GetMisadjustment();
194 main_filter_.ScaleFilter(scale);
195 output->ScaleOutputMainFilter(scale);
196 filter_misadjustment_estimator_.Reset();
197 }
198 }
peah522d71b2017-02-23 05:16:26 -0800199 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100200 E_shadow.Spectrum(optimization_, output->E2_shadow);
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100201 E_main.Spectrum(optimization_, output->E2_main);
202
peah522d71b2017-02-23 05:16:26 -0800203 // Update the main filter.
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100204 std::array<float, kFftLengthBy2Plus1> X2;
205 render_buffer.SpectralSum(main_filter_.SizePartitions(), &X2);
206 G_main_.Compute(X2, render_signal_analyzer, *output, main_filter_,
Per Åhgren9845a672018-01-15 13:09:02 +0100207 aec_state.SaturatedCapture() || main_saturation, &G);
peah86afe9d2017-04-06 15:45:32 -0700208 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800209 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
210 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
211
212 // Update the shadow filter.
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100213 if (shadow_filter_.SizePartitions() != main_filter_.SizePartitions()) {
214 render_buffer.SpectralSum(shadow_filter_.SizePartitions(), &X2);
215 }
216 G_shadow_.Compute(X2, render_signal_analyzer, E_shadow,
peah86afe9d2017-04-06 15:45:32 -0700217 shadow_filter_.SizePartitions(),
Per Åhgren9845a672018-01-15 13:09:02 +0100218 aec_state.SaturatedCapture() || shadow_saturation, &G);
peah86afe9d2017-04-06 15:45:32 -0700219 shadow_filter_.Adapt(render_buffer, G);
220
peah522d71b2017-02-23 05:16:26 -0800221 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
222 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200223 filter_misadjustment_estimator_.Dump(data_dumper_);
Per Åhgren5c532d32018-03-22 00:29:25 +0100224 DumpFilters();
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200225
226 if (adaptation_during_saturation_) {
227 std::for_each(e_main.begin(), e_main.end(),
228 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
229 }
peah522d71b2017-02-23 05:16:26 -0800230}
231
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200232void Subtractor::FilterMisadjustmentEstimator::Update(float e2, float y2) {
233 e2_acum_ += e2;
234 y2_acum_ += y2;
235 if (++n_blocks_acum_ == n_blocks_) {
236 if (y2_acum_ > n_blocks_ * 200.f * 200.f * kBlockSize) {
237 float update = (e2_acum_ / y2_acum_);
238 if (e2_acum_ > n_blocks_ * 7500.f * 7500.f * kBlockSize) {
239 overhang_ = 4; // Duration equal to blockSizeMs * n_blocks_ * 4
240 } else {
241 overhang_ = std::max(overhang_ - 1, 0);
242 }
243
244 if ((update < inv_misadjustment_) || (overhang_ > 0)) {
245 inv_misadjustment_ += 0.1f * (update - inv_misadjustment_);
246 }
247 }
248 e2_acum_ = 0.f;
249 y2_acum_ = 0.f;
250 n_blocks_acum_ = 0;
251 }
252}
253
254void Subtractor::FilterMisadjustmentEstimator::Reset() {
255 e2_acum_ = 0.f;
256 y2_acum_ = 0.f;
257 n_blocks_acum_ = 0;
258 inv_misadjustment_ = 0.f;
259 overhang_ = 0.f;
260}
261
262void Subtractor::FilterMisadjustmentEstimator::Dump(
263 ApmDataDumper* data_dumper) const {
264 data_dumper->DumpRaw("aec3_inv_misadjustment_factor", inv_misadjustment_);
265}
266
peah522d71b2017-02-23 05:16:26 -0800267} // namespace webrtc