| 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 | |
| 44 // type larger than |Format| such that operations can be made without | |
| 45 // overflowing. Without a bias |Fixed| must be the same as |Format|. | |
| 46 template<class Format, class Fixed, Format Bias> | |
| 47 static void FromInterleavedInternal(const void* src, int start_frame, | |
| 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); | |
| 53 const int channels = dest->channels(); | |
| 54 for (int ch = 0; ch < channels; ++ch) { | |
| 55 float* channel_data = dest->channel(ch); | |
| 56 for (int i = start_frame, offset = ch; i < start_frame + frames; | |
| 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 } | |
| 62 } | |
| 63 | |
| 64 // |Format| is the destination type. If a bias is present, |Fixed| must be a | |
| 65 // type larger than |Format| such that operations can be made without | |
| 66 // overflowing. Without a bias |Fixed| must be the same as |Format|. | |
| 67 template<class Format, class Fixed, Format Bias> | |
| 68 static void ToInterleavedInternal(const AudioBus* source, int start_frame, | |
| 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); | |
| 73 const int channels = source->channels(); | |
| 74 for (int ch = 0; ch < channels; ++ch) { | |
| 75 const float* channel_data = source->channel(ch); | |
| 76 for (int i = start_frame, offset = ch; i < start_frame + frames; | |
| 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 } | |
| 89 } | |
| 90 | |
| 91 static void ValidateConfig(int channels, int frames) { | 42 static void ValidateConfig(int channels, int frames) { |
| 92 CHECK_GT(frames, 0); | 43 CHECK_GT(frames, 0); |
| 93 CHECK_GT(channels, 0); | 44 CHECK_GT(channels, 0); |
| 94 CHECK_LE(channels, static_cast<int>(limits::kMaxChannels)); | 45 CHECK_LE(channels, static_cast<int>(limits::kMaxChannels)); |
| 95 } | 46 } |
| 96 | 47 |
| 97 static void CheckOverflow(int start_frame, int frames, int total_frames) { | 48 static void CheckOverflow(int start_frame, int frames, int total_frames) { |
| 98 CHECK_GE(start_frame, 0); | 49 CHECK_GE(start_frame, 0); |
| 99 CHECK_GE(frames, 0); | 50 CHECK_GE(frames, 0); |
| 100 CHECK_GT(total_frames, 0); | 51 CHECK_GT(total_frames, 0); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 for (int i = 0; i < channels; ++i) | 201 for (int i = 0; i < channels; ++i) |
| 251 channel_data_.push_back(data + i * aligned_frames); | 202 channel_data_.push_back(data + i * aligned_frames); |
| 252 } | 203 } |
| 253 | 204 |
| 254 // TODO(dalecurtis): See if intrinsic optimizations help any here. | 205 // TODO(dalecurtis): See if intrinsic optimizations help any here. |
| 255 void AudioBus::FromInterleavedPartial(const void* source, int start_frame, | 206 void AudioBus::FromInterleavedPartial(const void* source, int start_frame, |
| 256 int frames, int bytes_per_sample) { | 207 int frames, int bytes_per_sample) { |
| 257 CheckOverflow(start_frame, frames, frames_); | 208 CheckOverflow(start_frame, frames, frames_); |
| 258 switch (bytes_per_sample) { | 209 switch (bytes_per_sample) { |
| 259 case 1: | 210 case 1: |
| 260 FromInterleavedInternal<uint8_t, int16_t, kUint8Bias>( | 211 InterleavedToPlanar<uint8_t, float>( |
| 261 source, start_frame, frames, this, | 212 reinterpret_cast<const uint8_t*>(source), frames, start_frame, |
| 262 1.0f / std::numeric_limits<int8_t>::min(), | 213 this->channel_data_); |
| 263 1.0f / std::numeric_limits<int8_t>::max()); | |
| 264 break; | 214 break; |
| 265 case 2: | 215 case 2: |
| 266 FromInterleavedInternal<int16_t, int16_t, 0>( | 216 InterleavedToPlanar<int16_t, float>( |
| 267 source, start_frame, frames, this, | 217 reinterpret_cast<const int16_t*>(source), frames, start_frame, |
| 268 1.0f / std::numeric_limits<int16_t>::min(), | 218 this->channel_data_); |
| 269 1.0f / std::numeric_limits<int16_t>::max()); | |
| 270 break; | 219 break; |
| 271 case 4: | 220 case 4: |
| 272 FromInterleavedInternal<int32_t, int32_t, 0>( | 221 InterleavedToPlanar<int32_t, float>( |
| 273 source, start_frame, frames, this, | 222 reinterpret_cast<const int32_t*>(source), frames, start_frame, |
| 274 1.0f / std::numeric_limits<int32_t>::min(), | 223 this->channel_data_); |
| 275 1.0f / std::numeric_limits<int32_t>::max()); | |
| 276 break; | 224 break; |
| 277 default: | 225 default: |
| 278 NOTREACHED() << "Unsupported bytes per sample encountered."; | 226 NOTREACHED() << "Unsupported bytes per sample encountered."; |
| 279 ZeroFramesPartial(start_frame, frames); | 227 ZeroFramesPartial(start_frame, frames); |
| 280 return; | 228 return; |
| 281 } | 229 } |
| 282 | 230 |
| 283 // Don't clear remaining frames if this is a partial deinterleave. | 231 // Don't clear remaining frames if this is a partial deinterleave. |
| 284 if (!start_frame) { | 232 if (!start_frame) { |
| 285 // Zero any remaining frames. | 233 // Zero any remaining frames. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 296 void* dest) const { | 244 void* dest) const { |
| 297 ToInterleavedPartial(0, frames, bytes_per_sample, dest); | 245 ToInterleavedPartial(0, frames, bytes_per_sample, dest); |
| 298 } | 246 } |
| 299 | 247 |
| 300 // TODO(dalecurtis): See if intrinsic optimizations help any here. | 248 // TODO(dalecurtis): See if intrinsic optimizations help any here. |
| 301 void AudioBus::ToInterleavedPartial(int start_frame, int frames, | 249 void AudioBus::ToInterleavedPartial(int start_frame, int frames, |
| 302 int bytes_per_sample, void* dest) const { | 250 int bytes_per_sample, void* dest) const { |
| 303 CheckOverflow(start_frame, frames, frames_); | 251 CheckOverflow(start_frame, frames, frames_); |
| 304 switch (bytes_per_sample) { | 252 switch (bytes_per_sample) { |
| 305 case 1: | 253 case 1: |
| 306 ToInterleavedInternal<uint8_t, int16_t, kUint8Bias>( | 254 PlanarToInterleaved<float>(this->channel_data_, frames, start_frame, |
| 307 this, start_frame, frames, dest, std::numeric_limits<int8_t>::min(), | 255 reinterpret_cast<uint8_t*>(dest)); |
| 308 std::numeric_limits<int8_t>::max()); | |
| 309 break; | 256 break; |
| 310 case 2: | 257 case 2: |
| 311 ToInterleavedInternal<int16_t, int16_t, 0>( | 258 PlanarToInterleaved<float>(this->channel_data_, frames, start_frame, |
| 312 this, start_frame, frames, dest, std::numeric_limits<int16_t>::min(), | 259 reinterpret_cast<int16_t*>(dest)); |
| 313 std::numeric_limits<int16_t>::max()); | |
| 314 break; | 260 break; |
| 315 case 4: | 261 case 4: |
| 316 ToInterleavedInternal<int32_t, int32_t, 0>( | 262 PlanarToInterleaved<float>(this->channel_data_, frames, start_frame, |
| 317 this, start_frame, frames, dest, std::numeric_limits<int32_t>::min(), | 263 reinterpret_cast<int32_t*>(dest)); |
| 318 std::numeric_limits<int32_t>::max()); | |
| 319 break; | 264 break; |
| 320 default: | 265 default: |
| 321 NOTREACHED() << "Unsupported bytes per sample encountered."; | 266 NOTREACHED() << "Unsupported bytes per sample encountered."; |
| 322 memset(dest, 0, frames * bytes_per_sample); | 267 memset(dest, 0, frames * bytes_per_sample); |
| 323 return; | 268 return; |
| 324 } | 269 } |
| 325 } | 270 } |
| 326 | 271 |
| 327 void AudioBus::CopyTo(AudioBus* dest) const { | 272 void AudioBus::CopyTo(AudioBus* dest) const { |
| 328 CopyPartialFramesTo(0, frames(), 0, dest); | 273 CopyPartialFramesTo(0, frames(), 0, dest); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 return scoped_refptr<AudioBusRefCounted>( | 311 return scoped_refptr<AudioBusRefCounted>( |
| 367 new AudioBusRefCounted(channels, frames)); | 312 new AudioBusRefCounted(channels, frames)); |
| 368 } | 313 } |
| 369 | 314 |
| 370 AudioBusRefCounted::AudioBusRefCounted(int channels, int frames) | 315 AudioBusRefCounted::AudioBusRefCounted(int channels, int frames) |
| 371 : AudioBus(channels, frames) {} | 316 : AudioBus(channels, frames) {} |
| 372 | 317 |
| 373 AudioBusRefCounted::~AudioBusRefCounted() {} | 318 AudioBusRefCounted::~AudioBusRefCounted() {} |
| 374 | 319 |
| 375 } // namespace media | 320 } // namespace media |
| OLD | NEW |