OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "webrtc/modules/audio_coding/neteq/sync_buffer.h" | 23 #include "webrtc/modules/audio_coding/neteq/sync_buffer.h" |
24 | 24 |
25 namespace webrtc { | 25 namespace webrtc { |
26 | 26 |
27 Merge::Merge(int fs_hz, | 27 Merge::Merge(int fs_hz, |
28 size_t num_channels, | 28 size_t num_channels, |
29 Expand* expand, | 29 Expand* expand, |
30 SyncBuffer* sync_buffer) | 30 SyncBuffer* sync_buffer) |
31 : fs_hz_(fs_hz), | 31 : fs_hz_(fs_hz), |
32 num_channels_(num_channels), | 32 num_channels_(num_channels), |
33 fs_mult_(fs_hz_ / 8000), | 33 fs_mult_(static_cast<size_t>(fs_hz_ / 8000)), |
34 timestamps_per_call_(fs_hz_ / 100), | 34 timestamps_per_call_(static_cast<size_t>(fs_hz_ / 100)), |
35 expand_(expand), | 35 expand_(expand), |
36 sync_buffer_(sync_buffer), | 36 sync_buffer_(sync_buffer), |
37 expanded_(num_channels_) { | 37 expanded_(num_channels_) { |
38 assert(num_channels_ > 0); | 38 assert(num_channels_ > 0); |
39 } | 39 } |
40 | 40 |
41 int Merge::Process(int16_t* input, size_t input_length, | 41 size_t Merge::Process(int16_t* input, size_t input_length, |
42 int16_t* external_mute_factor_array, | 42 int16_t* external_mute_factor_array, |
43 AudioMultiVector* output) { | 43 AudioMultiVector* output) { |
44 // TODO(hlundin): Change to an enumerator and skip assert. | 44 // TODO(hlundin): Change to an enumerator and skip assert. |
45 assert(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ == 32000 || | 45 assert(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ == 32000 || |
46 fs_hz_ == 48000); | 46 fs_hz_ == 48000); |
47 assert(fs_hz_ <= kMaxSampleRate); // Should not be possible. | 47 assert(fs_hz_ <= kMaxSampleRate); // Should not be possible. |
48 | 48 |
49 int old_length; | 49 size_t old_length; |
50 int expand_period; | 50 size_t expand_period; |
51 // Get expansion data to overlap and mix with. | 51 // Get expansion data to overlap and mix with. |
52 int expanded_length = GetExpandedSignal(&old_length, &expand_period); | 52 size_t expanded_length = GetExpandedSignal(&old_length, &expand_period); |
53 | 53 |
54 // Transfer input signal to an AudioMultiVector. | 54 // Transfer input signal to an AudioMultiVector. |
55 AudioMultiVector input_vector(num_channels_); | 55 AudioMultiVector input_vector(num_channels_); |
56 input_vector.PushBackInterleaved(input, input_length); | 56 input_vector.PushBackInterleaved(input, input_length); |
57 size_t input_length_per_channel = input_vector.Size(); | 57 size_t input_length_per_channel = input_vector.Size(); |
58 assert(input_length_per_channel == input_length / num_channels_); | 58 assert(input_length_per_channel == input_length / num_channels_); |
59 | 59 |
60 int16_t best_correlation_index = 0; | 60 size_t best_correlation_index = 0; |
61 size_t output_length = 0; | 61 size_t output_length = 0; |
62 | 62 |
63 for (size_t channel = 0; channel < num_channels_; ++channel) { | 63 for (size_t channel = 0; channel < num_channels_; ++channel) { |
64 int16_t* input_channel = &input_vector[channel][0]; | 64 int16_t* input_channel = &input_vector[channel][0]; |
65 int16_t* expanded_channel = &expanded_[channel][0]; | 65 int16_t* expanded_channel = &expanded_[channel][0]; |
66 int16_t expanded_max, input_max; | 66 int16_t expanded_max, input_max; |
67 int16_t new_mute_factor = SignalScaling( | 67 int16_t new_mute_factor = SignalScaling( |
68 input_channel, static_cast<int>(input_length_per_channel), | 68 input_channel, input_length_per_channel, expanded_channel, |
69 expanded_channel, &expanded_max, &input_max); | 69 &expanded_max, &input_max); |
70 | 70 |
71 // Adjust muting factor (product of "main" muting factor and expand muting | 71 // Adjust muting factor (product of "main" muting factor and expand muting |
72 // factor). | 72 // factor). |
73 int16_t* external_mute_factor = &external_mute_factor_array[channel]; | 73 int16_t* external_mute_factor = &external_mute_factor_array[channel]; |
74 *external_mute_factor = | 74 *external_mute_factor = |
75 (*external_mute_factor * expand_->MuteFactor(channel)) >> 14; | 75 (*external_mute_factor * expand_->MuteFactor(channel)) >> 14; |
76 | 76 |
77 // Update |external_mute_factor| if it is lower than |new_mute_factor|. | 77 // Update |external_mute_factor| if it is lower than |new_mute_factor|. |
78 if (new_mute_factor > *external_mute_factor) { | 78 if (new_mute_factor > *external_mute_factor) { |
79 *external_mute_factor = std::min(new_mute_factor, | 79 *external_mute_factor = std::min(new_mute_factor, |
80 static_cast<int16_t>(16384)); | 80 static_cast<int16_t>(16384)); |
81 } | 81 } |
82 | 82 |
83 if (channel == 0) { | 83 if (channel == 0) { |
84 // Downsample, correlate, and find strongest correlation period for the | 84 // Downsample, correlate, and find strongest correlation period for the |
85 // master (i.e., first) channel only. | 85 // master (i.e., first) channel only. |
86 // Downsample to 4kHz sample rate. | 86 // Downsample to 4kHz sample rate. |
87 Downsample(input_channel, static_cast<int>(input_length_per_channel), | 87 Downsample(input_channel, input_length_per_channel, expanded_channel, |
88 expanded_channel, expanded_length); | 88 expanded_length); |
89 | 89 |
90 // Calculate the lag of the strongest correlation period. | 90 // Calculate the lag of the strongest correlation period. |
91 best_correlation_index = CorrelateAndPeakSearch( | 91 best_correlation_index = CorrelateAndPeakSearch( |
92 expanded_max, input_max, old_length, | 92 expanded_max, input_max, old_length, |
93 static_cast<int>(input_length_per_channel), expand_period); | 93 input_length_per_channel, expand_period); |
94 } | 94 } |
95 | 95 |
96 static const int kTempDataSize = 3600; | 96 static const int kTempDataSize = 3600; |
97 int16_t temp_data[kTempDataSize]; // TODO(hlundin) Remove this. | 97 int16_t temp_data[kTempDataSize]; // TODO(hlundin) Remove this. |
98 int16_t* decoded_output = temp_data + best_correlation_index; | 98 int16_t* decoded_output = temp_data + best_correlation_index; |
99 | 99 |
100 // Mute the new decoded data if needed (and unmute it linearly). | 100 // Mute the new decoded data if needed (and unmute it linearly). |
101 // This is the overlapping part of expanded_signal. | 101 // This is the overlapping part of expanded_signal. |
102 int interpolation_length = std::min( | 102 size_t interpolation_length = std::min( |
103 kMaxCorrelationLength * fs_mult_, | 103 kMaxCorrelationLength * fs_mult_, |
104 expanded_length - best_correlation_index); | 104 expanded_length - best_correlation_index); |
105 interpolation_length = std::min(interpolation_length, | 105 interpolation_length = std::min(interpolation_length, |
106 static_cast<int>(input_length_per_channel)); | 106 input_length_per_channel); |
107 if (*external_mute_factor < 16384) { | 107 if (*external_mute_factor < 16384) { |
108 // Set a suitable muting slope (Q20). 0.004 for NB, 0.002 for WB, | 108 // Set a suitable muting slope (Q20). 0.004 for NB, 0.002 for WB, |
109 // and so on. | 109 // and so on. |
110 int increment = 4194 / fs_mult_; | 110 int increment = static_cast<int>(4194 / fs_mult_); |
111 *external_mute_factor = | 111 *external_mute_factor = |
112 static_cast<int16_t>(DspHelper::RampSignal(input_channel, | 112 static_cast<int16_t>(DspHelper::RampSignal(input_channel, |
113 interpolation_length, | 113 interpolation_length, |
114 *external_mute_factor, | 114 *external_mute_factor, |
115 increment)); | 115 increment)); |
116 DspHelper::UnmuteSignal(&input_channel[interpolation_length], | 116 DspHelper::UnmuteSignal(&input_channel[interpolation_length], |
117 input_length_per_channel - interpolation_length, | 117 input_length_per_channel - interpolation_length, |
118 external_mute_factor, increment, | 118 external_mute_factor, increment, |
119 &decoded_output[interpolation_length]); | 119 &decoded_output[interpolation_length]); |
120 } else { | 120 } else { |
(...skipping 25 matching lines...) Expand all Loading... |
146 sizeof(temp_data[0]) * output_length); | 146 sizeof(temp_data[0]) * output_length); |
147 } | 147 } |
148 | 148 |
149 // Copy back the first part of the data to |sync_buffer_| and remove it from | 149 // Copy back the first part of the data to |sync_buffer_| and remove it from |
150 // |output|. | 150 // |output|. |
151 sync_buffer_->ReplaceAtIndex(*output, old_length, sync_buffer_->next_index()); | 151 sync_buffer_->ReplaceAtIndex(*output, old_length, sync_buffer_->next_index()); |
152 output->PopFront(old_length); | 152 output->PopFront(old_length); |
153 | 153 |
154 // Return new added length. |old_length| samples were borrowed from | 154 // Return new added length. |old_length| samples were borrowed from |
155 // |sync_buffer_|. | 155 // |sync_buffer_|. |
156 return static_cast<int>(output_length) - old_length; | 156 return output_length - old_length; |
157 } | 157 } |
158 | 158 |
159 int Merge::GetExpandedSignal(int* old_length, int* expand_period) { | 159 size_t Merge::GetExpandedSignal(size_t* old_length, size_t* expand_period) { |
160 // Check how much data that is left since earlier. | 160 // Check how much data that is left since earlier. |
161 *old_length = static_cast<int>(sync_buffer_->FutureLength()); | 161 *old_length = sync_buffer_->FutureLength(); |
162 // Should never be less than overlap_length. | 162 // Should never be less than overlap_length. |
163 assert(*old_length >= static_cast<int>(expand_->overlap_length())); | 163 assert(*old_length >= expand_->overlap_length()); |
164 // Generate data to merge the overlap with using expand. | 164 // Generate data to merge the overlap with using expand. |
165 expand_->SetParametersForMergeAfterExpand(); | 165 expand_->SetParametersForMergeAfterExpand(); |
166 | 166 |
167 if (*old_length >= 210 * kMaxSampleRate / 8000) { | 167 if (*old_length >= 210 * kMaxSampleRate / 8000) { |
168 // TODO(hlundin): Write test case for this. | 168 // TODO(hlundin): Write test case for this. |
169 // The number of samples available in the sync buffer is more than what fits | 169 // The number of samples available in the sync buffer is more than what fits |
170 // in expanded_signal. Keep the first 210 * kMaxSampleRate / 8000 samples, | 170 // in expanded_signal. Keep the first 210 * kMaxSampleRate / 8000 samples, |
171 // but shift them towards the end of the buffer. This is ok, since all of | 171 // but shift them towards the end of the buffer. This is ok, since all of |
172 // the buffer will be expand data anyway, so as long as the beginning is | 172 // the buffer will be expand data anyway, so as long as the beginning is |
173 // left untouched, we're fine. | 173 // left untouched, we're fine. |
174 int16_t length_diff = *old_length - 210 * kMaxSampleRate / 8000; | 174 size_t length_diff = *old_length - 210 * kMaxSampleRate / 8000; |
175 sync_buffer_->InsertZerosAtIndex(length_diff, sync_buffer_->next_index()); | 175 sync_buffer_->InsertZerosAtIndex(length_diff, sync_buffer_->next_index()); |
176 *old_length = 210 * kMaxSampleRate / 8000; | 176 *old_length = 210 * kMaxSampleRate / 8000; |
177 // This is the truncated length. | 177 // This is the truncated length. |
178 } | 178 } |
179 // This assert should always be true thanks to the if statement above. | 179 // This assert should always be true thanks to the if statement above. |
180 assert(210 * kMaxSampleRate / 8000 >= *old_length); | 180 assert(210 * kMaxSampleRate / 8000 >= *old_length); |
181 | 181 |
182 AudioMultiVector expanded_temp(num_channels_); | 182 AudioMultiVector expanded_temp(num_channels_); |
183 expand_->Process(&expanded_temp); | 183 expand_->Process(&expanded_temp); |
184 *expand_period = static_cast<int>(expanded_temp.Size()); // Samples per | 184 *expand_period = expanded_temp.Size(); // Samples per channel. |
185 // channel. | |
186 | 185 |
187 expanded_.Clear(); | 186 expanded_.Clear(); |
188 // Copy what is left since earlier into the expanded vector. | 187 // Copy what is left since earlier into the expanded vector. |
189 expanded_.PushBackFromIndex(*sync_buffer_, sync_buffer_->next_index()); | 188 expanded_.PushBackFromIndex(*sync_buffer_, sync_buffer_->next_index()); |
190 assert(expanded_.Size() == static_cast<size_t>(*old_length)); | 189 assert(expanded_.Size() == *old_length); |
191 assert(expanded_temp.Size() > 0); | 190 assert(expanded_temp.Size() > 0); |
192 // Do "ugly" copy and paste from the expanded in order to generate more data | 191 // Do "ugly" copy and paste from the expanded in order to generate more data |
193 // to correlate (but not interpolate) with. | 192 // to correlate (but not interpolate) with. |
194 const int required_length = (120 + 80 + 2) * fs_mult_; | 193 const size_t required_length = (120 + 80 + 2) * fs_mult_; |
195 if (expanded_.Size() < static_cast<size_t>(required_length)) { | 194 if (expanded_.Size() < required_length) { |
196 while (expanded_.Size() < static_cast<size_t>(required_length)) { | 195 while (expanded_.Size() < required_length) { |
197 // Append one more pitch period each time. | 196 // Append one more pitch period each time. |
198 expanded_.PushBack(expanded_temp); | 197 expanded_.PushBack(expanded_temp); |
199 } | 198 } |
200 // Trim the length to exactly |required_length|. | 199 // Trim the length to exactly |required_length|. |
201 expanded_.PopBack(expanded_.Size() - required_length); | 200 expanded_.PopBack(expanded_.Size() - required_length); |
202 } | 201 } |
203 assert(expanded_.Size() >= static_cast<size_t>(required_length)); | 202 assert(expanded_.Size() >= required_length); |
204 return required_length; | 203 return required_length; |
205 } | 204 } |
206 | 205 |
207 int16_t Merge::SignalScaling(const int16_t* input, int input_length, | 206 int16_t Merge::SignalScaling(const int16_t* input, size_t input_length, |
208 const int16_t* expanded_signal, | 207 const int16_t* expanded_signal, |
209 int16_t* expanded_max, int16_t* input_max) const { | 208 int16_t* expanded_max, int16_t* input_max) const { |
210 // Adjust muting factor if new vector is more or less of the BGN energy. | 209 // Adjust muting factor if new vector is more or less of the BGN energy. |
211 const int mod_input_length = std::min(64 * fs_mult_, input_length); | 210 const size_t mod_input_length = std::min(64 * fs_mult_, input_length); |
212 *expanded_max = WebRtcSpl_MaxAbsValueW16(expanded_signal, mod_input_length); | 211 *expanded_max = WebRtcSpl_MaxAbsValueW16(expanded_signal, mod_input_length); |
213 *input_max = WebRtcSpl_MaxAbsValueW16(input, mod_input_length); | 212 *input_max = WebRtcSpl_MaxAbsValueW16(input, mod_input_length); |
214 | 213 |
215 // Calculate energy of expanded signal. | 214 // Calculate energy of expanded signal. |
216 // |log_fs_mult| is log2(fs_mult_), but is not exact for 48000 Hz. | 215 // |log_fs_mult| is log2(fs_mult_), but is not exact for 48000 Hz. |
217 int log_fs_mult = 30 - WebRtcSpl_NormW32(fs_mult_); | 216 int log_fs_mult = 30 - WebRtcSpl_NormW32(static_cast<int32_t>(fs_mult_)); |
218 int expanded_shift = 6 + log_fs_mult | 217 int expanded_shift = 6 + log_fs_mult |
219 - WebRtcSpl_NormW32(*expanded_max * *expanded_max); | 218 - WebRtcSpl_NormW32(*expanded_max * *expanded_max); |
220 expanded_shift = std::max(expanded_shift, 0); | 219 expanded_shift = std::max(expanded_shift, 0); |
221 int32_t energy_expanded = WebRtcSpl_DotProductWithScale(expanded_signal, | 220 int32_t energy_expanded = WebRtcSpl_DotProductWithScale(expanded_signal, |
222 expanded_signal, | 221 expanded_signal, |
223 mod_input_length, | 222 mod_input_length, |
224 expanded_shift); | 223 expanded_shift); |
225 | 224 |
226 // Calculate energy of input signal. | 225 // Calculate energy of input signal. |
227 int input_shift = 6 + log_fs_mult - | 226 int input_shift = 6 + log_fs_mult - |
(...skipping 25 matching lines...) Expand all Loading... |
253 } else { | 252 } else { |
254 // Set to 1 (in Q14) when |expanded| has higher energy than |input|. | 253 // Set to 1 (in Q14) when |expanded| has higher energy than |input|. |
255 mute_factor = 16384; | 254 mute_factor = 16384; |
256 } | 255 } |
257 | 256 |
258 return mute_factor; | 257 return mute_factor; |
259 } | 258 } |
260 | 259 |
261 // TODO(hlundin): There are some parameter values in this method that seem | 260 // TODO(hlundin): There are some parameter values in this method that seem |
262 // strange. Compare with Expand::Correlation. | 261 // strange. Compare with Expand::Correlation. |
263 void Merge::Downsample(const int16_t* input, int input_length, | 262 void Merge::Downsample(const int16_t* input, size_t input_length, |
264 const int16_t* expanded_signal, int expanded_length) { | 263 const int16_t* expanded_signal, size_t expanded_length) { |
265 const int16_t* filter_coefficients; | 264 const int16_t* filter_coefficients; |
266 int num_coefficients; | 265 size_t num_coefficients; |
267 int decimation_factor = fs_hz_ / 4000; | 266 int decimation_factor = fs_hz_ / 4000; |
268 static const int kCompensateDelay = 0; | 267 static const size_t kCompensateDelay = 0; |
269 int length_limit = fs_hz_ / 100; // 10 ms in samples. | 268 size_t length_limit = static_cast<size_t>(fs_hz_ / 100); // 10 ms in samples. |
270 if (fs_hz_ == 8000) { | 269 if (fs_hz_ == 8000) { |
271 filter_coefficients = DspHelper::kDownsample8kHzTbl; | 270 filter_coefficients = DspHelper::kDownsample8kHzTbl; |
272 num_coefficients = 3; | 271 num_coefficients = 3; |
273 } else if (fs_hz_ == 16000) { | 272 } else if (fs_hz_ == 16000) { |
274 filter_coefficients = DspHelper::kDownsample16kHzTbl; | 273 filter_coefficients = DspHelper::kDownsample16kHzTbl; |
275 num_coefficients = 5; | 274 num_coefficients = 5; |
276 } else if (fs_hz_ == 32000) { | 275 } else if (fs_hz_ == 32000) { |
277 filter_coefficients = DspHelper::kDownsample32kHzTbl; | 276 filter_coefficients = DspHelper::kDownsample32kHzTbl; |
278 num_coefficients = 7; | 277 num_coefficients = 7; |
279 } else { // fs_hz_ == 48000 | 278 } else { // fs_hz_ == 48000 |
280 filter_coefficients = DspHelper::kDownsample48kHzTbl; | 279 filter_coefficients = DspHelper::kDownsample48kHzTbl; |
281 num_coefficients = 7; | 280 num_coefficients = 7; |
282 } | 281 } |
283 int signal_offset = num_coefficients - 1; | 282 size_t signal_offset = num_coefficients - 1; |
284 WebRtcSpl_DownsampleFast(&expanded_signal[signal_offset], | 283 WebRtcSpl_DownsampleFast(&expanded_signal[signal_offset], |
285 expanded_length - signal_offset, | 284 expanded_length - signal_offset, |
286 expanded_downsampled_, kExpandDownsampLength, | 285 expanded_downsampled_, kExpandDownsampLength, |
287 filter_coefficients, num_coefficients, | 286 filter_coefficients, num_coefficients, |
288 decimation_factor, kCompensateDelay); | 287 decimation_factor, kCompensateDelay); |
289 if (input_length <= length_limit) { | 288 if (input_length <= length_limit) { |
290 // Not quite long enough, so we have to cheat a bit. | 289 // Not quite long enough, so we have to cheat a bit. |
291 int16_t temp_len = input_length - signal_offset; | 290 size_t temp_len = input_length - signal_offset; |
292 // TODO(hlundin): Should |downsamp_temp_len| be corrected for round-off | 291 // TODO(hlundin): Should |downsamp_temp_len| be corrected for round-off |
293 // errors? I.e., (temp_len + decimation_factor - 1) / decimation_factor? | 292 // errors? I.e., (temp_len + decimation_factor - 1) / decimation_factor? |
294 int16_t downsamp_temp_len = temp_len / decimation_factor; | 293 size_t downsamp_temp_len = temp_len / decimation_factor; |
295 WebRtcSpl_DownsampleFast(&input[signal_offset], temp_len, | 294 WebRtcSpl_DownsampleFast(&input[signal_offset], temp_len, |
296 input_downsampled_, downsamp_temp_len, | 295 input_downsampled_, downsamp_temp_len, |
297 filter_coefficients, num_coefficients, | 296 filter_coefficients, num_coefficients, |
298 decimation_factor, kCompensateDelay); | 297 decimation_factor, kCompensateDelay); |
299 memset(&input_downsampled_[downsamp_temp_len], 0, | 298 memset(&input_downsampled_[downsamp_temp_len], 0, |
300 sizeof(int16_t) * (kInputDownsampLength - downsamp_temp_len)); | 299 sizeof(int16_t) * (kInputDownsampLength - downsamp_temp_len)); |
301 } else { | 300 } else { |
302 WebRtcSpl_DownsampleFast(&input[signal_offset], | 301 WebRtcSpl_DownsampleFast(&input[signal_offset], |
303 input_length - signal_offset, input_downsampled_, | 302 input_length - signal_offset, input_downsampled_, |
304 kInputDownsampLength, filter_coefficients, | 303 kInputDownsampLength, filter_coefficients, |
305 num_coefficients, decimation_factor, | 304 num_coefficients, decimation_factor, |
306 kCompensateDelay); | 305 kCompensateDelay); |
307 } | 306 } |
308 } | 307 } |
309 | 308 |
310 int16_t Merge::CorrelateAndPeakSearch(int16_t expanded_max, int16_t input_max, | 309 size_t Merge::CorrelateAndPeakSearch(int16_t expanded_max, int16_t input_max, |
311 int start_position, int input_length, | 310 size_t start_position, size_t input_length, |
312 int expand_period) const { | 311 size_t expand_period) const { |
313 // Calculate correlation without any normalization. | 312 // Calculate correlation without any normalization. |
314 const int max_corr_length = kMaxCorrelationLength; | 313 const size_t max_corr_length = kMaxCorrelationLength; |
315 int stop_position_downsamp = | 314 size_t stop_position_downsamp = |
316 std::min(max_corr_length, expand_->max_lag() / (fs_mult_ * 2) + 1); | 315 std::min(max_corr_length, expand_->max_lag() / (fs_mult_ * 2) + 1); |
317 int correlation_shift = 0; | 316 int correlation_shift = 0; |
318 if (expanded_max * input_max > 26843546) { | 317 if (expanded_max * input_max > 26843546) { |
319 correlation_shift = 3; | 318 correlation_shift = 3; |
320 } | 319 } |
321 | 320 |
322 int32_t correlation[kMaxCorrelationLength]; | 321 int32_t correlation[kMaxCorrelationLength]; |
323 WebRtcSpl_CrossCorrelation(correlation, input_downsampled_, | 322 WebRtcSpl_CrossCorrelation(correlation, input_downsampled_, |
324 expanded_downsampled_, kInputDownsampLength, | 323 expanded_downsampled_, kInputDownsampLength, |
325 stop_position_downsamp, correlation_shift, 1); | 324 stop_position_downsamp, correlation_shift, 1); |
326 | 325 |
327 // Normalize correlation to 14 bits and copy to a 16-bit array. | 326 // Normalize correlation to 14 bits and copy to a 16-bit array. |
328 const int pad_length = static_cast<int>(expand_->overlap_length() - 1); | 327 const size_t pad_length = expand_->overlap_length() - 1; |
329 const int correlation_buffer_size = 2 * pad_length + kMaxCorrelationLength; | 328 const size_t correlation_buffer_size = 2 * pad_length + kMaxCorrelationLength; |
330 rtc::scoped_ptr<int16_t[]> correlation16( | 329 rtc::scoped_ptr<int16_t[]> correlation16( |
331 new int16_t[correlation_buffer_size]); | 330 new int16_t[correlation_buffer_size]); |
332 memset(correlation16.get(), 0, correlation_buffer_size * sizeof(int16_t)); | 331 memset(correlation16.get(), 0, correlation_buffer_size * sizeof(int16_t)); |
333 int16_t* correlation_ptr = &correlation16[pad_length]; | 332 int16_t* correlation_ptr = &correlation16[pad_length]; |
334 int32_t max_correlation = WebRtcSpl_MaxAbsValueW32(correlation, | 333 int32_t max_correlation = WebRtcSpl_MaxAbsValueW32(correlation, |
335 stop_position_downsamp); | 334 stop_position_downsamp); |
336 int norm_shift = std::max(0, 17 - WebRtcSpl_NormW32(max_correlation)); | 335 int norm_shift = std::max(0, 17 - WebRtcSpl_NormW32(max_correlation)); |
337 WebRtcSpl_VectorBitShiftW32ToW16(correlation_ptr, stop_position_downsamp, | 336 WebRtcSpl_VectorBitShiftW32ToW16(correlation_ptr, stop_position_downsamp, |
338 correlation, norm_shift); | 337 correlation, norm_shift); |
339 | 338 |
340 // Calculate allowed starting point for peak finding. | 339 // Calculate allowed starting point for peak finding. |
341 // The peak location bestIndex must fulfill two criteria: | 340 // The peak location bestIndex must fulfill two criteria: |
342 // (1) w16_bestIndex + input_length < | 341 // (1) w16_bestIndex + input_length < |
343 // timestamps_per_call_ + expand_->overlap_length(); | 342 // timestamps_per_call_ + expand_->overlap_length(); |
344 // (2) w16_bestIndex + input_length < start_position. | 343 // (2) w16_bestIndex + input_length < start_position. |
345 int start_index = timestamps_per_call_ + | 344 size_t start_index = timestamps_per_call_ + expand_->overlap_length(); |
346 static_cast<int>(expand_->overlap_length()); | |
347 start_index = std::max(start_position, start_index); | 345 start_index = std::max(start_position, start_index); |
348 start_index = (input_length > start_index) ? 0 : (start_index - input_length); | 346 start_index = (input_length > start_index) ? 0 : (start_index - input_length); |
349 // Downscale starting index to 4kHz domain. (fs_mult_ * 2 = fs_hz_ / 4000.) | 347 // Downscale starting index to 4kHz domain. (fs_mult_ * 2 = fs_hz_ / 4000.) |
350 int start_index_downsamp = start_index / (fs_mult_ * 2); | 348 size_t start_index_downsamp = start_index / (fs_mult_ * 2); |
351 | 349 |
352 // Calculate a modified |stop_position_downsamp| to account for the increased | 350 // Calculate a modified |stop_position_downsamp| to account for the increased |
353 // start index |start_index_downsamp| and the effective array length. | 351 // start index |start_index_downsamp| and the effective array length. |
354 int modified_stop_pos = | 352 size_t modified_stop_pos = |
355 std::min(stop_position_downsamp, | 353 std::min(stop_position_downsamp, |
356 kMaxCorrelationLength + pad_length - start_index_downsamp); | 354 kMaxCorrelationLength + pad_length - start_index_downsamp); |
357 int best_correlation_index; | 355 size_t best_correlation_index; |
358 int16_t best_correlation; | 356 int16_t best_correlation; |
359 static const int kNumCorrelationCandidates = 1; | 357 static const size_t kNumCorrelationCandidates = 1; |
360 DspHelper::PeakDetection(&correlation_ptr[start_index_downsamp], | 358 DspHelper::PeakDetection(&correlation_ptr[start_index_downsamp], |
361 modified_stop_pos, kNumCorrelationCandidates, | 359 modified_stop_pos, kNumCorrelationCandidates, |
362 fs_mult_, &best_correlation_index, | 360 fs_mult_, &best_correlation_index, |
363 &best_correlation); | 361 &best_correlation); |
364 // Compensate for modified start index. | 362 // Compensate for modified start index. |
365 best_correlation_index += start_index; | 363 best_correlation_index += start_index; |
366 | 364 |
367 // Ensure that underrun does not occur for 10ms case => we have to get at | 365 // Ensure that underrun does not occur for 10ms case => we have to get at |
368 // least 10ms + overlap . (This should never happen thanks to the above | 366 // least 10ms + overlap . (This should never happen thanks to the above |
369 // modification of peak-finding starting point.) | 367 // modification of peak-finding starting point.) |
370 while (((best_correlation_index + input_length) < | 368 while (((best_correlation_index + input_length) < |
371 static_cast<int>(timestamps_per_call_ + expand_->overlap_length())) || | 369 (timestamps_per_call_ + expand_->overlap_length())) || |
372 ((best_correlation_index + input_length) < start_position)) { | 370 ((best_correlation_index + input_length) < start_position)) { |
373 assert(false); // Should never happen. | 371 assert(false); // Should never happen. |
374 best_correlation_index += expand_period; // Jump one lag ahead. | 372 best_correlation_index += expand_period; // Jump one lag ahead. |
375 } | 373 } |
376 return best_correlation_index; | 374 return best_correlation_index; |
377 } | 375 } |
378 | 376 |
379 int Merge::RequiredFutureSamples() { | 377 size_t Merge::RequiredFutureSamples() { |
380 return static_cast<int>(fs_hz_ / 100 * num_channels_); // 10 ms. | 378 return fs_hz_ / 100 * num_channels_; // 10 ms. |
381 } | 379 } |
382 | 380 |
383 | 381 |
384 } // namespace webrtc | 382 } // namespace webrtc |
OLD | NEW |