Import SincResampler from Chromium.
Committing the originals to make further reviews cleaner. TBR=bjornv BUG=webrtc:1395 Review URL: https://webrtc-codereview.appspot.com/1096010 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3508 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
9c4e662ea8
commit
a8ef811fe5
347
webrtc/common_audio/resampler/sinc_resampler.cc
Normal file
347
webrtc/common_audio/resampler/sinc_resampler.cc
Normal file
@ -0,0 +1,347 @@
|
||||
// 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_idx_|, etc.
|
||||
|
||||
// MSVC++ requires this to be set before any other includes to get M_PI.
|
||||
#define _USE_MATH_DEFINES
|
||||
|
||||
#include "media/base/sinc_resampler.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "base/cpu.h"
|
||||
#include "base/logging.h"
|
||||
#include "build/build_config.h"
|
||||
|
||||
#if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__)
|
||||
#include <xmmintrin.h>
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
|
||||
#include <arm_neon.h>
|
||||
#endif
|
||||
|
||||
namespace media {
|
||||
|
||||
namespace {
|
||||
|
||||
enum {
|
||||
// The kernel size can be adjusted for quality (higher is better) at the
|
||||
// expense of performance. Must be a multiple of 32.
|
||||
// TODO(dalecurtis): Test performance to see if we can jack this up to 64+.
|
||||
kKernelSize = 32,
|
||||
|
||||
// The number of destination frames generated per processing pass. Affects
|
||||
// how often and for how much SincResampler calls back for input. Must be
|
||||
// greater than kKernelSize.
|
||||
kBlockSize = 512,
|
||||
|
||||
// The kernel offset count is used for interpolation and is the number of
|
||||
// sub-sample kernel shifts. Can be adjusted for quality (higher is better)
|
||||
// at the expense of allocating more memory.
|
||||
kKernelOffsetCount = 32,
|
||||
kKernelStorageSize = kKernelSize * (kKernelOffsetCount + 1),
|
||||
|
||||
// The size (in samples) of the internal buffer used by the resampler.
|
||||
kBufferSize = kBlockSize + kKernelSize
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
const int SincResampler::kMaximumLookAheadSize = kBufferSize;
|
||||
|
||||
SincResampler::SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb)
|
||||
: io_sample_rate_ratio_(io_sample_rate_ratio),
|
||||
virtual_source_idx_(0),
|
||||
buffer_primed_(false),
|
||||
read_cb_(read_cb),
|
||||
// Create input buffers with a 16-byte alignment for SSE optimizations.
|
||||
kernel_storage_(static_cast<float*>(
|
||||
base::AlignedAlloc(sizeof(float) * kKernelStorageSize, 16))),
|
||||
input_buffer_(static_cast<float*>(
|
||||
base::AlignedAlloc(sizeof(float) * kBufferSize, 16))),
|
||||
// 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);
|
||||
|
||||
memset(kernel_storage_.get(), 0,
|
||||
sizeof(*kernel_storage_.get()) * kKernelStorageSize);
|
||||
memset(input_buffer_.get(), 0, sizeof(*input_buffer_.get()) * kBufferSize);
|
||||
|
||||
InitializeKernel();
|
||||
}
|
||||
|
||||
SincResampler::~SincResampler() {}
|
||||
|
||||
void SincResampler::InitializeKernel() {
|
||||
// Blackman window parameters.
|
||||
static const double kAlpha = 0.16;
|
||||
static const double kA0 = 0.5 * (1.0 - kAlpha);
|
||||
static const double kA1 = 0.5;
|
||||
static const double kA2 = 0.5 * kAlpha;
|
||||
|
||||
// |sinc_scale_factor| is basically the normalized cutoff frequency of the
|
||||
// low-pass filter.
|
||||
double sinc_scale_factor =
|
||||
io_sample_rate_ratio_ > 1.0 ? 1.0 / io_sample_rate_ratio_ : 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 low pass 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.
|
||||
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 = kA0 - kA1 * cos(2.0 * M_PI * x) + kA2
|
||||
* cos(4.0 * M_PI * x);
|
||||
|
||||
// Window the sinc() function and store at the correct offset.
|
||||
kernel_storage_.get()[i + offset_idx * kKernelSize] = sinc * window;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SincResampler::Resample(float* destination, int frames) {
|
||||
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);
|
||||
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);
|
||||
|
||||
// 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;
|
||||
|
||||
// Initialize input pointer based on quantized |virtual_source_idx_|.
|
||||
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;
|
||||
*destination++ = Convolve(
|
||||
input_ptr, k1, k2, kernel_interpolation_factor);
|
||||
|
||||
// Advance the virtual index.
|
||||
virtual_source_idx_ += io_sample_rate_ratio_;
|
||||
|
||||
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_.get()) * (kKernelSize / 2));
|
||||
memcpy(r2_, r4_, sizeof(*input_buffer_.get()) * (kKernelSize / 2));
|
||||
|
||||
// Step (4)
|
||||
// Refresh the buffer with more input.
|
||||
read_cb_.Run(r5_, kBlockSize);
|
||||
}
|
||||
}
|
||||
|
||||
int SincResampler::ChunkSize() {
|
||||
return kBlockSize / io_sample_rate_ratio_;
|
||||
}
|
||||
|
||||
void SincResampler::Flush() {
|
||||
virtual_source_idx_ = 0;
|
||||
buffer_primed_ = false;
|
||||
memset(input_buffer_.get(), 0, sizeof(*input_buffer_.get()) * kBufferSize);
|
||||
}
|
||||
|
||||
float SincResampler::Convolve(const float* input_ptr, const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor) {
|
||||
// Rely on function level static initialization to keep ConvolveProc selection
|
||||
// thread safe.
|
||||
typedef float (*ConvolveProc)(const float* src, const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor);
|
||||
#if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__)
|
||||
static const ConvolveProc kConvolveProc =
|
||||
base::CPU().has_sse() ? Convolve_SSE : Convolve_C;
|
||||
#elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
|
||||
static const ConvolveProc kConvolveProc = Convolve_NEON;
|
||||
#else
|
||||
static const ConvolveProc kConvolveProc = Convolve_C;
|
||||
#endif
|
||||
|
||||
return kConvolveProc(input_ptr, k1, k2, kernel_interpolation_factor);
|
||||
}
|
||||
|
||||
float SincResampler::Convolve_C(const float* input_ptr, const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor) {
|
||||
float sum1 = 0;
|
||||
float sum2 = 0;
|
||||
|
||||
// Generate a single output sample. Unrolling this loop hurt performance in
|
||||
// local testing.
|
||||
int n = kKernelSize;
|
||||
while (n--) {
|
||||
sum1 += *input_ptr * *k1++;
|
||||
sum2 += *input_ptr++ * *k2++;
|
||||
}
|
||||
|
||||
// Linearly interpolate the two "convolutions".
|
||||
return (1.0 - kernel_interpolation_factor) * sum1
|
||||
+ kernel_interpolation_factor * sum2;
|
||||
}
|
||||
|
||||
#if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__)
|
||||
float SincResampler::Convolve_SSE(const float* input_ptr, const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor) {
|
||||
// Ensure |k1|, |k2| are 16-byte aligned for SSE usage. Should always be true
|
||||
// so long as kKernelSize is a multiple of 16.
|
||||
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(k1) & 0x0F);
|
||||
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(k2) & 0x0F);
|
||||
|
||||
__m128 m_input;
|
||||
__m128 m_sums1 = _mm_setzero_ps();
|
||||
__m128 m_sums2 = _mm_setzero_ps();
|
||||
|
||||
// Based on |input_ptr| alignment, we need to use loadu or load. Unrolling
|
||||
// these loops hurt performance in local testing.
|
||||
if (reinterpret_cast<uintptr_t>(input_ptr) & 0x0F) {
|
||||
for (int i = 0; i < kKernelSize; i += 4) {
|
||||
m_input = _mm_loadu_ps(input_ptr + i);
|
||||
m_sums1 = _mm_add_ps(m_sums1, _mm_mul_ps(m_input, _mm_load_ps(k1 + i)));
|
||||
m_sums2 = _mm_add_ps(m_sums2, _mm_mul_ps(m_input, _mm_load_ps(k2 + i)));
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < kKernelSize; i += 4) {
|
||||
m_input = _mm_load_ps(input_ptr + i);
|
||||
m_sums1 = _mm_add_ps(m_sums1, _mm_mul_ps(m_input, _mm_load_ps(k1 + i)));
|
||||
m_sums2 = _mm_add_ps(m_sums2, _mm_mul_ps(m_input, _mm_load_ps(k2 + i)));
|
||||
}
|
||||
}
|
||||
|
||||
// Linearly interpolate the two "convolutions".
|
||||
m_sums1 = _mm_mul_ps(m_sums1, _mm_set_ps1(1.0 - kernel_interpolation_factor));
|
||||
m_sums2 = _mm_mul_ps(m_sums2, _mm_set_ps1(kernel_interpolation_factor));
|
||||
m_sums1 = _mm_add_ps(m_sums1, m_sums2);
|
||||
|
||||
// Sum components together.
|
||||
float result;
|
||||
m_sums2 = _mm_add_ps(_mm_movehl_ps(m_sums1, m_sums1), m_sums1);
|
||||
_mm_store_ss(&result, _mm_add_ss(m_sums2, _mm_shuffle_ps(
|
||||
m_sums2, m_sums2, 1)));
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
|
||||
float SincResampler::Convolve_NEON(const float* input_ptr, const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor) {
|
||||
float32x4_t m_input;
|
||||
float32x4_t m_sums1 = vmovq_n_f32(0);
|
||||
float32x4_t m_sums2 = vmovq_n_f32(0);
|
||||
|
||||
const float* upper = input_ptr + kKernelSize;
|
||||
for (; input_ptr < upper; ) {
|
||||
m_input = vld1q_f32(input_ptr);
|
||||
input_ptr += 4;
|
||||
m_sums1 = vmlaq_f32(m_sums1, m_input, vld1q_f32(k1));
|
||||
k1 += 4;
|
||||
m_sums2 = vmlaq_f32(m_sums2, m_input, vld1q_f32(k2));
|
||||
k2 += 4;
|
||||
}
|
||||
|
||||
// Linearly interpolate the two "convolutions".
|
||||
m_sums1 = vmlaq_f32(
|
||||
vmulq_f32(m_sums1, vmovq_n_f32(1.0 - kernel_interpolation_factor)),
|
||||
m_sums2, vmovq_n_f32(kernel_interpolation_factor));
|
||||
|
||||
// Sum components together.
|
||||
float32x2_t m_half = vadd_f32(vget_high_f32(m_sums1), vget_low_f32(m_sums1));
|
||||
return vget_lane_f32(vpadd_f32(m_half, m_half), 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace media
|
100
webrtc/common_audio/resampler/sinc_resampler.h
Normal file
100
webrtc/common_audio/resampler/sinc_resampler.h
Normal file
@ -0,0 +1,100 @@
|
||||
// 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.
|
||||
|
||||
#ifndef MEDIA_BASE_SINC_RESAMPLER_H_
|
||||
#define MEDIA_BASE_SINC_RESAMPLER_H_
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/gtest_prod_util.h"
|
||||
#include "base/memory/aligned_memory.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "media/base/media_export.h"
|
||||
|
||||
namespace media {
|
||||
|
||||
// SincResampler is a high-quality single-channel sample-rate converter.
|
||||
class MEDIA_EXPORT SincResampler {
|
||||
public:
|
||||
// The maximum number of samples that may be requested from the callback ahead
|
||||
// of the current position in the stream.
|
||||
static const int kMaximumLookAheadSize;
|
||||
|
||||
// Callback type for providing more data into the resampler. Expects |frames|
|
||||
// of data to be rendered into |destination|; zero padded if not enough frames
|
||||
// are available to satisfy the request.
|
||||
typedef base::Callback<void(float* destination, int frames)> ReadCB;
|
||||
|
||||
// Constructs a SincResampler with the specified |read_cb|, which is used to
|
||||
// acquire audio data for resampling. |io_sample_rate_ratio| is the ratio of
|
||||
// input / output sample rates.
|
||||
SincResampler(double io_sample_rate_ratio, const ReadCB& read_cb);
|
||||
virtual ~SincResampler();
|
||||
|
||||
// Resample |frames| of data from |read_cb_| into |destination|.
|
||||
void Resample(float* destination, int frames);
|
||||
|
||||
// The maximum size in frames that guarantees Resample() will only make a
|
||||
// single call to |read_cb_| for more data.
|
||||
int ChunkSize();
|
||||
|
||||
// Flush all buffered data and reset internal indices.
|
||||
void Flush();
|
||||
|
||||
private:
|
||||
FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, Convolve);
|
||||
FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, ConvolveBenchmark);
|
||||
|
||||
void InitializeKernel();
|
||||
|
||||
// Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are
|
||||
// linearly interpolated using |kernel_interpolation_factor|. On x86, the
|
||||
// underlying implementation is chosen at run time based on SSE support. On
|
||||
// ARM, NEON support is chosen at compile time based on compilation flags.
|
||||
static float Convolve(const float* input_ptr, const float* k1,
|
||||
const float* k2, double kernel_interpolation_factor);
|
||||
static float Convolve_C(const float* input_ptr, const float* k1,
|
||||
const float* k2, double kernel_interpolation_factor);
|
||||
static float Convolve_SSE(const float* input_ptr, const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor);
|
||||
static float Convolve_NEON(const float* input_ptr, const float* k1,
|
||||
const float* k2,
|
||||
double kernel_interpolation_factor);
|
||||
|
||||
// The ratio of input / output sample rates.
|
||||
double io_sample_rate_ratio_;
|
||||
|
||||
// An index on the source input buffer with sub-sample precision. It must be
|
||||
// double precision to avoid drift.
|
||||
double virtual_source_idx_;
|
||||
|
||||
// The buffer is primed once at the very beginning of processing.
|
||||
bool buffer_primed_;
|
||||
|
||||
// Source of data for resampling.
|
||||
ReadCB read_cb_;
|
||||
|
||||
// Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize.
|
||||
// The kernel offsets are sub-sample shifts of a windowed sinc shifted from
|
||||
// 0.0 to 1.0 sample.
|
||||
scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> kernel_storage_;
|
||||
|
||||
// Data from the source is copied into this buffer for each processing pass.
|
||||
scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> input_buffer_;
|
||||
|
||||
// Pointers to the various regions inside |input_buffer_|. See the diagram at
|
||||
// the top of the .cc file for more information.
|
||||
float* const r0_;
|
||||
float* const r1_;
|
||||
float* const r2_;
|
||||
float* const r3_;
|
||||
float* const r4_;
|
||||
float* const r5_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(SincResampler);
|
||||
};
|
||||
|
||||
} // namespace media
|
||||
|
||||
#endif // MEDIA_BASE_SINC_RESAMPLER_H_
|
405
webrtc/common_audio/resampler/sinc_resampler_unittest.cc
Normal file
405
webrtc/common_audio/resampler/sinc_resampler_unittest.cc
Normal file
@ -0,0 +1,405 @@
|
||||
// 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.
|
||||
|
||||
// MSVC++ requires this to be set before any other includes to get M_PI.
|
||||
#define _USE_MATH_DEFINES
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/bind_helpers.h"
|
||||
#include "base/command_line.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/string_number_conversions.h"
|
||||
#include "base/strings/stringize_macros.h"
|
||||
#include "base/time.h"
|
||||
#include "build/build_config.h"
|
||||
#include "media/base/sinc_resampler.h"
|
||||
#include "testing/gmock/include/gmock/gmock.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
using testing::_;
|
||||
|
||||
namespace media {
|
||||
|
||||
static const double kSampleRateRatio = 192000.0 / 44100.0;
|
||||
static const double kKernelInterpolationFactor = 0.5;
|
||||
|
||||
// Command line switch for runtime adjustment of ConvolveBenchmark iterations.
|
||||
static const char kConvolveIterations[] = "convolve-iterations";
|
||||
|
||||
// Helper class to ensure ChunkedResample() functions properly.
|
||||
class MockSource {
|
||||
public:
|
||||
MOCK_METHOD2(ProvideInput, void(float* destination, int frames));
|
||||
};
|
||||
|
||||
ACTION(ClearBuffer) {
|
||||
memset(arg0, 0, arg1 * sizeof(float));
|
||||
}
|
||||
|
||||
ACTION(FillBuffer) {
|
||||
// Value chosen arbitrarily such that SincResampler resamples it to something
|
||||
// easily representable on all platforms; e.g., using kSampleRateRatio this
|
||||
// becomes 1.81219.
|
||||
memset(arg0, 64, arg1 * sizeof(float));
|
||||
}
|
||||
|
||||
// Test requesting multiples of ChunkSize() frames results in the proper number
|
||||
// of callbacks.
|
||||
TEST(SincResamplerTest, ChunkedResample) {
|
||||
MockSource mock_source;
|
||||
|
||||
// Choose a high ratio of input to output samples which will result in quick
|
||||
// exhaustion of SincResampler's internal buffers.
|
||||
SincResampler resampler(
|
||||
kSampleRateRatio,
|
||||
base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
|
||||
|
||||
static const int kChunks = 2;
|
||||
int max_chunk_size = resampler.ChunkSize() * kChunks;
|
||||
scoped_array<float> resampled_destination(new float[max_chunk_size]);
|
||||
|
||||
// Verify requesting ChunkSize() frames causes a single callback.
|
||||
EXPECT_CALL(mock_source, ProvideInput(_, _))
|
||||
.Times(1).WillOnce(ClearBuffer());
|
||||
resampler.Resample(resampled_destination.get(), resampler.ChunkSize());
|
||||
|
||||
// Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks.
|
||||
testing::Mock::VerifyAndClear(&mock_source);
|
||||
EXPECT_CALL(mock_source, ProvideInput(_, _))
|
||||
.Times(kChunks).WillRepeatedly(ClearBuffer());
|
||||
resampler.Resample(resampled_destination.get(), max_chunk_size);
|
||||
}
|
||||
|
||||
// Test flush resets the internal state properly.
|
||||
TEST(SincResamplerTest, Flush) {
|
||||
MockSource mock_source;
|
||||
SincResampler resampler(
|
||||
kSampleRateRatio,
|
||||
base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
|
||||
scoped_array<float> resampled_destination(new float[resampler.ChunkSize()]);
|
||||
|
||||
// Fill the resampler with junk data.
|
||||
EXPECT_CALL(mock_source, ProvideInput(_, _))
|
||||
.Times(1).WillOnce(FillBuffer());
|
||||
resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2);
|
||||
ASSERT_NE(resampled_destination[0], 0);
|
||||
|
||||
// Flush and request more data, which should all be zeros now.
|
||||
resampler.Flush();
|
||||
testing::Mock::VerifyAndClear(&mock_source);
|
||||
EXPECT_CALL(mock_source, ProvideInput(_, _))
|
||||
.Times(1).WillOnce(ClearBuffer());
|
||||
resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2);
|
||||
for (int i = 0; i < resampler.ChunkSize() / 2; ++i)
|
||||
ASSERT_FLOAT_EQ(resampled_destination[i], 0);
|
||||
}
|
||||
|
||||
// Define platform independent function name for Convolve* tests.
|
||||
#if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__)
|
||||
#define CONVOLVE_FUNC Convolve_SSE
|
||||
#elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
|
||||
#define CONVOLVE_FUNC Convolve_NEON
|
||||
#endif
|
||||
|
||||
// Ensure various optimized Convolve() methods return the same value. Only run
|
||||
// this test if other optimized methods exist, otherwise the default Convolve()
|
||||
// will be tested by the parameterized SincResampler tests below.
|
||||
#if defined(CONVOLVE_FUNC)
|
||||
TEST(SincResamplerTest, Convolve) {
|
||||
// Initialize a dummy resampler.
|
||||
MockSource mock_source;
|
||||
SincResampler resampler(
|
||||
kSampleRateRatio,
|
||||
base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
|
||||
|
||||
// The optimized Convolve methods are slightly more precise than Convolve_C(),
|
||||
// so comparison must be done using an epsilon.
|
||||
static const double kEpsilon = 0.00000005;
|
||||
|
||||
// Use a kernel from SincResampler as input and kernel data, this has the
|
||||
// benefit of already being properly sized and aligned for Convolve_SSE().
|
||||
double result = resampler.Convolve_C(
|
||||
resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
|
||||
resampler.kernel_storage_.get(), kKernelInterpolationFactor);
|
||||
double result2 = resampler.CONVOLVE_FUNC(
|
||||
resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
|
||||
resampler.kernel_storage_.get(), kKernelInterpolationFactor);
|
||||
EXPECT_NEAR(result2, result, kEpsilon);
|
||||
|
||||
// Test Convolve() w/ unaligned input pointer.
|
||||
result = resampler.Convolve_C(
|
||||
resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(),
|
||||
resampler.kernel_storage_.get(), kKernelInterpolationFactor);
|
||||
result2 = resampler.CONVOLVE_FUNC(
|
||||
resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(),
|
||||
resampler.kernel_storage_.get(), kKernelInterpolationFactor);
|
||||
EXPECT_NEAR(result2, result, kEpsilon);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Benchmark for the various Convolve() methods. Make sure to build with
|
||||
// branding=Chrome so that DCHECKs are compiled out when benchmarking. Original
|
||||
// benchmarks were run with --convolve-iterations=50000000.
|
||||
TEST(SincResamplerTest, ConvolveBenchmark) {
|
||||
// Initialize a dummy resampler.
|
||||
MockSource mock_source;
|
||||
SincResampler resampler(
|
||||
kSampleRateRatio,
|
||||
base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source)));
|
||||
|
||||
// Retrieve benchmark iterations from command line.
|
||||
int convolve_iterations = 10;
|
||||
std::string iterations(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
|
||||
kConvolveIterations));
|
||||
if (!iterations.empty())
|
||||
base::StringToInt(iterations, &convolve_iterations);
|
||||
|
||||
printf("Benchmarking %d iterations:\n", convolve_iterations);
|
||||
|
||||
// Benchmark Convolve_C().
|
||||
base::TimeTicks start = base::TimeTicks::HighResNow();
|
||||
for (int i = 0; i < convolve_iterations; ++i) {
|
||||
resampler.Convolve_C(
|
||||
resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
|
||||
resampler.kernel_storage_.get(), kKernelInterpolationFactor);
|
||||
}
|
||||
double total_time_c_ms =
|
||||
(base::TimeTicks::HighResNow() - start).InMillisecondsF();
|
||||
printf("Convolve_C took %.2fms.\n", total_time_c_ms);
|
||||
|
||||
#if defined(CONVOLVE_FUNC)
|
||||
// Benchmark with unaligned input pointer.
|
||||
start = base::TimeTicks::HighResNow();
|
||||
for (int j = 0; j < convolve_iterations; ++j) {
|
||||
resampler.CONVOLVE_FUNC(
|
||||
resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(),
|
||||
resampler.kernel_storage_.get(), kKernelInterpolationFactor);
|
||||
}
|
||||
double total_time_optimized_unaligned_ms =
|
||||
(base::TimeTicks::HighResNow() - start).InMillisecondsF();
|
||||
printf(STRINGIZE(CONVOLVE_FUNC) "(unaligned) took %.2fms; which is %.2fx "
|
||||
"faster than Convolve_C.\n", total_time_optimized_unaligned_ms,
|
||||
total_time_c_ms / total_time_optimized_unaligned_ms);
|
||||
|
||||
// Benchmark with aligned input pointer.
|
||||
start = base::TimeTicks::HighResNow();
|
||||
for (int j = 0; j < convolve_iterations; ++j) {
|
||||
resampler.CONVOLVE_FUNC(
|
||||
resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
|
||||
resampler.kernel_storage_.get(), kKernelInterpolationFactor);
|
||||
}
|
||||
double total_time_optimized_aligned_ms =
|
||||
(base::TimeTicks::HighResNow() - start).InMillisecondsF();
|
||||
printf(STRINGIZE(CONVOLVE_FUNC) " (aligned) took %.2fms; which is %.2fx "
|
||||
"faster than Convolve_C and %.2fx faster than "
|
||||
STRINGIZE(CONVOLVE_FUNC) " (unaligned).\n",
|
||||
total_time_optimized_aligned_ms,
|
||||
total_time_c_ms / total_time_optimized_aligned_ms,
|
||||
total_time_optimized_unaligned_ms / total_time_optimized_aligned_ms);
|
||||
#endif
|
||||
}
|
||||
|
||||
#undef CONVOLVE_FUNC
|
||||
|
||||
// Fake audio source for testing the resampler. Generates a sinusoidal linear
|
||||
// chirp (http://en.wikipedia.org/wiki/Chirp) which can be tuned to stress the
|
||||
// resampler for the specific sample rate conversion being used.
|
||||
class SinusoidalLinearChirpSource {
|
||||
public:
|
||||
SinusoidalLinearChirpSource(int sample_rate, int samples,
|
||||
double max_frequency)
|
||||
: sample_rate_(sample_rate),
|
||||
total_samples_(samples),
|
||||
max_frequency_(max_frequency),
|
||||
current_index_(0) {
|
||||
// Chirp rate.
|
||||
double duration = static_cast<double>(total_samples_) / sample_rate_;
|
||||
k_ = (max_frequency_ - kMinFrequency) / duration;
|
||||
}
|
||||
|
||||
virtual ~SinusoidalLinearChirpSource() {}
|
||||
|
||||
void ProvideInput(float* destination, int frames) {
|
||||
for (int i = 0; i < frames; ++i, ++current_index_) {
|
||||
// Filter out frequencies higher than Nyquist.
|
||||
if (Frequency(current_index_) > 0.5 * sample_rate_) {
|
||||
destination[i] = 0;
|
||||
} else {
|
||||
// Calculate time in seconds.
|
||||
double t = static_cast<double>(current_index_) / sample_rate_;
|
||||
|
||||
// Sinusoidal linear chirp.
|
||||
destination[i] = sin(2 * M_PI * (kMinFrequency * t + (k_ / 2) * t * t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double Frequency(int position) {
|
||||
return kMinFrequency + position * (max_frequency_ - kMinFrequency)
|
||||
/ total_samples_;
|
||||
}
|
||||
|
||||
private:
|
||||
enum {
|
||||
kMinFrequency = 5
|
||||
};
|
||||
|
||||
double sample_rate_;
|
||||
int total_samples_;
|
||||
double max_frequency_;
|
||||
double k_;
|
||||
int current_index_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(SinusoidalLinearChirpSource);
|
||||
};
|
||||
|
||||
typedef std::tr1::tuple<int, int, double, double> SincResamplerTestData;
|
||||
class SincResamplerTest
|
||||
: public testing::TestWithParam<SincResamplerTestData> {
|
||||
public:
|
||||
SincResamplerTest()
|
||||
: input_rate_(std::tr1::get<0>(GetParam())),
|
||||
output_rate_(std::tr1::get<1>(GetParam())),
|
||||
rms_error_(std::tr1::get<2>(GetParam())),
|
||||
low_freq_error_(std::tr1::get<3>(GetParam())) {
|
||||
}
|
||||
|
||||
virtual ~SincResamplerTest() {}
|
||||
|
||||
protected:
|
||||
int input_rate_;
|
||||
int output_rate_;
|
||||
double rms_error_;
|
||||
double low_freq_error_;
|
||||
};
|
||||
|
||||
// Tests resampling using a given input and output sample rate.
|
||||
TEST_P(SincResamplerTest, Resample) {
|
||||
// Make comparisons using one second of data.
|
||||
static const double kTestDurationSecs = 1;
|
||||
int input_samples = kTestDurationSecs * input_rate_;
|
||||
int output_samples = kTestDurationSecs * output_rate_;
|
||||
|
||||
// Nyquist frequency for the input sampling rate.
|
||||
double input_nyquist_freq = 0.5 * input_rate_;
|
||||
|
||||
// Source for data to be resampled.
|
||||
SinusoidalLinearChirpSource resampler_source(
|
||||
input_rate_, input_samples, input_nyquist_freq);
|
||||
|
||||
SincResampler resampler(
|
||||
input_rate_ / static_cast<double>(output_rate_),
|
||||
base::Bind(&SinusoidalLinearChirpSource::ProvideInput,
|
||||
base::Unretained(&resampler_source)));
|
||||
|
||||
// TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to
|
||||
// allocate these on 32-byte boundaries and ensure they're sized % 32 bytes.
|
||||
scoped_array<float> resampled_destination(new float[output_samples]);
|
||||
scoped_array<float> pure_destination(new float[output_samples]);
|
||||
|
||||
// Generate resampled signal.
|
||||
resampler.Resample(resampled_destination.get(), output_samples);
|
||||
|
||||
// Generate pure signal.
|
||||
SinusoidalLinearChirpSource pure_source(
|
||||
output_rate_, output_samples, input_nyquist_freq);
|
||||
pure_source.ProvideInput(pure_destination.get(), output_samples);
|
||||
|
||||
// Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which
|
||||
// we refer to as low and high.
|
||||
static const double kLowFrequencyNyquistRange = 0.7;
|
||||
static const double kHighFrequencyNyquistRange = 0.9;
|
||||
|
||||
// Calculate Root-Mean-Square-Error and maximum error for the resampling.
|
||||
double sum_of_squares = 0;
|
||||
double low_freq_max_error = 0;
|
||||
double high_freq_max_error = 0;
|
||||
int minimum_rate = std::min(input_rate_, output_rate_);
|
||||
double low_frequency_range = kLowFrequencyNyquistRange * 0.5 * minimum_rate;
|
||||
double high_frequency_range = kHighFrequencyNyquistRange * 0.5 * minimum_rate;
|
||||
for (int i = 0; i < output_samples; ++i) {
|
||||
double error = fabs(resampled_destination[i] - pure_destination[i]);
|
||||
|
||||
if (pure_source.Frequency(i) < low_frequency_range) {
|
||||
if (error > low_freq_max_error)
|
||||
low_freq_max_error = error;
|
||||
} else if (pure_source.Frequency(i) < high_frequency_range) {
|
||||
if (error > high_freq_max_error)
|
||||
high_freq_max_error = error;
|
||||
}
|
||||
// TODO(dalecurtis): Sanity check frequencies > kHighFrequencyNyquistRange.
|
||||
|
||||
sum_of_squares += error * error;
|
||||
}
|
||||
|
||||
double rms_error = sqrt(sum_of_squares / output_samples);
|
||||
|
||||
// Convert each error to dbFS.
|
||||
#define DBFS(x) 20 * log10(x)
|
||||
rms_error = DBFS(rms_error);
|
||||
low_freq_max_error = DBFS(low_freq_max_error);
|
||||
high_freq_max_error = DBFS(high_freq_max_error);
|
||||
|
||||
EXPECT_LE(rms_error, rms_error_);
|
||||
EXPECT_LE(low_freq_max_error, low_freq_error_);
|
||||
|
||||
// All conversions currently have a high frequency error around -6 dbFS.
|
||||
static const double kHighFrequencyMaxError = -6.02;
|
||||
EXPECT_LE(high_freq_max_error, kHighFrequencyMaxError);
|
||||
}
|
||||
|
||||
// Almost all conversions have an RMS error of around -14 dbFS.
|
||||
static const double kResamplingRMSError = -14.58;
|
||||
|
||||
// Thresholds chosen arbitrarily based on what each resampling reported during
|
||||
// testing. All thresholds are in dbFS, http://en.wikipedia.org/wiki/DBFS.
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
SincResamplerTest, SincResamplerTest, testing::Values(
|
||||
// To 44.1kHz
|
||||
std::tr1::make_tuple(8000, 44100, kResamplingRMSError, -62.73),
|
||||
std::tr1::make_tuple(11025, 44100, kResamplingRMSError, -72.19),
|
||||
std::tr1::make_tuple(16000, 44100, kResamplingRMSError, -62.54),
|
||||
std::tr1::make_tuple(22050, 44100, kResamplingRMSError, -73.53),
|
||||
std::tr1::make_tuple(32000, 44100, kResamplingRMSError, -63.32),
|
||||
std::tr1::make_tuple(44100, 44100, kResamplingRMSError, -73.53),
|
||||
std::tr1::make_tuple(48000, 44100, -15.01, -64.04),
|
||||
std::tr1::make_tuple(96000, 44100, -18.49, -25.51),
|
||||
std::tr1::make_tuple(192000, 44100, -20.50, -13.31),
|
||||
|
||||
// To 48kHz
|
||||
std::tr1::make_tuple(8000, 48000, kResamplingRMSError, -63.43),
|
||||
std::tr1::make_tuple(11025, 48000, kResamplingRMSError, -62.61),
|
||||
std::tr1::make_tuple(16000, 48000, kResamplingRMSError, -63.96),
|
||||
std::tr1::make_tuple(22050, 48000, kResamplingRMSError, -62.42),
|
||||
std::tr1::make_tuple(32000, 48000, kResamplingRMSError, -64.04),
|
||||
std::tr1::make_tuple(44100, 48000, kResamplingRMSError, -62.63),
|
||||
std::tr1::make_tuple(48000, 48000, kResamplingRMSError, -73.52),
|
||||
std::tr1::make_tuple(96000, 48000, -18.40, -28.44),
|
||||
std::tr1::make_tuple(192000, 48000, -20.43, -14.11),
|
||||
|
||||
// To 96kHz
|
||||
std::tr1::make_tuple(8000, 96000, kResamplingRMSError, -63.19),
|
||||
std::tr1::make_tuple(11025, 96000, kResamplingRMSError, -62.61),
|
||||
std::tr1::make_tuple(16000, 96000, kResamplingRMSError, -63.39),
|
||||
std::tr1::make_tuple(22050, 96000, kResamplingRMSError, -62.42),
|
||||
std::tr1::make_tuple(32000, 96000, kResamplingRMSError, -63.95),
|
||||
std::tr1::make_tuple(44100, 96000, kResamplingRMSError, -62.63),
|
||||
std::tr1::make_tuple(48000, 96000, kResamplingRMSError, -73.52),
|
||||
std::tr1::make_tuple(96000, 96000, kResamplingRMSError, -73.52),
|
||||
std::tr1::make_tuple(192000, 96000, kResamplingRMSError, -28.41),
|
||||
|
||||
// To 192kHz
|
||||
std::tr1::make_tuple(8000, 192000, kResamplingRMSError, -63.10),
|
||||
std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61),
|
||||
std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14),
|
||||
std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42),
|
||||
std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38),
|
||||
std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63),
|
||||
std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44),
|
||||
std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52),
|
||||
std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52)));
|
||||
|
||||
} // namespace media
|
Loading…
x
Reference in New Issue
Block a user