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