2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ4_EXPAND_H_
12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_EXPAND_H_
16 #include "webrtc/modules/audio_coding/neteq4/audio_multi_vector.h"
17 #include "webrtc/system_wrappers/interface/constructor_magic.h"
18 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
19 #include "webrtc/typedefs.h"
23 // Forward declarations.
24 class BackgroundNoise;
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
34 Expand(BackgroundNoise* background_noise,
35 SyncBuffer* sync_buffer,
36 RandomVector* random_vector,
39 : background_noise_(background_noise),
40 sync_buffer_(sync_buffer),
41 random_vector_(random_vector),
44 num_channels_(num_channels),
45 overlap_length_(5 * fs / 8000),
46 lag_index_direction_(0),
47 current_lag_index_(0),
49 channel_parameters_(new ChannelParameters[num_channels_]) {
50 assert(fs == 8000 || fs == 16000 || fs == 32000 || fs == 48000);
51 assert(fs <= kMaxSampleRate); // Should not be possible.
52 assert(num_channels_ > 0);
53 memset(expand_lags_, 0, sizeof(expand_lags_));
62 // The main method to produce concealment data. The data is appended to the
64 int Process(AudioMultiVector* output);
66 // Prepare the object to do extra expansion during normal operation following
67 // a period of expands.
68 void SetParametersForNormalAfterExpand();
70 // Prepare the object to do extra expansion during merge operation following
71 // a period of expands.
72 void SetParametersForMergeAfterExpand();
74 // Sets the mute factor for |channel| to |value|.
75 void SetMuteFactor(int16_t value, size_t channel) {
76 assert(channel < num_channels_);
77 channel_parameters_[channel].mute_factor = value;
80 // Returns the mute factor for |channel|.
81 int16_t MuteFactor(size_t channel) {
82 assert(channel < num_channels_);
83 return channel_parameters_[channel].mute_factor;
86 // Accessors and mutators.
87 size_t overlap_length() const { return overlap_length_; }
88 int16_t max_lag() const { return max_lag_; }
91 static const int kUnvoicedLpcOrder = 6;
92 static const int kNumCorrelationCandidates = 3;
93 static const int kDistortionLength = 20;
94 static const int kLpcAnalysisLength = 160;
95 static const int kMaxSampleRate = 48000;
96 static const int kNumLags = 3;
97 static const int kMaxConsecutiveExpands = 200;
99 struct ChannelParameters {
102 : mute_factor(16384),
106 current_voice_mix_factor(0),
109 memset(ar_filter, 0, sizeof(ar_filter));
110 memset(ar_filter_state, 0, sizeof(ar_filter_state));
113 int16_t ar_filter[kUnvoicedLpcOrder + 1];
114 int16_t ar_filter_state[kUnvoicedLpcOrder];
116 int16_t ar_gain_scale;
117 int16_t voice_mix_factor; /* Q14 */
118 int16_t current_voice_mix_factor; /* Q14 */
119 AudioVector expand_vector0;
120 AudioVector expand_vector1;
122 int16_t mute_slope; /* Q20 */
125 // Analyze the signal history in |sync_buffer_|, and set up all parameters
126 // necessary to produce concealment data.
127 void AnalyzeSignal(int16_t* random_vector);
129 // Calculate the auto-correlation of |input|, with length |input_length|
130 // samples. The correlation is calculated from a downsampled version of
131 // |input|, and is written to |output|. The scale factor is written to
132 // |output_scale|. Returns the length of the correlation vector.
133 int16_t Correlation(const int16_t* input, size_t input_length,
134 int16_t* output, int16_t* output_scale) const;
136 void UpdateLagIndex();
138 BackgroundNoise* background_noise_;
139 SyncBuffer* sync_buffer_;
140 RandomVector* random_vector_;
143 const size_t num_channels_;
144 const size_t overlap_length_;
145 int consecutive_expands_;
147 size_t expand_lags_[kNumLags];
148 int lag_index_direction_;
149 int current_lag_index_;
151 scoped_array<ChannelParameters> channel_parameters_;
153 DISALLOW_COPY_AND_ASSIGN(Expand);
156 struct ExpandFactory {
158 virtual ~ExpandFactory() {}
160 virtual Expand* Create(BackgroundNoise* background_noise,
161 SyncBuffer* sync_buffer,
162 RandomVector* random_vector,
164 size_t num_channels) const;
167 } // namespace webrtc
168 #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_EXPAND_H_