Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Input buffer layout, dividing the total buffer into regions (r0_ - r5_): | 5 // Initial input buffer layout, dividing into regions r0_ to r4_: |
| 6 // | 6 // |
| 7 // |----------------|-----------------------------------------|----------------| | 7 // |----------------|-----------------------------------------|----------------| |
| 8 // | 8 // |
| 9 // kBlockSize + kKernelSize / 2 | 9 // 1st request: request_frames_ |
| 10 // <---------------------------------------------------------> | 10 // <---------------------------------------------------------> |
| 11 // r0_ | 11 // r0_ |
| 12 // | 12 // |
| 13 // block_size_ = request_frames_ - kKernelSize / 2 | |
| 14 // <---------------------------------------> | |
| 15 // | |
| 13 // kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 | 16 // kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 |
| 14 // <---------------> <---------------> <---------------> <---------------> | 17 // <---------------> <---------------> <---------------> <---------------> |
| 15 // r1_ r2_ r3_ r4_ | 18 // r1_ r2_ r3_ r4_ |
| 16 // | 19 // |
| 17 // kBlockSize | 20 // On the second request, block_size_ increases to request_frames_ while r0_, |
| 18 // <---------------------------------------> | 21 // r3_, and r4_ slide to the right by kKernelSize / 2: |
| 19 // r5_ | 22 // |
| 23 // |----------------|-----------------------------------------|----------------| | |
| 24 // | |
| 25 // 2nd request: request_frames_ | |
| 26 // <------------------ ... -----------------> | |
| 27 // | |
| 28 // block_size_ = request_frames_ | |
| 29 // <---------------- ... ------------------> | |
| 30 // | |
| 31 // These new regions remain constant until a Flush() occurs. While complicated, | |
| 32 // this allows us to reduce jitter by always requesting the same amount from the | |
| 33 // provided callback. | |
| 20 // | 34 // |
| 21 // The algorithm: | 35 // The algorithm: |
| 22 // | 36 // |
| 23 // 1) Consume input frames into r0_ (r1_ is zero-initialized). | 37 // 1) Consume |request_frames_| frames into r0_ (r1_ is zero-initialized). |
| 24 // 2) Position kernel centered at start of r0_ (r2_) and generate output frames | 38 // 2) Position kernel centered at start of r0_ (r2_) and generate output frames |
| 25 // until kernel is centered at start of r4_ or we've finished generating all | 39 // until kernel is centered at start of r4_ or we've finished generating all |
| 26 // the output frames. | 40 // the output frames. |
| 27 // 3) Copy r3_ to r1_ and r4_ to r2_. | 41 // 3) Copy r3_ to r1_, r4_ to r2_. |
| 28 // 4) Consume input frames into r5_ (zero-pad if we run out of input). | 42 // 4) If we're on the second load, set block_size_ equal to request_frames_ |
| 29 // 5) Goto (2) until all of input is consumed. | 43 // and reinitialize r0_, r3_, and r4_ appropriately. |
|
Chris Rogers
2013/05/06 21:17:31
It's very hard for me to follow this new diagram,
DaleCurtis
2013/05/06 22:52:09
PTAL. I've rewritten the entire documentation sect
| |
| 44 // 5) Goto (1). | |
| 30 // | 45 // |
| 31 // Note: we're glossing over how the sub-sample handling works with | 46 // Note: we're glossing over how the sub-sample handling works with |
| 32 // |virtual_source_idx_|, etc. | 47 // |virtual_source_idx_|, etc. |
| 33 | 48 |
| 34 // MSVC++ requires this to be set before any other includes to get M_PI. | 49 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 35 #define _USE_MATH_DEFINES | 50 #define _USE_MATH_DEFINES |
| 36 | 51 |
| 37 #include "media/base/sinc_resampler.h" | 52 #include "media/base/sinc_resampler.h" |
| 38 | 53 |
| 39 #include <cmath> | 54 #include <cmath> |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 57 // windowing it the transition from pass to stop does not happen right away. | 72 // windowing it the transition from pass to stop does not happen right away. |
| 58 // So we should adjust the low pass filter cutoff slightly downward to avoid | 73 // So we should adjust the low pass filter cutoff slightly downward to avoid |
| 59 // some aliasing at the very high-end. | 74 // some aliasing at the very high-end. |
| 60 // TODO(crogers): this value is empirical and to be more exact should vary | 75 // TODO(crogers): this value is empirical and to be more exact should vary |
| 61 // depending on kKernelSize. | 76 // depending on kKernelSize. |
| 62 sinc_scale_factor *= 0.9; | 77 sinc_scale_factor *= 0.9; |
| 63 | 78 |
| 64 return sinc_scale_factor; | 79 return sinc_scale_factor; |
| 65 } | 80 } |
| 66 | 81 |
| 67 SincResampler::SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb) | 82 SincResampler::SincResampler(double io_sample_rate_ratio, |
| 83 size_t request_frames, | |
| 84 const ReadCB& read_cb) | |
| 68 : io_sample_rate_ratio_(io_sample_rate_ratio), | 85 : io_sample_rate_ratio_(io_sample_rate_ratio), |
| 69 virtual_source_idx_(0), | |
| 70 buffer_primed_(false), | |
| 71 read_cb_(read_cb), | 86 read_cb_(read_cb), |
| 87 request_frames_(request_frames), | |
| 88 input_buffer_size_(request_frames_ + kKernelSize), | |
| 72 // Create input buffers with a 16-byte alignment for SSE optimizations. | 89 // Create input buffers with a 16-byte alignment for SSE optimizations. |
| 73 kernel_storage_(static_cast<float*>( | 90 kernel_storage_(static_cast<float*>( |
| 74 base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))), | 91 base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))), |
| 75 kernel_pre_sinc_storage_(static_cast<float*>( | 92 kernel_pre_sinc_storage_(static_cast<float*>( |
| 76 base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))), | 93 base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))), |
| 77 kernel_window_storage_(static_cast<float*>( | 94 kernel_window_storage_(static_cast<float*>( |
| 78 base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))), | 95 base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))), |
| 79 input_buffer_(static_cast<float*>( | 96 input_buffer_(static_cast<float*>( |
| 80 base::AlignedAlloc(sizeof(float) * kBufferSize, 16))), | 97 base::AlignedAlloc(sizeof(float) * input_buffer_size_, 16))), |
| 81 #if defined(ARCH_CPU_X86_FAMILY) && !defined(__SSE__) | 98 #if defined(ARCH_CPU_X86_FAMILY) && !defined(__SSE__) |
| 82 convolve_proc_(base::CPU().has_sse() ? Convolve_SSE : Convolve_C), | 99 convolve_proc_(base::CPU().has_sse() ? Convolve_SSE : Convolve_C), |
| 83 #endif | 100 #endif |
| 84 // Setup various region pointers in the buffer (see diagram above). | 101 reinitialize_regions_(false) { |
| 85 r0_(input_buffer_.get() + kKernelSize / 2), | 102 Flush(); |
| 86 r1_(input_buffer_.get()), | 103 CHECK_GT(block_size_, static_cast<size_t>(kKernelSize)) |
| 87 r2_(r0_), | 104 << "block_size must be greater than kKernelSize!"; |
| 88 r3_(r0_ + kBlockSize - kKernelSize / 2), | |
| 89 r4_(r0_ + kBlockSize), | |
| 90 r5_(r0_ + kKernelSize / 2) { | |
| 91 // Ensure kKernelSize is a multiple of 32 for easy SSE optimizations; causes | |
| 92 // r0_ and r5_ (used for input) to always be 16-byte aligned by virtue of | |
| 93 // input_buffer_ being 16-byte aligned. | |
| 94 DCHECK_EQ(kKernelSize % 32, 0) << "kKernelSize must be a multiple of 32!"; | |
| 95 DCHECK_GT(kBlockSize, kKernelSize) | |
| 96 << "kBlockSize must be greater than kKernelSize!"; | |
| 97 // Basic sanity checks to ensure buffer regions are laid out correctly: | |
| 98 // r0_ and r2_ should always be the same position. | |
| 99 DCHECK_EQ(r0_, r2_); | |
| 100 // r1_ at the beginning of the buffer. | |
| 101 DCHECK_EQ(r1_, input_buffer_.get()); | |
| 102 // r1_ left of r2_, r2_ left of r5_ and r1_, r2_ size correct. | |
| 103 DCHECK_EQ(r2_ - r1_, r5_ - r2_); | |
| 104 // r3_ left of r4_, r5_ left of r0_ and r3_ size correct. | |
| 105 DCHECK_EQ(r4_ - r3_, r5_ - r0_); | |
| 106 // r3_, r4_ size correct and r4_ at the end of the buffer. | |
| 107 DCHECK_EQ(r4_ + (r4_ - r3_), r1_ + kBufferSize); | |
| 108 // r5_ size correct and at the end of the buffer. | |
| 109 DCHECK_EQ(r5_ + kBlockSize, r1_ + kBufferSize); | |
| 110 | 105 |
| 111 memset(kernel_storage_.get(), 0, | 106 memset(kernel_storage_.get(), 0, |
| 112 sizeof(*kernel_storage_.get()) * kKernelStorageSize); | 107 sizeof(*kernel_storage_.get()) * kKernelStorageSize); |
| 113 memset(kernel_pre_sinc_storage_.get(), 0, | 108 memset(kernel_pre_sinc_storage_.get(), 0, |
| 114 sizeof(*kernel_pre_sinc_storage_.get()) * kKernelStorageSize); | 109 sizeof(*kernel_pre_sinc_storage_.get()) * kKernelStorageSize); |
| 115 memset(kernel_window_storage_.get(), 0, | 110 memset(kernel_window_storage_.get(), 0, |
| 116 sizeof(*kernel_window_storage_.get()) * kKernelStorageSize); | 111 sizeof(*kernel_window_storage_.get()) * kKernelStorageSize); |
| 117 memset(input_buffer_.get(), 0, sizeof(*input_buffer_.get()) * kBufferSize); | |
| 118 | 112 |
| 119 InitializeKernel(); | 113 InitializeKernel(); |
| 120 } | 114 } |
| 121 | 115 |
| 122 SincResampler::~SincResampler() {} | 116 SincResampler::~SincResampler() {} |
| 123 | 117 |
| 118 void SincResampler::UpdateRegions() { | |
| 119 // Setup various region pointers in the buffer (see diagram above). | |
| 120 r1_ = input_buffer_.get(); | |
| 121 r2_ = input_buffer_.get() + kKernelSize / 2; | |
| 122 r0_ = reinitialize_regions_ ? input_buffer_.get() + kKernelSize : r2_; | |
| 123 r3_ = r2_ + block_size_ - kKernelSize / 2; | |
| 124 r4_ = r2_ + block_size_; | |
| 125 | |
| 126 // r1_ at the beginning of the buffer. | |
| 127 CHECK_EQ(r1_, input_buffer_.get()); | |
| 128 // r1_ left of r2_, r4_ left of r3_ and size correct. | |
| 129 CHECK_EQ(r2_ - r1_, r4_ - r3_); | |
| 130 // r2_ left of r3. | |
| 131 CHECK_LT(r2_, r3_); | |
| 132 } | |
| 133 | |
| 124 void SincResampler::InitializeKernel() { | 134 void SincResampler::InitializeKernel() { |
| 125 // Blackman window parameters. | 135 // Blackman window parameters. |
| 126 static const double kAlpha = 0.16; | 136 static const double kAlpha = 0.16; |
| 127 static const double kA0 = 0.5 * (1.0 - kAlpha); | 137 static const double kA0 = 0.5 * (1.0 - kAlpha); |
| 128 static const double kA1 = 0.5; | 138 static const double kA1 = 0.5; |
| 129 static const double kA2 = 0.5 * kAlpha; | 139 static const double kA2 = 0.5 * kAlpha; |
| 130 | 140 |
| 131 // Generates a set of windowed sinc() kernels. | 141 // Generates a set of windowed sinc() kernels. |
| 132 // We generate a range of sub-sample offsets from 0.0 to 1.0. | 142 // We generate a range of sub-sample offsets from 0.0 to 1.0. |
| 133 const double sinc_scale_factor = SincScaleFactor(io_sample_rate_ratio_); | 143 const double sinc_scale_factor = SincScaleFactor(io_sample_rate_ratio_); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 // TODO(dalecurtis): Once Chrome moves to a SSE baseline this can be removed. | 204 // TODO(dalecurtis): Once Chrome moves to a SSE baseline this can be removed. |
| 195 #define CONVOLVE_FUNC convolve_proc_ | 205 #define CONVOLVE_FUNC convolve_proc_ |
| 196 #endif | 206 #endif |
| 197 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) | 207 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) |
| 198 #define CONVOLVE_FUNC Convolve_NEON | 208 #define CONVOLVE_FUNC Convolve_NEON |
| 199 #else | 209 #else |
| 200 // Unknown architecture. | 210 // Unknown architecture. |
| 201 #define CONVOLVE_FUNC Convolve_C | 211 #define CONVOLVE_FUNC Convolve_C |
| 202 #endif | 212 #endif |
| 203 | 213 |
| 204 void SincResampler::Resample(float* destination, int frames) { | 214 void SincResampler::Resample(int frames, float* destination) { |
| 205 int remaining_frames = frames; | 215 int remaining_frames = frames; |
| 206 | 216 |
| 207 // Step (1) -- Prime the input buffer at the start of the input stream. | 217 // Step (1) -- Prime the input buffer at the start of the input stream. |
| 208 if (!buffer_primed_) { | 218 if (!buffer_primed_) { |
| 209 read_cb_.Run(r0_, kBlockSize + kKernelSize / 2); | 219 read_cb_.Run(request_frames_, r0_); |
| 210 buffer_primed_ = true; | 220 buffer_primed_ = true; |
| 221 reinitialize_regions_ = true; | |
| 211 } | 222 } |
| 212 | 223 |
| 213 // Step (2) -- Resample! | 224 // Step (2) -- Resample! |
| 214 while (remaining_frames) { | 225 while (remaining_frames) { |
| 215 while (virtual_source_idx_ < kBlockSize) { | 226 while (virtual_source_idx_ < block_size_) { |
| 216 // |virtual_source_idx_| lies in between two kernel offsets so figure out | 227 // |virtual_source_idx_| lies in between two kernel offsets so figure out |
| 217 // what they are. | 228 // what they are. |
| 218 int source_idx = static_cast<int>(virtual_source_idx_); | 229 const int source_idx = virtual_source_idx_; |
| 219 double subsample_remainder = virtual_source_idx_ - source_idx; | 230 const double subsample_remainder = virtual_source_idx_ - source_idx; |
| 220 | 231 |
| 221 double virtual_offset_idx = subsample_remainder * kKernelOffsetCount; | 232 const double virtual_offset_idx = |
| 222 int offset_idx = static_cast<int>(virtual_offset_idx); | 233 subsample_remainder * kKernelOffsetCount; |
| 234 const int offset_idx = virtual_offset_idx; | |
| 223 | 235 |
| 224 // We'll compute "convolutions" for the two kernels which straddle | 236 // We'll compute "convolutions" for the two kernels which straddle |
| 225 // |virtual_source_idx_|. | 237 // |virtual_source_idx_|. |
| 226 float* k1 = kernel_storage_.get() + offset_idx * kKernelSize; | 238 const float* k1 = kernel_storage_.get() + offset_idx * kKernelSize; |
| 227 float* k2 = k1 + kKernelSize; | 239 const float* k2 = k1 + kKernelSize; |
| 228 | 240 |
| 229 // Ensure |k1|, |k2| are 16-byte aligned for SIMD usage. Should always be | 241 // Ensure |k1|, |k2| are 16-byte aligned for SIMD usage. Should always be |
| 230 // true so long as kKernelSize is a multiple of 16. | 242 // true so long as kKernelSize is a multiple of 16. |
| 231 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(k1) & 0x0F); | 243 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(k1) & 0x0F); |
| 232 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(k2) & 0x0F); | 244 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(k2) & 0x0F); |
| 233 | 245 |
| 234 // Initialize input pointer based on quantized |virtual_source_idx_|. | 246 // Initialize input pointer based on quantized |virtual_source_idx_|. |
| 235 float* input_ptr = r1_ + source_idx; | 247 const float* input_ptr = r1_ + source_idx; |
| 236 | 248 |
| 237 // Figure out how much to weight each kernel's "convolution". | 249 // Figure out how much to weight each kernel's "convolution". |
| 238 double kernel_interpolation_factor = virtual_offset_idx - offset_idx; | 250 const double kernel_interpolation_factor = |
| 251 virtual_offset_idx - offset_idx; | |
| 239 *destination++ = CONVOLVE_FUNC( | 252 *destination++ = CONVOLVE_FUNC( |
| 240 input_ptr, k1, k2, kernel_interpolation_factor); | 253 input_ptr, k1, k2, kernel_interpolation_factor); |
| 241 | 254 |
| 242 // Advance the virtual index. | 255 // Advance the virtual index. |
| 243 virtual_source_idx_ += io_sample_rate_ratio_; | 256 virtual_source_idx_ += io_sample_rate_ratio_; |
| 244 | 257 |
| 245 if (!--remaining_frames) | 258 if (!--remaining_frames) |
| 246 return; | 259 return; |
| 247 } | 260 } |
| 248 | 261 |
| 249 // Wrap back around to the start. | 262 // Wrap back around to the start. |
| 250 virtual_source_idx_ -= kBlockSize; | 263 virtual_source_idx_ -= block_size_; |
| 251 | 264 |
| 252 // Step (3) Copy r3_ to r1_ and r4_ to r2_. | 265 // Step (3) -- Copy r3_, r4_ to r1_, r2_. |
| 253 // This wraps the last input frames back to the start of the buffer. | 266 // This wraps the last input frames back to the start of the buffer. |
| 254 memcpy(r1_, r3_, sizeof(*input_buffer_.get()) * (kKernelSize / 2)); | 267 memcpy(r1_, r3_, sizeof(*input_buffer_.get()) * kKernelSize); |
| 255 memcpy(r2_, r4_, sizeof(*input_buffer_.get()) * (kKernelSize / 2)); | |
| 256 | 268 |
| 257 // Step (4) | 269 // Step (4) -- Reinitialize regions if necessary. |
| 258 // Refresh the buffer with more input. | 270 if (reinitialize_regions_) { |
| 259 read_cb_.Run(r5_, kBlockSize); | 271 block_size_ = request_frames_; |
| 272 UpdateRegions(); | |
| 273 reinitialize_regions_ = false; | |
| 274 } | |
| 275 | |
| 276 // Step (5) -- Refresh the buffer with more input. | |
| 277 read_cb_.Run(request_frames_, r0_); | |
| 260 } | 278 } |
| 261 } | 279 } |
| 262 | 280 |
| 263 #undef CONVOLVE_FUNC | 281 #undef CONVOLVE_FUNC |
| 264 | 282 |
| 265 int SincResampler::ChunkSize() const { | 283 int SincResampler::ChunkSize() const { |
| 266 return kBlockSize / io_sample_rate_ratio_; | 284 return block_size_ / io_sample_rate_ratio_; |
| 267 } | 285 } |
| 268 | 286 |
| 269 void SincResampler::Flush() { | 287 void SincResampler::Flush() { |
| 270 virtual_source_idx_ = 0; | 288 virtual_source_idx_ = 0; |
| 271 buffer_primed_ = false; | 289 buffer_primed_ = false; |
| 272 memset(input_buffer_.get(), 0, sizeof(*input_buffer_.get()) * kBufferSize); | 290 memset(input_buffer_.get(), 0, |
| 291 sizeof(*input_buffer_.get()) * input_buffer_size_); | |
| 292 | |
| 293 block_size_ = request_frames_ - kKernelSize / 2; | |
| 294 reinitialize_regions_ = false; | |
| 295 UpdateRegions(); | |
| 273 } | 296 } |
| 274 | 297 |
| 275 float SincResampler::Convolve_C(const float* input_ptr, const float* k1, | 298 float SincResampler::Convolve_C(const float* input_ptr, const float* k1, |
| 276 const float* k2, | 299 const float* k2, |
| 277 double kernel_interpolation_factor) { | 300 double kernel_interpolation_factor) { |
| 278 float sum1 = 0; | 301 float sum1 = 0; |
| 279 float sum2 = 0; | 302 float sum2 = 0; |
| 280 | 303 |
| 281 // Generate a single output sample. Unrolling this loop hurt performance in | 304 // Generate a single output sample. Unrolling this loop hurt performance in |
| 282 // local testing. | 305 // local testing. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 vmulq_f32(m_sums1, vmovq_n_f32(1.0 - kernel_interpolation_factor)), | 337 vmulq_f32(m_sums1, vmovq_n_f32(1.0 - kernel_interpolation_factor)), |
| 315 m_sums2, vmovq_n_f32(kernel_interpolation_factor)); | 338 m_sums2, vmovq_n_f32(kernel_interpolation_factor)); |
| 316 | 339 |
| 317 // Sum components together. | 340 // Sum components together. |
| 318 float32x2_t m_half = vadd_f32(vget_high_f32(m_sums1), vget_low_f32(m_sums1)); | 341 float32x2_t m_half = vadd_f32(vget_high_f32(m_sums1), vget_low_f32(m_sums1)); |
| 319 return vget_lane_f32(vpadd_f32(m_half, m_half), 0); | 342 return vget_lane_f32(vpadd_f32(m_half, m_half), 0); |
| 320 } | 343 } |
| 321 #endif | 344 #endif |
| 322 | 345 |
| 323 } // namespace media | 346 } // namespace media |
| OLD | NEW |