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 #include "modules/audio_coding/neteq/merge.h"
12
13 #include <string.h> // memmove, memcpy, memset, size_t
14
15 #include <algorithm> // min, max
16 #include <memory>
17
18 #include "common_audio/signal_processing/include/signal_processing_library.h"
19 #include "modules/audio_coding/neteq/audio_multi_vector.h"
20 #include "modules/audio_coding/neteq/cross_correlation.h"
21 #include "modules/audio_coding/neteq/dsp_helper.h"
22 #include "modules/audio_coding/neteq/expand.h"
23 #include "modules/audio_coding/neteq/sync_buffer.h"
24 #include "rtc_base/numerics/safe_conversions.h"
25 #include "rtc_base/numerics/safe_minmax.h"
26
27 namespace webrtc {
28
Merge(int fs_hz,size_t num_channels,Expand * expand,SyncBuffer * sync_buffer)29 Merge::Merge(int fs_hz,
30 size_t num_channels,
31 Expand* expand,
32 SyncBuffer* sync_buffer)
33 : fs_hz_(fs_hz),
34 num_channels_(num_channels),
35 fs_mult_(fs_hz_ / 8000),
36 timestamps_per_call_(static_cast<size_t>(fs_hz_ / 100)),
37 expand_(expand),
38 sync_buffer_(sync_buffer),
39 expanded_(num_channels_) {
40 RTC_DCHECK_GT(num_channels_, 0);
41 }
42
43 Merge::~Merge() = default;
44
Process(int16_t * input,size_t input_length,AudioMultiVector * output)45 size_t Merge::Process(int16_t* input,
46 size_t input_length,
47 AudioMultiVector* output) {
48 // TODO(hlundin): Change to an enumerator and skip assert.
49 RTC_DCHECK(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ == 32000 ||
50 fs_hz_ == 48000);
51 RTC_DCHECK_LE(fs_hz_, kMaxSampleRate); // Should not be possible.
52 if (input_length == 0) {
53 return 0;
54 }
55
56 size_t old_length;
57 size_t expand_period;
58 // Get expansion data to overlap and mix with.
59 size_t expanded_length = GetExpandedSignal(&old_length, &expand_period);
60
61 // Transfer input signal to an AudioMultiVector.
62 AudioMultiVector input_vector(num_channels_);
63 input_vector.PushBackInterleaved(
64 rtc::ArrayView<const int16_t>(input, input_length));
65 size_t input_length_per_channel = input_vector.Size();
66 RTC_DCHECK_EQ(input_length_per_channel, input_length / num_channels_);
67
68 size_t best_correlation_index = 0;
69 size_t output_length = 0;
70
71 std::unique_ptr<int16_t[]> input_channel(
72 new int16_t[input_length_per_channel]);
73 std::unique_ptr<int16_t[]> expanded_channel(new int16_t[expanded_length]);
74 for (size_t channel = 0; channel < num_channels_; ++channel) {
75 input_vector[channel].CopyTo(input_length_per_channel, 0,
76 input_channel.get());
77 expanded_[channel].CopyTo(expanded_length, 0, expanded_channel.get());
78
79 const int16_t new_mute_factor = std::min<int16_t>(
80 16384, SignalScaling(input_channel.get(), input_length_per_channel,
81 expanded_channel.get()));
82
83 if (channel == 0) {
84 // Downsample, correlate, and find strongest correlation period for the
85 // reference (i.e., first) channel only.
86 // Downsample to 4kHz sample rate.
87 Downsample(input_channel.get(), input_length_per_channel,
88 expanded_channel.get(), expanded_length);
89
90 // Calculate the lag of the strongest correlation period.
91 best_correlation_index = CorrelateAndPeakSearch(
92 old_length, input_length_per_channel, expand_period);
93 }
94
95 temp_data_.resize(input_length_per_channel + best_correlation_index);
96 int16_t* decoded_output = temp_data_.data() + best_correlation_index;
97
98 // Mute the new decoded data if needed (and unmute it linearly).
99 // This is the overlapping part of expanded_signal.
100 size_t interpolation_length =
101 std::min(kMaxCorrelationLength * fs_mult_,
102 expanded_length - best_correlation_index);
103 interpolation_length =
104 std::min(interpolation_length, input_length_per_channel);
105
106 RTC_DCHECK_LE(new_mute_factor, 16384);
107 int16_t mute_factor =
108 std::max(expand_->MuteFactor(channel), new_mute_factor);
109 RTC_DCHECK_GE(mute_factor, 0);
110
111 if (mute_factor < 16384) {
112 // Set a suitable muting slope (Q20). 0.004 for NB, 0.002 for WB,
113 // and so on, or as fast as it takes to come back to full gain within the
114 // frame length.
115 const int back_to_fullscale_inc = static_cast<int>(
116 ((16384 - mute_factor) << 6) / input_length_per_channel);
117 const int increment = std::max(4194 / fs_mult_, back_to_fullscale_inc);
118 mute_factor = static_cast<int16_t>(DspHelper::RampSignal(
119 input_channel.get(), interpolation_length, mute_factor, increment));
120 DspHelper::UnmuteSignal(&input_channel[interpolation_length],
121 input_length_per_channel - interpolation_length,
122 &mute_factor, increment,
123 &decoded_output[interpolation_length]);
124 } else {
125 // No muting needed.
126 memmove(
127 &decoded_output[interpolation_length],
128 &input_channel[interpolation_length],
129 sizeof(int16_t) * (input_length_per_channel - interpolation_length));
130 }
131
132 // Do overlap and mix linearly.
133 int16_t increment =
134 static_cast<int16_t>(16384 / (interpolation_length + 1)); // In Q14.
135 int16_t local_mute_factor = 16384 - increment;
136 memmove(temp_data_.data(), expanded_channel.get(),
137 sizeof(int16_t) * best_correlation_index);
138 DspHelper::CrossFade(&expanded_channel[best_correlation_index],
139 input_channel.get(), interpolation_length,
140 &local_mute_factor, increment, decoded_output);
141
142 output_length = best_correlation_index + input_length_per_channel;
143 if (channel == 0) {
144 RTC_DCHECK(output->Empty()); // Output should be empty at this point.
145 output->AssertSize(output_length);
146 } else {
147 RTC_DCHECK_EQ(output->Size(), output_length);
148 }
149 (*output)[channel].OverwriteAt(temp_data_.data(), output_length, 0);
150 }
151
152 // Copy back the first part of the data to `sync_buffer_` and remove it from
153 // `output`.
154 sync_buffer_->ReplaceAtIndex(*output, old_length, sync_buffer_->next_index());
155 output->PopFront(old_length);
156
157 // Return new added length. `old_length` samples were borrowed from
158 // `sync_buffer_`.
159 RTC_DCHECK_GE(output_length, old_length);
160 return output_length - old_length;
161 }
162
GetExpandedSignal(size_t * old_length,size_t * expand_period)163 size_t Merge::GetExpandedSignal(size_t* old_length, size_t* expand_period) {
164 // Check how much data that is left since earlier.
165 *old_length = sync_buffer_->FutureLength();
166 // Should never be less than overlap_length.
167 RTC_DCHECK_GE(*old_length, expand_->overlap_length());
168 // Generate data to merge the overlap with using expand.
169 expand_->SetParametersForMergeAfterExpand();
170
171 if (*old_length >= 210 * kMaxSampleRate / 8000) {
172 // TODO(hlundin): Write test case for this.
173 // The number of samples available in the sync buffer is more than what fits
174 // in expanded_signal. Keep the first 210 * kMaxSampleRate / 8000 samples,
175 // but shift them towards the end of the buffer. This is ok, since all of
176 // the buffer will be expand data anyway, so as long as the beginning is
177 // left untouched, we're fine.
178 size_t length_diff = *old_length - 210 * kMaxSampleRate / 8000;
179 sync_buffer_->InsertZerosAtIndex(length_diff, sync_buffer_->next_index());
180 *old_length = 210 * kMaxSampleRate / 8000;
181 // This is the truncated length.
182 }
183 // This assert should always be true thanks to the if statement above.
184 RTC_DCHECK_GE(210 * kMaxSampleRate / 8000, *old_length);
185
186 AudioMultiVector expanded_temp(num_channels_);
187 expand_->Process(&expanded_temp);
188 *expand_period = expanded_temp.Size(); // Samples per channel.
189
190 expanded_.Clear();
191 // Copy what is left since earlier into the expanded vector.
192 expanded_.PushBackFromIndex(*sync_buffer_, sync_buffer_->next_index());
193 RTC_DCHECK_EQ(expanded_.Size(), *old_length);
194 RTC_DCHECK_GT(expanded_temp.Size(), 0);
195 // Do "ugly" copy and paste from the expanded in order to generate more data
196 // to correlate (but not interpolate) with.
197 const size_t required_length = static_cast<size_t>((120 + 80 + 2) * fs_mult_);
198 if (expanded_.Size() < required_length) {
199 while (expanded_.Size() < required_length) {
200 // Append one more pitch period each time.
201 expanded_.PushBack(expanded_temp);
202 }
203 // Trim the length to exactly `required_length`.
204 expanded_.PopBack(expanded_.Size() - required_length);
205 }
206 RTC_DCHECK_GE(expanded_.Size(), required_length);
207 return required_length;
208 }
209
SignalScaling(const int16_t * input,size_t input_length,const int16_t * expanded_signal) const210 int16_t Merge::SignalScaling(const int16_t* input,
211 size_t input_length,
212 const int16_t* expanded_signal) const {
213 // Adjust muting factor if new vector is more or less of the BGN energy.
214 const auto mod_input_length = rtc::SafeMin<size_t>(
215 64 * rtc::dchecked_cast<size_t>(fs_mult_), input_length);
216 const int16_t expanded_max =
217 WebRtcSpl_MaxAbsValueW16(expanded_signal, mod_input_length);
218 int32_t factor =
219 (expanded_max * expanded_max) / (std::numeric_limits<int32_t>::max() /
220 static_cast<int32_t>(mod_input_length));
221 const int expanded_shift = factor == 0 ? 0 : 31 - WebRtcSpl_NormW32(factor);
222 int32_t energy_expanded = WebRtcSpl_DotProductWithScale(
223 expanded_signal, expanded_signal, mod_input_length, expanded_shift);
224
225 // Calculate energy of input signal.
226 const int16_t input_max = WebRtcSpl_MaxAbsValueW16(input, mod_input_length);
227 factor = (input_max * input_max) / (std::numeric_limits<int32_t>::max() /
228 static_cast<int32_t>(mod_input_length));
229 const int input_shift = factor == 0 ? 0 : 31 - WebRtcSpl_NormW32(factor);
230 int32_t energy_input = WebRtcSpl_DotProductWithScale(
231 input, input, mod_input_length, input_shift);
232
233 // Align to the same Q-domain.
234 if (input_shift > expanded_shift) {
235 energy_expanded = energy_expanded >> (input_shift - expanded_shift);
236 } else {
237 energy_input = energy_input >> (expanded_shift - input_shift);
238 }
239
240 // Calculate muting factor to use for new frame.
241 int16_t mute_factor;
242 if (energy_input > energy_expanded) {
243 // Normalize `energy_input` to 14 bits.
244 int16_t temp_shift = WebRtcSpl_NormW32(energy_input) - 17;
245 energy_input = WEBRTC_SPL_SHIFT_W32(energy_input, temp_shift);
246 // Put `energy_expanded` in a domain 14 higher, so that
247 // energy_expanded / energy_input is in Q14.
248 energy_expanded = WEBRTC_SPL_SHIFT_W32(energy_expanded, temp_shift + 14);
249 // Calculate sqrt(energy_expanded / energy_input) in Q14.
250 mute_factor = static_cast<int16_t>(
251 WebRtcSpl_SqrtFloor((energy_expanded / energy_input) << 14));
252 } else {
253 // Set to 1 (in Q14) when `expanded` has higher energy than `input`.
254 mute_factor = 16384;
255 }
256
257 return mute_factor;
258 }
259
260 // TODO(hlundin): There are some parameter values in this method that seem
261 // strange. Compare with Expand::Correlation.
Downsample(const int16_t * input,size_t input_length,const int16_t * expanded_signal,size_t expanded_length)262 void Merge::Downsample(const int16_t* input,
263 size_t input_length,
264 const int16_t* expanded_signal,
265 size_t expanded_length) {
266 const int16_t* filter_coefficients;
267 size_t num_coefficients;
268 int decimation_factor = fs_hz_ / 4000;
269 static const size_t kCompensateDelay = 0;
270 size_t length_limit = static_cast<size_t>(fs_hz_ / 100); // 10 ms in samples.
271 if (fs_hz_ == 8000) {
272 filter_coefficients = DspHelper::kDownsample8kHzTbl;
273 num_coefficients = 3;
274 } else if (fs_hz_ == 16000) {
275 filter_coefficients = DspHelper::kDownsample16kHzTbl;
276 num_coefficients = 5;
277 } else if (fs_hz_ == 32000) {
278 filter_coefficients = DspHelper::kDownsample32kHzTbl;
279 num_coefficients = 7;
280 } else { // fs_hz_ == 48000
281 filter_coefficients = DspHelper::kDownsample48kHzTbl;
282 num_coefficients = 7;
283 }
284 size_t signal_offset = num_coefficients - 1;
285 WebRtcSpl_DownsampleFast(
286 &expanded_signal[signal_offset], expanded_length - signal_offset,
287 expanded_downsampled_, kExpandDownsampLength, filter_coefficients,
288 num_coefficients, decimation_factor, kCompensateDelay);
289 if (input_length <= length_limit) {
290 // Not quite long enough, so we have to cheat a bit.
291 // If the input is shorter than the offset, we consider the input to be 0
292 // length. This will cause us to skip the downsampling since it makes no
293 // sense anyway, and input_downsampled_ will be filled with zeros. This is
294 // clearly a pathological case, and the signal quality will suffer, but
295 // there is not much we can do.
296 const size_t temp_len =
297 input_length > signal_offset ? input_length - signal_offset : 0;
298 // TODO(hlundin): Should `downsamp_temp_len` be corrected for round-off
299 // errors? I.e., (temp_len + decimation_factor - 1) / decimation_factor?
300 size_t downsamp_temp_len = temp_len / decimation_factor;
301 if (downsamp_temp_len > 0) {
302 WebRtcSpl_DownsampleFast(&input[signal_offset], temp_len,
303 input_downsampled_, downsamp_temp_len,
304 filter_coefficients, num_coefficients,
305 decimation_factor, kCompensateDelay);
306 }
307 memset(&input_downsampled_[downsamp_temp_len], 0,
308 sizeof(int16_t) * (kInputDownsampLength - downsamp_temp_len));
309 } else {
310 WebRtcSpl_DownsampleFast(
311 &input[signal_offset], input_length - signal_offset, input_downsampled_,
312 kInputDownsampLength, filter_coefficients, num_coefficients,
313 decimation_factor, kCompensateDelay);
314 }
315 }
316
CorrelateAndPeakSearch(size_t start_position,size_t input_length,size_t expand_period) const317 size_t Merge::CorrelateAndPeakSearch(size_t start_position,
318 size_t input_length,
319 size_t expand_period) const {
320 // Calculate correlation without any normalization.
321 const size_t max_corr_length = kMaxCorrelationLength;
322 size_t stop_position_downsamp =
323 std::min(max_corr_length, expand_->max_lag() / (fs_mult_ * 2) + 1);
324
325 int32_t correlation[kMaxCorrelationLength];
326 CrossCorrelationWithAutoShift(input_downsampled_, expanded_downsampled_,
327 kInputDownsampLength, stop_position_downsamp, 1,
328 correlation);
329
330 // Normalize correlation to 14 bits and copy to a 16-bit array.
331 const size_t pad_length = expand_->overlap_length() - 1;
332 const size_t correlation_buffer_size = 2 * pad_length + kMaxCorrelationLength;
333 std::unique_ptr<int16_t[]> correlation16(
334 new int16_t[correlation_buffer_size]);
335 memset(correlation16.get(), 0, correlation_buffer_size * sizeof(int16_t));
336 int16_t* correlation_ptr = &correlation16[pad_length];
337 int32_t max_correlation =
338 WebRtcSpl_MaxAbsValueW32(correlation, stop_position_downsamp);
339 int norm_shift = std::max(0, 17 - WebRtcSpl_NormW32(max_correlation));
340 WebRtcSpl_VectorBitShiftW32ToW16(correlation_ptr, stop_position_downsamp,
341 correlation, norm_shift);
342
343 // Calculate allowed starting point for peak finding.
344 // The peak location bestIndex must fulfill two criteria:
345 // (1) w16_bestIndex + input_length <
346 // timestamps_per_call_ + expand_->overlap_length();
347 // (2) w16_bestIndex + input_length < start_position.
348 size_t start_index = timestamps_per_call_ + expand_->overlap_length();
349 start_index = std::max(start_position, start_index);
350 start_index = (input_length > start_index) ? 0 : (start_index - input_length);
351 // Downscale starting index to 4kHz domain. (fs_mult_ * 2 = fs_hz_ / 4000.)
352 size_t start_index_downsamp = start_index / (fs_mult_ * 2);
353
354 // Calculate a modified `stop_position_downsamp` to account for the increased
355 // start index `start_index_downsamp` and the effective array length.
356 size_t modified_stop_pos =
357 std::min(stop_position_downsamp,
358 kMaxCorrelationLength + pad_length - start_index_downsamp);
359 size_t best_correlation_index;
360 int16_t best_correlation;
361 static const size_t kNumCorrelationCandidates = 1;
362 DspHelper::PeakDetection(&correlation_ptr[start_index_downsamp],
363 modified_stop_pos, kNumCorrelationCandidates,
364 fs_mult_, &best_correlation_index,
365 &best_correlation);
366 // Compensate for modified start index.
367 best_correlation_index += start_index;
368
369 // Ensure that underrun does not occur for 10ms case => we have to get at
370 // least 10ms + overlap . (This should never happen thanks to the above
371 // modification of peak-finding starting point.)
372 while (((best_correlation_index + input_length) <
373 (timestamps_per_call_ + expand_->overlap_length())) ||
374 ((best_correlation_index + input_length) < start_position)) {
375 RTC_DCHECK_NOTREACHED(); // Should never happen.
376 best_correlation_index += expand_period; // Jump one lag ahead.
377 }
378 return best_correlation_index;
379 }
380
RequiredFutureSamples()381 size_t Merge::RequiredFutureSamples() {
382 return fs_hz_ / 100 * num_channels_; // 10 ms.
383 }
384
385 } // namespace webrtc
386