blob: e2f70a4c682a6d08dde01e1dd3272a464c92aabe [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <array>
peah522d71b2017-02-23 05:16:26 -080017#include <memory>
18#include <vector>
19
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010022#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/aec3/aec3_common.h"
Per Åhgren3ab308f2018-02-21 08:46:03 +010024#include "modules/audio_processing/aec3/delay_estimate.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020025#include "modules/audio_processing/aec3/echo_audibility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/aec3/echo_path_variability.h"
27#include "modules/audio_processing/aec3/erl_estimator.h"
28#include "modules/audio_processing/aec3/erle_estimator.h"
Per Åhgren5c532d32018-03-22 00:29:25 +010029#include "modules/audio_processing/aec3/filter_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_processing/aec3/render_buffer.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"
Gustaf Ullbergafef7a72020-09-24 09:21:49 +020034#include "modules/audio_processing/aec3/transparent_mode.h"
peah522d71b2017-02-23 05:16:26 -080035
36namespace webrtc {
37
38class ApmDataDumper;
39
40// Handles the state and the conditions for the echo removal functionality.
41class AecState {
42 public:
Sam Zackrisson8f736c02019-10-01 12:47:53 +020043 AecState(const EchoCanceller3Config& config, size_t num_capture_channels);
peah522d71b2017-02-23 05:16:26 -080044 ~AecState();
45
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010046 // Returns whether the echo subtractor can be used to determine the residual
47 // echo.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020048 bool UsableLinearEstimate() const {
Gustaf Ullberg52caa0e2019-04-11 14:43:17 +020049 return filter_quality_state_.LinearFilterUsable() &&
50 config_.filter.use_linear_filter;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020051 }
peah522d71b2017-02-23 05:16:26 -080052
Per Åhgren5c532d32018-03-22 00:29:25 +010053 // Returns whether the echo subtractor output should be used as output.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020054 bool UseLinearFilterOutput() const {
Gustaf Ullberg52caa0e2019-04-11 14:43:17 +020055 return filter_quality_state_.LinearFilterUsable() &&
56 config_.filter.use_linear_filter;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020057 }
Per Åhgren5c532d32018-03-22 00:29:25 +010058
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 Åhgrenb4161d32019-10-08 12:35:47 +020068 bool UseStationarityProperties() const {
Jesús de Vicente Peña70a59632019-04-16 12:32:15 +020069 return config_.echo_audibility.use_stationarity_properties;
Per Åhgrenf4801a12018-09-27 13:14:02 +020070 }
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020071
peah522d71b2017-02-23 05:16:26 -080072 // Returns the ERLE.
Gustaf Ullberg437d1292021-04-20 13:48:57 +020073 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle(
74 bool onset_compensated) const {
75 return erle_estimator_.Erle(onset_compensated);
peah522d71b2017-02-23 05:16:26 -080076 }
77
Gustaf Ullberga63d1522021-06-11 14:02:53 +020078 // Returns the non-capped ERLE.
79 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleUnbounded()
80 const {
81 return erle_estimator_.ErleUnbounded();
82 }
83
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020084 // Returns the fullband ERLE estimate in log2 units.
85 float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); }
Gustaf Ullberg332150d2017-11-22 14:17:39 +010086
peah522d71b2017-02-23 05:16:26 -080087 // Returns the ERL.
88 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
89 return erl_estimator_.Erl();
90 }
91
Gustaf Ullberg332150d2017-11-22 14:17:39 +010092 // Returns the time-domain ERL.
93 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
94
peah522d71b2017-02-23 05:16:26 -080095 // Returns the delay estimate based on the linear filter.
Per Åhgren8718afb2019-10-15 10:31:35 +020096 int MinDirectPathFilterDelay() const {
97 return delay_state_.MinDirectPathFilterDelay();
Per Åhgren8be669f2019-10-11 23:02:26 +020098 }
peah522d71b2017-02-23 05:16:26 -080099
peah522d71b2017-02-23 05:16:26 -0800100 // Returns whether the capture signal is saturated.
101 bool SaturatedCapture() const { return capture_signal_saturation_; }
102
peah86afe9d2017-04-06 15:45:32 -0700103 // Returns whether the echo signal is saturated.
Gustaf Ullberg68d6d442019-01-29 10:08:15 +0100104 bool SaturatedEcho() const { return saturation_detector_.SaturatedEcho(); }
peah86afe9d2017-04-06 15:45:32 -0700105
peah522d71b2017-02-23 05:16:26 -0800106 // Updates the capture signal saturation.
107 void UpdateCaptureSaturation(bool capture_signal_saturation) {
108 capture_signal_saturation_ = capture_signal_saturation;
109 }
110
Per Åhgren1b4059e2017-10-15 20:19:21 +0200111 // Returns whether the transparent mode is active
Gustaf Ullbergafef7a72020-09-24 09:21:49 +0200112 bool TransparentModeActive() const {
113 return transparent_state_ && transparent_state_->Active();
Per Åhgrend8d09c32020-04-01 17:30:18 +0200114 }
peah522d71b2017-02-23 05:16:26 -0800115
peah86afe9d2017-04-06 15:45:32 -0700116 // Takes appropriate action at an echo path change.
117 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
118
peah89420452017-04-07 06:13:39 -0700119 // Returns the decay factor for the echo reverberation.
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200120 float ReverbDecay() const { return reverb_model_estimator_.ReverbDecay(); }
peah89420452017-04-07 06:13:39 -0700121
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200122 // Return the frequency response of the reverberant echo.
123 rtc::ArrayView<const float> GetReverbFrequencyResponse() const {
124 return reverb_model_estimator_.GetReverbFrequencyResponse();
125 }
126
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200127 // Returns whether the transition for going out of the initial stated has
128 // been triggered.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200129 bool TransitionTriggered() const {
130 return initial_state_.TransitionTriggered();
131 }
Per Åhgrena98c8072018-01-15 19:17:16 +0100132
peah522d71b2017-02-23 05:16:26 -0800133 // Updates the aec state.
Per Åhgren119e2192019-10-18 08:50:50 +0200134 // TODO(bugs.webrtc.org/10913): Compute multi-channel ERL.
Sam Zackrisson46b01402019-10-08 16:17:48 +0200135 void Update(
136 const absl::optional<DelayEstimate>& external_delay,
137 rtc::ArrayView<const std::vector<std::array<float, kFftLengthBy2Plus1>>>
Per Åhgren8be669f2019-10-11 23:02:26 +0200138 adaptive_filter_frequency_responses,
139 rtc::ArrayView<const std::vector<float>>
140 adaptive_filter_impulse_responses,
Sam Zackrisson46b01402019-10-08 16:17:48 +0200141 const RenderBuffer& render_buffer,
Per Åhgrenff045112020-03-20 11:20:39 +0100142 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> E2_refined,
Per Åhgrenf9807252019-10-09 13:57:07 +0200143 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Y2,
Sam Zackrisson46b01402019-10-08 16:17:48 +0200144 rtc::ArrayView<const SubtractorOutput> subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800145
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200146 // Returns filter length in blocks.
147 int FilterLengthBlocks() const {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200148 // All filters have the same length, so arbitrarily return channel 0 length.
Per Åhgren8be669f2019-10-11 23:02:26 +0200149 return filter_analyzer_.FilterLengthBlocks();
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200150 }
151
peah522d71b2017-02-23 05:16:26 -0800152 private:
153 static int instance_count_;
154 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200155 const EchoCanceller3Config config_;
Per Åhgren785d4c42019-10-17 14:40:54 +0200156 const size_t num_capture_channels_;
Per Åhgrend8d09c32020-04-01 17:30:18 +0200157 const bool deactivate_initial_state_reset_at_echo_path_change_;
158 const bool full_reset_at_echo_path_change_;
159 const bool subtractor_analyzer_reset_at_echo_path_change_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200160
161 // Class for controlling the transition from the intial state, which in turn
162 // controls when the filter parameters for the initial state should be used.
163 class InitialState {
164 public:
165 explicit InitialState(const EchoCanceller3Config& config);
166 // Resets the state to again begin in the initial state.
167 void Reset();
168
169 // Updates the state based on new data.
170 void Update(bool active_render, bool saturated_capture);
171
172 // Returns whether the initial state is active or not.
173 bool InitialStateActive() const { return initial_state_; }
174
175 // Returns that the transition from the initial state has was started.
176 bool TransitionTriggered() const { return transition_triggered_; }
177
178 private:
179 const bool conservative_initial_phase_;
180 const float initial_state_seconds_;
181 bool transition_triggered_ = false;
182 bool initial_state_ = true;
183 size_t strong_not_saturated_render_blocks_ = 0;
184 } initial_state_;
185
186 // Class for choosing the direct-path delay relative to the beginning of the
187 // filter, as well as any other data related to the delay used within
188 // AecState.
189 class FilterDelay {
190 public:
Per Åhgren8be669f2019-10-11 23:02:26 +0200191 FilterDelay(const EchoCanceller3Config& config,
192 size_t num_capture_channels);
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200193
194 // Returns whether an external delay has been reported to the AecState (from
195 // the delay estimator).
196 bool ExternalDelayReported() const { return external_delay_reported_; }
197
198 // Returns the delay in blocks relative to the beginning of the filter that
199 // corresponds to the direct path of the echo.
Per Åhgren8be669f2019-10-11 23:02:26 +0200200 rtc::ArrayView<const int> DirectPathFilterDelays() const {
201 return filter_delays_blocks_;
202 }
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200203
Per Åhgren8718afb2019-10-15 10:31:35 +0200204 // Returns the minimum delay among the direct path delays relative to the
205 // beginning of the filter
206 int MinDirectPathFilterDelay() const { return min_filter_delay_; }
207
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200208 // Updates the delay estimates based on new data.
Sam Zackrisson46b01402019-10-08 16:17:48 +0200209 void Update(
Per Åhgren8be669f2019-10-11 23:02:26 +0200210 rtc::ArrayView<const int> analyzer_filter_delay_estimates_blocks,
Sam Zackrisson46b01402019-10-08 16:17:48 +0200211 const absl::optional<DelayEstimate>& external_delay,
212 size_t blocks_with_proper_filter_adaptation);
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200213
214 private:
Sam Zackrissonfa292792020-10-05 15:23:04 +0200215 const int delay_headroom_blocks_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200216 bool external_delay_reported_ = false;
Per Åhgren8be669f2019-10-11 23:02:26 +0200217 std::vector<int> filter_delays_blocks_;
Sam Zackrissonfa292792020-10-05 15:23:04 +0200218 int min_filter_delay_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200219 absl::optional<DelayEstimate> external_delay_;
220 } delay_state_;
221
Gustaf Ullbergafef7a72020-09-24 09:21:49 +0200222 // Classifier for toggling transparent mode when there is no echo.
223 std::unique_ptr<TransparentMode> transparent_state_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200224
225 // Class for analyzing how well the linear filter is, and can be expected to,
226 // perform on the current signals. The purpose of this is for using to
227 // select the echo suppression functionality as well as the input to the echo
228 // suppressor.
229 class FilteringQualityAnalyzer {
230 public:
Per Åhgren8be669f2019-10-11 23:02:26 +0200231 FilteringQualityAnalyzer(const EchoCanceller3Config& config,
232 size_t num_capture_channels);
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200233
Per Åhgren8be669f2019-10-11 23:02:26 +0200234 // Returns whether the linear filter can be used for the echo
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200235 // canceller output.
Per Åhgren8be669f2019-10-11 23:02:26 +0200236 bool LinearFilterUsable() const { return overall_usable_linear_estimates_; }
237
238 // Returns whether an individual filter output can be used for the echo
239 // canceller output.
240 const std::vector<bool>& UsableLinearFilterOutputs() const {
241 return usable_linear_filter_estimates_;
242 }
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200243
244 // Resets the state of the analyzer.
245 void Reset();
246
247 // Updates the analysis based on new data.
248 void Update(bool active_render,
249 bool transparent_mode,
250 bool saturated_capture,
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200251 const absl::optional<DelayEstimate>& external_delay,
Sam Zackrisson46b01402019-10-08 16:17:48 +0200252 bool any_filter_converged);
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200253
254 private:
Per Åhgren8be669f2019-10-11 23:02:26 +0200255 const bool use_linear_filter_;
256 bool overall_usable_linear_estimates_ = false;
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200257 size_t filter_update_blocks_since_reset_ = 0;
258 size_t filter_update_blocks_since_start_ = 0;
259 bool convergence_seen_ = false;
Per Åhgren8be669f2019-10-11 23:02:26 +0200260 std::vector<bool> usable_linear_filter_estimates_;
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200261 } filter_quality_state_;
262
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200263 // Class for detecting whether the echo is to be considered to be
264 // saturated.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200265 class SaturationDetector {
266 public:
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200267 // Returns whether the echo is to be considered saturated.
Nico Weber22f99252019-02-20 10:13:16 -0500268 bool SaturatedEcho() const { return saturated_echo_; }
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200269
270 // Updates the detection decision based on new data.
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200271 void Update(rtc::ArrayView<const std::vector<float>> x,
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200272 bool saturated_capture,
273 bool usable_linear_estimate,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200274 rtc::ArrayView<const SubtractorOutput> subtractor_output,
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200275 float echo_path_gain);
276
277 private:
278 bool saturated_echo_ = false;
279 } saturation_detector_;
280
peah522d71b2017-02-23 05:16:26 -0800281 ErlEstimator erl_estimator_;
282 ErleEstimator erle_estimator_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200283 size_t strong_not_saturated_render_blocks_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100284 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800285 bool capture_signal_saturation_ = false;
Per Åhgren8be669f2019-10-11 23:02:26 +0200286 FilterAnalyzer filter_analyzer_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200287 EchoAudibility echo_audibility_;
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200288 ReverbModelEstimator reverb_model_estimator_;
Per Åhgren8718afb2019-10-15 10:31:35 +0200289 ReverbModel avg_render_reverb_;
Per Åhgren785d4c42019-10-17 14:40:54 +0200290 SubtractorOutputAnalyzer subtractor_output_analyzer_;
peah522d71b2017-02-23 05:16:26 -0800291};
292
293} // namespace webrtc
294
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200295#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_