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