xref: /aosp_15_r20/external/webrtc/api/audio/echo_canceller3_config.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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 #include "api/audio/echo_canceller3_config.h"
11 
12 #include <algorithm>
13 #include <cmath>
14 
15 #include "rtc_base/checks.h"
16 #include "rtc_base/numerics/safe_minmax.h"
17 
18 namespace webrtc {
19 namespace {
Limit(float * value,float min,float max)20 bool Limit(float* value, float min, float max) {
21   float clamped = rtc::SafeClamp(*value, min, max);
22   clamped = std::isfinite(clamped) ? clamped : min;
23   bool res = *value == clamped;
24   *value = clamped;
25   return res;
26 }
27 
Limit(size_t * value,size_t min,size_t max)28 bool Limit(size_t* value, size_t min, size_t max) {
29   size_t clamped = rtc::SafeClamp(*value, min, max);
30   bool res = *value == clamped;
31   *value = clamped;
32   return res;
33 }
34 
Limit(int * value,int min,int max)35 bool Limit(int* value, int min, int max) {
36   int clamped = rtc::SafeClamp(*value, min, max);
37   bool res = *value == clamped;
38   *value = clamped;
39   return res;
40 }
41 
FloorLimit(size_t * value,size_t min)42 bool FloorLimit(size_t* value, size_t min) {
43   size_t clamped = *value >= min ? *value : min;
44   bool res = *value == clamped;
45   *value = clamped;
46   return res;
47 }
48 
49 }  // namespace
50 
51 EchoCanceller3Config::EchoCanceller3Config() = default;
52 EchoCanceller3Config::EchoCanceller3Config(const EchoCanceller3Config& e) =
53     default;
54 EchoCanceller3Config& EchoCanceller3Config::operator=(
55     const EchoCanceller3Config& e) = default;
56 EchoCanceller3Config::Delay::Delay() = default;
57 EchoCanceller3Config::Delay::Delay(const EchoCanceller3Config::Delay& e) =
58     default;
59 EchoCanceller3Config::Delay& EchoCanceller3Config::Delay::operator=(
60     const Delay& e) = default;
61 
62 EchoCanceller3Config::EchoModel::EchoModel() = default;
63 EchoCanceller3Config::EchoModel::EchoModel(
64     const EchoCanceller3Config::EchoModel& e) = default;
65 EchoCanceller3Config::EchoModel& EchoCanceller3Config::EchoModel::operator=(
66     const EchoModel& e) = default;
67 
68 EchoCanceller3Config::Suppressor::Suppressor() = default;
69 EchoCanceller3Config::Suppressor::Suppressor(
70     const EchoCanceller3Config::Suppressor& e) = default;
71 EchoCanceller3Config::Suppressor& EchoCanceller3Config::Suppressor::operator=(
72     const Suppressor& e) = default;
73 
MaskingThresholds(float enr_transparent,float enr_suppress,float emr_transparent)74 EchoCanceller3Config::Suppressor::MaskingThresholds::MaskingThresholds(
75     float enr_transparent,
76     float enr_suppress,
77     float emr_transparent)
78     : enr_transparent(enr_transparent),
79       enr_suppress(enr_suppress),
80       emr_transparent(emr_transparent) {}
81 EchoCanceller3Config::Suppressor::MaskingThresholds::MaskingThresholds(
82     const EchoCanceller3Config::Suppressor::MaskingThresholds& e) = default;
83 EchoCanceller3Config::Suppressor::MaskingThresholds&
84 EchoCanceller3Config::Suppressor::MaskingThresholds::operator=(
85     const MaskingThresholds& e) = default;
86 
Tuning(MaskingThresholds mask_lf,MaskingThresholds mask_hf,float max_inc_factor,float max_dec_factor_lf)87 EchoCanceller3Config::Suppressor::Tuning::Tuning(MaskingThresholds mask_lf,
88                                                  MaskingThresholds mask_hf,
89                                                  float max_inc_factor,
90                                                  float max_dec_factor_lf)
91     : mask_lf(mask_lf),
92       mask_hf(mask_hf),
93       max_inc_factor(max_inc_factor),
94       max_dec_factor_lf(max_dec_factor_lf) {}
95 EchoCanceller3Config::Suppressor::Tuning::Tuning(
96     const EchoCanceller3Config::Suppressor::Tuning& e) = default;
97 EchoCanceller3Config::Suppressor::Tuning&
98 EchoCanceller3Config::Suppressor::Tuning::operator=(const Tuning& e) = default;
99 
Validate(EchoCanceller3Config * config)100 bool EchoCanceller3Config::Validate(EchoCanceller3Config* config) {
101   RTC_DCHECK(config);
102   EchoCanceller3Config* c = config;
103   bool res = true;
104 
105   if (c->delay.down_sampling_factor != 4 &&
106       c->delay.down_sampling_factor != 8) {
107     c->delay.down_sampling_factor = 4;
108     res = false;
109   }
110 
111   res = res & Limit(&c->delay.default_delay, 0, 5000);
112   res = res & Limit(&c->delay.num_filters, 0, 5000);
113   res = res & Limit(&c->delay.delay_headroom_samples, 0, 5000);
114   res = res & Limit(&c->delay.hysteresis_limit_blocks, 0, 5000);
115   res = res & Limit(&c->delay.fixed_capture_delay_samples, 0, 5000);
116   res = res & Limit(&c->delay.delay_estimate_smoothing, 0.f, 1.f);
117   res = res & Limit(&c->delay.delay_candidate_detection_threshold, 0.f, 1.f);
118   res = res & Limit(&c->delay.delay_selection_thresholds.initial, 1, 250);
119   res = res & Limit(&c->delay.delay_selection_thresholds.converged, 1, 250);
120 
121   res = res & FloorLimit(&c->filter.refined.length_blocks, 1);
122   res = res & Limit(&c->filter.refined.leakage_converged, 0.f, 1000.f);
123   res = res & Limit(&c->filter.refined.leakage_diverged, 0.f, 1000.f);
124   res = res & Limit(&c->filter.refined.error_floor, 0.f, 1000.f);
125   res = res & Limit(&c->filter.refined.error_ceil, 0.f, 100000000.f);
126   res = res & Limit(&c->filter.refined.noise_gate, 0.f, 100000000.f);
127 
128   res = res & FloorLimit(&c->filter.refined_initial.length_blocks, 1);
129   res = res & Limit(&c->filter.refined_initial.leakage_converged, 0.f, 1000.f);
130   res = res & Limit(&c->filter.refined_initial.leakage_diverged, 0.f, 1000.f);
131   res = res & Limit(&c->filter.refined_initial.error_floor, 0.f, 1000.f);
132   res = res & Limit(&c->filter.refined_initial.error_ceil, 0.f, 100000000.f);
133   res = res & Limit(&c->filter.refined_initial.noise_gate, 0.f, 100000000.f);
134 
135   if (c->filter.refined.length_blocks <
136       c->filter.refined_initial.length_blocks) {
137     c->filter.refined_initial.length_blocks = c->filter.refined.length_blocks;
138     res = false;
139   }
140 
141   res = res & FloorLimit(&c->filter.coarse.length_blocks, 1);
142   res = res & Limit(&c->filter.coarse.rate, 0.f, 1.f);
143   res = res & Limit(&c->filter.coarse.noise_gate, 0.f, 100000000.f);
144 
145   res = res & FloorLimit(&c->filter.coarse_initial.length_blocks, 1);
146   res = res & Limit(&c->filter.coarse_initial.rate, 0.f, 1.f);
147   res = res & Limit(&c->filter.coarse_initial.noise_gate, 0.f, 100000000.f);
148 
149   if (c->filter.coarse.length_blocks < c->filter.coarse_initial.length_blocks) {
150     c->filter.coarse_initial.length_blocks = c->filter.coarse.length_blocks;
151     res = false;
152   }
153 
154   res = res & Limit(&c->filter.config_change_duration_blocks, 0, 100000);
155   res = res & Limit(&c->filter.initial_state_seconds, 0.f, 100.f);
156   res = res & Limit(&c->filter.coarse_reset_hangover_blocks, 0, 250000);
157 
158   res = res & Limit(&c->erle.min, 1.f, 100000.f);
159   res = res & Limit(&c->erle.max_l, 1.f, 100000.f);
160   res = res & Limit(&c->erle.max_h, 1.f, 100000.f);
161   if (c->erle.min > c->erle.max_l || c->erle.min > c->erle.max_h) {
162     c->erle.min = std::min(c->erle.max_l, c->erle.max_h);
163     res = false;
164   }
165   res = res & Limit(&c->erle.num_sections, 1, c->filter.refined.length_blocks);
166 
167   res = res & Limit(&c->ep_strength.default_gain, 0.f, 1000000.f);
168   res = res & Limit(&c->ep_strength.default_len, -1.f, 1.f);
169   res = res & Limit(&c->ep_strength.nearend_len, -1.0f, 1.0f);
170 
171   res =
172       res & Limit(&c->echo_audibility.low_render_limit, 0.f, 32768.f * 32768.f);
173   res = res &
174         Limit(&c->echo_audibility.normal_render_limit, 0.f, 32768.f * 32768.f);
175   res = res & Limit(&c->echo_audibility.floor_power, 0.f, 32768.f * 32768.f);
176   res = res & Limit(&c->echo_audibility.audibility_threshold_lf, 0.f,
177                     32768.f * 32768.f);
178   res = res & Limit(&c->echo_audibility.audibility_threshold_mf, 0.f,
179                     32768.f * 32768.f);
180   res = res & Limit(&c->echo_audibility.audibility_threshold_hf, 0.f,
181                     32768.f * 32768.f);
182 
183   res = res &
184         Limit(&c->render_levels.active_render_limit, 0.f, 32768.f * 32768.f);
185   res = res & Limit(&c->render_levels.poor_excitation_render_limit, 0.f,
186                     32768.f * 32768.f);
187   res = res & Limit(&c->render_levels.poor_excitation_render_limit_ds8, 0.f,
188                     32768.f * 32768.f);
189 
190   res = res & Limit(&c->echo_model.noise_floor_hold, 0, 1000);
191   res = res & Limit(&c->echo_model.min_noise_floor_power, 0, 2000000.f);
192   res = res & Limit(&c->echo_model.stationary_gate_slope, 0, 1000000.f);
193   res = res & Limit(&c->echo_model.noise_gate_power, 0, 1000000.f);
194   res = res & Limit(&c->echo_model.noise_gate_slope, 0, 1000000.f);
195   res = res & Limit(&c->echo_model.render_pre_window_size, 0, 100);
196   res = res & Limit(&c->echo_model.render_post_window_size, 0, 100);
197 
198   res = res & Limit(&c->comfort_noise.noise_floor_dbfs, -200.f, 0.f);
199 
200   res = res & Limit(&c->suppressor.nearend_average_blocks, 1, 5000);
201 
202   res = res &
203         Limit(&c->suppressor.normal_tuning.mask_lf.enr_transparent, 0.f, 100.f);
204   res = res &
205         Limit(&c->suppressor.normal_tuning.mask_lf.enr_suppress, 0.f, 100.f);
206   res = res &
207         Limit(&c->suppressor.normal_tuning.mask_lf.emr_transparent, 0.f, 100.f);
208   res = res &
209         Limit(&c->suppressor.normal_tuning.mask_hf.enr_transparent, 0.f, 100.f);
210   res = res &
211         Limit(&c->suppressor.normal_tuning.mask_hf.enr_suppress, 0.f, 100.f);
212   res = res &
213         Limit(&c->suppressor.normal_tuning.mask_hf.emr_transparent, 0.f, 100.f);
214   res = res & Limit(&c->suppressor.normal_tuning.max_inc_factor, 0.f, 100.f);
215   res = res & Limit(&c->suppressor.normal_tuning.max_dec_factor_lf, 0.f, 100.f);
216 
217   res = res & Limit(&c->suppressor.nearend_tuning.mask_lf.enr_transparent, 0.f,
218                     100.f);
219   res = res &
220         Limit(&c->suppressor.nearend_tuning.mask_lf.enr_suppress, 0.f, 100.f);
221   res = res & Limit(&c->suppressor.nearend_tuning.mask_lf.emr_transparent, 0.f,
222                     100.f);
223   res = res & Limit(&c->suppressor.nearend_tuning.mask_hf.enr_transparent, 0.f,
224                     100.f);
225   res = res &
226         Limit(&c->suppressor.nearend_tuning.mask_hf.enr_suppress, 0.f, 100.f);
227   res = res & Limit(&c->suppressor.nearend_tuning.mask_hf.emr_transparent, 0.f,
228                     100.f);
229   res = res & Limit(&c->suppressor.nearend_tuning.max_inc_factor, 0.f, 100.f);
230   res =
231       res & Limit(&c->suppressor.nearend_tuning.max_dec_factor_lf, 0.f, 100.f);
232 
233   res = res & Limit(&c->suppressor.last_permanent_lf_smoothing_band, 0, 64);
234   res = res & Limit(&c->suppressor.last_lf_smoothing_band, 0, 64);
235   res = res & Limit(&c->suppressor.last_lf_band, 0, 63);
236   res = res &
237         Limit(&c->suppressor.first_hf_band, c->suppressor.last_lf_band + 1, 64);
238 
239   res = res & Limit(&c->suppressor.dominant_nearend_detection.enr_threshold,
240                     0.f, 1000000.f);
241   res = res & Limit(&c->suppressor.dominant_nearend_detection.snr_threshold,
242                     0.f, 1000000.f);
243   res = res & Limit(&c->suppressor.dominant_nearend_detection.hold_duration, 0,
244                     10000);
245   res = res & Limit(&c->suppressor.dominant_nearend_detection.trigger_threshold,
246                     0, 10000);
247 
248   res = res &
249         Limit(&c->suppressor.subband_nearend_detection.nearend_average_blocks,
250               1, 1024);
251   res =
252       res & Limit(&c->suppressor.subband_nearend_detection.subband1.low, 0, 65);
253   res = res & Limit(&c->suppressor.subband_nearend_detection.subband1.high,
254                     c->suppressor.subband_nearend_detection.subband1.low, 65);
255   res =
256       res & Limit(&c->suppressor.subband_nearend_detection.subband2.low, 0, 65);
257   res = res & Limit(&c->suppressor.subband_nearend_detection.subband2.high,
258                     c->suppressor.subband_nearend_detection.subband2.low, 65);
259   res = res & Limit(&c->suppressor.subband_nearend_detection.nearend_threshold,
260                     0.f, 1.e24f);
261   res = res & Limit(&c->suppressor.subband_nearend_detection.snr_threshold, 0.f,
262                     1.e24f);
263 
264   res = res & Limit(&c->suppressor.high_bands_suppression.enr_threshold, 0.f,
265                     1000000.f);
266   res = res & Limit(&c->suppressor.high_bands_suppression.max_gain_during_echo,
267                     0.f, 1.f);
268   res = res & Limit(&c->suppressor.high_bands_suppression
269                          .anti_howling_activation_threshold,
270                     0.f, 32768.f * 32768.f);
271   res = res & Limit(&c->suppressor.high_bands_suppression.anti_howling_gain,
272                     0.f, 1.f);
273 
274   res = res & Limit(&c->suppressor.floor_first_increase, 0.f, 1000000.f);
275 
276   return res;
277 }
278 }  // namespace webrtc
279