Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(423)

Side by Side Diff: media/base/sinc_resampler.cc

Issue 10702050: Add SincResampler ported from WebKit. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Decrease precision tests. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/base/sinc_resampler.h ('k') | media/base/sinc_resampler_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
41 #include "base/logging.h"
42
43 namespace media {
44
45 enum {
46 // The kernel size can be adjusted for quality (higher is better) at the
47 // expense of performance. Must be an even number.
48 // TODO(dalecurtis): Test performance to see if we can jack this up to 64+.
49 kKernelSize = 32,
50
51 // The number of destination frames generated per processing pass. Affects
52 // how often and for how much SincResampler calls back for input. Must be
53 // greater than kKernelSize.
54 kBlockSize = 512,
55
56 // The kernel offset count is used for interpolation and is the number of
57 // sub-sample kernel shifts. Can be adjusted for quality (higher is better)
58 // at the expense of allocating more memory.
59 kKernelOffsetCount = 32,
60 kKernelStorageSize = kKernelSize * (kKernelOffsetCount + 1),
61
62 // The size (in samples) of the internal buffer used by the resampler.
63 kBufferSize = kBlockSize + kKernelSize
64 };
65
66 SincResampler::SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb)
67 : io_sample_rate_ratio_(io_sample_rate_ratio),
68 virtual_source_idx_(0),
69 buffer_primed_(false),
70 read_cb_(read_cb),
71 // TODO(dalecurtis): When we switch to AVX/SSE optimization, we'll need to
72 // allocate with 32-byte alignment and ensure they're sized % 32 bytes.
73 kernel_storage_(new float[kKernelStorageSize]),
74 input_buffer_(new float[kBufferSize]),
75 // Setup various region pointers in the buffer (see diagram above).
76 r0_(input_buffer_.get() + kKernelSize / 2),
77 r1_(input_buffer_.get()),
78 r2_(r0_),
79 r3_(r0_ + kBlockSize - kKernelSize / 2),
80 r4_(r0_ + kBlockSize),
81 r5_(r0_ + kKernelSize / 2) {
82 DCHECK_EQ(kKernelSize % 2, 0) << "kKernelSize must be even!";
83 DCHECK_GT(kBlockSize, kKernelSize)
84 << "kBlockSize must be greater than kKernelSize!";
85 // Basic sanity checks to ensure buffer regions are laid out correctly:
86 // r0_ and r2_ should always be the same position.
87 DCHECK_EQ(r0_, r2_);
88 // r1_ at the beginning of the buffer.
89 DCHECK_EQ(r1_, input_buffer_.get());
90 // r1_ left of r2_, r2_ left of r5_ and r1_, r2_ size correct.
91 DCHECK_EQ(r2_ - r1_, r5_ - r2_);
92 // r3_ left of r4_, r5_ left of r0_ and r3_ size correct.
93 DCHECK_EQ(r4_ - r3_, r5_ - r0_);
94 // r3_, r4_ size correct and r4_ at the end of the buffer.
95 DCHECK_EQ(r4_ + (r4_ - r3_), r1_ + kBufferSize);
96 // r5_ size correct and at the end of the buffer.
97 DCHECK_EQ(r5_ + kBlockSize, r1_ + kBufferSize);
98
99 memset(kernel_storage_.get(), 0,
100 sizeof(*kernel_storage_.get()) * kKernelStorageSize);
101 memset(input_buffer_.get(), 0, sizeof(*input_buffer_.get()) * kBufferSize);
102
103 InitializeKernel();
104 }
105
106 SincResampler::~SincResampler() {}
107
108 void SincResampler::InitializeKernel() {
109 // Blackman window parameters.
110 static const double kAlpha = 0.16;
111 static const double kA0 = 0.5 * (1.0 - kAlpha);
112 static const double kA1 = 0.5;
113 static const double kA2 = 0.5 * kAlpha;
114
115 // |sinc_scale_factor| is basically the normalized cutoff frequency of the
116 // low-pass filter.
117 double sinc_scale_factor =
118 io_sample_rate_ratio_ > 1.0 ? 1.0 / io_sample_rate_ratio_ : 1.0;
119
120 // The sinc function is an idealized brick-wall filter, but since we're
121 // windowing it the transition from pass to stop does not happen right away.
122 // So we should adjust the low pass filter cutoff slightly downward to avoid
123 // some aliasing at the very high-end.
124 // TODO(crogers): this value is empirical and to be more exact should vary
125 // depending on kKernelSize.
126 sinc_scale_factor *= 0.9;
127
128 // Generates a set of windowed sinc() kernels.
129 // We generate a range of sub-sample offsets from 0.0 to 1.0.
130 for (int offset_idx = 0; offset_idx <= kKernelOffsetCount; ++offset_idx) {
131 double subsample_offset =
132 static_cast<double>(offset_idx) / kKernelOffsetCount;
133
134 for (int i = 0; i < kKernelSize; ++i) {
135 // Compute the sinc with offset.
136 double s =
137 sinc_scale_factor * M_PI * (i - kKernelSize / 2 - subsample_offset);
138 double sinc = (!s ? 1.0 : sin(s) / s) * sinc_scale_factor;
139
140 // Compute Blackman window, matching the offset of the sinc().
141 double x = (i - subsample_offset) / kKernelSize;
142 double window = kA0 - kA1 * cos(2.0 * M_PI * x) + kA2
143 * cos(4.0 * M_PI * x);
144
145 // Window the sinc() function and store at the correct offset.
146 kernel_storage_[i + offset_idx * kKernelSize] = sinc * window;
147 }
148 }
149 }
150
151 void SincResampler::Resample(float* destination, int frames) {
152 int remaining_frames = frames;
153
154 // Step (1) -- Prime the input buffer at the start of the input stream.
155 if (!buffer_primed_) {
156 read_cb_.Run(r0_, kBlockSize + kKernelSize / 2);
157 buffer_primed_ = true;
158 }
159
160 // Step (2) -- Resample!
161 while (remaining_frames) {
162 while (virtual_source_idx_ < kBlockSize) {
163 // |virtual_source_idx_| lies in between two kernel offsets so figure out
164 // what they are.
165 int source_idx = static_cast<int>(virtual_source_idx_);
166 double subsample_remainder = virtual_source_idx_ - source_idx;
167
168 double virtual_offset_idx = subsample_remainder * kKernelOffsetCount;
169 int offset_idx = static_cast<int>(virtual_offset_idx);
170
171 float* k1 = kernel_storage_.get() + offset_idx * kKernelSize;
172 float* k2 = k1 + kKernelSize;
173
174 // Initialize input pointer based on quantized |virtual_source_idx_|.
175 float* input_ptr = r1_ + source_idx;
176
177 // We'll compute "convolutions" for the two kernels which straddle
178 // |virtual_source_idx_|.
179 float sum1 = 0;
180 float sum2 = 0;
181
182 // Figure out how much to weight each kernel's "convolution".
183 double kernel_interpolation_factor = virtual_offset_idx - offset_idx;
184
185 // Generate a single output sample.
186 int n = kKernelSize;
187 float input;
188 // TODO(dalecurtis): For initial commit, I've ripped out all the SSE
189 // optimizations, these definitely need to go back in before release.
190 while (n--) {
191 input = *input_ptr++;
192 sum1 += input * *k1++;
193 sum2 += input * *k2++;
194 }
195
196 // Linearly interpolate the two "convolutions".
197 double result = (1.0 - kernel_interpolation_factor) * sum1
198 + kernel_interpolation_factor * sum2;
199
200 *destination++ = result;
201
202 // Advance the virtual index.
203 virtual_source_idx_ += io_sample_rate_ratio_;
204
205 if (!--remaining_frames)
206 return;
207 }
208
209 // Wrap back around to the start.
210 virtual_source_idx_ -= kBlockSize;
211
212 // Step (3) Copy r3_ to r1_ and r4_ to r2_.
213 // This wraps the last input frames back to the start of the buffer.
214 memcpy(r1_, r3_, sizeof(*input_buffer_.get()) * (kKernelSize / 2));
215 memcpy(r2_, r4_, sizeof(*input_buffer_.get()) * (kKernelSize / 2));
216
217 // Step (4)
218 // Refresh the buffer with more input.
219 read_cb_.Run(r5_, kBlockSize);
220 }
221 }
222
223 int SincResampler::ChunkSize() {
224 return kBlockSize / io_sample_rate_ratio_;
225 }
226
227 } // namespace media
OLDNEW
« no previous file with comments | « media/base/sinc_resampler.h ('k') | media/base/sinc_resampler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698