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 NoiseSuppressionFixedC_ {
     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   size_t                  anaLen;
     38   size_t                  anaLen2;
     39   size_t                  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[NUM_HIGH_BANDS_MAX][ANAL_BLOCKL_MAX];
     97 
     98   int                     qNoise;
     99   int                     prevQNoise;
    100   int                     prevQMagn;
    101   size_t                  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 } NoiseSuppressionFixedC;
    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(NoiseSuppressionFixedC* 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(NoiseSuppressionFixedC* inst, int mode);
    150 
    151 /****************************************************************************
    152  * WebRtcNsx_ProcessCore
    153  *
    154  * Do noise suppression.
    155  *
    156  * Input:
    157  *      - inst          : Instance that should be initialized
    158  *      - inFrame       : Input speech frame for each band
    159  *      - num_bands     : Number of bands
    160  *
    161  * Output:
    162  *      - inst          : Updated instance
    163  *      - outFrame      : Output speech frame for each band
    164  */
    165 void WebRtcNsx_ProcessCore(NoiseSuppressionFixedC* inst,
    166                            const short* const* inFrame,
    167                            int num_bands,
    168                            short* const* outFrame);
    169 
    170 /****************************************************************************
    171  * Some function pointers, for internal functions shared by ARM NEON and
    172  * generic C code.
    173  */
    174 // Noise Estimation.
    175 typedef void (*NoiseEstimation)(NoiseSuppressionFixedC* inst,
    176                                 uint16_t* magn,
    177                                 uint32_t* noise,
    178                                 int16_t* q_noise);
    179 extern NoiseEstimation WebRtcNsx_NoiseEstimation;
    180 
    181 // Filter the data in the frequency domain, and create spectrum.
    182 typedef void (*PrepareSpectrum)(NoiseSuppressionFixedC* inst,
    183                                 int16_t* freq_buff);
    184 extern PrepareSpectrum WebRtcNsx_PrepareSpectrum;
    185 
    186 // For the noise supression process, synthesis, read out fully processed
    187 // segment, and update synthesis buffer.
    188 typedef void (*SynthesisUpdate)(NoiseSuppressionFixedC* inst,
    189                                 int16_t* out_frame,
    190                                 int16_t gain_factor);
    191 extern SynthesisUpdate WebRtcNsx_SynthesisUpdate;
    192 
    193 // Update analysis buffer for lower band, and window data before FFT.
    194 typedef void (*AnalysisUpdate)(NoiseSuppressionFixedC* inst,
    195                                int16_t* out,
    196                                int16_t* new_speech);
    197 extern AnalysisUpdate WebRtcNsx_AnalysisUpdate;
    198 
    199 // Denormalize the real-valued signal |in|, the output from inverse FFT.
    200 typedef void (*Denormalize)(NoiseSuppressionFixedC* inst,
    201                             int16_t* in,
    202                             int factor);
    203 extern Denormalize WebRtcNsx_Denormalize;
    204 
    205 // Normalize the real-valued signal |in|, the input to forward FFT.
    206 typedef void (*NormalizeRealBuffer)(NoiseSuppressionFixedC* inst,
    207                                     const int16_t* in,
    208                                     int16_t* out);
    209 extern NormalizeRealBuffer WebRtcNsx_NormalizeRealBuffer;
    210 
    211 // Compute speech/noise probability.
    212 // Intended to be private.
    213 void WebRtcNsx_SpeechNoiseProb(NoiseSuppressionFixedC* inst,
    214                                uint16_t* nonSpeechProbFinal,
    215                                uint32_t* priorLocSnr,
    216                                uint32_t* postLocSnr);
    217 
    218 #if (defined WEBRTC_DETECT_NEON || defined WEBRTC_HAS_NEON)
    219 // For the above function pointers, functions for generic platforms are declared
    220 // and defined as static in file nsx_core.c, while those for ARM Neon platforms
    221 // are declared below and defined in file nsx_core_neon.c.
    222 void WebRtcNsx_NoiseEstimationNeon(NoiseSuppressionFixedC* inst,
    223                                    uint16_t* magn,
    224                                    uint32_t* noise,
    225                                    int16_t* q_noise);
    226 void WebRtcNsx_SynthesisUpdateNeon(NoiseSuppressionFixedC* inst,
    227                                    int16_t* out_frame,
    228                                    int16_t gain_factor);
    229 void WebRtcNsx_AnalysisUpdateNeon(NoiseSuppressionFixedC* inst,
    230                                   int16_t* out,
    231                                   int16_t* new_speech);
    232 void WebRtcNsx_PrepareSpectrumNeon(NoiseSuppressionFixedC* inst,
    233                                    int16_t* freq_buff);
    234 #endif
    235 
    236 #if defined(MIPS32_LE)
    237 // For the above function pointers, functions for generic platforms are declared
    238 // and defined as static in file nsx_core.c, while those for MIPS platforms
    239 // are declared below and defined in file nsx_core_mips.c.
    240 void WebRtcNsx_SynthesisUpdate_mips(NoiseSuppressionFixedC* inst,
    241                                     int16_t* out_frame,
    242                                     int16_t gain_factor);
    243 void WebRtcNsx_AnalysisUpdate_mips(NoiseSuppressionFixedC* inst,
    244                                    int16_t* out,
    245                                    int16_t* new_speech);
    246 void WebRtcNsx_PrepareSpectrum_mips(NoiseSuppressionFixedC* inst,
    247                                     int16_t* freq_buff);
    248 void WebRtcNsx_NormalizeRealBuffer_mips(NoiseSuppressionFixedC* inst,
    249                                         const int16_t* in,
    250                                         int16_t* out);
    251 #if defined(MIPS_DSP_R1_LE)
    252 void WebRtcNsx_Denormalize_mips(NoiseSuppressionFixedC* inst,
    253                                 int16_t* in,
    254                                 int factor);
    255 #endif
    256 
    257 #endif
    258 
    259 #ifdef __cplusplus
    260 }
    261 #endif
    262 
    263 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
    264