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..bc8ba15af2c58aad64ee212cb0376ef673a7608e 100644 |
| --- a/media/base/audio_bus.cc |
| +++ b/media/base/audio_bus.cc |
| @@ -19,33 +19,58 @@ static bool IsAligned(void* ptr) { |
| return (reinterpret_cast<uintptr_t>(ptr) & (kChannelAlignment - 1)) == 0U; |
| } |
| +// Calculates the data_size() for an AudioBus with the given params, sets |
| +// |aligned_frames| to the actual frame length of each channel array. |
| +static int BlockSizeInternal(int channels, int frames, |
| + int* aligned_frames) { |
|
scherkus (not reviewing)
2012/08/16 18:58:19
alignment
DaleCurtis
2012/08/17 02:15:58
Done.
|
| + DCHECK(aligned_frames); |
|
scherkus (not reviewing)
2012/08/16 18:58:19
dcheck not helpful here as you immediately deref
DaleCurtis
2012/08/17 02:15:58
Done.
|
| + // Choose a size such that each channel is aligned by kChannelAlignment. |
| + *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; |
|
scherkus (not reviewing)
2012/08/16 18:58:19
the * * is freaking me out -- want to do (*aligned
DaleCurtis
2012/08/17 02:15:58
Done.
|
| +} |
| + |
| 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()); |
| + ValidateConfig(channels, frames_); |
| - // 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; |
| + int aligned_frames = 0; |
| + data_size_ = BlockSizeInternal(channels, frames, &aligned_frames); |
| data_.reset(static_cast<float*>(base::AlignedAlloc( |
| data_size_, kChannelAlignment))); |
| - // 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, float* data) |
| + : data_size_(0), |
| + frames_(frames) { |
| + ValidateConfig(channels, frames_); |
| + |
| + // Manually align the input pointer if it's not already aligned. We pad the |
| + // amount returned by BlockSize() 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_|, since we do not own the block of memory provided |
| + // for channel data. |
| + int aligned_frames = 0; |
| + BlockSizeInternal(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) { |
| + ValidateConfig(channel_data_.size(), frames_); |
| + |
| // Sanity check wrapped vector for alignment and channel count. |
| for (size_t i = 0; i < channel_data_.size(); ++i) |
| DCHECK(IsAligned(channel_data_[i])); |
| @@ -67,6 +92,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, 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(), |
| + static_cast<float*>(data))); |
| +} |
| + |
| void* AudioBus::data() { |
| DCHECK(data_.get()); |
| return data_.get(); |
| @@ -87,4 +124,134 @@ void AudioBus::Zero() { |
| ZeroFrames(frames_); |
| } |
| +int AudioBus::BlockSize(int channels, int frames) { |
| + int aligned_frames = 0; |
| + return BlockSizeInternal(channels, frames, &aligned_frames); |
| +} |
| + |
| +int AudioBus::BlockSize(const AudioParameters& params) { |
| + int aligned_frames = 0; |
| + return BlockSizeInternal( |
| + 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) { |
| + 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()); |
| +} |
| + |
| +// |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> |
| +void FromInterleavedInternal(const void* src, int frames, AudioBus* dest) { |
|
scherkus (not reviewing)
2012/08/16 18:58:19
static + move to top
DaleCurtis
2012/08/17 02:15:58
Done.
|
| + const Format* source = static_cast<const Format*>(src); |
| + |
| + static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : |
| + std::numeric_limits<Format>::max() / 2 + 1; |
| + static const float kMaxScale = 1.0f / (kBias ? kBias - 1 : |
| + std::numeric_limits<Format>::max()); |
| + static const float kMinScale = 1.0f / (kBias ? kBias : |
| + -static_cast<Fixed>(std::numeric_limits<Format>::min())); |
| + |
| + int channels = dest->channels(); |
| + for (int ch = 0; ch < channels; ++ch) { |
| + float* channel_data = dest->channel(ch); |
| + for (int i = 0, offset = ch; i < frames; ++i, offset += channels) { |
| + Fixed v = static_cast<Fixed>(source[offset]) - kBias; |
| + channel_data[i] = v * (v < 0 ? kMinScale : kMaxScale); |
| + } |
| + } |
| +} |
| + |
| +// TODO(dalecurtis): See if intrinsic optimizations help any here. |
|
scherkus (not reviewing)
2012/08/16 18:58:19
can we get a bug tracking where we may want to use
DaleCurtis
2012/08/17 02:15:58
I think this is the last spot, I intend to remove
|
| +void AudioBus::FromInterleaved(const void* source, int frames, |
| + int bytes_per_sample) { |
| + DCHECK_LE(frames, frames_); |
| + switch (bytes_per_sample) { |
| + case 1: |
| + FromInterleavedInternal<uint8, int16>(source, frames, this); |
| + break; |
| + case 2: |
| + FromInterleavedInternal<int16, int32>(source, frames, this); |
| + break; |
| + case 4: |
| + FromInterleavedInternal<int32, int64>(source, frames, this); |
| + break; |
| + default: |
| + NOTREACHED() << "Unsupported bytes per sample encountered."; |
| + Zero(); |
|
scherkus (not reviewing)
2012/08/16 18:58:19
add return after Zeroing?
DaleCurtis
2012/08/17 02:15:58
Done.
|
| + } |
| + |
| + // 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, int frames, |
|
scherkus (not reviewing)
2012/08/16 18:58:19
we put static fn's at the top of files
DaleCurtis
2012/08/17 02:15:58
Done.
|
| + void* dst) { |
| + Format* dest = static_cast<Format*>(dst); |
| + |
| + static const Format kBias = std::numeric_limits<Format>::is_signed ? 0 : |
| + std::numeric_limits<Format>::max() / 2 + 1; |
| + static const Fixed kMaxValue = kBias ? kBias - 1 : |
| + std::numeric_limits<Format>::max(); |
| + static const Fixed kMinValue = kBias ? -kBias : |
| + std::numeric_limits<Format>::min(); |
| + |
| + int channels = source->channels(); |
| + for (int ch = 0; ch < channels; ++ch) { |
| + const float* channel_data = source->channel(ch); |
| + for (int i = 0, offset = ch; i < frames; ++i, offset += channels) { |
| + float v = channel_data[i]; |
| + Fixed sample = v * (v < 0 ? -kMinValue : kMaxValue); |
| + |
| + if (sample > kMaxValue) |
| + sample = kMaxValue; |
| + else if (sample < kMinValue) |
| + sample = kMinValue; |
| + |
| + dest[offset] = static_cast<Format>(sample) + kBias; |
| + } |
| + } |
| +} |
| + |
| +// TODO(dalecurtis): See if intrinsic optimizations help any here. |
| +void AudioBus::ToInterleaved(int frames, int bytes_per_sample, void* dest) { |
| + DCHECK_LE(frames, frames_); |
| + switch (bytes_per_sample) { |
| + case 1: |
| + ToInterleavedInternal<uint8, int16>(this, frames, dest); |
| + break; |
| + case 2: |
| + ToInterleavedInternal<int16, int32>(this, frames, dest); |
| + break; |
| + case 4: |
| + ToInterleavedInternal<int32, int64>(this, frames, dest); |
| + break; |
| + default: |
| + NOTREACHED() << "Unsupported bytes per sample encountered."; |
| + memset(dest, 0, frames * bytes_per_sample); |
| + break; |
| + } |
| +} |
| + |
| } // namespace media |