andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_processing/aecm/aecm_core.h" |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 12 | |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | #include <stdlib.h> |
| 15 | |
peah | 2704512 | 2016-04-11 05:38:14 | [diff] [blame] | 16 | extern "C" { |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "common_audio/ring_buffer.h" |
| 18 | #include "common_audio/signal_processing/include/real_fft.h" |
peah | 2704512 | 2016-04-11 05:38:14 | [diff] [blame] | 19 | } |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "modules/audio_processing/aecm/echo_control_mobile.h" |
| 21 | #include "modules/audio_processing/utility/delay_estimator_wrapper.h" |
peah | bdb7af6 | 2016-04-12 21:47:40 | [diff] [blame] | 22 | extern "C" { |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "system_wrappers/include/cpu_features_wrapper.h" |
peah | 2704512 | 2016-04-11 05:38:14 | [diff] [blame] | 24 | } |
kwiberg | 9e2be5f | 2016-09-14 12:23:22 | [diff] [blame] | 25 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 26 | #include "rtc_base/checks.h" |
Karl Wiberg | e40468b | 2017-11-22 09:42:26 | [diff] [blame] | 27 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 28 | #include "rtc_base/sanitizer.h" |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 29 | |
| 30 | // Square root of Hanning window in Q14. |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 31 | static const ALIGN8_BEG int16_t WebRtcAecm_kSqrtHanning[] ALIGN8_END = { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 32 | 0, 399, 798, 1196, 1594, 1990, 2386, 2780, 3172, 3562, 3951, |
| 33 | 4337, 4720, 5101, 5478, 5853, 6224, 6591, 6954, 7313, 7668, 8019, |
| 34 | 8364, 8705, 9040, 9370, 9695, 10013, 10326, 10633, 10933, 11227, 11514, |
| 35 | 11795, 12068, 12335, 12594, 12845, 13089, 13325, 13553, 13773, 13985, 14189, |
| 36 | 14384, 14571, 14749, 14918, 15079, 15231, 15373, 15506, 15631, 15746, 15851, |
| 37 | 15947, 16034, 16111, 16179, 16237, 16286, 16325, 16354, 16373, 16384}; |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 38 | |
| 39 | #ifdef AECM_WITH_ABS_APPROX |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 40 | // Q15 alpha = 0.99439986968132 const Factor for magnitude approximation |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 41 | static const uint16_t kAlpha1 = 32584; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 42 | // Q15 beta = 0.12967166976970 const Factor for magnitude approximation |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 43 | static const uint16_t kBeta1 = 4249; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 44 | // Q15 alpha = 0.94234827210087 const Factor for magnitude approximation |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 45 | static const uint16_t kAlpha2 = 30879; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 46 | // Q15 beta = 0.33787806009150 const Factor for magnitude approximation |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 47 | static const uint16_t kBeta2 = 11072; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 48 | // Q15 alpha = 0.82247698684306 const Factor for magnitude approximation |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 49 | static const uint16_t kAlpha3 = 26951; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 50 | // Q15 beta = 0.57762063060713 const Factor for magnitude approximation |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 51 | static const uint16_t kBeta3 = 18927; |
| 52 | #endif |
| 53 | |
| 54 | static const int16_t kNoiseEstQDomain = 15; |
| 55 | static const int16_t kNoiseEstIncCount = 5; |
| 56 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 57 | static void ComfortNoise(AecmCore* aecm, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 58 | const uint16_t* dfa, |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 59 | ComplexInt16* out, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 60 | const int16_t* lambda); |
| 61 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 62 | static void WindowAndFFT(AecmCore* aecm, |
| 63 | int16_t* fft, |
| 64 | const int16_t* time_signal, |
| 65 | ComplexInt16* freq_signal, |
| 66 | int time_signal_scaling) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 67 | int i = 0; |
| 68 | |
| 69 | // FFT of signal |
| 70 | for (i = 0; i < PART_LEN; i++) { |
| 71 | // Window time domain signal and insert into real part of |
| 72 | // transformation array |fft| |
Alex Loiko | ee67ca3 | 2018-01-12 12:48:06 | [diff] [blame] | 73 | int16_t scaled_time_signal = time_signal[i] * (1 << time_signal_scaling); |
bjornv@webrtc.org | b38b009 | 2015-03-10 06:40:02 | [diff] [blame] | 74 | fft[i] = (int16_t)((scaled_time_signal * WebRtcAecm_kSqrtHanning[i]) >> 14); |
Alex Loiko | ee67ca3 | 2018-01-12 12:48:06 | [diff] [blame] | 75 | scaled_time_signal = time_signal[i + PART_LEN] * (1 << time_signal_scaling); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 76 | fft[PART_LEN + i] = (int16_t)( |
| 77 | (scaled_time_signal * WebRtcAecm_kSqrtHanning[PART_LEN - i]) >> 14); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Do forward FFT, then take only the first PART_LEN complex samples, |
| 81 | // and change signs of the imaginary parts. |
| 82 | WebRtcSpl_RealForwardFFT(aecm->real_fft, fft, (int16_t*)freq_signal); |
| 83 | for (i = 0; i < PART_LEN; i++) { |
| 84 | freq_signal[i].imag = -freq_signal[i].imag; |
| 85 | } |
| 86 | } |
| 87 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 88 | static void InverseFFTAndWindow(AecmCore* aecm, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 89 | int16_t* fft, |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 90 | ComplexInt16* efw, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 91 | int16_t* output, |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 92 | const int16_t* nearendClean) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 93 | int i, j, outCFFT; |
| 94 | int32_t tmp32no1; |
| 95 | // Reuse |efw| for the inverse FFT output after transferring |
| 96 | // the contents to |fft|. |
| 97 | int16_t* ifft_out = (int16_t*)efw; |
| 98 | |
| 99 | // Synthesis |
| 100 | for (i = 1, j = 2; i < PART_LEN; i += 1, j += 2) { |
| 101 | fft[j] = efw[i].real; |
| 102 | fft[j + 1] = -efw[i].imag; |
| 103 | } |
| 104 | fft[0] = efw[0].real; |
| 105 | fft[1] = -efw[0].imag; |
| 106 | |
| 107 | fft[PART_LEN2] = efw[PART_LEN].real; |
| 108 | fft[PART_LEN2 + 1] = -efw[PART_LEN].imag; |
| 109 | |
| 110 | // Inverse FFT. Keep outCFFT to scale the samples in the next block. |
| 111 | outCFFT = WebRtcSpl_RealInverseFFT(aecm->real_fft, fft, ifft_out); |
| 112 | for (i = 0; i < PART_LEN; i++) { |
| 113 | ifft_out[i] = (int16_t)WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 114 | ifft_out[i], WebRtcAecm_kSqrtHanning[i], 14); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 115 | tmp32no1 = WEBRTC_SPL_SHIFT_W32((int32_t)ifft_out[i], |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 116 | outCFFT - aecm->dfaCleanQDomain); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 117 | output[i] = (int16_t)WEBRTC_SPL_SAT(WEBRTC_SPL_WORD16_MAX, |
| 118 | tmp32no1 + aecm->outBuf[i], |
| 119 | WEBRTC_SPL_WORD16_MIN); |
| 120 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 121 | tmp32no1 = |
| 122 | (ifft_out[PART_LEN + i] * WebRtcAecm_kSqrtHanning[PART_LEN - i]) >> 14; |
| 123 | tmp32no1 = WEBRTC_SPL_SHIFT_W32(tmp32no1, outCFFT - aecm->dfaCleanQDomain); |
| 124 | aecm->outBuf[i] = (int16_t)WEBRTC_SPL_SAT(WEBRTC_SPL_WORD16_MAX, tmp32no1, |
| 125 | WEBRTC_SPL_WORD16_MIN); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Copy the current block to the old position |
| 129 | // (aecm->outBuf is shifted elsewhere) |
| 130 | memcpy(aecm->xBuf, aecm->xBuf + PART_LEN, sizeof(int16_t) * PART_LEN); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 131 | memcpy(aecm->dBufNoisy, aecm->dBufNoisy + PART_LEN, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 132 | sizeof(int16_t) * PART_LEN); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 133 | if (nearendClean != NULL) { |
| 134 | memcpy(aecm->dBufClean, aecm->dBufClean + PART_LEN, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 135 | sizeof(int16_t) * PART_LEN); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Transforms a time domain signal into the frequency domain, outputting the |
| 140 | // complex valued signal, absolute value and sum of absolute values. |
| 141 | // |
| 142 | // time_signal [in] Pointer to time domain signal |
| 143 | // freq_signal_real [out] Pointer to real part of frequency domain array |
| 144 | // freq_signal_imag [out] Pointer to imaginary part of frequency domain |
| 145 | // array |
| 146 | // freq_signal_abs [out] Pointer to absolute value of frequency domain |
| 147 | // array |
| 148 | // freq_signal_sum_abs [out] Pointer to the sum of all absolute values in |
| 149 | // the frequency domain array |
| 150 | // return value The Q-domain of current frequency values |
| 151 | // |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 152 | static int TimeToFrequencyDomain(AecmCore* aecm, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 153 | const int16_t* time_signal, |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 154 | ComplexInt16* freq_signal, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 155 | uint16_t* freq_signal_abs, |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 156 | uint32_t* freq_signal_sum_abs) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 157 | int i = 0; |
| 158 | int time_signal_scaling = 0; |
| 159 | |
| 160 | int32_t tmp32no1 = 0; |
| 161 | int32_t tmp32no2 = 0; |
| 162 | |
| 163 | // In fft_buf, +16 for 32-byte alignment. |
| 164 | int16_t fft_buf[PART_LEN4 + 16]; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 165 | int16_t* fft = (int16_t*)(((uintptr_t)fft_buf + 31) & ~31); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 166 | |
| 167 | int16_t tmp16no1; |
| 168 | #ifndef WEBRTC_ARCH_ARM_V7 |
| 169 | int16_t tmp16no2; |
| 170 | #endif |
| 171 | #ifdef AECM_WITH_ABS_APPROX |
| 172 | int16_t max_value = 0; |
| 173 | int16_t min_value = 0; |
| 174 | uint16_t alpha = 0; |
| 175 | uint16_t beta = 0; |
| 176 | #endif |
| 177 | |
| 178 | #ifdef AECM_DYNAMIC_Q |
| 179 | tmp16no1 = WebRtcSpl_MaxAbsValueW16(time_signal, PART_LEN2); |
| 180 | time_signal_scaling = WebRtcSpl_NormW16(tmp16no1); |
| 181 | #endif |
| 182 | |
| 183 | WindowAndFFT(aecm, fft, time_signal, freq_signal, time_signal_scaling); |
| 184 | |
| 185 | // Extract imaginary and real part, calculate the magnitude for |
| 186 | // all frequency bins |
| 187 | freq_signal[0].imag = 0; |
| 188 | freq_signal[PART_LEN].imag = 0; |
| 189 | freq_signal_abs[0] = (uint16_t)WEBRTC_SPL_ABS_W16(freq_signal[0].real); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 190 | freq_signal_abs[PART_LEN] = |
| 191 | (uint16_t)WEBRTC_SPL_ABS_W16(freq_signal[PART_LEN].real); |
| 192 | (*freq_signal_sum_abs) = |
| 193 | (uint32_t)(freq_signal_abs[0]) + (uint32_t)(freq_signal_abs[PART_LEN]); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 194 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 195 | for (i = 1; i < PART_LEN; i++) { |
| 196 | if (freq_signal[i].real == 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 197 | freq_signal_abs[i] = (uint16_t)WEBRTC_SPL_ABS_W16(freq_signal[i].imag); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 198 | } else if (freq_signal[i].imag == 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 199 | freq_signal_abs[i] = (uint16_t)WEBRTC_SPL_ABS_W16(freq_signal[i].real); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 200 | } else { |
| 201 | // Approximation for magnitude of complex fft output |
| 202 | // magn = sqrt(real^2 + imag^2) |
| 203 | // magn ~= alpha * max(|imag|,|real|) + beta * min(|imag|,|real|) |
| 204 | // |
| 205 | // The parameters alpha and beta are stored in Q15 |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 206 | |
| 207 | #ifdef AECM_WITH_ABS_APPROX |
| 208 | tmp16no1 = WEBRTC_SPL_ABS_W16(freq_signal[i].real); |
| 209 | tmp16no2 = WEBRTC_SPL_ABS_W16(freq_signal[i].imag); |
| 210 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 211 | if (tmp16no1 > tmp16no2) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 212 | max_value = tmp16no1; |
| 213 | min_value = tmp16no2; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 214 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 215 | max_value = tmp16no2; |
| 216 | min_value = tmp16no1; |
| 217 | } |
| 218 | |
| 219 | // Magnitude in Q(-6) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 220 | if ((max_value >> 2) > min_value) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 221 | alpha = kAlpha1; |
| 222 | beta = kBeta1; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 223 | } else if ((max_value >> 1) > min_value) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 224 | alpha = kAlpha2; |
| 225 | beta = kBeta2; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 226 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 227 | alpha = kAlpha3; |
| 228 | beta = kBeta3; |
| 229 | } |
bjornv@webrtc.org | b38b009 | 2015-03-10 06:40:02 | [diff] [blame] | 230 | tmp16no1 = (int16_t)((max_value * alpha) >> 15); |
| 231 | tmp16no2 = (int16_t)((min_value * beta) >> 15); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 232 | freq_signal_abs[i] = (uint16_t)tmp16no1 + (uint16_t)tmp16no2; |
| 233 | #else |
| 234 | #ifdef WEBRTC_ARCH_ARM_V7 |
| 235 | __asm __volatile( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 236 | "smulbb %[tmp32no1], %[real], %[real]\n\t" |
| 237 | "smlabb %[tmp32no2], %[imag], %[imag], %[tmp32no1]\n\t" |
| 238 | : [tmp32no1] "+&r"(tmp32no1), [tmp32no2] "=r"(tmp32no2) |
| 239 | : [real] "r"(freq_signal[i].real), [imag] "r"(freq_signal[i].imag)); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 240 | #else |
| 241 | tmp16no1 = WEBRTC_SPL_ABS_W16(freq_signal[i].real); |
| 242 | tmp16no2 = WEBRTC_SPL_ABS_W16(freq_signal[i].imag); |
bjornv@webrtc.org | 758d6d4 | 2015-01-08 17:52:56 | [diff] [blame] | 243 | tmp32no1 = tmp16no1 * tmp16no1; |
| 244 | tmp32no2 = tmp16no2 * tmp16no2; |
bjornv@webrtc.org | 6e71d17 | 2014-08-25 07:44:52 | [diff] [blame] | 245 | tmp32no2 = WebRtcSpl_AddSatW32(tmp32no1, tmp32no2); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 246 | #endif // WEBRTC_ARCH_ARM_V7 |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 247 | tmp32no1 = WebRtcSpl_SqrtFloor(tmp32no2); |
| 248 | |
| 249 | freq_signal_abs[i] = (uint16_t)tmp32no1; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 250 | #endif // AECM_WITH_ABS_APPROX |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 251 | } |
| 252 | (*freq_signal_sum_abs) += (uint32_t)freq_signal_abs[i]; |
| 253 | } |
| 254 | |
| 255 | return time_signal_scaling; |
| 256 | } |
| 257 | |
oprypin | 8ad0e58 | 2017-09-05 10:00:37 | [diff] [blame] | 258 | int RTC_NO_SANITIZE("signed-integer-overflow") // bugs.webrtc.org/8200 |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 259 | WebRtcAecm_ProcessBlock(AecmCore* aecm, |
| 260 | const int16_t* farend, |
| 261 | const int16_t* nearendNoisy, |
| 262 | const int16_t* nearendClean, |
| 263 | int16_t* output) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 264 | int i; |
| 265 | |
| 266 | uint32_t xfaSum; |
| 267 | uint32_t dfaNoisySum; |
| 268 | uint32_t dfaCleanSum; |
| 269 | uint32_t echoEst32Gained; |
| 270 | uint32_t tmpU32; |
| 271 | |
| 272 | int32_t tmp32no1; |
| 273 | |
| 274 | uint16_t xfa[PART_LEN1]; |
| 275 | uint16_t dfaNoisy[PART_LEN1]; |
| 276 | uint16_t dfaClean[PART_LEN1]; |
| 277 | uint16_t* ptrDfaClean = dfaClean; |
| 278 | const uint16_t* far_spectrum_ptr = NULL; |
| 279 | |
| 280 | // 32 byte aligned buffers (with +8 or +16). |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 281 | // TODO(kma): define fft with ComplexInt16. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 282 | int16_t fft_buf[PART_LEN4 + 2 + 16]; // +2 to make a loop safe. |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 283 | int32_t echoEst32_buf[PART_LEN1 + 8]; |
| 284 | int32_t dfw_buf[PART_LEN2 + 8]; |
| 285 | int32_t efw_buf[PART_LEN2 + 8]; |
| 286 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 287 | int16_t* fft = (int16_t*)(((uintptr_t)fft_buf + 31) & ~31); |
| 288 | int32_t* echoEst32 = (int32_t*)(((uintptr_t)echoEst32_buf + 31) & ~31); |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 289 | ComplexInt16* dfw = (ComplexInt16*)(((uintptr_t)dfw_buf + 31) & ~31); |
| 290 | ComplexInt16* efw = (ComplexInt16*)(((uintptr_t)efw_buf + 31) & ~31); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 291 | |
| 292 | int16_t hnl[PART_LEN1]; |
| 293 | int16_t numPosCoef = 0; |
| 294 | int16_t nlpGain = ONE_Q14; |
| 295 | int delay; |
| 296 | int16_t tmp16no1; |
| 297 | int16_t tmp16no2; |
| 298 | int16_t mu; |
| 299 | int16_t supGain; |
| 300 | int16_t zeros32, zeros16; |
| 301 | int16_t zerosDBufNoisy, zerosDBufClean, zerosXBuf; |
| 302 | int far_q; |
bjornv@webrtc.org | c0ba439 | 2014-07-03 13:38:53 | [diff] [blame] | 303 | int16_t resolutionDiff, qDomainDiff, dfa_clean_q_domain_diff; |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 304 | |
| 305 | const int kMinPrefBand = 4; |
| 306 | const int kMaxPrefBand = 24; |
| 307 | int32_t avgHnl32 = 0; |
| 308 | |
| 309 | // Determine startup state. There are three states: |
| 310 | // (0) the first CONV_LEN blocks |
| 311 | // (1) another CONV_LEN blocks |
| 312 | // (2) the rest |
| 313 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 314 | if (aecm->startupState < 2) { |
| 315 | aecm->startupState = |
| 316 | (aecm->totCount >= CONV_LEN) + (aecm->totCount >= CONV_LEN2); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 317 | } |
| 318 | // END: Determine startup state |
| 319 | |
| 320 | // Buffer near and far end signals |
| 321 | memcpy(aecm->xBuf + PART_LEN, farend, sizeof(int16_t) * PART_LEN); |
| 322 | memcpy(aecm->dBufNoisy + PART_LEN, nearendNoisy, sizeof(int16_t) * PART_LEN); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 323 | if (nearendClean != NULL) { |
| 324 | memcpy(aecm->dBufClean + PART_LEN, nearendClean, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 325 | sizeof(int16_t) * PART_LEN); |
| 326 | } |
| 327 | |
| 328 | // Transform far end signal from time domain to frequency domain. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 329 | far_q = TimeToFrequencyDomain(aecm, aecm->xBuf, dfw, xfa, &xfaSum); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 330 | |
| 331 | // Transform noisy near end signal from time domain to frequency domain. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 332 | zerosDBufNoisy = |
| 333 | TimeToFrequencyDomain(aecm, aecm->dBufNoisy, dfw, dfaNoisy, &dfaNoisySum); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 334 | aecm->dfaNoisyQDomainOld = aecm->dfaNoisyQDomain; |
| 335 | aecm->dfaNoisyQDomain = (int16_t)zerosDBufNoisy; |
| 336 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 337 | if (nearendClean == NULL) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 338 | ptrDfaClean = dfaNoisy; |
| 339 | aecm->dfaCleanQDomainOld = aecm->dfaNoisyQDomainOld; |
| 340 | aecm->dfaCleanQDomain = aecm->dfaNoisyQDomain; |
| 341 | dfaCleanSum = dfaNoisySum; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 342 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 343 | // Transform clean near end signal from time domain to frequency domain. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 344 | zerosDBufClean = TimeToFrequencyDomain(aecm, aecm->dBufClean, dfw, dfaClean, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 345 | &dfaCleanSum); |
| 346 | aecm->dfaCleanQDomainOld = aecm->dfaCleanQDomain; |
| 347 | aecm->dfaCleanQDomain = (int16_t)zerosDBufClean; |
| 348 | } |
| 349 | |
| 350 | // Get the delay |
| 351 | // Save far-end history and estimate delay |
| 352 | WebRtcAecm_UpdateFarHistory(aecm, xfa, far_q); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 353 | if (WebRtc_AddFarSpectrumFix(aecm->delay_estimator_farend, xfa, PART_LEN1, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 354 | far_q) == -1) { |
| 355 | return -1; |
| 356 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 357 | delay = WebRtc_DelayEstimatorProcessFix(aecm->delay_estimator, dfaNoisy, |
| 358 | PART_LEN1, zerosDBufNoisy); |
| 359 | if (delay == -1) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 360 | return -1; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 361 | } else if (delay == -2) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 362 | // If the delay is unknown, we assume zero. |
| 363 | // NOTE: this will have to be adjusted if we ever add lookahead. |
| 364 | delay = 0; |
| 365 | } |
| 366 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 367 | if (aecm->fixedDelay >= 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 368 | // Use fixed delay |
| 369 | delay = aecm->fixedDelay; |
| 370 | } |
| 371 | |
| 372 | // Get aligned far end spectrum |
| 373 | far_spectrum_ptr = WebRtcAecm_AlignedFarend(aecm, &far_q, delay); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 374 | zerosXBuf = (int16_t)far_q; |
| 375 | if (far_spectrum_ptr == NULL) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | // Calculate log(energy) and update energy threshold levels |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 380 | WebRtcAecm_CalcEnergies(aecm, far_spectrum_ptr, zerosXBuf, dfaNoisySum, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 381 | echoEst32); |
| 382 | |
| 383 | // Calculate stepsize |
| 384 | mu = WebRtcAecm_CalcStepSize(aecm); |
| 385 | |
| 386 | // Update counters |
| 387 | aecm->totCount++; |
| 388 | |
| 389 | // This is the channel estimation algorithm. |
| 390 | // It is base on NLMS but has a variable step length, |
| 391 | // which was calculated above. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 392 | WebRtcAecm_UpdateChannel(aecm, far_spectrum_ptr, zerosXBuf, dfaNoisy, mu, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 393 | echoEst32); |
| 394 | supGain = WebRtcAecm_CalcSuppressionGain(aecm); |
| 395 | |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 396 | // Calculate Wiener filter hnl[] |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 397 | for (i = 0; i < PART_LEN1; i++) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 398 | // Far end signal through channel estimate in Q8 |
| 399 | // How much can we shift right to preserve resolution |
| 400 | tmp32no1 = echoEst32[i] - aecm->echoFilt[i]; |
Alessio Bazzica | ba68aab | 2017-10-16 11:45:58 | [diff] [blame] | 401 | aecm->echoFilt[i] += |
| 402 | rtc::dchecked_cast<int32_t>((int64_t{tmp32no1} * 50) >> 8); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 403 | |
| 404 | zeros32 = WebRtcSpl_NormW32(aecm->echoFilt[i]) + 1; |
| 405 | zeros16 = WebRtcSpl_NormW16(supGain) + 1; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 406 | if (zeros32 + zeros16 > 16) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 407 | // Multiplication is safe |
| 408 | // Result in |
| 409 | // Q(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN+ |
| 410 | // aecm->xfaQDomainBuf[diff]) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 411 | echoEst32Gained = |
| 412 | WEBRTC_SPL_UMUL_32_16((uint32_t)aecm->echoFilt[i], (uint16_t)supGain); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 413 | resolutionDiff = 14 - RESOLUTION_CHANNEL16 - RESOLUTION_SUPGAIN; |
| 414 | resolutionDiff += (aecm->dfaCleanQDomain - zerosXBuf); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 415 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 416 | tmp16no1 = 17 - zeros32 - zeros16; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 417 | resolutionDiff = |
| 418 | 14 + tmp16no1 - RESOLUTION_CHANNEL16 - RESOLUTION_SUPGAIN; |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 419 | resolutionDiff += (aecm->dfaCleanQDomain - zerosXBuf); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 420 | if (zeros32 > tmp16no1) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 421 | echoEst32Gained = WEBRTC_SPL_UMUL_32_16((uint32_t)aecm->echoFilt[i], |
bjornv@webrtc.org | d4fe824 | 2014-10-13 13:01:13 | [diff] [blame] | 422 | supGain >> tmp16no1); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 423 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 424 | // Result in Q-(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN-16) |
bjornv@webrtc.org | f87c0af | 2014-10-15 12:51:23 | [diff] [blame] | 425 | echoEst32Gained = (aecm->echoFilt[i] >> tmp16no1) * supGain; |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | zeros16 = WebRtcSpl_NormW16(aecm->nearFilt[i]); |
kwiberg | 9e2be5f | 2016-09-14 12:23:22 | [diff] [blame] | 430 | RTC_DCHECK_GE(zeros16, 0); // |zeros16| is a norm, hence non-negative. |
bjornv@webrtc.org | c0ba439 | 2014-07-03 13:38:53 | [diff] [blame] | 431 | dfa_clean_q_domain_diff = aecm->dfaCleanQDomain - aecm->dfaCleanQDomainOld; |
| 432 | if (zeros16 < dfa_clean_q_domain_diff && aecm->nearFilt[i]) { |
Alex Loiko | 600bdb4 | 2018-01-25 10:42:43 | [diff] [blame] | 433 | tmp16no1 = aecm->nearFilt[i] * (1 << zeros16); |
bjornv@webrtc.org | c0ba439 | 2014-07-03 13:38:53 | [diff] [blame] | 434 | qDomainDiff = zeros16 - dfa_clean_q_domain_diff; |
| 435 | tmp16no2 = ptrDfaClean[i] >> -qDomainDiff; |
| 436 | } else { |
| 437 | tmp16no1 = dfa_clean_q_domain_diff < 0 |
Alex Loiko | 736d2f7 | 2018-01-22 10:52:31 | [diff] [blame] | 438 | ? aecm->nearFilt[i] >> -dfa_clean_q_domain_diff |
| 439 | : aecm->nearFilt[i] * (1 << dfa_clean_q_domain_diff); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 440 | qDomainDiff = 0; |
bjornv@webrtc.org | c0ba439 | 2014-07-03 13:38:53 | [diff] [blame] | 441 | tmp16no2 = ptrDfaClean[i]; |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 442 | } |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 443 | tmp32no1 = (int32_t)(tmp16no2 - tmp16no1); |
bjornv@webrtc.org | f87c0af | 2014-10-15 12:51:23 | [diff] [blame] | 444 | tmp16no2 = (int16_t)(tmp32no1 >> 4); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 445 | tmp16no2 += tmp16no1; |
| 446 | zeros16 = WebRtcSpl_NormW16(tmp16no2); |
bjornv@webrtc.org | c0ba439 | 2014-07-03 13:38:53 | [diff] [blame] | 447 | if ((tmp16no2) & (-qDomainDiff > zeros16)) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 448 | aecm->nearFilt[i] = WEBRTC_SPL_WORD16_MAX; |
bjornv@webrtc.org | c0ba439 | 2014-07-03 13:38:53 | [diff] [blame] | 449 | } else { |
Alex Loiko | 736d2f7 | 2018-01-22 10:52:31 | [diff] [blame] | 450 | aecm->nearFilt[i] = qDomainDiff < 0 ? tmp16no2 * (1 << -qDomainDiff) |
bjornv@webrtc.org | c0ba439 | 2014-07-03 13:38:53 | [diff] [blame] | 451 | : tmp16no2 >> qDomainDiff; |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // Wiener filter coefficients, resulting hnl in Q14 |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 455 | if (echoEst32Gained == 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 456 | hnl[i] = ONE_Q14; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 457 | } else if (aecm->nearFilt[i] == 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 458 | hnl[i] = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 459 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 460 | // Multiply the suppression gain |
| 461 | // Rounding |
| 462 | echoEst32Gained += (uint32_t)(aecm->nearFilt[i] >> 1); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 463 | tmpU32 = |
| 464 | WebRtcSpl_DivU32U16(echoEst32Gained, (uint16_t)aecm->nearFilt[i]); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 465 | |
| 466 | // Current resolution is |
| 467 | // Q-(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN- max(0,17-zeros16- zeros32)) |
| 468 | // Make sure we are in Q14 |
| 469 | tmp32no1 = (int32_t)WEBRTC_SPL_SHIFT_W32(tmpU32, resolutionDiff); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 470 | if (tmp32no1 > ONE_Q14) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 471 | hnl[i] = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 472 | } else if (tmp32no1 < 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 473 | hnl[i] = ONE_Q14; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 474 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 475 | // 1-echoEst/dfa |
| 476 | hnl[i] = ONE_Q14 - (int16_t)tmp32no1; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 477 | if (hnl[i] < 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 478 | hnl[i] = 0; |
| 479 | } |
| 480 | } |
| 481 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 482 | if (hnl[i]) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 483 | numPosCoef++; |
| 484 | } |
| 485 | } |
| 486 | // Only in wideband. Prevent the gain in upper band from being larger than |
| 487 | // in lower band. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 488 | if (aecm->mult == 2) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 489 | // TODO(bjornv): Investigate if the scaling of hnl[i] below can cause |
| 490 | // speech distortion in double-talk. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 491 | for (i = 0; i < PART_LEN1; i++) { |
bjornv@webrtc.org | b38b009 | 2015-03-10 06:40:02 | [diff] [blame] | 492 | hnl[i] = (int16_t)((hnl[i] * hnl[i]) >> 14); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 493 | } |
| 494 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 495 | for (i = kMinPrefBand; i <= kMaxPrefBand; i++) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 496 | avgHnl32 += (int32_t)hnl[i]; |
| 497 | } |
kwiberg | 9e2be5f | 2016-09-14 12:23:22 | [diff] [blame] | 498 | RTC_DCHECK_GT(kMaxPrefBand - kMinPrefBand + 1, 0); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 499 | avgHnl32 /= (kMaxPrefBand - kMinPrefBand + 1); |
| 500 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 501 | for (i = kMaxPrefBand; i < PART_LEN1; i++) { |
| 502 | if (hnl[i] > (int16_t)avgHnl32) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 503 | hnl[i] = (int16_t)avgHnl32; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // Calculate NLP gain, result is in Q14 |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 509 | if (aecm->nlpFlag) { |
| 510 | for (i = 0; i < PART_LEN1; i++) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 511 | // Truncate values close to zero and one. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 512 | if (hnl[i] > NLP_COMP_HIGH) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 513 | hnl[i] = ONE_Q14; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 514 | } else if (hnl[i] < NLP_COMP_LOW) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 515 | hnl[i] = 0; |
| 516 | } |
| 517 | |
| 518 | // Remove outliers |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 519 | if (numPosCoef < 3) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 520 | nlpGain = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 521 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 522 | nlpGain = ONE_Q14; |
| 523 | } |
| 524 | |
| 525 | // NLP |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 526 | if ((hnl[i] == ONE_Q14) && (nlpGain == ONE_Q14)) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 527 | hnl[i] = ONE_Q14; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 528 | } else { |
bjornv@webrtc.org | b38b009 | 2015-03-10 06:40:02 | [diff] [blame] | 529 | hnl[i] = (int16_t)((hnl[i] * nlpGain) >> 14); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | // multiply with Wiener coefficients |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 533 | efw[i].real = (int16_t)( |
| 534 | WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].real, hnl[i], 14)); |
| 535 | efw[i].imag = (int16_t)( |
| 536 | WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].imag, hnl[i], 14)); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 537 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 538 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 539 | // multiply with Wiener coefficients |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 540 | for (i = 0; i < PART_LEN1; i++) { |
| 541 | efw[i].real = (int16_t)( |
| 542 | WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].real, hnl[i], 14)); |
| 543 | efw[i].imag = (int16_t)( |
| 544 | WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].imag, hnl[i], 14)); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 548 | if (aecm->cngMode == AecmTrue) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 549 | ComfortNoise(aecm, ptrDfaClean, efw, hnl); |
| 550 | } |
| 551 | |
| 552 | InverseFFTAndWindow(aecm, fft, efw, output, nearendClean); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 557 | static void ComfortNoise(AecmCore* aecm, |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 558 | const uint16_t* dfa, |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 | [diff] [blame] | 559 | ComplexInt16* out, |
| 560 | const int16_t* lambda) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 561 | int16_t i; |
| 562 | int16_t tmp16; |
| 563 | int32_t tmp32; |
| 564 | |
| 565 | int16_t randW16[PART_LEN]; |
| 566 | int16_t uReal[PART_LEN1]; |
| 567 | int16_t uImag[PART_LEN1]; |
| 568 | int32_t outLShift32; |
| 569 | int16_t noiseRShift16[PART_LEN1]; |
| 570 | |
| 571 | int16_t shiftFromNearToNoise = kNoiseEstQDomain - aecm->dfaCleanQDomain; |
| 572 | int16_t minTrackShift; |
| 573 | |
kwiberg | 9e2be5f | 2016-09-14 12:23:22 | [diff] [blame] | 574 | RTC_DCHECK_GE(shiftFromNearToNoise, 0); |
| 575 | RTC_DCHECK_LT(shiftFromNearToNoise, 16); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 576 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 577 | if (aecm->noiseEstCtr < 100) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 578 | // Track the minimum more quickly initially. |
| 579 | aecm->noiseEstCtr++; |
| 580 | minTrackShift = 6; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 581 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 582 | minTrackShift = 9; |
| 583 | } |
| 584 | |
| 585 | // Estimate noise power. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 586 | for (i = 0; i < PART_LEN1; i++) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 587 | // Shift to the noise domain. |
| 588 | tmp32 = (int32_t)dfa[i]; |
bjornv@webrtc.org | 750423c | 2014-09-30 09:26:36 | [diff] [blame] | 589 | outLShift32 = tmp32 << shiftFromNearToNoise; |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 590 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 591 | if (outLShift32 < aecm->noiseEst[i]) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 592 | // Reset "too low" counter |
| 593 | aecm->noiseEstTooLowCtr[i] = 0; |
| 594 | // Track the minimum. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 595 | if (aecm->noiseEst[i] < (1 << minTrackShift)) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 596 | // For small values, decrease noiseEst[i] every |
| 597 | // |kNoiseEstIncCount| block. The regular approach below can not |
| 598 | // go further down due to truncation. |
| 599 | aecm->noiseEstTooHighCtr[i]++; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 600 | if (aecm->noiseEstTooHighCtr[i] >= kNoiseEstIncCount) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 601 | aecm->noiseEst[i]--; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 602 | aecm->noiseEstTooHighCtr[i] = 0; // Reset the counter |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 603 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 604 | } else { |
| 605 | aecm->noiseEst[i] -= |
| 606 | ((aecm->noiseEst[i] - outLShift32) >> minTrackShift); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 607 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 608 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 609 | // Reset "too high" counter |
| 610 | aecm->noiseEstTooHighCtr[i] = 0; |
| 611 | // Ramp slowly upwards until we hit the minimum again. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 612 | if ((aecm->noiseEst[i] >> 19) > 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 613 | // Avoid overflow. |
| 614 | // Multiplication with 2049 will cause wrap around. Scale |
| 615 | // down first and then multiply |
| 616 | aecm->noiseEst[i] >>= 11; |
| 617 | aecm->noiseEst[i] *= 2049; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 618 | } else if ((aecm->noiseEst[i] >> 11) > 0) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 619 | // Large enough for relative increase |
| 620 | aecm->noiseEst[i] *= 2049; |
| 621 | aecm->noiseEst[i] >>= 11; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 622 | } else { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 623 | // Make incremental increases based on size every |
| 624 | // |kNoiseEstIncCount| block |
| 625 | aecm->noiseEstTooLowCtr[i]++; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 626 | if (aecm->noiseEstTooLowCtr[i] >= kNoiseEstIncCount) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 627 | aecm->noiseEst[i] += (aecm->noiseEst[i] >> 9) + 1; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 628 | aecm->noiseEstTooLowCtr[i] = 0; // Reset counter |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 634 | for (i = 0; i < PART_LEN1; i++) { |
bjornv@webrtc.org | f87c0af | 2014-10-15 12:51:23 | [diff] [blame] | 635 | tmp32 = aecm->noiseEst[i] >> shiftFromNearToNoise; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 636 | if (tmp32 > 32767) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 637 | tmp32 = 32767; |
bjornv@webrtc.org | 750423c | 2014-09-30 09:26:36 | [diff] [blame] | 638 | aecm->noiseEst[i] = tmp32 << shiftFromNearToNoise; |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 639 | } |
| 640 | noiseRShift16[i] = (int16_t)tmp32; |
| 641 | |
| 642 | tmp16 = ONE_Q14 - lambda[i]; |
bjornv@webrtc.org | b38b009 | 2015-03-10 06:40:02 | [diff] [blame] | 643 | noiseRShift16[i] = (int16_t)((tmp16 * noiseRShift16[i]) >> 14); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | // Generate a uniform random array on [0 2^15-1]. |
| 647 | WebRtcSpl_RandUArray(randW16, PART_LEN, &aecm->seed); |
| 648 | |
| 649 | // Generate noise according to estimated energy. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 650 | uReal[0] = 0; // Reject LF noise. |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 651 | uImag[0] = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 652 | for (i = 1; i < PART_LEN1; i++) { |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 653 | // Get a random index for the cos and sin tables over [0 359]. |
bjornv@webrtc.org | b38b009 | 2015-03-10 06:40:02 | [diff] [blame] | 654 | tmp16 = (int16_t)((359 * randW16[i - 1]) >> 15); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 655 | |
| 656 | // Tables are in Q13. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 657 | uReal[i] = |
| 658 | (int16_t)((noiseRShift16[i] * WebRtcAecm_kCosTable[tmp16]) >> 13); |
| 659 | uImag[i] = |
| 660 | (int16_t)((-noiseRShift16[i] * WebRtcAecm_kSinTable[tmp16]) >> 13); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 661 | } |
| 662 | uImag[PART_LEN] = 0; |
| 663 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 664 | for (i = 0; i < PART_LEN1; i++) { |
bjornv@webrtc.org | 6e71d17 | 2014-08-25 07:44:52 | [diff] [blame] | 665 | out[i].real = WebRtcSpl_AddSatW16(out[i].real, uReal[i]); |
| 666 | out[i].imag = WebRtcSpl_AddSatW16(out[i].imag, uImag[i]); |
andrew@webrtc.org | e03cafa | 2013-11-11 20:10:01 | [diff] [blame] | 667 | } |
| 668 | } |