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 MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ 13 14 #include <assert.h> 15 #include <memory> 16 17 #include "modules/audio_coding/neteq/audio_multi_vector.h" 18 #include "rtc_base/constructormagic.h" 19 #include "typedefs.h" // NOLINT(build/include) 20 21 namespace webrtc { 22 23 // Forward declarations. 24 class BackgroundNoise; 25 class RandomVector; 26 class StatisticsCalculator; 27 class SyncBuffer; 28 29 // This class handles extrapolation of audio data from the sync_buffer to 30 // produce packet-loss concealment. 31 // TODO(hlundin): Refactor this class to divide the long methods into shorter 32 // ones. 33 class Expand { 34 public: 35 Expand(BackgroundNoise* background_noise, 36 SyncBuffer* sync_buffer, 37 RandomVector* random_vector, 38 StatisticsCalculator* statistics, 39 int fs, 40 size_t num_channels); 41 42 virtual ~Expand(); 43 44 // Resets the object. 45 virtual void Reset(); 46 47 // The main method to produce concealment data. The data is appended to the 48 // end of |output|. 49 virtual int Process(AudioMultiVector* output); 50 51 // Prepare the object to do extra expansion during normal operation following 52 // a period of expands. 53 virtual void SetParametersForNormalAfterExpand(); 54 55 // Prepare the object to do extra expansion during merge operation following 56 // a period of expands. 57 virtual void SetParametersForMergeAfterExpand(); 58 59 // Returns the mute factor for |channel|. MuteFactor(size_t channel)60 int16_t MuteFactor(size_t channel) { 61 assert(channel < num_channels_); 62 return channel_parameters_[channel].mute_factor; 63 } 64 65 // Returns true if expansion has been faded down to zero amplitude (for all 66 // channels); false otherwise. 67 bool Muted() const; 68 69 // Accessors and mutators. 70 virtual size_t overlap_length() const; max_lag()71 size_t max_lag() const { return max_lag_; } 72 73 protected: 74 static const int kMaxConsecutiveExpands = 200; 75 void GenerateRandomVector(int16_t seed_increment, 76 size_t length, 77 int16_t* random_vector); 78 79 void GenerateBackgroundNoise(int16_t* random_vector, 80 size_t channel, 81 int mute_slope, 82 bool too_many_expands, 83 size_t num_noise_samples, 84 int16_t* buffer); 85 86 // Initializes member variables at the beginning of an expand period. 87 void InitializeForAnExpandPeriod(); 88 89 bool TooManyExpands(); 90 91 // Analyzes the signal history in |sync_buffer_|, and set up all parameters 92 // necessary to produce concealment data. 93 void AnalyzeSignal(int16_t* random_vector); 94 95 RandomVector* const random_vector_; 96 SyncBuffer* const sync_buffer_; 97 bool first_expand_; 98 const int fs_hz_; 99 const size_t num_channels_; 100 int consecutive_expands_; 101 102 private: 103 static const size_t kUnvoicedLpcOrder = 6; 104 static const size_t kNumCorrelationCandidates = 3; 105 static const size_t kDistortionLength = 20; 106 static const size_t kLpcAnalysisLength = 160; 107 static const size_t kMaxSampleRate = 48000; 108 static const int kNumLags = 3; 109 110 struct ChannelParameters { 111 ChannelParameters(); 112 int16_t mute_factor; 113 int16_t ar_filter[kUnvoicedLpcOrder + 1]; 114 int16_t ar_filter_state[kUnvoicedLpcOrder]; 115 int16_t ar_gain; 116 int16_t ar_gain_scale; 117 int16_t voice_mix_factor; /* Q14 */ 118 int16_t current_voice_mix_factor; /* Q14 */ 119 AudioVector expand_vector0; 120 AudioVector expand_vector1; 121 bool onset; 122 int mute_slope; /* Q20 */ 123 }; 124 125 // Calculate the auto-correlation of |input|, with length |input_length| 126 // samples. The correlation is calculated from a downsampled version of 127 // |input|, and is written to |output|. 128 void Correlation(const int16_t* input, 129 size_t input_length, 130 int16_t* output) const; 131 132 void UpdateLagIndex(); 133 134 BackgroundNoise* const background_noise_; 135 StatisticsCalculator* const statistics_; 136 const size_t overlap_length_; 137 size_t max_lag_; 138 size_t expand_lags_[kNumLags]; 139 int lag_index_direction_; 140 int current_lag_index_; 141 bool stop_muting_; 142 size_t expand_duration_samples_; 143 std::unique_ptr<ChannelParameters[]> channel_parameters_; 144 145 RTC_DISALLOW_COPY_AND_ASSIGN(Expand); 146 }; 147 148 struct ExpandFactory { ExpandFactoryExpandFactory149 ExpandFactory() {} ~ExpandFactoryExpandFactory150 virtual ~ExpandFactory() {} 151 152 virtual Expand* Create(BackgroundNoise* background_noise, 153 SyncBuffer* sync_buffer, 154 RandomVector* random_vector, 155 StatisticsCalculator* statistics, 156 int fs, 157 size_t num_channels) const; 158 }; 159 160 } // namespace webrtc 161 #endif // MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ 162