OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // Input buffer layout, dividing the total buffer into regions (r0_ - r5_): |
| 6 // |
| 7 // |----------------|-----------------------------------------|----------------| |
| 8 // |
| 9 // kBlockSize + kKernelSize / 2 |
| 10 // <---------------------------------------------------------> |
| 11 // r0_ |
| 12 // |
| 13 // kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 |
| 14 // <---------------> <---------------> <---------------> <---------------> |
| 15 // r1_ r2_ r3_ r4_ |
| 16 // |
| 17 // kBlockSize |
| 18 // <---------------------------------------> |
| 19 // r5_ |
| 20 // |
| 21 // The algorithm: |
| 22 // |
| 23 // 1) Consume input frames into r0_ (r1_ is zero-initialized). |
| 24 // 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 |
| 26 // the output frames. |
| 27 // 3) Copy r3_ to r1_ and r4_ to r2_. |
| 28 // 4) Consume input frames into r5_ (zero-pad if we run out of input). |
| 29 // 5) Goto (2) until all of input is consumed. |
| 30 // |
| 31 // Note: we're glossing over how the sub-sample handling works with |
| 32 // |virtual_source_idx_|, etc. |
| 33 |
| 34 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 35 #define _USE_MATH_DEFINES |
| 36 |
| 37 #include "media/base/sinc_resampler.h" |
| 38 |
| 39 #include <cmath> |
| 40 #include <limits> |
| 41 |
| 42 #include "base/logging.h" |
| 43 |
| 44 namespace media { |
| 45 |
| 46 enum { |
| 47 // The kernel size can be adjusted for quality (higher is better) at the |
| 48 // expense of performance. Must be an even number. |
| 49 // TODO(dalecurtis): Test performance to see if we can jack this up to 64+. |
| 50 kKernelSize = 32, |
| 51 |
| 52 // The number of destination frames generated per processing pass. Affects |
| 53 // how often and for how much SincResampler calls back for input. Must be |
| 54 // greater than kKernelSize. |
| 55 kBlockSize = 512, |
| 56 |
| 57 // The kernel offset count is used for interpolation and is the number of |
| 58 // sub-sample kernel shifts. Can be adjusted for quality (higher is better) |
| 59 // at the expense of allocating more memory. |
| 60 kKernelOffsetCount = 32, |
| 61 kKernelStorageSize = kKernelSize * (kKernelOffsetCount + 1), |
| 62 |
| 63 // The size (in samples) of the internal buffer used by the resampler. |
| 64 kBufferSize = kBlockSize + kKernelSize |
| 65 }; |
| 66 |
| 67 SincResampler::SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb) |
| 68 : io_sample_rate_ratio_(io_sample_rate_ratio), |
| 69 virtual_source_idx_(0), |
| 70 buffer_primed_(false), |
| 71 read_cb_(read_cb), |
| 72 // TODO(dalecurtis): When we switch to AVX/SSE optimization, we'll need to |
| 73 // allocate with 32-byte alignment and ensure they're sized % 32 bytes. |
| 74 kernel_storage_(new float[kKernelStorageSize]), |
| 75 input_buffer_(new float[kBufferSize]), |
| 76 // Setup various region pointers in the buffer (see diagram above). |
| 77 r0_(input_buffer_.get() + kKernelSize / 2), |
| 78 r1_(input_buffer_.get()), |
| 79 r2_(r0_), |
| 80 r3_(r0_ + kBlockSize - kKernelSize / 2), |
| 81 r4_(r0_ + kBlockSize), |
| 82 r5_(r0_ + kKernelSize / 2) { |
| 83 DCHECK_EQ(kKernelSize % 2, 0) << "kKernelSize must be even!"; |
| 84 DCHECK_GT(kBlockSize, kKernelSize) |
| 85 << "kBlockSize must be greater than kKernelSize!"; |
| 86 // Basic sanity checks to ensure buffer regions are laid out correctly: |
| 87 // r0_ and r2_ should always be the same position. |
| 88 DCHECK_EQ(r0_, r2_); |
| 89 // r1_ at the beginning of the buffer. |
| 90 DCHECK_EQ(r1_, input_buffer_.get()); |
| 91 // r1_ left of r2_, r2_ left of r5_ and r1_, r2_ size correct. |
| 92 DCHECK_EQ(r2_ - r1_, r5_ - r2_); |
| 93 // r3_ left of r4_, r5_ left of r0_ and r3_ size correct. |
| 94 DCHECK_EQ(r4_ - r3_, r5_ - r0_); |
| 95 // r3_, r4_ size correct and r4_ at the end of the buffer. |
| 96 DCHECK_EQ(r4_ + (r4_ - r3_), r1_ + kBufferSize); |
| 97 // r5_ size correct and at the end of the buffer. |
| 98 DCHECK_EQ(r5_ + kBlockSize, r1_ + kBufferSize); |
| 99 |
| 100 memset(kernel_storage_.get(), 0, |
| 101 sizeof(*kernel_storage_.get()) * kKernelStorageSize); |
| 102 memset(input_buffer_.get(), 0, sizeof(*input_buffer_.get()) * kBufferSize); |
| 103 |
| 104 InitializeKernel(); |
| 105 } |
| 106 |
| 107 SincResampler::~SincResampler() {} |
| 108 |
| 109 void SincResampler::InitializeKernel() { |
| 110 // Blackman window parameters. |
| 111 static const double kAlpha = 0.16; |
| 112 static const double kA0 = 0.5 * (1.0 - kAlpha); |
| 113 static const double kA1 = 0.5; |
| 114 static const double kA2 = 0.5 * kAlpha; |
| 115 |
| 116 // |sinc_scale_factor| is basically the normalized cutoff frequency of the |
| 117 // low-pass filter. |
| 118 double sinc_scale_factor = |
| 119 io_sample_rate_ratio_ > 1.0 ? 1.0 / io_sample_rate_ratio_ : 1.0; |
| 120 |
| 121 // The sinc function is an idealized brick-wall filter, but since we're |
| 122 // windowing it the transition from pass to stop does not happen right away. |
| 123 // So we should adjust the lowpass filter cutoff slightly downward to avoid |
| 124 // some aliasing at the very high-end. |
| 125 // TODO(crogers): this value is empirical and to be more exact should vary |
| 126 // depending on kKernelSize. |
| 127 sinc_scale_factor *= 0.9; |
| 128 |
| 129 // Generates a set of windowed sinc() kernels. |
| 130 // We generate a range of sub-sample offsets from 0.0 to 1.0. |
| 131 for (int offset_idx = 0; offset_idx <= kKernelOffsetCount; ++offset_idx) { |
| 132 double subsample_offset = |
| 133 static_cast<double>(offset_idx) / kKernelOffsetCount; |
| 134 |
| 135 for (int i = 0; i < kKernelSize; ++i) { |
| 136 // Compute the sinc with offset. |
| 137 double s = |
| 138 sinc_scale_factor * M_PI * (i - kKernelSize / 2 - subsample_offset); |
| 139 double sinc = (!s ? 1.0 : sin(s) / s) * sinc_scale_factor; |
| 140 |
| 141 // Compute Blackman window, matching the offset of the sinc(). |
| 142 double x = (i - subsample_offset) / kKernelSize; |
| 143 double window = kA0 - kA1 * cos(2.0 * M_PI * x) + kA2 |
| 144 * cos(4.0 * M_PI * x); |
| 145 |
| 146 // Window the sinc() function and store at the correct offset. |
| 147 kernel_storage_[i + offset_idx * kKernelSize] = sinc * window; |
| 148 } |
| 149 } |
| 150 } |
| 151 |
| 152 void SincResampler::Resample(float* destination, int frames) { |
| 153 int remaining_frames = frames; |
| 154 |
| 155 // Step (1) -- Prime the input buffer at the start of the input stream. |
| 156 if (!buffer_primed_) { |
| 157 read_cb_.Run(r0_, kBlockSize + kKernelSize / 2); |
| 158 buffer_primed_ = true; |
| 159 } |
| 160 |
| 161 // Step (2) -- Resample! |
| 162 while (remaining_frames) { |
| 163 while (virtual_source_idx_ < kBlockSize) { |
| 164 // |virtual_source_idx_| lies in between two kernel offsets so figure out |
| 165 // what they are. |
| 166 int source_idx = static_cast<int>(virtual_source_idx_); |
| 167 double subsample_remainder = virtual_source_idx_ - source_idx; |
| 168 |
| 169 double virtual_offset_idx = subsample_remainder * kKernelOffsetCount; |
| 170 int offset_idx = static_cast<int>(virtual_offset_idx); |
| 171 |
| 172 float* k1 = kernel_storage_.get() + offset_idx * kKernelSize; |
| 173 float* k2 = k1 + kKernelSize; |
| 174 |
| 175 // Initialize input pointer based on quantized |virtual_source_idx_|. |
| 176 float* input_ptr = r1_ + source_idx; |
| 177 |
| 178 // We'll compute "convolutions" for the two kernels which straddle |
| 179 // |virtual_source_idx_|. |
| 180 float sum1 = 0; |
| 181 float sum2 = 0; |
| 182 |
| 183 // Figure out how much to weight each kernel's "convolution". |
| 184 double kernel_interpolation_factor = virtual_offset_idx - offset_idx; |
| 185 |
| 186 // Generate a single output sample. |
| 187 int n = kKernelSize; |
| 188 float input; |
| 189 // TODO(dalecurtis): For initial commit, I've ripped out all the SSE |
| 190 // optimizations, these definitely need to go back in before release. |
| 191 while (n--) { |
| 192 input = *input_ptr++; |
| 193 sum1 += input * *k1++; |
| 194 sum2 += input * *k2++; |
| 195 } |
| 196 |
| 197 // Linearly interpolate the two "convolutions". |
| 198 double result = (1.0 - kernel_interpolation_factor) * sum1 |
| 199 + kernel_interpolation_factor * sum2; |
| 200 |
| 201 *destination++ = result; |
| 202 |
| 203 // Advance the virtual index. |
| 204 virtual_source_idx_ += io_sample_rate_ratio_; |
| 205 |
| 206 if (!--remaining_frames) |
| 207 return; |
| 208 } |
| 209 |
| 210 // Wrap back around to the start. Reset to zero if the delta is below |
| 211 // machine limits; allowing us to cap accumulated error over time. |
| 212 virtual_source_idx_ -= kBlockSize; |
| 213 if (virtual_source_idx_ < std::numeric_limits<double>::epsilon()) |
| 214 virtual_source_idx_ = 0; |
| 215 |
| 216 // Step (3) Copy r3_ to r1_ and r4_ to r2_. |
| 217 // This wraps the last input frames back to the start of the buffer. |
| 218 memcpy(r1_, r3_, sizeof(*input_buffer_.get()) * (kKernelSize / 2)); |
| 219 memcpy(r2_, r4_, sizeof(*input_buffer_.get()) * (kKernelSize / 2)); |
| 220 |
| 221 // Step (4) |
| 222 // Refresh the buffer with more input. |
| 223 read_cb_.Run(r5_, kBlockSize); |
| 224 } |
| 225 } |
| 226 |
| 227 int SincResampler::ChunkSize() { |
| 228 return kBlockSize / io_sample_rate_ratio_; |
| 229 } |
| 230 |
| 231 } // namespace media |
OLD | NEW |