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" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 // when stored in a contiguous block. | 26 // when stored in a contiguous block. |
| 27 *aligned_frames = | 27 *aligned_frames = |
| 28 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & | 28 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & |
| 29 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); | 29 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); |
| 30 return sizeof(float) * channels * (*aligned_frames); | 30 return sizeof(float) * channels * (*aligned_frames); |
| 31 } | 31 } |
| 32 | 32 |
| 33 // |Format| is the destination type, |Fixed| is a type larger than |Format| | 33 // |Format| is the destination type, |Fixed| is a type larger than |Format| |
| 34 // such that operations can be made without overflowing. | 34 // such that operations can be made without overflowing. |
| 35 template<class Format, class Fixed> | 35 template<class Format, class Fixed> |
| 36 static void FromInterleavedInternal(const void* src, int frames, | 36 static void FromInterleavedInternal(const void* src, int start_frame, |
| 37 AudioBus* dest) { | 37 int frames, AudioBus* dest) { |
|
Chris Rogers
2012/08/24 20:49:28
how about some DCHECKS, including negative values
DaleCurtis
2012/08/24 22:23:22
Not here, the DCHECKS are in the class methods. E
| |
| 38 const Format* source = static_cast<const Format*>(src); | 38 const Format* source = static_cast<const Format*>(src); |
| 39 | 39 |
| 40 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : | 40 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : |
| 41 std::numeric_limits<Format>::max() / 2 + 1; | 41 std::numeric_limits<Format>::max() / 2 + 1; |
| 42 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 : | 42 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 : |
| 43 std::numeric_limits<Format>::max()); | 43 std::numeric_limits<Format>::max()); |
| 44 static const float kMinScale = 1.0f / (kBias ? kBias : | 44 static const float kMinScale = 1.0f / (kBias ? kBias : |
| 45 -static_cast<Fixed>(std::numeric_limits<Format>::min())); | 45 -static_cast<Fixed>(std::numeric_limits<Format>::min())); |
| 46 | 46 |
| 47 int channels = dest->channels(); | 47 int channels = dest->channels(); |
| 48 for (int ch = 0; ch < channels; ++ch) { | 48 for (int ch = 0; ch < channels; ++ch) { |
| 49 float* channel_data = dest->channel(ch); | 49 float* channel_data = dest->channel(ch); |
| 50 for (int i = 0, offset = ch; i < frames; ++i, offset += channels) { | 50 for (int i = start_frame, offset = ch; i < start_frame + frames; |
| 51 ++i, offset += channels) { | |
| 51 Fixed v = static_cast<Fixed>(source[offset]) - kBias; | 52 Fixed v = static_cast<Fixed>(source[offset]) - kBias; |
| 52 channel_data[i] = v * (v < 0 ? kMinScale : kMaxScale); | 53 channel_data[i] = v * (v < 0 ? kMinScale : kMaxScale); |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 } | 56 } |
| 56 | 57 |
| 57 // |Format| is the destination type, |Fixed| is a type larger than |Format| | 58 // |Format| is the destination type, |Fixed| is a type larger than |Format| |
| 58 // such that operations can be made without overflowing. | 59 // such that operations can be made without overflowing. |
| 59 template<class Format, class Fixed> | 60 template<class Format, class Fixed> |
| 60 static void ToInterleavedInternal(const AudioBus* source, int frames, | 61 static void ToInterleavedInternal(const AudioBus* source, int frames, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 | 154 |
| 154 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params, | 155 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params, |
| 155 void* data) { | 156 void* data) { |
| 156 // |data| must be aligned by AudioBus::kChannelAlignment. | 157 // |data| must be aligned by AudioBus::kChannelAlignment. |
| 157 CHECK(IsAligned(data)); | 158 CHECK(IsAligned(data)); |
| 158 return scoped_ptr<AudioBus>(new AudioBus( | 159 return scoped_ptr<AudioBus>(new AudioBus( |
| 159 params.channels(), params.frames_per_buffer(), | 160 params.channels(), params.frames_per_buffer(), |
| 160 static_cast<float*>(data))); | 161 static_cast<float*>(data))); |
| 161 } | 162 } |
| 162 | 163 |
| 164 void AudioBus::ZeroFramesPartial(int start_frame, int frames) { | |
| 165 DCHECK_LE(start_frame + frames, frames_); | |
|
Chris Rogers
2012/08/24 20:49:28
What about checking for start_frame < 0 or frames
DaleCurtis
2012/08/24 22:23:22
Yeah, it's a bit weird, but is Chrome style. I've
| |
| 166 if (frames > 0) { | |
| 167 for (size_t i = 0; i < channel_data_.size(); ++i) { | |
| 168 memset(channel_data_[i] + start_frame, 0, | |
| 169 frames * sizeof(*channel_data_[i])); | |
| 170 } | |
| 171 } | |
| 172 } | |
| 173 | |
| 163 void AudioBus::ZeroFrames(int frames) { | 174 void AudioBus::ZeroFrames(int frames) { |
| 164 DCHECK_LE(frames, frames_); | 175 ZeroFramesPartial(0, frames); |
| 165 for (size_t i = 0; i < channel_data_.size(); ++i) | |
| 166 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i])); | |
| 167 } | 176 } |
| 168 | 177 |
| 169 void AudioBus::Zero() { | 178 void AudioBus::Zero() { |
| 170 ZeroFrames(frames_); | 179 ZeroFrames(frames_); |
| 171 } | 180 } |
| 172 | 181 |
| 173 int AudioBus::CalculateMemorySize(const AudioParameters& params) { | 182 int AudioBus::CalculateMemorySize(const AudioParameters& params) { |
| 174 int aligned_frames = 0; | 183 int aligned_frames = 0; |
| 175 return CalculateMemorySizeInternal( | 184 return CalculateMemorySizeInternal( |
| 176 params.channels(), params.frames_per_buffer(), &aligned_frames); | 185 params.channels(), params.frames_per_buffer(), &aligned_frames); |
| 177 } | 186 } |
| 178 | 187 |
| 179 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { | 188 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { |
| 180 DCHECK(IsAligned(data)); | 189 DCHECK(IsAligned(data)); |
| 181 DCHECK_EQ(channel_data_.size(), 0U); | 190 DCHECK_EQ(channel_data_.size(), 0U); |
| 182 // Separate audio data out into channels for easy lookup later. Figure out | 191 // Separate audio data out into channels for easy lookup later. Figure out |
| 183 channel_data_.reserve(channels); | 192 channel_data_.reserve(channels); |
| 184 for (int i = 0; i < channels; ++i) | 193 for (int i = 0; i < channels; ++i) |
| 185 channel_data_.push_back(data + i * aligned_frames); | 194 channel_data_.push_back(data + i * aligned_frames); |
| 186 } | 195 } |
| 187 | 196 |
| 188 // TODO(dalecurtis): See if intrinsic optimizations help any here. | 197 // TODO(dalecurtis): See if intrinsic optimizations help any here. |
| 189 void AudioBus::FromInterleaved(const void* source, int frames, | 198 void AudioBus::FromInterleavedPartial(const void* source, int start_frame, |
| 190 int bytes_per_sample) { | 199 int frames, int bytes_per_sample) { |
| 191 DCHECK_LE(frames, frames_); | 200 DCHECK_LE(start_frame + frames, frames_); |
| 192 switch (bytes_per_sample) { | 201 switch (bytes_per_sample) { |
| 193 case 1: | 202 case 1: |
| 194 FromInterleavedInternal<uint8, int16>(source, frames, this); | 203 FromInterleavedInternal<uint8, int16>(source, start_frame, frames, this); |
| 195 break; | 204 break; |
| 196 case 2: | 205 case 2: |
| 197 FromInterleavedInternal<int16, int32>(source, frames, this); | 206 FromInterleavedInternal<int16, int32>(source, start_frame, frames, this); |
| 198 break; | 207 break; |
| 199 case 4: | 208 case 4: |
| 200 FromInterleavedInternal<int32, int64>(source, frames, this); | 209 FromInterleavedInternal<int32, int64>(source, start_frame, frames, this); |
| 201 break; | 210 break; |
| 202 default: | 211 default: |
| 203 NOTREACHED() << "Unsupported bytes per sample encountered."; | 212 NOTREACHED() << "Unsupported bytes per sample encountered."; |
| 204 Zero(); | 213 ZeroFramesPartial(start_frame, frames); |
| 205 return; | 214 return; |
| 206 } | 215 } |
| 207 | 216 |
| 208 // Zero any remaining frames. | 217 // Don't clear remaining frames if this is a partial deinterleave. |
| 209 int remaining_frames = (frames_ - frames); | 218 if (!start_frame) { |
| 210 if (remaining_frames) { | 219 // Zero any remaining frames. |
| 211 for (int ch = 0; ch < channels(); ++ch) | 220 ZeroFramesPartial(frames, frames_ - frames); |
| 212 memset(channel(ch) + frames, 0, sizeof(*channel(ch)) * remaining_frames); | |
| 213 } | 221 } |
| 214 } | 222 } |
| 215 | 223 |
| 224 void AudioBus::FromInterleaved(const void* source, int frames, | |
| 225 int bytes_per_sample) { | |
| 226 FromInterleavedPartial(source, 0, frames, bytes_per_sample); | |
| 227 } | |
| 228 | |
| 216 // TODO(dalecurtis): See if intrinsic optimizations help any here. | 229 // TODO(dalecurtis): See if intrinsic optimizations help any here. |
| 217 void AudioBus::ToInterleaved(int frames, int bytes_per_sample, | 230 void AudioBus::ToInterleaved(int frames, int bytes_per_sample, |
| 218 void* dest) const { | 231 void* dest) const { |
| 219 DCHECK_LE(frames, frames_); | 232 DCHECK_LE(frames, frames_); |
| 220 switch (bytes_per_sample) { | 233 switch (bytes_per_sample) { |
| 221 case 1: | 234 case 1: |
| 222 ToInterleavedInternal<uint8, int16>(this, frames, dest); | 235 ToInterleavedInternal<uint8, int16>(this, frames, dest); |
| 223 break; | 236 break; |
| 224 case 2: | 237 case 2: |
| 225 ToInterleavedInternal<int16, int32>(this, frames, dest); | 238 ToInterleavedInternal<int16, int32>(this, frames, dest); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 238 DCHECK_EQ(channels(), dest->channels()); | 251 DCHECK_EQ(channels(), dest->channels()); |
| 239 DCHECK_EQ(frames(), dest->frames()); | 252 DCHECK_EQ(frames(), dest->frames()); |
| 240 | 253 |
| 241 // Since we don't know if the other AudioBus is wrapped or not (and we don't | 254 // Since we don't know if the other AudioBus is wrapped or not (and we don't |
| 242 // want to care), just copy using the public channel() accessors. | 255 // want to care), just copy using the public channel() accessors. |
| 243 for (int i = 0; i < channels(); ++i) | 256 for (int i = 0; i < channels(); ++i) |
| 244 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); | 257 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); |
| 245 } | 258 } |
| 246 | 259 |
| 247 } // namespace media | 260 } // namespace media |
| OLD | NEW |