aec_core.h revision 21a2fc902d37fb9607698dc0770c477b149163fe
1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11/*
12 * Specifies the interface for the AEC core.
13 */
14
15#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_MAIN_SOURCE_AEC_CORE_H_
16#define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_MAIN_SOURCE_AEC_CORE_H_
17
18#ifdef WEBRTC_AEC_DEBUG_DUMP
19#include <stdio.h>
20#endif
21
22#include "webrtc/typedefs.h"
23
24#define FRAME_LEN 80
25#define PART_LEN 64  // Length of partition
26#define PART_LEN1 (PART_LEN + 1)  // Unique fft coefficients
27#define PART_LEN2 (PART_LEN * 2)  // Length of partition * 2
28#define NR_PART 12  // Number of partitions in filter.
29#define PREF_BAND_SIZE 24
30
31// Delay estimator constants, used for logging.
32enum { kMaxDelayBlocks = 60 };
33enum { kLookaheadBlocks = 15 };
34enum { kHistorySizeBlocks = kMaxDelayBlocks + kLookaheadBlocks };
35
36typedef float complex_t[2];
37// For performance reasons, some arrays of complex numbers are replaced by twice
38// as long arrays of float, all the real parts followed by all the imaginary
39// ones (complex_t[SIZE] -> float[2][SIZE]). This allows SIMD optimizations and
40// is better than two arrays (one for the real parts and one for the imaginary
41// parts) as this other way would require two pointers instead of one and cause
42// extra register spilling. This also allows the offsets to be calculated at
43// compile time.
44
45// Metrics
46enum {offsetLevel = -100};
47
48typedef struct {
49    float sfrsum;
50    int sfrcounter;
51    float framelevel;
52    float frsum;
53    int frcounter;
54    float minlevel;
55    float averagelevel;
56} power_level_t;
57
58typedef struct {
59    float instant;
60    float average;
61    float min;
62    float max;
63    float sum;
64    float hisum;
65    float himean;
66    int counter;
67    int hicounter;
68} stats_t;
69
70typedef struct {
71    int farBufWritePos, farBufReadPos;
72
73    int knownDelay;
74    int inSamples, outSamples;
75    int delayEstCtr;
76
77    void *nearFrBuf, *outFrBuf;
78
79    void *nearFrBufH;
80    void *outFrBufH;
81
82    float dBuf[PART_LEN2];  // nearend
83    float eBuf[PART_LEN2];  // error
84
85    float dBufH[PART_LEN2];  // nearend
86
87    float xPow[PART_LEN1];
88    float dPow[PART_LEN1];
89    float dMinPow[PART_LEN1];
90    float dInitMinPow[PART_LEN1];
91    float *noisePow;
92
93    float xfBuf[2][NR_PART * PART_LEN1];  // farend fft buffer
94    float wfBuf[2][NR_PART * PART_LEN1];  // filter fft
95    complex_t sde[PART_LEN1];  // cross-psd of nearend and error
96    complex_t sxd[PART_LEN1];  // cross-psd of farend and nearend
97    complex_t xfwBuf[NR_PART * PART_LEN1];  // farend windowed fft buffer
98
99    float sx[PART_LEN1], sd[PART_LEN1], se[PART_LEN1];  // far, near, error psd
100    float hNs[PART_LEN1];
101    float hNlFbMin, hNlFbLocalMin;
102    float hNlXdAvgMin;
103    int hNlNewMin, hNlMinCtr;
104    float overDrive, overDriveSm;
105    float targetSupp, minOverDrive;
106    float outBuf[PART_LEN];
107    int delayIdx;
108
109    short stNearState, echoState;
110    short divergeState;
111
112    int xfBufBlockPos;
113
114    void* far_buf;
115    void* far_buf_windowed;
116    int system_delay;  // Current system delay buffered in AEC.
117
118    int mult;  // sampling frequency multiple
119    int sampFreq;
120    WebRtc_UWord32 seed;
121
122    float mu;  // stepsize
123    float errThresh;  // error threshold
124
125    int noiseEstCtr;
126
127    power_level_t farlevel;
128    power_level_t nearlevel;
129    power_level_t linoutlevel;
130    power_level_t nlpoutlevel;
131
132    int metricsMode;
133    int stateCounter;
134    stats_t erl;
135    stats_t erle;
136    stats_t aNlp;
137    stats_t rerl;
138
139    // Quantities to control H band scaling for SWB input
140    int freq_avg_ic;  // initial bin for averaging nlp gain
141    int flag_Hband_cn;  // for comfort noise
142    float cn_scale_Hband;  // scale for comfort noise in H band
143
144    int delay_histogram[kHistorySizeBlocks];
145    int delay_logging_enabled;
146    void* delay_estimator_farend;
147    void* delay_estimator;
148
149#ifdef WEBRTC_AEC_DEBUG_DUMP
150    void* far_time_buf;
151    FILE *farFile;
152    FILE *nearFile;
153    FILE *outFile;
154    FILE *outLinearFile;
155#endif
156} aec_t;
157
158typedef void (*WebRtcAec_FilterFar_t)(aec_t *aec, float yf[2][PART_LEN1]);
159extern WebRtcAec_FilterFar_t WebRtcAec_FilterFar;
160typedef void (*WebRtcAec_ScaleErrorSignal_t)(aec_t *aec, float ef[2][PART_LEN1]);
161extern WebRtcAec_ScaleErrorSignal_t WebRtcAec_ScaleErrorSignal;
162typedef void (*WebRtcAec_FilterAdaptation_t)
163  (aec_t *aec, float *fft, float ef[2][PART_LEN1]);
164extern WebRtcAec_FilterAdaptation_t WebRtcAec_FilterAdaptation;
165typedef void (*WebRtcAec_OverdriveAndSuppress_t)
166  (aec_t *aec, float hNl[PART_LEN1], const float hNlFb, float efw[2][PART_LEN1]);
167extern WebRtcAec_OverdriveAndSuppress_t WebRtcAec_OverdriveAndSuppress;
168
169int WebRtcAec_CreateAec(aec_t **aec);
170int WebRtcAec_FreeAec(aec_t *aec);
171int WebRtcAec_InitAec(aec_t *aec, int sampFreq);
172void WebRtcAec_InitAec_SSE2(void);
173
174void WebRtcAec_InitMetrics(aec_t *aec);
175void WebRtcAec_BufferFarendPartition(aec_t *aec, const float* farend);
176void WebRtcAec_ProcessFrame(aec_t* aec,
177                            const short *nearend,
178                            const short *nearendH,
179                            int knownDelay);
180
181// A helper function to call WebRtc_MoveReadPtr() for all far-end buffers.
182// Returns the number of elements moved, and adjusts |system_delay| by the
183// corresponding amount in ms.
184int WebRtcAec_MoveFarReadPtr(aec_t* aec, int elements);
185// Calculates the median and standard deviation among the delay estimates
186// collected since the last call to this function.
187int WebRtcAec_GetDelayMetricsCore(aec_t* self, int* median, int* std);
188// Returns the echo state (1: echo, 0: no echo).
189int WebRtcAec_echo_state(aec_t* self);
190
191#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_MAIN_SOURCE_AEC_CORE_H_
192