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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 15 | #include <memory> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/neteq/audio_vector.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | // Forward declarations. |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | class AudioMultiVector; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 23 | class BackgroundNoise; |
| 24 | class RandomVector; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 25 | class StatisticsCalculator; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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, |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 37 | StatisticsCalculator* statistics, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 38 | int fs, |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 39 | size_t num_channels); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 40 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 41 | virtual ~Expand(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 42 | |
Byoungchan Lee | 604fd2f | 2022-01-21 09:49:39 +0900 | [diff] [blame] | 43 | Expand(const Expand&) = delete; |
| 44 | Expand& operator=(const Expand&) = delete; |
| 45 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 46 | // Resets the object. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 47 | virtual void Reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | |
| 49 | // The main method to produce concealment data. The data is appended to the |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 50 | // end of `output`. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 51 | virtual int Process(AudioMultiVector* output); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 52 | |
| 53 | // Prepare the object to do extra expansion during normal operation following |
| 54 | // a period of expands. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 55 | virtual void SetParametersForNormalAfterExpand(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 56 | |
| 57 | // Prepare the object to do extra expansion during merge operation following |
| 58 | // a period of expands. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 59 | virtual void SetParametersForMergeAfterExpand(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 60 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 61 | // Returns the mute factor for `channel`. |
Ivo Creusen | c7f09ad | 2018-05-22 13:21:01 +0200 | [diff] [blame] | 62 | int16_t MuteFactor(size_t channel) const { |
Mirko Bonadei | 25ab322 | 2021-07-08 20:08:20 +0200 | [diff] [blame] | 63 | RTC_DCHECK_LT(channel, num_channels_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | return channel_parameters_[channel].mute_factor; |
| 65 | } |
| 66 | |
henrik.lundin | f3995f7 | 2016-05-10 05:54:35 -0700 | [diff] [blame] | 67 | // Returns true if expansion has been faded down to zero amplitude (for all |
| 68 | // channels); false otherwise. |
| 69 | bool Muted() const; |
| 70 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 71 | // Accessors and mutators. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 72 | virtual size_t overlap_length() const; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 73 | size_t max_lag() const { return max_lag_; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 74 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 75 | protected: |
| 76 | static const int kMaxConsecutiveExpands = 200; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 77 | void GenerateRandomVector(int16_t seed_increment, |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 78 | size_t length, |
| 79 | int16_t* random_vector); |
| 80 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 81 | // Initializes member variables at the beginning of an expand period. |
| 82 | void InitializeForAnExpandPeriod(); |
| 83 | |
| 84 | bool TooManyExpands(); |
| 85 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 86 | // Analyzes the signal history in `sync_buffer_`, and set up all parameters |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 87 | // necessary to produce concealment data. |
| 88 | void AnalyzeSignal(int16_t* random_vector); |
| 89 | |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 90 | RandomVector* const random_vector_; |
| 91 | SyncBuffer* const sync_buffer_; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 92 | bool first_expand_; |
| 93 | const int fs_hz_; |
| 94 | const size_t num_channels_; |
| 95 | int consecutive_expands_; |
| 96 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 97 | private: |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 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; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 103 | static const int kNumLags = 3; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 104 | |
| 105 | struct ChannelParameters { |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 106 | ChannelParameters(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 112 | int16_t voice_mix_factor; /* Q14 */ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 113 | int16_t current_voice_mix_factor; /* Q14 */ |
henrik.lundin@webrtc.org | 1871dd2 | 2013-10-14 20:33:25 +0000 | [diff] [blame] | 114 | AudioVector expand_vector0; |
| 115 | AudioVector expand_vector1; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 116 | bool onset; |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 117 | int mute_slope; /* Q20 */ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 120 | // Calculate the auto-correlation of `input`, with length `input_length` |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 121 | // samples. The correlation is calculated from a downsampled version of |
Artem Titov | d00ce74 | 2021-07-28 20:00:17 +0200 | [diff] [blame] | 122 | // `input`, and is written to `output`. |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 123 | void Correlation(const int16_t* input, |
| 124 | size_t input_length, |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 125 | int16_t* output) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 126 | |
| 127 | void UpdateLagIndex(); |
| 128 | |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 129 | BackgroundNoise* const background_noise_; |
| 130 | StatisticsCalculator* const statistics_; |
henrik.lundin@webrtc.org | 340746a | 2014-02-17 11:37:16 +0000 | [diff] [blame] | 131 | const size_t overlap_length_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 132 | size_t max_lag_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 133 | size_t expand_lags_[kNumLags]; |
| 134 | int lag_index_direction_; |
| 135 | int current_lag_index_; |
| 136 | bool stop_muting_; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 137 | size_t expand_duration_samples_; |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 138 | std::unique_ptr<ChannelParameters[]> channel_parameters_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 141 | struct ExpandFactory { |
| 142 | ExpandFactory() {} |
| 143 | virtual ~ExpandFactory() {} |
| 144 | |
| 145 | virtual Expand* Create(BackgroundNoise* background_noise, |
| 146 | SyncBuffer* sync_buffer, |
| 147 | RandomVector* random_vector, |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 148 | StatisticsCalculator* statistics, |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 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 |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 154 | #endif // MODULES_AUDIO_CODING_NETEQ_EXPAND_H_ |