blob: 1887492f8ad224eab4b30a3c38ea1d52af61c325 [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#ifndef MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
peah522d71b2017-02-23 05:16:26 -080013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <array>
peah522d71b2017-02-23 05:16:26 -080016#include <memory>
17#include <vector>
18
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010021#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/aec3/aec3_common.h"
Per Åhgren3ab308f2018-02-21 08:46:03 +010023#include "modules/audio_processing/aec3/delay_estimate.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020024#include "modules/audio_processing/aec3/echo_audibility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/aec3/echo_path_variability.h"
26#include "modules/audio_processing/aec3/erl_estimator.h"
27#include "modules/audio_processing/aec3/erle_estimator.h"
Per Åhgren5c532d32018-03-22 00:29:25 +010028#include "modules/audio_processing/aec3/filter_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/aec3/render_buffer.h"
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +020030#include "modules/audio_processing/aec3/render_reverb_model.h"
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020031#include "modules/audio_processing/aec3/reverb_model_estimator.h"
Per Åhgrenb20b9372018-07-13 00:22:54 +020032#include "modules/audio_processing/aec3/subtractor_output.h"
33#include "modules/audio_processing/aec3/subtractor_output_analyzer.h"
peah522d71b2017-02-23 05:16:26 -080034
35namespace webrtc {
36
37class ApmDataDumper;
38
39// Handles the state and the conditions for the echo removal functionality.
40class AecState {
41 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020042 explicit AecState(const EchoCanceller3Config& config);
peah522d71b2017-02-23 05:16:26 -080043 ~AecState();
44
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010045 // Returns whether the echo subtractor can be used to determine the residual
46 // echo.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020047 bool UsableLinearEstimate() const {
Gustaf Ullberg52caa0e2019-04-11 14:43:17 +020048 return filter_quality_state_.LinearFilterUsable() &&
49 config_.filter.use_linear_filter;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020050 }
peah522d71b2017-02-23 05:16:26 -080051
Per Åhgren5c532d32018-03-22 00:29:25 +010052 // Returns whether the echo subtractor output should be used as output.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020053 bool UseLinearFilterOutput() const {
Gustaf Ullberg52caa0e2019-04-11 14:43:17 +020054 return filter_quality_state_.LinearFilterUsable() &&
55 config_.filter.use_linear_filter;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020056 }
Per Åhgren5c532d32018-03-22 00:29:25 +010057
58 // Returns the estimated echo path gain.
Per Åhgrenced31ba2018-05-09 11:48:49 +020059 float EchoPathGain() const { return filter_analyzer_.Gain(); }
peah522d71b2017-02-23 05:16:26 -080060
peah522d71b2017-02-23 05:16:26 -080061 // Returns whether the render signal is currently active.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010062 bool ActiveRender() const { return blocks_with_active_render_ > 200; }
peahebe77782017-02-27 07:29:21 -080063
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020064 // Returns the appropriate scaling of the residual echo to match the
65 // audibility.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020066 void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020067
68 // Returns whether the stationary properties of the signals are used in the
69 // aec.
Per Åhgrenf4801a12018-09-27 13:14:02 +020070 bool UseStationaryProperties() const {
71 return config_.echo_audibility.use_stationary_properties;
72 }
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020073
peah522d71b2017-02-23 05:16:26 -080074 // Returns the ERLE.
75 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
76 return erle_estimator_.Erle();
77 }
78
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020079 // Returns an offset to apply to the estimation of the residual echo
80 // computation. Returning nullopt means that no offset should be used, while
81 // any other value will be applied as a multiplier to the estimated residual
82 // echo.
83 absl::optional<float> ErleUncertainty() const;
Gustaf Ullberg6c618c72018-06-28 14:21:16 +020084
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020085 // Returns the fullband ERLE estimate in log2 units.
86 float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); }
Gustaf Ullberg332150d2017-11-22 14:17:39 +010087
peah522d71b2017-02-23 05:16:26 -080088 // Returns the ERL.
89 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
90 return erl_estimator_.Erl();
91 }
92
Gustaf Ullberg332150d2017-11-22 14:17:39 +010093 // Returns the time-domain ERL.
94 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
95
peah522d71b2017-02-23 05:16:26 -080096 // Returns the delay estimate based on the linear filter.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020097 int FilterDelayBlocks() const { return delay_state_.DirectPathFilterDelay(); }
peah522d71b2017-02-23 05:16:26 -080098
peah522d71b2017-02-23 05:16:26 -080099 // Returns whether the capture signal is saturated.
100 bool SaturatedCapture() const { return capture_signal_saturation_; }
101
peah86afe9d2017-04-06 15:45:32 -0700102 // Returns whether the echo signal is saturated.
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100103 bool SaturatedEcho() const { return saturation_detector_.SaturatedEcho(); }
peah86afe9d2017-04-06 15:45:32 -0700104
peah522d71b2017-02-23 05:16:26 -0800105 // Updates the capture signal saturation.
106 void UpdateCaptureSaturation(bool capture_signal_saturation) {
107 capture_signal_saturation_ = capture_signal_saturation;
108 }
109
Per Åhgren1b4059e2017-10-15 20:19:21 +0200110 // Returns whether the transparent mode is active
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200111 bool TransparentMode() const { return transparent_state_.Active(); }
peah522d71b2017-02-23 05:16:26 -0800112
peah86afe9d2017-04-06 15:45:32 -0700113 // Takes appropriate action at an echo path change.
114 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
115
peah89420452017-04-07 06:13:39 -0700116 // Returns the decay factor for the echo reverberation.
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200117 float ReverbDecay() const { return reverb_model_estimator_.ReverbDecay(); }
peah89420452017-04-07 06:13:39 -0700118
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200119 // Return the frequency response of the reverberant echo.
120 rtc::ArrayView<const float> GetReverbFrequencyResponse() const {
121 return reverb_model_estimator_.GetReverbFrequencyResponse();
122 }
123
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200124 // Returns whether the transition for going out of the initial stated has
125 // been triggered.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200126 bool TransitionTriggered() const {
127 return initial_state_.TransitionTriggered();
128 }
Per Åhgrena98c8072018-01-15 19:17:16 +0100129
peah522d71b2017-02-23 05:16:26 -0800130 // Updates the aec state.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200131 void Update(const absl::optional<DelayEstimate>& external_delay,
Per Åhgren3ab308f2018-02-21 08:46:03 +0100132 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700133 adaptive_filter_frequency_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100134 const std::vector<float>& adaptive_filter_impulse_response,
peah86afe9d2017-04-06 15:45:32 -0700135 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800136 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800137 const std::array<float, kFftLengthBy2Plus1>& Y2,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200138 const SubtractorOutput& subtractor_output,
139 rtc::ArrayView<const float> y);
peah522d71b2017-02-23 05:16:26 -0800140
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200141 // Returns filter length in blocks.
142 int FilterLengthBlocks() const {
143 return filter_analyzer_.FilterLengthBlocks();
144 }
145
peah522d71b2017-02-23 05:16:26 -0800146 private:
147 static int instance_count_;
148 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200149 const EchoCanceller3Config config_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200150
151 // Class for controlling the transition from the intial state, which in turn
152 // controls when the filter parameters for the initial state should be used.
153 class InitialState {
154 public:
155 explicit InitialState(const EchoCanceller3Config& config);
156 // Resets the state to again begin in the initial state.
157 void Reset();
158
159 // Updates the state based on new data.
160 void Update(bool active_render, bool saturated_capture);
161
162 // Returns whether the initial state is active or not.
163 bool InitialStateActive() const { return initial_state_; }
164
165 // Returns that the transition from the initial state has was started.
166 bool TransitionTriggered() const { return transition_triggered_; }
167
168 private:
169 const bool conservative_initial_phase_;
170 const float initial_state_seconds_;
171 bool transition_triggered_ = false;
172 bool initial_state_ = true;
173 size_t strong_not_saturated_render_blocks_ = 0;
174 } initial_state_;
175
176 // Class for choosing the direct-path delay relative to the beginning of the
177 // filter, as well as any other data related to the delay used within
178 // AecState.
179 class FilterDelay {
180 public:
181 explicit FilterDelay(const EchoCanceller3Config& config);
182
183 // Returns whether an external delay has been reported to the AecState (from
184 // the delay estimator).
185 bool ExternalDelayReported() const { return external_delay_reported_; }
186
187 // Returns the delay in blocks relative to the beginning of the filter that
188 // corresponds to the direct path of the echo.
189 int DirectPathFilterDelay() const { return filter_delay_blocks_; }
190
191 // Updates the delay estimates based on new data.
192 void Update(const FilterAnalyzer& filter_analyzer,
193 const absl::optional<DelayEstimate>& external_delay,
194 size_t blocks_with_proper_filter_adaptation);
195
196 private:
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +0100197 const int delay_headroom_samples_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200198 bool external_delay_reported_ = false;
199 int filter_delay_blocks_ = 0;
200 absl::optional<DelayEstimate> external_delay_;
201 } delay_state_;
202
203 // Class for detecting and toggling the transparent mode which causes the
204 // suppressor to apply no suppression.
205 class TransparentMode {
206 public:
207 explicit TransparentMode(const EchoCanceller3Config& config);
208
209 // Returns whether the transparent mode should be active.
210 bool Active() const { return transparency_activated_; }
211
212 // Resets the state of the detector.
213 void Reset();
214
215 // Updates the detection deciscion based on new data.
216 void Update(int filter_delay_blocks,
217 bool consistent_filter,
218 bool converged_filter,
219 bool diverged_filter,
220 bool active_render,
221 bool saturated_capture);
222
223 private:
224 const bool bounded_erl_;
225 const bool linear_and_stable_echo_path_;
226 size_t capture_block_counter_ = 0;
227 bool transparency_activated_ = false;
228 size_t active_blocks_since_sane_filter_;
229 bool sane_filter_observed_ = false;
230 bool finite_erl_recently_detected_ = false;
231 size_t non_converged_sequence_size_;
232 size_t diverged_sequence_size_ = 0;
233 size_t active_non_converged_sequence_size_ = 0;
234 size_t num_converged_blocks_ = 0;
235 bool recent_convergence_during_activity_ = false;
236 size_t strong_not_saturated_render_blocks_ = 0;
237 } transparent_state_;
238
239 // Class for analyzing how well the linear filter is, and can be expected to,
240 // perform on the current signals. The purpose of this is for using to
241 // select the echo suppression functionality as well as the input to the echo
242 // suppressor.
243 class FilteringQualityAnalyzer {
244 public:
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200245 FilteringQualityAnalyzer(const EchoCanceller3Config& config);
246
247 // Returns whether the the linear filter can be used for the echo
248 // canceller output.
249 bool LinearFilterUsable() const { return usable_linear_estimate_; }
250
251 // Resets the state of the analyzer.
252 void Reset();
253
254 // Updates the analysis based on new data.
255 void Update(bool active_render,
256 bool transparent_mode,
257 bool saturated_capture,
258 bool consistent_estimate_,
259 const absl::optional<DelayEstimate>& external_delay,
260 bool converged_filter);
261
262 private:
263 bool usable_linear_estimate_ = false;
264 size_t filter_update_blocks_since_reset_ = 0;
265 size_t filter_update_blocks_since_start_ = 0;
266 bool convergence_seen_ = false;
267 } filter_quality_state_;
268
269 // Class containing the legacy functionality for analyzing how well the linear
270 // filter is, and can be expected to perform on the current signals. The
271 // purpose of this is for using to select the echo suppression functionality
272 // as well as the input to the echo suppressor.
273 class LegacyFilteringQualityAnalyzer {
274 public:
275 explicit LegacyFilteringQualityAnalyzer(const EchoCanceller3Config& config);
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200276
277 // Returns whether the the linear filter is can be used for the echo
278 // canceller output.
279 bool LinearFilterUsable() const { return usable_linear_estimate_; }
280
281 // Resets the state of the analyzer.
282 void Reset();
283
284 // Updates the analysis based on new data.
285 void Update(bool saturated_echo,
286 bool active_render,
287 bool saturated_capture,
288 bool transparent_mode,
289 const absl::optional<DelayEstimate>& external_delay,
290 bool converged_filter,
291 bool diverged_filter);
292
293 private:
294 const bool conservative_initial_phase_;
295 const float required_blocks_for_convergence_;
296 const bool linear_and_stable_echo_path_;
297 bool usable_linear_estimate_ = false;
298 size_t strong_not_saturated_render_blocks_ = 0;
299 size_t non_converged_sequence_size_;
300 size_t diverged_sequence_size_ = 0;
301 size_t active_non_converged_sequence_size_ = 0;
302 bool recent_convergence_during_activity_ = false;
303 bool recent_convergence_ = false;
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200304 } legacy_filter_quality_state_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200305
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200306 // Class for detecting whether the echo is to be considered to be
307 // saturated.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200308 class SaturationDetector {
309 public:
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200310 // Returns whether the echo is to be considered saturated.
Nico Weber22f99252019-02-20 10:13:16 -0500311 bool SaturatedEcho() const { return saturated_echo_; }
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200312
313 // Updates the detection decision based on new data.
314 void Update(rtc::ArrayView<const float> x,
315 bool saturated_capture,
316 bool usable_linear_estimate,
317 const SubtractorOutput& subtractor_output,
318 float echo_path_gain);
319
320 private:
321 bool saturated_echo_ = false;
322 } saturation_detector_;
323
324 // Legacy class for detecting whether the echo is to be considered to be
325 // saturated. This is kept as a fallback solution to use instead of the class
326 // SaturationDetector,
327 class LegacySaturationDetector {
328 public:
329 explicit LegacySaturationDetector(const EchoCanceller3Config& config);
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200330
331 // Returns whether the echo is to be considered saturated.
Nico Weber22f99252019-02-20 10:13:16 -0500332 bool SaturatedEcho() const { return saturated_echo_; }
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200333
334 // Resets the state of the detector.
335 void Reset();
336
337 // Updates the detection decision based on new data.
338 void Update(rtc::ArrayView<const float> x,
339 bool saturated_capture,
340 float echo_path_gain);
341
342 private:
343 const bool echo_can_saturate_;
344 size_t not_saturated_sequence_size_;
345 bool saturated_echo_ = false;
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200346 } legacy_saturation_detector_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200347
peah522d71b2017-02-23 05:16:26 -0800348 ErlEstimator erl_estimator_;
349 ErleEstimator erle_estimator_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200350 size_t strong_not_saturated_render_blocks_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100351 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800352 bool capture_signal_saturation_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +0100353 FilterAnalyzer filter_analyzer_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200354 absl::optional<DelayEstimate> external_delay_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200355 EchoAudibility echo_audibility_;
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200356 ReverbModelEstimator reverb_model_estimator_;
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200357 RenderReverbModel render_reverb_;
Per Åhgrenb20b9372018-07-13 00:22:54 +0200358 SubtractorOutputAnalyzer subtractor_output_analyzer_;
peah522d71b2017-02-23 05:16:26 -0800359};
360
361} // namespace webrtc
362
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200363#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_