Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/base/audio_bus.h" | 5 #include "media/base/audio_bus.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/audio/audio_parameters.h" | 10 #include "media/audio/audio_parameters.h" |
| 11 #include "media/base/limits.h" | 11 #include "media/base/limits.h" |
| 12 | 12 |
| 13 namespace media { | 13 namespace media { |
| 14 | 14 |
| 15 // Ensure each channel is 16-byte aligned for easy SSE optimizations. | 15 // Ensure each channel is 16-byte aligned for easy SSE optimizations. |
| 16 static const int kChannelAlignment = 16; | 16 static const int kChannelAlignment = 16; |
| 17 | 17 |
| 18 static bool IsAligned(void* ptr) { | 18 static bool IsAligned(void* ptr) { |
| 19 return (reinterpret_cast<uintptr_t>(ptr) & (kChannelAlignment - 1)) == 0U; | 19 return (reinterpret_cast<uintptr_t>(ptr) & (kChannelAlignment - 1)) == 0U; |
| 20 } | 20 } |
| 21 | 21 |
| 22 // Calculates the data_size() for an AudioBus with the given params, sets | |
| 23 // |aligned_frames| to the actual frame length of each channel array. | |
| 24 static int BlockSizeInternal(int channels, int frames, | |
| 25 int* aligned_frames) { | |
|
scherkus (not reviewing)
2012/08/16 18:58:19
alignment
DaleCurtis
2012/08/17 02:15:58
Done.
| |
| 26 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.
| |
| 27 // Choose a size such that each channel is aligned by kChannelAlignment. | |
| 28 *aligned_frames = | |
| 29 (frames + kChannelAlignment - 1) & ~(kChannelAlignment - 1); | |
| 30 // Include an extra kChannelAlignment bytes in case WrapBlock() needs to | |
| 31 // manually align the start of the first channel. | |
| 32 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.
| |
| 33 } | |
| 34 | |
| 22 AudioBus::AudioBus(int channels, int frames) | 35 AudioBus::AudioBus(int channels, int frames) |
| 23 : frames_(frames) { | 36 : frames_(frames) { |
| 24 CHECK_GT(frames, 0); | 37 ValidateConfig(channels, frames_); |
| 25 CHECK_LE(frames, limits::kMaxSamplesPerPacket); | |
| 26 CHECK_GT(channels, 0); | |
| 27 CHECK_LE(channels, limits::kMaxChannels); | |
| 28 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, | |
| 29 std::numeric_limits<int>::max()); | |
| 30 | 38 |
| 31 // Choose a size such that each channel is aligned by kChannelAlignment. | 39 int aligned_frames = 0; |
| 32 int aligned_frames = | 40 data_size_ = BlockSizeInternal(channels, frames, &aligned_frames); |
| 33 (frames_ + kChannelAlignment - 1) & ~(kChannelAlignment - 1); | |
| 34 data_size_ = sizeof(float) * channels * aligned_frames; | |
| 35 | 41 |
| 36 data_.reset(static_cast<float*>(base::AlignedAlloc( | 42 data_.reset(static_cast<float*>(base::AlignedAlloc( |
| 37 data_size_, kChannelAlignment))); | 43 data_size_, kChannelAlignment))); |
| 38 | 44 |
| 39 // Separate audio data out into channels for easy lookup later. | 45 BuildChannelData(channels, aligned_frames, data_.get()); |
| 40 channel_data_.reserve(channels); | 46 } |
| 41 for (int i = 0; i < channels; ++i) | 47 |
| 42 channel_data_.push_back(data_.get() + i * aligned_frames); | 48 AudioBus::AudioBus(int channels, int frames, float* data) |
| 49 : data_size_(0), | |
| 50 frames_(frames) { | |
| 51 ValidateConfig(channels, frames_); | |
| 52 | |
| 53 // Manually align the input pointer if it's not already aligned. We pad the | |
| 54 // amount returned by BlockSize() just for this case. | |
| 55 uintptr_t aligned_ptr = reinterpret_cast<uintptr_t>(data); | |
| 56 if (!IsAligned(data)) | |
| 57 aligned_ptr += kChannelAlignment - (aligned_ptr & (kChannelAlignment - 1)); | |
| 58 | |
| 59 // Don't set |data_size_|, since we do not own the block of memory provided | |
| 60 // for channel data. | |
| 61 int aligned_frames = 0; | |
| 62 BlockSizeInternal(channels, frames, &aligned_frames); | |
| 63 | |
| 64 BuildChannelData( | |
| 65 channels, aligned_frames, reinterpret_cast<float*>(aligned_ptr)); | |
| 43 } | 66 } |
| 44 | 67 |
| 45 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) | 68 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) |
| 46 : data_size_(0), | 69 : data_size_(0), |
| 47 channel_data_(channel_data), | 70 channel_data_(channel_data), |
| 48 frames_(frames) { | 71 frames_(frames) { |
| 72 ValidateConfig(channel_data_.size(), frames_); | |
| 73 | |
| 49 // Sanity check wrapped vector for alignment and channel count. | 74 // Sanity check wrapped vector for alignment and channel count. |
| 50 for (size_t i = 0; i < channel_data_.size(); ++i) | 75 for (size_t i = 0; i < channel_data_.size(); ++i) |
| 51 DCHECK(IsAligned(channel_data_[i])); | 76 DCHECK(IsAligned(channel_data_[i])); |
| 52 } | 77 } |
| 53 | 78 |
| 54 AudioBus::~AudioBus() {} | 79 AudioBus::~AudioBus() {} |
| 55 | 80 |
| 56 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { | 81 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { |
| 57 return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); | 82 return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); |
| 58 } | 83 } |
| 59 | 84 |
| 60 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { | 85 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { |
| 61 return scoped_ptr<AudioBus>(new AudioBus( | 86 return scoped_ptr<AudioBus>(new AudioBus( |
| 62 params.channels(), params.frames_per_buffer())); | 87 params.channels(), params.frames_per_buffer())); |
| 63 } | 88 } |
| 64 | 89 |
| 65 scoped_ptr<AudioBus> AudioBus::WrapVector( | 90 scoped_ptr<AudioBus> AudioBus::WrapVector( |
| 66 int frames, const std::vector<float*>& channel_data) { | 91 int frames, const std::vector<float*>& channel_data) { |
| 67 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); | 92 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); |
| 68 } | 93 } |
| 69 | 94 |
| 95 scoped_ptr<AudioBus> AudioBus::WrapBlock(int channels, int frames, void* data) { | |
| 96 return scoped_ptr<AudioBus>(new AudioBus( | |
| 97 channels, frames, static_cast<float*>(data))); | |
| 98 } | |
| 99 | |
| 100 scoped_ptr<AudioBus> AudioBus::WrapBlock(const AudioParameters& params, | |
| 101 void* data) { | |
| 102 return scoped_ptr<AudioBus>(new AudioBus( | |
| 103 params.channels(), params.frames_per_buffer(), | |
| 104 static_cast<float*>(data))); | |
| 105 } | |
| 106 | |
| 70 void* AudioBus::data() { | 107 void* AudioBus::data() { |
| 71 DCHECK(data_.get()); | 108 DCHECK(data_.get()); |
| 72 return data_.get(); | 109 return data_.get(); |
| 73 } | 110 } |
| 74 | 111 |
| 75 int AudioBus::data_size() const { | 112 int AudioBus::data_size() const { |
| 76 DCHECK(data_.get()); | 113 DCHECK(data_.get()); |
| 77 return data_size_; | 114 return data_size_; |
| 78 } | 115 } |
| 79 | 116 |
| 80 void AudioBus::ZeroFrames(int frames) { | 117 void AudioBus::ZeroFrames(int frames) { |
| 81 DCHECK_LE(frames, frames_); | 118 DCHECK_LE(frames, frames_); |
| 82 for (size_t i = 0; i < channel_data_.size(); ++i) | 119 for (size_t i = 0; i < channel_data_.size(); ++i) |
| 83 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i])); | 120 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i])); |
| 84 } | 121 } |
| 85 | 122 |
| 86 void AudioBus::Zero() { | 123 void AudioBus::Zero() { |
| 87 ZeroFrames(frames_); | 124 ZeroFrames(frames_); |
| 88 } | 125 } |
| 89 | 126 |
| 127 int AudioBus::BlockSize(int channels, int frames) { | |
| 128 int aligned_frames = 0; | |
| 129 return BlockSizeInternal(channels, frames, &aligned_frames); | |
| 130 } | |
| 131 | |
| 132 int AudioBus::BlockSize(const AudioParameters& params) { | |
| 133 int aligned_frames = 0; | |
| 134 return BlockSizeInternal( | |
| 135 params.channels(), params.frames_per_buffer(), &aligned_frames); | |
| 136 } | |
| 137 | |
| 138 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { | |
| 139 DCHECK(IsAligned(data)); | |
| 140 DCHECK_EQ(channel_data_.size(), 0U); | |
| 141 // Separate audio data out into channels for easy lookup later. Figure out | |
| 142 channel_data_.reserve(channels); | |
| 143 for (int i = 0; i < channels; ++i) | |
| 144 channel_data_.push_back(data + i * aligned_frames); | |
| 145 } | |
| 146 | |
| 147 void AudioBus::ValidateConfig(int channels, int frames) { | |
| 148 CHECK_GT(frames, 0); | |
| 149 CHECK_LE(frames, limits::kMaxSamplesPerPacket); | |
| 150 CHECK_GT(channels, 0); | |
| 151 CHECK_LE(channels, limits::kMaxChannels); | |
| 152 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, | |
| 153 std::numeric_limits<int>::max()); | |
| 154 } | |
| 155 | |
| 156 // |Format| is the destination type, |Fixed| is a type larger than |Format| | |
| 157 // such that operations can be made without overflowing. | |
| 158 template<class Format, class Fixed> | |
| 159 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.
| |
| 160 const Format* source = static_cast<const Format*>(src); | |
| 161 | |
| 162 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : | |
| 163 std::numeric_limits<Format>::max() / 2 + 1; | |
| 164 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 : | |
| 165 std::numeric_limits<Format>::max()); | |
| 166 static const float kMinScale = 1.0f / (kBias ? kBias : | |
| 167 -static_cast<Fixed>(std::numeric_limits<Format>::min())); | |
| 168 | |
| 169 int channels = dest->channels(); | |
| 170 for (int ch = 0; ch < channels; ++ch) { | |
| 171 float* channel_data = dest->channel(ch); | |
| 172 for (int i = 0, offset = ch; i < frames; ++i, offset += channels) { | |
| 173 Fixed v = static_cast<Fixed>(source[offset]) - kBias; | |
| 174 channel_data[i] = v * (v < 0 ? kMinScale : kMaxScale); | |
| 175 } | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 // 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
| |
| 180 void AudioBus::FromInterleaved(const void* source, int frames, | |
| 181 int bytes_per_sample) { | |
| 182 DCHECK_LE(frames, frames_); | |
| 183 switch (bytes_per_sample) { | |
| 184 case 1: | |
| 185 FromInterleavedInternal<uint8, int16>(source, frames, this); | |
| 186 break; | |
| 187 case 2: | |
| 188 FromInterleavedInternal<int16, int32>(source, frames, this); | |
| 189 break; | |
| 190 case 4: | |
| 191 FromInterleavedInternal<int32, int64>(source, frames, this); | |
| 192 break; | |
| 193 default: | |
| 194 NOTREACHED() << "Unsupported bytes per sample encountered."; | |
| 195 Zero(); | |
|
scherkus (not reviewing)
2012/08/16 18:58:19
add return after Zeroing?
DaleCurtis
2012/08/17 02:15:58
Done.
| |
| 196 } | |
| 197 | |
| 198 // Zero any remaining frames. | |
| 199 int remaining_frames = (frames_ - frames); | |
| 200 if (remaining_frames) { | |
| 201 for (int ch = 0; ch < channels(); ++ch) | |
| 202 memset(channel(ch) + frames, 0, sizeof(*channel(ch)) * remaining_frames); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 // |Format| is the destination type, |Fixed| is a type larger than |Format| | |
| 207 // such that operations can be made without overflowing. | |
| 208 template<class Format, class Fixed> | |
| 209 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.
| |
| 210 void* dst) { | |
| 211 Format* dest = static_cast<Format*>(dst); | |
| 212 | |
| 213 static const Format kBias = std::numeric_limits<Format>::is_signed ? 0 : | |
| 214 std::numeric_limits<Format>::max() / 2 + 1; | |
| 215 static const Fixed kMaxValue = kBias ? kBias - 1 : | |
| 216 std::numeric_limits<Format>::max(); | |
| 217 static const Fixed kMinValue = kBias ? -kBias : | |
| 218 std::numeric_limits<Format>::min(); | |
| 219 | |
| 220 int channels = source->channels(); | |
| 221 for (int ch = 0; ch < channels; ++ch) { | |
| 222 const float* channel_data = source->channel(ch); | |
| 223 for (int i = 0, offset = ch; i < frames; ++i, offset += channels) { | |
| 224 float v = channel_data[i]; | |
| 225 Fixed sample = v * (v < 0 ? -kMinValue : kMaxValue); | |
| 226 | |
| 227 if (sample > kMaxValue) | |
| 228 sample = kMaxValue; | |
| 229 else if (sample < kMinValue) | |
| 230 sample = kMinValue; | |
| 231 | |
| 232 dest[offset] = static_cast<Format>(sample) + kBias; | |
| 233 } | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 // TODO(dalecurtis): See if intrinsic optimizations help any here. | |
| 238 void AudioBus::ToInterleaved(int frames, int bytes_per_sample, void* dest) { | |
| 239 DCHECK_LE(frames, frames_); | |
| 240 switch (bytes_per_sample) { | |
| 241 case 1: | |
| 242 ToInterleavedInternal<uint8, int16>(this, frames, dest); | |
| 243 break; | |
| 244 case 2: | |
| 245 ToInterleavedInternal<int16, int32>(this, frames, dest); | |
| 246 break; | |
| 247 case 4: | |
| 248 ToInterleavedInternal<int32, int64>(this, frames, dest); | |
| 249 break; | |
| 250 default: | |
| 251 NOTREACHED() << "Unsupported bytes per sample encountered."; | |
| 252 memset(dest, 0, frames * bytes_per_sample); | |
| 253 break; | |
| 254 } | |
| 255 } | |
| 256 | |
| 90 } // namespace media | 257 } // namespace media |
| OLD | NEW |