1/*
2 *  Copyright (c) 2013 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#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_
13
14#include "webrtc/common_audio/wav_writer.h"
15#include "webrtc/modules/audio_processing/aec/aec_common.h"
16#include "webrtc/modules/audio_processing/aec/aec_core.h"
17#include "webrtc/modules/audio_processing/utility/ring_buffer.h"
18#include "webrtc/typedefs.h"
19
20// Number of partitions for the extended filter mode. The first one is an enum
21// to be used in array declarations, as it represents the maximum filter length.
22enum {
23  kExtendedNumPartitions = 32
24};
25static const int kNormalNumPartitions = 12;
26
27// Delay estimator constants, used for logging.
28enum {
29  kMaxDelayBlocks = 60
30};
31enum {
32  kLookaheadBlocks = 15
33};
34enum {
35  kHistorySizeBlocks = kMaxDelayBlocks + kLookaheadBlocks
36};
37
38// Extended filter adaptation parameters.
39// TODO(ajm): No narrowband tuning yet.
40static const float kExtendedMu = 0.4f;
41static const float kExtendedErrorThreshold = 1.0e-6f;
42
43typedef struct PowerLevel {
44  float sfrsum;
45  int sfrcounter;
46  float framelevel;
47  float frsum;
48  int frcounter;
49  float minlevel;
50  float averagelevel;
51} PowerLevel;
52
53struct AecCore {
54  int farBufWritePos, farBufReadPos;
55
56  int knownDelay;
57  int inSamples, outSamples;
58  int delayEstCtr;
59
60  RingBuffer* nearFrBuf;
61  RingBuffer* outFrBuf;
62
63  RingBuffer* nearFrBufH;
64  RingBuffer* outFrBufH;
65
66  float dBuf[PART_LEN2];  // nearend
67  float eBuf[PART_LEN2];  // error
68
69  float dBufH[PART_LEN2];  // nearend
70
71  float xPow[PART_LEN1];
72  float dPow[PART_LEN1];
73  float dMinPow[PART_LEN1];
74  float dInitMinPow[PART_LEN1];
75  float* noisePow;
76
77  float xfBuf[2][kExtendedNumPartitions * PART_LEN1];  // farend fft buffer
78  float wfBuf[2][kExtendedNumPartitions * PART_LEN1];  // filter fft
79  complex_t sde[PART_LEN1];  // cross-psd of nearend and error
80  complex_t sxd[PART_LEN1];  // cross-psd of farend and nearend
81  // Farend windowed fft buffer.
82  complex_t xfwBuf[kExtendedNumPartitions * PART_LEN1];
83
84  float sx[PART_LEN1], sd[PART_LEN1], se[PART_LEN1];  // far, near, error psd
85  float hNs[PART_LEN1];
86  float hNlFbMin, hNlFbLocalMin;
87  float hNlXdAvgMin;
88  int hNlNewMin, hNlMinCtr;
89  float overDrive, overDriveSm;
90  int nlp_mode;
91  float outBuf[PART_LEN];
92  int delayIdx;
93
94  short stNearState, echoState;
95  short divergeState;
96
97  int xfBufBlockPos;
98
99  RingBuffer* far_buf;
100  RingBuffer* far_buf_windowed;
101  int system_delay;  // Current system delay buffered in AEC.
102
103  int mult;  // sampling frequency multiple
104  int sampFreq;
105  uint32_t seed;
106
107  float normal_mu;               // stepsize
108  float normal_error_threshold;  // error threshold
109
110  int noiseEstCtr;
111
112  PowerLevel farlevel;
113  PowerLevel nearlevel;
114  PowerLevel linoutlevel;
115  PowerLevel nlpoutlevel;
116
117  int metricsMode;
118  int stateCounter;
119  Stats erl;
120  Stats erle;
121  Stats aNlp;
122  Stats rerl;
123
124  // Quantities to control H band scaling for SWB input
125  int freq_avg_ic;       // initial bin for averaging nlp gain
126  int flag_Hband_cn;     // for comfort noise
127  float cn_scale_Hband;  // scale for comfort noise in H band
128
129  int delay_histogram[kHistorySizeBlocks];
130  int delay_logging_enabled;
131  void* delay_estimator_farend;
132  void* delay_estimator;
133
134  int reported_delay_enabled;  // 0 = disabled, otherwise enabled.
135  // 1 = extended filter mode enabled, 0 = disabled.
136  int extended_filter_enabled;
137  // Runtime selection of number of filter partitions.
138  int num_partitions;
139
140#ifdef WEBRTC_AEC_DEBUG_DUMP
141  // Sequence number of this AEC instance, so that different instances can
142  // choose different dump file names.
143  int instance_index;
144
145  // Number of times we've restarted dumping; used to pick new dump file names
146  // each time.
147  int debug_dump_count;
148
149  RingBuffer* far_time_buf;
150  rtc_WavFile* farFile;
151  rtc_WavFile* nearFile;
152  rtc_WavFile* outFile;
153  rtc_WavFile* outLinearFile;
154#endif
155};
156
157typedef void (*WebRtcAec_FilterFar_t)(AecCore* aec, float yf[2][PART_LEN1]);
158extern WebRtcAec_FilterFar_t WebRtcAec_FilterFar;
159typedef void (*WebRtcAec_ScaleErrorSignal_t)(AecCore* aec,
160                                             float ef[2][PART_LEN1]);
161extern WebRtcAec_ScaleErrorSignal_t WebRtcAec_ScaleErrorSignal;
162typedef void (*WebRtcAec_FilterAdaptation_t)(AecCore* aec,
163                                             float* fft,
164                                             float ef[2][PART_LEN1]);
165extern WebRtcAec_FilterAdaptation_t WebRtcAec_FilterAdaptation;
166typedef void (*WebRtcAec_OverdriveAndSuppress_t)(AecCore* aec,
167                                                 float hNl[PART_LEN1],
168                                                 const float hNlFb,
169                                                 float efw[2][PART_LEN1]);
170extern WebRtcAec_OverdriveAndSuppress_t WebRtcAec_OverdriveAndSuppress;
171
172typedef void (*WebRtcAec_ComfortNoise_t)(AecCore* aec,
173                                         float efw[2][PART_LEN1],
174                                         complex_t* comfortNoiseHband,
175                                         const float* noisePow,
176                                         const float* lambda);
177extern WebRtcAec_ComfortNoise_t WebRtcAec_ComfortNoise;
178
179typedef void (*WebRtcAec_SubbandCoherence_t)(AecCore* aec,
180                                             float efw[2][PART_LEN1],
181                                             float xfw[2][PART_LEN1],
182                                             float* fft,
183                                             float* cohde,
184                                             float* cohxd);
185extern WebRtcAec_SubbandCoherence_t WebRtcAec_SubbandCoherence;
186
187#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_
188