blob: 803a598e950442eefb19a0b0adcc5840b0c4cb0e [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/aec_state.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <math.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <algorithm>
peah522d71b2017-02-23 05:16:26 -080016#include <numeric>
17#include <vector>
18
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/array_view.h"
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020021#include "modules/audio_processing/aec3/aec3_common.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
peah522d71b2017-02-23 05:16:26 -080025
26namespace webrtc {
27namespace {
28
Per Åhgren5c532d32018-03-22 00:29:25 +010029constexpr size_t kBlocksSinceConvergencedFilterInit = 10000;
30constexpr size_t kBlocksSinceConsistentEstimateInit = 10000;
31
peah522d71b2017-02-23 05:16:26 -080032} // namespace
33
34int AecState::instance_count_ = 0;
35
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020036void AecState::GetResidualEchoScaling(
37 rtc::ArrayView<float> residual_scaling) const {
38 bool filter_has_had_time_to_converge;
39 if (config_.filter.conservative_initial_phase) {
40 filter_has_had_time_to_converge =
41 strong_not_saturated_render_blocks_ >= 1.5f * kNumBlocksPerSecond;
42 } else {
43 filter_has_had_time_to_converge =
44 strong_not_saturated_render_blocks_ >= 0.8f * kNumBlocksPerSecond;
45 }
46 echo_audibility_.GetResidualEchoScaling(filter_has_had_time_to_converge,
47 residual_scaling);
48}
49
50absl::optional<float> AecState::ErleUncertainty() const {
Gustaf Ullberg68d6d442019-01-29 10:08:15 +010051 if (SaturatedEcho()) {
Per Åhgren3e7b7b12018-10-16 14:38:10 +020052 return 1.f;
53 }
54
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020055 return absl::nullopt;
56}
57
Sam Zackrisson8f736c02019-10-01 12:47:53 +020058AecState::AecState(const EchoCanceller3Config& config,
59 size_t num_capture_channels)
peah522d71b2017-02-23 05:16:26 -080060 : data_dumper_(
61 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
peah8cee56f2017-08-24 22:36:53 -070062 config_(config),
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020063 initial_state_(config_),
64 delay_state_(config_),
65 transparent_state_(config_),
66 filter_quality_state_(config_),
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020067 erl_estimator_(2 * kNumBlocksPerSecond),
Per Åhgrenb4161d32019-10-08 12:35:47 +020068 erle_estimator_(2 * kNumBlocksPerSecond, config_, num_capture_channels),
Sam Zackrisson46b01402019-10-08 16:17:48 +020069 max_echo_path_gain_(config_.ep_strength.default_gain),
70 filter_analyzers_(num_capture_channels),
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020071 echo_audibility_(
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020072 config_.echo_audibility.use_stationarity_properties_at_init),
Sam Zackrisson8f736c02019-10-01 12:47:53 +020073 reverb_model_estimator_(config_),
Sam Zackrisson46b01402019-10-08 16:17:48 +020074 subtractor_output_analyzers_(num_capture_channels) {
75 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
76 filter_analyzers_[ch] = std::make_unique<FilterAnalyzer>(config_);
77 }
78}
peah522d71b2017-02-23 05:16:26 -080079
80AecState::~AecState() = default;
81
peah86afe9d2017-04-06 15:45:32 -070082void AecState::HandleEchoPathChange(
83 const EchoPathVariability& echo_path_variability) {
Per Åhgren8ba58612017-12-01 23:01:44 +010084 const auto full_reset = [&]() {
Sam Zackrisson46b01402019-10-08 16:17:48 +020085 for (auto& filter_analyzer : filter_analyzers_) {
86 filter_analyzer->Reset();
87 }
88 max_echo_path_gain_ = config_.ep_strength.default_gain;
peah86afe9d2017-04-06 15:45:32 -070089 capture_signal_saturation_ = false;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020090 strong_not_saturated_render_blocks_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010091 blocks_with_active_render_ = 0;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020092 initial_state_.Reset();
93 transparent_state_.Reset();
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020094 erle_estimator_.Reset(true);
95 erl_estimator_.Reset();
Jesús de Vicente Peña70a59632019-04-16 12:32:15 +020096 filter_quality_state_.Reset();
Per Åhgren8ba58612017-12-01 23:01:44 +010097 };
peah6d822ad2017-04-10 13:52:14 -070098
Per Åhgren8ba58612017-12-01 23:01:44 +010099 // TODO(peah): Refine the reset scheme according to the type of gain and
100 // delay adjustment.
Per Åhgren8ba58612017-12-01 23:01:44 +0100101
102 if (echo_path_variability.delay_change !=
Per Åhgren88cf0502018-07-16 17:08:41 +0200103 EchoPathVariability::DelayAdjustment::kNone) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100104 full_reset();
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100105 } else if (echo_path_variability.gain_change) {
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200106 erle_estimator_.Reset(false);
Per Åhgrend2650d12018-10-02 17:00:59 +0200107 }
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200108 for (auto& analyzer : subtractor_output_analyzers_) {
109 analyzer.HandleEchoPathChange();
110 }
peah86afe9d2017-04-06 15:45:32 -0700111}
112
Per Åhgren09a718a2017-12-11 22:28:45 +0100113void AecState::Update(
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200114 const absl::optional<DelayEstimate>& external_delay,
Sam Zackrisson46b01402019-10-08 16:17:48 +0200115 rtc::ArrayView<const std::vector<std::array<float, kFftLengthBy2Plus1>>>
Per Åhgren09a718a2017-12-11 22:28:45 +0100116 adaptive_filter_frequency_response,
Sam Zackrisson46b01402019-10-08 16:17:48 +0200117 rtc::ArrayView<const std::vector<float>> adaptive_filter_impulse_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100118 const RenderBuffer& render_buffer,
119 const std::array<float, kFftLengthBy2Plus1>& E2_main,
120 const std::array<float, kFftLengthBy2Plus1>& Y2,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200121 rtc::ArrayView<const SubtractorOutput> subtractor_output) {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200122 const size_t num_capture_channels = filter_analyzers_.size();
123 RTC_DCHECK_EQ(num_capture_channels, subtractor_output.size());
124 RTC_DCHECK_EQ(num_capture_channels, subtractor_output_analyzers_.size());
125 RTC_DCHECK_EQ(num_capture_channels,
126 adaptive_filter_frequency_response.size());
127 RTC_DCHECK_EQ(num_capture_channels, adaptive_filter_impulse_response.size());
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200128
Sam Zackrisson46b01402019-10-08 16:17:48 +0200129 // Analyze the filter outputs and filters.
130 bool any_filter_converged = false;
131 bool all_filters_diverged = true;
132 bool any_filter_consistent = false;
133 max_echo_path_gain_ = 0.f;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200134 for (size_t ch = 0; ch < subtractor_output.size(); ++ch) {
135 subtractor_output_analyzers_[ch].Update(subtractor_output[ch]);
Sam Zackrisson46b01402019-10-08 16:17:48 +0200136 any_filter_converged = any_filter_converged ||
137 subtractor_output_analyzers_[ch].ConvergedFilter();
138 all_filters_diverged = all_filters_diverged &&
139 subtractor_output_analyzers_[ch].DivergedFilter();
Per Åhgrenb20b9372018-07-13 00:22:54 +0200140
Sam Zackrisson46b01402019-10-08 16:17:48 +0200141 filter_analyzers_[ch]->Update(adaptive_filter_impulse_response[ch],
142 render_buffer);
143 any_filter_consistent =
144 any_filter_consistent || filter_analyzers_[ch]->Consistent();
145 max_echo_path_gain_ =
146 std::max(max_echo_path_gain_, filter_analyzers_[ch]->Gain());
147 }
peah86afe9d2017-04-06 15:45:32 -0700148
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200149 // Estimate the direct path delay of the filter.
Gustaf Ullberg9466b662019-04-15 09:53:03 +0200150 if (config_.filter.use_linear_filter) {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200151 delay_state_.Update(filter_analyzers_, external_delay,
Gustaf Ullberg9466b662019-04-15 09:53:03 +0200152 strong_not_saturated_render_blocks_);
153 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100154
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200155 const std::vector<std::vector<float>>& aligned_render_block =
156 render_buffer.Block(-delay_state_.DirectPathFilterDelay())[0];
Per Åhgren5c532d32018-03-22 00:29:25 +0100157
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200158 // Update render counters.
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200159 bool active_render = false;
160 for (size_t ch = 0; ch < aligned_render_block.size(); ++ch) {
161 const float render_energy = std::inner_product(
162 aligned_render_block[ch].begin(), aligned_render_block[ch].end(),
163 aligned_render_block[ch].begin(), 0.f);
164 if (render_energy > (config_.render_levels.active_render_limit *
165 config_.render_levels.active_render_limit) *
166 kFftLengthBy2) {
167 active_render = true;
168 break;
169 }
170 }
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200171 blocks_with_active_render_ += active_render ? 1 : 0;
172 strong_not_saturated_render_blocks_ +=
173 active_render && !SaturatedCapture() ? 1 : 0;
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100174
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200175 std::array<float, kFftLengthBy2Plus1> X2_reverb;
Per Åhgreneac47f72019-08-08 09:08:52 +0200176 render_reverb_.Apply(render_buffer.GetSpectrumBuffer(),
177 delay_state_.DirectPathFilterDelay(), ReverbDecay(),
178 X2_reverb);
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200179
Jesús de Vicente Peña70a59632019-04-16 12:32:15 +0200180 if (config_.echo_audibility.use_stationarity_properties) {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200181 // Update the echo audibility evaluator.
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200182 echo_audibility_.Update(render_buffer,
183 render_reverb_.GetReverbContributionPowerSpectrum(),
184 delay_state_.DirectPathFilterDelay(),
185 delay_state_.ExternalDelayReported());
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200186 }
187
peah86afe9d2017-04-06 15:45:32 -0700188 // Update the ERL and ERLE measures.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200189 if (initial_state_.TransitionTriggered()) {
190 erle_estimator_.Reset(false);
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200191 }
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200192
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200193 // TODO(bugs.webrtc.org/10913): Take all channels into account.
Sam Zackrissona81c09d2019-09-05 09:35:10 +0200194 const auto& X2 = render_buffer.Spectrum(delay_state_.DirectPathFilterDelay(),
195 /*channel=*/0);
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100196 const auto& X2_input_erle = X2_reverb;
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200197
Sam Zackrisson46b01402019-10-08 16:17:48 +0200198 erle_estimator_.Update(render_buffer, adaptive_filter_frequency_response[0],
Jesús de Vicente Peña44974e12018-11-20 12:54:23 +0100199 X2_input_erle, Y2, E2_main,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200200 subtractor_output_analyzers_[0].ConvergedFilter(),
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200201 config_.erle.onset_detection);
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200202
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200203 erl_estimator_.Update(subtractor_output_analyzers_[0].ConvergedFilter(), X2,
204 Y2);
peah86afe9d2017-04-06 15:45:32 -0700205
Per Åhgren63b494d2017-12-06 11:32:38 +0100206 // Detect and flag echo saturation.
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100207 saturation_detector_.Update(aligned_render_block, SaturatedCapture(),
208 UsableLinearEstimate(), subtractor_output,
209 EchoPathGain());
peah86afe9d2017-04-06 15:45:32 -0700210
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200211 // Update the decision on whether to use the initial state parameter set.
212 initial_state_.Update(active_render, SaturatedCapture());
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100213
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200214 // Detect whether the transparent mode should be activated.
215 transparent_state_.Update(delay_state_.DirectPathFilterDelay(),
Sam Zackrisson46b01402019-10-08 16:17:48 +0200216 any_filter_consistent, any_filter_converged,
217 all_filters_diverged, active_render,
218 SaturatedCapture());
Per Åhgren5c532d32018-03-22 00:29:25 +0100219
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200220 // Analyze the quality of the filter.
Sam Zackrisson46b01402019-10-08 16:17:48 +0200221 filter_quality_state_.Update(active_render, TransparentMode(),
222 SaturatedCapture(), external_delay,
223 any_filter_converged);
Per Åhgrena98c8072018-01-15 19:17:16 +0100224
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200225 // Update the reverb estimate.
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200226 const bool stationary_block =
Jesús de Vicente Peña70a59632019-04-16 12:32:15 +0200227 config_.echo_audibility.use_stationarity_properties &&
Per Åhgrenf4801a12018-09-27 13:14:02 +0200228 echo_audibility_.IsBlockStationary();
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200229
Sam Zackrisson46b01402019-10-08 16:17:48 +0200230 reverb_model_estimator_.Update(filter_analyzers_[0]->GetAdjustedFilter(),
231 adaptive_filter_frequency_response[0],
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200232 erle_estimator_.GetInstLinearQualityEstimate(),
233 delay_state_.DirectPathFilterDelay(),
234 UsableLinearEstimate(), stationary_block);
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200235
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200236 erle_estimator_.Dump(data_dumper_);
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200237 reverb_model_estimator_.Dump(data_dumper_.get());
Per Åhgren5c532d32018-03-22 00:29:25 +0100238 data_dumper_->DumpRaw("aec3_erl", Erl());
Per Åhgren5c532d32018-03-22 00:29:25 +0100239 data_dumper_->DumpRaw("aec3_erl_time_domain", ErlTimeDomain());
Per Åhgrenb4161d32019-10-08 12:35:47 +0200240 data_dumper_->DumpRaw("aec3_erle", Erle()[0]);
Per Åhgren5c532d32018-03-22 00:29:25 +0100241 data_dumper_->DumpRaw("aec3_usable_linear_estimate", UsableLinearEstimate());
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200242 data_dumper_->DumpRaw("aec3_transparent_mode", TransparentMode());
Sam Zackrisson46b01402019-10-08 16:17:48 +0200243 data_dumper_->DumpRaw("aec3_filter_delay",
244 filter_analyzers_[0]->DelayBlocks());
Per Åhgren5c532d32018-03-22 00:29:25 +0100245
Sam Zackrisson46b01402019-10-08 16:17:48 +0200246 data_dumper_->DumpRaw("aec3_any_filter_consistent", any_filter_consistent);
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200247 data_dumper_->DumpRaw("aec3_initial_state",
248 initial_state_.InitialStateActive());
Per Åhgren5c532d32018-03-22 00:29:25 +0100249 data_dumper_->DumpRaw("aec3_capture_saturation", SaturatedCapture());
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200250 data_dumper_->DumpRaw("aec3_echo_saturation", SaturatedEcho());
Sam Zackrisson46b01402019-10-08 16:17:48 +0200251 data_dumper_->DumpRaw("aec3_any_filter_converged", any_filter_converged);
252 data_dumper_->DumpRaw("aec3_all_filters_diverged", all_filters_diverged);
Per Åhgren5c532d32018-03-22 00:29:25 +0100253
254 data_dumper_->DumpRaw("aec3_external_delay_avaliable",
255 external_delay ? 1 : 0);
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200256 data_dumper_->DumpRaw("aec3_filter_tail_freq_resp_est",
257 GetReverbFrequencyResponse());
peah29103572017-07-11 02:54:02 -0700258}
259
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200260AecState::InitialState::InitialState(const EchoCanceller3Config& config)
261 : conservative_initial_phase_(config.filter.conservative_initial_phase),
262 initial_state_seconds_(config.filter.initial_state_seconds) {
263 Reset();
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100264}
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200265void AecState::InitialState::InitialState::Reset() {
266 initial_state_ = true;
267 strong_not_saturated_render_blocks_ = 0;
268}
269void AecState::InitialState::InitialState::Update(bool active_render,
270 bool saturated_capture) {
271 strong_not_saturated_render_blocks_ +=
272 active_render && !saturated_capture ? 1 : 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100273
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200274 // Flag whether the initial state is still active.
275 bool prev_initial_state = initial_state_;
276 if (conservative_initial_phase_) {
277 initial_state_ =
278 strong_not_saturated_render_blocks_ < 5 * kNumBlocksPerSecond;
Per Åhgren31122d62018-04-10 16:33:55 +0200279 } else {
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200280 initial_state_ = strong_not_saturated_render_blocks_ <
281 initial_state_seconds_ * kNumBlocksPerSecond;
Per Åhgren31122d62018-04-10 16:33:55 +0200282 }
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100283
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200284 // Flag whether the transition from the initial state has started.
285 transition_triggered_ = !initial_state_ && prev_initial_state;
286}
287
288AecState::FilterDelay::FilterDelay(const EchoCanceller3Config& config)
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +0100289 : delay_headroom_samples_(config.delay.delay_headroom_samples) {}
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200290
291void AecState::FilterDelay::Update(
Sam Zackrisson46b01402019-10-08 16:17:48 +0200292 const std::vector<std::unique_ptr<FilterAnalyzer>>& filter_analyzers,
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200293 const absl::optional<DelayEstimate>& external_delay,
294 size_t blocks_with_proper_filter_adaptation) {
295 // Update the delay based on the external delay.
296 if (external_delay &&
297 (!external_delay_ || external_delay_->delay != external_delay->delay)) {
298 external_delay_ = external_delay;
299 external_delay_reported_ = true;
300 }
301
302 // Override the estimated delay if it is not certain that the filter has had
303 // time to converge.
304 const bool delay_estimator_may_not_have_converged =
305 blocks_with_proper_filter_adaptation < 2 * kNumBlocksPerSecond;
306 if (delay_estimator_may_not_have_converged && external_delay_) {
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +0100307 filter_delay_blocks_ = delay_headroom_samples_ / kBlockSize;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200308 } else {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200309 // Conservatively use the min delay among the filters.
310 filter_delay_blocks_ = filter_analyzers[0]->DelayBlocks();
311 for (size_t ch = 1; ch < filter_analyzers.size(); ++ch) {
312 filter_delay_blocks_ =
313 std::min(filter_delay_blocks_, filter_analyzers[ch]->DelayBlocks());
314 }
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200315 }
316}
317
318AecState::TransparentMode::TransparentMode(const EchoCanceller3Config& config)
319 : bounded_erl_(config.ep_strength.bounded_erl),
320 linear_and_stable_echo_path_(
321 config.echo_removal_control.linear_and_stable_echo_path),
322 active_blocks_since_sane_filter_(kBlocksSinceConsistentEstimateInit),
323 non_converged_sequence_size_(kBlocksSinceConvergencedFilterInit) {}
324
325void AecState::TransparentMode::Reset() {
326 non_converged_sequence_size_ = kBlocksSinceConvergencedFilterInit;
327 diverged_sequence_size_ = 0;
328 strong_not_saturated_render_blocks_ = 0;
329 if (linear_and_stable_echo_path_) {
330 recent_convergence_during_activity_ = false;
331 }
332}
333
334void AecState::TransparentMode::Update(int filter_delay_blocks,
Sam Zackrisson46b01402019-10-08 16:17:48 +0200335 bool any_filter_consistent,
336 bool any_filter_converged,
337 bool all_filters_diverged,
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200338 bool active_render,
339 bool saturated_capture) {
340 ++capture_block_counter_;
341 strong_not_saturated_render_blocks_ +=
342 active_render && !saturated_capture ? 1 : 0;
343
Sam Zackrisson46b01402019-10-08 16:17:48 +0200344 if (any_filter_consistent && filter_delay_blocks < 5) {
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200345 sane_filter_observed_ = true;
346 active_blocks_since_sane_filter_ = 0;
347 } else if (active_render) {
348 ++active_blocks_since_sane_filter_;
349 }
350
351 bool sane_filter_recently_seen;
352 if (!sane_filter_observed_) {
353 sane_filter_recently_seen =
354 capture_block_counter_ <= 5 * kNumBlocksPerSecond;
355 } else {
356 sane_filter_recently_seen =
357 active_blocks_since_sane_filter_ <= 30 * kNumBlocksPerSecond;
358 }
359
Sam Zackrisson46b01402019-10-08 16:17:48 +0200360 if (any_filter_converged) {
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200361 recent_convergence_during_activity_ = true;
362 active_non_converged_sequence_size_ = 0;
363 non_converged_sequence_size_ = 0;
364 ++num_converged_blocks_;
365 } else {
366 if (++non_converged_sequence_size_ > 20 * kNumBlocksPerSecond) {
367 num_converged_blocks_ = 0;
368 }
369
370 if (active_render &&
371 ++active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) {
372 recent_convergence_during_activity_ = false;
373 }
374 }
375
Sam Zackrisson46b01402019-10-08 16:17:48 +0200376 if (!all_filters_diverged) {
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200377 diverged_sequence_size_ = 0;
378 } else if (++diverged_sequence_size_ >= 60) {
379 // TODO(peah): Change these lines to ensure proper triggering of usable
380 // filter.
381 non_converged_sequence_size_ = kBlocksSinceConvergencedFilterInit;
382 }
383
384 if (active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) {
385 finite_erl_recently_detected_ = false;
386 }
387 if (num_converged_blocks_ > 50) {
388 finite_erl_recently_detected_ = true;
389 }
390
391 if (bounded_erl_) {
392 transparency_activated_ = false;
393 } else if (finite_erl_recently_detected_) {
394 transparency_activated_ = false;
395 } else if (sane_filter_recently_seen && recent_convergence_during_activity_) {
396 transparency_activated_ = false;
397 } else {
398 const bool filter_should_have_converged =
399 strong_not_saturated_render_blocks_ > 6 * kNumBlocksPerSecond;
400 transparency_activated_ = filter_should_have_converged;
401 }
402}
403
404AecState::FilteringQualityAnalyzer::FilteringQualityAnalyzer(
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200405 const EchoCanceller3Config& config) {}
406
407void AecState::FilteringQualityAnalyzer::Reset() {
408 usable_linear_estimate_ = false;
409 filter_update_blocks_since_reset_ = 0;
410}
411
412void AecState::FilteringQualityAnalyzer::Update(
413 bool active_render,
414 bool transparent_mode,
415 bool saturated_capture,
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200416 const absl::optional<DelayEstimate>& external_delay,
Sam Zackrisson46b01402019-10-08 16:17:48 +0200417 bool any_filter_converged) {
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200418 // Update blocks counter.
419 const bool filter_update = active_render && !saturated_capture;
420 filter_update_blocks_since_reset_ += filter_update ? 1 : 0;
421 filter_update_blocks_since_start_ += filter_update ? 1 : 0;
422
423 // Store convergence flag when observed.
Sam Zackrisson46b01402019-10-08 16:17:48 +0200424 convergence_seen_ = convergence_seen_ || any_filter_converged;
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200425
426 // Verify requirements for achieving a decent filter. The requirements for
427 // filter adaptation at call startup are more restrictive than after an
428 // in-call reset.
429 const bool sufficient_data_to_converge_at_startup =
430 filter_update_blocks_since_start_ > kNumBlocksPerSecond * 0.4f;
431 const bool sufficient_data_to_converge_at_reset =
432 sufficient_data_to_converge_at_startup &&
433 filter_update_blocks_since_reset_ > kNumBlocksPerSecond * 0.2f;
434
435 // The linear filter can only be used it has had time to converge.
436 usable_linear_estimate_ = sufficient_data_to_converge_at_startup &&
437 sufficient_data_to_converge_at_reset;
438
439 // The linear filter can only be used if an external delay or convergence have
440 // been identified
441 usable_linear_estimate_ =
442 usable_linear_estimate_ && (external_delay || convergence_seen_);
443
444 // If transparent mode is on, deactivate usign the linear filter.
445 usable_linear_estimate_ = usable_linear_estimate_ && !transparent_mode;
446}
447
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200448void AecState::SaturationDetector::Update(
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200449 rtc::ArrayView<const std::vector<float>> x,
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200450 bool saturated_capture,
451 bool usable_linear_estimate,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200452 rtc::ArrayView<const SubtractorOutput> subtractor_output,
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200453 float echo_path_gain) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200454 saturated_echo_ = false;
455 if (!saturated_capture) {
456 return;
457 }
458
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200459 if (usable_linear_estimate) {
460 constexpr float kSaturationThreshold = 20000.f;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200461 for (size_t ch = 0; ch < subtractor_output.size(); ++ch) {
462 saturated_echo_ =
463 saturated_echo_ ||
464 (subtractor_output[ch].s_main_max_abs > kSaturationThreshold ||
465 subtractor_output[ch].s_shadow_max_abs > kSaturationThreshold);
466 }
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200467 } else {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200468 float max_sample = 0.f;
469 for (auto& channel : x) {
470 for (float sample : channel) {
471 max_sample = std::max(max_sample, fabsf(sample));
472 }
473 }
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200474
475 const float kMargin = 10.f;
476 float peak_echo_amplitude = max_sample * echo_path_gain * kMargin;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200477 saturated_echo_ = saturated_echo_ || peak_echo_amplitude > 32000;
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200478 }
479}
480
peah522d71b2017-02-23 05:16:26 -0800481} // namespace webrtc