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