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#include <stdio.h>
19#include "typedefs.h"
20#include "signal_processing_library.h"
21
22//#define G167 // for running G167 tests
23//#define UNCONSTR // time-unconstrained filter
24//#define AEC_DEBUG // for recording files
25
26#define FRAME_LEN 80
27#define PART_LEN 64 // Length of partition
28#define PART_LEN1 (PART_LEN + 1) // Unique fft coefficients
29#define PART_LEN2 (PART_LEN * 2) // Length of partition * 2
30#define NR_PART 12 // Number of partitions
31#define FILT_LEN (PART_LEN * NR_PART) // Filter length
32#define FILT_LEN2 (FILT_LEN * 2) // Double filter length
33#define FAR_BUF_LEN (FILT_LEN2 * 2)
34#define PREF_BAND_SIZE 24
35
36#define BLOCKL_MAX FRAME_LEN
37
38typedef float complex_t[2];
39// For performance reasons, some arrays of complex numbers are replaced by twice
40// as long arrays of float, all the real parts followed by all the imaginary
41// ones (complex_t[SIZE] -> float[2][SIZE]). This allows SIMD optimizations and
42// is better than two arrays (one for the real parts and one for the imaginary
43// parts) as this other way would require two pointers instead of one and cause
44// extra register spilling. This also allows the offsets to be calculated at
45// compile time.
46
47// Metrics
48enum {offsetLevel = -100};
49
50typedef struct {
51    float sfrsum;
52    int sfrcounter;
53    float framelevel;
54    float frsum;
55    int frcounter;
56    float minlevel;
57    float averagelevel;
58} power_level_t;
59
60typedef struct {
61    float instant;
62    float average;
63    float min;
64    float max;
65    float sum;
66    float hisum;
67    float himean;
68    int counter;
69    int hicounter;
70} stats_t;
71
72typedef struct {
73    int farBufWritePos, farBufReadPos;
74
75    int knownDelay;
76    int inSamples, outSamples;
77    int delayEstCtr;
78
79    void *farFrBuf, *nearFrBuf, *outFrBuf;
80
81    void *nearFrBufH;
82    void *outFrBufH;
83
84    float xBuf[PART_LEN2]; // farend
85    float dBuf[PART_LEN2]; // nearend
86    float eBuf[PART_LEN2]; // error
87
88    float dBufH[PART_LEN2]; // nearend
89
90    float xPow[PART_LEN1];
91    float dPow[PART_LEN1];
92    float dMinPow[PART_LEN1];
93    float dInitMinPow[PART_LEN1];
94    float *noisePow;
95#ifdef FFTW
96    float fftR[PART_LEN2];
97    fftw_complex fftC[PART_LEN2];
98    fftw_plan fftPlan, ifftPlan;
99
100    fftw_complex xfBuf[NR_PART * PART_LEN1];
101    fftw_complex wfBuf[NR_PART * PART_LEN1];
102    fftw_complex sde[PART_LEN1];
103#else
104    float xfBuf[2][NR_PART * PART_LEN1]; // farend fft buffer
105    float wfBuf[2][NR_PART * PART_LEN1]; // filter fft
106    complex_t sde[PART_LEN1]; // cross-psd of nearend and error
107    complex_t sxd[PART_LEN1]; // cross-psd of farend and nearend
108    complex_t xfwBuf[NR_PART * PART_LEN1]; // farend windowed fft buffer
109#endif
110    float sx[PART_LEN1], sd[PART_LEN1], se[PART_LEN1]; // far, near and error psd
111    float hNs[PART_LEN1];
112    float hNlFbMin, hNlFbLocalMin;
113    float hNlXdAvgMin;
114    int hNlNewMin, hNlMinCtr;
115    float overDrive, overDriveSm;
116    float targetSupp, minOverDrive;
117    float outBuf[PART_LEN];
118    int delayIdx;
119
120    short stNearState, echoState;
121    short divergeState;
122
123    int xfBufBlockPos;
124
125    short farBuf[FILT_LEN2 * 2];
126
127    short mult; // sampling frequency multiple
128    int sampFreq;
129    WebRtc_UWord32 seed;
130
131    float mu; // stepsize
132    float errThresh; // error threshold
133
134    int noiseEstCtr;
135
136    // Toggles for G.167 testing
137#ifdef G167
138    short adaptToggle;  // Filter adaptation
139    short nlpToggle;    // Nonlinear processing
140    short cnToggle;     // Comfort noise
141#endif
142
143    power_level_t farlevel;
144    power_level_t nearlevel;
145    power_level_t linoutlevel;
146    power_level_t nlpoutlevel;
147
148    int metricsMode;
149    int stateCounter;
150    stats_t erl;
151    stats_t erle;
152    stats_t aNlp;
153    stats_t rerl;
154
155    // Quantities to control H band scaling for SWB input
156    int freq_avg_ic;         //initial bin for averaging nlp gain
157    int flag_Hband_cn;      //for comfort noise
158    float cn_scale_Hband;   //scale for comfort noise in H band
159
160#ifdef AEC_DEBUG
161    FILE *farFile;
162    FILE *nearFile;
163    FILE *outFile;
164    FILE *outLpFile;
165#endif
166} aec_t;
167
168typedef void (*WebRtcAec_FilterFar_t)(aec_t *aec, float yf[2][PART_LEN1]);
169extern WebRtcAec_FilterFar_t WebRtcAec_FilterFar;
170typedef void (*WebRtcAec_ScaleErrorSignal_t)(aec_t *aec, float ef[2][PART_LEN1]);
171extern WebRtcAec_ScaleErrorSignal_t WebRtcAec_ScaleErrorSignal;
172#define IP_LEN PART_LEN // this must be at least ceil(2 + sqrt(PART_LEN))
173#define W_LEN PART_LEN
174typedef void (*WebRtcAec_FilterAdaptation_t)
175  (aec_t *aec, float *fft, float ef[2][PART_LEN1]);
176extern WebRtcAec_FilterAdaptation_t WebRtcAec_FilterAdaptation;
177typedef void (*WebRtcAec_OverdriveAndSuppress_t)
178  (aec_t *aec, float hNl[PART_LEN1], const float hNlFb, float efw[2][PART_LEN1]);
179extern WebRtcAec_OverdriveAndSuppress_t WebRtcAec_OverdriveAndSuppress;
180
181int WebRtcAec_CreateAec(aec_t **aec);
182int WebRtcAec_FreeAec(aec_t *aec);
183int WebRtcAec_InitAec(aec_t *aec, int sampFreq);
184void WebRtcAec_InitAec_SSE2(void);
185
186void WebRtcAec_InitMetrics(aec_t *aec);
187void WebRtcAec_ProcessFrame(aec_t *aec, const short *farend,
188                       const short *nearend, const short *nearendH,
189                       short *out, short *outH,
190                       int knownDelay);
191
192#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_MAIN_SOURCE_AEC_CORE_H_
193
194