Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / neteq4 / expand.h
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
11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ4_EXPAND_H_
12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_EXPAND_H_
13
14 #include <assert.h>
15
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"
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,
38          size_t num_channels)
39       : background_noise_(background_noise),
40         sync_buffer_(sync_buffer),
41         random_vector_(random_vector),
42         first_expand_(true),
43         fs_hz_(fs),
44         num_channels_(num_channels),
45         overlap_length_(5 * fs / 8000),
46         lag_index_direction_(0),
47         current_lag_index_(0),
48         stop_muting_(false),
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_));
54     Reset();
55   }
56
57   virtual ~Expand() {}
58
59   // Resets the object.
60   void Reset();
61
62   // The main method to produce concealment data. The data is appended to the
63   // end of |output|.
64   int Process(AudioMultiVector* output);
65
66   // Prepare the object to do extra expansion during normal operation following
67   // a period of expands.
68   void SetParametersForNormalAfterExpand();
69
70   // Prepare the object to do extra expansion during merge operation following
71   // a period of expands.
72   void SetParametersForMergeAfterExpand();
73
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;
78   }
79
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;
84   }
85
86   // Accessors and mutators.
87   size_t overlap_length() const { return overlap_length_; }
88   int16_t max_lag() const { return max_lag_; }
89
90  private:
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;
98
99   struct ChannelParameters {
100     // Constructor.
101     ChannelParameters()
102         : mute_factor(16384),
103           ar_gain(0),
104           ar_gain_scale(0),
105           voice_mix_factor(0),
106           current_voice_mix_factor(0),
107           onset(false),
108           mute_slope(0) {
109       memset(ar_filter, 0, sizeof(ar_filter));
110       memset(ar_filter_state, 0, sizeof(ar_filter_state));
111     }
112     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;
117     int16_t voice_mix_factor; /* Q14 */
118     int16_t current_voice_mix_factor; /* Q14 */
119     AudioVector expand_vector0;
120     AudioVector expand_vector1;
121     bool onset;
122     int16_t mute_slope; /* Q20 */
123   };
124
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);
128
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;
135
136   void UpdateLagIndex();
137
138   BackgroundNoise* background_noise_;
139   SyncBuffer* sync_buffer_;
140   RandomVector* random_vector_;
141   bool first_expand_;
142   const int fs_hz_;
143   const size_t num_channels_;
144   const size_t overlap_length_;
145   int consecutive_expands_;
146   int16_t max_lag_;
147   size_t expand_lags_[kNumLags];
148   int lag_index_direction_;
149   int current_lag_index_;
150   bool stop_muting_;
151   scoped_array<ChannelParameters> channel_parameters_;
152
153   DISALLOW_COPY_AND_ASSIGN(Expand);
154 };
155
156 struct ExpandFactory {
157   ExpandFactory() {}
158   virtual ~ExpandFactory() {}
159
160   virtual Expand* Create(BackgroundNoise* background_noise,
161                          SyncBuffer* sync_buffer,
162                          RandomVector* random_vector,
163                          int fs,
164                          size_t num_channels) const;
165 };
166
167 }  // namespace webrtc
168 #endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_EXPAND_H_