diff options
-rw-r--r-- | media/base/audio_converter.cc | 21 | ||||
-rw-r--r-- | media/base/audio_converter_unittest.cc | 118 | ||||
-rw-r--r-- | media/base/fake_audio_render_callback.cc | 2 | ||||
-rw-r--r-- | media/base/multi_channel_resampler.cc | 23 | ||||
-rw-r--r-- | media/base/multi_channel_resampler.h | 11 | ||||
-rw-r--r-- | media/base/multi_channel_resampler_unittest.cc | 11 | ||||
-rw-r--r-- | media/base/sinc_resampler.cc | 170 | ||||
-rw-r--r-- | media/base/sinc_resampler.h | 48 | ||||
-rw-r--r-- | media/base/sinc_resampler_unittest.cc | 35 | ||||
-rw-r--r-- | remoting/codec/audio_encoder_opus.cc | 9 |
10 files changed, 283 insertions, 165 deletions
diff --git a/media/base/audio_converter.cc b/media/base/audio_converter.cc index 7ffc9ae..d7c09f7 100644 --- a/media/base/audio_converter.cc +++ b/media/base/audio_converter.cc @@ -56,12 +56,14 @@ AudioConverter::AudioConverter(const AudioParameters& input_params, if (input_params.sample_rate() != output_params.sample_rate()) { DVLOG(1) << "Resampling from " << input_params.sample_rate() << " to " << output_params.sample_rate(); - double io_sample_rate_ratio = input_params.sample_rate() / + const double io_sample_rate_ratio = input_params.sample_rate() / static_cast<double>(output_params.sample_rate()); + const int request_size = disable_fifo ? SincResampler::kDefaultRequestSize : + input_params.frames_per_buffer(); resampler_.reset(new MultiChannelResampler( downmix_early_ ? output_params.channels() : input_params.channels(), - io_sample_rate_ratio, base::Bind( + io_sample_rate_ratio, request_size, base::Bind( &AudioConverter::ProvideInput, base::Unretained(this)))); } @@ -72,14 +74,15 @@ AudioConverter::AudioConverter(const AudioParameters& input_params, base::Time::kMicrosecondsPerSecond / static_cast<double>(output_params.sample_rate())); - if (disable_fifo) + // The resampler can be configured to work with a specific request size, so a + // FIFO is not necessary when resampling. + if (disable_fifo || resampler_) return; - // Since the resampler / output device may want a different buffer size than - // the caller asked for, we need to use a FIFO to ensure that both sides - // read in chunk sizes they're configured for. - if (resampler_.get() || - input_params.frames_per_buffer() != output_params.frames_per_buffer()) { + // Since the output device may want a different buffer size than the caller + // asked for, we need to use a FIFO to ensure that both sides read in chunk + // sizes they're configured for. + if (input_params.frames_per_buffer() != output_params.frames_per_buffer()) { DVLOG(1) << "Rebuffering from " << input_params.frames_per_buffer() << " to " << output_params.frames_per_buffer(); audio_fifo_.reset(new AudioPullFifo( @@ -141,7 +144,7 @@ void AudioConverter::ConvertWithDelay(const base::TimeDelta& initial_delay, SourceCallback(0, temp_dest); } else { if (resampler_) - resampler_->Resample(temp_dest, temp_dest->frames()); + resampler_->Resample(temp_dest->frames(), temp_dest); else ProvideInput(0, temp_dest); } diff --git a/media/base/audio_converter_unittest.cc b/media/base/audio_converter_unittest.cc index 95622c9..f693a6d 100644 --- a/media/base/audio_converter_unittest.cc +++ b/media/base/audio_converter_unittest.cc @@ -234,6 +234,19 @@ TEST(AudioConverterTest, AudioDelay) { callback.last_audio_delay_milliseconds()); } +// InputCallback that zero's out the provided AudioBus. Used for benchmarking. +class NullInputProvider : public AudioConverter::InputCallback { + public: + NullInputProvider() {} + virtual ~NullInputProvider() {} + + virtual double ProvideInput(AudioBus* audio_bus, + base::TimeDelta buffer_delay) OVERRIDE { + audio_bus->Zero(); + return 1; + } +}; + // Benchmark for audio conversion. Original benchmarks were run with // --audio-converter-iterations=50000. TEST(AudioConverterTest, ConvertBenchmark) { @@ -244,47 +257,82 @@ TEST(AudioConverterTest, ConvertBenchmark) { if (benchmark_iterations < kDefaultIterations) benchmark_iterations = kDefaultIterations; - // Create input and output parameters to convert between the two most common - // sets of parameters (as indicated via UMA data). + NullInputProvider fake_input1; + NullInputProvider fake_input2; + NullInputProvider fake_input3; + + printf("Benchmarking %d iterations:\n", benchmark_iterations); + + { + // Create input and output parameters to convert between the two most common + // sets of parameters (as indicated via UMA data). + AudioParameters input_params( + AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, + 48000, 16, 2048); + AudioParameters output_params( + AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, + 44100, 16, 440); + scoped_ptr<AudioBus> output_bus = AudioBus::Create(output_params); + + scoped_ptr<AudioConverter> converter( + new AudioConverter(input_params, output_params, true)); + converter->AddInput(&fake_input1); + converter->AddInput(&fake_input2); + converter->AddInput(&fake_input3); + + // Benchmark Convert() w/ FIFO. + base::TimeTicks start = base::TimeTicks::HighResNow(); + for (int i = 0; i < benchmark_iterations; ++i) { + converter->Convert(output_bus.get()); + } + double total_time_ms = + (base::TimeTicks::HighResNow() - start).InMillisecondsF(); + printf("Convert() w/ Resampling took %.2fms.\n", total_time_ms); + } + + // Create input and output parameters to convert between common buffer sizes + // without any resampling for the FIFO vs no FIFO benchmarks. AudioParameters input_params( - AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, 48000, 16, 2048); + AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, + 44100, 16, 2048); AudioParameters output_params( - AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 44100, 16, 440); - scoped_ptr<AudioConverter> converter( - new AudioConverter(input_params, output_params, false)); - + AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, + 44100, 16, 440); scoped_ptr<AudioBus> output_bus = AudioBus::Create(output_params); - FakeAudioRenderCallback fake_input1(0.2); - FakeAudioRenderCallback fake_input2(0.4); - FakeAudioRenderCallback fake_input3(0.6); - converter->AddInput(&fake_input1); - converter->AddInput(&fake_input2); - converter->AddInput(&fake_input3); - - printf("Benchmarking %d iterations:\n", benchmark_iterations); - // Benchmark Convert() w/ FIFO. - base::TimeTicks start = base::TimeTicks::HighResNow(); - for (int i = 0; i < benchmark_iterations; ++i) { - converter->Convert(output_bus.get()); + { + scoped_ptr<AudioConverter> converter( + new AudioConverter(input_params, output_params, false)); + converter->AddInput(&fake_input1); + converter->AddInput(&fake_input2); + converter->AddInput(&fake_input3); + + // Benchmark Convert() w/ FIFO. + base::TimeTicks start = base::TimeTicks::HighResNow(); + for (int i = 0; i < benchmark_iterations; ++i) { + converter->Convert(output_bus.get()); + } + double total_time_ms = + (base::TimeTicks::HighResNow() - start).InMillisecondsF(); + printf("Convert() w/ FIFO took %.2fms.\n", total_time_ms); } - double total_time_ms = - (base::TimeTicks::HighResNow() - start).InMillisecondsF(); - printf("Convert() w/ FIFO took %.2fms.\n", total_time_ms); - - converter.reset(new AudioConverter(input_params, output_params, true)); - converter->AddInput(&fake_input1); - converter->AddInput(&fake_input2); - converter->AddInput(&fake_input3); - - // Benchmark Convert() w/o FIFO. - start = base::TimeTicks::HighResNow(); - for (int i = 0; i < benchmark_iterations; ++i) { - converter->Convert(output_bus.get()); + + { + scoped_ptr<AudioConverter> converter( + new AudioConverter(input_params, output_params, true)); + converter->AddInput(&fake_input1); + converter->AddInput(&fake_input2); + converter->AddInput(&fake_input3); + + // Benchmark Convert() w/o FIFO. + base::TimeTicks start = base::TimeTicks::HighResNow(); + for (int i = 0; i < benchmark_iterations; ++i) { + converter->Convert(output_bus.get()); + } + double total_time_ms = + (base::TimeTicks::HighResNow() - start).InMillisecondsF(); + printf("Convert() w/o FIFO took %.2fms.\n", total_time_ms); } - total_time_ms = - (base::TimeTicks::HighResNow() - start).InMillisecondsF(); - printf("Convert() w/o FIFO took %.2fms.\n", total_time_ms); } TEST_P(AudioConverterTest, NoInputs) { diff --git a/media/base/fake_audio_render_callback.cc b/media/base/fake_audio_render_callback.cc index af55910..5a0979e 100644 --- a/media/base/fake_audio_render_callback.cc +++ b/media/base/fake_audio_render_callback.cc @@ -43,7 +43,7 @@ int FakeAudioRenderCallback::Render(AudioBus* audio_bus, double FakeAudioRenderCallback::ProvideInput(AudioBus* audio_bus, base::TimeDelta buffer_delay) { - Render(audio_bus, buffer_delay.InMilliseconds()); + Render(audio_bus, buffer_delay.InMillisecondsF() + 0.5); return volume_; } diff --git a/media/base/multi_channel_resampler.cc b/media/base/multi_channel_resampler.cc index 9aa00d8..a9aa8d6 100644 --- a/media/base/multi_channel_resampler.cc +++ b/media/base/multi_channel_resampler.cc @@ -13,6 +13,7 @@ namespace media { MultiChannelResampler::MultiChannelResampler(int channels, double io_sample_rate_ratio, + size_t request_size, const ReadCB& read_cb) : last_frame_count_(0), read_cb_(read_cb), @@ -20,23 +21,30 @@ MultiChannelResampler::MultiChannelResampler(int channels, // 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))); + resamplers_.push_back(new SincResampler( + io_sample_rate_ratio, request_size, base::Bind( + &MultiChannelResampler::ProvideInput, base::Unretained(this), i))); } } MultiChannelResampler::~MultiChannelResampler() {} -void MultiChannelResampler::Resample(AudioBus* audio_bus, int frames) { +void MultiChannelResampler::Resample(int frames, AudioBus* audio_bus) { DCHECK_EQ(static_cast<size_t>(audio_bus->channels()), resamplers_.size()); + // Optimize the single channel case to avoid the chunking process below. + if (audio_bus->channels() == 1) { + resamplers_[0]->Resample(frames, audio_bus->channel(0)); + return; + } + // 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. output_frames_ready_ = 0; - int chunk_size = resamplers_[0]->ChunkSize(); while (output_frames_ready_ < frames) { + int chunk_size = resamplers_[0]->ChunkSize(); int frames_this_time = std::min(frames - output_frames_ready_, chunk_size); // Resample each channel. @@ -50,15 +58,16 @@ void MultiChannelResampler::Resample(AudioBus* audio_bus, int frames) { // since they all buffer in the same way and are processing the same // number of frames. resamplers_[i]->Resample( - audio_bus->channel(i) + output_frames_ready_, frames_this_time); + frames_this_time, audio_bus->channel(i) + output_frames_ready_); } output_frames_ready_ += frames_this_time; } } -void MultiChannelResampler::ProvideInput(int channel, float* destination, - int frames) { +void MultiChannelResampler::ProvideInput(int channel, + int frames, + float* destination) { // 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_bus_|). diff --git a/media/base/multi_channel_resampler.h b/media/base/multi_channel_resampler.h index 549ddea..88b7ceb 100644 --- a/media/base/multi_channel_resampler.h +++ b/media/base/multi_channel_resampler.h @@ -27,13 +27,16 @@ class MEDIA_EXPORT MultiChannelResampler { // 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, + // ratio of input / output sample rates. |request_frames| is the size in + // frames of the AudioBus to be filled by |read_cb|. + MultiChannelResampler(int channels, + double io_sample_rate_ratio, + size_t request_frames, const ReadCB& read_cb); virtual ~MultiChannelResampler(); // Resamples |frames| of data from |read_cb_| into AudioBus. - void Resample(AudioBus* audio_bus, int frames); + void Resample(int frames, AudioBus* audio_bus); // Flush all buffered data and reset internal indices. Not thread safe, do // not call while Resample() is in progress. @@ -47,7 +50,7 @@ class MEDIA_EXPORT MultiChannelResampler { 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); + void ProvideInput(int channel, int frames, float* destination); // Sanity check to ensure that ProvideInput() retrieves the same number of // frames for every channel. diff --git a/media/base/multi_channel_resampler_unittest.cc b/media/base/multi_channel_resampler_unittest.cc index ad67550..efaf0c5 100644 --- a/media/base/multi_channel_resampler_unittest.cc +++ b/media/base/multi_channel_resampler_unittest.cc @@ -50,7 +50,7 @@ class MultiChannelResamplerTest // MultiChannelResampler::MultiChannelAudioSourceProvider implementation, just // fills the provided audio_data with |kFillValue|. virtual void ProvideInput(int frame_delay, AudioBus* audio_bus) { - EXPECT_GT(frame_delay, last_frame_delay_); + EXPECT_GE(frame_delay, last_frame_delay_); last_frame_delay_ = frame_delay; float fill_value = fill_junk_values_ ? (1 / kFillValue) : kFillValue; @@ -63,12 +63,13 @@ class MultiChannelResamplerTest void MultiChannelTest(int channels, int frames, double expected_max_rms_error, double expected_max_error) { InitializeAudioData(channels, frames); - MultiChannelResampler resampler(channels, kScaleFactor, base::Bind( - &MultiChannelResamplerTest::ProvideInput, base::Unretained(this))); + MultiChannelResampler resampler( + channels, kScaleFactor, SincResampler::kDefaultRequestSize, base::Bind( + &MultiChannelResamplerTest::ProvideInput, base::Unretained(this))); // First prime the resampler with some junk data, so we can verify Flush(). fill_junk_values_ = true; - resampler.Resample(audio_bus_.get(), 1); + resampler.Resample(1, audio_bus_.get()); resampler.Flush(); fill_junk_values_ = false; @@ -77,7 +78,7 @@ class MultiChannelResamplerTest last_frame_delay_ = -1; // If Flush() didn't work, the rest of the tests will fail. - resampler.Resample(audio_bus_.get(), frames); + resampler.Resample(frames, audio_bus_.get()); TestValues(expected_max_rms_error, expected_max_error); } diff --git a/media/base/sinc_resampler.cc b/media/base/sinc_resampler.cc index 6bce67a..c253b83 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, diff --git a/media/base/sinc_resampler.h b/media/base/sinc_resampler.h index 3ccb85c..84695b1 100644 --- a/media/base/sinc_resampler.h +++ b/media/base/sinc_resampler.h @@ -23,38 +23,35 @@ class MEDIA_EXPORT SincResampler { // 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, + // Default request size. Affects how often and for how much SincResampler + // calls back for input. Must be greater than kKernelSize. + kDefaultRequestSize = 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, - - // The maximum number of samples that may be requested from the callback - // ahead of the current position in the stream. - kMaximumLookAheadSize = kBufferSize }; // 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; + typedef base::Callback<void(int frames, float* destination)> 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); + // acquire audio data for resampling. |io_sample_rate_ratio| is the ratio + // of input / output sample rates. |request_frames| controls the size in + // frames of the buffer requested by each |read_cb| call. The value must be + // greater than kKernelSize. Specify kDefaultRequestSize if there are no + // request size constraints. + SincResampler(double io_sample_rate_ratio, + size_t request_frames, + const ReadCB& read_cb); virtual ~SincResampler(); // Resample |frames| of data from |read_cb_| into |destination|. - void Resample(float* destination, int frames); + void Resample(int frames, float* destination); // The maximum size in frames that guarantees Resample() will only make a // single call to |read_cb_| for more data. @@ -76,6 +73,7 @@ class MEDIA_EXPORT SincResampler { FRIEND_TEST_ALL_PREFIXES(SincResamplerTest, ConvolveBenchmark); void InitializeKernel(); + void UpdateRegions(bool second_load); // Compute convolution of |k1| and |k2| over |input_ptr|, resultant sums are // linearly interpolated using |kernel_interpolation_factor|. On x86, the @@ -104,7 +102,16 @@ class MEDIA_EXPORT SincResampler { bool buffer_primed_; // Source of data for resampling. - ReadCB read_cb_; + const ReadCB read_cb_; + + // The size (in samples) to request from each |read_cb_| execution. + const size_t request_frames_; + + // The number of source frames processed per pass. + size_t block_size_; + + // The size (in samples) of the internal buffer used by the resampler. + const size_t input_buffer_size_; // Contains kKernelOffsetCount kernels back-to-back, each of size kKernelSize. // The kernel offsets are sub-sample shifts of a windowed sinc shifted from @@ -125,12 +132,11 @@ class MEDIA_EXPORT SincResampler { // 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* r0_; float* const r1_; float* const r2_; - float* const r3_; - float* const r4_; - float* const r5_; + float* r3_; + float* r4_; DISALLOW_COPY_AND_ASSIGN(SincResampler); }; diff --git a/media/base/sinc_resampler_unittest.cc b/media/base/sinc_resampler_unittest.cc index 39b2dbe..74a73cc 100644 --- a/media/base/sinc_resampler_unittest.cc +++ b/media/base/sinc_resampler_unittest.cc @@ -33,18 +33,18 @@ static const char kConvolveIterations[] = "convolve-iterations"; // Helper class to ensure ChunkedResample() functions properly. class MockSource { public: - MOCK_METHOD2(ProvideInput, void(float* destination, int frames)); + MOCK_METHOD2(ProvideInput, void(int frames, float* destination)); }; ACTION(ClearBuffer) { - memset(arg0, 0, arg1 * sizeof(float)); + memset(arg1, 0, arg0 * 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)); + memset(arg1, 64, arg0 * sizeof(float)); } // Test requesting multiples of ChunkSize() frames results in the proper number @@ -55,7 +55,7 @@ TEST(SincResamplerTest, ChunkedResample) { // Choose a high ratio of input to output samples which will result in quick // exhaustion of SincResampler's internal buffers. SincResampler resampler( - kSampleRateRatio, + kSampleRateRatio, SincResampler::kDefaultRequestSize, base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); static const int kChunks = 2; @@ -65,27 +65,27 @@ TEST(SincResamplerTest, ChunkedResample) { // Verify requesting ChunkSize() frames causes a single callback. EXPECT_CALL(mock_source, ProvideInput(_, _)) .Times(1).WillOnce(ClearBuffer()); - resampler.Resample(resampled_destination.get(), resampler.ChunkSize()); + resampler.Resample(resampler.ChunkSize(), resampled_destination.get()); // 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); + resampler.Resample(max_chunk_size, resampled_destination.get()); } // Test flush resets the internal state properly. TEST(SincResamplerTest, Flush) { MockSource mock_source; SincResampler resampler( - kSampleRateRatio, + kSampleRateRatio, SincResampler::kDefaultRequestSize, base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); scoped_ptr<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); + resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); ASSERT_NE(resampled_destination[0], 0); // Flush and request more data, which should all be zeros now. @@ -93,7 +93,7 @@ TEST(SincResamplerTest, Flush) { testing::Mock::VerifyAndClear(&mock_source); EXPECT_CALL(mock_source, ProvideInput(_, _)) .Times(1).WillOnce(ClearBuffer()); - resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); + resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); for (int i = 0; i < resampler.ChunkSize() / 2; ++i) ASSERT_FLOAT_EQ(resampled_destination[i], 0); } @@ -102,7 +102,7 @@ TEST(SincResamplerTest, Flush) { TEST(SincResamplerTest, DISABLED_SetRatioBench) { MockSource mock_source; SincResampler resampler( - kSampleRateRatio, + kSampleRateRatio, SincResampler::kDefaultRequestSize, base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); base::TimeTicks start = base::TimeTicks::HighResNow(); @@ -133,7 +133,7 @@ TEST(SincResamplerTest, Convolve) { // Initialize a dummy resampler. MockSource mock_source; SincResampler resampler( - kSampleRateRatio, + kSampleRateRatio, SincResampler::kDefaultRequestSize, base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); // The optimized Convolve methods are slightly more precise than Convolve_C(), @@ -168,7 +168,7 @@ TEST(SincResamplerTest, ConvolveBenchmark) { // Initialize a dummy resampler. MockSource mock_source; SincResampler resampler( - kSampleRateRatio, + kSampleRateRatio, SincResampler::kDefaultRequestSize, base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); // Retrieve benchmark iterations from command line. @@ -234,7 +234,8 @@ TEST(SincResamplerTest, ConvolveBenchmark) { // resampler for the specific sample rate conversion being used. class SinusoidalLinearChirpSource { public: - SinusoidalLinearChirpSource(int sample_rate, int samples, + SinusoidalLinearChirpSource(int sample_rate, + int samples, double max_frequency) : sample_rate_(sample_rate), total_samples_(samples), @@ -247,7 +248,7 @@ class SinusoidalLinearChirpSource { virtual ~SinusoidalLinearChirpSource() {} - void ProvideInput(float* destination, int frames) { + void ProvideInput(int frames, float* destination) { for (int i = 0; i < frames; ++i, ++current_index_) { // Filter out frequencies higher than Nyquist. if (Frequency(current_index_) > 0.5 * sample_rate_) { @@ -317,7 +318,7 @@ TEST_P(SincResamplerTest, Resample) { const double io_ratio = input_rate_ / static_cast<double>(output_rate_); SincResampler resampler( - io_ratio, + io_ratio, SincResampler::kDefaultRequestSize, base::Bind(&SinusoidalLinearChirpSource::ProvideInput, base::Unretained(&resampler_source))); @@ -339,12 +340,12 @@ TEST_P(SincResamplerTest, Resample) { scoped_ptr<float[]> pure_destination(new float[output_samples]); // Generate resampled signal. - resampler.Resample(resampled_destination.get(), output_samples); + resampler.Resample(output_samples, resampled_destination.get()); // Generate pure signal. SinusoidalLinearChirpSource pure_source( output_rate_, output_samples, input_nyquist_freq); - pure_source.ProvideInput(pure_destination.get(), output_samples); + pure_source.ProvideInput(output_samples, pure_destination.get()); // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which // we refer to as low and high. diff --git a/remoting/codec/audio_encoder_opus.cc b/remoting/codec/audio_encoder_opus.cc index 15160df..2efcf90 100644 --- a/remoting/codec/audio_encoder_opus.cc +++ b/remoting/codec/audio_encoder_opus.cc @@ -77,9 +77,12 @@ void AudioEncoderOpus::InitEncoder() { if (sampling_rate_ != kOpusSamplingRate) { resample_buffer_.reset( new char[kFrameSamples * kBytesPerSample * channels_]); + // TODO(sergeyu): Figure out the right buffer size to use per packet instead + // of using media::SincResampler::kDefaultRequestSize. resampler_.reset(new media::MultiChannelResampler( channels_, static_cast<double>(sampling_rate_) / kOpusSamplingRate, + media::SincResampler::kDefaultRequestSize, base::Bind(&AudioEncoderOpus::FetchBytesToResample, base::Unretained(this)))); resampler_bus_ = media::AudioBus::Create(channels_, kFrameSamples); @@ -88,7 +91,7 @@ void AudioEncoderOpus::InitEncoder() { // Drop leftover data because it's for different sampling rate. leftover_samples_ = 0; leftover_buffer_size_ = - frame_size_ + media::SincResampler::kMaximumLookAheadSize; + frame_size_ + media::SincResampler::kDefaultRequestSize; leftover_buffer_.reset( new int16[leftover_buffer_size_ * channels_]); } @@ -159,7 +162,7 @@ scoped_ptr<AudioPacket> AudioEncoderOpus::Encode( encoded_packet->set_channels(channels_); int prefetch_samples = - resampler_.get() ? media::SincResampler::kMaximumLookAheadSize : 0; + resampler_.get() ? media::SincResampler::kDefaultRequestSize : 0; int samples_wanted = frame_size_ + prefetch_samples; while (leftover_samples_ + samples_in_packet >= samples_wanted) { @@ -181,7 +184,7 @@ scoped_ptr<AudioPacket> AudioEncoderOpus::Encode( resampling_data_ = reinterpret_cast<const char*>(pcm_buffer); resampling_data_pos_ = 0; resampling_data_size_ = samples_wanted * channels_ * kBytesPerSample; - resampler_->Resample(resampler_bus_.get(), kFrameSamples); + resampler_->Resample(kFrameSamples, resampler_bus_.get()); resampling_data_ = NULL; samples_consumed = resampling_data_pos_ / channels_ / kBytesPerSample; |