OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // 8) Else, if we're not on the second load, goto (4). | 80 // 8) Else, if we're not on the second load, goto (4). |
81 // | 81 // |
82 // Note: we're glossing over how the sub-sample handling works with | 82 // Note: we're glossing over how the sub-sample handling works with |
83 // |virtual_source_idx_|, etc. | 83 // |virtual_source_idx_|, etc. |
84 | 84 |
85 // MSVC++ requires this to be set before any other includes to get M_PI. | 85 // MSVC++ requires this to be set before any other includes to get M_PI. |
86 #define _USE_MATH_DEFINES | 86 #define _USE_MATH_DEFINES |
87 | 87 |
88 #include "webrtc/common_audio/resampler/sinc_resampler.h" | 88 #include "webrtc/common_audio/resampler/sinc_resampler.h" |
89 | 89 |
90 #include <assert.h> | |
91 #include <math.h> | 90 #include <math.h> |
92 #include <string.h> | 91 #include <string.h> |
93 | 92 |
94 #include <limits> | 93 #include <limits> |
95 | 94 |
| 95 #include "webrtc/base/checks.h" |
96 #include "webrtc/system_wrappers/include/cpu_features_wrapper.h" | 96 #include "webrtc/system_wrappers/include/cpu_features_wrapper.h" |
97 #include "webrtc/typedefs.h" | 97 #include "webrtc/typedefs.h" |
98 | 98 |
99 namespace webrtc { | 99 namespace webrtc { |
100 | 100 |
101 namespace { | 101 namespace { |
102 | 102 |
103 double SincScaleFactor(double io_ratio) { | 103 double SincScaleFactor(double io_ratio) { |
104 // |sinc_scale_factor| is basically the normalized cutoff frequency of the | 104 // |sinc_scale_factor| is basically the normalized cutoff frequency of the |
105 // low-pass filter. | 105 // low-pass filter. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 AlignedMalloc(sizeof(float) * kKernelStorageSize, 16))), | 158 AlignedMalloc(sizeof(float) * kKernelStorageSize, 16))), |
159 input_buffer_(static_cast<float*>( | 159 input_buffer_(static_cast<float*>( |
160 AlignedMalloc(sizeof(float) * input_buffer_size_, 16))), | 160 AlignedMalloc(sizeof(float) * input_buffer_size_, 16))), |
161 #if defined(WEBRTC_CPU_DETECTION) | 161 #if defined(WEBRTC_CPU_DETECTION) |
162 convolve_proc_(NULL), | 162 convolve_proc_(NULL), |
163 #endif | 163 #endif |
164 r1_(input_buffer_.get()), | 164 r1_(input_buffer_.get()), |
165 r2_(input_buffer_.get() + kKernelSize / 2) { | 165 r2_(input_buffer_.get() + kKernelSize / 2) { |
166 #if defined(WEBRTC_CPU_DETECTION) | 166 #if defined(WEBRTC_CPU_DETECTION) |
167 InitializeCPUSpecificFeatures(); | 167 InitializeCPUSpecificFeatures(); |
168 assert(convolve_proc_); | 168 RTC_DCHECK(convolve_proc_); |
169 #endif | 169 #endif |
170 assert(request_frames_ > 0); | 170 RTC_DCHECK_GT(request_frames_, 0); |
171 Flush(); | 171 Flush(); |
172 assert(block_size_ > kKernelSize); | 172 RTC_DCHECK_GT(block_size_, kKernelSize); |
173 | 173 |
174 memset(kernel_storage_.get(), 0, | 174 memset(kernel_storage_.get(), 0, |
175 sizeof(*kernel_storage_.get()) * kKernelStorageSize); | 175 sizeof(*kernel_storage_.get()) * kKernelStorageSize); |
176 memset(kernel_pre_sinc_storage_.get(), 0, | 176 memset(kernel_pre_sinc_storage_.get(), 0, |
177 sizeof(*kernel_pre_sinc_storage_.get()) * kKernelStorageSize); | 177 sizeof(*kernel_pre_sinc_storage_.get()) * kKernelStorageSize); |
178 memset(kernel_window_storage_.get(), 0, | 178 memset(kernel_window_storage_.get(), 0, |
179 sizeof(*kernel_window_storage_.get()) * kKernelStorageSize); | 179 sizeof(*kernel_window_storage_.get()) * kKernelStorageSize); |
180 | 180 |
181 InitializeKernel(); | 181 InitializeKernel(); |
182 } | 182 } |
183 | 183 |
184 SincResampler::~SincResampler() {} | 184 SincResampler::~SincResampler() {} |
185 | 185 |
186 void SincResampler::UpdateRegions(bool second_load) { | 186 void SincResampler::UpdateRegions(bool second_load) { |
187 // Setup various region pointers in the buffer (see diagram above). If we're | 187 // Setup various region pointers in the buffer (see diagram above). If we're |
188 // on the second load we need to slide r0_ to the right by kKernelSize / 2. | 188 // on the second load we need to slide r0_ to the right by kKernelSize / 2. |
189 r0_ = input_buffer_.get() + (second_load ? kKernelSize : kKernelSize / 2); | 189 r0_ = input_buffer_.get() + (second_load ? kKernelSize : kKernelSize / 2); |
190 r3_ = r0_ + request_frames_ - kKernelSize; | 190 r3_ = r0_ + request_frames_ - kKernelSize; |
191 r4_ = r0_ + request_frames_ - kKernelSize / 2; | 191 r4_ = r0_ + request_frames_ - kKernelSize / 2; |
192 block_size_ = r4_ - r2_; | 192 block_size_ = r4_ - r2_; |
193 | 193 |
194 // r1_ at the beginning of the buffer. | 194 // r1_ at the beginning of the buffer. |
195 assert(r1_ == input_buffer_.get()); | 195 RTC_DCHECK_EQ(r1_, input_buffer_.get()); |
196 // r1_ left of r2_, r4_ left of r3_ and size correct. | 196 // r1_ left of r2_, r4_ left of r3_ and size correct. |
197 assert(r2_ - r1_ == r4_ - r3_); | 197 RTC_DCHECK_EQ(r2_ - r1_, r4_ - r3_); |
198 // r2_ left of r3. | 198 // r2_ left of r3. |
199 assert(r2_ < r3_); | 199 RTC_DCHECK_LT(r2_, r3_); |
200 } | 200 } |
201 | 201 |
202 void SincResampler::InitializeKernel() { | 202 void SincResampler::InitializeKernel() { |
203 // Blackman window parameters. | 203 // Blackman window parameters. |
204 static const double kAlpha = 0.16; | 204 static const double kAlpha = 0.16; |
205 static const double kA0 = 0.5 * (1.0 - kAlpha); | 205 static const double kA0 = 0.5 * (1.0 - kAlpha); |
206 static const double kA1 = 0.5; | 206 static const double kA1 = 0.5; |
207 static const double kA2 = 0.5 * kAlpha; | 207 static const double kA2 = 0.5 * kAlpha; |
208 | 208 |
209 // Generates a set of windowed sinc() kernels. | 209 // Generates a set of windowed sinc() kernels. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 const float* const kernel_ptr = kernel_storage_.get(); | 276 const float* const kernel_ptr = kernel_storage_.get(); |
277 while (remaining_frames) { | 277 while (remaining_frames) { |
278 // |i| may be negative if the last Resample() call ended on an iteration | 278 // |i| may be negative if the last Resample() call ended on an iteration |
279 // that put |virtual_source_idx_| over the limit. | 279 // that put |virtual_source_idx_| over the limit. |
280 // | 280 // |
281 // Note: The loop construct here can severely impact performance on ARM | 281 // Note: The loop construct here can severely impact performance on ARM |
282 // or when built with clang. See https://codereview.chromium.org/18566009/ | 282 // or when built with clang. See https://codereview.chromium.org/18566009/ |
283 for (int i = static_cast<int>( | 283 for (int i = static_cast<int>( |
284 ceil((block_size_ - virtual_source_idx_) / current_io_ratio)); | 284 ceil((block_size_ - virtual_source_idx_) / current_io_ratio)); |
285 i > 0; --i) { | 285 i > 0; --i) { |
286 assert(virtual_source_idx_ < block_size_); | 286 RTC_DCHECK_LT(virtual_source_idx_, block_size_); |
287 | 287 |
288 // |virtual_source_idx_| lies in between two kernel offsets so figure out | 288 // |virtual_source_idx_| lies in between two kernel offsets so figure out |
289 // what they are. | 289 // what they are. |
290 const int source_idx = static_cast<int>(virtual_source_idx_); | 290 const int source_idx = static_cast<int>(virtual_source_idx_); |
291 const double subsample_remainder = virtual_source_idx_ - source_idx; | 291 const double subsample_remainder = virtual_source_idx_ - source_idx; |
292 | 292 |
293 const double virtual_offset_idx = | 293 const double virtual_offset_idx = |
294 subsample_remainder * kKernelOffsetCount; | 294 subsample_remainder * kKernelOffsetCount; |
295 const int offset_idx = static_cast<int>(virtual_offset_idx); | 295 const int offset_idx = static_cast<int>(virtual_offset_idx); |
296 | 296 |
297 // We'll compute "convolutions" for the two kernels which straddle | 297 // We'll compute "convolutions" for the two kernels which straddle |
298 // |virtual_source_idx_|. | 298 // |virtual_source_idx_|. |
299 const float* const k1 = kernel_ptr + offset_idx * kKernelSize; | 299 const float* const k1 = kernel_ptr + offset_idx * kKernelSize; |
300 const float* const k2 = k1 + kKernelSize; | 300 const float* const k2 = k1 + kKernelSize; |
301 | 301 |
302 // Ensure |k1|, |k2| are 16-byte aligned for SIMD usage. Should always be | 302 // Ensure |k1|, |k2| are 16-byte aligned for SIMD usage. Should always be |
303 // true so long as kKernelSize is a multiple of 16. | 303 // true so long as kKernelSize is a multiple of 16. |
304 assert(0u == (reinterpret_cast<uintptr_t>(k1) & 0x0F)); | 304 RTC_DCHECK_EQ(0, reinterpret_cast<uintptr_t>(k1) % 16); |
305 assert(0u == (reinterpret_cast<uintptr_t>(k2) & 0x0F)); | 305 RTC_DCHECK_EQ(0, reinterpret_cast<uintptr_t>(k2) % 16); |
306 | 306 |
307 // Initialize input pointer based on quantized |virtual_source_idx_|. | 307 // Initialize input pointer based on quantized |virtual_source_idx_|. |
308 const float* const input_ptr = r1_ + source_idx; | 308 const float* const input_ptr = r1_ + source_idx; |
309 | 309 |
310 // Figure out how much to weight each kernel's "convolution". | 310 // Figure out how much to weight each kernel's "convolution". |
311 const double kernel_interpolation_factor = | 311 const double kernel_interpolation_factor = |
312 virtual_offset_idx - offset_idx; | 312 virtual_offset_idx - offset_idx; |
313 *destination++ = CONVOLVE_FUNC( | 313 *destination++ = CONVOLVE_FUNC( |
314 input_ptr, k1, k2, kernel_interpolation_factor); | 314 input_ptr, k1, k2, kernel_interpolation_factor); |
315 | 315 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 sum1 += *input_ptr * *k1++; | 363 sum1 += *input_ptr * *k1++; |
364 sum2 += *input_ptr++ * *k2++; | 364 sum2 += *input_ptr++ * *k2++; |
365 } | 365 } |
366 | 366 |
367 // Linearly interpolate the two "convolutions". | 367 // Linearly interpolate the two "convolutions". |
368 return static_cast<float>((1.0 - kernel_interpolation_factor) * sum1 + | 368 return static_cast<float>((1.0 - kernel_interpolation_factor) * sum1 + |
369 kernel_interpolation_factor * sum2); | 369 kernel_interpolation_factor * sum2); |
370 } | 370 } |
371 | 371 |
372 } // namespace webrtc | 372 } // namespace webrtc |
OLD | NEW |