blob: 14522c0f1d329a93e37d8f5084e680f5d9081740 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aecm/echo_control_mobile.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
niklase@google.com470e71d2011-07-07 08:21:25 +000013#ifdef AEC_DEBUG
14#include <stdio.h>
15#endif
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000016#include <stdlib.h>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <string.h>
andrew@webrtc.org9ae13542013-02-25 17:07:35 +000018
peah27045122016-04-10 22:38:14 -070019extern "C" {
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "common_audio/ring_buffer.h"
21#include "common_audio/signal_processing/include/signal_processing_library.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "modules/audio_processing/aecm/aecm_defines.h"
peah27045122016-04-10 22:38:14 -070023}
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/aecm/aecm_core.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
Per Åhgrenbe36db12020-03-20 16:45:09 +010026namespace webrtc {
27
28namespace {
29
Yves Gerey665174f2018-06-19 15:03:05 +020030#define BUF_SIZE_FRAMES 50 // buffer size (frames)
niklase@google.com470e71d2011-07-07 08:21:25 +000031// Maximum length of resampled signal. Must be an integer multiple of frames
32// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
33// The factor of 2 handles wb, and the + 1 is as a safety margin
34#define MAX_RESAMP_LEN (5 * FRAME_LEN)
35
Yves Gerey665174f2018-06-19 15:03:05 +020036static const size_t kBufSizeSamp =
37 BUF_SIZE_FRAMES * FRAME_LEN; // buffer size (samples)
38static const int kSampMsNb = 8; // samples per ms in nb
niklase@google.com470e71d2011-07-07 08:21:25 +000039// Target suppression levels for nlp modes
40// log{0.001, 0.00001, 0.00000001}
41static const int kInitCheck = 42;
42
Yves Gerey665174f2018-06-19 15:03:05 +020043typedef struct {
44 int sampFreq;
45 int scSampFreq;
46 short bufSizeStart;
47 int knownDelay;
niklase@google.com470e71d2011-07-07 08:21:25 +000048
Yves Gerey665174f2018-06-19 15:03:05 +020049 // Stores the last frame added to the farend buffer
50 short farendOld[2][FRAME_LEN];
51 short initFlag; // indicates if AEC has been initialized
niklase@google.com470e71d2011-07-07 08:21:25 +000052
Yves Gerey665174f2018-06-19 15:03:05 +020053 // Variables used for averaging far end buffer size
54 short counter;
55 short sum;
56 short firstVal;
57 short checkBufSizeCtr;
niklase@google.com470e71d2011-07-07 08:21:25 +000058
Yves Gerey665174f2018-06-19 15:03:05 +020059 // Variables used for delay shifts
60 short msInSndCardBuf;
61 short filtDelay;
62 int timeForDelayChange;
63 int ECstartup;
64 int checkBuffSize;
65 int delayChange;
66 short lastDelayDiff;
niklase@google.com470e71d2011-07-07 08:21:25 +000067
Yves Gerey665174f2018-06-19 15:03:05 +020068 int16_t echoMode;
niklase@google.com470e71d2011-07-07 08:21:25 +000069
70#ifdef AEC_DEBUG
Yves Gerey665174f2018-06-19 15:03:05 +020071 FILE* bufFile;
72 FILE* delayFile;
73 FILE* preCompFile;
74 FILE* postCompFile;
75#endif // AEC_DEBUG
76 // Structures
77 RingBuffer* farendBuf;
niklase@google.com470e71d2011-07-07 08:21:25 +000078
Yves Gerey665174f2018-06-19 15:03:05 +020079 AecmCore* aecmCore;
pbos@webrtc.orge468bc92014-12-18 09:11:33 +000080} AecMobile;
niklase@google.com470e71d2011-07-07 08:21:25 +000081
Per Åhgrenbe36db12020-03-20 16:45:09 +010082} // namespace
83
niklase@google.com470e71d2011-07-07 08:21:25 +000084// Estimates delay to set the position of the farend buffer read pointer
85// (controlled by knownDelay)
Alex Loiko890988c2017-08-31 10:25:48 +020086static int WebRtcAecm_EstBufDelay(AecMobile* aecm, short msInSndCardBuf);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
88// Stuffs the farend buffer if the estimated delay is too large
Alex Loiko890988c2017-08-31 10:25:48 +020089static int WebRtcAecm_DelayComp(AecMobile* aecm);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
Bjorn Volckera7437942015-05-28 15:58:42 +020091void* WebRtcAecm_Create() {
Alex Henrieab835fe2020-03-20 12:18:30 -060092 // Allocate zero-filled memory.
93 AecMobile* aecm = static_cast<AecMobile*>(calloc(1, sizeof(AecMobile)));
niklase@google.com470e71d2011-07-07 08:21:25 +000094
Yves Gerey665174f2018-06-19 15:03:05 +020095 aecm->aecmCore = WebRtcAecm_CreateCore();
96 if (!aecm->aecmCore) {
97 WebRtcAecm_Free(aecm);
98 return NULL;
99 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
Yves Gerey665174f2018-06-19 15:03:05 +0200101 aecm->farendBuf = WebRtc_CreateBuffer(kBufSizeSamp, sizeof(int16_t));
102 if (!aecm->farendBuf) {
103 WebRtcAecm_Free(aecm);
104 return NULL;
105 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
niklase@google.com470e71d2011-07-07 08:21:25 +0000107#ifdef AEC_DEBUG
Yves Gerey665174f2018-06-19 15:03:05 +0200108 aecm->aecmCore->farFile = fopen("aecFar.pcm", "wb");
109 aecm->aecmCore->nearFile = fopen("aecNear.pcm", "wb");
110 aecm->aecmCore->outFile = fopen("aecOut.pcm", "wb");
111 // aecm->aecmCore->outLpFile = fopen("aecOutLp.pcm","wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
Yves Gerey665174f2018-06-19 15:03:05 +0200113 aecm->bufFile = fopen("aecBuf.dat", "wb");
114 aecm->delayFile = fopen("aecDelay.dat", "wb");
115 aecm->preCompFile = fopen("preComp.pcm", "wb");
116 aecm->postCompFile = fopen("postComp.pcm", "wb");
117#endif // AEC_DEBUG
118 return aecm;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119}
120
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200121void WebRtcAecm_Free(void* aecmInst) {
peah27045122016-04-10 22:38:14 -0700122 AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
Yves Gerey665174f2018-06-19 15:03:05 +0200124 if (aecm == NULL) {
125 return;
126 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
128#ifdef AEC_DEBUG
Yves Gerey665174f2018-06-19 15:03:05 +0200129 fclose(aecm->aecmCore->farFile);
130 fclose(aecm->aecmCore->nearFile);
131 fclose(aecm->aecmCore->outFile);
132 // fclose(aecm->aecmCore->outLpFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
Yves Gerey665174f2018-06-19 15:03:05 +0200134 fclose(aecm->bufFile);
135 fclose(aecm->delayFile);
136 fclose(aecm->preCompFile);
137 fclose(aecm->postCompFile);
138#endif // AEC_DEBUG
139 WebRtcAecm_FreeCore(aecm->aecmCore);
140 WebRtc_FreeBuffer(aecm->farendBuf);
141 free(aecm);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142}
143
Yves Gerey665174f2018-06-19 15:03:05 +0200144int32_t WebRtcAecm_Init(void* aecmInst, int32_t sampFreq) {
145 AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
146 AecmConfig aecConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
Yves Gerey665174f2018-06-19 15:03:05 +0200148 if (aecm == NULL) {
149 return -1;
150 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
Yves Gerey665174f2018-06-19 15:03:05 +0200152 if (sampFreq != 8000 && sampFreq != 16000) {
153 return AECM_BAD_PARAMETER_ERROR;
154 }
155 aecm->sampFreq = sampFreq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
Yves Gerey665174f2018-06-19 15:03:05 +0200157 // Initialize AECM core
158 if (WebRtcAecm_InitCore(aecm->aecmCore, aecm->sampFreq) == -1) {
159 return AECM_UNSPECIFIED_ERROR;
160 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
Yves Gerey665174f2018-06-19 15:03:05 +0200162 // Initialize farend buffer
163 WebRtc_InitBuffer(aecm->farendBuf);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
Yves Gerey665174f2018-06-19 15:03:05 +0200165 aecm->initFlag = kInitCheck; // indicates that initialization has been done
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
Yves Gerey665174f2018-06-19 15:03:05 +0200167 aecm->delayChange = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
Yves Gerey665174f2018-06-19 15:03:05 +0200169 aecm->sum = 0;
170 aecm->counter = 0;
171 aecm->checkBuffSize = 1;
172 aecm->firstVal = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
Yves Gerey665174f2018-06-19 15:03:05 +0200174 aecm->ECstartup = 1;
175 aecm->bufSizeStart = 0;
176 aecm->checkBufSizeCtr = 0;
177 aecm->filtDelay = 0;
178 aecm->timeForDelayChange = 0;
179 aecm->knownDelay = 0;
180 aecm->lastDelayDiff = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
Yves Gerey665174f2018-06-19 15:03:05 +0200182 memset(&aecm->farendOld, 0, sizeof(aecm->farendOld));
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
Yves Gerey665174f2018-06-19 15:03:05 +0200184 // Default settings.
185 aecConfig.cngMode = AecmTrue;
186 aecConfig.echoMode = 3;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
Yves Gerey665174f2018-06-19 15:03:05 +0200188 if (WebRtcAecm_set_config(aecm, aecConfig) == -1) {
189 return AECM_UNSPECIFIED_ERROR;
190 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
Yves Gerey665174f2018-06-19 15:03:05 +0200192 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
peahc12be392015-11-09 23:53:50 -0800195// Returns any error that is caused when buffering the
196// farend signal.
Yves Gerey665174f2018-06-19 15:03:05 +0200197int32_t WebRtcAecm_GetBufferFarendError(void* aecmInst,
198 const int16_t* farend,
199 size_t nrOfSamples) {
peah27045122016-04-10 22:38:14 -0700200 AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
peahc12be392015-11-09 23:53:50 -0800202 if (aecm == NULL)
203 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000204
peahc12be392015-11-09 23:53:50 -0800205 if (farend == NULL)
206 return AECM_NULL_POINTER_ERROR;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
peahc12be392015-11-09 23:53:50 -0800208 if (aecm->initFlag != kInitCheck)
209 return AECM_UNINITIALIZED_ERROR;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
peahc12be392015-11-09 23:53:50 -0800211 if (nrOfSamples != 80 && nrOfSamples != 160)
212 return AECM_BAD_PARAMETER_ERROR;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
peahc12be392015-11-09 23:53:50 -0800214 return 0;
215}
niklase@google.com470e71d2011-07-07 08:21:25 +0000216
Yves Gerey665174f2018-06-19 15:03:05 +0200217int32_t WebRtcAecm_BufferFarend(void* aecmInst,
218 const int16_t* farend,
peahc12be392015-11-09 23:53:50 -0800219 size_t nrOfSamples) {
peah27045122016-04-10 22:38:14 -0700220 AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
peahc12be392015-11-09 23:53:50 -0800221
222 const int32_t err =
223 WebRtcAecm_GetBufferFarendError(aecmInst, farend, nrOfSamples);
224
225 if (err != 0)
226 return err;
227
228 // TODO(unknown): Is this really a good idea?
Yves Gerey665174f2018-06-19 15:03:05 +0200229 if (!aecm->ECstartup) {
peahc12be392015-11-09 23:53:50 -0800230 WebRtcAecm_DelayComp(aecm);
231 }
232
233 WebRtc_WriteBuffer(aecm->farendBuf, farend, nrOfSamples);
234
235 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236}
237
Yves Gerey665174f2018-06-19 15:03:05 +0200238int32_t WebRtcAecm_Process(void* aecmInst,
239 const int16_t* nearendNoisy,
240 const int16_t* nearendClean,
241 int16_t* out,
242 size_t nrOfSamples,
243 int16_t msInSndCardBuf) {
244 AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
245 int32_t retVal = 0;
246 size_t i;
247 short nmbrOfFilledBuffers;
248 size_t nBlocks10ms;
249 size_t nFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000250#ifdef AEC_DEBUG
Yves Gerey665174f2018-06-19 15:03:05 +0200251 short msInAECBuf;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252#endif
253
Yves Gerey665174f2018-06-19 15:03:05 +0200254 if (aecm == NULL) {
255 return -1;
256 }
257
258 if (nearendNoisy == NULL) {
259 return AECM_NULL_POINTER_ERROR;
260 }
261
262 if (out == NULL) {
263 return AECM_NULL_POINTER_ERROR;
264 }
265
266 if (aecm->initFlag != kInitCheck) {
267 return AECM_UNINITIALIZED_ERROR;
268 }
269
270 if (nrOfSamples != 80 && nrOfSamples != 160) {
271 return AECM_BAD_PARAMETER_ERROR;
272 }
273
274 if (msInSndCardBuf < 0) {
275 msInSndCardBuf = 0;
276 retVal = AECM_BAD_PARAMETER_WARNING;
277 } else if (msInSndCardBuf > 500) {
278 msInSndCardBuf = 500;
279 retVal = AECM_BAD_PARAMETER_WARNING;
280 }
281 msInSndCardBuf += 10;
282 aecm->msInSndCardBuf = msInSndCardBuf;
283
284 nFrames = nrOfSamples / FRAME_LEN;
285 nBlocks10ms = nFrames / aecm->aecmCore->mult;
286
287 if (aecm->ECstartup) {
288 if (nearendClean == NULL) {
289 if (out != nearendNoisy) {
290 memcpy(out, nearendNoisy, sizeof(short) * nrOfSamples);
291 }
292 } else if (out != nearendClean) {
293 memcpy(out, nearendClean, sizeof(short) * nrOfSamples);
294 }
295
296 nmbrOfFilledBuffers =
297 (short)WebRtc_available_read(aecm->farendBuf) / FRAME_LEN;
298 // The AECM is in the start up mode
299 // AECM is disabled until the soundcard buffer and farend buffers are OK
300
301 // Mechanism to ensure that the soundcard buffer is reasonably stable.
302 if (aecm->checkBuffSize) {
303 aecm->checkBufSizeCtr++;
304 // Before we fill up the far end buffer we require the amount of data on
305 // the sound card to be stable (+/-8 ms) compared to the first value. This
306 // comparison is made during the following 4 consecutive frames. If it
307 // seems to be stable then we start to fill up the far end buffer.
308
309 if (aecm->counter == 0) {
310 aecm->firstVal = aecm->msInSndCardBuf;
311 aecm->sum = 0;
312 }
313
314 if (abs(aecm->firstVal - aecm->msInSndCardBuf) <
315 WEBRTC_SPL_MAX(0.2 * aecm->msInSndCardBuf, kSampMsNb)) {
316 aecm->sum += aecm->msInSndCardBuf;
317 aecm->counter++;
318 } else {
319 aecm->counter = 0;
320 }
321
322 if (aecm->counter * nBlocks10ms >= 6) {
323 // The farend buffer size is determined in blocks of 80 samples
324 // Use 75% of the average value of the soundcard buffer
325 aecm->bufSizeStart = WEBRTC_SPL_MIN(
326 (3 * aecm->sum * aecm->aecmCore->mult) / (aecm->counter * 40),
327 BUF_SIZE_FRAMES);
328 // buffersize has now been determined
329 aecm->checkBuffSize = 0;
330 }
331
332 if (aecm->checkBufSizeCtr * nBlocks10ms > 50) {
333 // for really bad sound cards, don't disable echocanceller for more than
334 // 0.5 sec
335 aecm->bufSizeStart = WEBRTC_SPL_MIN(
336 (3 * aecm->msInSndCardBuf * aecm->aecmCore->mult) / 40,
337 BUF_SIZE_FRAMES);
338 aecm->checkBuffSize = 0;
339 }
340 }
341
342 // if checkBuffSize changed in the if-statement above
343 if (!aecm->checkBuffSize) {
344 // soundcard buffer is now reasonably stable
345 // When the far end buffer is filled with approximately the same amount of
346 // data as the amount on the sound card we end the start up phase and
347 // start to cancel echoes.
348
349 if (nmbrOfFilledBuffers == aecm->bufSizeStart) {
350 aecm->ECstartup = 0; // Enable the AECM
351 } else if (nmbrOfFilledBuffers > aecm->bufSizeStart) {
352 WebRtc_MoveReadPtr(aecm->farendBuf,
353 (int)WebRtc_available_read(aecm->farendBuf) -
354 (int)aecm->bufSizeStart * FRAME_LEN);
355 aecm->ECstartup = 0;
356 }
357 }
358
359 } else {
360 // AECM is enabled
361
362 // Note only 1 block supported for nb and 2 blocks for wb
363 for (i = 0; i < nFrames; i++) {
364 int16_t farend[FRAME_LEN];
365 const int16_t* farend_ptr = NULL;
366
367 nmbrOfFilledBuffers =
368 (short)WebRtc_available_read(aecm->farendBuf) / FRAME_LEN;
369
370 // Check that there is data in the far end buffer
371 if (nmbrOfFilledBuffers > 0) {
372 // Get the next 80 samples from the farend buffer
373 WebRtc_ReadBuffer(aecm->farendBuf, (void**)&farend_ptr, farend,
374 FRAME_LEN);
375
376 // Always store the last frame for use when we run out of data
377 memcpy(&(aecm->farendOld[i][0]), farend_ptr, FRAME_LEN * sizeof(short));
378 } else {
379 // We have no data so we use the last played frame
380 memcpy(farend, &(aecm->farendOld[i][0]), FRAME_LEN * sizeof(short));
381 farend_ptr = farend;
382 }
383
384 // Call buffer delay estimator when all data is extracted,
385 // i,e. i = 0 for NB and i = 1 for WB
386 if ((i == 0 && aecm->sampFreq == 8000) ||
387 (i == 1 && aecm->sampFreq == 16000)) {
388 WebRtcAecm_EstBufDelay(aecm, aecm->msInSndCardBuf);
389 }
390
391 // Call the AECM
392 /*WebRtcAecm_ProcessFrame(aecm->aecmCore, farend, &nearend[FRAME_LEN * i],
393 &out[FRAME_LEN * i], aecm->knownDelay);*/
394 if (WebRtcAecm_ProcessFrame(
395 aecm->aecmCore, farend_ptr, &nearendNoisy[FRAME_LEN * i],
396 (nearendClean ? &nearendClean[FRAME_LEN * i] : NULL),
397 &out[FRAME_LEN * i]) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000398 return -1;
399 }
Yves Gerey665174f2018-06-19 15:03:05 +0200400 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000401
402#ifdef AEC_DEBUG
Yves Gerey665174f2018-06-19 15:03:05 +0200403 msInAECBuf = (short)WebRtc_available_read(aecm->farendBuf) /
404 (kSampMsNb * aecm->aecmCore->mult);
405 fwrite(&msInAECBuf, 2, 1, aecm->bufFile);
406 fwrite(&(aecm->knownDelay), sizeof(aecm->knownDelay), 1, aecm->delayFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000407#endif
408
Yves Gerey665174f2018-06-19 15:03:05 +0200409 return retVal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410}
411
Yves Gerey665174f2018-06-19 15:03:05 +0200412int32_t WebRtcAecm_set_config(void* aecmInst, AecmConfig config) {
413 AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
Yves Gerey665174f2018-06-19 15:03:05 +0200415 if (aecm == NULL) {
416 return -1;
417 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
Yves Gerey665174f2018-06-19 15:03:05 +0200419 if (aecm->initFlag != kInitCheck) {
420 return AECM_UNINITIALIZED_ERROR;
421 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000422
Yves Gerey665174f2018-06-19 15:03:05 +0200423 if (config.cngMode != AecmFalse && config.cngMode != AecmTrue) {
424 return AECM_BAD_PARAMETER_ERROR;
425 }
426 aecm->aecmCore->cngMode = config.cngMode;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
Yves Gerey665174f2018-06-19 15:03:05 +0200428 if (config.echoMode < 0 || config.echoMode > 4) {
429 return AECM_BAD_PARAMETER_ERROR;
430 }
431 aecm->echoMode = config.echoMode;
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
Yves Gerey665174f2018-06-19 15:03:05 +0200433 if (aecm->echoMode == 0) {
434 aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 3;
435 aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 3;
436 aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 3;
437 aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 3;
438 aecm->aecmCore->supGainErrParamDiffAB =
439 (SUPGAIN_ERROR_PARAM_A >> 3) - (SUPGAIN_ERROR_PARAM_B >> 3);
440 aecm->aecmCore->supGainErrParamDiffBD =
441 (SUPGAIN_ERROR_PARAM_B >> 3) - (SUPGAIN_ERROR_PARAM_D >> 3);
442 } else if (aecm->echoMode == 1) {
443 aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 2;
444 aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 2;
445 aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 2;
446 aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 2;
447 aecm->aecmCore->supGainErrParamDiffAB =
448 (SUPGAIN_ERROR_PARAM_A >> 2) - (SUPGAIN_ERROR_PARAM_B >> 2);
449 aecm->aecmCore->supGainErrParamDiffBD =
450 (SUPGAIN_ERROR_PARAM_B >> 2) - (SUPGAIN_ERROR_PARAM_D >> 2);
451 } else if (aecm->echoMode == 2) {
452 aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 1;
453 aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 1;
454 aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 1;
455 aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 1;
456 aecm->aecmCore->supGainErrParamDiffAB =
457 (SUPGAIN_ERROR_PARAM_A >> 1) - (SUPGAIN_ERROR_PARAM_B >> 1);
458 aecm->aecmCore->supGainErrParamDiffBD =
459 (SUPGAIN_ERROR_PARAM_B >> 1) - (SUPGAIN_ERROR_PARAM_D >> 1);
460 } else if (aecm->echoMode == 3) {
461 aecm->aecmCore->supGain = SUPGAIN_DEFAULT;
462 aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT;
463 aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A;
464 aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D;
465 aecm->aecmCore->supGainErrParamDiffAB =
466 SUPGAIN_ERROR_PARAM_A - SUPGAIN_ERROR_PARAM_B;
467 aecm->aecmCore->supGainErrParamDiffBD =
468 SUPGAIN_ERROR_PARAM_B - SUPGAIN_ERROR_PARAM_D;
469 } else if (aecm->echoMode == 4) {
470 aecm->aecmCore->supGain = SUPGAIN_DEFAULT << 1;
471 aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT << 1;
472 aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A << 1;
473 aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D << 1;
474 aecm->aecmCore->supGainErrParamDiffAB =
475 (SUPGAIN_ERROR_PARAM_A << 1) - (SUPGAIN_ERROR_PARAM_B << 1);
476 aecm->aecmCore->supGainErrParamDiffBD =
477 (SUPGAIN_ERROR_PARAM_B << 1) - (SUPGAIN_ERROR_PARAM_D << 1);
478 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000479
Yves Gerey665174f2018-06-19 15:03:05 +0200480 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000481}
482
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000483int32_t WebRtcAecm_InitEchoPath(void* aecmInst,
484 const void* echo_path,
Yves Gerey665174f2018-06-19 15:03:05 +0200485 size_t size_bytes) {
486 AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
487 const int16_t* echo_path_ptr = static_cast<const int16_t*>(echo_path);
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000488
Yves Gerey665174f2018-06-19 15:03:05 +0200489 if (aecmInst == NULL) {
490 return -1;
491 }
492 if (echo_path == NULL) {
493 return AECM_NULL_POINTER_ERROR;
494 }
495 if (size_bytes != WebRtcAecm_echo_path_size_bytes()) {
496 // Input channel size does not match the size of AECM
497 return AECM_BAD_PARAMETER_ERROR;
498 }
499 if (aecm->initFlag != kInitCheck) {
500 return AECM_UNINITIALIZED_ERROR;
501 }
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000502
Yves Gerey665174f2018-06-19 15:03:05 +0200503 WebRtcAecm_InitEchoPathCore(aecm->aecmCore, echo_path_ptr);
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000504
Yves Gerey665174f2018-06-19 15:03:05 +0200505 return 0;
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000506}
507
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000508int32_t WebRtcAecm_GetEchoPath(void* aecmInst,
509 void* echo_path,
Yves Gerey665174f2018-06-19 15:03:05 +0200510 size_t size_bytes) {
511 AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
512 int16_t* echo_path_ptr = static_cast<int16_t*>(echo_path);
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000513
Yves Gerey665174f2018-06-19 15:03:05 +0200514 if (aecmInst == NULL) {
515 return -1;
516 }
517 if (echo_path == NULL) {
518 return AECM_NULL_POINTER_ERROR;
519 }
520 if (size_bytes != WebRtcAecm_echo_path_size_bytes()) {
521 // Input channel size does not match the size of AECM
522 return AECM_BAD_PARAMETER_ERROR;
523 }
524 if (aecm->initFlag != kInitCheck) {
525 return AECM_UNINITIALIZED_ERROR;
526 }
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000527
Yves Gerey665174f2018-06-19 15:03:05 +0200528 memcpy(echo_path_ptr, aecm->aecmCore->channelStored, size_bytes);
529 return 0;
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000530}
531
Yves Gerey665174f2018-06-19 15:03:05 +0200532size_t WebRtcAecm_echo_path_size_bytes() {
533 return (PART_LEN1 * sizeof(int16_t));
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000534}
535
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000536static int WebRtcAecm_EstBufDelay(AecMobile* aecm, short msInSndCardBuf) {
Yves Gerey665174f2018-06-19 15:03:05 +0200537 short delayNew, nSampSndCard;
538 short nSampFar = (short)WebRtc_available_read(aecm->farendBuf);
539 short diff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000540
Yves Gerey665174f2018-06-19 15:03:05 +0200541 nSampSndCard = msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult;
niklase@google.com470e71d2011-07-07 08:21:25 +0000542
Yves Gerey665174f2018-06-19 15:03:05 +0200543 delayNew = nSampSndCard - nSampFar;
niklase@google.com470e71d2011-07-07 08:21:25 +0000544
Yves Gerey665174f2018-06-19 15:03:05 +0200545 if (delayNew < FRAME_LEN) {
546 WebRtc_MoveReadPtr(aecm->farendBuf, FRAME_LEN);
547 delayNew += FRAME_LEN;
548 }
549
550 aecm->filtDelay =
551 WEBRTC_SPL_MAX(0, (8 * aecm->filtDelay + 2 * delayNew) / 10);
552
553 diff = aecm->filtDelay - aecm->knownDelay;
554 if (diff > 224) {
555 if (aecm->lastDelayDiff < 96) {
556 aecm->timeForDelayChange = 0;
557 } else {
558 aecm->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000559 }
Yves Gerey665174f2018-06-19 15:03:05 +0200560 } else if (diff < 96 && aecm->knownDelay > 0) {
561 if (aecm->lastDelayDiff > 224) {
562 aecm->timeForDelayChange = 0;
563 } else {
564 aecm->timeForDelayChange++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000565 }
Yves Gerey665174f2018-06-19 15:03:05 +0200566 } else {
567 aecm->timeForDelayChange = 0;
568 }
569 aecm->lastDelayDiff = diff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000570
Yves Gerey665174f2018-06-19 15:03:05 +0200571 if (aecm->timeForDelayChange > 25) {
572 aecm->knownDelay = WEBRTC_SPL_MAX((int)aecm->filtDelay - 160, 0);
573 }
574 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000575}
576
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000577static int WebRtcAecm_DelayComp(AecMobile* aecm) {
Yves Gerey665174f2018-06-19 15:03:05 +0200578 int nSampFar = (int)WebRtc_available_read(aecm->farendBuf);
579 int nSampSndCard, delayNew, nSampAdd;
580 const int maxStuffSamp = 10 * FRAME_LEN;
niklase@google.com470e71d2011-07-07 08:21:25 +0000581
Yves Gerey665174f2018-06-19 15:03:05 +0200582 nSampSndCard = aecm->msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult;
583 delayNew = nSampSndCard - nSampFar;
niklase@google.com470e71d2011-07-07 08:21:25 +0000584
Yves Gerey665174f2018-06-19 15:03:05 +0200585 if (delayNew > FAR_BUF_LEN - FRAME_LEN * aecm->aecmCore->mult) {
586 // The difference of the buffer sizes is larger than the maximum
587 // allowed known delay. Compensate by stuffing the buffer.
588 nSampAdd =
589 (int)(WEBRTC_SPL_MAX(((nSampSndCard >> 1) - nSampFar), FRAME_LEN));
590 nSampAdd = WEBRTC_SPL_MIN(nSampAdd, maxStuffSamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000591
Yves Gerey665174f2018-06-19 15:03:05 +0200592 WebRtc_MoveReadPtr(aecm->farendBuf, -nSampAdd);
593 aecm->delayChange = 1; // the delay needs to be updated
594 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
Yves Gerey665174f2018-06-19 15:03:05 +0200596 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000597}
Per Åhgrenbe36db12020-03-20 16:45:09 +0100598
599} // namespace webrtc