| 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 { |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 157 | res = res & Limit(&c->erle.min, 1.f, 100000.f); |
| 158 | res = res & Limit(&c->erle.max_l, 1.f, 100000.f); |
| 159 | res = res & Limit(&c->erle.max_h, 1.f, 100000.f); |
| 160 | if (c->erle.min > c->erle.max_l || c->erle.min > c->erle.max_h) { |
| 161 | c->erle.min = std::min(c->erle.max_l, c->erle.max_h); |
| 162 | res = false; |
| 163 | } |
| 164 | res = res & Limit(&c->erle.num_sections, 1, c->filter.refined.length_blocks); |
| 165 |
| 166 | res = res & Limit(&c->ep_strength.default_gain, 0.f, 1000000.f); |
| 167 | res = res & Limit(&c->ep_strength.default_len, -1.f, 1.f); |
| 168 |
| 169 | res = |
| 170 | res & Limit(&c->echo_audibility.low_render_limit, 0.f, 32768.f * 32768.f); |
| 171 | res = res & |
| 172 | Limit(&c->echo_audibility.normal_render_limit, 0.f, 32768.f * 32768.f); |
| 173 | res = res & Limit(&c->echo_audibility.floor_power, 0.f, 32768.f * 32768.f); |
| 174 | res = res & Limit(&c->echo_audibility.audibility_threshold_lf, 0.f, |
| 175 | 32768.f * 32768.f); |
| 176 | res = res & Limit(&c->echo_audibility.audibility_threshold_mf, 0.f, |
| 177 | 32768.f * 32768.f); |
| 178 | res = res & Limit(&c->echo_audibility.audibility_threshold_hf, 0.f, |
| 179 | 32768.f * 32768.f); |
| 180 |
| 181 | res = res & |
| 182 | Limit(&c->render_levels.active_render_limit, 0.f, 32768.f * 32768.f); |
| 183 | res = res & Limit(&c->render_levels.poor_excitation_render_limit, 0.f, |
| 184 | 32768.f * 32768.f); |
| 185 | res = res & Limit(&c->render_levels.poor_excitation_render_limit_ds8, 0.f, |
| 186 | 32768.f * 32768.f); |
| 187 |
| 188 | res = res & Limit(&c->echo_model.noise_floor_hold, 0, 1000); |
| 189 | res = res & Limit(&c->echo_model.min_noise_floor_power, 0, 2000000.f); |
| 190 | res = res & Limit(&c->echo_model.stationary_gate_slope, 0, 1000000.f); |
| 191 | res = res & Limit(&c->echo_model.noise_gate_power, 0, 1000000.f); |
| 192 | res = res & Limit(&c->echo_model.noise_gate_slope, 0, 1000000.f); |
| 193 | res = res & Limit(&c->echo_model.render_pre_window_size, 0, 100); |
| 194 | res = res & Limit(&c->echo_model.render_post_window_size, 0, 100); |
| 195 |
| 196 | res = res & Limit(&c->comfort_noise.noise_floor_dbfs, -200.f, 0.f); |
| 197 |
| 198 | res = res & Limit(&c->suppressor.nearend_average_blocks, 1, 5000); |
| 199 |
| 200 | res = res & |
| 201 | Limit(&c->suppressor.normal_tuning.mask_lf.enr_transparent, 0.f, 100.f); |
| 202 | res = res & |
| 203 | Limit(&c->suppressor.normal_tuning.mask_lf.enr_suppress, 0.f, 100.f); |
| 204 | res = res & |
| 205 | Limit(&c->suppressor.normal_tuning.mask_lf.emr_transparent, 0.f, 100.f); |
| 206 | res = res & |
| 207 | Limit(&c->suppressor.normal_tuning.mask_hf.enr_transparent, 0.f, 100.f); |
| 208 | res = res & |
| 209 | Limit(&c->suppressor.normal_tuning.mask_hf.enr_suppress, 0.f, 100.f); |
| 210 | res = res & |
| 211 | Limit(&c->suppressor.normal_tuning.mask_hf.emr_transparent, 0.f, 100.f); |
| 212 | res = res & Limit(&c->suppressor.normal_tuning.max_inc_factor, 0.f, 100.f); |
| 213 | res = res & Limit(&c->suppressor.normal_tuning.max_dec_factor_lf, 0.f, 100.f); |
| 214 |
| 215 | res = res & Limit(&c->suppressor.nearend_tuning.mask_lf.enr_transparent, 0.f, |
| 216 | 100.f); |
| 217 | res = res & |
| 218 | Limit(&c->suppressor.nearend_tuning.mask_lf.enr_suppress, 0.f, 100.f); |
| 219 | res = res & Limit(&c->suppressor.nearend_tuning.mask_lf.emr_transparent, 0.f, |
| 220 | 100.f); |
| 221 | res = res & Limit(&c->suppressor.nearend_tuning.mask_hf.enr_transparent, 0.f, |
| 222 | 100.f); |
| 223 | res = res & |
| 224 | Limit(&c->suppressor.nearend_tuning.mask_hf.enr_suppress, 0.f, 100.f); |
| 225 | res = res & Limit(&c->suppressor.nearend_tuning.mask_hf.emr_transparent, 0.f, |
| 226 | 100.f); |
| 227 | res = res & Limit(&c->suppressor.nearend_tuning.max_inc_factor, 0.f, 100.f); |
| 228 | res = |
| 229 | res & Limit(&c->suppressor.nearend_tuning.max_dec_factor_lf, 0.f, 100.f); |
| 230 |
| 231 | res = res & Limit(&c->suppressor.dominant_nearend_detection.enr_threshold, |
| 232 | 0.f, 1000000.f); |
| 233 | res = res & Limit(&c->suppressor.dominant_nearend_detection.snr_threshold, |
| 234 | 0.f, 1000000.f); |
| 235 | res = res & Limit(&c->suppressor.dominant_nearend_detection.hold_duration, 0, |
| 236 | 10000); |
| 237 | res = res & Limit(&c->suppressor.dominant_nearend_detection.trigger_threshold, |
| 238 | 0, 10000); |
| 239 |
| 240 | res = res & |
| 241 | Limit(&c->suppressor.subband_nearend_detection.nearend_average_blocks, |
| 242 | 1, 1024); |
| 243 | res = |
| 244 | res & Limit(&c->suppressor.subband_nearend_detection.subband1.low, 0, 65); |
| 245 | res = res & Limit(&c->suppressor.subband_nearend_detection.subband1.high, |
| 246 | c->suppressor.subband_nearend_detection.subband1.low, 65); |
| 247 | res = |
| 248 | res & Limit(&c->suppressor.subband_nearend_detection.subband2.low, 0, 65); |
| 249 | res = res & Limit(&c->suppressor.subband_nearend_detection.subband2.high, |
| 250 | c->suppressor.subband_nearend_detection.subband2.low, 65); |
| 251 | res = res & Limit(&c->suppressor.subband_nearend_detection.nearend_threshold, |
| 252 | 0.f, 1.e24f); |
| 253 | res = res & Limit(&c->suppressor.subband_nearend_detection.snr_threshold, 0.f, |
| 254 | 1.e24f); |
| 255 |
| 256 | res = res & Limit(&c->suppressor.high_bands_suppression.enr_threshold, 0.f, |
| 257 | 1000000.f); |
| 258 | res = res & Limit(&c->suppressor.high_bands_suppression.max_gain_during_echo, |
| 259 | 0.f, 1.f); |
| 260 | res = res & Limit(&c->suppressor.high_bands_suppression |
| 261 | .anti_howling_activation_threshold, |
| 262 | 0.f, 32768.f * 32768.f); |
| 263 | res = res & Limit(&c->suppressor.high_bands_suppression.anti_howling_gain, |
| 264 | 0.f, 1.f); |
| 265 |
| 266 | res = res & Limit(&c->suppressor.floor_first_increase, 0.f, 1000000.f); |
| 267 |
| 268 | return res; |
| 269 | } |
| 270 | } // namespace webrtc |
| 271 |