| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "media/audio/audio_parameters.h" | 14 #include "media/audio/audio_parameters.h" |
| 15 #include "media/base/audio_sample_conversion.h" |
| 15 #include "media/base/limits.h" | 16 #include "media/base/limits.h" |
| 16 #include "media/base/vector_math.h" | 17 #include "media/base/vector_math.h" |
| 17 | 18 |
| 18 namespace media { | 19 namespace media { |
| 19 | 20 |
| 20 static const uint8_t kUint8Bias = 128; | |
| 21 | |
| 22 static bool IsAligned(void* ptr) { | 21 static bool IsAligned(void* ptr) { |
| 23 return (reinterpret_cast<uintptr_t>(ptr) & | 22 return (reinterpret_cast<uintptr_t>(ptr) & |
| 24 (AudioBus::kChannelAlignment - 1)) == 0U; | 23 (AudioBus::kChannelAlignment - 1)) == 0U; |
| 25 } | 24 } |
| 26 | 25 |
| 27 // Calculates the required size for an AudioBus with the given params, sets | 26 // Calculates the required size for an AudioBus with the given params, sets |
| 28 // |aligned_frames| to the actual frame length of each channel array. | 27 // |aligned_frames| to the actual frame length of each channel array. |
| 29 static int CalculateMemorySizeInternal(int channels, int frames, | 28 static int CalculateMemorySizeInternal(int channels, int frames, |
| 30 int* out_aligned_frames) { | 29 int* out_aligned_frames) { |
| 31 // Choose a size such that each channel will be aligned by | 30 // Choose a size such that each channel will be aligned by |
| 32 // kChannelAlignment when stored in a contiguous block. | 31 // kChannelAlignment when stored in a contiguous block. |
| 33 int aligned_frames = | 32 int aligned_frames = |
| 34 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & | 33 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & |
| 35 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); | 34 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); |
| 36 | 35 |
| 37 if (out_aligned_frames) | 36 if (out_aligned_frames) |
| 38 *out_aligned_frames = aligned_frames; | 37 *out_aligned_frames = aligned_frames; |
| 39 | 38 |
| 40 return sizeof(float) * channels * aligned_frames; | 39 return sizeof(float) * channels * aligned_frames; |
| 41 } | 40 } |
| 42 | 41 |
| 43 // |Format| is the destination type. If a bias is present, |Fixed| must be a | 42 template <class Format> |
| 44 // type larger than |Format| such that operations can be made without | 43 static void FromInterleavedInternal(const void* src, |
| 45 // overflowing. Without a bias |Fixed| must be the same as |Format|. | 44 int start_frame, |
| 46 template<class Format, class Fixed, Format Bias> | 45 int frames, |
| 47 static void FromInterleavedInternal(const void* src, int start_frame, | 46 AudioBus* dest) { |
| 48 int frames, AudioBus* dest, | |
| 49 float min, float max) { | |
| 50 static_assert((Bias == 0 && sizeof(Fixed) == sizeof(Format)) || | |
| 51 sizeof(Fixed) > sizeof(Format), "invalid deinterleave types"); | |
| 52 const Format* source = static_cast<const Format*>(src); | 47 const Format* source = static_cast<const Format*>(src); |
| 53 const int channels = dest->channels(); | 48 const int channels = dest->channels(); |
| 54 for (int ch = 0; ch < channels; ++ch) { | 49 for (int ch = 0; ch < channels; ++ch) { |
| 55 float* channel_data = dest->channel(ch); | 50 DeinterleaveOneChannel(source, start_frame, frames, dest->channel(ch), |
| 56 for (int i = start_frame, offset = ch; i < start_frame + frames; | 51 channels, ch); |
| 57 ++i, offset += channels) { | |
| 58 const Fixed v = static_cast<Fixed>(source[offset]) - Bias; | |
| 59 channel_data[i] = v * (v < 0 ? -min : max); | |
| 60 } | |
| 61 } | 52 } |
| 62 } | 53 } |
| 63 | 54 |
| 64 // |Format| is the destination type. If a bias is present, |Fixed| must be a | 55 template <class Format> |
| 65 // type larger than |Format| such that operations can be made without | 56 static void ToInterleavedInternal(const AudioBus* source, |
| 66 // overflowing. Without a bias |Fixed| must be the same as |Format|. | 57 int start_frame, |
| 67 template<class Format, class Fixed, Format Bias> | 58 int frames, |
| 68 static void ToInterleavedInternal(const AudioBus* source, int start_frame, | 59 void* dst) { |
| 69 int frames, void* dst, Fixed min, Fixed max) { | |
| 70 static_assert((Bias == 0 && sizeof(Fixed) == sizeof(Format)) || | |
| 71 sizeof(Fixed) > sizeof(Format), "invalid interleave types"); | |
| 72 Format* dest = static_cast<Format*>(dst); | 60 Format* dest = static_cast<Format*>(dst); |
| 73 const int channels = source->channels(); | 61 const int channels = source->channels(); |
| 74 for (int ch = 0; ch < channels; ++ch) { | 62 for (int ch = 0; ch < channels; ++ch) { |
| 75 const float* channel_data = source->channel(ch); | 63 InterleaveOneChannel(source->channel(ch), start_frame, frames, dest, |
| 76 for (int i = start_frame, offset = ch; i < start_frame + frames; | 64 channels, ch); |
| 77 ++i, offset += channels) { | |
| 78 const float v = channel_data[i]; | |
| 79 | |
| 80 Fixed sample; | |
| 81 if (v < 0) | |
| 82 sample = v <= -1 ? min : static_cast<Fixed>(-v * min); | |
| 83 else | |
| 84 sample = v >= 1 ? max : static_cast<Fixed>(v * max); | |
| 85 | |
| 86 dest[offset] = static_cast<Format>(sample) + Bias; | |
| 87 } | |
| 88 } | 65 } |
| 89 } | 66 } |
| 90 | 67 |
| 91 static void ValidateConfig(int channels, int frames) { | 68 static void ValidateConfig(int channels, int frames) { |
| 92 CHECK_GT(frames, 0); | 69 CHECK_GT(frames, 0); |
| 93 CHECK_GT(channels, 0); | 70 CHECK_GT(channels, 0); |
| 94 CHECK_LE(channels, static_cast<int>(limits::kMaxChannels)); | 71 CHECK_LE(channels, static_cast<int>(limits::kMaxChannels)); |
| 95 } | 72 } |
| 96 | 73 |
| 97 static void CheckOverflow(int start_frame, int frames, int total_frames) { | 74 static void CheckOverflow(int start_frame, int frames, int total_frames) { |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 for (int i = 0; i < channels; ++i) | 227 for (int i = 0; i < channels; ++i) |
| 251 channel_data_.push_back(data + i * aligned_frames); | 228 channel_data_.push_back(data + i * aligned_frames); |
| 252 } | 229 } |
| 253 | 230 |
| 254 // TODO(dalecurtis): See if intrinsic optimizations help any here. | 231 // TODO(dalecurtis): See if intrinsic optimizations help any here. |
| 255 void AudioBus::FromInterleavedPartial(const void* source, int start_frame, | 232 void AudioBus::FromInterleavedPartial(const void* source, int start_frame, |
| 256 int frames, int bytes_per_sample) { | 233 int frames, int bytes_per_sample) { |
| 257 CheckOverflow(start_frame, frames, frames_); | 234 CheckOverflow(start_frame, frames, frames_); |
| 258 switch (bytes_per_sample) { | 235 switch (bytes_per_sample) { |
| 259 case 1: | 236 case 1: |
| 260 FromInterleavedInternal<uint8_t, int16_t, kUint8Bias>( | 237 FromInterleavedInternal<uint8_t>(source, start_frame, frames, this); |
| 261 source, start_frame, frames, this, | |
| 262 1.0f / std::numeric_limits<int8_t>::min(), | |
| 263 1.0f / std::numeric_limits<int8_t>::max()); | |
| 264 break; | 238 break; |
| 265 case 2: | 239 case 2: |
| 266 FromInterleavedInternal<int16_t, int16_t, 0>( | 240 FromInterleavedInternal<int16_t>(source, start_frame, frames, this); |
| 267 source, start_frame, frames, this, | |
| 268 1.0f / std::numeric_limits<int16_t>::min(), | |
| 269 1.0f / std::numeric_limits<int16_t>::max()); | |
| 270 break; | 241 break; |
| 271 case 4: | 242 case 4: |
| 272 FromInterleavedInternal<int32_t, int32_t, 0>( | 243 FromInterleavedInternal<int32_t>(source, start_frame, frames, this); |
| 273 source, start_frame, frames, this, | |
| 274 1.0f / std::numeric_limits<int32_t>::min(), | |
| 275 1.0f / std::numeric_limits<int32_t>::max()); | |
| 276 break; | 244 break; |
| 277 default: | 245 default: |
| 278 NOTREACHED() << "Unsupported bytes per sample encountered."; | 246 NOTREACHED() << "Unsupported bytes per sample encountered."; |
| 279 ZeroFramesPartial(start_frame, frames); | 247 ZeroFramesPartial(start_frame, frames); |
| 280 return; | 248 return; |
| 281 } | 249 } |
| 282 | 250 |
| 283 // Don't clear remaining frames if this is a partial deinterleave. | 251 // Don't clear remaining frames if this is a partial deinterleave. |
| 284 if (!start_frame) { | 252 if (!start_frame) { |
| 285 // Zero any remaining frames. | 253 // Zero any remaining frames. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 296 void* dest) const { | 264 void* dest) const { |
| 297 ToInterleavedPartial(0, frames, bytes_per_sample, dest); | 265 ToInterleavedPartial(0, frames, bytes_per_sample, dest); |
| 298 } | 266 } |
| 299 | 267 |
| 300 // TODO(dalecurtis): See if intrinsic optimizations help any here. | 268 // TODO(dalecurtis): See if intrinsic optimizations help any here. |
| 301 void AudioBus::ToInterleavedPartial(int start_frame, int frames, | 269 void AudioBus::ToInterleavedPartial(int start_frame, int frames, |
| 302 int bytes_per_sample, void* dest) const { | 270 int bytes_per_sample, void* dest) const { |
| 303 CheckOverflow(start_frame, frames, frames_); | 271 CheckOverflow(start_frame, frames, frames_); |
| 304 switch (bytes_per_sample) { | 272 switch (bytes_per_sample) { |
| 305 case 1: | 273 case 1: |
| 306 ToInterleavedInternal<uint8_t, int16_t, kUint8Bias>( | 274 ToInterleavedInternal<uint8_t>(this, start_frame, frames, dest); |
| 307 this, start_frame, frames, dest, std::numeric_limits<int8_t>::min(), | |
| 308 std::numeric_limits<int8_t>::max()); | |
| 309 break; | 275 break; |
| 310 case 2: | 276 case 2: |
| 311 ToInterleavedInternal<int16_t, int16_t, 0>( | 277 ToInterleavedInternal<int16_t>(this, start_frame, frames, dest); |
| 312 this, start_frame, frames, dest, std::numeric_limits<int16_t>::min(), | |
| 313 std::numeric_limits<int16_t>::max()); | |
| 314 break; | 278 break; |
| 315 case 4: | 279 case 4: |
| 316 ToInterleavedInternal<int32_t, int32_t, 0>( | 280 ToInterleavedInternal<int32_t>(this, start_frame, frames, dest); |
| 317 this, start_frame, frames, dest, std::numeric_limits<int32_t>::min(), | |
| 318 std::numeric_limits<int32_t>::max()); | |
| 319 break; | 281 break; |
| 320 default: | 282 default: |
| 321 NOTREACHED() << "Unsupported bytes per sample encountered."; | 283 NOTREACHED() << "Unsupported bytes per sample encountered."; |
| 322 memset(dest, 0, frames * bytes_per_sample); | 284 memset(dest, 0, frames * bytes_per_sample); |
| 323 return; | 285 return; |
| 324 } | 286 } |
| 325 } | 287 } |
| 326 | 288 |
| 327 void AudioBus::CopyTo(AudioBus* dest) const { | 289 void AudioBus::CopyTo(AudioBus* dest) const { |
| 328 CopyPartialFramesTo(0, frames(), 0, dest); | 290 CopyPartialFramesTo(0, frames(), 0, dest); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 return scoped_refptr<AudioBusRefCounted>( | 328 return scoped_refptr<AudioBusRefCounted>( |
| 367 new AudioBusRefCounted(channels, frames)); | 329 new AudioBusRefCounted(channels, frames)); |
| 368 } | 330 } |
| 369 | 331 |
| 370 AudioBusRefCounted::AudioBusRefCounted(int channels, int frames) | 332 AudioBusRefCounted::AudioBusRefCounted(int channels, int frames) |
| 371 : AudioBus(channels, frames) {} | 333 : AudioBus(channels, frames) {} |
| 372 | 334 |
| 373 AudioBusRefCounted::~AudioBusRefCounted() {} | 335 AudioBusRefCounted::~AudioBusRefCounted() {} |
| 374 | 336 |
| 375 } // namespace media | 337 } // namespace media |
| OLD | NEW |