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

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: Comments. Created 7 years, 7 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
« no previous file with comments | « media/base/sinc_resampler.h ('k') | media/base/sinc_resampler_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/sinc_resampler.cc
diff --git a/media/base/sinc_resampler.cc b/media/base/sinc_resampler.cc
index 6bce67a3e7e85252240fad089711a2f769be6d14..c253b8355bfa72c5fafa5d9a5b3a2b9d9be54785 100644
--- a/media/base/sinc_resampler.cc
+++ b/media/base/sinc_resampler.cc
@@ -2,31 +2,73 @@
// 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_ (note: r0_, r3_
+// and r4_ will move after the first load):
//
// |----------------|-----------------------------------------|----------------|
//
-// kBlockSize + kKernelSize / 2
+// request_frames_
// <--------------------------------------------------------->
-// r0_
+// r0_ (during first load)
//
// kKernelSize / 2 kKernelSize / 2 kKernelSize / 2 kKernelSize / 2
// <---------------> <---------------> <---------------> <--------------->
// r1_ r2_ r3_ r4_
//
-// kBlockSize
-// <--------------------------------------->
-// r5_
+// block_size_ == r4_ - r2_
+// <--------------------------------------->
+//
+// request_frames_
+// <------------------ ... ----------------->
+// r0_ (during second load)
+//
+// On the second request r0_ slides to the right by kKernelSize / 2 and r3_, r4_
+// and block_size_ are reinitialized via step (3) in the algorithm below.
+//
+// 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).
-// 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.
+// 1) Allocate input_buffer of size: request_frames_ + kKernelSize; this ensures
+// there's enough room to read request_frames_ from the callback into region
+// r0_ (which will move between the first and subsequent passes).
+//
+// 2) Let r1_, r2_ each represent half the kernel centered around r0_:
+//
+// r0_ = input_buffer_ + kKernelSize / 2
+// r1_ = input_buffer_
+// r2_ = r0_
+//
+// r0_ is always request_frames_ in size. r1_, r2_ are kKernelSize / 2 in
+// size. r1_ must be zero initialized to avoid convolution with garbage (see
+// step (5) for why).
+//
+// 3) Let r3_, r4_ each represent half the kernel right aligned with the end of
+// r0_ and choose block_size_ as the distance in frames between r4_ and r2_:
+//
+// r3_ = r0_ + request_frames_ - kKernelSize
+// r4_ = r0_ + request_frames_ - kKernelSize / 2
+// block_size_ = r4_ - r2_ = request_frames_ - kKernelSize / 2
+//
+// 4) Consume request_frames_ frames into r0_.
+//
+// 5) Position kernel centered at start of r2_ and generate output frames until
+// the kernel is centered at the start of r4_ or we've finished generating
+// all the output frames.
+//
+// 6) Wrap left over data from the r3_ to r1_ and r4_ to r2_.
+//
+// 7) If we're on the second load, in order to avoid overwriting the frames we
+// just wrapped from r4_ we need to slide r0_ to the right by the size of
+// r4_, which is kKernelSize / 2:
+//
+// r0_ = r0_ + kKernelSize / 2 = input_buffer_ + kKernelSize
+//
+// r3_, r4_, and block_size_ then need to be reinitialized, so goto (3).
+//
+// 8) Else, if we're not on the second load, goto (4).
//
// Note: we're glossing over how the sub-sample handling works with
// |virtual_source_idx_|, etc.
@@ -64,11 +106,13 @@ 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_frames,
+ const ReadCB& read_cb)
: io_sample_rate_ratio_(io_sample_rate_ratio),
- virtual_source_idx_(0),
- buffer_primed_(false),
read_cb_(read_cb),
+ request_frames_(request_frames),
+ input_buffer_size_(request_frames_ + 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 +121,15 @@ 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);
+ r2_(input_buffer_.get() + kKernelSize / 2) {
+ Flush();
+ CHECK_GT(block_size_, static_cast<size_t>(kKernelSize))
+ << "block_size must be greater than kKernelSize!";
memset(kernel_storage_.get(), 0,
sizeof(*kernel_storage_.get()) * kKernelStorageSize);
@@ -114,13 +137,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(bool second_load) {
+ // Setup various region pointers in the buffer (see diagram above). If we're
+ // on the second load we need to slide r0_ to the right by kKernelSize / 2.
+ r0_ = input_buffer_.get() + (second_load ? kKernelSize : kKernelSize / 2);
+ r3_ = r0_ + request_frames_ - kKernelSize;
+ r4_ = r0_ + request_frames_ - kKernelSize / 2;
+ block_size_ = r4_ - r2_;
+
+ // 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;
@@ -201,30 +239,31 @@ void SincResampler::SetRatio(double io_sample_rate_ratio) {
#define CONVOLVE_FUNC Convolve_C
#endif
-void SincResampler::Resample(float* destination, int frames) {
+void SincResampler::Resample(int frames, float* destination) {
int remaining_frames = 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(request_frames_, r0_);
buffer_primed_ = 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 +271,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 +287,33 @@ 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) -- Reinitialize regions if necessary.
+ if (r0_ == r2_)
+ UpdateRegions(true);
- // Step (4)
- // Refresh the buffer with more input.
- read_cb_.Run(r5_, kBlockSize);
+ // Step (5) -- Refresh the buffer with more input.
+ read_cb_.Run(request_frames_, r0_);
}
}
#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_);
+ UpdateRegions(false);
}
float SincResampler::Convolve_C(const float* input_ptr, const float* k1,
« 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