Chromium Code Reviews| Index: media/base/sinc_resampler.cc |
| diff --git a/media/base/sinc_resampler.cc b/media/base/sinc_resampler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ba0f129baf05d83ce8843cbe211e83dd69e22831 |
| --- /dev/null |
| +++ b/media/base/sinc_resampler.cc |
| @@ -0,0 +1,199 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +// |
| +// Input buffer layout, dividing the total buffer into regions (r0 - r5): |
| +// |
| +// |----------------|-----------------------------------------|----------------| |
| +// |
| +// kBlockSize + kKernelSize / 2 |
| +// <---------------------------------------------------------> |
| +// r0 |
| +// |
| +// kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 |
| +// <---------------> <---------------> <---------------> <---------------> |
| +// r1 r2 r3 r4 |
| +// |
| +// kBlockSize |
| +// <---------------------------------------> |
| +// r5 |
| +// |
| +// The algorithm: |
| +// |
| +// 1) Consume input frames into r0 (r1 is zero-initialized). |
| +// 2) Position kernel centered at start of r0 (r2) and generate output frames |
| +// until kernel is centered at start of r4 or we've finished generating all |
| +// the output frames. |
| +// 3) Copy r3 to r1 and r4 to r2. |
| +// 4) Consume input frames into r5 (zero-pad if we run out of input). |
| +// 5) Goto (2) until all of input is consumed. |
| +// |
| +// Note: we're glossing over how the sub-sample handling works with |
| +// |virtual_source_index_|, etc. |
| +// MSVC++ requires this to be set before any other includes to get M_PI. |
| +#define _USE_MATH_DEFINES |
|
Chris Rogers
2012/07/02 20:38:14
Not sure if there's any cleaner way to do this?
I
DaleCurtis
2012/07/03 00:36:34
Not that I've found, this is what I used in my oth
|
| + |
| +#include "media/base/sinc_resampler.h" |
| + |
| +#include <cmath> |
| + |
| +#include "base/logging.h" |
| + |
| +namespace media { |
| + |
| +// The kernel size can be adjusted for quality (higher is better). |
| +static const int kKernelSize = 32; |
|
Chris Rogers
2012/07/02 20:38:14
32 should be fine for now, but you could add a TOD
DaleCurtis
2012/07/03 00:36:34
Done.
|
| +// The kernel offset count is used for interpolation and is the number of |
| +// sub-sample kernel shifts. |
| +static const int kKernelOffsetCount = 32; |
| + |
| +const int SincResampler::kBlockSize = 512; |
| +const int SincResampler::kBufferSize = kBlockSize + kKernelSize; |
| +COMPILE_ASSERT(kKernelSize % 2 == 0, kKernelSize_must_be_even); |
| +COMPILE_ASSERT( |
| + SincResampler::kBlockSize > kKernelSize, kBlockSize_must_be_gt_kKernelSize); |
| + |
| +SincResampler::SincResampler(AudioSourceProvider* provider, double scale_factor) |
| + : scale_factor_(scale_factor), |
| + virtual_source_idx_(0), |
| + buffer_primed_(false), |
| + provider_(provider), |
| + // TODO(dalecurtis): When we switch to AVX/SSE optimization, we'll need to |
| + // allocate with 32-byte alignment and ensure they're sized % 32 bytes. |
| + kernel_storage_(new float[kKernelSize * (kKernelOffsetCount + 1)]), |
| + input_buffer_(new float[kBufferSize]) { |
| + memset(kernel_storage_, 0, |
| + sizeof(*kernel_storage_) * kKernelSize * (kKernelOffsetCount + 1)); |
| + memset(input_buffer_, 0, sizeof(*kernel_storage_) * kBufferSize); |
| + |
| + InitializeKernel(); |
| +} |
| + |
| +SincResampler::~SincResampler() { |
| + delete[] kernel_storage_; |
| + delete[] input_buffer_; |
| +} |
| + |
| +void SincResampler::InitializeKernel() { |
| + // Blackman window parameters. |
| + double alpha = 0.16; |
| + double a0 = 0.5 * (1.0 - alpha); |
| + double a1 = 0.5; |
| + double a2 = 0.5 * alpha; |
| + |
| + // |sinc_scale_factor| is basically the normalized cutoff frequency of the |
| + // low-pass filter. |
| + double sinc_scale_factor = scale_factor_ > 1.0 ? 1.0 / scale_factor_ : 1.0; |
| + |
| + // The sinc function is an idealized brick-wall filter, but since we're |
| + // windowing it the transition from pass to stop does not happen right away. |
| + // So we should adjust the lowpass filter cutoff slightly downward to avoid |
| + // some aliasing at the very high-end. |
| + // TODO(crogers): this value is empirical and to be more exact should vary |
| + // depending on kKernelSize. |
| + sinc_scale_factor *= 0.9; |
| + |
| + // Generates a set of windowed sinc() kernels. |
| + // We generate a range of sub-sample offsets from 0.0 to 1.0. |
| + for (int offset_idx = 0; offset_idx <= kKernelOffsetCount; ++offset_idx) { |
| + double subsample_offset = |
| + static_cast<double>(offset_idx) / kKernelOffsetCount; |
| + |
| + for (int i = 0; i < kKernelSize; ++i) { |
| + // Compute the sinc with offset. |
| + // TODO(dalecurtis): Is M_PI precise enough here? It was using piDouble. |
|
Chris Rogers
2012/07/02 20:38:14
I think it should be fine -- in any case the unit
DaleCurtis
2012/07/03 00:36:34
Done.
|
| + double s = |
| + sinc_scale_factor * M_PI * (i - kKernelSize / 2 - subsample_offset); |
| + double sinc = (!s ? 1.0 : sin(s) / s) * sinc_scale_factor; |
| + |
| + // Compute Blackman window, matching the offset of the sinc(). |
| + double x = (i - subsample_offset) / kKernelSize; |
| + double window = a0 - a1 * cos(2.0 * M_PI * x) + a2 * cos(4.0 * M_PI * x); |
| + |
| + // Window the sinc() function and store at the correct offset. |
| + kernel_storage_[i + offset_idx * kKernelSize] = sinc * window; |
| + } |
| + } |
| +} |
| + |
| +void SincResampler::Resample(float* destination, int number_of_frames) { |
| + int remaining_frames = number_of_frames; |
| + |
| + // Setup various region pointers in the buffer (see diagram above). |
| + float* r0 = input_buffer_ + kKernelSize / 2; |
| + float* r1 = input_buffer_; |
| + float* r2 = r0; |
| + float* r3 = r0 + kBlockSize - kKernelSize / 2; |
| + float* r4 = r0 + kBlockSize; |
| + float* r5 = r0 + kKernelSize / 2; |
| + |
| + // Step (1) -- Prime the input buffer at the start of the input stream. |
| + if (!buffer_primed_) { |
| + provider_->ProvideInput(r0, kBlockSize + kKernelSize / 2); |
| + buffer_primed_ = true; |
| + } |
| + |
| + // Step (2) -- Resample! |
| + while (remaining_frames) { |
| + while (virtual_source_idx_ < kBlockSize) { |
| + // |virtual_source_idx_| lies in between two kernel offsets so figure out |
| + // what they are. |
| + int source_idx = static_cast<int>(virtual_source_idx_); |
| + double subsample_remainder = virtual_source_idx_ - source_idx; |
| + |
| + double virtual_offset_idx = subsample_remainder * kKernelOffsetCount; |
| + int offset_idx = static_cast<int>(virtual_offset_idx); |
| + |
| + float* k1 = kernel_storage_ + offset_idx * kKernelSize; |
| + float* k2 = k1 + kKernelSize; |
| + |
| + // Initialize input pointer based on quantized |virtual_source_idx_|. |
| + float* input_ptr = r1 + source_idx; |
| + |
| + // We'll compute "convolutions" for the two kernels which straddle |
| + // |virtual_source_idx_|. |
| + float sum1 = 0; |
| + float sum2 = 0; |
| + |
| + // Figure out how much to weight each kernel's "convolution". |
| + double kernel_interpolation_factor = virtual_offset_idx - offset_idx; |
| + |
| + // Generate a single output sample. |
| + int n = kKernelSize; |
| + float input; |
| + // TODO(dalecurtis): For initial commit, I've ripped out all the SSE |
| + // optimizations, these definitely need to go back in before release. |
| + while (n--) { |
| + input = *input_ptr++; |
| + sum1 += input * *k1++; |
| + sum2 += input * *k2++; |
| + } |
| + |
| + // Linearly interpolate the two "convolutions". |
| + double result = (1.0 - kernel_interpolation_factor) * sum1 |
| + + kernel_interpolation_factor * sum2; |
| + |
| + *destination++ = result; |
| + |
| + // Advance the virtual index. |
| + virtual_source_idx_ += scale_factor_; |
| + |
| + if (!--remaining_frames) |
| + return; |
| + } |
| + |
| + // Wrap back around to the start. |
| + virtual_source_idx_ -= kBlockSize; |
| + |
| + // Step (3) Copy r3 to r1 and r4 to r2. |
| + // This wraps the last input frames back to the start of the buffer. |
| + memcpy(r1, r3, sizeof(*input_buffer_) * (kKernelSize / 2)); |
| + memcpy(r2, r4, sizeof(*input_buffer_) * (kKernelSize / 2)); |
| + |
| + // Step (4) |
| + // Refresh the buffer with more input. |
| + provider_->ProvideInput(r5, kBlockSize); |
| + } |
| +} |
| + |
| +} // namespace media |