henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <assert.h> |
| 15 | |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 16 | #include "webrtc/base/constructormagic.h" |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 17 | #include "webrtc/base/scoped_ptr.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | // Forward declarations. |
| 24 | class BackgroundNoise; |
| 25 | class RandomVector; |
| 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 | int fs, |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 38 | size_t num_channels); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 39 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 40 | virtual ~Expand(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 41 | |
| 42 | // Resets the object. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 43 | virtual void Reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 44 | |
| 45 | // The main method to produce concealment data. The data is appended to the |
| 46 | // end of |output|. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 47 | virtual int Process(AudioMultiVector* output); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | |
| 49 | // Prepare the object to do extra expansion during normal operation following |
| 50 | // a period of expands. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 51 | virtual void SetParametersForNormalAfterExpand(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 52 | |
| 53 | // Prepare the object to do extra expansion during merge operation following |
| 54 | // a period of expands. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 55 | virtual void SetParametersForMergeAfterExpand(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 56 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 57 | // Returns the mute factor for |channel|. |
| 58 | int16_t MuteFactor(size_t channel) { |
| 59 | assert(channel < num_channels_); |
| 60 | return channel_parameters_[channel].mute_factor; |
| 61 | } |
| 62 | |
| 63 | // Accessors and mutators. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 64 | virtual size_t overlap_length() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 65 | int16_t max_lag() const { return max_lag_; } |
| 66 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 67 | protected: |
| 68 | static const int kMaxConsecutiveExpands = 200; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 69 | void GenerateRandomVector(int16_t seed_increment, |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 70 | size_t length, |
| 71 | int16_t* random_vector); |
| 72 | |
| 73 | void GenerateBackgroundNoise(int16_t* random_vector, |
| 74 | size_t channel, |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame^] | 75 | int mute_slope, |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 76 | bool too_many_expands, |
| 77 | size_t num_noise_samples, |
| 78 | int16_t* buffer); |
| 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* random_vector_; |
| 90 | SyncBuffer* sync_buffer_; |
| 91 | bool first_expand_; |
| 92 | const int fs_hz_; |
| 93 | const size_t num_channels_; |
| 94 | int consecutive_expands_; |
| 95 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 96 | private: |
| 97 | static const int kUnvoicedLpcOrder = 6; |
| 98 | static const int kNumCorrelationCandidates = 3; |
| 99 | static const int kDistortionLength = 20; |
| 100 | static const int kLpcAnalysisLength = 160; |
| 101 | static const int kMaxSampleRate = 48000; |
| 102 | static const int kNumLags = 3; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 103 | |
| 104 | struct ChannelParameters { |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 105 | ChannelParameters(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 */ |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 113 | AudioVector expand_vector0; |
| 114 | AudioVector expand_vector1; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 115 | bool onset; |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame^] | 116 | int mute_slope; /* Q20 */ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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|. The scale factor is written to |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 122 | // |output_scale|. |
| 123 | void Correlation(const int16_t* input, |
| 124 | size_t input_length, |
| 125 | int16_t* output, |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame^] | 126 | int* output_scale) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 127 | |
| 128 | void UpdateLagIndex(); |
| 129 | |
| 130 | BackgroundNoise* background_noise_; |
henrik.lundin@webrtc.org | 340746a | 2014-02-17 11:37:16 +0000 | [diff] [blame] | 131 | const size_t overlap_length_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 132 | int16_t max_lag_; |
| 133 | size_t expand_lags_[kNumLags]; |
| 134 | int lag_index_direction_; |
| 135 | int current_lag_index_; |
| 136 | bool stop_muting_; |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 137 | rtc::scoped_ptr<ChannelParameters[]> channel_parameters_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(Expand); |
| 140 | }; |
| 141 | |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 142 | struct ExpandFactory { |
| 143 | ExpandFactory() {} |
| 144 | virtual ~ExpandFactory() {} |
| 145 | |
| 146 | virtual Expand* Create(BackgroundNoise* background_noise, |
| 147 | SyncBuffer* sync_buffer, |
| 148 | RandomVector* random_vector, |
| 149 | int fs, |
| 150 | size_t num_channels) const; |
| 151 | }; |
| 152 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 153 | } // namespace webrtc |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 154 | #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ |