blob: 5c90128f29a28f8dee9112b2a0a6fbe8afcaca85 [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
Christian Schuldtf4e99db2018-03-01 11:32:50 +010014#include <math.h>
15
peah522d71b2017-02-23 05:16:26 -080016#include <algorithm>
17#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"
Per Åhgren12eb8582018-03-06 10:40:51 +010031#include "modules/audio_processing/aec3/suppression_gain_limiter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080033
34namespace webrtc {
35
36class ApmDataDumper;
37
38// Handles the state and the conditions for the echo removal functionality.
39class AecState {
40 public:
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020041 explicit AecState(const EchoCanceller3Config& config);
peah522d71b2017-02-23 05:16:26 -080042 ~AecState();
43
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010044 // Returns whether the echo subtractor can be used to determine the residual
45 // echo.
peah522d71b2017-02-23 05:16:26 -080046 bool UsableLinearEstimate() const { return usable_linear_estimate_; }
47
Per Åhgren5c532d32018-03-22 00:29:25 +010048 // Returns whether the echo subtractor output should be used as output.
49 bool UseLinearFilterOutput() const { return use_linear_filter_output_; }
50
51 // Returns the estimated echo path gain.
Per Åhgrenced31ba2018-05-09 11:48:49 +020052 float EchoPathGain() const { return filter_analyzer_.Gain(); }
peah522d71b2017-02-23 05:16:26 -080053
peah522d71b2017-02-23 05:16:26 -080054 // Returns whether the render signal is currently active.
Per Åhgren4b3bc0f2017-12-20 15:26:13 +010055 bool ActiveRender() const { return blocks_with_active_render_ > 200; }
peahebe77782017-02-27 07:29:21 -080056
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020057 // Returns the appropriate scaling of the residual echo to match the
58 // audibility.
59 void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const {
60 echo_audibility_.GetResidualEchoScaling(residual_scaling);
61 }
62
63 // Returns whether the stationary properties of the signals are used in the
64 // aec.
Per Åhgren90e3fbd2018-05-16 15:25:04 +020065 bool UseStationaryProperties() const { return use_stationary_properties_; }
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020066
peah522d71b2017-02-23 05:16:26 -080067 // Returns the ERLE.
68 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
69 return erle_estimator_.Erle();
70 }
71
Gustaf Ullberg6c618c72018-06-28 14:21:16 +020072 // Returns any uncertainty in the ERLE estimate.
73 absl::optional<float> ErleUncertainty() const {
74 if (allow_linear_mode_with_diverged_filter_ && diverged_linear_filter_) {
75 return 10.f;
76 }
77 return absl::nullopt;
78 }
79
Gustaf Ullberg332150d2017-11-22 14:17:39 +010080 // Returns the time-domain ERLE.
81 float ErleTimeDomain() const { return erle_estimator_.ErleTimeDomain(); }
82
peah522d71b2017-02-23 05:16:26 -080083 // Returns the ERL.
84 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
85 return erl_estimator_.Erl();
86 }
87
Gustaf Ullberg332150d2017-11-22 14:17:39 +010088 // Returns the time-domain ERL.
89 float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
90
peah522d71b2017-02-23 05:16:26 -080091 // Returns the delay estimate based on the linear filter.
Per Åhgren5c532d32018-03-22 00:29:25 +010092 int FilterDelayBlocks() const { return filter_delay_blocks_; }
93
94 // Returns the internal delay estimate based on the linear filter.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020095 absl::optional<int> InternalDelay() const { return internal_delay_; }
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.
101 bool SaturatedEcho() const { return echo_saturation_; }
102
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
109 bool TransparentMode() const { return transparent_mode_; }
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.
peah29103572017-07-11 02:54:02 -0700115 float ReverbDecay() const { return reverb_decay_; }
peah89420452017-04-07 06:13:39 -0700116
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100117 // Returns the upper limit for the echo suppression gain.
Per Åhgren12eb8582018-03-06 10:40:51 +0100118 float SuppressionGainLimit() const {
119 return suppression_gain_limiter_.Limit();
120 }
peah6d822ad2017-04-10 13:52:14 -0700121
Jesús de Vicente Peñadd092872018-05-25 16:55:11 +0200122 // Returns whether the suppression gain limiter is active.
123 bool IsSuppressionGainLimitActive() const {
124 return suppression_gain_limiter_.IsActive();
125 }
126
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100127 // Returns whether the linear filter should have been able to properly adapt.
128 bool FilterHasHadTimeToConverge() const {
129 return filter_has_had_time_to_converge_;
130 }
Per Åhgren1b4059e2017-10-15 20:19:21 +0200131
Per Åhgrena98c8072018-01-15 19:17:16 +0100132 // Returns whether the filter adaptation is still in the initial state.
133 bool InitialState() const { return initial_state_; }
134
peah522d71b2017-02-23 05:16:26 -0800135 // Updates the aec state.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200136 void Update(const absl::optional<DelayEstimate>& external_delay,
Per Åhgren3ab308f2018-02-21 08:46:03 +0100137 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -0700138 adaptive_filter_frequency_response,
Per Åhgren09a718a2017-12-11 22:28:45 +0100139 const std::vector<float>& adaptive_filter_impulse_response,
Per Åhgren1b4059e2017-10-15 20:19:21 +0200140 bool converged_filter,
Per Åhgren5c532d32018-03-22 00:29:25 +0100141 bool diverged_filter,
peah86afe9d2017-04-06 15:45:32 -0700142 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800143 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800144 const std::array<float, kFftLengthBy2Plus1>& Y2,
Per Åhgren5c532d32018-03-22 00:29:25 +0100145 const std::array<float, kBlockSize>& s);
peah522d71b2017-02-23 05:16:26 -0800146
Jesús de Vicente Peñae58bd8a2018-06-26 17:19:15 +0200147 // Returns the tail freq. response of the linear filter.
148 rtc::ArrayView<const float> GetFreqRespTail() const {
149 return filter_analyzer_.GetFreqRespTail();
150 }
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +0200151
152 // Returns filter length in blocks.
153 int FilterLengthBlocks() const {
154 return filter_analyzer_.FilterLengthBlocks();
155 }
156
peah522d71b2017-02-23 05:16:26 -0800157 private:
Per Åhgren09a718a2017-12-11 22:28:45 +0100158 void UpdateReverb(const std::vector<float>& impulse_response);
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100159 bool DetectActiveRender(rtc::ArrayView<const float> x) const;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100160 void UpdateSuppressorGainLimit(bool render_activity);
Per Åhgren31122d62018-04-10 16:33:55 +0200161 bool DetectEchoSaturation(rtc::ArrayView<const float> x,
162 float echo_path_gain);
peah29103572017-07-11 02:54:02 -0700163
peah522d71b2017-02-23 05:16:26 -0800164 static int instance_count_;
165 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200166 const EchoCanceller3Config config_;
Per Åhgrend18e87e2018-05-09 12:07:26 +0200167 const bool allow_transparent_mode_;
Per Åhgren90e3fbd2018-05-16 15:25:04 +0200168 const bool use_stationary_properties_;
Per Åhgren05d8ee12018-06-07 15:59:59 +0200169 const bool enforce_delay_after_realignment_;
Gustaf Ullberg6c618c72018-06-28 14:21:16 +0200170 const bool allow_linear_mode_with_diverged_filter_;
peah522d71b2017-02-23 05:16:26 -0800171 ErlEstimator erl_estimator_;
172 ErleEstimator erle_estimator_;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200173 size_t capture_block_counter_ = 0;
Per Åhgren5c532d32018-03-22 00:29:25 +0100174 size_t blocks_since_reset_ = 0;
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100175 size_t blocks_with_proper_filter_adaptation_ = 0;
176 size_t blocks_with_active_render_ = 0;
peah522d71b2017-02-23 05:16:26 -0800177 bool usable_linear_estimate_ = false;
Gustaf Ullberg6c618c72018-06-28 14:21:16 +0200178 bool diverged_linear_filter_ = false;
peah522d71b2017-02-23 05:16:26 -0800179 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700180 bool echo_saturation_ = false;
Per Åhgren1b4059e2017-10-15 20:19:21 +0200181 bool transparent_mode_ = false;
peahe52a2032017-04-19 09:03:40 -0700182 bool render_received_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +0100183 int filter_delay_blocks_ = 0;
peah86afe9d2017-04-06 15:45:32 -0700184 size_t blocks_since_last_saturation_ = 1000;
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100185 float tail_energy_ = 0.f;
186 float accumulated_nz_ = 0.f;
187 float accumulated_nn_ = 0.f;
188 float accumulated_count_ = 0.f;
189 size_t current_reverb_decay_section_ = 0;
190 size_t num_reverb_decay_sections_ = 0;
191 size_t num_reverb_decay_sections_next_ = 0;
192 bool found_end_of_reverb_decay_ = false;
193 bool main_filter_is_adapting_ = true;
194 std::array<float, kMaxAdaptiveFilterLength> block_energies_;
Per Åhgren09a718a2017-12-11 22:28:45 +0100195 std::vector<float> max_render_;
Christian Schuldtf4e99db2018-03-01 11:32:50 +0100196 float reverb_decay_ = fabsf(config_.ep_strength.default_len);
Per Åhgren4b3bc0f2017-12-20 15:26:13 +0100197 bool filter_has_had_time_to_converge_ = false;
Per Åhgrena98c8072018-01-15 19:17:16 +0100198 bool initial_state_ = true;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100199 const float gain_rampup_increase_;
Per Åhgren12eb8582018-03-06 10:40:51 +0100200 SuppressionGainUpperLimiter suppression_gain_limiter_;
Per Åhgren5c532d32018-03-22 00:29:25 +0100201 FilterAnalyzer filter_analyzer_;
202 bool use_linear_filter_output_ = false;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200203 absl::optional<int> internal_delay_;
Per Åhgren5c532d32018-03-22 00:29:25 +0100204 size_t diverged_blocks_ = 0;
205 bool filter_should_have_converged_ = false;
206 size_t blocks_since_converged_filter_;
207 size_t active_blocks_since_consistent_filter_estimate_;
208 bool converged_filter_seen_ = false;
209 bool consistent_filter_seen_ = false;
210 bool external_delay_seen_ = false;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200211 absl::optional<DelayEstimate> external_delay_;
Per Åhgren05d8ee12018-06-07 15:59:59 +0200212 size_t frames_since_external_delay_change_ = 0;
Per Åhgrenf3e2bf12018-03-22 10:15:59 +0100213 size_t converged_filter_count_ = 0;
214 bool finite_erl_ = false;
Per Åhgren8131eb02018-03-28 18:13:41 +0200215 size_t active_blocks_since_converged_filter_ = 0;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +0200216 EchoAudibility echo_audibility_;
peah8cee56f2017-08-24 22:36:53 -0700217 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
peah522d71b2017-02-23 05:16:26 -0800218};
219
220} // namespace webrtc
221
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200222#endif // MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_