blob: 609e8ac7ed1b927c72809b91421af7c263854b8d [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"
Per Åhgren88cf0502018-07-16 17:08:41 +020019#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010020#include "rtc_base/numerics/safe_minmax.h"
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020021#include "system_wrappers/include/field_trial.h"
peah522d71b2017-02-23 05:16:26 -080022
23namespace webrtc {
24
25namespace {
26
Per Åhgren88cf0502018-07-16 17:08:41 +020027bool EnableAgcGainChangeResponse() {
28 return !field_trial::IsEnabled("WebRTC-Aec3AgcGainChangeResponseKillSwitch");
29}
30
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020031bool EnableAdaptationDuringSaturation() {
32 return !field_trial::IsEnabled("WebRTC-Aec3RapidAgcGainRecoveryKillSwitch");
33}
34
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020035bool EnableMisadjustmentEstimator() {
36 return !field_trial::IsEnabled("WebRTC-Aec3MisadjustmentEstimatorKillSwitch");
37}
38
Per Åhgrene4db6a12018-07-26 15:32:24 +020039bool EnableShadowFilterJumpstart() {
40 return !field_trial::IsEnabled("WebRTC-Aec3ShadowFilterJumpstartKillSwitch");
41}
42
Per Åhgren22754392018-08-10 18:37:38 +020043bool EnableShadowFilterBoostedJumpstart() {
44 return !field_trial::IsEnabled(
45 "WebRTC-Aec3ShadowFilterBoostedJumpstartKillSwitch");
46}
47
48bool EnableEarlyShadowFilterJumpstart() {
49 return !field_trial::IsEnabled(
50 "WebRTC-Aec3EarlyShadowFilterJumpstartKillSwitch");
51}
52
peah86afe9d2017-04-06 15:45:32 -070053void PredictionError(const Aec3Fft& fft,
54 const FftData& S,
55 rtc::ArrayView<const float> y,
56 std::array<float, kBlockSize>* e,
Per Åhgren9845a672018-01-15 13:09:02 +010057 std::array<float, kBlockSize>* s,
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020058 bool adaptation_during_saturation,
Per Åhgren9845a672018-01-15 13:09:02 +010059 bool* saturation) {
Per Åhgren7634c162017-12-18 15:45:49 +010060 std::array<float, kFftLength> tmp;
61 fft.Ifft(S, &tmp);
peah522d71b2017-02-23 05:16:26 -080062 constexpr float kScale = 1.0f / kFftLengthBy2;
Per Åhgren7634c162017-12-18 15:45:49 +010063 std::transform(y.begin(), y.end(), tmp.begin() + kFftLengthBy2, e->begin(),
64 [&](float a, float b) { return a - b * kScale; });
peah29103572017-07-11 02:54:02 -070065
Per Åhgren9845a672018-01-15 13:09:02 +010066 *saturation = false;
67
peah29103572017-07-11 02:54:02 -070068 if (s) {
69 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010070 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070071 }
Per Åhgren9845a672018-01-15 13:09:02 +010072 auto result = std::minmax_element(s->begin(), s->end());
73 *saturation = *result.first <= -32768 || *result.first >= 32767;
74 }
75 if (!(*saturation)) {
76 auto result = std::minmax_element(e->begin(), e->end());
77 *saturation = *result.first <= -32768 || *result.first >= 32767;
peah29103572017-07-11 02:54:02 -070078 }
Per Åhgren7634c162017-12-18 15:45:49 +010079
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020080 if (!adaptation_during_saturation) {
81 std::for_each(e->begin(), e->end(),
82 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
83 } else {
84 *saturation = false;
85 }
peah522d71b2017-02-23 05:16:26 -080086}
Per Åhgrenec22e3f2017-12-20 15:20:37 +010087
Per Åhgren7f5175a2018-07-25 16:30:54 +020088void ScaleFilterOutput(rtc::ArrayView<const float> y,
89 float factor,
90 rtc::ArrayView<float> e,
91 rtc::ArrayView<float> s) {
92 RTC_DCHECK_EQ(y.size(), e.size());
93 RTC_DCHECK_EQ(y.size(), s.size());
94 for (size_t k = 0; k < y.size(); ++k) {
95 s[k] *= factor;
96 e[k] = y[k] - s[k];
97 }
98}
99
peah522d71b2017-02-23 05:16:26 -0800100} // namespace
101
Per Åhgren09a718a2017-12-11 22:28:45 +0100102Subtractor::Subtractor(const EchoCanceller3Config& config,
103 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -0800104 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -0800105 : fft_(),
106 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -0800107 optimization_(optimization),
Per Åhgrena98c8072018-01-15 19:17:16 +0100108 config_(config),
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200109 adaptation_during_saturation_(EnableAdaptationDuringSaturation()),
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200110 enable_misadjustment_estimator_(EnableMisadjustmentEstimator()),
Per Åhgren88cf0502018-07-16 17:08:41 +0200111 enable_agc_gain_change_response_(EnableAgcGainChangeResponse()),
Per Åhgrene4db6a12018-07-26 15:32:24 +0200112 enable_shadow_filter_jumpstart_(EnableShadowFilterJumpstart()),
Per Åhgren22754392018-08-10 18:37:38 +0200113 enable_shadow_filter_boosted_jumpstart_(
114 EnableShadowFilterBoostedJumpstart()),
115 enable_early_shadow_filter_jumpstart_(EnableEarlyShadowFilterJumpstart()),
Per Åhgrena98c8072018-01-15 19:17:16 +0100116 main_filter_(config_.filter.main.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100117 config_.filter.main_initial.length_blocks,
118 config.filter.config_change_duration_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +0100119 optimization,
120 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +0100121 shadow_filter_(config_.filter.shadow.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100122 config_.filter.shadow_initial.length_blocks,
123 config.filter.config_change_duration_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +0100124 optimization,
125 data_dumper_),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100126 G_main_(config_.filter.main_initial,
127 config_.filter.config_change_duration_blocks),
128 G_shadow_(config_.filter.shadow_initial,
129 config.filter.config_change_duration_blocks) {
peah522d71b2017-02-23 05:16:26 -0800130 RTC_DCHECK(data_dumper_);
131}
132
peah29103572017-07-11 02:54:02 -0700133Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -0800134
135void Subtractor::HandleEchoPathChange(
136 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100137 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -0800138 main_filter_.HandleEchoPathChange();
139 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +0100140 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -0700141 G_shadow_.HandleEchoPathChange();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100142 G_main_.SetConfig(config_.filter.main_initial, true);
143 G_shadow_.SetConfig(config_.filter.shadow_initial, true);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100144 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks,
145 true);
Per Åhgrena98c8072018-01-15 19:17:16 +0100146 shadow_filter_.SetSizePartitions(
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100147 config_.filter.shadow_initial.length_blocks, true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100148 };
149
Per Åhgren88cf0502018-07-16 17:08:41 +0200150 if (echo_path_variability.delay_change !=
151 EchoPathVariability::DelayAdjustment::kNone) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100152 full_reset();
Per Åhgren88cf0502018-07-16 17:08:41 +0200153 }
154
155 if (echo_path_variability.gain_change && enable_agc_gain_change_response_) {
Per Åhgren88cf0502018-07-16 17:08:41 +0200156 G_main_.HandleEchoPathChange(echo_path_variability);
peah522d71b2017-02-23 05:16:26 -0800157 }
158}
159
Per Åhgrena98c8072018-01-15 19:17:16 +0100160void Subtractor::ExitInitialState() {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100161 G_main_.SetConfig(config_.filter.main, false);
162 G_shadow_.SetConfig(config_.filter.shadow, false);
163 main_filter_.SetSizePartitions(config_.filter.main.length_blocks, false);
164 shadow_filter_.SetSizePartitions(config_.filter.shadow.length_blocks, false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100165}
166
peahcf02cf12017-04-05 14:18:07 -0700167void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800168 const rtc::ArrayView<const float> capture,
169 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700170 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800171 SubtractorOutput* output) {
172 RTC_DCHECK_EQ(kBlockSize, capture.size());
173 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800174 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -0700175 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800176 std::array<float, kBlockSize>& e_main = output->e_main;
177 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
178
179 FftData S;
180 FftData& G = S;
181
Per Åhgren7f5175a2018-07-25 16:30:54 +0200182 // Form the outputs of the main and shadow filters.
peah86afe9d2017-04-06 15:45:32 -0700183 main_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100184 bool main_saturation = false;
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200185 PredictionError(fft_, S, y, &e_main, &output->s_main,
186 adaptation_during_saturation_, &main_saturation);
peah522d71b2017-02-23 05:16:26 -0800187
peah86afe9d2017-04-06 15:45:32 -0700188 shadow_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100189 bool shadow_saturation = false;
Per Åhgren78026752018-08-01 16:24:08 +0200190 PredictionError(fft_, S, y, &e_shadow, &output->s_shadow,
191 adaptation_during_saturation_, &shadow_saturation);
peah522d71b2017-02-23 05:16:26 -0800192
Per Åhgrene4db6a12018-07-26 15:32:24 +0200193 // Compute the signal powers in the subtractor output.
194 output->UpdatePowers(y);
195
Per Åhgren7f5175a2018-07-25 16:30:54 +0200196 // Adjust the filter if needed.
197 bool main_filter_adjusted = false;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200198 if (enable_misadjustment_estimator_) {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200199 filter_misadjustment_estimator_.Update(*output);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200200 if (filter_misadjustment_estimator_.IsAdjustmentNeeded()) {
201 float scale = filter_misadjustment_estimator_.GetMisadjustment();
202 main_filter_.ScaleFilter(scale);
Per Åhgren7f5175a2018-07-25 16:30:54 +0200203 ScaleFilterOutput(y, scale, e_main, output->s_main);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200204 filter_misadjustment_estimator_.Reset();
Per Åhgren7f5175a2018-07-25 16:30:54 +0200205 main_filter_adjusted = true;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200206 }
207 }
Per Åhgren7f5175a2018-07-25 16:30:54 +0200208
209 // Compute the FFts of the main and shadow filter outputs.
210 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kHanning, &E_main);
211 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kHanning, &E_shadow);
212
peah522d71b2017-02-23 05:16:26 -0800213 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100214 E_shadow.Spectrum(optimization_, output->E2_shadow);
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100215 E_main.Spectrum(optimization_, output->E2_main);
216
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200217 // Compute the render powers.
218 std::array<float, kFftLengthBy2Plus1> X2_main;
219 std::array<float, kFftLengthBy2Plus1> X2_shadow_data;
220 std::array<float, kFftLengthBy2Plus1>& X2_shadow =
221 main_filter_.SizePartitions() == shadow_filter_.SizePartitions()
222 ? X2_main
223 : X2_shadow_data;
224 if (main_filter_.SizePartitions() == shadow_filter_.SizePartitions()) {
225 render_buffer.SpectralSum(main_filter_.SizePartitions(), &X2_main);
226 } else if (main_filter_.SizePartitions() > shadow_filter_.SizePartitions()) {
227 render_buffer.SpectralSums(shadow_filter_.SizePartitions(),
228 main_filter_.SizePartitions(), &X2_shadow,
229 &X2_main);
230 } else {
231 render_buffer.SpectralSums(main_filter_.SizePartitions(),
232 shadow_filter_.SizePartitions(), &X2_main,
233 &X2_shadow);
234 }
235
peah522d71b2017-02-23 05:16:26 -0800236 // Update the main filter.
Per Åhgren7f5175a2018-07-25 16:30:54 +0200237 if (!main_filter_adjusted) {
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200238 G_main_.Compute(X2_main, render_signal_analyzer, *output, main_filter_,
Per Åhgren7f5175a2018-07-25 16:30:54 +0200239 aec_state.SaturatedCapture() || main_saturation, &G);
240 } else {
241 G.re.fill(0.f);
242 G.im.fill(0.f);
243 }
peah86afe9d2017-04-06 15:45:32 -0700244 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800245 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
246 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
247
248 // Update the shadow filter.
Per Åhgrene4db6a12018-07-26 15:32:24 +0200249 poor_shadow_filter_counter_ =
250 output->e2_main < output->e2_shadow ? poor_shadow_filter_counter_ + 1 : 0;
Per Åhgren22754392018-08-10 18:37:38 +0200251 if (((poor_shadow_filter_counter_ < 5 &&
252 enable_early_shadow_filter_jumpstart_) ||
253 (poor_shadow_filter_counter_ < 10 &&
254 !enable_early_shadow_filter_jumpstart_)) ||
255 !enable_shadow_filter_jumpstart_) {
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200256 G_shadow_.Compute(X2_shadow, render_signal_analyzer, E_shadow,
Per Åhgrene4db6a12018-07-26 15:32:24 +0200257 shadow_filter_.SizePartitions(),
258 aec_state.SaturatedCapture() || shadow_saturation, &G);
259 shadow_filter_.Adapt(render_buffer, G);
260 } else {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200261 poor_shadow_filter_counter_ = 0;
Per Åhgren22754392018-08-10 18:37:38 +0200262 if (enable_shadow_filter_boosted_jumpstart_) {
263 shadow_filter_.SetFilter(main_filter_.GetFilter());
Per Åhgrenee8ad5f2018-08-10 21:15:48 +0200264 G_shadow_.Compute(X2_shadow, render_signal_analyzer, E_main,
Per Åhgren22754392018-08-10 18:37:38 +0200265 shadow_filter_.SizePartitions(),
266 aec_state.SaturatedCapture() || main_saturation, &G);
267 shadow_filter_.Adapt(render_buffer, G);
268 } else {
269 G.re.fill(0.f);
270 G.im.fill(0.f);
271 shadow_filter_.Adapt(render_buffer, G);
272 shadow_filter_.SetFilter(main_filter_.GetFilter());
273 }
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100274 }
peah86afe9d2017-04-06 15:45:32 -0700275
peah522d71b2017-02-23 05:16:26 -0800276 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
277 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200278 filter_misadjustment_estimator_.Dump(data_dumper_);
Per Åhgren5c532d32018-03-22 00:29:25 +0100279 DumpFilters();
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200280
281 if (adaptation_during_saturation_) {
282 std::for_each(e_main.begin(), e_main.end(),
283 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
284 }
Per Åhgren71ebf992018-07-16 14:46:11 +0200285
286 data_dumper_->DumpWav("aec3_main_filter_output", kBlockSize, &e_main[0],
287 16000, 1);
288 data_dumper_->DumpWav("aec3_shadow_filter_output", kBlockSize, &e_shadow[0],
289 16000, 1);
peah522d71b2017-02-23 05:16:26 -0800290}
291
Per Åhgrenb20b9372018-07-13 00:22:54 +0200292void Subtractor::FilterMisadjustmentEstimator::Update(
Per Åhgrene4db6a12018-07-26 15:32:24 +0200293 const SubtractorOutput& output) {
294 e2_acum_ += output.e2_main;
295 y2_acum_ += output.y2;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200296 if (++n_blocks_acum_ == n_blocks_) {
297 if (y2_acum_ > n_blocks_ * 200.f * 200.f * kBlockSize) {
298 float update = (e2_acum_ / y2_acum_);
299 if (e2_acum_ > n_blocks_ * 7500.f * 7500.f * kBlockSize) {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200300 // Duration equal to blockSizeMs * n_blocks_ * 4.
301 overhang_ = 4;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200302 } else {
303 overhang_ = std::max(overhang_ - 1, 0);
304 }
305
306 if ((update < inv_misadjustment_) || (overhang_ > 0)) {
307 inv_misadjustment_ += 0.1f * (update - inv_misadjustment_);
308 }
309 }
310 e2_acum_ = 0.f;
311 y2_acum_ = 0.f;
312 n_blocks_acum_ = 0;
313 }
314}
315
316void Subtractor::FilterMisadjustmentEstimator::Reset() {
317 e2_acum_ = 0.f;
318 y2_acum_ = 0.f;
319 n_blocks_acum_ = 0;
320 inv_misadjustment_ = 0.f;
321 overhang_ = 0.f;
322}
323
324void Subtractor::FilterMisadjustmentEstimator::Dump(
325 ApmDataDumper* data_dumper) const {
326 data_dumper->DumpRaw("aec3_inv_misadjustment_factor", inv_misadjustment_);
327}
328
peah522d71b2017-02-23 05:16:26 -0800329} // namespace webrtc