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