blob: 5bf239bfc53c8cda5e37ef203ea00a7c1d4de10a [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#include "modules/audio_coding/neteq/merge.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
13#include <assert.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <string.h> // memmove, memcpy, memset, size_t
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000015
16#include <algorithm> // min, max
kwiberg2d0c3322016-02-14 09:28:33 -080017#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "common_audio/signal_processing/include/signal_processing_library.h"
20#include "modules/audio_coding/neteq/audio_multi_vector.h"
21#include "modules/audio_coding/neteq/cross_correlation.h"
22#include "modules/audio_coding/neteq/dsp_helper.h"
23#include "modules/audio_coding/neteq/expand.h"
24#include "modules/audio_coding/neteq/sync_buffer.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010025#include "rtc_base/numerics/safe_conversions.h"
26#include "rtc_base/numerics/safe_minmax.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027
28namespace webrtc {
29
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020030Merge::Merge(int fs_hz,
31 size_t num_channels,
32 Expand* expand,
33 SyncBuffer* sync_buffer)
34 : fs_hz_(fs_hz),
35 num_channels_(num_channels),
36 fs_mult_(fs_hz_ / 8000),
Peter Kastingdce40cf2015-08-24 14:52:23 -070037 timestamps_per_call_(static_cast<size_t>(fs_hz_ / 100)),
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020038 expand_(expand),
39 sync_buffer_(sync_buffer),
40 expanded_(num_channels_) {
41 assert(num_channels_ > 0);
42}
43
minyue5bd33972016-05-02 04:46:11 -070044Merge::~Merge() = default;
45
Yves Gerey665174f2018-06-19 15:03:05 +020046size_t Merge::Process(int16_t* input,
47 size_t input_length,
Peter Kastingdce40cf2015-08-24 14:52:23 -070048 AudioMultiVector* output) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049 // TODO(hlundin): Change to an enumerator and skip assert.
Yves Gerey665174f2018-06-19 15:03:05 +020050 assert(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ == 32000 ||
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051 fs_hz_ == 48000);
52 assert(fs_hz_ <= kMaxSampleRate); // Should not be possible.
Ivo Creusenf65a0032020-12-03 10:06:25 +010053 if (input_length == 0) {
54 return 0;
55 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056
Peter Kastingdce40cf2015-08-24 14:52:23 -070057 size_t old_length;
58 size_t expand_period;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059 // Get expansion data to overlap and mix with.
Peter Kastingdce40cf2015-08-24 14:52:23 -070060 size_t expanded_length = GetExpandedSignal(&old_length, &expand_period);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061
62 // Transfer input signal to an AudioMultiVector.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000063 AudioMultiVector input_vector(num_channels_);
Henrik Lundin00eb12a2018-09-05 18:14:52 +020064 input_vector.PushBackInterleaved(
65 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066 size_t input_length_per_channel = input_vector.Size();
67 assert(input_length_per_channel == input_length / num_channels_);
68
Peter Kastingdce40cf2015-08-24 14:52:23 -070069 size_t best_correlation_index = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070 size_t output_length = 0;
71
minyue-webrtc79553cb2016-05-10 19:55:56 +020072 std::unique_ptr<int16_t[]> input_channel(
73 new int16_t[input_length_per_channel]);
74 std::unique_ptr<int16_t[]> expanded_channel(new int16_t[expanded_length]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075 for (size_t channel = 0; channel < num_channels_; ++channel) {
Yves Gerey665174f2018-06-19 15:03:05 +020076 input_vector[channel].CopyTo(input_length_per_channel, 0,
77 input_channel.get());
minyue-webrtc79553cb2016-05-10 19:55:56 +020078 expanded_[channel].CopyTo(expanded_length, 0, expanded_channel.get());
79
Henrik Lundin6dc82e82018-05-22 10:40:23 +020080 const int16_t new_mute_factor = std::min<int16_t>(
81 16384, SignalScaling(input_channel.get(), input_length_per_channel,
82 expanded_channel.get()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083
84 if (channel == 0) {
85 // Downsample, correlate, and find strongest correlation period for the
Henrik Lundin11b6f682020-06-29 12:17:42 +020086 // reference (i.e., first) channel only.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087 // Downsample to 4kHz sample rate.
minyue-webrtc79553cb2016-05-10 19:55:56 +020088 Downsample(input_channel.get(), input_length_per_channel,
89 expanded_channel.get(), expanded_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000090
91 // Calculate the lag of the strongest correlation period.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000092 best_correlation_index = CorrelateAndPeakSearch(
minyue53ff70f2016-05-02 01:50:30 -070093 old_length, input_length_per_channel, expand_period);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094 }
95
minyue5bd33972016-05-02 04:46:11 -070096 temp_data_.resize(input_length_per_channel + best_correlation_index);
97 int16_t* decoded_output = temp_data_.data() + best_correlation_index;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098
99 // Mute the new decoded data if needed (and unmute it linearly).
100 // This is the overlapping part of expanded_signal.
Yves Gerey665174f2018-06-19 15:03:05 +0200101 size_t interpolation_length =
102 std::min(kMaxCorrelationLength * fs_mult_,
103 expanded_length - best_correlation_index);
104 interpolation_length =
105 std::min(interpolation_length, input_length_per_channel);
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200106
107 RTC_DCHECK_LE(new_mute_factor, 16384);
108 int16_t mute_factor =
109 std::max(expand_->MuteFactor(channel), new_mute_factor);
110 RTC_DCHECK_GE(mute_factor, 0);
111
112 if (mute_factor < 16384) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 // Set a suitable muting slope (Q20). 0.004 for NB, 0.002 for WB,
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200114 // and so on, or as fast as it takes to come back to full gain within the
115 // frame length.
116 const int back_to_fullscale_inc = static_cast<int>(
117 ((16384 - mute_factor) << 6) / input_length_per_channel);
118 const int increment = std::max(4194 / fs_mult_, back_to_fullscale_inc);
119 mute_factor = static_cast<int16_t>(DspHelper::RampSignal(
120 input_channel.get(), interpolation_length, mute_factor, increment));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 DspHelper::UnmuteSignal(&input_channel[interpolation_length],
122 input_length_per_channel - interpolation_length,
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200123 &mute_factor, increment,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124 &decoded_output[interpolation_length]);
125 } else {
126 // No muting needed.
127 memmove(
128 &decoded_output[interpolation_length],
129 &input_channel[interpolation_length],
130 sizeof(int16_t) * (input_length_per_channel - interpolation_length));
131 }
132
133 // Do overlap and mix linearly.
Peter Kastingb7e50542015-06-11 12:55:50 -0700134 int16_t increment =
135 static_cast<int16_t>(16384 / (interpolation_length + 1)); // In Q14.
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200136 int16_t local_mute_factor = 16384 - increment;
minyue-webrtc79553cb2016-05-10 19:55:56 +0200137 memmove(temp_data_.data(), expanded_channel.get(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000138 sizeof(int16_t) * best_correlation_index);
139 DspHelper::CrossFade(&expanded_channel[best_correlation_index],
minyue-webrtc79553cb2016-05-10 19:55:56 +0200140 input_channel.get(), interpolation_length,
Henrik Lundin6dc82e82018-05-22 10:40:23 +0200141 &local_mute_factor, increment, decoded_output);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000142
143 output_length = best_correlation_index + input_length_per_channel;
144 if (channel == 0) {
145 assert(output->Empty()); // Output should be empty at this point.
146 output->AssertSize(output_length);
147 } else {
148 assert(output->Size() == output_length);
149 }
minyue-webrtc79553cb2016-05-10 19:55:56 +0200150 (*output)[channel].OverwriteAt(temp_data_.data(), output_length, 0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000151 }
152
153 // Copy back the first part of the data to |sync_buffer_| and remove it from
154 // |output|.
155 sync_buffer_->ReplaceAtIndex(*output, old_length, sync_buffer_->next_index());
156 output->PopFront(old_length);
157
158 // Return new added length. |old_length| samples were borrowed from
159 // |sync_buffer_|.
henrik.lundin2979f552017-05-05 05:04:16 -0700160 RTC_DCHECK_GE(output_length, old_length);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700161 return output_length - old_length;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162}
163
Peter Kastingdce40cf2015-08-24 14:52:23 -0700164size_t Merge::GetExpandedSignal(size_t* old_length, size_t* expand_period) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000165 // Check how much data that is left since earlier.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700166 *old_length = sync_buffer_->FutureLength();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 // Should never be less than overlap_length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700168 assert(*old_length >= expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000169 // Generate data to merge the overlap with using expand.
170 expand_->SetParametersForMergeAfterExpand();
171
172 if (*old_length >= 210 * kMaxSampleRate / 8000) {
173 // TODO(hlundin): Write test case for this.
174 // The number of samples available in the sync buffer is more than what fits
175 // in expanded_signal. Keep the first 210 * kMaxSampleRate / 8000 samples,
176 // but shift them towards the end of the buffer. This is ok, since all of
177 // the buffer will be expand data anyway, so as long as the beginning is
178 // left untouched, we're fine.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700179 size_t length_diff = *old_length - 210 * kMaxSampleRate / 8000;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 sync_buffer_->InsertZerosAtIndex(length_diff, sync_buffer_->next_index());
181 *old_length = 210 * kMaxSampleRate / 8000;
182 // This is the truncated length.
183 }
184 // This assert should always be true thanks to the if statement above.
Peter Kastingf045e4d2015-06-10 21:15:38 -0700185 assert(210 * kMaxSampleRate / 8000 >= *old_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000186
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +0000187 AudioMultiVector expanded_temp(num_channels_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000188 expand_->Process(&expanded_temp);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700189 *expand_period = expanded_temp.Size(); // Samples per channel.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190
191 expanded_.Clear();
192 // Copy what is left since earlier into the expanded vector.
193 expanded_.PushBackFromIndex(*sync_buffer_, sync_buffer_->next_index());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700194 assert(expanded_.Size() == *old_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000195 assert(expanded_temp.Size() > 0);
196 // Do "ugly" copy and paste from the expanded in order to generate more data
197 // to correlate (but not interpolate) with.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700198 const size_t required_length = static_cast<size_t>((120 + 80 + 2) * fs_mult_);
199 if (expanded_.Size() < required_length) {
200 while (expanded_.Size() < required_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201 // Append one more pitch period each time.
202 expanded_.PushBack(expanded_temp);
203 }
204 // Trim the length to exactly |required_length|.
205 expanded_.PopBack(expanded_.Size() - required_length);
206 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700207 assert(expanded_.Size() >= required_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000208 return required_length;
209}
210
Yves Gerey665174f2018-06-19 15:03:05 +0200211int16_t Merge::SignalScaling(const int16_t* input,
212 size_t input_length,
minyue53ff70f2016-05-02 01:50:30 -0700213 const int16_t* expanded_signal) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000214 // Adjust muting factor if new vector is more or less of the BGN energy.
kwiberg7885d3f2017-04-25 12:35:07 -0700215 const auto mod_input_length = rtc::SafeMin<size_t>(
216 64 * rtc::dchecked_cast<size_t>(fs_mult_), input_length);
minyue53ff70f2016-05-02 01:50:30 -0700217 const int16_t expanded_max =
218 WebRtcSpl_MaxAbsValueW16(expanded_signal, mod_input_length);
Yves Gerey665174f2018-06-19 15:03:05 +0200219 int32_t factor =
220 (expanded_max * expanded_max) / (std::numeric_limits<int32_t>::max() /
221 static_cast<int32_t>(mod_input_length));
minyue5bd33972016-05-02 04:46:11 -0700222 const int expanded_shift = factor == 0 ? 0 : 31 - WebRtcSpl_NormW32(factor);
Yves Gerey665174f2018-06-19 15:03:05 +0200223 int32_t energy_expanded = WebRtcSpl_DotProductWithScale(
224 expanded_signal, expanded_signal, mod_input_length, expanded_shift);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000225
226 // Calculate energy of input signal.
minyue5bd33972016-05-02 04:46:11 -0700227 const int16_t input_max = WebRtcSpl_MaxAbsValueW16(input, mod_input_length);
228 factor = (input_max * input_max) / (std::numeric_limits<int32_t>::max() /
Yves Gerey665174f2018-06-19 15:03:05 +0200229 static_cast<int32_t>(mod_input_length));
minyue5bd33972016-05-02 04:46:11 -0700230 const int input_shift = factor == 0 ? 0 : 31 - WebRtcSpl_NormW32(factor);
Yves Gerey665174f2018-06-19 15:03:05 +0200231 int32_t energy_input = WebRtcSpl_DotProductWithScale(
232 input, input, mod_input_length, input_shift);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000233
234 // Align to the same Q-domain.
235 if (input_shift > expanded_shift) {
236 energy_expanded = energy_expanded >> (input_shift - expanded_shift);
237 } else {
238 energy_input = energy_input >> (expanded_shift - input_shift);
239 }
240
241 // Calculate muting factor to use for new frame.
242 int16_t mute_factor;
243 if (energy_input > energy_expanded) {
244 // Normalize |energy_input| to 14 bits.
245 int16_t temp_shift = WebRtcSpl_NormW32(energy_input) - 17;
246 energy_input = WEBRTC_SPL_SHIFT_W32(energy_input, temp_shift);
247 // Put |energy_expanded| in a domain 14 higher, so that
248 // energy_expanded / energy_input is in Q14.
249 energy_expanded = WEBRTC_SPL_SHIFT_W32(energy_expanded, temp_shift + 14);
250 // Calculate sqrt(energy_expanded / energy_input) in Q14.
Peter Kastingb7e50542015-06-11 12:55:50 -0700251 mute_factor = static_cast<int16_t>(
252 WebRtcSpl_SqrtFloor((energy_expanded / energy_input) << 14));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000253 } else {
254 // Set to 1 (in Q14) when |expanded| has higher energy than |input|.
255 mute_factor = 16384;
256 }
257
258 return mute_factor;
259}
260
261// TODO(hlundin): There are some parameter values in this method that seem
262// strange. Compare with Expand::Correlation.
Yves Gerey665174f2018-06-19 15:03:05 +0200263void Merge::Downsample(const int16_t* input,
264 size_t input_length,
265 const int16_t* expanded_signal,
266 size_t expanded_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000267 const int16_t* filter_coefficients;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700268 size_t num_coefficients;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269 int decimation_factor = fs_hz_ / 4000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700270 static const size_t kCompensateDelay = 0;
271 size_t length_limit = static_cast<size_t>(fs_hz_ / 100); // 10 ms in samples.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 if (fs_hz_ == 8000) {
273 filter_coefficients = DspHelper::kDownsample8kHzTbl;
274 num_coefficients = 3;
275 } else if (fs_hz_ == 16000) {
276 filter_coefficients = DspHelper::kDownsample16kHzTbl;
277 num_coefficients = 5;
278 } else if (fs_hz_ == 32000) {
279 filter_coefficients = DspHelper::kDownsample32kHzTbl;
280 num_coefficients = 7;
281 } else { // fs_hz_ == 48000
282 filter_coefficients = DspHelper::kDownsample48kHzTbl;
283 num_coefficients = 7;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000284 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700285 size_t signal_offset = num_coefficients - 1;
Yves Gerey665174f2018-06-19 15:03:05 +0200286 WebRtcSpl_DownsampleFast(
287 &expanded_signal[signal_offset], expanded_length - signal_offset,
288 expanded_downsampled_, kExpandDownsampLength, filter_coefficients,
289 num_coefficients, decimation_factor, kCompensateDelay);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290 if (input_length <= length_limit) {
291 // Not quite long enough, so we have to cheat a bit.
Henrik Lundin80b28062019-11-25 10:21:00 +0100292 // If the input is shorter than the offset, we consider the input to be 0
293 // length. This will cause us to skip the downsampling since it makes no
294 // sense anyway, and input_downsampled_ will be filled with zeros. This is
295 // clearly a pathological case, and the signal quality will suffer, but
296 // there is not much we can do.
297 const size_t temp_len =
298 input_length > signal_offset ? input_length - signal_offset : 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000299 // TODO(hlundin): Should |downsamp_temp_len| be corrected for round-off
300 // errors? I.e., (temp_len + decimation_factor - 1) / decimation_factor?
Peter Kastingdce40cf2015-08-24 14:52:23 -0700301 size_t downsamp_temp_len = temp_len / decimation_factor;
Henrik Lundin80b28062019-11-25 10:21:00 +0100302 if (downsamp_temp_len > 0) {
303 WebRtcSpl_DownsampleFast(&input[signal_offset], temp_len,
304 input_downsampled_, downsamp_temp_len,
305 filter_coefficients, num_coefficients,
306 decimation_factor, kCompensateDelay);
307 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 memset(&input_downsampled_[downsamp_temp_len], 0,
309 sizeof(int16_t) * (kInputDownsampLength - downsamp_temp_len));
310 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200311 WebRtcSpl_DownsampleFast(
312 &input[signal_offset], input_length - signal_offset, input_downsampled_,
313 kInputDownsampLength, filter_coefficients, num_coefficients,
314 decimation_factor, kCompensateDelay);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315 }
316}
317
Yves Gerey665174f2018-06-19 15:03:05 +0200318size_t Merge::CorrelateAndPeakSearch(size_t start_position,
319 size_t input_length,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700320 size_t expand_period) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321 // Calculate correlation without any normalization.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700322 const size_t max_corr_length = kMaxCorrelationLength;
323 size_t stop_position_downsamp =
Peter Kasting728d9032015-06-11 14:31:38 -0700324 std::min(max_corr_length, expand_->max_lag() / (fs_mult_ * 2) + 1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325
326 int32_t correlation[kMaxCorrelationLength];
minyue53ff70f2016-05-02 01:50:30 -0700327 CrossCorrelationWithAutoShift(input_downsampled_, expanded_downsampled_,
328 kInputDownsampLength, stop_position_downsamp, 1,
329 correlation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330
331 // Normalize correlation to 14 bits and copy to a 16-bit array.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700332 const size_t pad_length = expand_->overlap_length() - 1;
333 const size_t correlation_buffer_size = 2 * pad_length + kMaxCorrelationLength;
kwiberg2d0c3322016-02-14 09:28:33 -0800334 std::unique_ptr<int16_t[]> correlation16(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000335 new int16_t[correlation_buffer_size]);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000336 memset(correlation16.get(), 0, correlation_buffer_size * sizeof(int16_t));
337 int16_t* correlation_ptr = &correlation16[pad_length];
Yves Gerey665174f2018-06-19 15:03:05 +0200338 int32_t max_correlation =
339 WebRtcSpl_MaxAbsValueW32(correlation, stop_position_downsamp);
Peter Kasting36b7cc32015-06-11 19:57:18 -0700340 int norm_shift = std::max(0, 17 - WebRtcSpl_NormW32(max_correlation));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 WebRtcSpl_VectorBitShiftW32ToW16(correlation_ptr, stop_position_downsamp,
342 correlation, norm_shift);
343
344 // Calculate allowed starting point for peak finding.
345 // The peak location bestIndex must fulfill two criteria:
346 // (1) w16_bestIndex + input_length <
347 // timestamps_per_call_ + expand_->overlap_length();
348 // (2) w16_bestIndex + input_length < start_position.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700349 size_t start_index = timestamps_per_call_ + expand_->overlap_length();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 start_index = std::max(start_position, start_index);
Peter Kastingf045e4d2015-06-10 21:15:38 -0700351 start_index = (input_length > start_index) ? 0 : (start_index - input_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 // Downscale starting index to 4kHz domain. (fs_mult_ * 2 = fs_hz_ / 4000.)
Peter Kastingdce40cf2015-08-24 14:52:23 -0700353 size_t start_index_downsamp = start_index / (fs_mult_ * 2);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354
355 // Calculate a modified |stop_position_downsamp| to account for the increased
356 // start index |start_index_downsamp| and the effective array length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700357 size_t modified_stop_pos =
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000358 std::min(stop_position_downsamp,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000359 kMaxCorrelationLength + pad_length - start_index_downsamp);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700360 size_t best_correlation_index;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000361 int16_t best_correlation;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700362 static const size_t kNumCorrelationCandidates = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363 DspHelper::PeakDetection(&correlation_ptr[start_index_downsamp],
364 modified_stop_pos, kNumCorrelationCandidates,
365 fs_mult_, &best_correlation_index,
366 &best_correlation);
367 // Compensate for modified start index.
368 best_correlation_index += start_index;
369
370 // Ensure that underrun does not occur for 10ms case => we have to get at
371 // least 10ms + overlap . (This should never happen thanks to the above
372 // modification of peak-finding starting point.)
Peter Kasting728d9032015-06-11 14:31:38 -0700373 while (((best_correlation_index + input_length) <
Peter Kastingdce40cf2015-08-24 14:52:23 -0700374 (timestamps_per_call_ + expand_->overlap_length())) ||
375 ((best_correlation_index + input_length) < start_position)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200376 assert(false); // Should never happen.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 best_correlation_index += expand_period; // Jump one lag ahead.
378 }
379 return best_correlation_index;
380}
381
Peter Kastingdce40cf2015-08-24 14:52:23 -0700382size_t Merge::RequiredFutureSamples() {
383 return fs_hz_ / 100 * num_channels_; // 10 ms.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000384}
385
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386} // namespace webrtc