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#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
13
14#include "typedefs.h"
15#include "signal_processing_library.h"
16
17#include "nsx_defines.h"
18
19#ifdef NS_FILEDEBUG
20#include <stdio.h>
21#endif
22
23typedef struct NsxInst_t_ {
24  WebRtc_UWord32          fs;
25
26  const WebRtc_Word16*    window;
27  WebRtc_Word16           analysisBuffer[ANAL_BLOCKL_MAX];
28  WebRtc_Word16           synthesisBuffer[ANAL_BLOCKL_MAX];
29  WebRtc_UWord16          noiseSupFilter[HALF_ANAL_BLOCKL];
30  WebRtc_UWord16          overdrive; /* Q8 */
31  WebRtc_UWord16          denoiseBound; /* Q14 */
32  const WebRtc_Word16*    factor2Table;
33  WebRtc_Word16           noiseEstLogQuantile[SIMULT* HALF_ANAL_BLOCKL];
34  WebRtc_Word16           noiseEstDensity[SIMULT* HALF_ANAL_BLOCKL];
35  WebRtc_Word16           noiseEstCounter[SIMULT];
36  WebRtc_Word16           noiseEstQuantile[HALF_ANAL_BLOCKL];
37
38  WebRtc_Word16           anaLen;
39  int                     anaLen2;
40  int                     magnLen;
41  int                     aggrMode;
42  int                     stages;
43  int                     initFlag;
44  int                     gainMap;
45
46  WebRtc_Word32           maxLrt;
47  WebRtc_Word32           minLrt;
48  WebRtc_Word32           logLrtTimeAvgW32[HALF_ANAL_BLOCKL]; //log lrt factor with time-smoothing in Q8
49  WebRtc_Word32           featureLogLrt;
50  WebRtc_Word32           thresholdLogLrt;
51  WebRtc_Word16           weightLogLrt;
52
53  WebRtc_UWord32          featureSpecDiff;
54  WebRtc_UWord32          thresholdSpecDiff;
55  WebRtc_Word16           weightSpecDiff;
56
57  WebRtc_UWord32          featureSpecFlat;
58  WebRtc_UWord32          thresholdSpecFlat;
59  WebRtc_Word16           weightSpecFlat;
60
61  WebRtc_Word32           avgMagnPause[HALF_ANAL_BLOCKL]; //conservative estimate of noise spectrum
62  WebRtc_UWord32          magnEnergy;
63  WebRtc_UWord32          sumMagn;
64  WebRtc_UWord32          curAvgMagnEnergy;
65  WebRtc_UWord32          timeAvgMagnEnergy;
66  WebRtc_UWord32          timeAvgMagnEnergyTmp;
67
68  WebRtc_UWord32          whiteNoiseLevel;              //initial noise estimate
69  WebRtc_UWord32          initMagnEst[HALF_ANAL_BLOCKL];//initial magnitude spectrum estimate
70  WebRtc_Word32           pinkNoiseNumerator;           //pink noise parameter: numerator
71  WebRtc_Word32           pinkNoiseExp;                 //pink noise parameter: power of freq
72  int                     minNorm;                      //smallest normalization factor
73  int                     zeroInputSignal;              //zero input signal flag
74
75  WebRtc_UWord32          prevNoiseU32[HALF_ANAL_BLOCKL]; //noise spectrum from previous frame
76  WebRtc_UWord16          prevMagnU16[HALF_ANAL_BLOCKL]; //magnitude spectrum from previous frame
77  WebRtc_Word16           priorNonSpeechProb; //prior speech/noise probability // Q14
78
79  int                     blockIndex; //frame index counter
80  int                     modelUpdate; //parameter for updating or estimating thresholds/weights for prior model
81  int                     cntThresUpdate;
82
83  //histograms for parameter estimation
84  WebRtc_Word16           histLrt[HIST_PAR_EST];
85  WebRtc_Word16           histSpecFlat[HIST_PAR_EST];
86  WebRtc_Word16           histSpecDiff[HIST_PAR_EST];
87
88  //quantities for high band estimate
89  WebRtc_Word16           dataBufHBFX[ANAL_BLOCKL_MAX]; /* Q0 */
90
91  int                     qNoise;
92  int                     prevQNoise;
93  int                     prevQMagn;
94  int                     blockLen10ms;
95
96  WebRtc_Word16           real[ANAL_BLOCKL_MAX];
97  WebRtc_Word16           imag[ANAL_BLOCKL_MAX];
98  WebRtc_Word32           energyIn;
99  int                     scaleEnergyIn;
100  int                     normData;
101
102} NsxInst_t;
103
104#ifdef __cplusplus
105extern "C"
106{
107#endif
108
109/****************************************************************************
110 * WebRtcNsx_InitCore(...)
111 *
112 * This function initializes a noise suppression instance
113 *
114 * Input:
115 *      - inst          : Instance that should be initialized
116 *      - fs            : Sampling frequency
117 *
118 * Output:
119 *      - inst          : Initialized instance
120 *
121 * Return value         :  0 - Ok
122 *                        -1 - Error
123 */
124WebRtc_Word32 WebRtcNsx_InitCore(NsxInst_t* inst, WebRtc_UWord32 fs);
125
126/****************************************************************************
127 * WebRtcNsx_set_policy_core(...)
128 *
129 * This changes the aggressiveness of the noise suppression method.
130 *
131 * Input:
132 *      - inst       : Instance that should be initialized
133 *      - mode       : 0: Mild (6 dB), 1: Medium (10 dB), 2: Aggressive (15 dB)
134 *
135 * Output:
136 *      - inst       : Initialized instance
137 *
138 * Return value      :  0 - Ok
139 *                     -1 - Error
140 */
141int WebRtcNsx_set_policy_core(NsxInst_t* inst, int mode);
142
143/****************************************************************************
144 * WebRtcNsx_ProcessCore
145 *
146 * Do noise suppression.
147 *
148 * Input:
149 *      - inst          : Instance that should be initialized
150 *      - inFrameLow    : Input speech frame for lower band
151 *      - inFrameHigh   : Input speech frame for higher band
152 *
153 * Output:
154 *      - inst          : Updated instance
155 *      - outFrameLow   : Output speech frame for lower band
156 *      - outFrameHigh  : Output speech frame for higher band
157 *
158 * Return value         :  0 - OK
159 *                        -1 - Error
160 */
161int WebRtcNsx_ProcessCore(NsxInst_t* inst,
162                          short* inFrameLow,
163                          short* inFrameHigh,
164                          short* outFrameLow,
165                          short* outFrameHigh);
166
167/****************************************************************************
168 * Some function pointers, for internal functions shared by ARM NEON and
169 * generic C code.
170 */
171// Noise Estimation.
172typedef void (*NoiseEstimation)(NsxInst_t* inst,
173                                uint16_t* magn,
174                                uint32_t* noise,
175                                int16_t* q_noise);
176extern NoiseEstimation WebRtcNsx_NoiseEstimation;
177
178// Filter the data in the frequency domain, and create spectrum.
179typedef void (*PrepareSpectrum)(NsxInst_t* inst,
180                                int16_t* freq_buff);
181extern PrepareSpectrum WebRtcNsx_PrepareSpectrum;
182
183// For the noise supression process, synthesis, read out fully processed
184// segment, and update synthesis buffer.
185typedef void (*SynthesisUpdate)(NsxInst_t* inst,
186                                int16_t* out_frame,
187                                int16_t gain_factor);
188extern SynthesisUpdate WebRtcNsx_SynthesisUpdate;
189
190// Update analysis buffer for lower band, and window data before FFT.
191typedef void (*AnalysisUpdate)(NsxInst_t* inst,
192                               int16_t* out,
193                               int16_t* new_speech);
194extern AnalysisUpdate WebRtcNsx_AnalysisUpdate;
195
196// Denormalize the input buffer.
197typedef void (*Denormalize)(NsxInst_t* inst,
198                            int16_t* in,
199                            int factor);
200extern Denormalize WebRtcNsx_Denormalize;
201
202// Create a complex number buffer, as the intput interleaved with zeros,
203// and normalize it.
204typedef void (*CreateComplexBuffer)(NsxInst_t* inst,
205                                    int16_t* in,
206                                    int16_t* out);
207extern CreateComplexBuffer WebRtcNsx_CreateComplexBuffer;
208
209/****************************************************************************
210 * Initialization of the above function pointers for ARM Neon.
211 */
212void WebRtcNsx_InitNeon(void);
213
214extern const WebRtc_Word16 WebRtcNsx_kLogTable[9];
215extern const WebRtc_Word16 WebRtcNsx_kLogTableFrac[256];
216extern const WebRtc_Word16 WebRtcNsx_kCounterDiv[201];
217
218#ifdef __cplusplus
219}
220#endif
221
222#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
223