niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | /* |
| 12 | * Contains the API functions for the AEC. |
| 13 | */ |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 14 | #include "webrtc/modules/audio_processing/aec/include/echo_cancellation.h" |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 15 | |
| 16 | #include <math.h> |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 17 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 23 | #include "webrtc/common_audio/ring_buffer.h" |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 24 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
| 25 | #include "webrtc/modules/audio_processing/aec/aec_core.h" |
| 26 | #include "webrtc/modules/audio_processing/aec/aec_resampler.h" |
| 27 | #include "webrtc/modules/audio_processing/aec/echo_cancellation_internal.h" |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 28 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 30 | // Measured delays [ms] |
| 31 | // Device Chrome GTP |
| 32 | // MacBook Air 10 |
| 33 | // MacBook Retina 10 100 |
| 34 | // MacPro 30? |
| 35 | // |
| 36 | // Win7 Desktop 70 80? |
| 37 | // Win7 T430s 110 |
| 38 | // Win8 T420s 70 |
| 39 | // |
| 40 | // Daisy 50 |
| 41 | // Pixel (w/ preproc?) 240 |
| 42 | // Pixel (w/o preproc?) 110 110 |
| 43 | |
| 44 | // The extended filter mode gives us the flexibility to ignore the system's |
| 45 | // reported delays. We do this for platforms which we believe provide results |
| 46 | // which are incompatible with the AEC's expectations. Based on measurements |
| 47 | // (some provided above) we set a conservative (i.e. lower than measured) |
| 48 | // fixed delay. |
| 49 | // |
| 50 | // WEBRTC_UNTRUSTED_DELAY will only have an impact when |extended_filter_mode| |
| 51 | // is enabled. See the note along with |DelayCorrection| in |
| 52 | // echo_cancellation_impl.h for more details on the mode. |
| 53 | // |
| 54 | // Justification: |
| 55 | // Chromium/Mac: Here, the true latency is so low (~10-20 ms), that it plays |
| 56 | // havoc with the AEC's buffering. To avoid this, we set a fixed delay of 20 ms |
| 57 | // and then compensate by rewinding by 10 ms (in wideband) through |
| 58 | // kDelayDiffOffsetSamples. This trick does not seem to work for larger rewind |
| 59 | // values, but fortunately this is sufficient. |
| 60 | // |
| 61 | // Chromium/Linux(ChromeOS): The values we get on this platform don't correspond |
| 62 | // well to reality. The variance doesn't match the AEC's buffer changes, and the |
| 63 | // bulk values tend to be too low. However, the range across different hardware |
| 64 | // appears to be too large to choose a single value. |
| 65 | // |
| 66 | // GTP/Linux(ChromeOS): TBD, but for the moment we will trust the values. |
| 67 | #if defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_MAC) |
| 68 | #define WEBRTC_UNTRUSTED_DELAY |
bjornv@webrtc.org | 820f8e9 | 2014-08-11 15:39:00 +0000 | [diff] [blame] | 69 | #endif |
andrew@webrtc.org | acb0050 | 2013-10-04 16:59:17 +0000 | [diff] [blame] | 70 | |
bjornv@webrtc.org | 820f8e9 | 2014-08-11 15:39:00 +0000 | [diff] [blame] | 71 | #if defined(WEBRTC_UNTRUSTED_DELAY) && defined(WEBRTC_MAC) |
andrew@webrtc.org | acb0050 | 2013-10-04 16:59:17 +0000 | [diff] [blame] | 72 | static const int kDelayDiffOffsetSamples = -160; |
| 73 | #else |
| 74 | // Not enabled for now. |
| 75 | static const int kDelayDiffOffsetSamples = 0; |
| 76 | #endif |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 77 | |
| 78 | #if defined(WEBRTC_MAC) |
| 79 | static const int kFixedDelayMs = 20; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 80 | #else |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 81 | static const int kFixedDelayMs = 50; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 82 | #endif |
andrew@webrtc.org | c2e471d | 2013-10-15 02:11:21 +0000 | [diff] [blame] | 83 | #if !defined(WEBRTC_UNTRUSTED_DELAY) |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 84 | static const int kMinTrustedDelayMs = 20; |
andrew@webrtc.org | c2e471d | 2013-10-15 02:11:21 +0000 | [diff] [blame] | 85 | #endif |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 86 | static const int kMaxTrustedDelayMs = 500; |
| 87 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | // Maximum length of resampled signal. Must be an integer multiple of frames |
| 89 | // (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN |
| 90 | // The factor of 2 handles wb, and the + 1 is as a safety margin |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 91 | // TODO(bjornv): Replace with kResamplerBufferSize |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | #define MAX_RESAMP_LEN (5 * FRAME_LEN) |
| 93 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 94 | static const int kMaxBufSizeStart = 62; // In partitions |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 95 | static const int sampMsNb = 8; // samples per ms in nb |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | static const int initCheck = 42; |
| 97 | |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 98 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 99 | int webrtc_aec_instance_count = 0; |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 102 | // Estimates delay to set the position of the far-end buffer read pointer |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | // (controlled by knownDelay) |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 104 | static void EstBufDelayNormal(Aec* aecInst); |
| 105 | static void EstBufDelayExtended(Aec* aecInst); |
| 106 | static int ProcessNormal(Aec* self, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 107 | const float* const* near, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 108 | size_t num_bands, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 109 | float* const* out, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 110 | size_t num_samples, |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 111 | int16_t reported_delay_ms, |
| 112 | int32_t skew); |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 113 | static void ProcessExtended(Aec* self, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 114 | const float* const* near, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 115 | size_t num_bands, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 116 | float* const* out, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 117 | size_t num_samples, |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 118 | int16_t reported_delay_ms, |
| 119 | int32_t skew); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 121 | void* WebRtcAec_Create() { |
| 122 | Aec* aecpc = malloc(sizeof(Aec)); |
| 123 | |
| 124 | if (!aecpc) { |
| 125 | return NULL; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 126 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 128 | aecpc->aec = WebRtcAec_CreateAec(); |
| 129 | if (!aecpc->aec) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 130 | WebRtcAec_Free(aecpc); |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 131 | return NULL; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 132 | } |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 133 | aecpc->resampler = WebRtcAec_CreateResampler(); |
| 134 | if (!aecpc->resampler) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 135 | WebRtcAec_Free(aecpc); |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 136 | return NULL; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 137 | } |
| 138 | // Create far-end pre-buffer. The buffer size has to be large enough for |
| 139 | // largest possible drift compensation (kResamplerBufferSize) + "almost" an |
| 140 | // FFT buffer (PART_LEN2 - 1). |
| 141 | aecpc->far_pre_buf = |
| 142 | WebRtc_CreateBuffer(PART_LEN2 + kResamplerBufferSize, sizeof(float)); |
| 143 | if (!aecpc->far_pre_buf) { |
| 144 | WebRtcAec_Free(aecpc); |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 145 | return NULL; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 146 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 148 | aecpc->initFlag = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 150 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 151 | { |
| 152 | char filename[64]; |
| 153 | sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count); |
| 154 | aecpc->bufFile = fopen(filename, "wb"); |
| 155 | sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count); |
| 156 | aecpc->skewFile = fopen(filename, "wb"); |
| 157 | sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count); |
| 158 | aecpc->delayFile = fopen(filename, "wb"); |
| 159 | webrtc_aec_instance_count++; |
| 160 | } |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 161 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 163 | return aecpc; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Bjorn Volcker | f6a99e6 | 2015-04-10 07:56:57 +0200 | [diff] [blame] | 166 | void WebRtcAec_Free(void* aecInst) { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 167 | Aec* aecpc = aecInst; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 169 | if (aecpc == NULL) { |
Bjorn Volcker | f6a99e6 | 2015-04-10 07:56:57 +0200 | [diff] [blame] | 170 | return; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 171 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 173 | WebRtc_FreeBuffer(aecpc->far_pre_buf); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 174 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 175 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 176 | fclose(aecpc->bufFile); |
| 177 | fclose(aecpc->skewFile); |
| 178 | fclose(aecpc->delayFile); |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 179 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 181 | WebRtcAec_FreeAec(aecpc->aec); |
| 182 | WebRtcAec_FreeResampler(aecpc->resampler); |
| 183 | free(aecpc); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 186 | int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq) { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 187 | Aec* aecpc = aecInst; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 188 | AecConfig aecConfig; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 190 | if (sampFreq != 8000 && |
| 191 | sampFreq != 16000 && |
| 192 | sampFreq != 32000 && |
| 193 | sampFreq != 48000) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 194 | return AEC_BAD_PARAMETER_ERROR; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 195 | } |
| 196 | aecpc->sampFreq = sampFreq; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 198 | if (scSampFreq < 1 || scSampFreq > 96000) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 199 | return AEC_BAD_PARAMETER_ERROR; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 200 | } |
| 201 | aecpc->scSampFreq = scSampFreq; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 203 | // Initialize echo canceller core |
| 204 | if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 205 | return AEC_UNSPECIFIED_ERROR; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 206 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 208 | if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 209 | return AEC_UNSPECIFIED_ERROR; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 210 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 212 | WebRtc_InitBuffer(aecpc->far_pre_buf); |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 213 | WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap. |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 214 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 215 | aecpc->initFlag = initCheck; // indicates that initialization has been done |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 217 | if (aecpc->sampFreq == 32000 || aecpc->sampFreq == 48000) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 218 | aecpc->splitSampFreq = 16000; |
| 219 | } else { |
| 220 | aecpc->splitSampFreq = sampFreq; |
| 221 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 222 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 223 | aecpc->delayCtr = 0; |
| 224 | aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq; |
| 225 | // Sampling frequency multiplier (SWB is processed as 160 frame size). |
| 226 | aecpc->rate_factor = aecpc->splitSampFreq / 8000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 228 | aecpc->sum = 0; |
| 229 | aecpc->counter = 0; |
| 230 | aecpc->checkBuffSize = 1; |
| 231 | aecpc->firstVal = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | |
Bjorn Volcker | 7101269 | 2015-06-18 11:04:56 +0200 | [diff] [blame] | 233 | // We skip the startup_phase completely (setting to 0) if DA-AEC is enabled, |
| 234 | // but not extended_filter mode. |
| 235 | aecpc->startup_phase = WebRtcAec_extended_filter_enabled(aecpc->aec) || |
henrik.lundin | 0f133b9 | 2015-07-02 00:17:55 -0700 | [diff] [blame] | 236 | !WebRtcAec_delay_agnostic_enabled(aecpc->aec); |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 237 | aecpc->bufSizeStart = 0; |
| 238 | aecpc->checkBufSizeCtr = 0; |
| 239 | aecpc->msInSndCardBuf = 0; |
| 240 | aecpc->filtDelay = -1; // -1 indicates an initialized state. |
| 241 | aecpc->timeForDelayChange = 0; |
| 242 | aecpc->knownDelay = 0; |
| 243 | aecpc->lastDelayDiff = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 244 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 245 | aecpc->skewFrCtr = 0; |
| 246 | aecpc->resample = kAecFalse; |
| 247 | aecpc->highSkewCtr = 0; |
| 248 | aecpc->skew = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 249 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 250 | aecpc->farend_started = 0; |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 251 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 252 | // Default settings. |
| 253 | aecConfig.nlpMode = kAecNlpModerate; |
| 254 | aecConfig.skewMode = kAecFalse; |
| 255 | aecConfig.metricsMode = kAecFalse; |
| 256 | aecConfig.delay_logging = kAecFalse; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 257 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 258 | if (WebRtcAec_set_config(aecpc, aecConfig) == -1) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 259 | return AEC_UNSPECIFIED_ERROR; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 260 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 262 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | } |
| 264 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 265 | // Returns any error that is caused when buffering the |
| 266 | // far-end signal. |
| 267 | int32_t WebRtcAec_GetBufferFarendError(void* aecInst, |
| 268 | const float* farend, |
| 269 | size_t nrOfSamples) { |
| 270 | Aec* aecpc = aecInst; |
| 271 | |
| 272 | if (!farend) |
| 273 | return AEC_NULL_POINTER_ERROR; |
| 274 | |
| 275 | if (aecpc->initFlag != initCheck) |
| 276 | return AEC_UNINITIALIZED_ERROR; |
| 277 | |
| 278 | // number of samples == 160 for SWB input |
| 279 | if (nrOfSamples != 80 && nrOfSamples != 160) |
| 280 | return AEC_BAD_PARAMETER_ERROR; |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 285 | // only buffer L band for farend |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 286 | int32_t WebRtcAec_BufferFarend(void* aecInst, |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 287 | const float* farend, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 288 | size_t nrOfSamples) { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 289 | Aec* aecpc = aecInst; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 290 | size_t newNrOfSamples = nrOfSamples; |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 291 | float new_farend[MAX_RESAMP_LEN]; |
| 292 | const float* farend_ptr = farend; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 293 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 294 | // Get any error caused by buffering the farend signal. |
| 295 | int32_t error_code = WebRtcAec_GetBufferFarendError(aecInst, farend, |
| 296 | nrOfSamples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 298 | if (error_code != 0) |
| 299 | return error_code; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 300 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 301 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 302 | if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) { |
| 303 | // Resample and get a new number of samples |
| 304 | WebRtcAec_ResampleLinear(aecpc->resampler, |
| 305 | farend, |
| 306 | nrOfSamples, |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 307 | aecpc->skew, |
| 308 | new_farend, |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 309 | &newNrOfSamples); |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 310 | farend_ptr = new_farend; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 311 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 312 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 313 | aecpc->farend_started = 1; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 314 | WebRtcAec_SetSystemDelay( |
| 315 | aecpc->aec, WebRtcAec_system_delay(aecpc->aec) + (int)newNrOfSamples); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 316 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 317 | // Write the time-domain data to |far_pre_buf|. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 318 | WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_ptr, newNrOfSamples); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 319 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 320 | // Transform to frequency domain if we have enough data. |
| 321 | while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) { |
| 322 | // We have enough data to pass to the FFT, hence read PART_LEN2 samples. |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 323 | { |
andrew@webrtc.org | e65d9d9 | 2015-01-21 22:05:12 +0000 | [diff] [blame] | 324 | float* ptmp = NULL; |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 325 | float tmp[PART_LEN2]; |
| 326 | WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**)&ptmp, tmp, PART_LEN2); |
| 327 | WebRtcAec_BufferFarendPartition(aecpc->aec, ptmp); |
kwiberg@webrtc.org | 584cd8d | 2014-08-25 06:26:04 +0000 | [diff] [blame] | 328 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 329 | WebRtc_WriteBuffer( |
| 330 | WebRtcAec_far_time_buf(aecpc->aec), &ptmp[PART_LEN], 1); |
| 331 | #endif |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 332 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 333 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 334 | // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing. |
| 335 | WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 336 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 337 | |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 338 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 339 | } |
| 340 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 341 | int32_t WebRtcAec_Process(void* aecInst, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 342 | const float* const* nearend, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 343 | size_t num_bands, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 344 | float* const* out, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 345 | size_t nrOfSamples, |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 346 | int16_t msInSndCardBuf, |
| 347 | int32_t skew) { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 348 | Aec* aecpc = aecInst; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 349 | int32_t retVal = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 350 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 351 | if (out == NULL) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 352 | return AEC_NULL_POINTER_ERROR; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 353 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 354 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 355 | if (aecpc->initFlag != initCheck) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 356 | return AEC_UNINITIALIZED_ERROR; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 357 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 358 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 359 | // number of samples == 160 for SWB input |
| 360 | if (nrOfSamples != 80 && nrOfSamples != 160) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 361 | return AEC_BAD_PARAMETER_ERROR; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 362 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 363 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 364 | if (msInSndCardBuf < 0) { |
| 365 | msInSndCardBuf = 0; |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 366 | retVal = AEC_BAD_PARAMETER_WARNING; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 367 | } else if (msInSndCardBuf > kMaxTrustedDelayMs) { |
| 368 | // The clamping is now done in ProcessExtended/Normal(). |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 369 | retVal = AEC_BAD_PARAMETER_WARNING; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 370 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 371 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 372 | // This returns the value of aec->extended_filter_enabled. |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 373 | if (WebRtcAec_extended_filter_enabled(aecpc->aec)) { |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 374 | ProcessExtended(aecpc, |
| 375 | nearend, |
| 376 | num_bands, |
| 377 | out, |
| 378 | nrOfSamples, |
| 379 | msInSndCardBuf, |
| 380 | skew); |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 381 | } else { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 382 | retVal = ProcessNormal(aecpc, |
| 383 | nearend, |
| 384 | num_bands, |
| 385 | out, |
| 386 | nrOfSamples, |
| 387 | msInSndCardBuf, |
| 388 | skew); |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 389 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 390 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 391 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 392 | { |
| 393 | int16_t far_buf_size_ms = (int16_t)(WebRtcAec_system_delay(aecpc->aec) / |
| 394 | (sampMsNb * aecpc->rate_factor)); |
| 395 | (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile); |
| 396 | (void)fwrite( |
| 397 | &aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, aecpc->delayFile); |
| 398 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | #endif |
| 400 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 401 | return retVal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 402 | } |
| 403 | |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 404 | int WebRtcAec_set_config(void* handle, AecConfig config) { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 405 | Aec* self = (Aec*)handle; |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 406 | if (self->initFlag != initCheck) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 407 | return AEC_UNINITIALIZED_ERROR; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 408 | } |
| 409 | |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 410 | if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 411 | return AEC_BAD_PARAMETER_ERROR; |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 412 | } |
| 413 | self->skewMode = config.skewMode; |
| 414 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 415 | if (config.nlpMode != kAecNlpConservative && |
| 416 | config.nlpMode != kAecNlpModerate && |
| 417 | config.nlpMode != kAecNlpAggressive) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 418 | return AEC_BAD_PARAMETER_ERROR; |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 422 | return AEC_BAD_PARAMETER_ERROR; |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 426 | return AEC_BAD_PARAMETER_ERROR; |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 427 | } |
| 428 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 429 | WebRtcAec_SetConfigCore( |
| 430 | self->aec, config.nlpMode, config.metricsMode, config.delay_logging); |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 431 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 432 | } |
| 433 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 434 | int WebRtcAec_get_echo_status(void* handle, int* status) { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 435 | Aec* self = (Aec*)handle; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 436 | if (status == NULL) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 437 | return AEC_NULL_POINTER_ERROR; |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 438 | } |
| 439 | if (self->initFlag != initCheck) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 440 | return AEC_UNINITIALIZED_ERROR; |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 441 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 442 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 443 | *status = WebRtcAec_echo_state(self->aec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 444 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 445 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 448 | int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) { |
| 449 | const float kUpWeight = 0.7f; |
| 450 | float dtmp; |
| 451 | int stmp; |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 452 | Aec* self = (Aec*)handle; |
bjornv@webrtc.org | cea70f4 | 2013-02-19 21:03:10 +0000 | [diff] [blame] | 453 | Stats erl; |
| 454 | Stats erle; |
| 455 | Stats a_nlp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 456 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 457 | if (handle == NULL) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 458 | return -1; |
| 459 | } |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 460 | if (metrics == NULL) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 461 | return AEC_NULL_POINTER_ERROR; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 462 | } |
| 463 | if (self->initFlag != initCheck) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 464 | return AEC_UNINITIALIZED_ERROR; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 465 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 466 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 467 | WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 469 | // ERL |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 470 | metrics->erl.instant = (int)erl.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 471 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 472 | if ((erl.himean > kOffsetLevel) && (erl.average > kOffsetLevel)) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 473 | // Use a mix between regular average and upper part average. |
| 474 | dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 475 | metrics->erl.average = (int)dtmp; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 476 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 477 | metrics->erl.average = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 478 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 479 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 480 | metrics->erl.max = (int)erl.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 481 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 482 | if (erl.min < (kOffsetLevel * (-1))) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 483 | metrics->erl.min = (int)erl.min; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 484 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 485 | metrics->erl.min = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 486 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 487 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 488 | // ERLE |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 489 | metrics->erle.instant = (int)erle.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 490 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 491 | if ((erle.himean > kOffsetLevel) && (erle.average > kOffsetLevel)) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 492 | // Use a mix between regular average and upper part average. |
| 493 | dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 494 | metrics->erle.average = (int)dtmp; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 495 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 496 | metrics->erle.average = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 497 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 498 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 499 | metrics->erle.max = (int)erle.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 500 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 501 | if (erle.min < (kOffsetLevel * (-1))) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 502 | metrics->erle.min = (int)erle.min; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 503 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 504 | metrics->erle.min = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 505 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 506 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 507 | // RERL |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 508 | if ((metrics->erl.average > kOffsetLevel) && |
| 509 | (metrics->erle.average > kOffsetLevel)) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 510 | stmp = metrics->erl.average + metrics->erle.average; |
| 511 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 512 | stmp = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 513 | } |
| 514 | metrics->rerl.average = stmp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 515 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 516 | // No other statistics needed, but returned for completeness. |
| 517 | metrics->rerl.instant = stmp; |
| 518 | metrics->rerl.max = stmp; |
| 519 | metrics->rerl.min = stmp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 520 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 521 | // A_NLP |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 522 | metrics->aNlp.instant = (int)a_nlp.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 523 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 524 | if ((a_nlp.himean > kOffsetLevel) && (a_nlp.average > kOffsetLevel)) { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 525 | // Use a mix between regular average and upper part average. |
| 526 | dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 527 | metrics->aNlp.average = (int)dtmp; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 528 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 529 | metrics->aNlp.average = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 530 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 531 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 532 | metrics->aNlp.max = (int)a_nlp.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 533 | |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 534 | if (a_nlp.min < (kOffsetLevel * (-1))) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 535 | metrics->aNlp.min = (int)a_nlp.min; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 536 | } else { |
bjornv@webrtc.org | 71e91f3 | 2013-02-20 19:24:50 +0000 | [diff] [blame] | 537 | metrics->aNlp.min = kOffsetLevel; |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 538 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 539 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 540 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 541 | } |
| 542 | |
bjornv@webrtc.org | b1786db | 2015-02-03 06:06:26 +0000 | [diff] [blame] | 543 | int WebRtcAec_GetDelayMetrics(void* handle, |
| 544 | int* median, |
| 545 | int* std, |
| 546 | float* fraction_poor_delays) { |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 547 | Aec* self = handle; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 548 | if (median == NULL) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 549 | return AEC_NULL_POINTER_ERROR; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 550 | } |
| 551 | if (std == NULL) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 552 | return AEC_NULL_POINTER_ERROR; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 553 | } |
| 554 | if (self->initFlag != initCheck) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 555 | return AEC_UNINITIALIZED_ERROR; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 556 | } |
bjornv@webrtc.org | b1786db | 2015-02-03 06:06:26 +0000 | [diff] [blame] | 557 | if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std, |
| 558 | fraction_poor_delays) == |
| 559 | -1) { |
bjornv@webrtc.org | 325f625 | 2013-02-15 15:21:02 +0000 | [diff] [blame] | 560 | // Logging disabled. |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 561 | return AEC_UNSUPPORTED_FUNCTION_ERROR; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 562 | } |
| 563 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 567 | |
bjornv@webrtc.org | 132c15d | 2013-02-27 21:03:41 +0000 | [diff] [blame] | 568 | AecCore* WebRtcAec_aec_core(void* handle) { |
| 569 | if (!handle) { |
| 570 | return NULL; |
| 571 | } |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 572 | return ((Aec*)handle)->aec; |
bjornv@webrtc.org | 132c15d | 2013-02-27 21:03:41 +0000 | [diff] [blame] | 573 | } |
| 574 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 575 | static int ProcessNormal(Aec* aecpc, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 576 | const float* const* nearend, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 577 | size_t num_bands, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 578 | float* const* out, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 579 | size_t nrOfSamples, |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 580 | int16_t msInSndCardBuf, |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 581 | int32_t skew) { |
| 582 | int retVal = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 583 | size_t i; |
| 584 | size_t nBlocks10ms; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 585 | // Limit resampling to doubling/halving of signal |
| 586 | const float minSkewEst = -0.5f; |
| 587 | const float maxSkewEst = 1.0f; |
| 588 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 589 | msInSndCardBuf = |
| 590 | msInSndCardBuf > kMaxTrustedDelayMs ? kMaxTrustedDelayMs : msInSndCardBuf; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 591 | // TODO(andrew): we need to investigate if this +10 is really wanted. |
| 592 | msInSndCardBuf += 10; |
| 593 | aecpc->msInSndCardBuf = msInSndCardBuf; |
| 594 | |
| 595 | if (aecpc->skewMode == kAecTrue) { |
| 596 | if (aecpc->skewFrCtr < 25) { |
| 597 | aecpc->skewFrCtr++; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 598 | } else { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 599 | retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew); |
| 600 | if (retVal == -1) { |
| 601 | aecpc->skew = 0; |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame^] | 602 | retVal = AEC_BAD_PARAMETER_WARNING; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 603 | } |
| 604 | |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 605 | aecpc->skew /= aecpc->sampFactor * nrOfSamples; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 606 | |
| 607 | if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) { |
| 608 | aecpc->resample = kAecFalse; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 609 | } else { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 610 | aecpc->resample = kAecTrue; |
| 611 | } |
| 612 | |
| 613 | if (aecpc->skew < minSkewEst) { |
| 614 | aecpc->skew = minSkewEst; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 615 | } else if (aecpc->skew > maxSkewEst) { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 616 | aecpc->skew = maxSkewEst; |
| 617 | } |
| 618 | |
| 619 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 620 | (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile); |
| 621 | #endif |
| 622 | } |
| 623 | } |
| 624 | |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 625 | nBlocks10ms = nrOfSamples / (FRAME_LEN * aecpc->rate_factor); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 626 | |
| 627 | if (aecpc->startup_phase) { |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 628 | for (i = 0; i < num_bands; ++i) { |
| 629 | // Only needed if they don't already point to the same place. |
| 630 | if (nearend[i] != out[i]) { |
| 631 | memcpy(out[i], nearend[i], sizeof(nearend[i][0]) * nrOfSamples); |
| 632 | } |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | // The AEC is in the start up mode |
| 636 | // AEC is disabled until the system delay is OK |
| 637 | |
| 638 | // Mechanism to ensure that the system delay is reasonably stable. |
| 639 | if (aecpc->checkBuffSize) { |
| 640 | aecpc->checkBufSizeCtr++; |
| 641 | // Before we fill up the far-end buffer we require the system delay |
| 642 | // to be stable (+/-8 ms) compared to the first value. This |
| 643 | // comparison is made during the following 6 consecutive 10 ms |
| 644 | // blocks. If it seems to be stable then we start to fill up the |
| 645 | // far-end buffer. |
| 646 | if (aecpc->counter == 0) { |
| 647 | aecpc->firstVal = aecpc->msInSndCardBuf; |
| 648 | aecpc->sum = 0; |
| 649 | } |
| 650 | |
| 651 | if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) < |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 652 | WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 653 | aecpc->sum += aecpc->msInSndCardBuf; |
| 654 | aecpc->counter++; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 655 | } else { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 656 | aecpc->counter = 0; |
| 657 | } |
| 658 | |
| 659 | if (aecpc->counter * nBlocks10ms >= 6) { |
| 660 | // The far-end buffer size is determined in partitions of |
| 661 | // PART_LEN samples. Use 75% of the average value of the system |
| 662 | // delay as buffer size to start with. |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 663 | aecpc->bufSizeStart = |
| 664 | WEBRTC_SPL_MIN((3 * aecpc->sum * aecpc->rate_factor * 8) / |
| 665 | (4 * aecpc->counter * PART_LEN), |
| 666 | kMaxBufSizeStart); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 667 | // Buffer size has now been determined. |
| 668 | aecpc->checkBuffSize = 0; |
| 669 | } |
| 670 | |
| 671 | if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) { |
| 672 | // For really bad systems, don't disable the echo canceller for |
| 673 | // more than 0.5 sec. |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 674 | aecpc->bufSizeStart = WEBRTC_SPL_MIN( |
| 675 | (aecpc->msInSndCardBuf * aecpc->rate_factor * 3) / 40, |
| 676 | kMaxBufSizeStart); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 677 | aecpc->checkBuffSize = 0; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // If |checkBuffSize| changed in the if-statement above. |
| 682 | if (!aecpc->checkBuffSize) { |
| 683 | // The system delay is now reasonably stable (or has been unstable |
| 684 | // for too long). When the far-end buffer is filled with |
| 685 | // approximately the same amount of data as reported by the system |
| 686 | // we end the startup phase. |
| 687 | int overhead_elements = |
| 688 | WebRtcAec_system_delay(aecpc->aec) / PART_LEN - aecpc->bufSizeStart; |
| 689 | if (overhead_elements == 0) { |
| 690 | // Enable the AEC |
| 691 | aecpc->startup_phase = 0; |
| 692 | } else if (overhead_elements > 0) { |
| 693 | // TODO(bjornv): Do we need a check on how much we actually |
| 694 | // moved the read pointer? It should always be possible to move |
| 695 | // the pointer |overhead_elements| since we have only added data |
| 696 | // to the buffer and no delay compensation nor AEC processing |
| 697 | // has been done. |
| 698 | WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements); |
| 699 | |
| 700 | // Enable the AEC |
| 701 | aecpc->startup_phase = 0; |
| 702 | } |
| 703 | } |
| 704 | } else { |
| 705 | // AEC is enabled. |
Bjorn Volcker | 7101269 | 2015-06-18 11:04:56 +0200 | [diff] [blame] | 706 | EstBufDelayNormal(aecpc); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 707 | |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 708 | // Call the AEC. |
| 709 | // TODO(bjornv): Re-structure such that we don't have to pass |
| 710 | // |aecpc->knownDelay| as input. Change name to something like |
| 711 | // |system_buffer_diff|. |
| 712 | WebRtcAec_ProcessFrames(aecpc->aec, |
| 713 | nearend, |
| 714 | num_bands, |
| 715 | nrOfSamples, |
| 716 | aecpc->knownDelay, |
| 717 | out); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | return retVal; |
| 721 | } |
| 722 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 723 | static void ProcessExtended(Aec* self, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 724 | const float* const* near, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 725 | size_t num_bands, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 726 | float* const* out, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 727 | size_t num_samples, |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 728 | int16_t reported_delay_ms, |
| 729 | int32_t skew) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 730 | size_t i; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 731 | const int delay_diff_offset = kDelayDiffOffsetSamples; |
bjornv@webrtc.org | 820f8e9 | 2014-08-11 15:39:00 +0000 | [diff] [blame] | 732 | #if defined(WEBRTC_UNTRUSTED_DELAY) |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 733 | reported_delay_ms = kFixedDelayMs; |
| 734 | #else |
| 735 | // This is the usual mode where we trust the reported system delay values. |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 736 | // Due to the longer filter, we no longer add 10 ms to the reported delay |
| 737 | // to reduce chance of non-causality. Instead we apply a minimum here to avoid |
| 738 | // issues with the read pointer jumping around needlessly. |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 739 | reported_delay_ms = reported_delay_ms < kMinTrustedDelayMs |
| 740 | ? kMinTrustedDelayMs |
| 741 | : reported_delay_ms; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 742 | // If the reported delay appears to be bogus, we attempt to recover by using |
| 743 | // the measured fixed delay values. We use >= here because higher layers |
| 744 | // may already clamp to this maximum value, and we would otherwise not |
| 745 | // detect it here. |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 746 | reported_delay_ms = reported_delay_ms >= kMaxTrustedDelayMs |
| 747 | ? kFixedDelayMs |
| 748 | : reported_delay_ms; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 749 | #endif |
| 750 | self->msInSndCardBuf = reported_delay_ms; |
| 751 | |
| 752 | if (!self->farend_started) { |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 753 | for (i = 0; i < num_bands; ++i) { |
| 754 | // Only needed if they don't already point to the same place. |
| 755 | if (near[i] != out[i]) { |
| 756 | memcpy(out[i], near[i], sizeof(near[i][0]) * num_samples); |
| 757 | } |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 758 | } |
| 759 | return; |
| 760 | } |
| 761 | if (self->startup_phase) { |
| 762 | // In the extended mode, there isn't a startup "phase", just a special |
| 763 | // action on the first frame. In the trusted delay case, we'll take the |
| 764 | // current reported delay, unless it's less then our conservative |
| 765 | // measurement. |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 766 | int startup_size_ms = |
| 767 | reported_delay_ms < kFixedDelayMs ? kFixedDelayMs : reported_delay_ms; |
Bjorn Volcker | 7101269 | 2015-06-18 11:04:56 +0200 | [diff] [blame] | 768 | #if defined(WEBRTC_ANDROID) |
Bjorn Volcker | 1ff218f | 2015-05-06 12:08:38 +0200 | [diff] [blame] | 769 | int target_delay = startup_size_ms * self->rate_factor * 8; |
Bjorn Volcker | 7101269 | 2015-06-18 11:04:56 +0200 | [diff] [blame] | 770 | #else |
Bjorn Volcker | 1ff218f | 2015-05-06 12:08:38 +0200 | [diff] [blame] | 771 | // To avoid putting the AEC in a non-causal state we're being slightly |
| 772 | // conservative and scale by 2. On Android we use a fixed delay and |
| 773 | // therefore there is no need to scale the target_delay. |
Bjorn Volcker | 7101269 | 2015-06-18 11:04:56 +0200 | [diff] [blame] | 774 | int target_delay = startup_size_ms * self->rate_factor * 8 / 2; |
Bjorn Volcker | 1ff218f | 2015-05-06 12:08:38 +0200 | [diff] [blame] | 775 | #endif |
| 776 | int overhead_elements = |
| 777 | (WebRtcAec_system_delay(self->aec) - target_delay) / PART_LEN; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 778 | WebRtcAec_MoveFarReadPtr(self->aec, overhead_elements); |
| 779 | self->startup_phase = 0; |
| 780 | } |
| 781 | |
Bjorn Volcker | 7101269 | 2015-06-18 11:04:56 +0200 | [diff] [blame] | 782 | EstBufDelayExtended(self); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 783 | |
andrew@webrtc.org | 8e2f9bc | 2013-10-01 01:12:25 +0000 | [diff] [blame] | 784 | { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 785 | // |delay_diff_offset| gives us the option to manually rewind the delay on |
| 786 | // very low delay platforms which can't be expressed purely through |
| 787 | // |reported_delay_ms|. |
andrew@webrtc.org | 8e2f9bc | 2013-10-01 01:12:25 +0000 | [diff] [blame] | 788 | const int adjusted_known_delay = |
| 789 | WEBRTC_SPL_MAX(0, self->knownDelay + delay_diff_offset); |
| 790 | |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 791 | WebRtcAec_ProcessFrames(self->aec, |
| 792 | near, |
| 793 | num_bands, |
| 794 | num_samples, |
| 795 | adjusted_known_delay, |
| 796 | out); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 800 | static void EstBufDelayNormal(Aec* aecpc) { |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 801 | int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor; |
bjornv@webrtc.org | 4d1cfae | 2013-02-20 17:31:38 +0000 | [diff] [blame] | 802 | int current_delay = nSampSndCard - WebRtcAec_system_delay(aecpc->aec); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 803 | int delay_difference = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 804 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 805 | // Before we proceed with the delay estimate filtering we: |
| 806 | // 1) Compensate for the frame that will be read. |
| 807 | // 2) Compensate for drift resampling. |
bjornv@webrtc.org | 7056908 | 2012-04-12 12:13:50 +0000 | [diff] [blame] | 808 | // 3) Compensate for non-causality if needed, since the estimated delay can't |
| 809 | // be negative. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 810 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 811 | // 1) Compensating for the frame(s) that will be read/processed. |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 812 | current_delay += FRAME_LEN * aecpc->rate_factor; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 813 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 814 | // 2) Account for resampling frame delay. |
| 815 | if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) { |
| 816 | current_delay -= kResamplingDelay; |
| 817 | } |
| 818 | |
bjornv@webrtc.org | 7056908 | 2012-04-12 12:13:50 +0000 | [diff] [blame] | 819 | // 3) Compensate for non-causality, if needed, by flushing one block. |
andrew@webrtc.org | 61bf8e3 | 2012-03-15 19:04:55 +0000 | [diff] [blame] | 820 | if (current_delay < PART_LEN) { |
| 821 | current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN; |
| 822 | } |
| 823 | |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 824 | // We use -1 to signal an initialized state in the "extended" implementation; |
| 825 | // compensate for that. |
| 826 | aecpc->filtDelay = aecpc->filtDelay < 0 ? 0 : aecpc->filtDelay; |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 827 | aecpc->filtDelay = |
| 828 | WEBRTC_SPL_MAX(0, (short)(0.8 * aecpc->filtDelay + 0.2 * current_delay)); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 829 | |
| 830 | delay_difference = aecpc->filtDelay - aecpc->knownDelay; |
| 831 | if (delay_difference > 224) { |
| 832 | if (aecpc->lastDelayDiff < 96) { |
| 833 | aecpc->timeForDelayChange = 0; |
| 834 | } else { |
| 835 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 836 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 837 | } else if (delay_difference < 96 && aecpc->knownDelay > 0) { |
| 838 | if (aecpc->lastDelayDiff > 224) { |
| 839 | aecpc->timeForDelayChange = 0; |
| 840 | } else { |
| 841 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 842 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 843 | } else { |
| 844 | aecpc->timeForDelayChange = 0; |
| 845 | } |
| 846 | aecpc->lastDelayDiff = delay_difference; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 847 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 848 | if (aecpc->timeForDelayChange > 25) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 849 | aecpc->knownDelay = WEBRTC_SPL_MAX((int)aecpc->filtDelay - 160, 0); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 850 | } |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 851 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 852 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 853 | static void EstBufDelayExtended(Aec* self) { |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 854 | int reported_delay = self->msInSndCardBuf * sampMsNb * self->rate_factor; |
| 855 | int current_delay = reported_delay - WebRtcAec_system_delay(self->aec); |
| 856 | int delay_difference = 0; |
| 857 | |
| 858 | // Before we proceed with the delay estimate filtering we: |
| 859 | // 1) Compensate for the frame that will be read. |
| 860 | // 2) Compensate for drift resampling. |
| 861 | // 3) Compensate for non-causality if needed, since the estimated delay can't |
| 862 | // be negative. |
| 863 | |
| 864 | // 1) Compensating for the frame(s) that will be read/processed. |
| 865 | current_delay += FRAME_LEN * self->rate_factor; |
| 866 | |
| 867 | // 2) Account for resampling frame delay. |
| 868 | if (self->skewMode == kAecTrue && self->resample == kAecTrue) { |
| 869 | current_delay -= kResamplingDelay; |
| 870 | } |
| 871 | |
| 872 | // 3) Compensate for non-causality, if needed, by flushing two blocks. |
| 873 | if (current_delay < PART_LEN) { |
| 874 | current_delay += WebRtcAec_MoveFarReadPtr(self->aec, 2) * PART_LEN; |
| 875 | } |
| 876 | |
| 877 | if (self->filtDelay == -1) { |
| 878 | self->filtDelay = WEBRTC_SPL_MAX(0, 0.5 * current_delay); |
| 879 | } else { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 880 | self->filtDelay = WEBRTC_SPL_MAX( |
| 881 | 0, (short)(0.95 * self->filtDelay + 0.05 * current_delay)); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | delay_difference = self->filtDelay - self->knownDelay; |
| 885 | if (delay_difference > 384) { |
| 886 | if (self->lastDelayDiff < 128) { |
| 887 | self->timeForDelayChange = 0; |
| 888 | } else { |
| 889 | self->timeForDelayChange++; |
| 890 | } |
| 891 | } else if (delay_difference < 128 && self->knownDelay > 0) { |
| 892 | if (self->lastDelayDiff > 384) { |
| 893 | self->timeForDelayChange = 0; |
| 894 | } else { |
| 895 | self->timeForDelayChange++; |
| 896 | } |
| 897 | } else { |
| 898 | self->timeForDelayChange = 0; |
| 899 | } |
| 900 | self->lastDelayDiff = delay_difference; |
| 901 | |
| 902 | if (self->timeForDelayChange > 25) { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 903 | self->knownDelay = WEBRTC_SPL_MAX((int)self->filtDelay - 256, 0); |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 904 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 905 | } |