blob: f8609872968c9b6bfa8ff43cddc3979a1625b4e6 [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ñac98849c2018-10-22 11:41:05 +020031#include "modules/audio_processing/aec3/render_reverb_model.h"
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020032#include "modules/audio_processing/aec3/reverb_model_estimator.h"
Per Åhgrenb20b9372018-07-13 00:22:54 +020033#include "modules/audio_processing/aec3/subtractor_output.h"
34#include "modules/audio_processing/aec3/subtractor_output_analyzer.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
59 // Returns the estimated echo path gain.
Per Åhgrenced31ba2018-05-09 11:48:49 +020060 float EchoPathGain() const { return filter_analyzer_.Gain(); }
peah522d71b2017-02-23 05:16:26 -080061
peah522d71b2017-02-23 05:16:26 -080062 // Returns whether the render signal is currently active.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010063 bool ActiveRender() const { return blocks_with_active_render_ > 200; }
peahebe77782017-02-27 07:29:21 -080064
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020065 // Returns the appropriate scaling of the residual echo to match the
66 // audibility.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020067 void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020068
69 // Returns whether the stationary properties of the signals are used in the
70 // aec.
Per Åhgrenb4161d32019-10-08 12:35:47 +020071 bool UseStationarityProperties() const {
Jesús de Vicente Peña70a59632019-04-16 12:32:15 +020072 return config_.echo_audibility.use_stationarity_properties;
Per Åhgrenf4801a12018-09-27 13:14:02 +020073 }
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020074
peah522d71b2017-02-23 05:16:26 -080075 // Returns the ERLE.
Per Åhgrenb4161d32019-10-08 12:35:47 +020076 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle() const {
peah522d71b2017-02-23 05:16:26 -080077 return erle_estimator_.Erle();
78 }
79
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020080 // Returns an offset to apply to the estimation of the residual echo
81 // computation. Returning nullopt means that no offset should be used, while
82 // any other value will be applied as a multiplier to the estimated residual
83 // echo.
84 absl::optional<float> ErleUncertainty() const;
Gustaf Ullberg6c618c72018-06-28 14:21:16 +020085
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020086 // Returns the fullband ERLE estimate in log2 units.
87 float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); }
Gustaf Ullberg332150d2017-11-22 14:17:39 +010088
peah522d71b2017-02-23 05:16:26 -080089 // Returns the ERL.
90 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
91 return erl_estimator_.Erl();
92 }
93
Gustaf Ullberg332150d2017-11-22 14:17:39 +010094 // Returns the time-domain ERL.
95 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
96
peah522d71b2017-02-23 05:16:26 -080097 // Returns the delay estimate based on the linear filter.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020098 int FilterDelayBlocks() const { return delay_state_.DirectPathFilterDelay(); }
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
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200112 bool TransparentMode() const { return transparent_state_.Active(); }
peah522d71b2017-02-23 05:16:26 -0800113
peah86afe9d2017-04-06 15:45:32 -0700114 // Takes appropriate action at an echo path change.
115 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
116
peah89420452017-04-07 06:13:39 -0700117 // Returns the decay factor for the echo reverberation.
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200118 float ReverbDecay() const { return reverb_model_estimator_.ReverbDecay(); }
peah89420452017-04-07 06:13:39 -0700119
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200120 // Return the frequency response of the reverberant echo.
121 rtc::ArrayView<const float> GetReverbFrequencyResponse() const {
122 return reverb_model_estimator_.GetReverbFrequencyResponse();
123 }
124
Jesús de Vicente Peña02e9e442018-08-29 13:34:07 +0200125 // Returns whether the transition for going out of the initial stated has
126 // been triggered.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200127 bool TransitionTriggered() const {
128 return initial_state_.TransitionTriggered();
129 }
Per Åhgrena98c8072018-01-15 19:17:16 +0100130
peah522d71b2017-02-23 05:16:26 -0800131 // Updates the aec state.
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200132 // TODO(bugs.webrtc.org/10913): Handle multi-channel adaptive filter response.
133 // TODO(bugs.webrtc.org/10913): Compute multi-channel ERL, ERLE, and reverb.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200134 void Update(const absl::optional<DelayEstimate>& external_delay,
Per Åhgren3ab308f2018-02-21 08:46:03 +0100135 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700136 adaptive_filter_frequency_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100137 const std::vector<float>& adaptive_filter_impulse_response,
peah86afe9d2017-04-06 15:45:32 -0700138 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800139 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800140 const std::array<float, kFftLengthBy2Plus1>& Y2,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200141 rtc::ArrayView<const SubtractorOutput> subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800142
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200143 // Returns filter length in blocks.
144 int FilterLengthBlocks() const {
145 return filter_analyzer_.FilterLengthBlocks();
146 }
147
peah522d71b2017-02-23 05:16:26 -0800148 private:
149 static int instance_count_;
150 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200151 const EchoCanceller3Config config_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200152
153 // Class for controlling the transition from the intial state, which in turn
154 // controls when the filter parameters for the initial state should be used.
155 class InitialState {
156 public:
157 explicit InitialState(const EchoCanceller3Config& config);
158 // Resets the state to again begin in the initial state.
159 void Reset();
160
161 // Updates the state based on new data.
162 void Update(bool active_render, bool saturated_capture);
163
164 // Returns whether the initial state is active or not.
165 bool InitialStateActive() const { return initial_state_; }
166
167 // Returns that the transition from the initial state has was started.
168 bool TransitionTriggered() const { return transition_triggered_; }
169
170 private:
171 const bool conservative_initial_phase_;
172 const float initial_state_seconds_;
173 bool transition_triggered_ = false;
174 bool initial_state_ = true;
175 size_t strong_not_saturated_render_blocks_ = 0;
176 } initial_state_;
177
178 // Class for choosing the direct-path delay relative to the beginning of the
179 // filter, as well as any other data related to the delay used within
180 // AecState.
181 class FilterDelay {
182 public:
183 explicit FilterDelay(const EchoCanceller3Config& config);
184
185 // Returns whether an external delay has been reported to the AecState (from
186 // the delay estimator).
187 bool ExternalDelayReported() const { return external_delay_reported_; }
188
189 // Returns the delay in blocks relative to the beginning of the filter that
190 // corresponds to the direct path of the echo.
191 int DirectPathFilterDelay() const { return filter_delay_blocks_; }
192
193 // Updates the delay estimates based on new data.
194 void Update(const FilterAnalyzer& filter_analyzer,
195 const absl::optional<DelayEstimate>& external_delay,
196 size_t blocks_with_proper_filter_adaptation);
197
198 private:
Gustaf Ullberg9249fbf2019-03-14 11:24:54 +0100199 const int delay_headroom_samples_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200200 bool external_delay_reported_ = false;
201 int filter_delay_blocks_ = 0;
202 absl::optional<DelayEstimate> external_delay_;
203 } delay_state_;
204
205 // Class for detecting and toggling the transparent mode which causes the
206 // suppressor to apply no suppression.
207 class TransparentMode {
208 public:
209 explicit TransparentMode(const EchoCanceller3Config& config);
210
211 // Returns whether the transparent mode should be active.
212 bool Active() const { return transparency_activated_; }
213
214 // Resets the state of the detector.
215 void Reset();
216
217 // Updates the detection deciscion based on new data.
218 void Update(int filter_delay_blocks,
219 bool consistent_filter,
220 bool converged_filter,
221 bool diverged_filter,
222 bool active_render,
223 bool saturated_capture);
224
225 private:
226 const bool bounded_erl_;
227 const bool linear_and_stable_echo_path_;
228 size_t capture_block_counter_ = 0;
229 bool transparency_activated_ = false;
230 size_t active_blocks_since_sane_filter_;
231 bool sane_filter_observed_ = false;
232 bool finite_erl_recently_detected_ = false;
233 size_t non_converged_sequence_size_;
234 size_t diverged_sequence_size_ = 0;
235 size_t active_non_converged_sequence_size_ = 0;
236 size_t num_converged_blocks_ = 0;
237 bool recent_convergence_during_activity_ = false;
238 size_t strong_not_saturated_render_blocks_ = 0;
239 } transparent_state_;
240
241 // Class for analyzing how well the linear filter is, and can be expected to,
242 // perform on the current signals. The purpose of this is for using to
243 // select the echo suppression functionality as well as the input to the echo
244 // suppressor.
245 class FilteringQualityAnalyzer {
246 public:
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200247 FilteringQualityAnalyzer(const EchoCanceller3Config& config);
248
249 // Returns whether the the linear filter can be used for the echo
250 // canceller output.
251 bool LinearFilterUsable() const { return usable_linear_estimate_; }
252
253 // Resets the state of the analyzer.
254 void Reset();
255
256 // Updates the analysis based on new data.
257 void Update(bool active_render,
258 bool transparent_mode,
259 bool saturated_capture,
260 bool consistent_estimate_,
261 const absl::optional<DelayEstimate>& external_delay,
262 bool converged_filter);
263
264 private:
265 bool usable_linear_estimate_ = false;
266 size_t filter_update_blocks_since_reset_ = 0;
267 size_t filter_update_blocks_since_start_ = 0;
268 bool convergence_seen_ = false;
269 } filter_quality_state_;
270
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200271 // Class for detecting whether the echo is to be considered to be
272 // saturated.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200273 class SaturationDetector {
274 public:
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200275 // Returns whether the echo is to be considered saturated.
Nico Weber22f99252019-02-20 10:13:16 -0500276 bool SaturatedEcho() const { return saturated_echo_; }
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200277
278 // Updates the detection decision based on new data.
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200279 void Update(rtc::ArrayView<const std::vector<float>> x,
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200280 bool saturated_capture,
281 bool usable_linear_estimate,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200282 rtc::ArrayView<const SubtractorOutput> subtractor_output,
Per Åhgren3e7b7b12018-10-16 14:38:10 +0200283 float echo_path_gain);
284
285 private:
286 bool saturated_echo_ = false;
287 } saturation_detector_;
288
peah522d71b2017-02-23 05:16:26 -0800289 ErlEstimator erl_estimator_;
290 ErleEstimator erle_estimator_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200291 size_t strong_not_saturated_render_blocks_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100292 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800293 bool capture_signal_saturation_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +0100294 FilterAnalyzer filter_analyzer_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200295 absl::optional<DelayEstimate> external_delay_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200296 EchoAudibility echo_audibility_;
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +0200297 ReverbModelEstimator reverb_model_estimator_;
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +0200298 RenderReverbModel render_reverb_;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200299 std::vector<SubtractorOutputAnalyzer> subtractor_output_analyzers_;
peah522d71b2017-02-23 05:16:26 -0800300};
301
302} // namespace webrtc
303
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200304#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_