blob: 37e58d6facb81b0e7891cdd62b9a4277ab9ab6e1 [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_EXPAND_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_EXPAND_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
14#include <assert.h>
15
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000016#include "webrtc/base/constructormagic.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000017#include "webrtc/base/scoped_ptr.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000018#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019#include "webrtc/typedefs.h"
20
21namespace webrtc {
22
23// Forward declarations.
24class 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|.
60 int16_t MuteFactor(size_t channel) {
61 assert(channel < num_channels_);
62 return channel_parameters_[channel].mute_factor;
63 }
64
65 // Accessors and mutators.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020066 virtual size_t overlap_length() const;
Peter Kastingdce40cf2015-08-24 14:52:23 -070067 size_t max_lag() const { return max_lag_; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000069 protected:
70 static const int kMaxConsecutiveExpands = 200;
Peter Kastingb7e50542015-06-11 12:55:50 -070071 void GenerateRandomVector(int16_t seed_increment,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000072 size_t length,
73 int16_t* random_vector);
74
75 void GenerateBackgroundNoise(int16_t* random_vector,
76 size_t channel,
Peter Kasting36b7cc32015-06-11 19:57:18 -070077 int mute_slope,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000078 bool too_many_expands,
79 size_t num_noise_samples,
80 int16_t* buffer);
81
82 // Initializes member variables at the beginning of an expand period.
83 void InitializeForAnExpandPeriod();
84
85 bool TooManyExpands();
86
87 // Analyzes the signal history in |sync_buffer_|, and set up all parameters
88 // necessary to produce concealment data.
89 void AnalyzeSignal(int16_t* random_vector);
90
Henrik Lundinbef77e22015-08-18 14:58:09 +020091 RandomVector* const random_vector_;
92 SyncBuffer* const sync_buffer_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000093 bool first_expand_;
94 const int fs_hz_;
95 const size_t num_channels_;
96 int consecutive_expands_;
97
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098 private:
Peter Kastingdce40cf2015-08-24 14:52:23 -070099 static const size_t kUnvoicedLpcOrder = 6;
100 static const size_t kNumCorrelationCandidates = 3;
101 static const size_t kDistortionLength = 20;
102 static const size_t kLpcAnalysisLength = 160;
103 static const size_t kMaxSampleRate = 48000;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104 static const int kNumLags = 3;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105
106 struct ChannelParameters {
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200107 ChannelParameters();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 int16_t mute_factor;
109 int16_t ar_filter[kUnvoicedLpcOrder + 1];
110 int16_t ar_filter_state[kUnvoicedLpcOrder];
111 int16_t ar_gain;
112 int16_t ar_gain_scale;
113 int16_t voice_mix_factor; /* Q14 */
114 int16_t current_voice_mix_factor; /* Q14 */
henrik.lundin@webrtc.org1871dd22013-10-14 20:33:25 +0000115 AudioVector expand_vector0;
116 AudioVector expand_vector1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117 bool onset;
Peter Kasting36b7cc32015-06-11 19:57:18 -0700118 int mute_slope; /* Q20 */
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000119 };
120
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 // Calculate the auto-correlation of |input|, with length |input_length|
122 // samples. The correlation is calculated from a downsampled version of
123 // |input|, and is written to |output|. The scale factor is written to
Peter Kasting728d9032015-06-11 14:31:38 -0700124 // |output_scale|.
125 void Correlation(const int16_t* input,
126 size_t input_length,
127 int16_t* output,
Peter Kasting36b7cc32015-06-11 19:57:18 -0700128 int* output_scale) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000129
130 void UpdateLagIndex();
131
Henrik Lundinbef77e22015-08-18 14:58:09 +0200132 BackgroundNoise* const background_noise_;
133 StatisticsCalculator* const statistics_;
henrik.lundin@webrtc.org340746a2014-02-17 11:37:16 +0000134 const size_t overlap_length_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700135 size_t max_lag_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000136 size_t expand_lags_[kNumLags];
137 int lag_index_direction_;
138 int current_lag_index_;
139 bool stop_muting_;
Henrik Lundinbef77e22015-08-18 14:58:09 +0200140 size_t expand_duration_samples_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000141 rtc::scoped_ptr<ChannelParameters[]> channel_parameters_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000142
143 DISALLOW_COPY_AND_ASSIGN(Expand);
144};
145
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000146struct ExpandFactory {
147 ExpandFactory() {}
148 virtual ~ExpandFactory() {}
149
150 virtual Expand* Create(BackgroundNoise* background_noise,
151 SyncBuffer* sync_buffer,
152 RandomVector* random_vector,
Henrik Lundinbef77e22015-08-18 14:58:09 +0200153 StatisticsCalculator* statistics,
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000154 int fs,
155 size_t num_channels) const;
156};
157
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000159#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_EXPAND_H_