1 /*
2  *  Copyright (c) 2011 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 "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.
32 enum { kMaxDelayBlocks = 60 };
33 enum { kLookaheadBlocks = 15 };
34 enum { kHistorySizeBlocks = kMaxDelayBlocks + kLookaheadBlocks };
35 
36 typedef 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
46 enum {offsetLevel = -100};
47 
48 typedef 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 
58 typedef 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 
70 typedef 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 and 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;
147 
148 #ifdef WEBRTC_AEC_DEBUG_DUMP
149     void* far_time_buf;
150     FILE *farFile;
151     FILE *nearFile;
152     FILE *outFile;
153     FILE *outLinearFile;
154 #endif
155 } aec_t;
156 
157 typedef void (*WebRtcAec_FilterFar_t)(aec_t *aec, float yf[2][PART_LEN1]);
158 extern WebRtcAec_FilterFar_t WebRtcAec_FilterFar;
159 typedef void (*WebRtcAec_ScaleErrorSignal_t)(aec_t *aec, float ef[2][PART_LEN1]);
160 extern WebRtcAec_ScaleErrorSignal_t WebRtcAec_ScaleErrorSignal;
161 typedef void (*WebRtcAec_FilterAdaptation_t)
162   (aec_t *aec, float *fft, float ef[2][PART_LEN1]);
163 extern WebRtcAec_FilterAdaptation_t WebRtcAec_FilterAdaptation;
164 typedef void (*WebRtcAec_OverdriveAndSuppress_t)
165   (aec_t *aec, float hNl[PART_LEN1], const float hNlFb, float efw[2][PART_LEN1]);
166 extern WebRtcAec_OverdriveAndSuppress_t WebRtcAec_OverdriveAndSuppress;
167 
168 int WebRtcAec_CreateAec(aec_t **aec);
169 int WebRtcAec_FreeAec(aec_t *aec);
170 int WebRtcAec_InitAec(aec_t *aec, int sampFreq);
171 void WebRtcAec_InitAec_SSE2(void);
172 
173 void WebRtcAec_InitMetrics(aec_t *aec);
174 void WebRtcAec_BufferFarendPartition(aec_t *aec, const float* farend);
175 void WebRtcAec_ProcessFrame(aec_t* aec,
176                             const short *nearend,
177                             const short *nearendH,
178                             int knownDelay);
179 
180 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_MAIN_SOURCE_AEC_CORE_H_
181