Update to current webrtc library
This is from the upstream library commit id 3326535126e435f1ba647885ce43a8f0f3d317eb, corresponding to Chromium 88.0.4290.1.
This commit is contained in:
44
webrtc/modules/audio_processing/aecm/BUILD.gn
Normal file
44
webrtc/modules/audio_processing/aecm/BUILD.gn
Normal file
@ -0,0 +1,44 @@
|
||||
# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
||||
#
|
||||
# Use of this source code is governed by a BSD-style license
|
||||
# that can be found in the LICENSE file in the root of the source
|
||||
# tree. An additional intellectual property rights grant can be found
|
||||
# in the file PATENTS. All contributing project authors may
|
||||
# be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
import("../../../webrtc.gni")
|
||||
|
||||
rtc_library("aecm_core") {
|
||||
sources = [
|
||||
"aecm_core.cc",
|
||||
"aecm_core.h",
|
||||
"aecm_defines.h",
|
||||
"echo_control_mobile.cc",
|
||||
"echo_control_mobile.h",
|
||||
]
|
||||
deps = [
|
||||
"../../../common_audio:common_audio_c",
|
||||
"../../../rtc_base:checks",
|
||||
"../../../rtc_base:rtc_base_approved",
|
||||
"../../../rtc_base:sanitizer",
|
||||
"../../../system_wrappers",
|
||||
"../utility:legacy_delay_estimator",
|
||||
]
|
||||
cflags = []
|
||||
|
||||
if (rtc_build_with_neon) {
|
||||
sources += [ "aecm_core_neon.cc" ]
|
||||
|
||||
if (current_cpu != "arm64") {
|
||||
# Enable compilation for the NEON instruction set.
|
||||
suppressed_configs += [ "//build/config/compiler:compiler_arm_fpu" ]
|
||||
cflags += [ "-mfpu=neon" ]
|
||||
}
|
||||
}
|
||||
|
||||
if (current_cpu == "mipsel") {
|
||||
sources += [ "aecm_core_mips.cc" ]
|
||||
} else {
|
||||
sources += [ "aecm_core_c.cc" ]
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
1125
webrtc/modules/audio_processing/aecm/aecm_core.cc
Normal file
1125
webrtc/modules/audio_processing/aecm/aecm_core.cc
Normal file
File diff suppressed because it is too large
Load Diff
@ -10,13 +10,18 @@
|
||||
|
||||
// Performs echo control (suppression) with fft routines in fixed-point.
|
||||
|
||||
#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AECM_AECM_CORE_H_
|
||||
#define WEBRTC_MODULES_AUDIO_PROCESSING_AECM_AECM_CORE_H_
|
||||
#ifndef MODULES_AUDIO_PROCESSING_AECM_AECM_CORE_H_
|
||||
#define MODULES_AUDIO_PROCESSING_AECM_AECM_CORE_H_
|
||||
|
||||
#include "webrtc/common_audio/ring_buffer.h"
|
||||
#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
|
||||
#include "webrtc/modules/audio_processing/aecm/aecm_defines.h"
|
||||
#include "webrtc/typedefs.h"
|
||||
extern "C" {
|
||||
#include "common_audio/ring_buffer.h"
|
||||
#include "common_audio/signal_processing/include/signal_processing_library.h"
|
||||
}
|
||||
#include "modules/audio_processing/aecm/aecm_defines.h"
|
||||
|
||||
struct RealFFT;
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
#ifdef _MSC_VER // visual c++
|
||||
#define ALIGN8_BEG __declspec(align(8))
|
||||
@ -27,109 +32,109 @@
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int16_t real;
|
||||
int16_t imag;
|
||||
int16_t real;
|
||||
int16_t imag;
|
||||
} ComplexInt16;
|
||||
|
||||
typedef struct {
|
||||
int farBufWritePos;
|
||||
int farBufReadPos;
|
||||
int knownDelay;
|
||||
int lastKnownDelay;
|
||||
int firstVAD; // Parameter to control poorly initialized channels
|
||||
int farBufWritePos;
|
||||
int farBufReadPos;
|
||||
int knownDelay;
|
||||
int lastKnownDelay;
|
||||
int firstVAD; // Parameter to control poorly initialized channels
|
||||
|
||||
RingBuffer* farFrameBuf;
|
||||
RingBuffer* nearNoisyFrameBuf;
|
||||
RingBuffer* nearCleanFrameBuf;
|
||||
RingBuffer* outFrameBuf;
|
||||
RingBuffer* farFrameBuf;
|
||||
RingBuffer* nearNoisyFrameBuf;
|
||||
RingBuffer* nearCleanFrameBuf;
|
||||
RingBuffer* outFrameBuf;
|
||||
|
||||
int16_t farBuf[FAR_BUF_LEN];
|
||||
int16_t farBuf[FAR_BUF_LEN];
|
||||
|
||||
int16_t mult;
|
||||
uint32_t seed;
|
||||
int16_t mult;
|
||||
uint32_t seed;
|
||||
|
||||
// Delay estimation variables
|
||||
void* delay_estimator_farend;
|
||||
void* delay_estimator;
|
||||
uint16_t currentDelay;
|
||||
// Far end history variables
|
||||
// TODO(bjornv): Replace |far_history| with ring_buffer.
|
||||
uint16_t far_history[PART_LEN1 * MAX_DELAY];
|
||||
int far_history_pos;
|
||||
int far_q_domains[MAX_DELAY];
|
||||
// Delay estimation variables
|
||||
void* delay_estimator_farend;
|
||||
void* delay_estimator;
|
||||
uint16_t currentDelay;
|
||||
// Far end history variables
|
||||
// TODO(bjornv): Replace |far_history| with ring_buffer.
|
||||
uint16_t far_history[PART_LEN1 * MAX_DELAY];
|
||||
int far_history_pos;
|
||||
int far_q_domains[MAX_DELAY];
|
||||
|
||||
int16_t nlpFlag;
|
||||
int16_t fixedDelay;
|
||||
int16_t nlpFlag;
|
||||
int16_t fixedDelay;
|
||||
|
||||
uint32_t totCount;
|
||||
uint32_t totCount;
|
||||
|
||||
int16_t dfaCleanQDomain;
|
||||
int16_t dfaCleanQDomainOld;
|
||||
int16_t dfaNoisyQDomain;
|
||||
int16_t dfaNoisyQDomainOld;
|
||||
int16_t dfaCleanQDomain;
|
||||
int16_t dfaCleanQDomainOld;
|
||||
int16_t dfaNoisyQDomain;
|
||||
int16_t dfaNoisyQDomainOld;
|
||||
|
||||
int16_t nearLogEnergy[MAX_BUF_LEN];
|
||||
int16_t farLogEnergy;
|
||||
int16_t echoAdaptLogEnergy[MAX_BUF_LEN];
|
||||
int16_t echoStoredLogEnergy[MAX_BUF_LEN];
|
||||
int16_t nearLogEnergy[MAX_BUF_LEN];
|
||||
int16_t farLogEnergy;
|
||||
int16_t echoAdaptLogEnergy[MAX_BUF_LEN];
|
||||
int16_t echoStoredLogEnergy[MAX_BUF_LEN];
|
||||
|
||||
// The extra 16 or 32 bytes in the following buffers are for alignment based
|
||||
// Neon code.
|
||||
// It's designed this way since the current GCC compiler can't align a
|
||||
// buffer in 16 or 32 byte boundaries properly.
|
||||
int16_t channelStored_buf[PART_LEN1 + 8];
|
||||
int16_t channelAdapt16_buf[PART_LEN1 + 8];
|
||||
int32_t channelAdapt32_buf[PART_LEN1 + 8];
|
||||
int16_t xBuf_buf[PART_LEN2 + 16]; // farend
|
||||
int16_t dBufClean_buf[PART_LEN2 + 16]; // nearend
|
||||
int16_t dBufNoisy_buf[PART_LEN2 + 16]; // nearend
|
||||
int16_t outBuf_buf[PART_LEN + 8];
|
||||
// The extra 16 or 32 bytes in the following buffers are for alignment based
|
||||
// Neon code.
|
||||
// It's designed this way since the current GCC compiler can't align a
|
||||
// buffer in 16 or 32 byte boundaries properly.
|
||||
int16_t channelStored_buf[PART_LEN1 + 8];
|
||||
int16_t channelAdapt16_buf[PART_LEN1 + 8];
|
||||
int32_t channelAdapt32_buf[PART_LEN1 + 8];
|
||||
int16_t xBuf_buf[PART_LEN2 + 16]; // farend
|
||||
int16_t dBufClean_buf[PART_LEN2 + 16]; // nearend
|
||||
int16_t dBufNoisy_buf[PART_LEN2 + 16]; // nearend
|
||||
int16_t outBuf_buf[PART_LEN + 8];
|
||||
|
||||
// Pointers to the above buffers
|
||||
int16_t *channelStored;
|
||||
int16_t *channelAdapt16;
|
||||
int32_t *channelAdapt32;
|
||||
int16_t *xBuf;
|
||||
int16_t *dBufClean;
|
||||
int16_t *dBufNoisy;
|
||||
int16_t *outBuf;
|
||||
// Pointers to the above buffers
|
||||
int16_t* channelStored;
|
||||
int16_t* channelAdapt16;
|
||||
int32_t* channelAdapt32;
|
||||
int16_t* xBuf;
|
||||
int16_t* dBufClean;
|
||||
int16_t* dBufNoisy;
|
||||
int16_t* outBuf;
|
||||
|
||||
int32_t echoFilt[PART_LEN1];
|
||||
int16_t nearFilt[PART_LEN1];
|
||||
int32_t noiseEst[PART_LEN1];
|
||||
int noiseEstTooLowCtr[PART_LEN1];
|
||||
int noiseEstTooHighCtr[PART_LEN1];
|
||||
int16_t noiseEstCtr;
|
||||
int16_t cngMode;
|
||||
int32_t echoFilt[PART_LEN1];
|
||||
int16_t nearFilt[PART_LEN1];
|
||||
int32_t noiseEst[PART_LEN1];
|
||||
int noiseEstTooLowCtr[PART_LEN1];
|
||||
int noiseEstTooHighCtr[PART_LEN1];
|
||||
int16_t noiseEstCtr;
|
||||
int16_t cngMode;
|
||||
|
||||
int32_t mseAdaptOld;
|
||||
int32_t mseStoredOld;
|
||||
int32_t mseThreshold;
|
||||
int32_t mseAdaptOld;
|
||||
int32_t mseStoredOld;
|
||||
int32_t mseThreshold;
|
||||
|
||||
int16_t farEnergyMin;
|
||||
int16_t farEnergyMax;
|
||||
int16_t farEnergyMaxMin;
|
||||
int16_t farEnergyVAD;
|
||||
int16_t farEnergyMSE;
|
||||
int currentVADValue;
|
||||
int16_t vadUpdateCount;
|
||||
int16_t farEnergyMin;
|
||||
int16_t farEnergyMax;
|
||||
int16_t farEnergyMaxMin;
|
||||
int16_t farEnergyVAD;
|
||||
int16_t farEnergyMSE;
|
||||
int currentVADValue;
|
||||
int16_t vadUpdateCount;
|
||||
|
||||
int16_t startupState;
|
||||
int16_t mseChannelCount;
|
||||
int16_t supGain;
|
||||
int16_t supGainOld;
|
||||
int16_t startupState;
|
||||
int16_t mseChannelCount;
|
||||
int16_t supGain;
|
||||
int16_t supGainOld;
|
||||
|
||||
int16_t supGainErrParamA;
|
||||
int16_t supGainErrParamD;
|
||||
int16_t supGainErrParamDiffAB;
|
||||
int16_t supGainErrParamDiffBD;
|
||||
int16_t supGainErrParamA;
|
||||
int16_t supGainErrParamD;
|
||||
int16_t supGainErrParamDiffAB;
|
||||
int16_t supGainErrParamDiffBD;
|
||||
|
||||
struct RealFFT* real_fft;
|
||||
struct RealFFT* real_fft;
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
FILE *farFile;
|
||||
FILE *nearFile;
|
||||
FILE *outFile;
|
||||
FILE* farFile;
|
||||
FILE* nearFile;
|
||||
FILE* outFile;
|
||||
#endif
|
||||
} AecmCore;
|
||||
|
||||
@ -400,7 +405,7 @@ extern ResetAdaptiveChannel WebRtcAecm_ResetAdaptiveChannel;
|
||||
// For the above function pointers, functions for generic platforms are declared
|
||||
// and defined as static in file aecm_core.c, while those for ARM Neon platforms
|
||||
// are declared below and defined in file aecm_core_neon.c.
|
||||
#if defined(WEBRTC_DETECT_NEON) || defined(WEBRTC_HAS_NEON)
|
||||
#if defined(WEBRTC_HAS_NEON)
|
||||
void WebRtcAecm_CalcLinearEnergiesNeon(AecmCore* aecm,
|
||||
const uint16_t* far_spectrum,
|
||||
int32_t* echo_est,
|
||||
@ -431,4 +436,6 @@ void WebRtcAecm_ResetAdaptiveChannel_mips(AecmCore* aecm);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif
|
||||
|
@ -8,49 +8,50 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "webrtc/modules/audio_processing/aecm/aecm_core.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "webrtc/common_audio/ring_buffer.h"
|
||||
#include "webrtc/common_audio/signal_processing/include/real_fft.h"
|
||||
#include "webrtc/modules/audio_processing/aecm/include/echo_control_mobile.h"
|
||||
#include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h"
|
||||
#include "webrtc/system_wrappers/include/compile_assert_c.h"
|
||||
#include "webrtc/system_wrappers/include/cpu_features_wrapper.h"
|
||||
#include "webrtc/typedefs.h"
|
||||
#include "modules/audio_processing/aecm/aecm_core.h"
|
||||
|
||||
extern "C" {
|
||||
#include "common_audio/ring_buffer.h"
|
||||
#include "common_audio/signal_processing/include/real_fft.h"
|
||||
}
|
||||
#include "modules/audio_processing/aecm/echo_control_mobile.h"
|
||||
#include "modules/audio_processing/utility/delay_estimator_wrapper.h"
|
||||
extern "C" {
|
||||
#include "system_wrappers/include/cpu_features_wrapper.h"
|
||||
}
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/numerics/safe_conversions.h"
|
||||
#include "rtc_base/sanitizer.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
namespace {
|
||||
|
||||
// Square root of Hanning window in Q14.
|
||||
#if defined(WEBRTC_DETECT_NEON) || defined(WEBRTC_HAS_NEON)
|
||||
// Table is defined in an ARM assembly file.
|
||||
extern const ALIGN8_BEG int16_t WebRtcAecm_kSqrtHanning[] ALIGN8_END;
|
||||
#else
|
||||
static const ALIGN8_BEG int16_t WebRtcAecm_kSqrtHanning[] ALIGN8_END = {
|
||||
0, 399, 798, 1196, 1594, 1990, 2386, 2780, 3172,
|
||||
3562, 3951, 4337, 4720, 5101, 5478, 5853, 6224,
|
||||
6591, 6954, 7313, 7668, 8019, 8364, 8705, 9040,
|
||||
9370, 9695, 10013, 10326, 10633, 10933, 11227, 11514,
|
||||
11795, 12068, 12335, 12594, 12845, 13089, 13325, 13553,
|
||||
13773, 13985, 14189, 14384, 14571, 14749, 14918, 15079,
|
||||
15231, 15373, 15506, 15631, 15746, 15851, 15947, 16034,
|
||||
16111, 16179, 16237, 16286, 16325, 16354, 16373, 16384
|
||||
};
|
||||
#endif
|
||||
0, 399, 798, 1196, 1594, 1990, 2386, 2780, 3172, 3562, 3951,
|
||||
4337, 4720, 5101, 5478, 5853, 6224, 6591, 6954, 7313, 7668, 8019,
|
||||
8364, 8705, 9040, 9370, 9695, 10013, 10326, 10633, 10933, 11227, 11514,
|
||||
11795, 12068, 12335, 12594, 12845, 13089, 13325, 13553, 13773, 13985, 14189,
|
||||
14384, 14571, 14749, 14918, 15079, 15231, 15373, 15506, 15631, 15746, 15851,
|
||||
15947, 16034, 16111, 16179, 16237, 16286, 16325, 16354, 16373, 16384};
|
||||
|
||||
#ifdef AECM_WITH_ABS_APPROX
|
||||
//Q15 alpha = 0.99439986968132 const Factor for magnitude approximation
|
||||
// Q15 alpha = 0.99439986968132 const Factor for magnitude approximation
|
||||
static const uint16_t kAlpha1 = 32584;
|
||||
//Q15 beta = 0.12967166976970 const Factor for magnitude approximation
|
||||
// Q15 beta = 0.12967166976970 const Factor for magnitude approximation
|
||||
static const uint16_t kBeta1 = 4249;
|
||||
//Q15 alpha = 0.94234827210087 const Factor for magnitude approximation
|
||||
// Q15 alpha = 0.94234827210087 const Factor for magnitude approximation
|
||||
static const uint16_t kAlpha2 = 30879;
|
||||
//Q15 beta = 0.33787806009150 const Factor for magnitude approximation
|
||||
// Q15 beta = 0.33787806009150 const Factor for magnitude approximation
|
||||
static const uint16_t kBeta2 = 11072;
|
||||
//Q15 alpha = 0.82247698684306 const Factor for magnitude approximation
|
||||
// Q15 alpha = 0.82247698684306 const Factor for magnitude approximation
|
||||
static const uint16_t kAlpha3 = 26951;
|
||||
//Q15 beta = 0.57762063060713 const Factor for magnitude approximation
|
||||
// Q15 beta = 0.57762063060713 const Factor for magnitude approximation
|
||||
static const uint16_t kBeta3 = 18927;
|
||||
#endif
|
||||
|
||||
@ -60,7 +61,115 @@ static const int16_t kNoiseEstIncCount = 5;
|
||||
static void ComfortNoise(AecmCore* aecm,
|
||||
const uint16_t* dfa,
|
||||
ComplexInt16* out,
|
||||
const int16_t* lambda);
|
||||
const int16_t* lambda) {
|
||||
int16_t i;
|
||||
int16_t tmp16;
|
||||
int32_t tmp32;
|
||||
|
||||
int16_t randW16[PART_LEN];
|
||||
int16_t uReal[PART_LEN1];
|
||||
int16_t uImag[PART_LEN1];
|
||||
int32_t outLShift32;
|
||||
int16_t noiseRShift16[PART_LEN1];
|
||||
|
||||
int16_t shiftFromNearToNoise = kNoiseEstQDomain - aecm->dfaCleanQDomain;
|
||||
int16_t minTrackShift;
|
||||
|
||||
RTC_DCHECK_GE(shiftFromNearToNoise, 0);
|
||||
RTC_DCHECK_LT(shiftFromNearToNoise, 16);
|
||||
|
||||
if (aecm->noiseEstCtr < 100) {
|
||||
// Track the minimum more quickly initially.
|
||||
aecm->noiseEstCtr++;
|
||||
minTrackShift = 6;
|
||||
} else {
|
||||
minTrackShift = 9;
|
||||
}
|
||||
|
||||
// Estimate noise power.
|
||||
for (i = 0; i < PART_LEN1; i++) {
|
||||
// Shift to the noise domain.
|
||||
tmp32 = (int32_t)dfa[i];
|
||||
outLShift32 = tmp32 << shiftFromNearToNoise;
|
||||
|
||||
if (outLShift32 < aecm->noiseEst[i]) {
|
||||
// Reset "too low" counter
|
||||
aecm->noiseEstTooLowCtr[i] = 0;
|
||||
// Track the minimum.
|
||||
if (aecm->noiseEst[i] < (1 << minTrackShift)) {
|
||||
// For small values, decrease noiseEst[i] every
|
||||
// |kNoiseEstIncCount| block. The regular approach below can not
|
||||
// go further down due to truncation.
|
||||
aecm->noiseEstTooHighCtr[i]++;
|
||||
if (aecm->noiseEstTooHighCtr[i] >= kNoiseEstIncCount) {
|
||||
aecm->noiseEst[i]--;
|
||||
aecm->noiseEstTooHighCtr[i] = 0; // Reset the counter
|
||||
}
|
||||
} else {
|
||||
aecm->noiseEst[i] -=
|
||||
((aecm->noiseEst[i] - outLShift32) >> minTrackShift);
|
||||
}
|
||||
} else {
|
||||
// Reset "too high" counter
|
||||
aecm->noiseEstTooHighCtr[i] = 0;
|
||||
// Ramp slowly upwards until we hit the minimum again.
|
||||
if ((aecm->noiseEst[i] >> 19) > 0) {
|
||||
// Avoid overflow.
|
||||
// Multiplication with 2049 will cause wrap around. Scale
|
||||
// down first and then multiply
|
||||
aecm->noiseEst[i] >>= 11;
|
||||
aecm->noiseEst[i] *= 2049;
|
||||
} else if ((aecm->noiseEst[i] >> 11) > 0) {
|
||||
// Large enough for relative increase
|
||||
aecm->noiseEst[i] *= 2049;
|
||||
aecm->noiseEst[i] >>= 11;
|
||||
} else {
|
||||
// Make incremental increases based on size every
|
||||
// |kNoiseEstIncCount| block
|
||||
aecm->noiseEstTooLowCtr[i]++;
|
||||
if (aecm->noiseEstTooLowCtr[i] >= kNoiseEstIncCount) {
|
||||
aecm->noiseEst[i] += (aecm->noiseEst[i] >> 9) + 1;
|
||||
aecm->noiseEstTooLowCtr[i] = 0; // Reset counter
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < PART_LEN1; i++) {
|
||||
tmp32 = aecm->noiseEst[i] >> shiftFromNearToNoise;
|
||||
if (tmp32 > 32767) {
|
||||
tmp32 = 32767;
|
||||
aecm->noiseEst[i] = tmp32 << shiftFromNearToNoise;
|
||||
}
|
||||
noiseRShift16[i] = (int16_t)tmp32;
|
||||
|
||||
tmp16 = ONE_Q14 - lambda[i];
|
||||
noiseRShift16[i] = (int16_t)((tmp16 * noiseRShift16[i]) >> 14);
|
||||
}
|
||||
|
||||
// Generate a uniform random array on [0 2^15-1].
|
||||
WebRtcSpl_RandUArray(randW16, PART_LEN, &aecm->seed);
|
||||
|
||||
// Generate noise according to estimated energy.
|
||||
uReal[0] = 0; // Reject LF noise.
|
||||
uImag[0] = 0;
|
||||
for (i = 1; i < PART_LEN1; i++) {
|
||||
// Get a random index for the cos and sin tables over [0 359].
|
||||
tmp16 = (int16_t)((359 * randW16[i - 1]) >> 15);
|
||||
|
||||
// Tables are in Q13.
|
||||
uReal[i] =
|
||||
(int16_t)((noiseRShift16[i] * WebRtcAecm_kCosTable[tmp16]) >> 13);
|
||||
uImag[i] =
|
||||
(int16_t)((-noiseRShift16[i] * WebRtcAecm_kSinTable[tmp16]) >> 13);
|
||||
}
|
||||
uImag[PART_LEN] = 0;
|
||||
|
||||
for (i = 0; i < PART_LEN1; i++) {
|
||||
out[i].real = WebRtcSpl_AddSatW16(out[i].real, uReal[i]);
|
||||
out[i].imag = WebRtcSpl_AddSatW16(out[i].imag, uImag[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void WindowAndFFT(AecmCore* aecm,
|
||||
int16_t* fft,
|
||||
@ -73,11 +182,11 @@ static void WindowAndFFT(AecmCore* aecm,
|
||||
for (i = 0; i < PART_LEN; i++) {
|
||||
// Window time domain signal and insert into real part of
|
||||
// transformation array |fft|
|
||||
int16_t scaled_time_signal = time_signal[i] << time_signal_scaling;
|
||||
int16_t scaled_time_signal = time_signal[i] * (1 << time_signal_scaling);
|
||||
fft[i] = (int16_t)((scaled_time_signal * WebRtcAecm_kSqrtHanning[i]) >> 14);
|
||||
scaled_time_signal = time_signal[i + PART_LEN] << time_signal_scaling;
|
||||
fft[PART_LEN + i] = (int16_t)((
|
||||
scaled_time_signal * WebRtcAecm_kSqrtHanning[PART_LEN - i]) >> 14);
|
||||
scaled_time_signal = time_signal[i + PART_LEN] * (1 << time_signal_scaling);
|
||||
fft[PART_LEN + i] = (int16_t)(
|
||||
(scaled_time_signal * WebRtcAecm_kSqrtHanning[PART_LEN - i]) >> 14);
|
||||
}
|
||||
|
||||
// Do forward FFT, then take only the first PART_LEN complex samples,
|
||||
@ -114,32 +223,27 @@ static void InverseFFTAndWindow(AecmCore* aecm,
|
||||
outCFFT = WebRtcSpl_RealInverseFFT(aecm->real_fft, fft, ifft_out);
|
||||
for (i = 0; i < PART_LEN; i++) {
|
||||
ifft_out[i] = (int16_t)WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(
|
||||
ifft_out[i], WebRtcAecm_kSqrtHanning[i], 14);
|
||||
ifft_out[i], WebRtcAecm_kSqrtHanning[i], 14);
|
||||
tmp32no1 = WEBRTC_SPL_SHIFT_W32((int32_t)ifft_out[i],
|
||||
outCFFT - aecm->dfaCleanQDomain);
|
||||
outCFFT - aecm->dfaCleanQDomain);
|
||||
output[i] = (int16_t)WEBRTC_SPL_SAT(WEBRTC_SPL_WORD16_MAX,
|
||||
tmp32no1 + aecm->outBuf[i],
|
||||
WEBRTC_SPL_WORD16_MIN);
|
||||
|
||||
tmp32no1 = (ifft_out[PART_LEN + i] *
|
||||
WebRtcAecm_kSqrtHanning[PART_LEN - i]) >> 14;
|
||||
tmp32no1 = WEBRTC_SPL_SHIFT_W32(tmp32no1,
|
||||
outCFFT - aecm->dfaCleanQDomain);
|
||||
aecm->outBuf[i] = (int16_t)WEBRTC_SPL_SAT(WEBRTC_SPL_WORD16_MAX,
|
||||
tmp32no1,
|
||||
WEBRTC_SPL_WORD16_MIN);
|
||||
tmp32no1 =
|
||||
(ifft_out[PART_LEN + i] * WebRtcAecm_kSqrtHanning[PART_LEN - i]) >> 14;
|
||||
tmp32no1 = WEBRTC_SPL_SHIFT_W32(tmp32no1, outCFFT - aecm->dfaCleanQDomain);
|
||||
aecm->outBuf[i] = (int16_t)WEBRTC_SPL_SAT(WEBRTC_SPL_WORD16_MAX, tmp32no1,
|
||||
WEBRTC_SPL_WORD16_MIN);
|
||||
}
|
||||
|
||||
// Copy the current block to the old position
|
||||
// (aecm->outBuf is shifted elsewhere)
|
||||
memcpy(aecm->xBuf, aecm->xBuf + PART_LEN, sizeof(int16_t) * PART_LEN);
|
||||
memcpy(aecm->dBufNoisy,
|
||||
aecm->dBufNoisy + PART_LEN,
|
||||
memcpy(aecm->dBufNoisy, aecm->dBufNoisy + PART_LEN,
|
||||
sizeof(int16_t) * PART_LEN);
|
||||
if (nearendClean != NULL)
|
||||
{
|
||||
memcpy(aecm->dBufClean,
|
||||
aecm->dBufClean + PART_LEN,
|
||||
if (nearendClean != NULL) {
|
||||
memcpy(aecm->dBufClean, aecm->dBufClean + PART_LEN,
|
||||
sizeof(int16_t) * PART_LEN);
|
||||
}
|
||||
}
|
||||
@ -170,7 +274,7 @@ static int TimeToFrequencyDomain(AecmCore* aecm,
|
||||
|
||||
// In fft_buf, +16 for 32-byte alignment.
|
||||
int16_t fft_buf[PART_LEN4 + 16];
|
||||
int16_t *fft = (int16_t *) (((uintptr_t) fft_buf + 31) & ~31);
|
||||
int16_t* fft = (int16_t*)(((uintptr_t)fft_buf + 31) & ~31);
|
||||
|
||||
int16_t tmp16no1;
|
||||
#ifndef WEBRTC_ARCH_ARM_V7
|
||||
@ -195,23 +299,17 @@ static int TimeToFrequencyDomain(AecmCore* aecm,
|
||||
freq_signal[0].imag = 0;
|
||||
freq_signal[PART_LEN].imag = 0;
|
||||
freq_signal_abs[0] = (uint16_t)WEBRTC_SPL_ABS_W16(freq_signal[0].real);
|
||||
freq_signal_abs[PART_LEN] = (uint16_t)WEBRTC_SPL_ABS_W16(
|
||||
freq_signal[PART_LEN].real);
|
||||
(*freq_signal_sum_abs) = (uint32_t)(freq_signal_abs[0]) +
|
||||
(uint32_t)(freq_signal_abs[PART_LEN]);
|
||||
freq_signal_abs[PART_LEN] =
|
||||
(uint16_t)WEBRTC_SPL_ABS_W16(freq_signal[PART_LEN].real);
|
||||
(*freq_signal_sum_abs) =
|
||||
(uint32_t)(freq_signal_abs[0]) + (uint32_t)(freq_signal_abs[PART_LEN]);
|
||||
|
||||
for (i = 1; i < PART_LEN; i++)
|
||||
{
|
||||
if (freq_signal[i].real == 0)
|
||||
{
|
||||
for (i = 1; i < PART_LEN; i++) {
|
||||
if (freq_signal[i].real == 0) {
|
||||
freq_signal_abs[i] = (uint16_t)WEBRTC_SPL_ABS_W16(freq_signal[i].imag);
|
||||
}
|
||||
else if (freq_signal[i].imag == 0)
|
||||
{
|
||||
} else if (freq_signal[i].imag == 0) {
|
||||
freq_signal_abs[i] = (uint16_t)WEBRTC_SPL_ABS_W16(freq_signal[i].real);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Approximation for magnitude of complex fft output
|
||||
// magn = sqrt(real^2 + imag^2)
|
||||
// magn ~= alpha * max(|imag|,|real|) + beta * min(|imag|,|real|)
|
||||
@ -222,27 +320,22 @@ static int TimeToFrequencyDomain(AecmCore* aecm,
|
||||
tmp16no1 = WEBRTC_SPL_ABS_W16(freq_signal[i].real);
|
||||
tmp16no2 = WEBRTC_SPL_ABS_W16(freq_signal[i].imag);
|
||||
|
||||
if(tmp16no1 > tmp16no2)
|
||||
{
|
||||
if (tmp16no1 > tmp16no2) {
|
||||
max_value = tmp16no1;
|
||||
min_value = tmp16no2;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
max_value = tmp16no2;
|
||||
min_value = tmp16no1;
|
||||
}
|
||||
|
||||
// Magnitude in Q(-6)
|
||||
if ((max_value >> 2) > min_value)
|
||||
{
|
||||
if ((max_value >> 2) > min_value) {
|
||||
alpha = kAlpha1;
|
||||
beta = kBeta1;
|
||||
} else if ((max_value >> 1) > min_value)
|
||||
{
|
||||
} else if ((max_value >> 1) > min_value) {
|
||||
alpha = kAlpha2;
|
||||
beta = kBeta2;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
alpha = kAlpha3;
|
||||
beta = kBeta3;
|
||||
}
|
||||
@ -252,24 +345,21 @@ static int TimeToFrequencyDomain(AecmCore* aecm,
|
||||
#else
|
||||
#ifdef WEBRTC_ARCH_ARM_V7
|
||||
__asm __volatile(
|
||||
"smulbb %[tmp32no1], %[real], %[real]\n\t"
|
||||
"smlabb %[tmp32no2], %[imag], %[imag], %[tmp32no1]\n\t"
|
||||
:[tmp32no1]"+&r"(tmp32no1),
|
||||
[tmp32no2]"=r"(tmp32no2)
|
||||
:[real]"r"(freq_signal[i].real),
|
||||
[imag]"r"(freq_signal[i].imag)
|
||||
);
|
||||
"smulbb %[tmp32no1], %[real], %[real]\n\t"
|
||||
"smlabb %[tmp32no2], %[imag], %[imag], %[tmp32no1]\n\t"
|
||||
: [tmp32no1] "+&r"(tmp32no1), [tmp32no2] "=r"(tmp32no2)
|
||||
: [real] "r"(freq_signal[i].real), [imag] "r"(freq_signal[i].imag));
|
||||
#else
|
||||
tmp16no1 = WEBRTC_SPL_ABS_W16(freq_signal[i].real);
|
||||
tmp16no2 = WEBRTC_SPL_ABS_W16(freq_signal[i].imag);
|
||||
tmp32no1 = tmp16no1 * tmp16no1;
|
||||
tmp32no2 = tmp16no2 * tmp16no2;
|
||||
tmp32no2 = WebRtcSpl_AddSatW32(tmp32no1, tmp32no2);
|
||||
#endif // WEBRTC_ARCH_ARM_V7
|
||||
#endif // WEBRTC_ARCH_ARM_V7
|
||||
tmp32no1 = WebRtcSpl_SqrtFloor(tmp32no2);
|
||||
|
||||
freq_signal_abs[i] = (uint16_t)tmp32no1;
|
||||
#endif // AECM_WITH_ABS_APPROX
|
||||
#endif // AECM_WITH_ABS_APPROX
|
||||
}
|
||||
(*freq_signal_sum_abs) += (uint32_t)freq_signal_abs[i];
|
||||
}
|
||||
@ -277,7 +367,10 @@ static int TimeToFrequencyDomain(AecmCore* aecm,
|
||||
return time_signal_scaling;
|
||||
}
|
||||
|
||||
int WebRtcAecm_ProcessBlock(AecmCore* aecm,
|
||||
} // namespace
|
||||
|
||||
int RTC_NO_SANITIZE("signed-integer-overflow") // bugs.webrtc.org/8200
|
||||
WebRtcAecm_ProcessBlock(AecmCore* aecm,
|
||||
const int16_t* farend,
|
||||
const int16_t* nearendNoisy,
|
||||
const int16_t* nearendClean,
|
||||
@ -300,13 +393,13 @@ int WebRtcAecm_ProcessBlock(AecmCore* aecm,
|
||||
|
||||
// 32 byte aligned buffers (with +8 or +16).
|
||||
// TODO(kma): define fft with ComplexInt16.
|
||||
int16_t fft_buf[PART_LEN4 + 2 + 16]; // +2 to make a loop safe.
|
||||
int16_t fft_buf[PART_LEN4 + 2 + 16]; // +2 to make a loop safe.
|
||||
int32_t echoEst32_buf[PART_LEN1 + 8];
|
||||
int32_t dfw_buf[PART_LEN2 + 8];
|
||||
int32_t efw_buf[PART_LEN2 + 8];
|
||||
|
||||
int16_t* fft = (int16_t*) (((uintptr_t) fft_buf + 31) & ~ 31);
|
||||
int32_t* echoEst32 = (int32_t*) (((uintptr_t) echoEst32_buf + 31) & ~ 31);
|
||||
int16_t* fft = (int16_t*)(((uintptr_t)fft_buf + 31) & ~31);
|
||||
int32_t* echoEst32 = (int32_t*)(((uintptr_t)echoEst32_buf + 31) & ~31);
|
||||
ComplexInt16* dfw = (ComplexInt16*)(((uintptr_t)dfw_buf + 31) & ~31);
|
||||
ComplexInt16* efw = (ComplexInt16*)(((uintptr_t)efw_buf + 31) & ~31);
|
||||
|
||||
@ -332,53 +425,37 @@ int WebRtcAecm_ProcessBlock(AecmCore* aecm,
|
||||
// (1) another CONV_LEN blocks
|
||||
// (2) the rest
|
||||
|
||||
if (aecm->startupState < 2)
|
||||
{
|
||||
aecm->startupState = (aecm->totCount >= CONV_LEN) +
|
||||
(aecm->totCount >= CONV_LEN2);
|
||||
if (aecm->startupState < 2) {
|
||||
aecm->startupState =
|
||||
(aecm->totCount >= CONV_LEN) + (aecm->totCount >= CONV_LEN2);
|
||||
}
|
||||
// END: Determine startup state
|
||||
|
||||
// Buffer near and far end signals
|
||||
memcpy(aecm->xBuf + PART_LEN, farend, sizeof(int16_t) * PART_LEN);
|
||||
memcpy(aecm->dBufNoisy + PART_LEN, nearendNoisy, sizeof(int16_t) * PART_LEN);
|
||||
if (nearendClean != NULL)
|
||||
{
|
||||
memcpy(aecm->dBufClean + PART_LEN,
|
||||
nearendClean,
|
||||
if (nearendClean != NULL) {
|
||||
memcpy(aecm->dBufClean + PART_LEN, nearendClean,
|
||||
sizeof(int16_t) * PART_LEN);
|
||||
}
|
||||
|
||||
// Transform far end signal from time domain to frequency domain.
|
||||
far_q = TimeToFrequencyDomain(aecm,
|
||||
aecm->xBuf,
|
||||
dfw,
|
||||
xfa,
|
||||
&xfaSum);
|
||||
far_q = TimeToFrequencyDomain(aecm, aecm->xBuf, dfw, xfa, &xfaSum);
|
||||
|
||||
// Transform noisy near end signal from time domain to frequency domain.
|
||||
zerosDBufNoisy = TimeToFrequencyDomain(aecm,
|
||||
aecm->dBufNoisy,
|
||||
dfw,
|
||||
dfaNoisy,
|
||||
&dfaNoisySum);
|
||||
zerosDBufNoisy =
|
||||
TimeToFrequencyDomain(aecm, aecm->dBufNoisy, dfw, dfaNoisy, &dfaNoisySum);
|
||||
aecm->dfaNoisyQDomainOld = aecm->dfaNoisyQDomain;
|
||||
aecm->dfaNoisyQDomain = (int16_t)zerosDBufNoisy;
|
||||
|
||||
|
||||
if (nearendClean == NULL)
|
||||
{
|
||||
if (nearendClean == NULL) {
|
||||
ptrDfaClean = dfaNoisy;
|
||||
aecm->dfaCleanQDomainOld = aecm->dfaNoisyQDomainOld;
|
||||
aecm->dfaCleanQDomain = aecm->dfaNoisyQDomain;
|
||||
dfaCleanSum = dfaNoisySum;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
// Transform clean near end signal from time domain to frequency domain.
|
||||
zerosDBufClean = TimeToFrequencyDomain(aecm,
|
||||
aecm->dBufClean,
|
||||
dfw,
|
||||
dfaClean,
|
||||
zerosDBufClean = TimeToFrequencyDomain(aecm, aecm->dBufClean, dfw, dfaClean,
|
||||
&dfaCleanSum);
|
||||
aecm->dfaCleanQDomainOld = aecm->dfaCleanQDomain;
|
||||
aecm->dfaCleanQDomain = (int16_t)zerosDBufClean;
|
||||
@ -387,46 +464,34 @@ int WebRtcAecm_ProcessBlock(AecmCore* aecm,
|
||||
// Get the delay
|
||||
// Save far-end history and estimate delay
|
||||
WebRtcAecm_UpdateFarHistory(aecm, xfa, far_q);
|
||||
if (WebRtc_AddFarSpectrumFix(aecm->delay_estimator_farend,
|
||||
xfa,
|
||||
PART_LEN1,
|
||||
if (WebRtc_AddFarSpectrumFix(aecm->delay_estimator_farend, xfa, PART_LEN1,
|
||||
far_q) == -1) {
|
||||
return -1;
|
||||
}
|
||||
delay = WebRtc_DelayEstimatorProcessFix(aecm->delay_estimator,
|
||||
dfaNoisy,
|
||||
PART_LEN1,
|
||||
zerosDBufNoisy);
|
||||
if (delay == -1)
|
||||
{
|
||||
delay = WebRtc_DelayEstimatorProcessFix(aecm->delay_estimator, dfaNoisy,
|
||||
PART_LEN1, zerosDBufNoisy);
|
||||
if (delay == -1) {
|
||||
return -1;
|
||||
}
|
||||
else if (delay == -2)
|
||||
{
|
||||
} else if (delay == -2) {
|
||||
// If the delay is unknown, we assume zero.
|
||||
// NOTE: this will have to be adjusted if we ever add lookahead.
|
||||
delay = 0;
|
||||
}
|
||||
|
||||
if (aecm->fixedDelay >= 0)
|
||||
{
|
||||
if (aecm->fixedDelay >= 0) {
|
||||
// Use fixed delay
|
||||
delay = aecm->fixedDelay;
|
||||
}
|
||||
|
||||
// Get aligned far end spectrum
|
||||
far_spectrum_ptr = WebRtcAecm_AlignedFarend(aecm, &far_q, delay);
|
||||
zerosXBuf = (int16_t) far_q;
|
||||
if (far_spectrum_ptr == NULL)
|
||||
{
|
||||
zerosXBuf = (int16_t)far_q;
|
||||
if (far_spectrum_ptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Calculate log(energy) and update energy threshold levels
|
||||
WebRtcAecm_CalcEnergies(aecm,
|
||||
far_spectrum_ptr,
|
||||
zerosXBuf,
|
||||
dfaNoisySum,
|
||||
WebRtcAecm_CalcEnergies(aecm, far_spectrum_ptr, zerosXBuf, dfaNoisySum,
|
||||
echoEst32);
|
||||
|
||||
// Calculate stepsize
|
||||
@ -438,63 +503,54 @@ int WebRtcAecm_ProcessBlock(AecmCore* aecm,
|
||||
// This is the channel estimation algorithm.
|
||||
// It is base on NLMS but has a variable step length,
|
||||
// which was calculated above.
|
||||
WebRtcAecm_UpdateChannel(aecm,
|
||||
far_spectrum_ptr,
|
||||
zerosXBuf,
|
||||
dfaNoisy,
|
||||
mu,
|
||||
WebRtcAecm_UpdateChannel(aecm, far_spectrum_ptr, zerosXBuf, dfaNoisy, mu,
|
||||
echoEst32);
|
||||
supGain = WebRtcAecm_CalcSuppressionGain(aecm);
|
||||
|
||||
|
||||
// Calculate Wiener filter hnl[]
|
||||
for (i = 0; i < PART_LEN1; i++)
|
||||
{
|
||||
for (i = 0; i < PART_LEN1; i++) {
|
||||
// Far end signal through channel estimate in Q8
|
||||
// How much can we shift right to preserve resolution
|
||||
tmp32no1 = echoEst32[i] - aecm->echoFilt[i];
|
||||
aecm->echoFilt[i] += (tmp32no1 * 50) >> 8;
|
||||
aecm->echoFilt[i] +=
|
||||
rtc::dchecked_cast<int32_t>((int64_t{tmp32no1} * 50) >> 8);
|
||||
|
||||
zeros32 = WebRtcSpl_NormW32(aecm->echoFilt[i]) + 1;
|
||||
zeros16 = WebRtcSpl_NormW16(supGain) + 1;
|
||||
if (zeros32 + zeros16 > 16)
|
||||
{
|
||||
if (zeros32 + zeros16 > 16) {
|
||||
// Multiplication is safe
|
||||
// Result in
|
||||
// Q(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN+
|
||||
// aecm->xfaQDomainBuf[diff])
|
||||
echoEst32Gained = WEBRTC_SPL_UMUL_32_16((uint32_t)aecm->echoFilt[i],
|
||||
(uint16_t)supGain);
|
||||
echoEst32Gained =
|
||||
WEBRTC_SPL_UMUL_32_16((uint32_t)aecm->echoFilt[i], (uint16_t)supGain);
|
||||
resolutionDiff = 14 - RESOLUTION_CHANNEL16 - RESOLUTION_SUPGAIN;
|
||||
resolutionDiff += (aecm->dfaCleanQDomain - zerosXBuf);
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
tmp16no1 = 17 - zeros32 - zeros16;
|
||||
resolutionDiff = 14 + tmp16no1 - RESOLUTION_CHANNEL16 -
|
||||
RESOLUTION_SUPGAIN;
|
||||
resolutionDiff =
|
||||
14 + tmp16no1 - RESOLUTION_CHANNEL16 - RESOLUTION_SUPGAIN;
|
||||
resolutionDiff += (aecm->dfaCleanQDomain - zerosXBuf);
|
||||
if (zeros32 > tmp16no1)
|
||||
{
|
||||
if (zeros32 > tmp16no1) {
|
||||
echoEst32Gained = WEBRTC_SPL_UMUL_32_16((uint32_t)aecm->echoFilt[i],
|
||||
supGain >> tmp16no1);
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
// Result in Q-(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN-16)
|
||||
echoEst32Gained = (aecm->echoFilt[i] >> tmp16no1) * supGain;
|
||||
}
|
||||
}
|
||||
|
||||
zeros16 = WebRtcSpl_NormW16(aecm->nearFilt[i]);
|
||||
assert(zeros16 >= 0); // |zeros16| is a norm, hence non-negative.
|
||||
RTC_DCHECK_GE(zeros16, 0); // |zeros16| is a norm, hence non-negative.
|
||||
dfa_clean_q_domain_diff = aecm->dfaCleanQDomain - aecm->dfaCleanQDomainOld;
|
||||
if (zeros16 < dfa_clean_q_domain_diff && aecm->nearFilt[i]) {
|
||||
tmp16no1 = aecm->nearFilt[i] << zeros16;
|
||||
tmp16no1 = aecm->nearFilt[i] * (1 << zeros16);
|
||||
qDomainDiff = zeros16 - dfa_clean_q_domain_diff;
|
||||
tmp16no2 = ptrDfaClean[i] >> -qDomainDiff;
|
||||
} else {
|
||||
tmp16no1 = dfa_clean_q_domain_diff < 0
|
||||
? aecm->nearFilt[i] >> -dfa_clean_q_domain_diff
|
||||
: aecm->nearFilt[i] << dfa_clean_q_domain_diff;
|
||||
? aecm->nearFilt[i] >> -dfa_clean_q_domain_diff
|
||||
: aecm->nearFilt[i] * (1 << dfa_clean_q_domain_diff);
|
||||
qDomainDiff = 0;
|
||||
tmp16no2 = ptrDfaClean[i];
|
||||
}
|
||||
@ -505,130 +561,105 @@ int WebRtcAecm_ProcessBlock(AecmCore* aecm,
|
||||
if ((tmp16no2) & (-qDomainDiff > zeros16)) {
|
||||
aecm->nearFilt[i] = WEBRTC_SPL_WORD16_MAX;
|
||||
} else {
|
||||
aecm->nearFilt[i] = qDomainDiff < 0 ? tmp16no2 << -qDomainDiff
|
||||
aecm->nearFilt[i] = qDomainDiff < 0 ? tmp16no2 * (1 << -qDomainDiff)
|
||||
: tmp16no2 >> qDomainDiff;
|
||||
}
|
||||
|
||||
// Wiener filter coefficients, resulting hnl in Q14
|
||||
if (echoEst32Gained == 0)
|
||||
{
|
||||
if (echoEst32Gained == 0) {
|
||||
hnl[i] = ONE_Q14;
|
||||
} else if (aecm->nearFilt[i] == 0)
|
||||
{
|
||||
} else if (aecm->nearFilt[i] == 0) {
|
||||
hnl[i] = 0;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
// Multiply the suppression gain
|
||||
// Rounding
|
||||
echoEst32Gained += (uint32_t)(aecm->nearFilt[i] >> 1);
|
||||
tmpU32 = WebRtcSpl_DivU32U16(echoEst32Gained,
|
||||
(uint16_t)aecm->nearFilt[i]);
|
||||
tmpU32 =
|
||||
WebRtcSpl_DivU32U16(echoEst32Gained, (uint16_t)aecm->nearFilt[i]);
|
||||
|
||||
// Current resolution is
|
||||
// Q-(RESOLUTION_CHANNEL+RESOLUTION_SUPGAIN- max(0,17-zeros16- zeros32))
|
||||
// Make sure we are in Q14
|
||||
tmp32no1 = (int32_t)WEBRTC_SPL_SHIFT_W32(tmpU32, resolutionDiff);
|
||||
if (tmp32no1 > ONE_Q14)
|
||||
{
|
||||
if (tmp32no1 > ONE_Q14) {
|
||||
hnl[i] = 0;
|
||||
} else if (tmp32no1 < 0)
|
||||
{
|
||||
} else if (tmp32no1 < 0) {
|
||||
hnl[i] = ONE_Q14;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
// 1-echoEst/dfa
|
||||
hnl[i] = ONE_Q14 - (int16_t)tmp32no1;
|
||||
if (hnl[i] < 0)
|
||||
{
|
||||
if (hnl[i] < 0) {
|
||||
hnl[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hnl[i])
|
||||
{
|
||||
if (hnl[i]) {
|
||||
numPosCoef++;
|
||||
}
|
||||
}
|
||||
// Only in wideband. Prevent the gain in upper band from being larger than
|
||||
// in lower band.
|
||||
if (aecm->mult == 2)
|
||||
{
|
||||
if (aecm->mult == 2) {
|
||||
// TODO(bjornv): Investigate if the scaling of hnl[i] below can cause
|
||||
// speech distortion in double-talk.
|
||||
for (i = 0; i < PART_LEN1; i++)
|
||||
{
|
||||
for (i = 0; i < PART_LEN1; i++) {
|
||||
hnl[i] = (int16_t)((hnl[i] * hnl[i]) >> 14);
|
||||
}
|
||||
|
||||
for (i = kMinPrefBand; i <= kMaxPrefBand; i++)
|
||||
{
|
||||
for (i = kMinPrefBand; i <= kMaxPrefBand; i++) {
|
||||
avgHnl32 += (int32_t)hnl[i];
|
||||
}
|
||||
assert(kMaxPrefBand - kMinPrefBand + 1 > 0);
|
||||
RTC_DCHECK_GT(kMaxPrefBand - kMinPrefBand + 1, 0);
|
||||
avgHnl32 /= (kMaxPrefBand - kMinPrefBand + 1);
|
||||
|
||||
for (i = kMaxPrefBand; i < PART_LEN1; i++)
|
||||
{
|
||||
if (hnl[i] > (int16_t)avgHnl32)
|
||||
{
|
||||
for (i = kMaxPrefBand; i < PART_LEN1; i++) {
|
||||
if (hnl[i] > (int16_t)avgHnl32) {
|
||||
hnl[i] = (int16_t)avgHnl32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate NLP gain, result is in Q14
|
||||
if (aecm->nlpFlag)
|
||||
{
|
||||
for (i = 0; i < PART_LEN1; i++)
|
||||
{
|
||||
if (aecm->nlpFlag) {
|
||||
for (i = 0; i < PART_LEN1; i++) {
|
||||
// Truncate values close to zero and one.
|
||||
if (hnl[i] > NLP_COMP_HIGH)
|
||||
{
|
||||
if (hnl[i] > NLP_COMP_HIGH) {
|
||||
hnl[i] = ONE_Q14;
|
||||
} else if (hnl[i] < NLP_COMP_LOW)
|
||||
{
|
||||
} else if (hnl[i] < NLP_COMP_LOW) {
|
||||
hnl[i] = 0;
|
||||
}
|
||||
|
||||
// Remove outliers
|
||||
if (numPosCoef < 3)
|
||||
{
|
||||
if (numPosCoef < 3) {
|
||||
nlpGain = 0;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
nlpGain = ONE_Q14;
|
||||
}
|
||||
|
||||
// NLP
|
||||
if ((hnl[i] == ONE_Q14) && (nlpGain == ONE_Q14))
|
||||
{
|
||||
if ((hnl[i] == ONE_Q14) && (nlpGain == ONE_Q14)) {
|
||||
hnl[i] = ONE_Q14;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
hnl[i] = (int16_t)((hnl[i] * nlpGain) >> 14);
|
||||
}
|
||||
|
||||
// multiply with Wiener coefficients
|
||||
efw[i].real = (int16_t)(WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].real,
|
||||
hnl[i], 14));
|
||||
efw[i].imag = (int16_t)(WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].imag,
|
||||
hnl[i], 14));
|
||||
efw[i].real = (int16_t)(
|
||||
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].real, hnl[i], 14));
|
||||
efw[i].imag = (int16_t)(
|
||||
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].imag, hnl[i], 14));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// multiply with Wiener coefficients
|
||||
for (i = 0; i < PART_LEN1; i++)
|
||||
{
|
||||
efw[i].real = (int16_t)(WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].real,
|
||||
hnl[i], 14));
|
||||
efw[i].imag = (int16_t)(WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].imag,
|
||||
hnl[i], 14));
|
||||
for (i = 0; i < PART_LEN1; i++) {
|
||||
efw[i].real = (int16_t)(
|
||||
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].real, hnl[i], 14));
|
||||
efw[i].imag = (int16_t)(
|
||||
WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(dfw[i].imag, hnl[i], 14));
|
||||
}
|
||||
}
|
||||
|
||||
if (aecm->cngMode == AecmTrue)
|
||||
{
|
||||
if (aecm->cngMode == AecmTrue) {
|
||||
ComfortNoise(aecm, ptrDfaClean, efw, hnl);
|
||||
}
|
||||
|
||||
@ -637,135 +668,4 @@ int WebRtcAecm_ProcessBlock(AecmCore* aecm,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ComfortNoise(AecmCore* aecm,
|
||||
const uint16_t* dfa,
|
||||
ComplexInt16* out,
|
||||
const int16_t* lambda) {
|
||||
int16_t i;
|
||||
int16_t tmp16;
|
||||
int32_t tmp32;
|
||||
|
||||
int16_t randW16[PART_LEN];
|
||||
int16_t uReal[PART_LEN1];
|
||||
int16_t uImag[PART_LEN1];
|
||||
int32_t outLShift32;
|
||||
int16_t noiseRShift16[PART_LEN1];
|
||||
|
||||
int16_t shiftFromNearToNoise = kNoiseEstQDomain - aecm->dfaCleanQDomain;
|
||||
int16_t minTrackShift;
|
||||
|
||||
assert(shiftFromNearToNoise >= 0);
|
||||
assert(shiftFromNearToNoise < 16);
|
||||
|
||||
if (aecm->noiseEstCtr < 100)
|
||||
{
|
||||
// Track the minimum more quickly initially.
|
||||
aecm->noiseEstCtr++;
|
||||
minTrackShift = 6;
|
||||
} else
|
||||
{
|
||||
minTrackShift = 9;
|
||||
}
|
||||
|
||||
// Estimate noise power.
|
||||
for (i = 0; i < PART_LEN1; i++)
|
||||
{
|
||||
// Shift to the noise domain.
|
||||
tmp32 = (int32_t)dfa[i];
|
||||
outLShift32 = tmp32 << shiftFromNearToNoise;
|
||||
|
||||
if (outLShift32 < aecm->noiseEst[i])
|
||||
{
|
||||
// Reset "too low" counter
|
||||
aecm->noiseEstTooLowCtr[i] = 0;
|
||||
// Track the minimum.
|
||||
if (aecm->noiseEst[i] < (1 << minTrackShift))
|
||||
{
|
||||
// For small values, decrease noiseEst[i] every
|
||||
// |kNoiseEstIncCount| block. The regular approach below can not
|
||||
// go further down due to truncation.
|
||||
aecm->noiseEstTooHighCtr[i]++;
|
||||
if (aecm->noiseEstTooHighCtr[i] >= kNoiseEstIncCount)
|
||||
{
|
||||
aecm->noiseEst[i]--;
|
||||
aecm->noiseEstTooHighCtr[i] = 0; // Reset the counter
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aecm->noiseEst[i] -= ((aecm->noiseEst[i] - outLShift32)
|
||||
>> minTrackShift);
|
||||
}
|
||||
} else
|
||||
{
|
||||
// Reset "too high" counter
|
||||
aecm->noiseEstTooHighCtr[i] = 0;
|
||||
// Ramp slowly upwards until we hit the minimum again.
|
||||
if ((aecm->noiseEst[i] >> 19) > 0)
|
||||
{
|
||||
// Avoid overflow.
|
||||
// Multiplication with 2049 will cause wrap around. Scale
|
||||
// down first and then multiply
|
||||
aecm->noiseEst[i] >>= 11;
|
||||
aecm->noiseEst[i] *= 2049;
|
||||
}
|
||||
else if ((aecm->noiseEst[i] >> 11) > 0)
|
||||
{
|
||||
// Large enough for relative increase
|
||||
aecm->noiseEst[i] *= 2049;
|
||||
aecm->noiseEst[i] >>= 11;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Make incremental increases based on size every
|
||||
// |kNoiseEstIncCount| block
|
||||
aecm->noiseEstTooLowCtr[i]++;
|
||||
if (aecm->noiseEstTooLowCtr[i] >= kNoiseEstIncCount)
|
||||
{
|
||||
aecm->noiseEst[i] += (aecm->noiseEst[i] >> 9) + 1;
|
||||
aecm->noiseEstTooLowCtr[i] = 0; // Reset counter
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < PART_LEN1; i++)
|
||||
{
|
||||
tmp32 = aecm->noiseEst[i] >> shiftFromNearToNoise;
|
||||
if (tmp32 > 32767)
|
||||
{
|
||||
tmp32 = 32767;
|
||||
aecm->noiseEst[i] = tmp32 << shiftFromNearToNoise;
|
||||
}
|
||||
noiseRShift16[i] = (int16_t)tmp32;
|
||||
|
||||
tmp16 = ONE_Q14 - lambda[i];
|
||||
noiseRShift16[i] = (int16_t)((tmp16 * noiseRShift16[i]) >> 14);
|
||||
}
|
||||
|
||||
// Generate a uniform random array on [0 2^15-1].
|
||||
WebRtcSpl_RandUArray(randW16, PART_LEN, &aecm->seed);
|
||||
|
||||
// Generate noise according to estimated energy.
|
||||
uReal[0] = 0; // Reject LF noise.
|
||||
uImag[0] = 0;
|
||||
for (i = 1; i < PART_LEN1; i++)
|
||||
{
|
||||
// Get a random index for the cos and sin tables over [0 359].
|
||||
tmp16 = (int16_t)((359 * randW16[i - 1]) >> 15);
|
||||
|
||||
// Tables are in Q13.
|
||||
uReal[i] = (int16_t)((noiseRShift16[i] * WebRtcAecm_kCosTable[tmp16]) >>
|
||||
13);
|
||||
uImag[i] = (int16_t)((-noiseRShift16[i] * WebRtcAecm_kSinTable[tmp16]) >>
|
||||
13);
|
||||
}
|
||||
uImag[PART_LEN] = 0;
|
||||
|
||||
for (i = 0; i < PART_LEN1; i++)
|
||||
{
|
||||
out[i].real = WebRtcSpl_AddSatW16(out[i].real, uReal[i]);
|
||||
out[i].imag = WebRtcSpl_AddSatW16(out[i].imag, uImag[i]);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
File diff suppressed because it is too large
Load Diff
1656
webrtc/modules/audio_processing/aecm/aecm_core_mips.cc
Normal file
1656
webrtc/modules/audio_processing/aecm/aecm_core_mips.cc
Normal file
File diff suppressed because it is too large
Load Diff
@ -8,29 +8,19 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "webrtc/modules/audio_processing/aecm/aecm_core.h"
|
||||
|
||||
#include <arm_neon.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "webrtc/common_audio/signal_processing/include/real_fft.h"
|
||||
#include "common_audio/signal_processing/include/real_fft.h"
|
||||
#include "modules/audio_processing/aecm/aecm_core.h"
|
||||
#include "rtc_base/checks.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
namespace {
|
||||
|
||||
// TODO(kma): Re-write the corresponding assembly file, the offset
|
||||
// generating script and makefile, to replace these C functions.
|
||||
|
||||
// Square root of Hanning window in Q14.
|
||||
const ALIGN8_BEG int16_t WebRtcAecm_kSqrtHanning[] ALIGN8_END = {
|
||||
0,
|
||||
399, 798, 1196, 1594, 1990, 2386, 2780, 3172,
|
||||
3562, 3951, 4337, 4720, 5101, 5478, 5853, 6224,
|
||||
6591, 6954, 7313, 7668, 8019, 8364, 8705, 9040,
|
||||
9370, 9695, 10013, 10326, 10633, 10933, 11227, 11514,
|
||||
11795, 12068, 12335, 12594, 12845, 13089, 13325, 13553,
|
||||
13773, 13985, 14189, 14384, 14571, 14749, 14918, 15079,
|
||||
15231, 15373, 15506, 15631, 15746, 15851, 15947, 16034,
|
||||
16111, 16179, 16237, 16286, 16325, 16354, 16373, 16384
|
||||
};
|
||||
|
||||
static inline void AddLanes(uint32_t* ptr, uint32x4_t v) {
|
||||
#if defined(WEBRTC_ARCH_ARM64)
|
||||
*(ptr) = vaddvq_u32(v);
|
||||
@ -42,6 +32,8 @@ static inline void AddLanes(uint32_t* ptr, uint32x4_t v) {
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void WebRtcAecm_CalcLinearEnergiesNeon(AecmCore* aecm,
|
||||
const uint16_t* far_spectrum,
|
||||
int32_t* echo_est,
|
||||
@ -90,12 +82,12 @@ void WebRtcAecm_CalcLinearEnergiesNeon(AecmCore* aecm,
|
||||
echo_stored_v = vaddq_u32(echo_est_v_low, echo_stored_v);
|
||||
echo_stored_v = vaddq_u32(echo_est_v_high, echo_stored_v);
|
||||
|
||||
echo_adapt_v = vmlal_u16(echo_adapt_v,
|
||||
vreinterpret_u16_s16(vget_low_s16(adapt_v)),
|
||||
vget_low_u16(spectrum_v));
|
||||
echo_adapt_v = vmlal_u16(echo_adapt_v,
|
||||
vreinterpret_u16_s16(vget_high_s16(adapt_v)),
|
||||
vget_high_u16(spectrum_v));
|
||||
echo_adapt_v =
|
||||
vmlal_u16(echo_adapt_v, vreinterpret_u16_s16(vget_low_s16(adapt_v)),
|
||||
vget_low_u16(spectrum_v));
|
||||
echo_adapt_v =
|
||||
vmlal_u16(echo_adapt_v, vreinterpret_u16_s16(vget_high_s16(adapt_v)),
|
||||
vget_high_u16(spectrum_v));
|
||||
|
||||
start_stored_p += 8;
|
||||
start_adapt_p += 8;
|
||||
@ -117,9 +109,9 @@ void WebRtcAecm_CalcLinearEnergiesNeon(AecmCore* aecm,
|
||||
void WebRtcAecm_StoreAdaptiveChannelNeon(AecmCore* aecm,
|
||||
const uint16_t* far_spectrum,
|
||||
int32_t* echo_est) {
|
||||
assert((uintptr_t)echo_est % 32 == 0);
|
||||
assert((uintptr_t)(aecm->channelStored) % 16 == 0);
|
||||
assert((uintptr_t)(aecm->channelAdapt16) % 16 == 0);
|
||||
RTC_DCHECK_EQ(0, (uintptr_t)echo_est % 32);
|
||||
RTC_DCHECK_EQ(0, (uintptr_t)aecm->channelStored % 16);
|
||||
RTC_DCHECK_EQ(0, (uintptr_t)aecm->channelAdapt16 % 16);
|
||||
|
||||
// This is C code of following optimized code.
|
||||
// During startup we store the channel every block.
|
||||
@ -174,9 +166,9 @@ void WebRtcAecm_StoreAdaptiveChannelNeon(AecmCore* aecm,
|
||||
}
|
||||
|
||||
void WebRtcAecm_ResetAdaptiveChannelNeon(AecmCore* aecm) {
|
||||
assert((uintptr_t)(aecm->channelStored) % 16 == 0);
|
||||
assert((uintptr_t)(aecm->channelAdapt16) % 16 == 0);
|
||||
assert((uintptr_t)(aecm->channelAdapt32) % 32 == 0);
|
||||
RTC_DCHECK_EQ(0, (uintptr_t)aecm->channelStored % 16);
|
||||
RTC_DCHECK_EQ(0, (uintptr_t)aecm->channelAdapt16 % 16);
|
||||
RTC_DCHECK_EQ(0, (uintptr_t)aecm->channelAdapt32 % 32);
|
||||
|
||||
// The C code of following optimized code.
|
||||
// for (i = 0; i < PART_LEN1; i++) {
|
||||
@ -210,3 +202,5 @@ void WebRtcAecm_ResetAdaptiveChannelNeon(AecmCore* aecm) {
|
||||
aecm->channelAdapt16[PART_LEN] = aecm->channelStored[PART_LEN];
|
||||
aecm->channelAdapt32[PART_LEN] = (int32_t)aecm->channelStored[PART_LEN] << 16;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
@ -8,80 +8,80 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AECM_AECM_DEFINES_H_
|
||||
#define WEBRTC_MODULES_AUDIO_PROCESSING_AECM_AECM_DEFINES_H_
|
||||
#ifndef MODULES_AUDIO_PROCESSING_AECM_AECM_DEFINES_H_
|
||||
#define MODULES_AUDIO_PROCESSING_AECM_AECM_DEFINES_H_
|
||||
|
||||
#define AECM_DYNAMIC_Q /* Turn on/off dynamic Q-domain. */
|
||||
#define AECM_DYNAMIC_Q /* Turn on/off dynamic Q-domain. */
|
||||
|
||||
/* Algorithm parameters */
|
||||
#define FRAME_LEN 80 /* Total frame length, 10 ms. */
|
||||
#define FRAME_LEN 80 /* Total frame length, 10 ms. */
|
||||
|
||||
#define PART_LEN 64 /* Length of partition. */
|
||||
#define PART_LEN_SHIFT 7 /* Length of (PART_LEN * 2) in base 2. */
|
||||
#define PART_LEN 64 /* Length of partition. */
|
||||
#define PART_LEN_SHIFT 7 /* Length of (PART_LEN * 2) in base 2. */
|
||||
|
||||
#define PART_LEN1 (PART_LEN + 1) /* Unique fft coefficients. */
|
||||
#define PART_LEN2 (PART_LEN << 1) /* Length of partition * 2. */
|
||||
#define PART_LEN4 (PART_LEN << 2) /* Length of partition * 4. */
|
||||
#define FAR_BUF_LEN PART_LEN4 /* Length of buffers. */
|
||||
#define MAX_DELAY 100
|
||||
#define PART_LEN1 (PART_LEN + 1) /* Unique fft coefficients. */
|
||||
#define PART_LEN2 (PART_LEN << 1) /* Length of partition * 2. */
|
||||
#define PART_LEN4 (PART_LEN << 2) /* Length of partition * 4. */
|
||||
#define FAR_BUF_LEN PART_LEN4 /* Length of buffers. */
|
||||
#define MAX_DELAY 100
|
||||
|
||||
/* Counter parameters */
|
||||
#define CONV_LEN 512 /* Convergence length used at startup. */
|
||||
#define CONV_LEN2 (CONV_LEN << 1) /* Used at startup. */
|
||||
#define CONV_LEN 512 /* Convergence length used at startup. */
|
||||
#define CONV_LEN2 (CONV_LEN << 1) /* Used at startup. */
|
||||
|
||||
/* Energy parameters */
|
||||
#define MAX_BUF_LEN 64 /* History length of energy signals. */
|
||||
#define FAR_ENERGY_MIN 1025 /* Lowest Far energy level: At least 2 */
|
||||
/* in energy. */
|
||||
#define FAR_ENERGY_DIFF 929 /* Allowed difference between max */
|
||||
/* and min. */
|
||||
#define ENERGY_DEV_OFFSET 0 /* The energy error offset in Q8. */
|
||||
#define ENERGY_DEV_TOL 400 /* The energy estimation tolerance (Q8). */
|
||||
#define FAR_ENERGY_VAD_REGION 230 /* Far VAD tolerance region. */
|
||||
#define MAX_BUF_LEN 64 /* History length of energy signals. */
|
||||
#define FAR_ENERGY_MIN 1025 /* Lowest Far energy level: At least 2 */
|
||||
/* in energy. */
|
||||
#define FAR_ENERGY_DIFF 929 /* Allowed difference between max */
|
||||
/* and min. */
|
||||
#define ENERGY_DEV_OFFSET 0 /* The energy error offset in Q8. */
|
||||
#define ENERGY_DEV_TOL 400 /* The energy estimation tolerance (Q8). */
|
||||
#define FAR_ENERGY_VAD_REGION 230 /* Far VAD tolerance region. */
|
||||
|
||||
/* Stepsize parameters */
|
||||
#define MU_MIN 10 /* Min stepsize 2^-MU_MIN (far end energy */
|
||||
/* dependent). */
|
||||
#define MU_MAX 1 /* Max stepsize 2^-MU_MAX (far end energy */
|
||||
/* dependent). */
|
||||
#define MU_DIFF 9 /* MU_MIN - MU_MAX */
|
||||
#define MU_MIN 10 /* Min stepsize 2^-MU_MIN (far end energy */
|
||||
/* dependent). */
|
||||
#define MU_MAX 1 /* Max stepsize 2^-MU_MAX (far end energy */
|
||||
/* dependent). */
|
||||
#define MU_DIFF 9 /* MU_MIN - MU_MAX */
|
||||
|
||||
/* Channel parameters */
|
||||
#define MIN_MSE_COUNT 20 /* Min number of consecutive blocks with enough */
|
||||
/* far end energy to compare channel estimates. */
|
||||
#define MIN_MSE_DIFF 29 /* The ratio between adapted and stored channel to */
|
||||
/* accept a new storage (0.8 in Q-MSE_RESOLUTION). */
|
||||
#define MSE_RESOLUTION 5 /* MSE parameter resolution. */
|
||||
#define RESOLUTION_CHANNEL16 12 /* W16 Channel in Q-RESOLUTION_CHANNEL16. */
|
||||
#define RESOLUTION_CHANNEL32 28 /* W32 Channel in Q-RESOLUTION_CHANNEL. */
|
||||
#define CHANNEL_VAD 16 /* Minimum energy in frequency band */
|
||||
/* to update channel. */
|
||||
#define MIN_MSE_COUNT 20 /* Min number of consecutive blocks with enough */
|
||||
/* far end energy to compare channel estimates. */
|
||||
#define MIN_MSE_DIFF 29 /* The ratio between adapted and stored channel to */
|
||||
/* accept a new storage (0.8 in Q-MSE_RESOLUTION). */
|
||||
#define MSE_RESOLUTION 5 /* MSE parameter resolution. */
|
||||
#define RESOLUTION_CHANNEL16 12 /* W16 Channel in Q-RESOLUTION_CHANNEL16. */
|
||||
#define RESOLUTION_CHANNEL32 28 /* W32 Channel in Q-RESOLUTION_CHANNEL. */
|
||||
#define CHANNEL_VAD 16 /* Minimum energy in frequency band */
|
||||
/* to update channel. */
|
||||
|
||||
/* Suppression gain parameters: SUPGAIN parameters in Q-(RESOLUTION_SUPGAIN). */
|
||||
#define RESOLUTION_SUPGAIN 8 /* Channel in Q-(RESOLUTION_SUPGAIN). */
|
||||
#define SUPGAIN_DEFAULT (1 << RESOLUTION_SUPGAIN) /* Default. */
|
||||
#define SUPGAIN_ERROR_PARAM_A 3072 /* Estimation error parameter */
|
||||
/* (Maximum gain) (8 in Q8). */
|
||||
#define SUPGAIN_ERROR_PARAM_B 1536 /* Estimation error parameter */
|
||||
/* (Gain before going down). */
|
||||
#define SUPGAIN_ERROR_PARAM_D SUPGAIN_DEFAULT /* Estimation error parameter */
|
||||
/* (Should be the same as Default) (1 in Q8). */
|
||||
#define SUPGAIN_EPC_DT 200 /* SUPGAIN_ERROR_PARAM_C * ENERGY_DEV_TOL */
|
||||
#define RESOLUTION_SUPGAIN 8 /* Channel in Q-(RESOLUTION_SUPGAIN). */
|
||||
#define SUPGAIN_DEFAULT (1 << RESOLUTION_SUPGAIN) /* Default. */
|
||||
#define SUPGAIN_ERROR_PARAM_A 3072 /* Estimation error parameter */
|
||||
/* (Maximum gain) (8 in Q8). */
|
||||
#define SUPGAIN_ERROR_PARAM_B 1536 /* Estimation error parameter */
|
||||
/* (Gain before going down). */
|
||||
#define SUPGAIN_ERROR_PARAM_D SUPGAIN_DEFAULT /* Estimation error parameter */
|
||||
/* (Should be the same as Default) (1 in Q8). */
|
||||
#define SUPGAIN_EPC_DT 200 /* SUPGAIN_ERROR_PARAM_C * ENERGY_DEV_TOL */
|
||||
|
||||
/* Defines for "check delay estimation" */
|
||||
#define CORR_WIDTH 31 /* Number of samples to correlate over. */
|
||||
#define CORR_MAX 16 /* Maximum correlation offset. */
|
||||
#define CORR_MAX_BUF 63
|
||||
#define CORR_DEV 4
|
||||
#define CORR_MAX_LEVEL 20
|
||||
#define CORR_MAX_LOW 4
|
||||
#define CORR_BUF_LEN (CORR_MAX << 1) + 1
|
||||
#define CORR_WIDTH 31 /* Number of samples to correlate over. */
|
||||
#define CORR_MAX 16 /* Maximum correlation offset. */
|
||||
#define CORR_MAX_BUF 63
|
||||
#define CORR_DEV 4
|
||||
#define CORR_MAX_LEVEL 20
|
||||
#define CORR_MAX_LOW 4
|
||||
#define CORR_BUF_LEN (CORR_MAX << 1) + 1
|
||||
/* Note that CORR_WIDTH + 2*CORR_MAX <= MAX_BUF_LEN. */
|
||||
|
||||
#define ONE_Q14 (1 << 14)
|
||||
#define ONE_Q14 (1 << 14)
|
||||
|
||||
/* NLP defines */
|
||||
#define NLP_COMP_LOW 3277 /* 0.2 in Q14 */
|
||||
#define NLP_COMP_HIGH ONE_Q14 /* 1 in Q14 */
|
||||
#define NLP_COMP_LOW 3277 /* 0.2 in Q14 */
|
||||
#define NLP_COMP_HIGH ONE_Q14 /* 1 in Q14 */
|
||||
|
||||
#endif
|
||||
|
@ -1,702 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "webrtc/modules/audio_processing/aecm/include/echo_control_mobile.h"
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "webrtc/common_audio/ring_buffer.h"
|
||||
#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
|
||||
#include "webrtc/modules/audio_processing/aecm/aecm_core.h"
|
||||
|
||||
#define BUF_SIZE_FRAMES 50 // buffer size (frames)
|
||||
// Maximum length of resampled signal. Must be an integer multiple of frames
|
||||
// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
|
||||
// The factor of 2 handles wb, and the + 1 is as a safety margin
|
||||
#define MAX_RESAMP_LEN (5 * FRAME_LEN)
|
||||
|
||||
static const size_t kBufSizeSamp = BUF_SIZE_FRAMES * FRAME_LEN; // buffer size (samples)
|
||||
static const int kSampMsNb = 8; // samples per ms in nb
|
||||
// Target suppression levels for nlp modes
|
||||
// log{0.001, 0.00001, 0.00000001}
|
||||
static const int kInitCheck = 42;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int sampFreq;
|
||||
int scSampFreq;
|
||||
short bufSizeStart;
|
||||
int knownDelay;
|
||||
|
||||
// Stores the last frame added to the farend buffer
|
||||
short farendOld[2][FRAME_LEN];
|
||||
short initFlag; // indicates if AEC has been initialized
|
||||
|
||||
// Variables used for averaging far end buffer size
|
||||
short counter;
|
||||
short sum;
|
||||
short firstVal;
|
||||
short checkBufSizeCtr;
|
||||
|
||||
// Variables used for delay shifts
|
||||
short msInSndCardBuf;
|
||||
short filtDelay;
|
||||
int timeForDelayChange;
|
||||
int ECstartup;
|
||||
int checkBuffSize;
|
||||
int delayChange;
|
||||
short lastDelayDiff;
|
||||
|
||||
int16_t echoMode;
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
FILE *bufFile;
|
||||
FILE *delayFile;
|
||||
FILE *preCompFile;
|
||||
FILE *postCompFile;
|
||||
#endif // AEC_DEBUG
|
||||
// Structures
|
||||
RingBuffer *farendBuf;
|
||||
|
||||
int lastError;
|
||||
|
||||
AecmCore* aecmCore;
|
||||
} AecMobile;
|
||||
|
||||
// Estimates delay to set the position of the farend buffer read pointer
|
||||
// (controlled by knownDelay)
|
||||
static int WebRtcAecm_EstBufDelay(AecMobile* aecmInst, short msInSndCardBuf);
|
||||
|
||||
// Stuffs the farend buffer if the estimated delay is too large
|
||||
static int WebRtcAecm_DelayComp(AecMobile* aecmInst);
|
||||
|
||||
void* WebRtcAecm_Create() {
|
||||
AecMobile* aecm = malloc(sizeof(AecMobile));
|
||||
|
||||
WebRtcSpl_Init();
|
||||
|
||||
aecm->aecmCore = WebRtcAecm_CreateCore();
|
||||
if (!aecm->aecmCore) {
|
||||
WebRtcAecm_Free(aecm);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
aecm->farendBuf = WebRtc_CreateBuffer(kBufSizeSamp,
|
||||
sizeof(int16_t));
|
||||
if (!aecm->farendBuf)
|
||||
{
|
||||
WebRtcAecm_Free(aecm);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
aecm->initFlag = 0;
|
||||
aecm->lastError = 0;
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
aecm->aecmCore->farFile = fopen("aecFar.pcm","wb");
|
||||
aecm->aecmCore->nearFile = fopen("aecNear.pcm","wb");
|
||||
aecm->aecmCore->outFile = fopen("aecOut.pcm","wb");
|
||||
//aecm->aecmCore->outLpFile = fopen("aecOutLp.pcm","wb");
|
||||
|
||||
aecm->bufFile = fopen("aecBuf.dat", "wb");
|
||||
aecm->delayFile = fopen("aecDelay.dat", "wb");
|
||||
aecm->preCompFile = fopen("preComp.pcm", "wb");
|
||||
aecm->postCompFile = fopen("postComp.pcm", "wb");
|
||||
#endif // AEC_DEBUG
|
||||
return aecm;
|
||||
}
|
||||
|
||||
void WebRtcAecm_Free(void* aecmInst) {
|
||||
AecMobile* aecm = aecmInst;
|
||||
|
||||
if (aecm == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
fclose(aecm->aecmCore->farFile);
|
||||
fclose(aecm->aecmCore->nearFile);
|
||||
fclose(aecm->aecmCore->outFile);
|
||||
//fclose(aecm->aecmCore->outLpFile);
|
||||
|
||||
fclose(aecm->bufFile);
|
||||
fclose(aecm->delayFile);
|
||||
fclose(aecm->preCompFile);
|
||||
fclose(aecm->postCompFile);
|
||||
#endif // AEC_DEBUG
|
||||
WebRtcAecm_FreeCore(aecm->aecmCore);
|
||||
WebRtc_FreeBuffer(aecm->farendBuf);
|
||||
free(aecm);
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_Init(void *aecmInst, int32_t sampFreq)
|
||||
{
|
||||
AecMobile* aecm = aecmInst;
|
||||
AecmConfig aecConfig;
|
||||
|
||||
if (aecm == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sampFreq != 8000 && sampFreq != 16000)
|
||||
{
|
||||
aecm->lastError = AECM_BAD_PARAMETER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
aecm->sampFreq = sampFreq;
|
||||
|
||||
// Initialize AECM core
|
||||
if (WebRtcAecm_InitCore(aecm->aecmCore, aecm->sampFreq) == -1)
|
||||
{
|
||||
aecm->lastError = AECM_UNSPECIFIED_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Initialize farend buffer
|
||||
WebRtc_InitBuffer(aecm->farendBuf);
|
||||
|
||||
aecm->initFlag = kInitCheck; // indicates that initialization has been done
|
||||
|
||||
aecm->delayChange = 1;
|
||||
|
||||
aecm->sum = 0;
|
||||
aecm->counter = 0;
|
||||
aecm->checkBuffSize = 1;
|
||||
aecm->firstVal = 0;
|
||||
|
||||
aecm->ECstartup = 1;
|
||||
aecm->bufSizeStart = 0;
|
||||
aecm->checkBufSizeCtr = 0;
|
||||
aecm->filtDelay = 0;
|
||||
aecm->timeForDelayChange = 0;
|
||||
aecm->knownDelay = 0;
|
||||
aecm->lastDelayDiff = 0;
|
||||
|
||||
memset(&aecm->farendOld[0][0], 0, 160);
|
||||
|
||||
// Default settings.
|
||||
aecConfig.cngMode = AecmTrue;
|
||||
aecConfig.echoMode = 3;
|
||||
|
||||
if (WebRtcAecm_set_config(aecm, aecConfig) == -1)
|
||||
{
|
||||
aecm->lastError = AECM_UNSPECIFIED_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_BufferFarend(void *aecmInst, const int16_t *farend,
|
||||
size_t nrOfSamples)
|
||||
{
|
||||
AecMobile* aecm = aecmInst;
|
||||
int32_t retVal = 0;
|
||||
|
||||
if (aecm == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (farend == NULL)
|
||||
{
|
||||
aecm->lastError = AECM_NULL_POINTER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aecm->initFlag != kInitCheck)
|
||||
{
|
||||
aecm->lastError = AECM_UNINITIALIZED_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nrOfSamples != 80 && nrOfSamples != 160)
|
||||
{
|
||||
aecm->lastError = AECM_BAD_PARAMETER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TODO: Is this really a good idea?
|
||||
if (!aecm->ECstartup)
|
||||
{
|
||||
WebRtcAecm_DelayComp(aecm);
|
||||
}
|
||||
|
||||
WebRtc_WriteBuffer(aecm->farendBuf, farend, nrOfSamples);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_Process(void *aecmInst, const int16_t *nearendNoisy,
|
||||
const int16_t *nearendClean, int16_t *out,
|
||||
size_t nrOfSamples, int16_t msInSndCardBuf)
|
||||
{
|
||||
AecMobile* aecm = aecmInst;
|
||||
int32_t retVal = 0;
|
||||
size_t i;
|
||||
short nmbrOfFilledBuffers;
|
||||
size_t nBlocks10ms;
|
||||
size_t nFrames;
|
||||
#ifdef AEC_DEBUG
|
||||
short msInAECBuf;
|
||||
#endif
|
||||
|
||||
if (aecm == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nearendNoisy == NULL)
|
||||
{
|
||||
aecm->lastError = AECM_NULL_POINTER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (out == NULL)
|
||||
{
|
||||
aecm->lastError = AECM_NULL_POINTER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aecm->initFlag != kInitCheck)
|
||||
{
|
||||
aecm->lastError = AECM_UNINITIALIZED_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nrOfSamples != 80 && nrOfSamples != 160)
|
||||
{
|
||||
aecm->lastError = AECM_BAD_PARAMETER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (msInSndCardBuf < 0)
|
||||
{
|
||||
msInSndCardBuf = 0;
|
||||
aecm->lastError = AECM_BAD_PARAMETER_WARNING;
|
||||
retVal = -1;
|
||||
} else if (msInSndCardBuf > 500)
|
||||
{
|
||||
msInSndCardBuf = 500;
|
||||
aecm->lastError = AECM_BAD_PARAMETER_WARNING;
|
||||
retVal = -1;
|
||||
}
|
||||
msInSndCardBuf += 10;
|
||||
aecm->msInSndCardBuf = msInSndCardBuf;
|
||||
|
||||
nFrames = nrOfSamples / FRAME_LEN;
|
||||
nBlocks10ms = nFrames / aecm->aecmCore->mult;
|
||||
|
||||
if (aecm->ECstartup)
|
||||
{
|
||||
if (nearendClean == NULL)
|
||||
{
|
||||
if (out != nearendNoisy)
|
||||
{
|
||||
memcpy(out, nearendNoisy, sizeof(short) * nrOfSamples);
|
||||
}
|
||||
} else if (out != nearendClean)
|
||||
{
|
||||
memcpy(out, nearendClean, sizeof(short) * nrOfSamples);
|
||||
}
|
||||
|
||||
nmbrOfFilledBuffers =
|
||||
(short) WebRtc_available_read(aecm->farendBuf) / FRAME_LEN;
|
||||
// The AECM is in the start up mode
|
||||
// AECM is disabled until the soundcard buffer and farend buffers are OK
|
||||
|
||||
// Mechanism to ensure that the soundcard buffer is reasonably stable.
|
||||
if (aecm->checkBuffSize)
|
||||
{
|
||||
aecm->checkBufSizeCtr++;
|
||||
// Before we fill up the far end buffer we require the amount of data on the
|
||||
// sound card to be stable (+/-8 ms) compared to the first value. This
|
||||
// comparison is made during the following 4 consecutive frames. If it seems
|
||||
// to be stable then we start to fill up the far end buffer.
|
||||
|
||||
if (aecm->counter == 0)
|
||||
{
|
||||
aecm->firstVal = aecm->msInSndCardBuf;
|
||||
aecm->sum = 0;
|
||||
}
|
||||
|
||||
if (abs(aecm->firstVal - aecm->msInSndCardBuf)
|
||||
< WEBRTC_SPL_MAX(0.2 * aecm->msInSndCardBuf, kSampMsNb))
|
||||
{
|
||||
aecm->sum += aecm->msInSndCardBuf;
|
||||
aecm->counter++;
|
||||
} else
|
||||
{
|
||||
aecm->counter = 0;
|
||||
}
|
||||
|
||||
if (aecm->counter * nBlocks10ms >= 6)
|
||||
{
|
||||
// The farend buffer size is determined in blocks of 80 samples
|
||||
// Use 75% of the average value of the soundcard buffer
|
||||
aecm->bufSizeStart
|
||||
= WEBRTC_SPL_MIN((3 * aecm->sum
|
||||
* aecm->aecmCore->mult) / (aecm->counter * 40), BUF_SIZE_FRAMES);
|
||||
// buffersize has now been determined
|
||||
aecm->checkBuffSize = 0;
|
||||
}
|
||||
|
||||
if (aecm->checkBufSizeCtr * nBlocks10ms > 50)
|
||||
{
|
||||
// for really bad sound cards, don't disable echocanceller for more than 0.5 sec
|
||||
aecm->bufSizeStart = WEBRTC_SPL_MIN((3 * aecm->msInSndCardBuf
|
||||
* aecm->aecmCore->mult) / 40, BUF_SIZE_FRAMES);
|
||||
aecm->checkBuffSize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// if checkBuffSize changed in the if-statement above
|
||||
if (!aecm->checkBuffSize)
|
||||
{
|
||||
// soundcard buffer is now reasonably stable
|
||||
// When the far end buffer is filled with approximately the same amount of
|
||||
// data as the amount on the sound card we end the start up phase and start
|
||||
// to cancel echoes.
|
||||
|
||||
if (nmbrOfFilledBuffers == aecm->bufSizeStart)
|
||||
{
|
||||
aecm->ECstartup = 0; // Enable the AECM
|
||||
} else if (nmbrOfFilledBuffers > aecm->bufSizeStart)
|
||||
{
|
||||
WebRtc_MoveReadPtr(aecm->farendBuf,
|
||||
(int) WebRtc_available_read(aecm->farendBuf)
|
||||
- (int) aecm->bufSizeStart * FRAME_LEN);
|
||||
aecm->ECstartup = 0;
|
||||
}
|
||||
}
|
||||
|
||||
} else
|
||||
{
|
||||
// AECM is enabled
|
||||
|
||||
// Note only 1 block supported for nb and 2 blocks for wb
|
||||
for (i = 0; i < nFrames; i++)
|
||||
{
|
||||
int16_t farend[FRAME_LEN];
|
||||
const int16_t* farend_ptr = NULL;
|
||||
|
||||
nmbrOfFilledBuffers =
|
||||
(short) WebRtc_available_read(aecm->farendBuf) / FRAME_LEN;
|
||||
|
||||
// Check that there is data in the far end buffer
|
||||
if (nmbrOfFilledBuffers > 0)
|
||||
{
|
||||
// Get the next 80 samples from the farend buffer
|
||||
WebRtc_ReadBuffer(aecm->farendBuf, (void**) &farend_ptr, farend,
|
||||
FRAME_LEN);
|
||||
|
||||
// Always store the last frame for use when we run out of data
|
||||
memcpy(&(aecm->farendOld[i][0]), farend_ptr,
|
||||
FRAME_LEN * sizeof(short));
|
||||
} else
|
||||
{
|
||||
// We have no data so we use the last played frame
|
||||
memcpy(farend, &(aecm->farendOld[i][0]), FRAME_LEN * sizeof(short));
|
||||
farend_ptr = farend;
|
||||
}
|
||||
|
||||
// Call buffer delay estimator when all data is extracted,
|
||||
// i,e. i = 0 for NB and i = 1 for WB
|
||||
if ((i == 0 && aecm->sampFreq == 8000) || (i == 1 && aecm->sampFreq == 16000))
|
||||
{
|
||||
WebRtcAecm_EstBufDelay(aecm, aecm->msInSndCardBuf);
|
||||
}
|
||||
|
||||
// Call the AECM
|
||||
/*WebRtcAecm_ProcessFrame(aecm->aecmCore, farend, &nearend[FRAME_LEN * i],
|
||||
&out[FRAME_LEN * i], aecm->knownDelay);*/
|
||||
if (WebRtcAecm_ProcessFrame(aecm->aecmCore,
|
||||
farend_ptr,
|
||||
&nearendNoisy[FRAME_LEN * i],
|
||||
(nearendClean
|
||||
? &nearendClean[FRAME_LEN * i]
|
||||
: NULL),
|
||||
&out[FRAME_LEN * i]) == -1)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
msInAECBuf = (short) WebRtc_available_read(aecm->farendBuf) /
|
||||
(kSampMsNb * aecm->aecmCore->mult);
|
||||
fwrite(&msInAECBuf, 2, 1, aecm->bufFile);
|
||||
fwrite(&(aecm->knownDelay), sizeof(aecm->knownDelay), 1, aecm->delayFile);
|
||||
#endif
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_set_config(void *aecmInst, AecmConfig config)
|
||||
{
|
||||
AecMobile* aecm = aecmInst;
|
||||
|
||||
if (aecm == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aecm->initFlag != kInitCheck)
|
||||
{
|
||||
aecm->lastError = AECM_UNINITIALIZED_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (config.cngMode != AecmFalse && config.cngMode != AecmTrue)
|
||||
{
|
||||
aecm->lastError = AECM_BAD_PARAMETER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
aecm->aecmCore->cngMode = config.cngMode;
|
||||
|
||||
if (config.echoMode < 0 || config.echoMode > 4)
|
||||
{
|
||||
aecm->lastError = AECM_BAD_PARAMETER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
aecm->echoMode = config.echoMode;
|
||||
|
||||
if (aecm->echoMode == 0)
|
||||
{
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 3;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 3;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 3;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 3;
|
||||
aecm->aecmCore->supGainErrParamDiffAB = (SUPGAIN_ERROR_PARAM_A >> 3)
|
||||
- (SUPGAIN_ERROR_PARAM_B >> 3);
|
||||
aecm->aecmCore->supGainErrParamDiffBD = (SUPGAIN_ERROR_PARAM_B >> 3)
|
||||
- (SUPGAIN_ERROR_PARAM_D >> 3);
|
||||
} else if (aecm->echoMode == 1)
|
||||
{
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 2;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 2;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 2;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 2;
|
||||
aecm->aecmCore->supGainErrParamDiffAB = (SUPGAIN_ERROR_PARAM_A >> 2)
|
||||
- (SUPGAIN_ERROR_PARAM_B >> 2);
|
||||
aecm->aecmCore->supGainErrParamDiffBD = (SUPGAIN_ERROR_PARAM_B >> 2)
|
||||
- (SUPGAIN_ERROR_PARAM_D >> 2);
|
||||
} else if (aecm->echoMode == 2)
|
||||
{
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 1;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 1;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 1;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 1;
|
||||
aecm->aecmCore->supGainErrParamDiffAB = (SUPGAIN_ERROR_PARAM_A >> 1)
|
||||
- (SUPGAIN_ERROR_PARAM_B >> 1);
|
||||
aecm->aecmCore->supGainErrParamDiffBD = (SUPGAIN_ERROR_PARAM_B >> 1)
|
||||
- (SUPGAIN_ERROR_PARAM_D >> 1);
|
||||
} else if (aecm->echoMode == 3)
|
||||
{
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D;
|
||||
aecm->aecmCore->supGainErrParamDiffAB = SUPGAIN_ERROR_PARAM_A - SUPGAIN_ERROR_PARAM_B;
|
||||
aecm->aecmCore->supGainErrParamDiffBD = SUPGAIN_ERROR_PARAM_B - SUPGAIN_ERROR_PARAM_D;
|
||||
} else if (aecm->echoMode == 4)
|
||||
{
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT << 1;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT << 1;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A << 1;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D << 1;
|
||||
aecm->aecmCore->supGainErrParamDiffAB = (SUPGAIN_ERROR_PARAM_A << 1)
|
||||
- (SUPGAIN_ERROR_PARAM_B << 1);
|
||||
aecm->aecmCore->supGainErrParamDiffBD = (SUPGAIN_ERROR_PARAM_B << 1)
|
||||
- (SUPGAIN_ERROR_PARAM_D << 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_get_config(void *aecmInst, AecmConfig *config)
|
||||
{
|
||||
AecMobile* aecm = aecmInst;
|
||||
|
||||
if (aecm == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (config == NULL)
|
||||
{
|
||||
aecm->lastError = AECM_NULL_POINTER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aecm->initFlag != kInitCheck)
|
||||
{
|
||||
aecm->lastError = AECM_UNINITIALIZED_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
config->cngMode = aecm->aecmCore->cngMode;
|
||||
config->echoMode = aecm->echoMode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_InitEchoPath(void* aecmInst,
|
||||
const void* echo_path,
|
||||
size_t size_bytes)
|
||||
{
|
||||
AecMobile* aecm = aecmInst;
|
||||
const int16_t* echo_path_ptr = echo_path;
|
||||
|
||||
if (aecmInst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (echo_path == NULL) {
|
||||
aecm->lastError = AECM_NULL_POINTER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
if (size_bytes != WebRtcAecm_echo_path_size_bytes())
|
||||
{
|
||||
// Input channel size does not match the size of AECM
|
||||
aecm->lastError = AECM_BAD_PARAMETER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
if (aecm->initFlag != kInitCheck)
|
||||
{
|
||||
aecm->lastError = AECM_UNINITIALIZED_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
WebRtcAecm_InitEchoPathCore(aecm->aecmCore, echo_path_ptr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_GetEchoPath(void* aecmInst,
|
||||
void* echo_path,
|
||||
size_t size_bytes)
|
||||
{
|
||||
AecMobile* aecm = aecmInst;
|
||||
int16_t* echo_path_ptr = echo_path;
|
||||
|
||||
if (aecmInst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (echo_path == NULL) {
|
||||
aecm->lastError = AECM_NULL_POINTER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
if (size_bytes != WebRtcAecm_echo_path_size_bytes())
|
||||
{
|
||||
// Input channel size does not match the size of AECM
|
||||
aecm->lastError = AECM_BAD_PARAMETER_ERROR;
|
||||
return -1;
|
||||
}
|
||||
if (aecm->initFlag != kInitCheck)
|
||||
{
|
||||
aecm->lastError = AECM_UNINITIALIZED_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(echo_path_ptr, aecm->aecmCore->channelStored, size_bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t WebRtcAecm_echo_path_size_bytes()
|
||||
{
|
||||
return (PART_LEN1 * sizeof(int16_t));
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_get_error_code(void *aecmInst)
|
||||
{
|
||||
AecMobile* aecm = aecmInst;
|
||||
|
||||
if (aecm == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return aecm->lastError;
|
||||
}
|
||||
|
||||
static int WebRtcAecm_EstBufDelay(AecMobile* aecm, short msInSndCardBuf) {
|
||||
short delayNew, nSampSndCard;
|
||||
short nSampFar = (short) WebRtc_available_read(aecm->farendBuf);
|
||||
short diff;
|
||||
|
||||
nSampSndCard = msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult;
|
||||
|
||||
delayNew = nSampSndCard - nSampFar;
|
||||
|
||||
if (delayNew < FRAME_LEN)
|
||||
{
|
||||
WebRtc_MoveReadPtr(aecm->farendBuf, FRAME_LEN);
|
||||
delayNew += FRAME_LEN;
|
||||
}
|
||||
|
||||
aecm->filtDelay = WEBRTC_SPL_MAX(0, (8 * aecm->filtDelay + 2 * delayNew) / 10);
|
||||
|
||||
diff = aecm->filtDelay - aecm->knownDelay;
|
||||
if (diff > 224)
|
||||
{
|
||||
if (aecm->lastDelayDiff < 96)
|
||||
{
|
||||
aecm->timeForDelayChange = 0;
|
||||
} else
|
||||
{
|
||||
aecm->timeForDelayChange++;
|
||||
}
|
||||
} else if (diff < 96 && aecm->knownDelay > 0)
|
||||
{
|
||||
if (aecm->lastDelayDiff > 224)
|
||||
{
|
||||
aecm->timeForDelayChange = 0;
|
||||
} else
|
||||
{
|
||||
aecm->timeForDelayChange++;
|
||||
}
|
||||
} else
|
||||
{
|
||||
aecm->timeForDelayChange = 0;
|
||||
}
|
||||
aecm->lastDelayDiff = diff;
|
||||
|
||||
if (aecm->timeForDelayChange > 25)
|
||||
{
|
||||
aecm->knownDelay = WEBRTC_SPL_MAX((int)aecm->filtDelay - 160, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int WebRtcAecm_DelayComp(AecMobile* aecm) {
|
||||
int nSampFar = (int) WebRtc_available_read(aecm->farendBuf);
|
||||
int nSampSndCard, delayNew, nSampAdd;
|
||||
const int maxStuffSamp = 10 * FRAME_LEN;
|
||||
|
||||
nSampSndCard = aecm->msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult;
|
||||
delayNew = nSampSndCard - nSampFar;
|
||||
|
||||
if (delayNew > FAR_BUF_LEN - FRAME_LEN * aecm->aecmCore->mult)
|
||||
{
|
||||
// The difference of the buffer sizes is larger than the maximum
|
||||
// allowed known delay. Compensate by stuffing the buffer.
|
||||
nSampAdd = (int)(WEBRTC_SPL_MAX(((nSampSndCard >> 1) - nSampFar),
|
||||
FRAME_LEN));
|
||||
nSampAdd = WEBRTC_SPL_MIN(nSampAdd, maxStuffSamp);
|
||||
|
||||
WebRtc_MoveReadPtr(aecm->farendBuf, -nSampAdd);
|
||||
aecm->delayChange = 1; // the delay needs to be updated
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
599
webrtc/modules/audio_processing/aecm/echo_control_mobile.cc
Normal file
599
webrtc/modules/audio_processing/aecm/echo_control_mobile.cc
Normal file
@ -0,0 +1,599 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "modules/audio_processing/aecm/echo_control_mobile.h"
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
extern "C" {
|
||||
#include "common_audio/ring_buffer.h"
|
||||
#include "common_audio/signal_processing/include/signal_processing_library.h"
|
||||
#include "modules/audio_processing/aecm/aecm_defines.h"
|
||||
}
|
||||
#include "modules/audio_processing/aecm/aecm_core.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
namespace {
|
||||
|
||||
#define BUF_SIZE_FRAMES 50 // buffer size (frames)
|
||||
// Maximum length of resampled signal. Must be an integer multiple of frames
|
||||
// (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN
|
||||
// The factor of 2 handles wb, and the + 1 is as a safety margin
|
||||
#define MAX_RESAMP_LEN (5 * FRAME_LEN)
|
||||
|
||||
static const size_t kBufSizeSamp =
|
||||
BUF_SIZE_FRAMES * FRAME_LEN; // buffer size (samples)
|
||||
static const int kSampMsNb = 8; // samples per ms in nb
|
||||
// Target suppression levels for nlp modes
|
||||
// log{0.001, 0.00001, 0.00000001}
|
||||
static const int kInitCheck = 42;
|
||||
|
||||
typedef struct {
|
||||
int sampFreq;
|
||||
int scSampFreq;
|
||||
short bufSizeStart;
|
||||
int knownDelay;
|
||||
|
||||
// Stores the last frame added to the farend buffer
|
||||
short farendOld[2][FRAME_LEN];
|
||||
short initFlag; // indicates if AEC has been initialized
|
||||
|
||||
// Variables used for averaging far end buffer size
|
||||
short counter;
|
||||
short sum;
|
||||
short firstVal;
|
||||
short checkBufSizeCtr;
|
||||
|
||||
// Variables used for delay shifts
|
||||
short msInSndCardBuf;
|
||||
short filtDelay;
|
||||
int timeForDelayChange;
|
||||
int ECstartup;
|
||||
int checkBuffSize;
|
||||
int delayChange;
|
||||
short lastDelayDiff;
|
||||
|
||||
int16_t echoMode;
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
FILE* bufFile;
|
||||
FILE* delayFile;
|
||||
FILE* preCompFile;
|
||||
FILE* postCompFile;
|
||||
#endif // AEC_DEBUG
|
||||
// Structures
|
||||
RingBuffer* farendBuf;
|
||||
|
||||
AecmCore* aecmCore;
|
||||
} AecMobile;
|
||||
|
||||
} // namespace
|
||||
|
||||
// Estimates delay to set the position of the farend buffer read pointer
|
||||
// (controlled by knownDelay)
|
||||
static int WebRtcAecm_EstBufDelay(AecMobile* aecm, short msInSndCardBuf);
|
||||
|
||||
// Stuffs the farend buffer if the estimated delay is too large
|
||||
static int WebRtcAecm_DelayComp(AecMobile* aecm);
|
||||
|
||||
void* WebRtcAecm_Create() {
|
||||
// Allocate zero-filled memory.
|
||||
AecMobile* aecm = static_cast<AecMobile*>(calloc(1, sizeof(AecMobile)));
|
||||
|
||||
aecm->aecmCore = WebRtcAecm_CreateCore();
|
||||
if (!aecm->aecmCore) {
|
||||
WebRtcAecm_Free(aecm);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
aecm->farendBuf = WebRtc_CreateBuffer(kBufSizeSamp, sizeof(int16_t));
|
||||
if (!aecm->farendBuf) {
|
||||
WebRtcAecm_Free(aecm);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
aecm->aecmCore->farFile = fopen("aecFar.pcm", "wb");
|
||||
aecm->aecmCore->nearFile = fopen("aecNear.pcm", "wb");
|
||||
aecm->aecmCore->outFile = fopen("aecOut.pcm", "wb");
|
||||
// aecm->aecmCore->outLpFile = fopen("aecOutLp.pcm","wb");
|
||||
|
||||
aecm->bufFile = fopen("aecBuf.dat", "wb");
|
||||
aecm->delayFile = fopen("aecDelay.dat", "wb");
|
||||
aecm->preCompFile = fopen("preComp.pcm", "wb");
|
||||
aecm->postCompFile = fopen("postComp.pcm", "wb");
|
||||
#endif // AEC_DEBUG
|
||||
return aecm;
|
||||
}
|
||||
|
||||
void WebRtcAecm_Free(void* aecmInst) {
|
||||
AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
|
||||
|
||||
if (aecm == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
fclose(aecm->aecmCore->farFile);
|
||||
fclose(aecm->aecmCore->nearFile);
|
||||
fclose(aecm->aecmCore->outFile);
|
||||
// fclose(aecm->aecmCore->outLpFile);
|
||||
|
||||
fclose(aecm->bufFile);
|
||||
fclose(aecm->delayFile);
|
||||
fclose(aecm->preCompFile);
|
||||
fclose(aecm->postCompFile);
|
||||
#endif // AEC_DEBUG
|
||||
WebRtcAecm_FreeCore(aecm->aecmCore);
|
||||
WebRtc_FreeBuffer(aecm->farendBuf);
|
||||
free(aecm);
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_Init(void* aecmInst, int32_t sampFreq) {
|
||||
AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
|
||||
AecmConfig aecConfig;
|
||||
|
||||
if (aecm == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sampFreq != 8000 && sampFreq != 16000) {
|
||||
return AECM_BAD_PARAMETER_ERROR;
|
||||
}
|
||||
aecm->sampFreq = sampFreq;
|
||||
|
||||
// Initialize AECM core
|
||||
if (WebRtcAecm_InitCore(aecm->aecmCore, aecm->sampFreq) == -1) {
|
||||
return AECM_UNSPECIFIED_ERROR;
|
||||
}
|
||||
|
||||
// Initialize farend buffer
|
||||
WebRtc_InitBuffer(aecm->farendBuf);
|
||||
|
||||
aecm->initFlag = kInitCheck; // indicates that initialization has been done
|
||||
|
||||
aecm->delayChange = 1;
|
||||
|
||||
aecm->sum = 0;
|
||||
aecm->counter = 0;
|
||||
aecm->checkBuffSize = 1;
|
||||
aecm->firstVal = 0;
|
||||
|
||||
aecm->ECstartup = 1;
|
||||
aecm->bufSizeStart = 0;
|
||||
aecm->checkBufSizeCtr = 0;
|
||||
aecm->filtDelay = 0;
|
||||
aecm->timeForDelayChange = 0;
|
||||
aecm->knownDelay = 0;
|
||||
aecm->lastDelayDiff = 0;
|
||||
|
||||
memset(&aecm->farendOld, 0, sizeof(aecm->farendOld));
|
||||
|
||||
// Default settings.
|
||||
aecConfig.cngMode = AecmTrue;
|
||||
aecConfig.echoMode = 3;
|
||||
|
||||
if (WebRtcAecm_set_config(aecm, aecConfig) == -1) {
|
||||
return AECM_UNSPECIFIED_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Returns any error that is caused when buffering the
|
||||
// farend signal.
|
||||
int32_t WebRtcAecm_GetBufferFarendError(void* aecmInst,
|
||||
const int16_t* farend,
|
||||
size_t nrOfSamples) {
|
||||
AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
|
||||
|
||||
if (aecm == NULL)
|
||||
return -1;
|
||||
|
||||
if (farend == NULL)
|
||||
return AECM_NULL_POINTER_ERROR;
|
||||
|
||||
if (aecm->initFlag != kInitCheck)
|
||||
return AECM_UNINITIALIZED_ERROR;
|
||||
|
||||
if (nrOfSamples != 80 && nrOfSamples != 160)
|
||||
return AECM_BAD_PARAMETER_ERROR;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_BufferFarend(void* aecmInst,
|
||||
const int16_t* farend,
|
||||
size_t nrOfSamples) {
|
||||
AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
|
||||
|
||||
const int32_t err =
|
||||
WebRtcAecm_GetBufferFarendError(aecmInst, farend, nrOfSamples);
|
||||
|
||||
if (err != 0)
|
||||
return err;
|
||||
|
||||
// TODO(unknown): Is this really a good idea?
|
||||
if (!aecm->ECstartup) {
|
||||
WebRtcAecm_DelayComp(aecm);
|
||||
}
|
||||
|
||||
WebRtc_WriteBuffer(aecm->farendBuf, farend, nrOfSamples);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_Process(void* aecmInst,
|
||||
const int16_t* nearendNoisy,
|
||||
const int16_t* nearendClean,
|
||||
int16_t* out,
|
||||
size_t nrOfSamples,
|
||||
int16_t msInSndCardBuf) {
|
||||
AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
|
||||
int32_t retVal = 0;
|
||||
size_t i;
|
||||
short nmbrOfFilledBuffers;
|
||||
size_t nBlocks10ms;
|
||||
size_t nFrames;
|
||||
#ifdef AEC_DEBUG
|
||||
short msInAECBuf;
|
||||
#endif
|
||||
|
||||
if (aecm == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nearendNoisy == NULL) {
|
||||
return AECM_NULL_POINTER_ERROR;
|
||||
}
|
||||
|
||||
if (out == NULL) {
|
||||
return AECM_NULL_POINTER_ERROR;
|
||||
}
|
||||
|
||||
if (aecm->initFlag != kInitCheck) {
|
||||
return AECM_UNINITIALIZED_ERROR;
|
||||
}
|
||||
|
||||
if (nrOfSamples != 80 && nrOfSamples != 160) {
|
||||
return AECM_BAD_PARAMETER_ERROR;
|
||||
}
|
||||
|
||||
if (msInSndCardBuf < 0) {
|
||||
msInSndCardBuf = 0;
|
||||
retVal = AECM_BAD_PARAMETER_WARNING;
|
||||
} else if (msInSndCardBuf > 500) {
|
||||
msInSndCardBuf = 500;
|
||||
retVal = AECM_BAD_PARAMETER_WARNING;
|
||||
}
|
||||
msInSndCardBuf += 10;
|
||||
aecm->msInSndCardBuf = msInSndCardBuf;
|
||||
|
||||
nFrames = nrOfSamples / FRAME_LEN;
|
||||
nBlocks10ms = nFrames / aecm->aecmCore->mult;
|
||||
|
||||
if (aecm->ECstartup) {
|
||||
if (nearendClean == NULL) {
|
||||
if (out != nearendNoisy) {
|
||||
memcpy(out, nearendNoisy, sizeof(short) * nrOfSamples);
|
||||
}
|
||||
} else if (out != nearendClean) {
|
||||
memcpy(out, nearendClean, sizeof(short) * nrOfSamples);
|
||||
}
|
||||
|
||||
nmbrOfFilledBuffers =
|
||||
(short)WebRtc_available_read(aecm->farendBuf) / FRAME_LEN;
|
||||
// The AECM is in the start up mode
|
||||
// AECM is disabled until the soundcard buffer and farend buffers are OK
|
||||
|
||||
// Mechanism to ensure that the soundcard buffer is reasonably stable.
|
||||
if (aecm->checkBuffSize) {
|
||||
aecm->checkBufSizeCtr++;
|
||||
// Before we fill up the far end buffer we require the amount of data on
|
||||
// the sound card to be stable (+/-8 ms) compared to the first value. This
|
||||
// comparison is made during the following 4 consecutive frames. If it
|
||||
// seems to be stable then we start to fill up the far end buffer.
|
||||
|
||||
if (aecm->counter == 0) {
|
||||
aecm->firstVal = aecm->msInSndCardBuf;
|
||||
aecm->sum = 0;
|
||||
}
|
||||
|
||||
if (abs(aecm->firstVal - aecm->msInSndCardBuf) <
|
||||
WEBRTC_SPL_MAX(0.2 * aecm->msInSndCardBuf, kSampMsNb)) {
|
||||
aecm->sum += aecm->msInSndCardBuf;
|
||||
aecm->counter++;
|
||||
} else {
|
||||
aecm->counter = 0;
|
||||
}
|
||||
|
||||
if (aecm->counter * nBlocks10ms >= 6) {
|
||||
// The farend buffer size is determined in blocks of 80 samples
|
||||
// Use 75% of the average value of the soundcard buffer
|
||||
aecm->bufSizeStart = WEBRTC_SPL_MIN(
|
||||
(3 * aecm->sum * aecm->aecmCore->mult) / (aecm->counter * 40),
|
||||
BUF_SIZE_FRAMES);
|
||||
// buffersize has now been determined
|
||||
aecm->checkBuffSize = 0;
|
||||
}
|
||||
|
||||
if (aecm->checkBufSizeCtr * nBlocks10ms > 50) {
|
||||
// for really bad sound cards, don't disable echocanceller for more than
|
||||
// 0.5 sec
|
||||
aecm->bufSizeStart = WEBRTC_SPL_MIN(
|
||||
(3 * aecm->msInSndCardBuf * aecm->aecmCore->mult) / 40,
|
||||
BUF_SIZE_FRAMES);
|
||||
aecm->checkBuffSize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// if checkBuffSize changed in the if-statement above
|
||||
if (!aecm->checkBuffSize) {
|
||||
// soundcard buffer is now reasonably stable
|
||||
// When the far end buffer is filled with approximately the same amount of
|
||||
// data as the amount on the sound card we end the start up phase and
|
||||
// start to cancel echoes.
|
||||
|
||||
if (nmbrOfFilledBuffers == aecm->bufSizeStart) {
|
||||
aecm->ECstartup = 0; // Enable the AECM
|
||||
} else if (nmbrOfFilledBuffers > aecm->bufSizeStart) {
|
||||
WebRtc_MoveReadPtr(aecm->farendBuf,
|
||||
(int)WebRtc_available_read(aecm->farendBuf) -
|
||||
(int)aecm->bufSizeStart * FRAME_LEN);
|
||||
aecm->ECstartup = 0;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// AECM is enabled
|
||||
|
||||
// Note only 1 block supported for nb and 2 blocks for wb
|
||||
for (i = 0; i < nFrames; i++) {
|
||||
int16_t farend[FRAME_LEN];
|
||||
const int16_t* farend_ptr = NULL;
|
||||
|
||||
nmbrOfFilledBuffers =
|
||||
(short)WebRtc_available_read(aecm->farendBuf) / FRAME_LEN;
|
||||
|
||||
// Check that there is data in the far end buffer
|
||||
if (nmbrOfFilledBuffers > 0) {
|
||||
// Get the next 80 samples from the farend buffer
|
||||
WebRtc_ReadBuffer(aecm->farendBuf, (void**)&farend_ptr, farend,
|
||||
FRAME_LEN);
|
||||
|
||||
// Always store the last frame for use when we run out of data
|
||||
memcpy(&(aecm->farendOld[i][0]), farend_ptr, FRAME_LEN * sizeof(short));
|
||||
} else {
|
||||
// We have no data so we use the last played frame
|
||||
memcpy(farend, &(aecm->farendOld[i][0]), FRAME_LEN * sizeof(short));
|
||||
farend_ptr = farend;
|
||||
}
|
||||
|
||||
// Call buffer delay estimator when all data is extracted,
|
||||
// i,e. i = 0 for NB and i = 1 for WB
|
||||
if ((i == 0 && aecm->sampFreq == 8000) ||
|
||||
(i == 1 && aecm->sampFreq == 16000)) {
|
||||
WebRtcAecm_EstBufDelay(aecm, aecm->msInSndCardBuf);
|
||||
}
|
||||
|
||||
// Call the AECM
|
||||
/*WebRtcAecm_ProcessFrame(aecm->aecmCore, farend, &nearend[FRAME_LEN * i],
|
||||
&out[FRAME_LEN * i], aecm->knownDelay);*/
|
||||
if (WebRtcAecm_ProcessFrame(
|
||||
aecm->aecmCore, farend_ptr, &nearendNoisy[FRAME_LEN * i],
|
||||
(nearendClean ? &nearendClean[FRAME_LEN * i] : NULL),
|
||||
&out[FRAME_LEN * i]) == -1)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef AEC_DEBUG
|
||||
msInAECBuf = (short)WebRtc_available_read(aecm->farendBuf) /
|
||||
(kSampMsNb * aecm->aecmCore->mult);
|
||||
fwrite(&msInAECBuf, 2, 1, aecm->bufFile);
|
||||
fwrite(&(aecm->knownDelay), sizeof(aecm->knownDelay), 1, aecm->delayFile);
|
||||
#endif
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_set_config(void* aecmInst, AecmConfig config) {
|
||||
AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
|
||||
|
||||
if (aecm == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aecm->initFlag != kInitCheck) {
|
||||
return AECM_UNINITIALIZED_ERROR;
|
||||
}
|
||||
|
||||
if (config.cngMode != AecmFalse && config.cngMode != AecmTrue) {
|
||||
return AECM_BAD_PARAMETER_ERROR;
|
||||
}
|
||||
aecm->aecmCore->cngMode = config.cngMode;
|
||||
|
||||
if (config.echoMode < 0 || config.echoMode > 4) {
|
||||
return AECM_BAD_PARAMETER_ERROR;
|
||||
}
|
||||
aecm->echoMode = config.echoMode;
|
||||
|
||||
if (aecm->echoMode == 0) {
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 3;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 3;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 3;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 3;
|
||||
aecm->aecmCore->supGainErrParamDiffAB =
|
||||
(SUPGAIN_ERROR_PARAM_A >> 3) - (SUPGAIN_ERROR_PARAM_B >> 3);
|
||||
aecm->aecmCore->supGainErrParamDiffBD =
|
||||
(SUPGAIN_ERROR_PARAM_B >> 3) - (SUPGAIN_ERROR_PARAM_D >> 3);
|
||||
} else if (aecm->echoMode == 1) {
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 2;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 2;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 2;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 2;
|
||||
aecm->aecmCore->supGainErrParamDiffAB =
|
||||
(SUPGAIN_ERROR_PARAM_A >> 2) - (SUPGAIN_ERROR_PARAM_B >> 2);
|
||||
aecm->aecmCore->supGainErrParamDiffBD =
|
||||
(SUPGAIN_ERROR_PARAM_B >> 2) - (SUPGAIN_ERROR_PARAM_D >> 2);
|
||||
} else if (aecm->echoMode == 2) {
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 1;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 1;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 1;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 1;
|
||||
aecm->aecmCore->supGainErrParamDiffAB =
|
||||
(SUPGAIN_ERROR_PARAM_A >> 1) - (SUPGAIN_ERROR_PARAM_B >> 1);
|
||||
aecm->aecmCore->supGainErrParamDiffBD =
|
||||
(SUPGAIN_ERROR_PARAM_B >> 1) - (SUPGAIN_ERROR_PARAM_D >> 1);
|
||||
} else if (aecm->echoMode == 3) {
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D;
|
||||
aecm->aecmCore->supGainErrParamDiffAB =
|
||||
SUPGAIN_ERROR_PARAM_A - SUPGAIN_ERROR_PARAM_B;
|
||||
aecm->aecmCore->supGainErrParamDiffBD =
|
||||
SUPGAIN_ERROR_PARAM_B - SUPGAIN_ERROR_PARAM_D;
|
||||
} else if (aecm->echoMode == 4) {
|
||||
aecm->aecmCore->supGain = SUPGAIN_DEFAULT << 1;
|
||||
aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT << 1;
|
||||
aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A << 1;
|
||||
aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D << 1;
|
||||
aecm->aecmCore->supGainErrParamDiffAB =
|
||||
(SUPGAIN_ERROR_PARAM_A << 1) - (SUPGAIN_ERROR_PARAM_B << 1);
|
||||
aecm->aecmCore->supGainErrParamDiffBD =
|
||||
(SUPGAIN_ERROR_PARAM_B << 1) - (SUPGAIN_ERROR_PARAM_D << 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_InitEchoPath(void* aecmInst,
|
||||
const void* echo_path,
|
||||
size_t size_bytes) {
|
||||
AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
|
||||
const int16_t* echo_path_ptr = static_cast<const int16_t*>(echo_path);
|
||||
|
||||
if (aecmInst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (echo_path == NULL) {
|
||||
return AECM_NULL_POINTER_ERROR;
|
||||
}
|
||||
if (size_bytes != WebRtcAecm_echo_path_size_bytes()) {
|
||||
// Input channel size does not match the size of AECM
|
||||
return AECM_BAD_PARAMETER_ERROR;
|
||||
}
|
||||
if (aecm->initFlag != kInitCheck) {
|
||||
return AECM_UNINITIALIZED_ERROR;
|
||||
}
|
||||
|
||||
WebRtcAecm_InitEchoPathCore(aecm->aecmCore, echo_path_ptr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t WebRtcAecm_GetEchoPath(void* aecmInst,
|
||||
void* echo_path,
|
||||
size_t size_bytes) {
|
||||
AecMobile* aecm = static_cast<AecMobile*>(aecmInst);
|
||||
int16_t* echo_path_ptr = static_cast<int16_t*>(echo_path);
|
||||
|
||||
if (aecmInst == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (echo_path == NULL) {
|
||||
return AECM_NULL_POINTER_ERROR;
|
||||
}
|
||||
if (size_bytes != WebRtcAecm_echo_path_size_bytes()) {
|
||||
// Input channel size does not match the size of AECM
|
||||
return AECM_BAD_PARAMETER_ERROR;
|
||||
}
|
||||
if (aecm->initFlag != kInitCheck) {
|
||||
return AECM_UNINITIALIZED_ERROR;
|
||||
}
|
||||
|
||||
memcpy(echo_path_ptr, aecm->aecmCore->channelStored, size_bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t WebRtcAecm_echo_path_size_bytes() {
|
||||
return (PART_LEN1 * sizeof(int16_t));
|
||||
}
|
||||
|
||||
static int WebRtcAecm_EstBufDelay(AecMobile* aecm, short msInSndCardBuf) {
|
||||
short delayNew, nSampSndCard;
|
||||
short nSampFar = (short)WebRtc_available_read(aecm->farendBuf);
|
||||
short diff;
|
||||
|
||||
nSampSndCard = msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult;
|
||||
|
||||
delayNew = nSampSndCard - nSampFar;
|
||||
|
||||
if (delayNew < FRAME_LEN) {
|
||||
WebRtc_MoveReadPtr(aecm->farendBuf, FRAME_LEN);
|
||||
delayNew += FRAME_LEN;
|
||||
}
|
||||
|
||||
aecm->filtDelay =
|
||||
WEBRTC_SPL_MAX(0, (8 * aecm->filtDelay + 2 * delayNew) / 10);
|
||||
|
||||
diff = aecm->filtDelay - aecm->knownDelay;
|
||||
if (diff > 224) {
|
||||
if (aecm->lastDelayDiff < 96) {
|
||||
aecm->timeForDelayChange = 0;
|
||||
} else {
|
||||
aecm->timeForDelayChange++;
|
||||
}
|
||||
} else if (diff < 96 && aecm->knownDelay > 0) {
|
||||
if (aecm->lastDelayDiff > 224) {
|
||||
aecm->timeForDelayChange = 0;
|
||||
} else {
|
||||
aecm->timeForDelayChange++;
|
||||
}
|
||||
} else {
|
||||
aecm->timeForDelayChange = 0;
|
||||
}
|
||||
aecm->lastDelayDiff = diff;
|
||||
|
||||
if (aecm->timeForDelayChange > 25) {
|
||||
aecm->knownDelay = WEBRTC_SPL_MAX((int)aecm->filtDelay - 160, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int WebRtcAecm_DelayComp(AecMobile* aecm) {
|
||||
int nSampFar = (int)WebRtc_available_read(aecm->farendBuf);
|
||||
int nSampSndCard, delayNew, nSampAdd;
|
||||
const int maxStuffSamp = 10 * FRAME_LEN;
|
||||
|
||||
nSampSndCard = aecm->msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult;
|
||||
delayNew = nSampSndCard - nSampFar;
|
||||
|
||||
if (delayNew > FAR_BUF_LEN - FRAME_LEN * aecm->aecmCore->mult) {
|
||||
// The difference of the buffer sizes is larger than the maximum
|
||||
// allowed known delay. Compensate by stuffing the buffer.
|
||||
nSampAdd =
|
||||
(int)(WEBRTC_SPL_MAX(((nSampSndCard >> 1) - nSampFar), FRAME_LEN));
|
||||
nSampAdd = WEBRTC_SPL_MIN(nSampAdd, maxStuffSamp);
|
||||
|
||||
WebRtc_MoveReadPtr(aecm->farendBuf, -nSampAdd);
|
||||
aecm->delayChange = 1; // the delay needs to be updated
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
@ -8,31 +8,29 @@
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AECM_INCLUDE_ECHO_CONTROL_MOBILE_H_
|
||||
#define WEBRTC_MODULES_AUDIO_PROCESSING_AECM_INCLUDE_ECHO_CONTROL_MOBILE_H_
|
||||
#ifndef MODULES_AUDIO_PROCESSING_AECM_ECHO_CONTROL_MOBILE_H_
|
||||
#define MODULES_AUDIO_PROCESSING_AECM_ECHO_CONTROL_MOBILE_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "webrtc/typedefs.h"
|
||||
namespace webrtc {
|
||||
|
||||
enum {
|
||||
AecmFalse = 0,
|
||||
AecmTrue
|
||||
};
|
||||
enum { AecmFalse = 0, AecmTrue };
|
||||
|
||||
// Errors
|
||||
#define AECM_UNSPECIFIED_ERROR 12000
|
||||
#define AECM_UNSUPPORTED_FUNCTION_ERROR 12001
|
||||
#define AECM_UNINITIALIZED_ERROR 12002
|
||||
#define AECM_NULL_POINTER_ERROR 12003
|
||||
#define AECM_BAD_PARAMETER_ERROR 12004
|
||||
#define AECM_UNSPECIFIED_ERROR 12000
|
||||
#define AECM_UNSUPPORTED_FUNCTION_ERROR 12001
|
||||
#define AECM_UNINITIALIZED_ERROR 12002
|
||||
#define AECM_NULL_POINTER_ERROR 12003
|
||||
#define AECM_BAD_PARAMETER_ERROR 12004
|
||||
|
||||
// Warnings
|
||||
#define AECM_BAD_PARAMETER_WARNING 12100
|
||||
#define AECM_BAD_PARAMETER_WARNING 12100
|
||||
|
||||
typedef struct {
|
||||
int16_t cngMode; // AECM_FALSE, AECM_TRUE (default)
|
||||
int16_t echoMode; // 0, 1, 2, 3 (default), 4
|
||||
int16_t cngMode; // AECM_FALSE, AECM_TRUE (default)
|
||||
int16_t echoMode; // 0, 1, 2, 3 (default), 4
|
||||
} AecmConfig;
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -66,7 +64,7 @@ void WebRtcAecm_Free(void* aecmInst);
|
||||
* Outputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* int32_t return 0: OK
|
||||
* -1: error
|
||||
* 1200-12004,12100: error/warning
|
||||
*/
|
||||
int32_t WebRtcAecm_Init(void* aecmInst, int32_t sampFreq);
|
||||
|
||||
@ -83,12 +81,31 @@ int32_t WebRtcAecm_Init(void* aecmInst, int32_t sampFreq);
|
||||
* Outputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* int32_t return 0: OK
|
||||
* -1: error
|
||||
* 1200-12004,12100: error/warning
|
||||
*/
|
||||
int32_t WebRtcAecm_BufferFarend(void* aecmInst,
|
||||
const int16_t* farend,
|
||||
size_t nrOfSamples);
|
||||
|
||||
/*
|
||||
* Reports any errors that would arise when buffering a farend buffer.
|
||||
*
|
||||
* Inputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* void* aecmInst Pointer to the AECM instance
|
||||
* int16_t* farend In buffer containing one frame of
|
||||
* farend signal
|
||||
* int16_t nrOfSamples Number of samples in farend buffer
|
||||
*
|
||||
* Outputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* int32_t return 0: OK
|
||||
* 1200-12004,12100: error/warning
|
||||
*/
|
||||
int32_t WebRtcAecm_GetBufferFarendError(void* aecmInst,
|
||||
const int16_t* farend,
|
||||
size_t nrOfSamples);
|
||||
|
||||
/*
|
||||
* Runs the AECM on an 80 or 160 sample blocks of data.
|
||||
*
|
||||
@ -112,7 +129,7 @@ int32_t WebRtcAecm_BufferFarend(void* aecmInst,
|
||||
* -------------------------------------------------------------------
|
||||
* int16_t* out Out buffer, one frame of processed nearend
|
||||
* int32_t return 0: OK
|
||||
* -1: error
|
||||
* 1200-12004,12100: error/warning
|
||||
*/
|
||||
int32_t WebRtcAecm_Process(void* aecmInst,
|
||||
const int16_t* nearendNoisy,
|
||||
@ -133,26 +150,10 @@ int32_t WebRtcAecm_Process(void* aecmInst,
|
||||
* Outputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* int32_t return 0: OK
|
||||
* -1: error
|
||||
* 1200-12004,12100: error/warning
|
||||
*/
|
||||
int32_t WebRtcAecm_set_config(void* aecmInst, AecmConfig config);
|
||||
|
||||
/*
|
||||
* This function enables the user to set certain parameters on-the-fly
|
||||
*
|
||||
* Inputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* void* aecmInst Pointer to the AECM instance
|
||||
*
|
||||
* Outputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* AecmConfig* config Pointer to the config instance that
|
||||
* all properties will be written to
|
||||
* int32_t return 0: OK
|
||||
* -1: error
|
||||
*/
|
||||
int32_t WebRtcAecm_get_config(void *aecmInst, AecmConfig *config);
|
||||
|
||||
/*
|
||||
* This function enables the user to set the echo path on-the-fly.
|
||||
*
|
||||
@ -165,7 +166,7 @@ int32_t WebRtcAecm_get_config(void *aecmInst, AecmConfig *config);
|
||||
* Outputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* int32_t return 0: OK
|
||||
* -1: error
|
||||
* 1200-12004,12100: error/warning
|
||||
*/
|
||||
int32_t WebRtcAecm_InitEchoPath(void* aecmInst,
|
||||
const void* echo_path,
|
||||
@ -184,7 +185,7 @@ int32_t WebRtcAecm_InitEchoPath(void* aecmInst,
|
||||
* Outputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* int32_t return 0: OK
|
||||
* -1: error
|
||||
* 1200-12004,12100: error/warning
|
||||
*/
|
||||
int32_t WebRtcAecm_GetEchoPath(void* aecmInst,
|
||||
void* echo_path,
|
||||
@ -199,20 +200,10 @@ int32_t WebRtcAecm_GetEchoPath(void* aecmInst,
|
||||
*/
|
||||
size_t WebRtcAecm_echo_path_size_bytes();
|
||||
|
||||
/*
|
||||
* Gets the last error code.
|
||||
*
|
||||
* Inputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* void* aecmInst Pointer to the AECM instance
|
||||
*
|
||||
* Outputs Description
|
||||
* -------------------------------------------------------------------
|
||||
* int32_t return 11000-11100: error code
|
||||
*/
|
||||
int32_t WebRtcAecm_get_error_code(void *aecmInst);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AECM_INCLUDE_ECHO_CONTROL_MOBILE_H_
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // MODULES_AUDIO_PROCESSING_AECM_ECHO_CONTROL_MOBILE_H_
|
Reference in New Issue
Block a user