Add MultiChannelResampler wrapper for SincResampler.

Wraps the SincResampler for multichannel use.  Can't land until after
https://chromiumcodereview.appspot.com/10702050/ lands.

Since the SincResampler unit tests focus on accuracy, the multi channel
resampling tests just ensure the wrapper functions as expected with a
single resampling frequency and a constant fill value.

BUG=133637
TEST=MultiChannelResampler Unittests.


Review URL: https://chromiumcodereview.appspot.com/10701049

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146254 0039d316-1c4b-4281-b951-d872f2087c98
parent b0274b34
// 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.
#include "media/base/multi_channel_resampler.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
namespace media {
MultiChannelResampler::MultiChannelResampler(int channels,
double io_sample_rate_ratio,
const ReadCB& read_cb)
: last_frame_count_(0),
first_frame_count_(0),
read_cb_(read_cb) {
// Allocate each channel's resampler.
resamplers_.reserve(channels);
for (int i = 0; i < channels; ++i) {
resamplers_.push_back(new SincResampler(io_sample_rate_ratio, base::Bind(
&MultiChannelResampler::ProvideInput, base::Unretained(this), i)));
}
}
MultiChannelResampler::~MultiChannelResampler() {
// Clean up |resampler_audio_data_|. Skip the first channel since we never
// allocated that, but just used the destination passed into ProvideInput().
for (size_t i = 1; i < resampler_audio_data_.size(); ++i)
delete [] resampler_audio_data_[i];
resampler_audio_data_.clear();
}
void MultiChannelResampler::Resample(const std::vector<float*>& destination,
int frames) {
DCHECK_EQ(destination.size(), resamplers_.size());
// We need to ensure that SincResampler only calls ProvideInput once for each
// channel. To ensure this, we chunk the number of requested frames into
// SincResampler::ChunkSize() sized chunks. SincResampler guarantees it will
// only call ProvideInput() once when we resample this way.
int frames_done = 0;
int chunk_size = resamplers_[0]->ChunkSize();
while (frames_done < frames) {
int frames_this_time = std::min(frames - frames_done, chunk_size);
// Resample each channel.
for (size_t i = 0; i < resamplers_.size(); ++i) {
DCHECK_EQ(chunk_size, resamplers_[i]->ChunkSize());
// Depending on the sample-rate scale factor, and the internal buffering
// used in a SincResampler kernel, this call to Resample() will only
// sometimes call ProvideInput(). However, if it calls ProvideInput() for
// the first channel, then it will call it for the remaining channels,
// since they all buffer in the same way and are processing the same
// number of frames.
resamplers_[i]->Resample(destination[i] + frames_done, frames_this_time);
}
frames_done += frames_this_time;
}
}
void MultiChannelResampler::ProvideInput(int channel, float* destination,
int frames) {
// Get the data from the multi-channel provider when the first channel asks
// for it. For subsequent channels, we can just dish out the channel data
// from that (stored in |resampler_audio_data_|).
if (channel == 0) {
// Allocate staging arrays on the first request.
if (resampler_audio_data_.size() == 0) {
first_frame_count_ = frames;
// Skip allocation of the first buffer, since we'll use |destination|
// directly for that.
resampler_audio_data_.reserve(resamplers_.size());
resampler_audio_data_.push_back(destination);
for (size_t i = 1; i < resamplers_.size(); ++i)
resampler_audio_data_.push_back(new float[frames]);
} else {
DCHECK_LE(frames, first_frame_count_);
resampler_audio_data_[0] = destination;
}
last_frame_count_ = frames;
read_cb_.Run(resampler_audio_data_, frames);
} else {
// All channels must ask for the same amount. This should always be the
// case, but let's just make sure.
DCHECK_EQ(frames, last_frame_count_);
// Copy the channel data from what we received from |read_cb_|.
memcpy(destination, resampler_audio_data_[channel],
sizeof(*resampler_audio_data_[channel]) * frames);
}
}
} // namespace media
// 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_MULTI_CHANNEL_RESAMPLER_H_
#define MEDIA_BASE_MULTI_CHANNEL_RESAMPLER_H_
#include <vector>
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "media/base/sinc_resampler.h"
namespace media {
// MultiChannelResampler is a multi channel wrapper for SincResampler; allowing
// high quality sample rate conversion of multiple channels at once.
class MEDIA_EXPORT MultiChannelResampler {
public:
// Callback type for providing more data into the resampler. Expects |frames|
// of data for all channels to be rendered into |destination|; zero padded if
// not enough frames are available to satisfy the request.
typedef base::Callback<void(const std::vector<float*>& destination,
int frames)> ReadCB;
// Constructs a MultiChannelResampler 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.
MultiChannelResampler(int channels, double io_sample_rate_ratio,
const ReadCB& read_cb);
virtual ~MultiChannelResampler();
// Resample |frames| of data from |read_cb_| into |destination|.
void Resample(const std::vector<float*>& destination, int frames);
private:
// SincResampler::ReadCB implementation. ProvideInput() will be called for
// each channel (in channel order) as SincResampler needs more data.
void ProvideInput(int channel, float* destination, int frames);
// Sanity check to ensure that ProvideInput() retrieves the same number of
// frames for every channel.
int last_frame_count_;
// Sanity check to ensure |resampler_audio_data_| is properly allocated.
int first_frame_count_;
// Source of data for resampling.
ReadCB read_cb_;
// Each channel has its own high quality resampler.
ScopedVector<SincResampler> resamplers_;
// Buffer for audio data going into SincResampler from ReadCB. Owned by this
// class and only temporarily passed out to ReadCB when data is required.
std::vector<float*> resampler_audio_data_;
};
} // namespace media
#endif // MEDIA_BASE_MULTI_CHANNEL_RESAMPLER_H_
// 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.
#include <cmath>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "media/base/multi_channel_resampler.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
// Just test a basic resampling case. The SincResampler unit test will take
// care of accuracy testing; we just need to check that multichannel works as
// expected within some tolerance.
static const float kScaleFactor = 192000.0f / 44100.0f;
// Simulate large and small sample requests used by the different audio paths.
static const int kHighLatencySize = 8192;
// Low latency buffers show a larger error than high latency ones. Which makes
// sense since each error represents a larger portion of the total request.
static const int kLowLatencySize = 128;
// Test fill value.
static const float kFillValue = 0.1f;
// Chosen arbitrarily based on what each resampler reported during testing.
static const double kLowLatencyMaxRMSError = 0.0036;
static const double kLowLatencyMaxError = 0.04;
static const double kHighLatencyMaxRMSError = 0.0036;
static const double kHighLatencyMaxError = 0.04;
class MultiChannelResamplerTestCase
: public testing::TestWithParam<int> {
public:
MultiChannelResamplerTestCase() {}
virtual ~MultiChannelResamplerTestCase() {
if (!audio_data_.empty()) {
for (size_t i = 0; i < audio_data_.size(); ++i)
delete [] audio_data_[i];
audio_data_.clear();
}
}
void InitializeAudioData(int channels, int frames) {
frames_ = frames;
audio_data_.reserve(channels);
for (int i = 0; i < channels; ++i) {
audio_data_.push_back(new float[frames]);
// Zero initialize so we can be sure every value has been provided.
memset(audio_data_[i], 0, sizeof(*audio_data_[i]) * frames);
}
}
// MultiChannelResampler::MultiChannelAudioSourceProvider implementation, just
// fills the provided audio_data with |kFillValue|.
virtual void ProvideInput(const std::vector<float*>& audio_data,
int number_of_frames) {
EXPECT_EQ(audio_data.size(), audio_data_.size());
for (size_t i = 0; i < audio_data.size(); ++i)
for (int j = 0; j < number_of_frames; ++j)
audio_data[i][j] = kFillValue;
}
void MultiChannelTest(int channels, int frames, double expected_max_rms_error,
double expected_max_error) {
InitializeAudioData(channels, frames);
MultiChannelResampler resampler(
channels, kScaleFactor, base::Bind(
&MultiChannelResamplerTestCase::ProvideInput,
base::Unretained(this)));
resampler.Resample(audio_data_, frames);
TestValues(expected_max_rms_error, expected_max_error);
}
void HighLatencyTest(int channels) {
MultiChannelTest(channels, kHighLatencySize, kHighLatencyMaxRMSError,
kHighLatencyMaxError);
}
void LowLatencyTest(int channels) {
MultiChannelTest(channels, kLowLatencySize, kLowLatencyMaxRMSError,
kLowLatencyMaxError);
}
void TestValues(double expected_max_rms_error, double expected_max_error ) {
// Calculate Root-Mean-Square-Error for the resampling.
double max_error = 0.0;
double sum_of_squares = 0.0;
for (size_t i = 0; i < audio_data_.size(); ++i) {
for (int j = 0; j < frames_; ++j) {
// Ensure all values are accounted for.
ASSERT_NE(audio_data_[i][j], 0);
double error = fabs(audio_data_[i][j] - kFillValue);
max_error = std::max(max_error, error);
sum_of_squares += error * error;
}
}
double rms_error = sqrt(
sum_of_squares / (frames_ * audio_data_.size()));
EXPECT_LE(rms_error, expected_max_rms_error);
EXPECT_LE(max_error, expected_max_error);
}
protected:
int frames_;
std::vector<float*> audio_data_;
DISALLOW_COPY_AND_ASSIGN(MultiChannelResamplerTestCase);
};
TEST_P(MultiChannelResamplerTestCase, HighLatency) {
HighLatencyTest(GetParam());
}
TEST_P(MultiChannelResamplerTestCase, LowLatency) {
LowLatencyTest(GetParam());
}
// Test common channel layouts: mono, stereo, 5.1, 7.1.
INSTANTIATE_TEST_CASE_P(
MultiChannelResamplerTest, MultiChannelResamplerTestCase,
testing::Values(1, 2, 6, 8));
} // namespace media
......@@ -181,6 +181,8 @@
'base/media_win.cc',
'base/message_loop_factory.cc',
'base/message_loop_factory.h',
'base/multi_channel_resampler.cc',
'base/multi_channel_resampler.h',
'base/pipeline.cc',
'base/pipeline.h',
'base/pipeline_status.cc',
......@@ -684,6 +686,7 @@
'base/fake_audio_render_callback.h',
'base/filter_collection_unittest.cc',
'base/h264_bitstream_converter_unittest.cc',
'base/multi_channel_resampler_unittest.cc',
'base/pipeline_unittest.cc',
'base/ranges_unittest.cc',
'base/run_all_unittests.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment