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

Unified Diff: media/base/sinc_resampler.cc

Issue 14189035: Reduce jitter from uneven SincResampler buffer size requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup. Created 7 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: media/base/sinc_resampler.cc
diff --git a/media/base/sinc_resampler.cc b/media/base/sinc_resampler.cc
index 6bce67a3e7e85252240fad089711a2f769be6d14..98d6b7462d6aafaef4e1dc9d79882441f3d48ac4 100644
--- a/media/base/sinc_resampler.cc
+++ b/media/base/sinc_resampler.cc
@@ -2,31 +2,46 @@
// 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_):
+// Initial input buffer layout, dividing into regions r0_ to r4_:
//
// |----------------|-----------------------------------------|----------------|
//
-// kBlockSize + kKernelSize / 2
+// 1st request: request_size_
// <--------------------------------------------------------->
// r0_
//
+// block_size_ = request_size_ - kKernelSize / 2
+// <--------------------------------------->
+//
// kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 kKernelSize / 2
// <---------------> <---------------> <---------------> <--------------->
// r1_ r2_ r3_ r4_
//
-// kBlockSize
-// <--------------------------------------->
-// r5_
+// On the second request, block_size_ increases to request_size_ while r0_, r3_,
+// and r4_ slide to the right by kKernelSize / 2:
+//
+// |----------------|-----------------------------------------|----------------|
+//
+// 2nd request: request_size_
+// <------------------ ... ----------------->
+//
+// block_size_ = request_size_
+// <---------------- ... ------------------>
+//
+// These new regions remain constant until a Flush() occurs. While complicated,
+// this allows us to reduce jitter by always requesting the same amount from the
+// provided callback.
//
// The algorithm:
//
-// 1) Consume input frames into r0_ (r1_ is zero-initialized).
+// 1) Consume |request_size_| 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.
+// 3) Copy r3_ to r1_, r4_ to r2_.
+// 4) If we're on the second load, set block_size_ equal to request_size_
+// and reinitialize r0_, r3_, and r4_ appropriately.
+// 5) Goto (1).
//
// Note: we're glossing over how the sub-sample handling works with
// |virtual_source_idx_|, etc.
@@ -64,11 +79,12 @@ static double SincScaleFactor(double io_ratio) {
return sinc_scale_factor;
}
-SincResampler::SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb)
+SincResampler::SincResampler(double io_sample_rate_ratio, size_t request_size,
+ const ReadCB& read_cb)
: io_sample_rate_ratio_(io_sample_rate_ratio),
- virtual_source_idx_(0),
- buffer_primed_(false),
read_cb_(read_cb),
+ request_size_(request_size),
+ input_buffer_size_(request_size_ + kKernelSize),
// Create input buffers with a 16-byte alignment for SSE optimizations.
kernel_storage_(static_cast<float*>(
base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))),
@@ -77,36 +93,14 @@ SincResampler::SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb)
kernel_window_storage_(static_cast<float*>(
base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))),
input_buffer_(static_cast<float*>(
- base::AlignedAlloc(sizeof(float) * kBufferSize, 16))),
+ base::AlignedAlloc(sizeof(float) * input_buffer_size_, 16))),
#if defined(ARCH_CPU_X86_FAMILY) && !defined(__SSE__)
convolve_proc_(base::CPU().has_sse() ? Convolve_SSE : Convolve_C),
#endif
- // Setup various region pointers in the buffer (see diagram above).
- r0_(input_buffer_.get() + kKernelSize / 2),
- r1_(input_buffer_.get()),
- r2_(r0_),
- r3_(r0_ + kBlockSize - kKernelSize / 2),
- r4_(r0_ + kBlockSize),
- r5_(r0_ + kKernelSize / 2) {
- // Ensure kKernelSize is a multiple of 32 for easy SSE optimizations; causes
- // r0_ and r5_ (used for input) to always be 16-byte aligned by virtue of
- // input_buffer_ being 16-byte aligned.
- DCHECK_EQ(kKernelSize % 32, 0) << "kKernelSize must be a multiple of 32!";
- DCHECK_GT(kBlockSize, kKernelSize)
- << "kBlockSize must be greater than kKernelSize!";
- // Basic sanity checks to ensure buffer regions are laid out correctly:
- // r0_ and r2_ should always be the same position.
- DCHECK_EQ(r0_, r2_);
- // r1_ at the beginning of the buffer.
- DCHECK_EQ(r1_, input_buffer_.get());
- // r1_ left of r2_, r2_ left of r5_ and r1_, r2_ size correct.
- DCHECK_EQ(r2_ - r1_, r5_ - r2_);
- // r3_ left of r4_, r5_ left of r0_ and r3_ size correct.
- DCHECK_EQ(r4_ - r3_, r5_ - r0_);
- // r3_, r4_ size correct and r4_ at the end of the buffer.
- DCHECK_EQ(r4_ + (r4_ - r3_), r1_ + kBufferSize);
- // r5_ size correct and at the end of the buffer.
- DCHECK_EQ(r5_ + kBlockSize, r1_ + kBufferSize);
+ reinitialize_regions_(false) {
+ Flush();
+ CHECK_GT(block_size_, static_cast<size_t>(kKernelSize))
henrika (OOO until Aug 14) 2013/04/27 19:43:40 Thanks :-)
+ << "block_size must be greater than kKernelSize!";
memset(kernel_storage_.get(), 0,
sizeof(*kernel_storage_.get()) * kKernelStorageSize);
@@ -114,13 +108,28 @@ SincResampler::SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb)
sizeof(*kernel_pre_sinc_storage_.get()) * kKernelStorageSize);
memset(kernel_window_storage_.get(), 0,
sizeof(*kernel_window_storage_.get()) * kKernelStorageSize);
- memset(input_buffer_.get(), 0, sizeof(*input_buffer_.get()) * kBufferSize);
InitializeKernel();
}
SincResampler::~SincResampler() {}
+void SincResampler::UpdateRegions() {
+ // Setup various region pointers in the buffer (see diagram above).
+ r1_ = input_buffer_.get();
+ r2_ = input_buffer_.get() + kKernelSize / 2;
+ r0_ = reinitialize_regions_ ? input_buffer_.get() + kKernelSize : r2_;
+ r3_ = r2_ + block_size_ - kKernelSize / 2;
+ r4_ = r2_ + block_size_;
+
+ // r1_ at the beginning of the buffer.
+ CHECK_EQ(r1_, input_buffer_.get());
+ // r1_ left of r2_, r4_ left of r3_ and size correct.
+ CHECK_EQ(r2_ - r1_, r4_ - r3_);
+ // r2_ left of r3.
+ CHECK_LT(r2_, r3_);
+}
+
void SincResampler::InitializeKernel() {
// Blackman window parameters.
static const double kAlpha = 0.16;
@@ -206,25 +215,27 @@ void SincResampler::Resample(float* destination, int frames) {
// Step (1) -- Prime the input buffer at the start of the input stream.
if (!buffer_primed_) {
- read_cb_.Run(r0_, kBlockSize + kKernelSize / 2);
+ read_cb_.Run(r0_, request_size_);
buffer_primed_ = true;
+ reinitialize_regions_ = true;
}
// Step (2) -- Resample!
while (remaining_frames) {
- while (virtual_source_idx_ < kBlockSize) {
+ while (virtual_source_idx_ < block_size_) {
// |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;
+ const int source_idx = virtual_source_idx_;
+ const double subsample_remainder = virtual_source_idx_ - source_idx;
- double virtual_offset_idx = subsample_remainder * kKernelOffsetCount;
- int offset_idx = static_cast<int>(virtual_offset_idx);
+ const double virtual_offset_idx =
+ subsample_remainder * kKernelOffsetCount;
+ const int offset_idx = virtual_offset_idx;
// We'll compute "convolutions" for the two kernels which straddle
// |virtual_source_idx_|.
- float* k1 = kernel_storage_.get() + offset_idx * kKernelSize;
- float* k2 = k1 + kKernelSize;
+ const float* k1 = kernel_storage_.get() + offset_idx * kKernelSize;
+ const float* k2 = k1 + kKernelSize;
// Ensure |k1|, |k2| are 16-byte aligned for SIMD usage. Should always be
// true so long as kKernelSize is a multiple of 16.
@@ -232,10 +243,11 @@ void SincResampler::Resample(float* destination, int frames) {
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(k2) & 0x0F);
// Initialize input pointer based on quantized |virtual_source_idx_|.
- float* input_ptr = r1_ + source_idx;
+ const float* input_ptr = r1_ + source_idx;
// Figure out how much to weight each kernel's "convolution".
- double kernel_interpolation_factor = virtual_offset_idx - offset_idx;
+ const double kernel_interpolation_factor =
+ virtual_offset_idx - offset_idx;
*destination++ = CONVOLVE_FUNC(
input_ptr, k1, k2, kernel_interpolation_factor);
@@ -247,29 +259,39 @@ void SincResampler::Resample(float* destination, int frames) {
}
// Wrap back around to the start.
- virtual_source_idx_ -= kBlockSize;
+ virtual_source_idx_ -= block_size_;
- // Step (3) Copy r3_ to r1_ and r4_ to r2_.
+ // Step (3) -- Copy r3_, r4_ to r1_, r2_.
// This wraps the last input frames back to the start of the buffer.
- memcpy(r1_, r3_, sizeof(*input_buffer_.get()) * (kKernelSize / 2));
- memcpy(r2_, r4_, sizeof(*input_buffer_.get()) * (kKernelSize / 2));
+ memcpy(r1_, r3_, sizeof(*input_buffer_.get()) * kKernelSize);
- // Step (4)
- // Refresh the buffer with more input.
- read_cb_.Run(r5_, kBlockSize);
+ // Step (4) -- Reinitialize regions if necessary.
+ if (reinitialize_regions_) {
+ block_size_ = request_size_;
+ UpdateRegions();
+ reinitialize_regions_ = false;
+ }
+
+ // Step (5) -- Refresh the buffer with more input.
+ read_cb_.Run(r0_, request_size_);
}
}
#undef CONVOLVE_FUNC
int SincResampler::ChunkSize() const {
- return kBlockSize / io_sample_rate_ratio_;
+ return block_size_ / io_sample_rate_ratio_;
}
void SincResampler::Flush() {
virtual_source_idx_ = 0;
buffer_primed_ = false;
- memset(input_buffer_.get(), 0, sizeof(*input_buffer_.get()) * kBufferSize);
+ memset(input_buffer_.get(), 0,
+ sizeof(*input_buffer_.get()) * input_buffer_size_);
+
+ block_size_ = request_size_ - kKernelSize / 2;
+ reinitialize_regions_ = false;
+ UpdateRegions();
}
float SincResampler::Convolve_C(const float* input_ptr, const float* k1,

Powered by Google App Engine
This is Rietveld 408576698