Chromium Code Reviews| Index: media/base/audio_bus.cc |
| diff --git a/media/base/audio_bus.cc b/media/base/audio_bus.cc |
| index 4c43af7a4856af4a3f33655c94e78abe8313f8bf..957302ad8d6133bb2d1b2db3cef9527a46c961d7 100644 |
| --- a/media/base/audio_bus.cc |
| +++ b/media/base/audio_bus.cc |
| @@ -19,33 +19,60 @@ static bool IsAligned(void* ptr) { |
| return (reinterpret_cast<uintptr_t>(ptr) & (kChannelAlignment - 1)) == 0U; |
| } |
| -AudioBus::AudioBus(int channels, int frames) |
| - : frames_(frames) { |
| - CHECK_GT(frames, 0); |
| - CHECK_LE(frames, limits::kMaxSamplesPerPacket); |
| - CHECK_GT(channels, 0); |
| - CHECK_LE(channels, limits::kMaxChannels); |
| - DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, |
| - std::numeric_limits<int>::max()); |
| - |
| +// Calculates the expected data_size() for an AudioBus with the given params, |
| +// sets |aligned_frames| to the actual frame length of each channel array. |
| +static int ExpectedDataSizeInternal(int channels, int frames, |
| + int* aligned_frames) { |
|
Chris Rogers
2012/08/15 19:39:31
NULL-check aligned_frames
DaleCurtis
2012/08/16 01:54:14
Done.
|
| // Choose a size such that each channel is aligned by kChannelAlignment. |
| - int aligned_frames = |
| - (frames_ + kChannelAlignment - 1) & ~(kChannelAlignment - 1); |
| - data_size_ = sizeof(float) * channels * aligned_frames; |
| + *aligned_frames = |
| + (frames + kChannelAlignment - 1) & ~(kChannelAlignment - 1); |
| + // Include an extra kChannelAlignment bytes in case WrapBlock() needs to |
| + // manually align the start of the first channel. |
| + return sizeof(float) * channels * *aligned_frames + kChannelAlignment; |
| +} |
| + |
| +AudioBus::AudioBus(int channels, int frames, int sample_rate) |
| + : frames_(frames), |
| + sample_rate_(sample_rate) { |
| + ValidateConfig(channels, frames_, sample_rate_); |
| + |
| + int aligned_frames = 0; |
| + data_size_ = ExpectedDataSizeInternal(channels, frames, &aligned_frames); |
| data_.reset(static_cast<float*>(base::AlignedAlloc( |
| data_size_, kChannelAlignment))); |
|
Chris Rogers
2012/08/15 19:39:31
Are we now allocating more than we need to in orde
DaleCurtis
2012/08/15 20:12:54
We're allocating more than we need since we don't
|
| - // Separate audio data out into channels for easy lookup later. |
| - channel_data_.reserve(channels); |
| - for (int i = 0; i < channels; ++i) |
| - channel_data_.push_back(data_.get() + i * aligned_frames); |
| + BuildChannelData(channels, aligned_frames, data_.get()); |
| +} |
| + |
| +AudioBus::AudioBus(int channels, int frames, int sample_rate, float* data) |
| + : data_size_(0), |
| + frames_(frames), |
| + sample_rate_(sample_rate) { |
| + ValidateConfig(channels, frames_, sample_rate_); |
| + |
| + // Manually align the input pointer if it's not already aligned. We pad the |
| + // amount returned by ExpectedDataSize() just for this case. |
| + uintptr_t aligned_ptr = reinterpret_cast<uintptr_t>(data); |
| + if (!IsAligned(data)) |
| + aligned_ptr += kChannelAlignment - (aligned_ptr & (kChannelAlignment - 1)); |
| + |
| + // Don't set |data_size_| based on expected data size, since we do not own the |
| + // block of memory provided for channel data. |
| + int aligned_frames = 0; |
| + ExpectedDataSizeInternal(channels, frames, &aligned_frames); |
| + |
| + BuildChannelData( |
| + channels, aligned_frames, reinterpret_cast<float*>(aligned_ptr)); |
| } |
| AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) |
| : data_size_(0), |
| channel_data_(channel_data), |
| - frames_(frames) { |
| + frames_(frames), |
| + sample_rate_(0) { |
| + ValidateConfig(channel_data_.size(), frames_, sample_rate_); |
| + |
| // Sanity check wrapped vector for alignment and channel count. |
| for (size_t i = 0; i < channel_data_.size(); ++i) |
| DCHECK(IsAligned(channel_data_[i])); |
| @@ -54,12 +81,12 @@ AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) |
| AudioBus::~AudioBus() {} |
| scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { |
| - return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); |
| + return scoped_ptr<AudioBus>(new AudioBus(channels, frames, 0)); |
| } |
| scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { |
| return scoped_ptr<AudioBus>(new AudioBus( |
| - params.channels(), params.frames_per_buffer())); |
| + params.channels(), params.frames_per_buffer(), params.sample_rate())); |
| } |
| scoped_ptr<AudioBus> AudioBus::WrapVector( |
| @@ -67,6 +94,18 @@ scoped_ptr<AudioBus> AudioBus::WrapVector( |
| return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); |
| } |
| +scoped_ptr<AudioBus> AudioBus::WrapBlock(int channels, int frames, void* data) { |
| + return scoped_ptr<AudioBus>(new AudioBus( |
| + channels, frames, 0, static_cast<float*>(data))); |
| +} |
| + |
| +scoped_ptr<AudioBus> AudioBus::WrapBlock(const AudioParameters& params, |
| + void* data) { |
| + return scoped_ptr<AudioBus>(new AudioBus( |
| + params.channels(), params.frames_per_buffer(), params.sample_rate(), |
| + static_cast<float*>(data))); |
| +} |
| + |
| void* AudioBus::data() { |
| DCHECK(data_.get()); |
| return data_.get(); |
| @@ -87,4 +126,149 @@ void AudioBus::Zero() { |
| ZeroFrames(frames_); |
| } |
| +int AudioBus::sample_rate() const { |
| + DCHECK_NE(sample_rate_, 0); |
| + return sample_rate_; |
| +} |
| + |
| +int AudioBus::ExpectedDataSize(int channels, int frames) { |
| + int aligned_frames = 0; |
| + return ExpectedDataSizeInternal(channels, frames, &aligned_frames); |
| +} |
| + |
| +int AudioBus::ExpectedDataSize(const AudioParameters& params) { |
| + int aligned_frames = 0; |
| + return ExpectedDataSizeInternal( |
| + params.channels(), params.frames_per_buffer(), &aligned_frames); |
| +} |
| + |
| +void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { |
| + DCHECK(IsAligned(data)); |
| + DCHECK_EQ(channel_data_.size(), 0U); |
| + // Separate audio data out into channels for easy lookup later. Figure out |
| + channel_data_.reserve(channels); |
| + for (int i = 0; i < channels; ++i) |
| + channel_data_.push_back(data + i * aligned_frames); |
| +} |
| + |
| +void AudioBus::ValidateConfig(int channels, int frames, int sample_rate) { |
| + CHECK_GT(frames, 0); |
| + CHECK_LE(frames, limits::kMaxSamplesPerPacket); |
| + CHECK_GT(channels, 0); |
| + CHECK_LE(channels, limits::kMaxChannels); |
| + DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, |
| + std::numeric_limits<int>::max()); |
| + if (sample_rate != 0) |
| + DCHECK_GT(sample_rate, limits::kMinSampleRate); |
| + DCHECK_LT(sample_rate, limits::kMaxSampleRate); |
| +} |
| + |
| +// TODO(dalecurtis): See if intrinsic optimizations help any here. |
| +void AudioBus::FromInterleaved(const void* source, int frames, |
|
henrika (OOO until Aug 14)
2012/08/15 09:59:37
Perhaps mention that these methods are related to
DaleCurtis
2012/08/16 01:54:14
Ideally those methods will go away and these will
|
| + int bytes_per_sample) { |
| + DCHECK_LE(frames, frames_); |
| + |
| + switch (bytes_per_sample) { |
| + case 1: { |
| + static const float kMinScale = 1.0f / kint8min; |
| + static const float kMaxScale = 1.0f / kint8max; |
| + const uint8* source8 = reinterpret_cast<const uint8*>(source); |
| + for (int i = 0; i < frames; ++i) { |
| + for (int ch = 0; ch < channels(); ++ch) { |
| + int v = static_cast<int>(*source8++) - (-kint8min); |
| + channel(ch)[i] = v * (v < 0 ? -kMinScale : kMaxScale); |
|
Chris Rogers
2012/08/15 19:39:31
It seems like it would be more efficient to revers
DaleCurtis
2012/08/15 20:12:54
How about iterating over i=0..frames * channels an
DaleCurtis
2012/08/16 01:54:14
I did some basic benchmarking and the new template
|
| + } |
| + } |
| + break; |
| + } |
| + |
| + case 2: { |
| + static const float kMinScale = 1.0f / kint16min; |
| + static const float kMaxScale = 1.0f / kint16max; |
| + const int16* source16 = reinterpret_cast<const int16*>(source); |
| + for (int i = 0; i < frames; ++i) { |
| + for (int ch = 0; ch < channels(); ++ch) { |
| + float v = *source16++; |
|
henrika (OOO until Aug 14)
2012/08/15 09:59:37
Is it not more clear to use static_cast<> here?
DaleCurtis
2012/08/16 01:54:14
I think that would be over-verbose.
|
| + channel(ch)[i] = v * (v < 0 ? -kMinScale : kMaxScale); |
| + } |
| + } |
| + break; |
| + } |
| + |
| + case 4: { |
| + static const float kMinScale = 1.0f / kint32min; |
| + static const float kMaxScale = 1.0f / kint32max; |
| + const int32* source32 = reinterpret_cast<const int32*>(source); |
| + for (int i = 0; i < frames; ++i) { |
| + for (int ch = 0; ch < channels(); ++ch) { |
| + float v = *source32++; |
| + channel(ch)[i] = v * (v < 0 ? -kMinScale : kMaxScale); |
| + } |
| + } |
| + break; |
| + } |
| + |
| + default: |
| + CHECK(false) << "Unsupported bytes per sample encountered."; |
| + } |
| + |
| + // Zero any remaining frames. |
| + int remaining_frames = (frames_ - frames); |
| + if (remaining_frames) { |
| + for (int ch = 0; ch < channels(); ++ch) |
| + memset(channel(ch) + frames, 0, sizeof(*channel(ch)) * remaining_frames); |
| + } |
| +} |
| + |
| +// |Format| is the destination type, |Fixed| is a type larger than |Format| |
| +// such that operations can be made without overflowing. |
| +template<class Format, class Fixed> |
| +static void ToInterleavedInternal(const AudioBus* source, void* dest_bytes, |
| + int frames) { |
| + Format* destination = reinterpret_cast<Format*>(dest_bytes); |
| + Fixed max_value = std::numeric_limits<Format>::max(); |
| + Fixed min_value = std::numeric_limits<Format>::min(); |
| + |
| + Format bias = 0; |
| + if (!std::numeric_limits<Format>::is_signed) { |
| + bias = (max_value / 2) + 1; |
| + max_value = bias - 1; |
| + min_value = -bias; |
| + } |
| + |
| + int channels = source->channels(); |
| + for (int i = 0; i < channels; ++i) { |
| + const float* channel_data = source->channel(i); |
| + for (int j = 0; j < frames; ++j) { |
| + Fixed sample = |
| + (channel_data[j] < 0 ? -min_value : max_value) * channel_data[j]; |
| + if (sample > max_value) |
| + sample = max_value; |
| + else if (sample < min_value) |
| + sample = min_value; |
| + |
| + destination[j * channels + i] = static_cast<Format>(sample) + bias; |
| + } |
| + } |
| +} |
| + |
| +// TODO(dalecurtis): See if intrinsic optimizations help any here. |
| +void AudioBus::ToInterleaved(void* dest, int frames, int bytes_per_sample) { |
| + DCHECK_LE(frames, frames_); |
| + switch (bytes_per_sample) { |
| + case 1: |
| + ToInterleavedInternal<uint8, int32>(this, dest, frames); |
| + break; |
| + case 2: |
| + ToInterleavedInternal<int16, int32>(this, dest, frames); |
| + break; |
| + case 4: |
| + ToInterleavedInternal<int32, int64>(this, dest, frames); |
| + break; |
| + default: |
| + CHECK(false) << "Unsupported bytes per sample encountered."; |
| + break; |
| + } |
| +} |
| + |
| } // namespace media |