Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: webrtc/modules/audio_processing/aec3/residual_echo_estimator.cc

Issue 2678423005: Finalization of the first version of EchoCanceller 3 (Closed)
Patch Set: Fixed compilation error Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
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
11 #include "webrtc/modules/audio_processing/aec3/residual_echo_estimator.h"
12
13 #include <math.h>
14 #include <vector>
15
16 #include "webrtc/base/checks.h"
17
18 namespace webrtc {
19 namespace {
20
21 constexpr float kSaturationLeakageFactor = 10.f;
22 constexpr size_t kSaturationLeakageBlocks = 10;
23
24 // Estimates the residual echo power when there is no detection correlation
25 // between the render and capture signals.
26 void InfiniteErlPowerEstimate(
27 size_t active_render_counter,
28 size_t blocks_since_last_saturation,
29 const std::array<float, kFftLengthBy2Plus1>& S2_fallback,
30 std::array<float, kFftLengthBy2Plus1>* R2) {
31 if (active_render_counter > 5 * 250) {
32 // After an amount of active render samples for which an echo should have
33 // been detected in the capture signal if the ERL was not infinite, set the
34 // residual echo to 0.
35 R2->fill(0.f);
36 } else {
37 // Before certainty has been reached about the presence of echo, use the
38 // fallback echo power estimate as the residual echo estimate. Add a leakage
39 // factor when there is saturation.
40 std::copy(S2_fallback.begin(), S2_fallback.end(), R2->begin());
41 if (blocks_since_last_saturation < kSaturationLeakageBlocks) {
42 std::for_each(R2->begin(), R2->end(),
43 [](float& a) { a *= kSaturationLeakageFactor; });
44 }
45 }
46 }
47
48 // Estimates the echo power in an half-duplex manner.
49 void HalfDuplexPowerEstimate(bool active_render,
50 const std::array<float, kFftLengthBy2Plus1>& Y2,
51 std::array<float, kFftLengthBy2Plus1>* R2) {
52 // Set the residual echo power to the power of the capture signal.
53 if (active_render) {
54 std::copy(Y2.begin(), Y2.end(), R2->begin());
55 } else {
56 R2->fill(0.f);
57 }
58 }
59
60 // Estimates the residual echo power based on gains.
61 void GainBasedPowerEstimate(
62 size_t external_delay,
63 const FftBuffer& X_buffer,
64 size_t blocks_since_last_saturation,
65 const std::array<bool, kFftLengthBy2Plus1>& bands_with_reliable_filter,
66 const std::array<float, kFftLengthBy2Plus1>& echo_path_gain,
67 const std::array<float, kFftLengthBy2Plus1>& S2_fallback,
68 std::array<float, kFftLengthBy2Plus1>* R2) {
69 const auto& X2 = X_buffer.Spectrum(external_delay);
70
71 // Base the residual echo power on gain of the linear echo path estimate if
72 // that is reliable, otherwise use the fallback echo path estimate. Add a
73 // leakage factor when there is saturation.
74 for (size_t k = 0; k < R2->size(); ++k) {
75 (*R2)[k] = bands_with_reliable_filter[k] ? echo_path_gain[k] * X2[k]
76 : S2_fallback[k];
77 }
78 if (blocks_since_last_saturation < kSaturationLeakageBlocks) {
79 std::for_each(R2->begin(), R2->end(),
80 [](float& a) { a *= kSaturationLeakageFactor; });
81 }
82 }
83
84 // Estimates the residual echo power based on the linear echo path.
85 void ErleBasedPowerEstimate(
86 bool headset_detected,
87 const FftBuffer& X_buffer,
88 bool using_subtractor_output,
89 size_t linear_filter_based_delay,
90 size_t blocks_since_last_saturation,
91 bool poorly_aligned_filter,
92 const std::array<bool, kFftLengthBy2Plus1>& bands_with_reliable_filter,
93 const std::array<float, kFftLengthBy2Plus1>& echo_path_gain,
94 const std::array<float, kFftLengthBy2Plus1>& S2_fallback,
95 const std::array<float, kFftLengthBy2Plus1>& S2_linear,
96 const std::array<float, kFftLengthBy2Plus1>& Y2,
97 const std::array<float, kFftLengthBy2Plus1>& erle,
98 const std::array<float, kFftLengthBy2Plus1>& erl,
99 std::array<float, kFftLengthBy2Plus1>* R2) {
100 // Residual echo power after saturation.
101 if (blocks_since_last_saturation < kSaturationLeakageBlocks) {
102 for (size_t k = 0; k < R2->size(); ++k) {
103 (*R2)[k] = kSaturationLeakageFactor *
104 (bands_with_reliable_filter[k] && using_subtractor_output
105 ? S2_linear[k]
106 : std::min(S2_fallback[k], Y2[k]));
107 }
108 return;
109 }
110
111 // Residual echo power when a headset is used.
112 if (headset_detected) {
113 const auto& X2 = X_buffer.Spectrum(linear_filter_based_delay);
114 for (size_t k = 0; k < R2->size(); ++k) {
115 RTC_DCHECK_LT(0.f, erle[k]);
116 (*R2)[k] = bands_with_reliable_filter[k] && using_subtractor_output
117 ? S2_linear[k] / erle[k]
118 : std::min(S2_fallback[k], Y2[k]);
119 (*R2)[k] = std::min((*R2)[k], X2[k] * erl[k]);
120 }
121 return;
122 }
123
124 // Residual echo power when the adaptive filter is poorly aligned.
125 if (poorly_aligned_filter) {
126 for (size_t k = 0; k < R2->size(); ++k) {
127 (*R2)[k] = bands_with_reliable_filter[k] && using_subtractor_output
128 ? S2_linear[k]
129 : std::min(S2_fallback[k], Y2[k]);
130 }
131 return;
132 }
133
134 // Residual echo power when there is no recent saturation, no headset detected
135 // and when the adaptive filter is well aligned.
136 for (size_t k = 0; k < R2->size(); ++k) {
137 RTC_DCHECK_LT(0.f, erle[k]);
138 const auto& X2 = X_buffer.Spectrum(linear_filter_based_delay);
139 (*R2)[k] = bands_with_reliable_filter[k] && using_subtractor_output
140 ? S2_linear[k] / erle[k]
141 : std::min(echo_path_gain[k] * X2[k], Y2[k]);
142 }
143 }
144
145 } // namespace
146
147 ResidualEchoEstimator::ResidualEchoEstimator() {
148 echo_path_gain_.fill(0.f);
149 }
150
151 ResidualEchoEstimator::~ResidualEchoEstimator() = default;
152
153 void ResidualEchoEstimator::Estimate(
154 bool using_subtractor_output,
155 const AecState& aec_state,
156 const FftBuffer& X_buffer,
157 const std::vector<std::array<float, kFftLengthBy2Plus1>>& H2,
158 const std::array<float, kFftLengthBy2Plus1>& E2_main,
159 const std::array<float, kFftLengthBy2Plus1>& E2_shadow,
160 const std::array<float, kFftLengthBy2Plus1>& S2_linear,
161 const std::array<float, kFftLengthBy2Plus1>& S2_fallback,
162 const std::array<float, kFftLengthBy2Plus1>& Y2,
163 std::array<float, kFftLengthBy2Plus1>* R2) {
164 RTC_DCHECK(R2);
165 const rtc::Optional<size_t>& linear_filter_based_delay =
166 aec_state.FilterDelay();
167
168 // Update the echo path gain.
169 if (linear_filter_based_delay) {
170 std::copy(H2[*linear_filter_based_delay].begin(),
171 H2[*linear_filter_based_delay].end(), echo_path_gain_.begin());
172 }
173
174 // Counts the blocks since saturation.
175 if (aec_state.SaturatedCapture()) {
176 blocks_since_last_saturation_ = 0;
177 } else {
178 ++blocks_since_last_saturation_;
179 }
180
181 // Counts the number of active render blocks that are in a row.
182 if (aec_state.ActiveRender()) {
183 ++active_render_counter_;
184 }
185
186 const auto& bands_with_reliable_filter = aec_state.BandsWithReliableFilter();
187
188 if (aec_state.UsableLinearEstimate()) {
189 // Residual echo power estimation when the adaptive filter is reliable.
190 RTC_DCHECK(linear_filter_based_delay);
191 ErleBasedPowerEstimate(
192 aec_state.HeadsetDetected(), X_buffer, using_subtractor_output,
193 *linear_filter_based_delay, blocks_since_last_saturation_,
194 aec_state.PoorlyAlignedFilter(), bands_with_reliable_filter,
195 echo_path_gain_, S2_fallback, S2_linear, Y2, aec_state.Erle(),
196 aec_state.Erl(), R2);
197 } else if (aec_state.ModelBasedAecFeasible()) {
198 // Residual echo power when the adaptive filter is not reliable but still an
199 // external echo path delay is provided (and hence can be estimated).
200 RTC_DCHECK(aec_state.ExternalDelay());
201 GainBasedPowerEstimate(
202 *aec_state.ExternalDelay(), X_buffer, blocks_since_last_saturation_,
203 bands_with_reliable_filter, echo_path_gain_, S2_fallback, R2);
204 } else if (aec_state.EchoLeakageDetected()) {
205 // Residual echo power when an external residual echo detection algorithm
206 // has deemed the echo canceller to leak echoes.
207 HalfDuplexPowerEstimate(aec_state.ActiveRender(), Y2, R2);
208 } else {
209 // Residual echo power when none of the other cases are fulfilled.
210 InfiniteErlPowerEstimate(active_render_counter_,
211 blocks_since_last_saturation_, S2_fallback, R2);
212 }
213 }
214
215 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698