blob: 4cfe7b9d8cbeec1e7990bb4279a64016be149600 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/audio_multi_vector.h"
18#include "rtc_base/constructormagic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019
20namespace webrtc {
21
22// Forward declarations.
23class BackgroundNoise;
24class RandomVector;
Henrik Lundinbef77e22015-08-18 14:58:09 +020025class StatisticsCalculator;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026class 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.
32class Expand {
33 public:
34 Expand(BackgroundNoise* background_noise,
35 SyncBuffer* sync_buffer,
36 RandomVector* random_vector,
Henrik Lundinbef77e22015-08-18 14:58:09 +020037 StatisticsCalculator* statistics,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038 int fs,
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020039 size_t num_channels);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020041 virtual ~Expand();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042
43 // Resets the object.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000044 virtual void Reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045
46 // The main method to produce concealment data. The data is appended to the
47 // end of |output|.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000048 virtual int Process(AudioMultiVector* output);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
50 // Prepare the object to do extra expansion during normal operation following
51 // a period of expands.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000052 virtual void SetParametersForNormalAfterExpand();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053
54 // Prepare the object to do extra expansion during merge operation following
55 // a period of expands.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000056 virtual void SetParametersForMergeAfterExpand();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058 // Returns the mute factor for |channel|.
Ivo Creusenc7f09ad2018-05-22 13:21:01 +020059 int16_t MuteFactor(size_t channel) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060 assert(channel < num_channels_);
61 return channel_parameters_[channel].mute_factor;
62 }
63
henrik.lundinf3995f72016-05-10 05:54:35 -070064 // Returns true if expansion has been faded down to zero amplitude (for all
65 // channels); false otherwise.
66 bool Muted() const;
67
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 // Accessors and mutators.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020069 virtual size_t overlap_length() const;
Peter Kastingdce40cf2015-08-24 14:52:23 -070070 size_t max_lag() const { return max_lag_; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000072 protected:
73 static const int kMaxConsecutiveExpands = 200;
Peter Kastingb7e50542015-06-11 12:55:50 -070074 void GenerateRandomVector(int16_t seed_increment,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000075 size_t length,
76 int16_t* random_vector);
77
78 void GenerateBackgroundNoise(int16_t* random_vector,
79 size_t channel,
Peter Kasting36b7cc32015-06-11 19:57:18 -070080 int mute_slope,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000081 bool too_many_expands,
82 size_t num_noise_samples,
83 int16_t* buffer);
84
85 // Initializes member variables at the beginning of an expand period.
86 void InitializeForAnExpandPeriod();
87
88 bool TooManyExpands();
89
90 // Analyzes the signal history in |sync_buffer_|, and set up all parameters
91 // necessary to produce concealment data.
92 void AnalyzeSignal(int16_t* random_vector);
93
Henrik Lundinbef77e22015-08-18 14:58:09 +020094 RandomVector* const random_vector_;
95 SyncBuffer* const sync_buffer_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000096 bool first_expand_;
97 const int fs_hz_;
98 const size_t num_channels_;
99 int consecutive_expands_;
100
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101 private:
Peter Kastingdce40cf2015-08-24 14:52:23 -0700102 static const size_t kUnvoicedLpcOrder = 6;
103 static const size_t kNumCorrelationCandidates = 3;
104 static const size_t kDistortionLength = 20;
105 static const size_t kLpcAnalysisLength = 160;
106 static const size_t kMaxSampleRate = 48000;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000107 static const int kNumLags = 3;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108
109 struct ChannelParameters {
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200110 ChannelParameters();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111 int16_t mute_factor;
112 int16_t ar_filter[kUnvoicedLpcOrder + 1];
113 int16_t ar_filter_state[kUnvoicedLpcOrder];
114 int16_t ar_gain;
115 int16_t ar_gain_scale;
Yves Gerey665174f2018-06-19 15:03:05 +0200116 int16_t voice_mix_factor; /* Q14 */
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117 int16_t current_voice_mix_factor; /* Q14 */
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +0000118 AudioVector expand_vector0;
119 AudioVector expand_vector1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120 bool onset;
Peter Kasting36b7cc32015-06-11 19:57:18 -0700121 int mute_slope; /* Q20 */
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122 };
123
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124 // Calculate the auto-correlation of |input|, with length |input_length|
125 // samples. The correlation is calculated from a downsampled version of
minyue53ff70f2016-05-02 01:50:30 -0700126 // |input|, and is written to |output|.
Peter Kasting728d9032015-06-11 14:31:38 -0700127 void Correlation(const int16_t* input,
128 size_t input_length,
minyue53ff70f2016-05-02 01:50:30 -0700129 int16_t* output) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000130
131 void UpdateLagIndex();
132
Henrik Lundinbef77e22015-08-18 14:58:09 +0200133 BackgroundNoise* const background_noise_;
134 StatisticsCalculator* const statistics_;
henrik.lundin@webrtc.org340746a2014-02-17 11:37:16 +0000135 const size_t overlap_length_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 size_t max_lag_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000137 size_t expand_lags_[kNumLags];
138 int lag_index_direction_;
139 int current_lag_index_;
140 bool stop_muting_;
Henrik Lundinbef77e22015-08-18 14:58:09 +0200141 size_t expand_duration_samples_;
kwiberg2d0c3322016-02-14 09:28:33 -0800142 std::unique_ptr<ChannelParameters[]> channel_parameters_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000143
henrikg3c089d72015-09-16 05:37:44 -0700144 RTC_DISALLOW_COPY_AND_ASSIGN(Expand);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000145};
146
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000147struct ExpandFactory {
148 ExpandFactory() {}
149 virtual ~ExpandFactory() {}
150
151 virtual Expand* Create(BackgroundNoise* background_noise,
152 SyncBuffer* sync_buffer,
153 RandomVector* random_vector,
Henrik Lundinbef77e22015-08-18 14:58:09 +0200154 StatisticsCalculator* statistics,
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000155 int fs,
156 size_t num_channels) const;
157};
158
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200160#endif // MODULES_AUDIO_CODING_NETEQ_EXPAND_H_