blob: 41711547c34aef61fef47c0bd8d8d47b40ed602a [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
peah86afe9d2017-04-06 15:45:32 -070043void PredictionError(const Aec3Fft& fft,
44 const FftData& S,
45 rtc::ArrayView<const float> y,
46 std::array<float, kBlockSize>* e,
Per Åhgren9845a672018-01-15 13:09:02 +010047 std::array<float, kBlockSize>* s,
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020048 bool adaptation_during_saturation,
Per Åhgren9845a672018-01-15 13:09:02 +010049 bool* saturation) {
Per Åhgren7634c162017-12-18 15:45:49 +010050 std::array<float, kFftLength> tmp;
51 fft.Ifft(S, &tmp);
peah522d71b2017-02-23 05:16:26 -080052 constexpr float kScale = 1.0f / kFftLengthBy2;
Per Åhgren7634c162017-12-18 15:45:49 +010053 std::transform(y.begin(), y.end(), tmp.begin() + kFftLengthBy2, e->begin(),
54 [&](float a, float b) { return a - b * kScale; });
peah29103572017-07-11 02:54:02 -070055
Per Åhgren9845a672018-01-15 13:09:02 +010056 *saturation = false;
57
peah29103572017-07-11 02:54:02 -070058 if (s) {
59 for (size_t k = 0; k < s->size(); ++k) {
Per Åhgren7634c162017-12-18 15:45:49 +010060 (*s)[k] = kScale * tmp[k + kFftLengthBy2];
peah29103572017-07-11 02:54:02 -070061 }
Per Åhgren9845a672018-01-15 13:09:02 +010062 auto result = std::minmax_element(s->begin(), s->end());
63 *saturation = *result.first <= -32768 || *result.first >= 32767;
64 }
65 if (!(*saturation)) {
66 auto result = std::minmax_element(e->begin(), e->end());
67 *saturation = *result.first <= -32768 || *result.first >= 32767;
peah29103572017-07-11 02:54:02 -070068 }
Per Åhgren7634c162017-12-18 15:45:49 +010069
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020070 if (!adaptation_during_saturation) {
71 std::for_each(e->begin(), e->end(),
72 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
73 } else {
74 *saturation = false;
75 }
peah522d71b2017-02-23 05:16:26 -080076}
Per Åhgrenec22e3f2017-12-20 15:20:37 +010077
Per Åhgren7f5175a2018-07-25 16:30:54 +020078void ScaleFilterOutput(rtc::ArrayView<const float> y,
79 float factor,
80 rtc::ArrayView<float> e,
81 rtc::ArrayView<float> s) {
82 RTC_DCHECK_EQ(y.size(), e.size());
83 RTC_DCHECK_EQ(y.size(), s.size());
84 for (size_t k = 0; k < y.size(); ++k) {
85 s[k] *= factor;
86 e[k] = y[k] - s[k];
87 }
88}
89
peah522d71b2017-02-23 05:16:26 -080090} // namespace
91
Per Åhgren09a718a2017-12-11 22:28:45 +010092Subtractor::Subtractor(const EchoCanceller3Config& config,
93 ApmDataDumper* data_dumper,
peah522d71b2017-02-23 05:16:26 -080094 Aec3Optimization optimization)
aleloi88b82b52017-02-23 06:27:03 -080095 : fft_(),
96 data_dumper_(data_dumper),
peah522d71b2017-02-23 05:16:26 -080097 optimization_(optimization),
Per Åhgrena98c8072018-01-15 19:17:16 +010098 config_(config),
Per Åhgrenfc63c9e2018-06-28 13:23:23 +020099 adaptation_during_saturation_(EnableAdaptationDuringSaturation()),
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200100 enable_misadjustment_estimator_(EnableMisadjustmentEstimator()),
Per Åhgren88cf0502018-07-16 17:08:41 +0200101 enable_agc_gain_change_response_(EnableAgcGainChangeResponse()),
Per Åhgrene4db6a12018-07-26 15:32:24 +0200102 enable_shadow_filter_jumpstart_(EnableShadowFilterJumpstart()),
Per Åhgrena98c8072018-01-15 19:17:16 +0100103 main_filter_(config_.filter.main.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100104 config_.filter.main_initial.length_blocks,
105 config.filter.config_change_duration_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +0100106 optimization,
107 data_dumper_),
Per Åhgrena98c8072018-01-15 19:17:16 +0100108 shadow_filter_(config_.filter.shadow.length_blocks,
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100109 config_.filter.shadow_initial.length_blocks,
110 config.filter.config_change_duration_blocks,
Per Åhgren08ea5892018-01-15 08:07:41 +0100111 optimization,
112 data_dumper_),
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100113 G_main_(config_.filter.main_initial,
114 config_.filter.config_change_duration_blocks),
115 G_shadow_(config_.filter.shadow_initial,
116 config.filter.config_change_duration_blocks) {
peah522d71b2017-02-23 05:16:26 -0800117 RTC_DCHECK(data_dumper_);
Per Åhgren08ea5892018-01-15 08:07:41 +0100118 // Currently, the rest of AEC3 requires the main and shadow filter lengths to
119 // be identical.
Per Åhgrena98c8072018-01-15 19:17:16 +0100120 RTC_DCHECK_EQ(config_.filter.main.length_blocks,
121 config_.filter.shadow.length_blocks);
122 RTC_DCHECK_EQ(config_.filter.main_initial.length_blocks,
123 config_.filter.shadow_initial.length_blocks);
peah522d71b2017-02-23 05:16:26 -0800124}
125
peah29103572017-07-11 02:54:02 -0700126Subtractor::~Subtractor() = default;
peah522d71b2017-02-23 05:16:26 -0800127
128void Subtractor::HandleEchoPathChange(
129 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100130 const auto full_reset = [&]() {
peah522d71b2017-02-23 05:16:26 -0800131 main_filter_.HandleEchoPathChange();
132 shadow_filter_.HandleEchoPathChange();
Per Åhgren8ba58612017-12-01 23:01:44 +0100133 G_main_.HandleEchoPathChange(echo_path_variability);
peahdebaa442017-05-03 05:39:09 -0700134 G_shadow_.HandleEchoPathChange();
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100135 G_main_.SetConfig(config_.filter.main_initial, true);
136 G_shadow_.SetConfig(config_.filter.shadow_initial, true);
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100137 main_filter_.SetSizePartitions(config_.filter.main_initial.length_blocks,
138 true);
Per Åhgrena98c8072018-01-15 19:17:16 +0100139 shadow_filter_.SetSizePartitions(
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100140 config_.filter.shadow_initial.length_blocks, true);
Per Åhgren8ba58612017-12-01 23:01:44 +0100141 };
142
Per Åhgren88cf0502018-07-16 17:08:41 +0200143 if (echo_path_variability.delay_change !=
144 EchoPathVariability::DelayAdjustment::kNone) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100145 full_reset();
Per Åhgren88cf0502018-07-16 17:08:41 +0200146 }
147
148 if (echo_path_variability.gain_change && enable_agc_gain_change_response_) {
149 RTC_LOG(LS_WARNING) << "Resetting main filter adaptation speed due to "
150 "microphone gain change";
151 G_main_.HandleEchoPathChange(echo_path_variability);
peah522d71b2017-02-23 05:16:26 -0800152 }
153}
154
Per Åhgrena98c8072018-01-15 19:17:16 +0100155void Subtractor::ExitInitialState() {
Per Åhgren5f1a31c2018-03-08 15:54:41 +0100156 G_main_.SetConfig(config_.filter.main, false);
157 G_shadow_.SetConfig(config_.filter.shadow, false);
158 main_filter_.SetSizePartitions(config_.filter.main.length_blocks, false);
159 shadow_filter_.SetSizePartitions(config_.filter.shadow.length_blocks, false);
Per Åhgrena98c8072018-01-15 19:17:16 +0100160}
161
peahcf02cf12017-04-05 14:18:07 -0700162void Subtractor::Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800163 const rtc::ArrayView<const float> capture,
164 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -0700165 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -0800166 SubtractorOutput* output) {
167 RTC_DCHECK_EQ(kBlockSize, capture.size());
168 rtc::ArrayView<const float> y = capture;
peah522d71b2017-02-23 05:16:26 -0800169 FftData& E_main = output->E_main;
peah86afe9d2017-04-06 15:45:32 -0700170 FftData E_shadow;
peah522d71b2017-02-23 05:16:26 -0800171 std::array<float, kBlockSize>& e_main = output->e_main;
172 std::array<float, kBlockSize>& e_shadow = output->e_shadow;
173
174 FftData S;
175 FftData& G = S;
176
Per Åhgren7f5175a2018-07-25 16:30:54 +0200177 // Form the outputs of the main and shadow filters.
peah86afe9d2017-04-06 15:45:32 -0700178 main_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100179 bool main_saturation = false;
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200180 PredictionError(fft_, S, y, &e_main, &output->s_main,
181 adaptation_during_saturation_, &main_saturation);
peah522d71b2017-02-23 05:16:26 -0800182
peah86afe9d2017-04-06 15:45:32 -0700183 shadow_filter_.Filter(render_buffer, &S);
Per Åhgren9845a672018-01-15 13:09:02 +0100184 bool shadow_saturation = false;
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200185 PredictionError(fft_, S, y, &e_shadow, nullptr, adaptation_during_saturation_,
186 &shadow_saturation);
peah522d71b2017-02-23 05:16:26 -0800187
Per Åhgrene4db6a12018-07-26 15:32:24 +0200188 // Compute the signal powers in the subtractor output.
189 output->UpdatePowers(y);
190
Per Åhgren7f5175a2018-07-25 16:30:54 +0200191 // Adjust the filter if needed.
192 bool main_filter_adjusted = false;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200193 if (enable_misadjustment_estimator_) {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200194 filter_misadjustment_estimator_.Update(*output);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200195 if (filter_misadjustment_estimator_.IsAdjustmentNeeded()) {
196 float scale = filter_misadjustment_estimator_.GetMisadjustment();
197 main_filter_.ScaleFilter(scale);
Per Åhgren7f5175a2018-07-25 16:30:54 +0200198 ScaleFilterOutput(y, scale, e_main, output->s_main);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200199 filter_misadjustment_estimator_.Reset();
Per Åhgren7f5175a2018-07-25 16:30:54 +0200200 main_filter_adjusted = true;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200201 }
202 }
Per Åhgren7f5175a2018-07-25 16:30:54 +0200203
204 // Compute the FFts of the main and shadow filter outputs.
205 fft_.ZeroPaddedFft(e_main, Aec3Fft::Window::kHanning, &E_main);
206 fft_.ZeroPaddedFft(e_shadow, Aec3Fft::Window::kHanning, &E_shadow);
207
peah522d71b2017-02-23 05:16:26 -0800208 // Compute spectra for future use.
Per Åhgren8ba58612017-12-01 23:01:44 +0100209 E_shadow.Spectrum(optimization_, output->E2_shadow);
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100210 E_main.Spectrum(optimization_, output->E2_main);
211
peah522d71b2017-02-23 05:16:26 -0800212 // Update the main filter.
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100213 std::array<float, kFftLengthBy2Plus1> X2;
214 render_buffer.SpectralSum(main_filter_.SizePartitions(), &X2);
Per Åhgren7f5175a2018-07-25 16:30:54 +0200215 if (!main_filter_adjusted) {
216 G_main_.Compute(X2, render_signal_analyzer, *output, main_filter_,
217 aec_state.SaturatedCapture() || main_saturation, &G);
218 } else {
219 G.re.fill(0.f);
220 G.im.fill(0.f);
221 }
peah86afe9d2017-04-06 15:45:32 -0700222 main_filter_.Adapt(render_buffer, G);
peah522d71b2017-02-23 05:16:26 -0800223 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re);
224 data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im);
225
226 // Update the shadow filter.
Per Åhgrene4db6a12018-07-26 15:32:24 +0200227 poor_shadow_filter_counter_ =
228 output->e2_main < output->e2_shadow ? poor_shadow_filter_counter_ + 1 : 0;
229 if (poor_shadow_filter_counter_ < 10 || !enable_shadow_filter_jumpstart_) {
230 if (shadow_filter_.SizePartitions() != main_filter_.SizePartitions()) {
231 render_buffer.SpectralSum(shadow_filter_.SizePartitions(), &X2);
232 }
233 G_shadow_.Compute(X2, render_signal_analyzer, E_shadow,
234 shadow_filter_.SizePartitions(),
235 aec_state.SaturatedCapture() || shadow_saturation, &G);
236 shadow_filter_.Adapt(render_buffer, G);
237 } else {
238 G.re.fill(0.f);
239 G.im.fill(0.f);
240 poor_shadow_filter_counter_ = 0;
241 shadow_filter_.SetFilter(main_filter_.GetFilter());
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100242 }
peah86afe9d2017-04-06 15:45:32 -0700243
peah522d71b2017-02-23 05:16:26 -0800244 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re);
245 data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200246 filter_misadjustment_estimator_.Dump(data_dumper_);
Per Åhgren5c532d32018-03-22 00:29:25 +0100247 DumpFilters();
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200248
249 if (adaptation_during_saturation_) {
250 std::for_each(e_main.begin(), e_main.end(),
251 [](float& a) { a = rtc::SafeClamp(a, -32768.f, 32767.f); });
252 }
Per Åhgren71ebf992018-07-16 14:46:11 +0200253
254 data_dumper_->DumpWav("aec3_main_filter_output", kBlockSize, &e_main[0],
255 16000, 1);
256 data_dumper_->DumpWav("aec3_shadow_filter_output", kBlockSize, &e_shadow[0],
257 16000, 1);
peah522d71b2017-02-23 05:16:26 -0800258}
259
Per Åhgrenb20b9372018-07-13 00:22:54 +0200260void Subtractor::FilterMisadjustmentEstimator::Update(
Per Åhgrene4db6a12018-07-26 15:32:24 +0200261 const SubtractorOutput& output) {
262 e2_acum_ += output.e2_main;
263 y2_acum_ += output.y2;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200264 if (++n_blocks_acum_ == n_blocks_) {
265 if (y2_acum_ > n_blocks_ * 200.f * 200.f * kBlockSize) {
266 float update = (e2_acum_ / y2_acum_);
267 if (e2_acum_ > n_blocks_ * 7500.f * 7500.f * kBlockSize) {
Per Åhgrene4db6a12018-07-26 15:32:24 +0200268 // Duration equal to blockSizeMs * n_blocks_ * 4.
269 overhang_ = 4;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200270 } else {
271 overhang_ = std::max(overhang_ - 1, 0);
272 }
273
274 if ((update < inv_misadjustment_) || (overhang_ > 0)) {
275 inv_misadjustment_ += 0.1f * (update - inv_misadjustment_);
276 }
277 }
278 e2_acum_ = 0.f;
279 y2_acum_ = 0.f;
280 n_blocks_acum_ = 0;
281 }
282}
283
284void Subtractor::FilterMisadjustmentEstimator::Reset() {
285 e2_acum_ = 0.f;
286 y2_acum_ = 0.f;
287 n_blocks_acum_ = 0;
288 inv_misadjustment_ = 0.f;
289 overhang_ = 0.f;
290}
291
292void Subtractor::FilterMisadjustmentEstimator::Dump(
293 ApmDataDumper* data_dumper) const {
294 data_dumper->DumpRaw("aec3_inv_misadjustment_factor", inv_misadjustment_);
295}
296
peah522d71b2017-02-23 05:16:26 -0800297} // namespace webrtc