blob: 7c041b8671200a45c0ec18b2cf44f74051fad998 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_EXPAND_H_
12#define MODULES_AUDIO_CODING_NETEQ_EXPAND_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
14#include <assert.h>
kwiberg2d0c3322016-02-14 09:28:33 -080015#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
Yves Gerey988cc082018-10-23 12:03:01 +020017#include "modules/audio_coding/neteq/audio_vector.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019
20namespace webrtc {
21
22// Forward declarations.
Yves Gerey988cc082018-10-23 12:03:01 +020023class AudioMultiVector;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024class BackgroundNoise;
25class RandomVector;
Henrik Lundinbef77e22015-08-18 14:58:09 +020026class StatisticsCalculator;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027class SyncBuffer;
28
29// This class handles extrapolation of audio data from the sync_buffer to
30// produce packet-loss concealment.
31// TODO(hlundin): Refactor this class to divide the long methods into shorter
32// ones.
33class Expand {
34 public:
35 Expand(BackgroundNoise* background_noise,
36 SyncBuffer* sync_buffer,
37 RandomVector* random_vector,
Henrik Lundinbef77e22015-08-18 14:58:09 +020038 StatisticsCalculator* statistics,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 int fs,
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020040 size_t num_channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020042 virtual ~Expand();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
44 // Resets the object.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000045 virtual void Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046
47 // The main method to produce concealment data. The data is appended to the
48 // end of |output|.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000049 virtual int Process(AudioMultiVector* output);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
51 // Prepare the object to do extra expansion during normal operation following
52 // a period of expands.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000053 virtual void SetParametersForNormalAfterExpand();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054
55 // Prepare the object to do extra expansion during merge operation following
56 // a period of expands.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000057 virtual void SetParametersForMergeAfterExpand();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059 // Returns the mute factor for |channel|.
Ivo Creusenc7f09ad2018-05-22 13:21:01 +020060 int16_t MuteFactor(size_t channel) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 assert(channel < num_channels_);
62 return channel_parameters_[channel].mute_factor;
63 }
64
henrik.lundinf3995f72016-05-10 05:54:35 -070065 // Returns true if expansion has been faded down to zero amplitude (for all
66 // channels); false otherwise.
67 bool Muted() const;
68
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 // Accessors and mutators.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020070 virtual size_t overlap_length() const;
Peter Kastingdce40cf2015-08-24 14:52:23 -070071 size_t max_lag() const { return max_lag_; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000073 protected:
74 static const int kMaxConsecutiveExpands = 200;
Peter Kastingb7e50542015-06-11 12:55:50 -070075 void GenerateRandomVector(int16_t seed_increment,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000076 size_t length,
77 int16_t* random_vector);
78
79 void GenerateBackgroundNoise(int16_t* random_vector,
80 size_t channel,
Peter Kasting36b7cc32015-06-11 19:57:18 -070081 int mute_slope,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000082 bool too_many_expands,
83 size_t num_noise_samples,
84 int16_t* buffer);
85
86 // Initializes member variables at the beginning of an expand period.
87 void InitializeForAnExpandPeriod();
88
89 bool TooManyExpands();
90
91 // Analyzes the signal history in |sync_buffer_|, and set up all parameters
92 // necessary to produce concealment data.
93 void AnalyzeSignal(int16_t* random_vector);
94
Henrik Lundinbef77e22015-08-18 14:58:09 +020095 RandomVector* const random_vector_;
96 SyncBuffer* const sync_buffer_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000097 bool first_expand_;
98 const int fs_hz_;
99 const size_t num_channels_;
100 int consecutive_expands_;
101
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102 private:
Peter Kastingdce40cf2015-08-24 14:52:23 -0700103 static const size_t kUnvoicedLpcOrder = 6;
104 static const size_t kNumCorrelationCandidates = 3;
105 static const size_t kDistortionLength = 20;
106 static const size_t kLpcAnalysisLength = 160;
107 static const size_t kMaxSampleRate = 48000;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 static const int kNumLags = 3;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109
110 struct ChannelParameters {
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200111 ChannelParameters();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112 int16_t mute_factor;
113 int16_t ar_filter[kUnvoicedLpcOrder + 1];
114 int16_t ar_filter_state[kUnvoicedLpcOrder];
115 int16_t ar_gain;
116 int16_t ar_gain_scale;
Yves Gerey665174f2018-06-19 15:03:05 +0200117 int16_t voice_mix_factor; /* Q14 */
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118 int16_t current_voice_mix_factor; /* Q14 */
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +0000119 AudioVector expand_vector0;
120 AudioVector expand_vector1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 bool onset;
Peter Kasting36b7cc32015-06-11 19:57:18 -0700122 int mute_slope; /* Q20 */
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123 };
124
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000125 // Calculate the auto-correlation of |input|, with length |input_length|
126 // samples. The correlation is calculated from a downsampled version of
minyue53ff70f2016-05-02 01:50:30 -0700127 // |input|, and is written to |output|.
Peter Kasting728d9032015-06-11 14:31:38 -0700128 void Correlation(const int16_t* input,
129 size_t input_length,
minyue53ff70f2016-05-02 01:50:30 -0700130 int16_t* output) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000131
132 void UpdateLagIndex();
133
Henrik Lundinbef77e22015-08-18 14:58:09 +0200134 BackgroundNoise* const background_noise_;
135 StatisticsCalculator* const statistics_;
henrik.lundin@webrtc.org340746a2014-02-17 11:37:16 +0000136 const size_t overlap_length_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 size_t max_lag_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000138 size_t expand_lags_[kNumLags];
139 int lag_index_direction_;
140 int current_lag_index_;
141 bool stop_muting_;
Henrik Lundinbef77e22015-08-18 14:58:09 +0200142 size_t expand_duration_samples_;
kwiberg2d0c3322016-02-14 09:28:33 -0800143 std::unique_ptr<ChannelParameters[]> channel_parameters_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000144
henrikg3c089d72015-09-16 05:37:44 -0700145 RTC_DISALLOW_COPY_AND_ASSIGN(Expand);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000146};
147
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000148struct ExpandFactory {
149 ExpandFactory() {}
150 virtual ~ExpandFactory() {}
151
152 virtual Expand* Create(BackgroundNoise* background_noise,
153 SyncBuffer* sync_buffer,
154 RandomVector* random_vector,
Henrik Lundinbef77e22015-08-18 14:58:09 +0200155 StatisticsCalculator* statistics,
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000156 int fs,
157 size_t num_channels) const;
158};
159
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200161#endif // MODULES_AUDIO_CODING_NETEQ_EXPAND_H_