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