Home | History | Annotate | Download | only in ns
      1 /*
      2  *  Copyright (c) 2012 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 #ifdef NS_FILEDEBUG
     15 #include <stdio.h>
     16 #endif
     17 
     18 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
     19 #include "webrtc/modules/audio_processing/ns/nsx_defines.h"
     20 #include "webrtc/typedefs.h"
     21 
     22 typedef struct NsxInst_t_ {
     23   uint32_t                fs;
     24 
     25   const int16_t*          window;
     26   int16_t                 analysisBuffer[ANAL_BLOCKL_MAX];
     27   int16_t                 synthesisBuffer[ANAL_BLOCKL_MAX];
     28   uint16_t                noiseSupFilter[HALF_ANAL_BLOCKL];
     29   uint16_t                overdrive; /* Q8 */
     30   uint16_t                denoiseBound; /* Q14 */
     31   const int16_t*          factor2Table;
     32   int16_t                 noiseEstLogQuantile[SIMULT* HALF_ANAL_BLOCKL];
     33   int16_t                 noiseEstDensity[SIMULT* HALF_ANAL_BLOCKL];
     34   int16_t                 noiseEstCounter[SIMULT];
     35   int16_t                 noiseEstQuantile[HALF_ANAL_BLOCKL];
     36 
     37   int                     anaLen;
     38   int                     anaLen2;
     39   int                     magnLen;
     40   int                     aggrMode;
     41   int                     stages;
     42   int                     initFlag;
     43   int                     gainMap;
     44 
     45   int32_t                 maxLrt;
     46   int32_t                 minLrt;
     47   // Log LRT factor with time-smoothing in Q8.
     48   int32_t                 logLrtTimeAvgW32[HALF_ANAL_BLOCKL];
     49   int32_t                 featureLogLrt;
     50   int32_t                 thresholdLogLrt;
     51   int16_t                 weightLogLrt;
     52 
     53   uint32_t                featureSpecDiff;
     54   uint32_t                thresholdSpecDiff;
     55   int16_t                 weightSpecDiff;
     56 
     57   uint32_t                featureSpecFlat;
     58   uint32_t                thresholdSpecFlat;
     59   int16_t                 weightSpecFlat;
     60 
     61   // Conservative estimate of noise spectrum.
     62   int32_t                 avgMagnPause[HALF_ANAL_BLOCKL];
     63   uint32_t                magnEnergy;
     64   uint32_t                sumMagn;
     65   uint32_t                curAvgMagnEnergy;
     66   uint32_t                timeAvgMagnEnergy;
     67   uint32_t                timeAvgMagnEnergyTmp;
     68 
     69   uint32_t                whiteNoiseLevel;  // Initial noise estimate.
     70   // Initial magnitude spectrum estimate.
     71   uint32_t                initMagnEst[HALF_ANAL_BLOCKL];
     72   // Pink noise parameters:
     73   int32_t                 pinkNoiseNumerator;  // Numerator.
     74   int32_t                 pinkNoiseExp;  // Power of freq.
     75   int                     minNorm;  // Smallest normalization factor.
     76   int                     zeroInputSignal;  // Zero input signal flag.
     77 
     78   // Noise spectrum from previous frame.
     79   uint32_t                prevNoiseU32[HALF_ANAL_BLOCKL];
     80   // Magnitude spectrum from previous frame.
     81   uint16_t                prevMagnU16[HALF_ANAL_BLOCKL];
     82   // Prior speech/noise probability in Q14.
     83   int16_t                 priorNonSpeechProb;
     84 
     85   int                     blockIndex;  // Frame index counter.
     86   // Parameter for updating or estimating thresholds/weights for prior model.
     87   int                     modelUpdate;
     88   int                     cntThresUpdate;
     89 
     90   // Histograms for parameter estimation.
     91   int16_t                 histLrt[HIST_PAR_EST];
     92   int16_t                 histSpecFlat[HIST_PAR_EST];
     93   int16_t                 histSpecDiff[HIST_PAR_EST];
     94 
     95   // Quantities for high band estimate.
     96   int16_t                 dataBufHBFX[ANAL_BLOCKL_MAX];  // Q0
     97 
     98   int                     qNoise;
     99   int                     prevQNoise;
    100   int                     prevQMagn;
    101   int                     blockLen10ms;
    102 
    103   int16_t                 real[ANAL_BLOCKL_MAX];
    104   int16_t                 imag[ANAL_BLOCKL_MAX];
    105   int32_t                 energyIn;
    106   int                     scaleEnergyIn;
    107   int                     normData;
    108 
    109   struct RealFFT* real_fft;
    110 } NsxInst_t;
    111 
    112 #ifdef __cplusplus
    113 extern "C"
    114 {
    115 #endif
    116 
    117 /****************************************************************************
    118  * WebRtcNsx_InitCore(...)
    119  *
    120  * This function initializes a noise suppression instance
    121  *
    122  * Input:
    123  *      - inst          : Instance that should be initialized
    124  *      - fs            : Sampling frequency
    125  *
    126  * Output:
    127  *      - inst          : Initialized instance
    128  *
    129  * Return value         :  0 - Ok
    130  *                        -1 - Error
    131  */
    132 int32_t WebRtcNsx_InitCore(NsxInst_t* inst, uint32_t fs);
    133 
    134 /****************************************************************************
    135  * WebRtcNsx_set_policy_core(...)
    136  *
    137  * This changes the aggressiveness of the noise suppression method.
    138  *
    139  * Input:
    140  *      - inst       : Instance that should be initialized
    141  *      - mode       : 0: Mild (6 dB), 1: Medium (10 dB), 2: Aggressive (15 dB)
    142  *
    143  * Output:
    144  *      - inst       : Initialized instance
    145  *
    146  * Return value      :  0 - Ok
    147  *                     -1 - Error
    148  */
    149 int WebRtcNsx_set_policy_core(NsxInst_t* inst, int mode);
    150 
    151 /****************************************************************************
    152  * WebRtcNsx_ProcessCore
    153  *
    154  * Do noise suppression.
    155  *
    156  * Input:
    157  *      - inst          : Instance that should be initialized
    158  *      - inFrameLow    : Input speech frame for lower band
    159  *      - inFrameHigh   : Input speech frame for higher band
    160  *
    161  * Output:
    162  *      - inst          : Updated instance
    163  *      - outFrameLow   : Output speech frame for lower band
    164  *      - outFrameHigh  : Output speech frame for higher band
    165  *
    166  * Return value         :  0 - OK
    167  *                        -1 - Error
    168  */
    169 int WebRtcNsx_ProcessCore(NsxInst_t* inst,
    170                           short* inFrameLow,
    171                           short* inFrameHigh,
    172                           short* outFrameLow,
    173                           short* outFrameHigh);
    174 
    175 /****************************************************************************
    176  * Some function pointers, for internal functions shared by ARM NEON and
    177  * generic C code.
    178  */
    179 // Noise Estimation.
    180 typedef void (*NoiseEstimation)(NsxInst_t* inst,
    181                                 uint16_t* magn,
    182                                 uint32_t* noise,
    183                                 int16_t* q_noise);
    184 extern NoiseEstimation WebRtcNsx_NoiseEstimation;
    185 
    186 // Filter the data in the frequency domain, and create spectrum.
    187 typedef void (*PrepareSpectrum)(NsxInst_t* inst,
    188                                 int16_t* freq_buff);
    189 extern PrepareSpectrum WebRtcNsx_PrepareSpectrum;
    190 
    191 // For the noise supression process, synthesis, read out fully processed
    192 // segment, and update synthesis buffer.
    193 typedef void (*SynthesisUpdate)(NsxInst_t* inst,
    194                                 int16_t* out_frame,
    195                                 int16_t gain_factor);
    196 extern SynthesisUpdate WebRtcNsx_SynthesisUpdate;
    197 
    198 // Update analysis buffer for lower band, and window data before FFT.
    199 typedef void (*AnalysisUpdate)(NsxInst_t* inst,
    200                                int16_t* out,
    201                                int16_t* new_speech);
    202 extern AnalysisUpdate WebRtcNsx_AnalysisUpdate;
    203 
    204 // Denormalize the real-valued signal |in|, the output from inverse FFT.
    205 typedef void (*Denormalize) (NsxInst_t* inst, int16_t* in, int factor);
    206 extern Denormalize WebRtcNsx_Denormalize;
    207 
    208 // Normalize the real-valued signal |in|, the input to forward FFT.
    209 typedef void (*NormalizeRealBuffer) (NsxInst_t* inst,
    210                                      const int16_t* in,
    211                                      int16_t* out);
    212 extern NormalizeRealBuffer WebRtcNsx_NormalizeRealBuffer;
    213 
    214 // Compute speech/noise probability.
    215 // Intended to be private.
    216 void WebRtcNsx_SpeechNoiseProb(NsxInst_t* inst,
    217                                uint16_t* nonSpeechProbFinal,
    218                                uint32_t* priorLocSnr,
    219                                uint32_t* postLocSnr);
    220 
    221 #if (defined WEBRTC_DETECT_ARM_NEON) || defined (WEBRTC_ARCH_ARM_NEON)
    222 // For the above function pointers, functions for generic platforms are declared
    223 // and defined as static in file nsx_core.c, while those for ARM Neon platforms
    224 // are declared below and defined in file nsx_core_neon.S.
    225 void WebRtcNsx_NoiseEstimationNeon(NsxInst_t* inst,
    226                                    uint16_t* magn,
    227                                    uint32_t* noise,
    228                                    int16_t* q_noise);
    229 void WebRtcNsx_SynthesisUpdateNeon(NsxInst_t* inst,
    230                                    int16_t* out_frame,
    231                                    int16_t gain_factor);
    232 void WebRtcNsx_AnalysisUpdateNeon(NsxInst_t* inst,
    233                                   int16_t* out,
    234                                   int16_t* new_speech);
    235 void WebRtcNsx_PrepareSpectrumNeon(NsxInst_t* inst, int16_t* freq_buff);
    236 #endif
    237 
    238 #if defined(MIPS32_LE)
    239 // For the above function pointers, functions for generic platforms are declared
    240 // and defined as static in file nsx_core.c, while those for MIPS platforms
    241 // are declared below and defined in file nsx_core_mips.c.
    242 void WebRtcNsx_SynthesisUpdate_mips(NsxInst_t* inst,
    243                                     int16_t* out_frame,
    244                                     int16_t gain_factor);
    245 void WebRtcNsx_AnalysisUpdate_mips(NsxInst_t* inst,
    246                                    int16_t* out,
    247                                    int16_t* new_speech);
    248 void WebRtcNsx_PrepareSpectrum_mips(NsxInst_t* inst, int16_t* freq_buff);
    249 void WebRtcNsx_NormalizeRealBuffer_mips(NsxInst_t* inst,
    250                                         const int16_t* in,
    251                                         int16_t* out);
    252 #if defined(MIPS_DSP_R1_LE)
    253 void WebRtcNsx_Denormalize_mips(NsxInst_t* inst, int16_t* in, int factor);
    254 #endif
    255 
    256 #endif
    257 
    258 #ifdef __cplusplus
    259 }
    260 #endif
    261 
    262 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
    263