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. | |
| 16 static const int kChannelAlignment = 16; | |
| 17 | |
| 18 static bool IsAligned(void* ptr) { | 15 static bool IsAligned(void* ptr) { |
| 19 return (reinterpret_cast<uintptr_t>(ptr) & (kChannelAlignment - 1)) == 0U; | 16 return (reinterpret_cast<uintptr_t>(ptr) & |
| 17 (AudioBus::kChannelAlignment - 1)) == 0U; | |
|
scherkus (not reviewing)
2012/08/17 23:46:07
left paren ( is still open, so this code should li
DaleCurtis
2012/08/20 22:26:21
Done.
| |
| 20 } | 18 } |
| 21 | 19 |
| 22 AudioBus::AudioBus(int channels, int frames) | 20 // Calculates the required size for an AudioBus with the given params, sets |
| 23 : frames_(frames) { | 21 // |aligned_frames| to the actual frame length of each channel array. |
| 22 static int CalculateMemorySizeInternal(int channels, int frames, | |
| 23 int* aligned_frames) { | |
| 24 CHECK(aligned_frames); | |
| 25 // Choose a size such that each channel is aligned by kChannelAlignment. | |
|
scherkus (not reviewing)
2012/08/17 23:46:07
Do we still need the kChannelAlignment stuff if th
DaleCurtis
2012/08/20 22:26:21
Yes because we don't require the user to provide a
| |
| 26 *aligned_frames = (frames + AudioBus::kChannelAlignment - 1) & | |
| 27 ~(AudioBus::kChannelAlignment - 1); | |
| 28 DCHECK_EQ(*aligned_frames & (AudioBus::kChannelAlignment - 1), 0); | |
| 29 return sizeof(float) * channels * (*aligned_frames);; | |
| 30 } | |
| 31 | |
| 32 // |Format| is the destination type, |Fixed| is a type larger than |Format| | |
| 33 // such that operations can be made without overflowing. | |
| 34 template<class Format, class Fixed> | |
| 35 static void FromInterleavedInternal(const void* src, int frames, | |
| 36 AudioBus* dest) { | |
| 37 const Format* source = static_cast<const Format*>(src); | |
| 38 | |
| 39 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : | |
| 40 std::numeric_limits<Format>::max() / 2 + 1; | |
| 41 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 : | |
| 42 std::numeric_limits<Format>::max()); | |
| 43 static const float kMinScale = 1.0f / (kBias ? kBias : | |
| 44 -static_cast<Fixed>(std::numeric_limits<Format>::min())); | |
| 45 | |
| 46 int channels = dest->channels(); | |
| 47 for (int ch = 0; ch < channels; ++ch) { | |
| 48 float* channel_data = dest->channel(ch); | |
| 49 for (int i = 0, offset = ch; i < frames; ++i, offset += channels) { | |
| 50 Fixed v = static_cast<Fixed>(source[offset]) - kBias; | |
| 51 channel_data[i] = v * (v < 0 ? kMinScale : kMaxScale); | |
| 52 } | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 // |Format| is the destination type, |Fixed| is a type larger than |Format| | |
| 57 // such that operations can be made without overflowing. | |
| 58 template<class Format, class Fixed> | |
| 59 static void ToInterleavedInternal(const AudioBus* source, int frames, | |
| 60 void* dst) { | |
| 61 Format* dest = static_cast<Format*>(dst); | |
| 62 | |
| 63 static const Format kBias = std::numeric_limits<Format>::is_signed ? 0 : | |
| 64 std::numeric_limits<Format>::max() / 2 + 1; | |
| 65 static const Fixed kMaxValue = kBias ? kBias - 1 : | |
| 66 std::numeric_limits<Format>::max(); | |
| 67 static const Fixed kMinValue = kBias ? -kBias : | |
| 68 std::numeric_limits<Format>::min(); | |
| 69 | |
| 70 int channels = source->channels(); | |
| 71 for (int ch = 0; ch < channels; ++ch) { | |
| 72 const float* channel_data = source->channel(ch); | |
| 73 for (int i = 0, offset = ch; i < frames; ++i, offset += channels) { | |
| 74 float v = channel_data[i]; | |
| 75 Fixed sample = v * (v < 0 ? -kMinValue : kMaxValue); | |
| 76 | |
| 77 if (sample > kMaxValue) | |
| 78 sample = kMaxValue; | |
| 79 else if (sample < kMinValue) | |
| 80 sample = kMinValue; | |
| 81 | |
| 82 dest[offset] = static_cast<Format>(sample) + kBias; | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 static void ValidateConfig(int channels, int frames) { | |
| 24 CHECK_GT(frames, 0); | 88 CHECK_GT(frames, 0); |
| 25 CHECK_LE(frames, limits::kMaxSamplesPerPacket); | 89 CHECK_LE(frames, limits::kMaxSamplesPerPacket); |
| 26 CHECK_GT(channels, 0); | 90 CHECK_GT(channels, 0); |
| 27 CHECK_LE(channels, limits::kMaxChannels); | 91 CHECK_LE(channels, limits::kMaxChannels); |
| 28 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, | 92 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, |
| 29 std::numeric_limits<int>::max()); | 93 std::numeric_limits<int>::max()); |
| 94 } | |
| 30 | 95 |
| 31 // Choose a size such that each channel is aligned by kChannelAlignment. | 96 AudioBus::AudioBus(int channels, int frames) |
| 32 int aligned_frames = | 97 : frames_(frames) { |
| 33 (frames_ + kChannelAlignment - 1) & ~(kChannelAlignment - 1); | 98 ValidateConfig(channels, frames_); |
| 34 data_size_ = sizeof(float) * channels * aligned_frames; | 99 |
| 100 int aligned_frames = 0; | |
| 101 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames); | |
| 35 | 102 |
| 36 data_.reset(static_cast<float*>(base::AlignedAlloc( | 103 data_.reset(static_cast<float*>(base::AlignedAlloc( |
| 37 data_size_, kChannelAlignment))); | 104 size, AudioBus::kChannelAlignment))); |
| 38 | 105 |
| 39 // Separate audio data out into channels for easy lookup later. | 106 BuildChannelData(channels, aligned_frames, data_.get()); |
| 40 channel_data_.reserve(channels); | 107 } |
| 41 for (int i = 0; i < channels; ++i) | 108 |
| 42 channel_data_.push_back(data_.get() + i * aligned_frames); | 109 AudioBus::AudioBus(int channels, int frames, float* data) |
| 110 : frames_(frames) { | |
| 111 ValidateConfig(channels, frames_); | |
| 112 | |
| 113 int aligned_frames = 0; | |
| 114 CalculateMemorySizeInternal(channels, frames, &aligned_frames); | |
| 115 | |
| 116 BuildChannelData(channels, aligned_frames, data); | |
| 43 } | 117 } |
| 44 | 118 |
| 45 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) | 119 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) |
| 46 : data_size_(0), | 120 : channel_data_(channel_data), |
| 47 channel_data_(channel_data), | |
| 48 frames_(frames) { | 121 frames_(frames) { |
| 122 ValidateConfig(channel_data_.size(), frames_); | |
| 123 | |
| 49 // Sanity check wrapped vector for alignment and channel count. | 124 // Sanity check wrapped vector for alignment and channel count. |
| 50 for (size_t i = 0; i < channel_data_.size(); ++i) | 125 for (size_t i = 0; i < channel_data_.size(); ++i) |
| 51 DCHECK(IsAligned(channel_data_[i])); | 126 DCHECK(IsAligned(channel_data_[i])); |
| 52 } | 127 } |
| 53 | 128 |
| 54 AudioBus::~AudioBus() {} | 129 AudioBus::~AudioBus() {} |
| 55 | 130 |
| 56 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { | 131 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { |
| 57 return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); | 132 return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); |
| 58 } | 133 } |
| 59 | 134 |
| 60 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { | 135 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { |
| 61 return scoped_ptr<AudioBus>(new AudioBus( | 136 return scoped_ptr<AudioBus>(new AudioBus( |
| 62 params.channels(), params.frames_per_buffer())); | 137 params.channels(), params.frames_per_buffer())); |
| 63 } | 138 } |
| 64 | 139 |
| 65 scoped_ptr<AudioBus> AudioBus::WrapVector( | 140 scoped_ptr<AudioBus> AudioBus::WrapVector( |
| 66 int frames, const std::vector<float*>& channel_data) { | 141 int frames, const std::vector<float*>& channel_data) { |
| 67 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); | 142 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); |
| 68 } | 143 } |
| 69 | 144 |
| 70 void* AudioBus::data() { | 145 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params, |
| 71 DCHECK(data_.get()); | 146 void* data) { |
| 72 return data_.get(); | 147 // |data| must be aligned by AudioBus::kChannelAlignment. |
| 73 } | 148 CHECK(IsAligned(data)); |
| 74 | 149 return scoped_ptr<AudioBus>(new AudioBus( |
| 75 int AudioBus::data_size() const { | 150 params.channels(), params.frames_per_buffer(), |
| 76 DCHECK(data_.get()); | 151 static_cast<float*>(data))); |
| 77 return data_size_; | |
| 78 } | 152 } |
| 79 | 153 |
| 80 void AudioBus::ZeroFrames(int frames) { | 154 void AudioBus::ZeroFrames(int frames) { |
| 81 DCHECK_LE(frames, frames_); | 155 DCHECK_LE(frames, frames_); |
| 82 for (size_t i = 0; i < channel_data_.size(); ++i) | 156 for (size_t i = 0; i < channel_data_.size(); ++i) |
| 83 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i])); | 157 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i])); |
| 84 } | 158 } |
| 85 | 159 |
| 86 void AudioBus::Zero() { | 160 void AudioBus::Zero() { |
| 87 ZeroFrames(frames_); | 161 ZeroFrames(frames_); |
| 88 } | 162 } |
| 89 | 163 |
| 164 int AudioBus::CalculateMemorySize(const AudioParameters& params) { | |
| 165 int aligned_frames = 0; | |
| 166 return CalculateMemorySizeInternal( | |
| 167 params.channels(), params.frames_per_buffer(), &aligned_frames); | |
| 168 } | |
| 169 | |
| 170 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { | |
| 171 DCHECK(IsAligned(data)); | |
| 172 DCHECK_EQ(channel_data_.size(), 0U); | |
| 173 // Separate audio data out into channels for easy lookup later. Figure out | |
| 174 channel_data_.reserve(channels); | |
| 175 for (int i = 0; i < channels; ++i) | |
| 176 channel_data_.push_back(data + i * aligned_frames); | |
| 177 } | |
| 178 | |
| 179 // TODO(dalecurtis): See if intrinsic optimizations help any here. | |
| 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(); | |
| 196 return; | |
| 197 } | |
| 198 | |
| 199 // Zero any remaining frames. | |
| 200 int remaining_frames = (frames_ - frames); | |
| 201 if (remaining_frames) { | |
| 202 for (int ch = 0; ch < channels(); ++ch) | |
| 203 memset(channel(ch) + frames, 0, sizeof(*channel(ch)) * remaining_frames); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // TODO(dalecurtis): See if intrinsic optimizations help any here. | |
| 208 void AudioBus::ToInterleaved(int frames, int bytes_per_sample, | |
| 209 void* dest) const { | |
| 210 DCHECK_LE(frames, frames_); | |
| 211 switch (bytes_per_sample) { | |
| 212 case 1: | |
| 213 ToInterleavedInternal<uint8, int16>(this, frames, dest); | |
| 214 break; | |
| 215 case 2: | |
| 216 ToInterleavedInternal<int16, int32>(this, frames, dest); | |
| 217 break; | |
| 218 case 4: | |
| 219 ToInterleavedInternal<int32, int64>(this, frames, dest); | |
| 220 break; | |
| 221 default: | |
| 222 NOTREACHED() << "Unsupported bytes per sample encountered."; | |
| 223 memset(dest, 0, frames * bytes_per_sample); | |
| 224 return; | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 void AudioBus::CopyTo(AudioBus* dest) const { | |
| 229 DCHECK_EQ(channels(), dest->channels()); | |
| 230 DCHECK_EQ(frames(), dest->frames()); | |
| 231 | |
| 232 // Since we don't know if the other AudioBus is wrapped or not (and we don't | |
| 233 // want to care), just copy using the public channel() accessors. | |
| 234 for (int i = 0; i < channels(); ++i) | |
| 235 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); | |
| 236 } | |
| 237 | |
| 90 } // namespace media | 238 } // namespace media |
| OLD | NEW |