blob: e323b2c6b1a9f819ccdc7fcc59baefb1d30dde45 [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 {
48 return filter_quality_state_.LinearFilterUsable();
49 }
peah522d71b2017-02-23 05:16:26 -080050
Per Åhgren5c532d32018-03-22 00:29:25 +010051 // Returns whether the echo subtractor output should be used as output.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020052 bool UseLinearFilterOutput() const {
53 return filter_quality_state_.LinearFilterUsable();
54 }
Per Åhgren5c532d32018-03-22 00:29:25 +010055
56 // Returns the estimated echo path gain.
Per Åhgrenced31ba2018-05-09 11:48:49 +020057 float EchoPathGain() const { return filter_analyzer_.Gain(); }
peah522d71b2017-02-23 05:16:26 -080058
peah522d71b2017-02-23 05:16:26 -080059 // Returns whether the render signal is currently active.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010060 bool ActiveRender() const { return blocks_with_active_render_ > 200; }
peahebe77782017-02-27 07:29:21 -080061
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020062 // Returns the appropriate scaling of the residual echo to match the
63 // audibility.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020064 void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020065
66 // Returns whether the stationary properties of the signals are used in the
67 // aec.
Per Åhgrenf4801a12018-09-27 13:14:02 +020068 bool UseStationaryProperties() const {
69 return config_.echo_audibility.use_stationary_properties;
70 }
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020071
peah522d71b2017-02-23 05:16:26 -080072 // Returns the ERLE.
73 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
74 return erle_estimator_.Erle();
75 }
76
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020077 // Returns an offset to apply to the estimation of the residual echo
78 // computation. Returning nullopt means that no offset should be used, while
79 // any other value will be applied as a multiplier to the estimated residual
80 // echo.
81 absl::optional<float> ErleUncertainty() const;
Gustaf Ullberg6c618c72018-06-28 14:21:16 +020082
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020083 // Returns the fullband ERLE estimate in log2 units.
84 float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); }
Gustaf Ullberg332150d2017-11-22 14:17:39 +010085
peah522d71b2017-02-23 05:16:26 -080086 // Returns the ERL.
87 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
88 return erl_estimator_.Erl();
89 }
90
Gustaf Ullberg332150d2017-11-22 14:17:39 +010091 // Returns the time-domain ERL.
92 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
93
peah522d71b2017-02-23 05:16:26 -080094 // Returns the delay estimate based on the linear filter.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020095 int FilterDelayBlocks() const { return delay_state_.DirectPathFilterDelay(); }
peah522d71b2017-02-23 05:16:26 -080096
peah522d71b2017-02-23 05:16:26 -080097 // Returns whether the capture signal is saturated.
98 bool SaturatedCapture() const { return capture_signal_saturation_; }
99
peah86afe9d2017-04-06 15:45:32 -0700100 // Returns whether the echo signal is saturated.
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100101 bool SaturatedEcho() const { return saturation_detector_.SaturatedEcho(); }
peah86afe9d2017-04-06 15:45:32 -0700102
peah522d71b2017-02-23 05:16:26 -0800103 // Updates the capture signal saturation.
104 void UpdateCaptureSaturation(bool capture_signal_saturation) {
105 capture_signal_saturation_ = capture_signal_saturation;
106 }
107
Per Åhgren1b4059e2017-10-15 20:19:21 +0200108 // Returns whether the transparent mode is active
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200109 bool TransparentMode() const { return transparent_state_.Active(); }
peah522d71b2017-02-23 05:16:26 -0800110
peah86afe9d2017-04-06 15:45:32 -0700111 // Takes appropriate action at an echo path change.
112 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
113
peah89420452017-04-07 06:13:39 -0700114 // Returns the decay factor for the echo reverberation.
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200115 float ReverbDecay() const { return reverb_model_estimator_.ReverbDecay(); }
peah89420452017-04-07 06:13:39 -0700116
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200117 // Return the frequency response of the reverberant echo.
118 rtc::ArrayView<const float> GetReverbFrequencyResponse() const {
119 return reverb_model_estimator_.GetReverbFrequencyResponse();
120 }
121
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200122 // Returns whether the transition for going out of the initial stated has
123 // been triggered.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200124 bool TransitionTriggered() const {
125 return initial_state_.TransitionTriggered();
126 }
Per Åhgrena98c8072018-01-15 19:17:16 +0100127
peah522d71b2017-02-23 05:16:26 -0800128 // Updates the aec state.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200129 void Update(const absl::optional<DelayEstimate>& external_delay,
Per Åhgren3ab308f2018-02-21 08:46:03 +0100130 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700131 adaptive_filter_frequency_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100132 const std::vector<float>& adaptive_filter_impulse_response,
peah86afe9d2017-04-06 15:45:32 -0700133 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800134 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800135 const std::array<float, kFftLengthBy2Plus1>& Y2,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200136 const SubtractorOutput& subtractor_output,
137 rtc::ArrayView<const float> y);
peah522d71b2017-02-23 05:16:26 -0800138
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200139 // Returns filter length in blocks.
140 int FilterLengthBlocks() const {
141 return filter_analyzer_.FilterLengthBlocks();
142 }
143
peah522d71b2017-02-23 05:16:26 -0800144 private:
145 static int instance_count_;
146 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200147 const EchoCanceller3Config config_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200148
149 // Class for controlling the transition from the intial state, which in turn
150 // controls when the filter parameters for the initial state should be used.
151 class InitialState {
152 public:
153 explicit InitialState(const EchoCanceller3Config& config);
154 // Resets the state to again begin in the initial state.
155 void Reset();
156
157 // Updates the state based on new data.
158 void Update(bool active_render, bool saturated_capture);
159
160 // Returns whether the initial state is active or not.
161 bool InitialStateActive() const { return initial_state_; }
162
163 // Returns that the transition from the initial state has was started.
164 bool TransitionTriggered() const { return transition_triggered_; }
165
166 private:
167 const bool conservative_initial_phase_;
168 const float initial_state_seconds_;
169 bool transition_triggered_ = false;
170 bool initial_state_ = true;
171 size_t strong_not_saturated_render_blocks_ = 0;
172 } initial_state_;
173
174 // Class for choosing the direct-path delay relative to the beginning of the
175 // filter, as well as any other data related to the delay used within
176 // AecState.
177 class FilterDelay {
178 public:
179 explicit FilterDelay(const EchoCanceller3Config& config);
180
181 // Returns whether an external delay has been reported to the AecState (from
182 // the delay estimator).
183 bool ExternalDelayReported() const { return external_delay_reported_; }
184
185 // Returns the delay in blocks relative to the beginning of the filter that
186 // corresponds to the direct path of the echo.
187 int DirectPathFilterDelay() const { return filter_delay_blocks_; }
188
189 // Updates the delay estimates based on new data.
190 void Update(const FilterAnalyzer& filter_analyzer,
191 const absl::optional<DelayEstimate>& external_delay,
192 size_t blocks_with_proper_filter_adaptation);
193
194 private:
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +0100195 const int delay_headroom_samples_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200196 bool external_delay_reported_ = false;
197 int filter_delay_blocks_ = 0;
198 absl::optional<DelayEstimate> external_delay_;
199 } delay_state_;
200
201 // Class for detecting and toggling the transparent mode which causes the
202 // suppressor to apply no suppression.
203 class TransparentMode {
204 public:
205 explicit TransparentMode(const EchoCanceller3Config& config);
206
207 // Returns whether the transparent mode should be active.
208 bool Active() const { return transparency_activated_; }
209
210 // Resets the state of the detector.
211 void Reset();
212
213 // Updates the detection deciscion based on new data.
214 void Update(int filter_delay_blocks,
215 bool consistent_filter,
216 bool converged_filter,
217 bool diverged_filter,
218 bool active_render,
219 bool saturated_capture);
220
221 private:
222 const bool bounded_erl_;
223 const bool linear_and_stable_echo_path_;
224 size_t capture_block_counter_ = 0;
225 bool transparency_activated_ = false;
226 size_t active_blocks_since_sane_filter_;
227 bool sane_filter_observed_ = false;
228 bool finite_erl_recently_detected_ = false;
229 size_t non_converged_sequence_size_;
230 size_t diverged_sequence_size_ = 0;
231 size_t active_non_converged_sequence_size_ = 0;
232 size_t num_converged_blocks_ = 0;
233 bool recent_convergence_during_activity_ = false;
234 size_t strong_not_saturated_render_blocks_ = 0;
235 } transparent_state_;
236
237 // Class for analyzing how well the linear filter is, and can be expected to,
238 // perform on the current signals. The purpose of this is for using to
239 // select the echo suppression functionality as well as the input to the echo
240 // suppressor.
241 class FilteringQualityAnalyzer {
242 public:
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200243 FilteringQualityAnalyzer(const EchoCanceller3Config& config);
244
245 // Returns whether the the linear filter can be used for the echo
246 // canceller output.
247 bool LinearFilterUsable() const { return usable_linear_estimate_; }
248
249 // Resets the state of the analyzer.
250 void Reset();
251
252 // Updates the analysis based on new data.
253 void Update(bool active_render,
254 bool transparent_mode,
255 bool saturated_capture,
256 bool consistent_estimate_,
257 const absl::optional<DelayEstimate>& external_delay,
258 bool converged_filter);
259
260 private:
261 bool usable_linear_estimate_ = false;
262 size_t filter_update_blocks_since_reset_ = 0;
263 size_t filter_update_blocks_since_start_ = 0;
264 bool convergence_seen_ = false;
265 } filter_quality_state_;
266
267 // Class containing the legacy functionality for analyzing how well the linear
268 // filter is, and can be expected to perform on the current signals. The
269 // purpose of this is for using to select the echo suppression functionality
270 // as well as the input to the echo suppressor.
271 class LegacyFilteringQualityAnalyzer {
272 public:
273 explicit LegacyFilteringQualityAnalyzer(const EchoCanceller3Config& config);
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200274
275 // Returns whether the the linear filter is can be used for the echo
276 // canceller output.
277 bool LinearFilterUsable() const { return usable_linear_estimate_; }
278
279 // Resets the state of the analyzer.
280 void Reset();
281
282 // Updates the analysis based on new data.
283 void Update(bool saturated_echo,
284 bool active_render,
285 bool saturated_capture,
286 bool transparent_mode,
287 const absl::optional<DelayEstimate>& external_delay,
288 bool converged_filter,
289 bool diverged_filter);
290
291 private:
292 const bool conservative_initial_phase_;
293 const float required_blocks_for_convergence_;
294 const bool linear_and_stable_echo_path_;
295 bool usable_linear_estimate_ = false;
296 size_t strong_not_saturated_render_blocks_ = 0;
297 size_t non_converged_sequence_size_;
298 size_t diverged_sequence_size_ = 0;
299 size_t active_non_converged_sequence_size_ = 0;
300 bool recent_convergence_during_activity_ = false;
301 bool recent_convergence_ = false;
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200302 } legacy_filter_quality_state_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200303
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200304 // Class for detecting whether the echo is to be considered to be
305 // saturated.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200306 class SaturationDetector {
307 public:
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200308 // Returns whether the echo is to be considered saturated.
Nico Weber22f99252019-02-20 10:13:16 -0500309 bool SaturatedEcho() const { return saturated_echo_; }
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200310
311 // Updates the detection decision based on new data.
312 void Update(rtc::ArrayView<const float> x,
313 bool saturated_capture,
314 bool usable_linear_estimate,
315 const SubtractorOutput& subtractor_output,
316 float echo_path_gain);
317
318 private:
319 bool saturated_echo_ = false;
320 } saturation_detector_;
321
322 // Legacy class for detecting whether the echo is to be considered to be
323 // saturated. This is kept as a fallback solution to use instead of the class
324 // SaturationDetector,
325 class LegacySaturationDetector {
326 public:
327 explicit LegacySaturationDetector(const EchoCanceller3Config& config);
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200328
329 // Returns whether the echo is to be considered saturated.
Nico Weber22f99252019-02-20 10:13:16 -0500330 bool SaturatedEcho() const { return saturated_echo_; }
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200331
332 // Resets the state of the detector.
333 void Reset();
334
335 // Updates the detection decision based on new data.
336 void Update(rtc::ArrayView<const float> x,
337 bool saturated_capture,
338 float echo_path_gain);
339
340 private:
341 const bool echo_can_saturate_;
342 size_t not_saturated_sequence_size_;
343 bool saturated_echo_ = false;
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200344 } legacy_saturation_detector_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200345
peah522d71b2017-02-23 05:16:26 -0800346 ErlEstimator erl_estimator_;
347 ErleEstimator erle_estimator_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200348 size_t strong_not_saturated_render_blocks_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100349 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800350 bool capture_signal_saturation_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +0100351 FilterAnalyzer filter_analyzer_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200352 absl::optional<DelayEstimate> external_delay_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200353 EchoAudibility echo_audibility_;
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200354 ReverbModelEstimator reverb_model_estimator_;
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200355 RenderReverbModel render_reverb_;
Per Åhgrenb20b9372018-07-13 00:22:54 +0200356 SubtractorOutputAnalyzer subtractor_output_analyzer_;
peah522d71b2017-02-23 05:16:26 -0800357};
358
359} // namespace webrtc
360
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200361#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_