| OLD | NEW | 
|    1 /* |    1 /* | 
|    2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |    2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 
|    3  * |    3  * | 
|    4  *  Use of this source code is governed by a BSD-style license |    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 |    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 |    6  *  tree. An additional intellectual property rights grant can be found | 
|    7  *  in the file PATENTS.  All contributing project authors may |    7  *  in the file PATENTS.  All contributing project authors may | 
|    8  *  be found in the AUTHORS file in the root of the source tree. |    8  *  be found in the AUTHORS file in the root of the source tree. | 
|    9  */ |    9  */ | 
|   10  |   10  | 
|   11 /* |   11 /* | 
|   12  * Specifies the interface for the AEC core. |   12  * Specifies the interface for the AEC core. | 
|   13  */ |   13  */ | 
|   14  |   14  | 
|   15 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_H_ |   15 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_H_ | 
|   16 #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_H_ |   16 #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_H_ | 
|   17  |   17  | 
|   18 #include <stddef.h> |   18 #include <stddef.h> | 
|   19  |   19  | 
 |   20 #include <memory> | 
 |   21  | 
 |   22 extern "C" { | 
 |   23 #include "webrtc/common_audio/ring_buffer.h" | 
 |   24 } | 
 |   25 #include "webrtc/base/constructormagic.h" | 
 |   26 #include "webrtc/common_audio/wav_file.h" | 
 |   27 #include "webrtc/modules/audio_processing/aec/aec_common.h" | 
 |   28 #include "webrtc/modules/audio_processing/utility/block_mean_calculator.h" | 
|   20 #include "webrtc/typedefs.h" |   29 #include "webrtc/typedefs.h" | 
|   21  |   30  | 
|   22 namespace webrtc { |   31 namespace webrtc { | 
|   23  |   32  | 
|   24 #define FRAME_LEN 80 |   33 #define FRAME_LEN 80 | 
|   25 #define PART_LEN 64               // Length of partition |   34 #define PART_LEN 64               // Length of partition | 
|   26 #define PART_LEN1 (PART_LEN + 1)  // Unique fft coefficients |   35 #define PART_LEN1 (PART_LEN + 1)  // Unique fft coefficients | 
|   27 #define PART_LEN2 (PART_LEN * 2)  // Length of partition * 2 |   36 #define PART_LEN2 (PART_LEN * 2)  // Length of partition * 2 | 
|   28 #define NUM_HIGH_BANDS_MAX 2      // Max number of high bands |   37 #define NUM_HIGH_BANDS_MAX 2      // Max number of high bands | 
|   29  |   38  | 
 |   39 class ApmDataDumper; | 
 |   40  | 
|   30 typedef float complex_t[2]; |   41 typedef float complex_t[2]; | 
|   31 // For performance reasons, some arrays of complex numbers are replaced by twice |   42 // For performance reasons, some arrays of complex numbers are replaced by twice | 
|   32 // as long arrays of float, all the real parts followed by all the imaginary |   43 // as long arrays of float, all the real parts followed by all the imaginary | 
|   33 // ones (complex_t[SIZE] -> float[2][SIZE]). This allows SIMD optimizations and |   44 // ones (complex_t[SIZE] -> float[2][SIZE]). This allows SIMD optimizations and | 
|   34 // is better than two arrays (one for the real parts and one for the imaginary |   45 // is better than two arrays (one for the real parts and one for the imaginary | 
|   35 // parts) as this other way would require two pointers instead of one and cause |   46 // parts) as this other way would require two pointers instead of one and cause | 
|   36 // extra register spilling. This also allows the offsets to be calculated at |   47 // extra register spilling. This also allows the offsets to be calculated at | 
|   37 // compile time. |   48 // compile time. | 
|   38  |   49  | 
|   39 // Metrics |   50 // Metrics | 
|   40 enum { kOffsetLevel = -100 }; |   51 enum { kOffsetLevel = -100 }; | 
|   41  |   52  | 
|   42 typedef struct Stats { |   53 typedef struct Stats { | 
|   43   float instant; |   54   float instant; | 
|   44   float average; |   55   float average; | 
|   45   float min; |   56   float min; | 
|   46   float max; |   57   float max; | 
|   47   float sum; |   58   float sum; | 
|   48   float hisum; |   59   float hisum; | 
|   49   float himean; |   60   float himean; | 
|   50   size_t counter; |   61   size_t counter; | 
|   51   size_t hicounter; |   62   size_t hicounter; | 
|   52 } Stats; |   63 } Stats; | 
|   53  |   64  | 
|   54 typedef struct AecCore AecCore; |   65 // Number of partitions for the extended filter mode. The first one is an enum | 
 |   66 // to be used in array declarations, as it represents the maximum filter length. | 
 |   67 enum { kExtendedNumPartitions = 32 }; | 
 |   68 static const int kNormalNumPartitions = 12; | 
 |   69  | 
 |   70 // Delay estimator constants, used for logging and delay compensation if | 
 |   71 // if reported delays are disabled. | 
 |   72 enum { kLookaheadBlocks = 15 }; | 
 |   73 enum { | 
 |   74   // 500 ms for 16 kHz which is equivalent with the limit of reported delays. | 
 |   75   kHistorySizeBlocks = 125 | 
 |   76 }; | 
 |   77  | 
 |   78 typedef struct PowerLevel { | 
 |   79   PowerLevel(); | 
 |   80  | 
 |   81   BlockMeanCalculator framelevel; | 
 |   82   BlockMeanCalculator averagelevel; | 
 |   83   float minlevel; | 
 |   84 } PowerLevel; | 
 |   85  | 
 |   86 class DivergentFilterFraction { | 
 |   87  public: | 
 |   88   DivergentFilterFraction(); | 
 |   89  | 
 |   90   // Reset. | 
 |   91   void Reset(); | 
 |   92  | 
 |   93   void AddObservation(const PowerLevel& nearlevel, | 
 |   94                       const PowerLevel& linoutlevel, | 
 |   95                       const PowerLevel& nlpoutlevel); | 
 |   96  | 
 |   97   // Return the latest fraction. | 
 |   98   float GetLatestFraction() const; | 
 |   99  | 
 |  100  private: | 
 |  101   // Clear all values added. | 
 |  102   void Clear(); | 
 |  103  | 
 |  104   size_t count_; | 
 |  105   size_t occurrence_; | 
 |  106   float fraction_; | 
 |  107  | 
 |  108   RTC_DISALLOW_COPY_AND_ASSIGN(DivergentFilterFraction); | 
 |  109 }; | 
 |  110  | 
 |  111 typedef struct CoherenceState { | 
 |  112   complex_t sde[PART_LEN1];  // cross-psd of nearend and error | 
 |  113   complex_t sxd[PART_LEN1];  // cross-psd of farend and nearend | 
 |  114   float sx[PART_LEN1], sd[PART_LEN1], se[PART_LEN1];  // far, near, error psd | 
 |  115 } CoherenceState; | 
 |  116  | 
 |  117 struct AecCore { | 
 |  118   explicit AecCore(int instance_index); | 
 |  119   ~AecCore(); | 
 |  120  | 
 |  121   std::unique_ptr<ApmDataDumper> data_dumper; | 
 |  122  | 
 |  123   CoherenceState coherence_state; | 
 |  124  | 
 |  125   int farBufWritePos, farBufReadPos; | 
 |  126  | 
 |  127   int knownDelay; | 
 |  128   int inSamples, outSamples; | 
 |  129   int delayEstCtr; | 
 |  130  | 
 |  131   RingBuffer* nearFrBuf; | 
 |  132   RingBuffer* outFrBuf; | 
 |  133  | 
 |  134   RingBuffer* nearFrBufH[NUM_HIGH_BANDS_MAX]; | 
 |  135   RingBuffer* outFrBufH[NUM_HIGH_BANDS_MAX]; | 
 |  136  | 
 |  137   float dBuf[PART_LEN2];  // nearend | 
 |  138   float eBuf[PART_LEN2];  // error | 
 |  139  | 
 |  140   float dBufH[NUM_HIGH_BANDS_MAX][PART_LEN2];  // nearend | 
 |  141  | 
 |  142   float xPow[PART_LEN1]; | 
 |  143   float dPow[PART_LEN1]; | 
 |  144   float dMinPow[PART_LEN1]; | 
 |  145   float dInitMinPow[PART_LEN1]; | 
 |  146   float* noisePow; | 
 |  147  | 
 |  148   float xfBuf[2][kExtendedNumPartitions * PART_LEN1];  // farend fft buffer | 
 |  149   float wfBuf[2][kExtendedNumPartitions * PART_LEN1];  // filter fft | 
 |  150   // Farend windowed fft buffer. | 
 |  151   complex_t xfwBuf[kExtendedNumPartitions * PART_LEN1]; | 
 |  152  | 
 |  153   float hNs[PART_LEN1]; | 
 |  154   float hNlFbMin, hNlFbLocalMin; | 
 |  155   float hNlXdAvgMin; | 
 |  156   int hNlNewMin, hNlMinCtr; | 
 |  157   float overDrive; | 
 |  158   float overdrive_scaling; | 
 |  159   int nlp_mode; | 
 |  160   float outBuf[PART_LEN]; | 
 |  161   int delayIdx; | 
 |  162  | 
 |  163   short stNearState, echoState; | 
 |  164   short divergeState; | 
 |  165  | 
 |  166   int xfBufBlockPos; | 
 |  167  | 
 |  168   RingBuffer* far_time_buf; | 
 |  169  | 
 |  170   int system_delay;  // Current system delay buffered in AEC. | 
 |  171  | 
 |  172   int mult;  // sampling frequency multiple | 
 |  173   int sampFreq = 16000; | 
 |  174   size_t num_bands; | 
 |  175   uint32_t seed; | 
 |  176  | 
 |  177   float filter_step_size;  // stepsize | 
 |  178   float error_threshold;   // error threshold | 
 |  179  | 
 |  180   int noiseEstCtr; | 
 |  181  | 
 |  182   PowerLevel farlevel; | 
 |  183   PowerLevel nearlevel; | 
 |  184   PowerLevel linoutlevel; | 
 |  185   PowerLevel nlpoutlevel; | 
 |  186  | 
 |  187   int metricsMode; | 
 |  188   int stateCounter; | 
 |  189   Stats erl; | 
 |  190   Stats erle; | 
 |  191   Stats aNlp; | 
 |  192   Stats rerl; | 
 |  193   DivergentFilterFraction divergent_filter_fraction; | 
 |  194  | 
 |  195   // Quantities to control H band scaling for SWB input | 
 |  196   int freq_avg_ic;       // initial bin for averaging nlp gain | 
 |  197   int flag_Hband_cn;     // for comfort noise | 
 |  198   float cn_scale_Hband;  // scale for comfort noise in H band | 
 |  199  | 
 |  200   int delay_metrics_delivered; | 
 |  201   int delay_histogram[kHistorySizeBlocks]; | 
 |  202   int num_delay_values; | 
 |  203   int delay_median; | 
 |  204   int delay_std; | 
 |  205   float fraction_poor_delays; | 
 |  206   int delay_logging_enabled; | 
 |  207   void* delay_estimator_farend; | 
 |  208   void* delay_estimator; | 
 |  209   // Variables associated with delay correction through signal based delay | 
 |  210   // estimation feedback. | 
 |  211   int signal_delay_correction; | 
 |  212   int previous_delay; | 
 |  213   int delay_correction_count; | 
 |  214   int shift_offset; | 
 |  215   float delay_quality_threshold; | 
 |  216   int frame_count; | 
 |  217  | 
 |  218   // 0 = delay agnostic mode (signal based delay correction) disabled. | 
 |  219   // Otherwise enabled. | 
 |  220   int delay_agnostic_enabled; | 
 |  221   // 1 = extended filter mode enabled, 0 = disabled. | 
 |  222   int extended_filter_enabled; | 
 |  223   // 1 = next generation aec mode enabled, 0 = disabled. | 
 |  224   int aec3_enabled; | 
 |  225   bool refined_adaptive_filter_enabled; | 
 |  226  | 
 |  227   // Runtime selection of number of filter partitions. | 
 |  228   int num_partitions; | 
 |  229  | 
 |  230   // Flag that extreme filter divergence has been detected by the Echo | 
 |  231   // Suppressor. | 
 |  232   int extreme_filter_divergence; | 
 |  233 }; | 
|   55  |  234  | 
|   56 AecCore* WebRtcAec_CreateAec(int instance_count);  // Returns NULL on error. |  235 AecCore* WebRtcAec_CreateAec(int instance_count);  // Returns NULL on error. | 
|   57 void WebRtcAec_FreeAec(AecCore* aec); |  236 void WebRtcAec_FreeAec(AecCore* aec); | 
|   58 int WebRtcAec_InitAec(AecCore* aec, int sampFreq); |  237 int WebRtcAec_InitAec(AecCore* aec, int sampFreq); | 
|   59 void WebRtcAec_InitAec_SSE2(void); |  238 void WebRtcAec_InitAec_SSE2(void); | 
|   60 #if defined(MIPS_FPU_LE) |  239 #if defined(MIPS_FPU_LE) | 
|   61 void WebRtcAec_InitAec_mips(void); |  240 void WebRtcAec_InitAec_mips(void); | 
|   62 #endif |  241 #endif | 
|   63 #if defined(WEBRTC_DETECT_NEON) || defined(WEBRTC_HAS_NEON) |  242 #if defined(WEBRTC_DETECT_NEON) || defined(WEBRTC_HAS_NEON) | 
|   64 void WebRtcAec_InitAec_neon(void); |  243 void WebRtcAec_InitAec_neon(void); | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  134 int WebRtcAec_system_delay(AecCore* self); |  313 int WebRtcAec_system_delay(AecCore* self); | 
|  135  |  314  | 
|  136 // Sets the |system_delay| to |value|.  Note that if the value is changed |  315 // Sets the |system_delay| to |value|.  Note that if the value is changed | 
|  137 // improperly, there can be a performance regression.  So it should be used with |  316 // improperly, there can be a performance regression.  So it should be used with | 
|  138 // care. |  317 // care. | 
|  139 void WebRtcAec_SetSystemDelay(AecCore* self, int delay); |  318 void WebRtcAec_SetSystemDelay(AecCore* self, int delay); | 
|  140  |  319  | 
|  141 }  // namespace webrtc |  320 }  // namespace webrtc | 
|  142  |  321  | 
|  143 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_H_ |  322 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_H_ | 
| OLD | NEW |