blob: 9efbe4fb9c613ae2c0b92e300a4a1e9012ed4053 [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#include "webrtc/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
kwiberg7885d3f2017-04-25 12:35:07 -070019#include "webrtc/base/safe_conversions.h"
20#include "webrtc/base/safe_minmax.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000022#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
minyue53ff70f2016-05-02 01:50:30 -070023#include "webrtc/modules/audio_coding/neteq/cross_correlation.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000024#include "webrtc/modules/audio_coding/neteq/dsp_helper.h"
25#include "webrtc/modules/audio_coding/neteq/expand.h"
26#include "webrtc/modules/audio_coding/neteq/sync_buffer.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
Peter Kastingdce40cf2015-08-24 14:52:23 -070046size_t Merge::Process(int16_t* input, size_t input_length,
47 int16_t* external_mute_factor_array,
48 AudioMultiVector* output) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049 // TODO(hlundin): Change to an enumerator and skip assert.
50 assert(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ == 32000 ||
51 fs_hz_ == 48000);
52 assert(fs_hz_ <= kMaxSampleRate); // Should not be possible.
53
Peter Kastingdce40cf2015-08-24 14:52:23 -070054 size_t old_length;
55 size_t expand_period;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056 // Get expansion data to overlap and mix with.
Peter Kastingdce40cf2015-08-24 14:52:23 -070057 size_t expanded_length = GetExpandedSignal(&old_length, &expand_period);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058
59 // Transfer input signal to an AudioMultiVector.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000060 AudioMultiVector input_vector(num_channels_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 input_vector.PushBackInterleaved(input, input_length);
62 size_t input_length_per_channel = input_vector.Size();
63 assert(input_length_per_channel == input_length / num_channels_);
64
Peter Kastingdce40cf2015-08-24 14:52:23 -070065 size_t best_correlation_index = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066 size_t output_length = 0;
67
minyue-webrtc79553cb2016-05-10 19:55:56 +020068 std::unique_ptr<int16_t[]> input_channel(
69 new int16_t[input_length_per_channel]);
70 std::unique_ptr<int16_t[]> expanded_channel(new int16_t[expanded_length]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071 for (size_t channel = 0; channel < num_channels_; ++channel) {
minyue-webrtc79553cb2016-05-10 19:55:56 +020072 input_vector[channel].CopyTo(
73 input_length_per_channel, 0, input_channel.get());
74 expanded_[channel].CopyTo(expanded_length, 0, expanded_channel.get());
75
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000076 int16_t new_mute_factor = SignalScaling(
minyue-webrtc79553cb2016-05-10 19:55:56 +020077 input_channel.get(), input_length_per_channel, expanded_channel.get());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078
79 // Adjust muting factor (product of "main" muting factor and expand muting
80 // factor).
81 int16_t* external_mute_factor = &external_mute_factor_array[channel];
82 *external_mute_factor =
83 (*external_mute_factor * expand_->MuteFactor(channel)) >> 14;
84
85 // Update |external_mute_factor| if it is lower than |new_mute_factor|.
86 if (new_mute_factor > *external_mute_factor) {
87 *external_mute_factor = std::min(new_mute_factor,
88 static_cast<int16_t>(16384));
89 }
90
91 if (channel == 0) {
92 // Downsample, correlate, and find strongest correlation period for the
93 // master (i.e., first) channel only.
94 // Downsample to 4kHz sample rate.
minyue-webrtc79553cb2016-05-10 19:55:56 +020095 Downsample(input_channel.get(), input_length_per_channel,
96 expanded_channel.get(), expanded_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097
98 // Calculate the lag of the strongest correlation period.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000099 best_correlation_index = CorrelateAndPeakSearch(
minyue53ff70f2016-05-02 01:50:30 -0700100 old_length, input_length_per_channel, expand_period);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101 }
102
minyue5bd33972016-05-02 04:46:11 -0700103 temp_data_.resize(input_length_per_channel + best_correlation_index);
104 int16_t* decoded_output = temp_data_.data() + best_correlation_index;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105
106 // Mute the new decoded data if needed (and unmute it linearly).
107 // This is the overlapping part of expanded_signal.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700108 size_t interpolation_length = std::min(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109 kMaxCorrelationLength * fs_mult_,
110 expanded_length - best_correlation_index);
111 interpolation_length = std::min(interpolation_length,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700112 input_length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 if (*external_mute_factor < 16384) {
114 // Set a suitable muting slope (Q20). 0.004 for NB, 0.002 for WB,
115 // and so on.
116 int increment = 4194 / fs_mult_;
Peter Kastingb7e50542015-06-11 12:55:50 -0700117 *external_mute_factor =
minyue-webrtc79553cb2016-05-10 19:55:56 +0200118 static_cast<int16_t>(DspHelper::RampSignal(input_channel.get(),
Peter Kastingb7e50542015-06-11 12:55:50 -0700119 interpolation_length,
120 *external_mute_factor,
121 increment));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122 DspHelper::UnmuteSignal(&input_channel[interpolation_length],
123 input_length_per_channel - interpolation_length,
124 external_mute_factor, increment,
125 &decoded_output[interpolation_length]);
126 } else {
127 // No muting needed.
128 memmove(
129 &decoded_output[interpolation_length],
130 &input_channel[interpolation_length],
131 sizeof(int16_t) * (input_length_per_channel - interpolation_length));
132 }
133
134 // Do overlap and mix linearly.
Peter Kastingb7e50542015-06-11 12:55:50 -0700135 int16_t increment =
136 static_cast<int16_t>(16384 / (interpolation_length + 1)); // In Q14.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000137 int16_t mute_factor = 16384 - increment;
minyue-webrtc79553cb2016-05-10 19:55:56 +0200138 memmove(temp_data_.data(), expanded_channel.get(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000139 sizeof(int16_t) * best_correlation_index);
140 DspHelper::CrossFade(&expanded_channel[best_correlation_index],
minyue-webrtc79553cb2016-05-10 19:55:56 +0200141 input_channel.get(), interpolation_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000142 &mute_factor, increment, decoded_output);
143
144 output_length = best_correlation_index + input_length_per_channel;
145 if (channel == 0) {
146 assert(output->Empty()); // Output should be empty at this point.
147 output->AssertSize(output_length);
148 } else {
149 assert(output->Size() == output_length);
150 }
minyue-webrtc79553cb2016-05-10 19:55:56 +0200151 (*output)[channel].OverwriteAt(temp_data_.data(), output_length, 0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000152 }
153
154 // Copy back the first part of the data to |sync_buffer_| and remove it from
155 // |output|.
156 sync_buffer_->ReplaceAtIndex(*output, old_length, sync_buffer_->next_index());
157 output->PopFront(old_length);
158
159 // Return new added length. |old_length| samples were borrowed from
160 // |sync_buffer_|.
henrik.lundin2979f552017-05-05 05:04:16 -0700161 RTC_DCHECK_GE(output_length, old_length);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700162 return output_length - old_length;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000163}
164
Peter Kastingdce40cf2015-08-24 14:52:23 -0700165size_t Merge::GetExpandedSignal(size_t* old_length, size_t* expand_period) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166 // Check how much data that is left since earlier.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700167 *old_length = sync_buffer_->FutureLength();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000168 // Should never be less than overlap_length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700169 assert(*old_length >= expand_->overlap_length());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170 // Generate data to merge the overlap with using expand.
171 expand_->SetParametersForMergeAfterExpand();
172
173 if (*old_length >= 210 * kMaxSampleRate / 8000) {
174 // TODO(hlundin): Write test case for this.
175 // The number of samples available in the sync buffer is more than what fits
176 // in expanded_signal. Keep the first 210 * kMaxSampleRate / 8000 samples,
177 // but shift them towards the end of the buffer. This is ok, since all of
178 // the buffer will be expand data anyway, so as long as the beginning is
179 // left untouched, we're fine.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700180 size_t length_diff = *old_length - 210 * kMaxSampleRate / 8000;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181 sync_buffer_->InsertZerosAtIndex(length_diff, sync_buffer_->next_index());
182 *old_length = 210 * kMaxSampleRate / 8000;
183 // This is the truncated length.
184 }
185 // This assert should always be true thanks to the if statement above.
Peter Kastingf045e4d2015-06-10 21:15:38 -0700186 assert(210 * kMaxSampleRate / 8000 >= *old_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +0000188 AudioMultiVector expanded_temp(num_channels_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189 expand_->Process(&expanded_temp);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700190 *expand_period = expanded_temp.Size(); // Samples per channel.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191
192 expanded_.Clear();
193 // Copy what is left since earlier into the expanded vector.
194 expanded_.PushBackFromIndex(*sync_buffer_, sync_buffer_->next_index());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700195 assert(expanded_.Size() == *old_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000196 assert(expanded_temp.Size() > 0);
197 // Do "ugly" copy and paste from the expanded in order to generate more data
198 // to correlate (but not interpolate) with.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700199 const size_t required_length = static_cast<size_t>((120 + 80 + 2) * fs_mult_);
200 if (expanded_.Size() < required_length) {
201 while (expanded_.Size() < required_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202 // Append one more pitch period each time.
203 expanded_.PushBack(expanded_temp);
204 }
205 // Trim the length to exactly |required_length|.
206 expanded_.PopBack(expanded_.Size() - required_length);
207 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700208 assert(expanded_.Size() >= required_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000209 return required_length;
210}
211
Peter Kastingdce40cf2015-08-24 14:52:23 -0700212int16_t Merge::SignalScaling(const int16_t* input, 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);
minyue5bd33972016-05-02 04:46:11 -0700219 int32_t factor = (expanded_max * expanded_max) /
220 (std::numeric_limits<int32_t>::max() /
221 static_cast<int32_t>(mod_input_length));
222 const int expanded_shift = factor == 0 ? 0 : 31 - WebRtcSpl_NormW32(factor);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000223 int32_t energy_expanded = WebRtcSpl_DotProductWithScale(expanded_signal,
224 expanded_signal,
225 mod_input_length,
226 expanded_shift);
227
228 // Calculate energy of input signal.
minyue5bd33972016-05-02 04:46:11 -0700229 const int16_t input_max = WebRtcSpl_MaxAbsValueW16(input, mod_input_length);
230 factor = (input_max * input_max) / (std::numeric_limits<int32_t>::max() /
231 static_cast<int32_t>(mod_input_length));
232 const int input_shift = factor == 0 ? 0 : 31 - WebRtcSpl_NormW32(factor);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000233 int32_t energy_input = WebRtcSpl_DotProductWithScale(input, input,
234 mod_input_length,
235 input_shift);
236
237 // Align to the same Q-domain.
238 if (input_shift > expanded_shift) {
239 energy_expanded = energy_expanded >> (input_shift - expanded_shift);
240 } else {
241 energy_input = energy_input >> (expanded_shift - input_shift);
242 }
243
244 // Calculate muting factor to use for new frame.
245 int16_t mute_factor;
246 if (energy_input > energy_expanded) {
247 // Normalize |energy_input| to 14 bits.
248 int16_t temp_shift = WebRtcSpl_NormW32(energy_input) - 17;
249 energy_input = WEBRTC_SPL_SHIFT_W32(energy_input, temp_shift);
250 // Put |energy_expanded| in a domain 14 higher, so that
251 // energy_expanded / energy_input is in Q14.
252 energy_expanded = WEBRTC_SPL_SHIFT_W32(energy_expanded, temp_shift + 14);
253 // Calculate sqrt(energy_expanded / energy_input) in Q14.
Peter Kastingb7e50542015-06-11 12:55:50 -0700254 mute_factor = static_cast<int16_t>(
255 WebRtcSpl_SqrtFloor((energy_expanded / energy_input) << 14));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256 } else {
257 // Set to 1 (in Q14) when |expanded| has higher energy than |input|.
258 mute_factor = 16384;
259 }
260
261 return mute_factor;
262}
263
264// TODO(hlundin): There are some parameter values in this method that seem
265// strange. Compare with Expand::Correlation.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700266void Merge::Downsample(const int16_t* input, size_t input_length,
267 const int16_t* expanded_signal, size_t expanded_length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 const int16_t* filter_coefficients;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700269 size_t num_coefficients;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000270 int decimation_factor = fs_hz_ / 4000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700271 static const size_t kCompensateDelay = 0;
272 size_t length_limit = static_cast<size_t>(fs_hz_ / 100); // 10 ms in samples.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273 if (fs_hz_ == 8000) {
274 filter_coefficients = DspHelper::kDownsample8kHzTbl;
275 num_coefficients = 3;
276 } else if (fs_hz_ == 16000) {
277 filter_coefficients = DspHelper::kDownsample16kHzTbl;
278 num_coefficients = 5;
279 } else if (fs_hz_ == 32000) {
280 filter_coefficients = DspHelper::kDownsample32kHzTbl;
281 num_coefficients = 7;
282 } else { // fs_hz_ == 48000
283 filter_coefficients = DspHelper::kDownsample48kHzTbl;
284 num_coefficients = 7;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700286 size_t signal_offset = num_coefficients - 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000287 WebRtcSpl_DownsampleFast(&expanded_signal[signal_offset],
288 expanded_length - signal_offset,
289 expanded_downsampled_, kExpandDownsampLength,
290 filter_coefficients, num_coefficients,
291 decimation_factor, kCompensateDelay);
292 if (input_length <= length_limit) {
293 // Not quite long enough, so we have to cheat a bit.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700294 size_t temp_len = input_length - signal_offset;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 // TODO(hlundin): Should |downsamp_temp_len| be corrected for round-off
296 // errors? I.e., (temp_len + decimation_factor - 1) / decimation_factor?
Peter Kastingdce40cf2015-08-24 14:52:23 -0700297 size_t downsamp_temp_len = temp_len / decimation_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 WebRtcSpl_DownsampleFast(&input[signal_offset], temp_len,
299 input_downsampled_, downsamp_temp_len,
300 filter_coefficients, num_coefficients,
301 decimation_factor, kCompensateDelay);
302 memset(&input_downsampled_[downsamp_temp_len], 0,
303 sizeof(int16_t) * (kInputDownsampLength - downsamp_temp_len));
304 } else {
305 WebRtcSpl_DownsampleFast(&input[signal_offset],
306 input_length - signal_offset, input_downsampled_,
307 kInputDownsampLength, filter_coefficients,
308 num_coefficients, decimation_factor,
309 kCompensateDelay);
310 }
311}
312
minyue53ff70f2016-05-02 01:50:30 -0700313size_t Merge::CorrelateAndPeakSearch(size_t start_position, size_t input_length,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700314 size_t expand_period) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315 // Calculate correlation without any normalization.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700316 const size_t max_corr_length = kMaxCorrelationLength;
317 size_t stop_position_downsamp =
Peter Kasting728d9032015-06-11 14:31:38 -0700318 std::min(max_corr_length, expand_->max_lag() / (fs_mult_ * 2) + 1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319
320 int32_t correlation[kMaxCorrelationLength];
minyue53ff70f2016-05-02 01:50:30 -0700321 CrossCorrelationWithAutoShift(input_downsampled_, expanded_downsampled_,
322 kInputDownsampLength, stop_position_downsamp, 1,
323 correlation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000324
325 // Normalize correlation to 14 bits and copy to a 16-bit array.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700326 const size_t pad_length = expand_->overlap_length() - 1;
327 const size_t correlation_buffer_size = 2 * pad_length + kMaxCorrelationLength;
kwiberg2d0c3322016-02-14 09:28:33 -0800328 std::unique_ptr<int16_t[]> correlation16(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000329 new int16_t[correlation_buffer_size]);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000330 memset(correlation16.get(), 0, correlation_buffer_size * sizeof(int16_t));
331 int16_t* correlation_ptr = &correlation16[pad_length];
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 int32_t max_correlation = WebRtcSpl_MaxAbsValueW32(correlation,
333 stop_position_downsamp);
Peter Kasting36b7cc32015-06-11 19:57:18 -0700334 int norm_shift = std::max(0, 17 - WebRtcSpl_NormW32(max_correlation));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335 WebRtcSpl_VectorBitShiftW32ToW16(correlation_ptr, stop_position_downsamp,
336 correlation, norm_shift);
337
338 // Calculate allowed starting point for peak finding.
339 // The peak location bestIndex must fulfill two criteria:
340 // (1) w16_bestIndex + input_length <
341 // timestamps_per_call_ + expand_->overlap_length();
342 // (2) w16_bestIndex + input_length < start_position.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700343 size_t start_index = timestamps_per_call_ + expand_->overlap_length();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344 start_index = std::max(start_position, start_index);
Peter Kastingf045e4d2015-06-10 21:15:38 -0700345 start_index = (input_length > start_index) ? 0 : (start_index - input_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346 // Downscale starting index to 4kHz domain. (fs_mult_ * 2 = fs_hz_ / 4000.)
Peter Kastingdce40cf2015-08-24 14:52:23 -0700347 size_t start_index_downsamp = start_index / (fs_mult_ * 2);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000348
349 // Calculate a modified |stop_position_downsamp| to account for the increased
350 // start index |start_index_downsamp| and the effective array length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700351 size_t modified_stop_pos =
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 std::min(stop_position_downsamp,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000353 kMaxCorrelationLength + pad_length - start_index_downsamp);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700354 size_t best_correlation_index;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 int16_t best_correlation;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700356 static const size_t kNumCorrelationCandidates = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 DspHelper::PeakDetection(&correlation_ptr[start_index_downsamp],
358 modified_stop_pos, kNumCorrelationCandidates,
359 fs_mult_, &best_correlation_index,
360 &best_correlation);
361 // Compensate for modified start index.
362 best_correlation_index += start_index;
363
364 // Ensure that underrun does not occur for 10ms case => we have to get at
365 // least 10ms + overlap . (This should never happen thanks to the above
366 // modification of peak-finding starting point.)
Peter Kasting728d9032015-06-11 14:31:38 -0700367 while (((best_correlation_index + input_length) <
Peter Kastingdce40cf2015-08-24 14:52:23 -0700368 (timestamps_per_call_ + expand_->overlap_length())) ||
369 ((best_correlation_index + input_length) < start_position)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000370 assert(false); // Should never happen.
371 best_correlation_index += expand_period; // Jump one lag ahead.
372 }
373 return best_correlation_index;
374}
375
Peter Kastingdce40cf2015-08-24 14:52:23 -0700376size_t Merge::RequiredFutureSamples() {
377 return fs_hz_ / 100 * num_channels_; // 10 ms.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000378}
379
380
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381} // namespace webrtc