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/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
15 #include "media/base/audio_parameters.h" | 15 #include "media/base/audio_parameters.h" |
16 #include "media/base/limits.h" | 16 #include "media/base/limits.h" |
17 #include "media/base/vector_math.h" | 17 #include "media/base/vector_math.h" |
18 | 18 |
19 namespace media { | 19 namespace media { |
20 | 20 |
21 static const uint8_t kUint8Bias = 128; | 21 static const uint8_t kUint8Bias = 128; |
22 | 22 |
23 static bool IsAligned(void* ptr) { | 23 static bool IsAligned(void* ptr) { |
24 return (reinterpret_cast<uintptr_t>(ptr) & | 24 return (reinterpret_cast<uintptr_t>(ptr) & |
25 (AudioBus::kChannelAlignment - 1)) == 0U; | 25 (AudioBus::kChannelAlignment - 1)) == 0U; |
26 } | 26 } |
27 | 27 |
28 // Calculates the required size for an AudioBus with the given params, sets | 28 // In order to guarantee that the memory block for each channel starts at an |
29 // |aligned_frames| to the actual frame length of each channel array. | 29 // aligned address when splitting a contiguous block of memory into one block |
30 // per channel, we may have to make these blocks larger than otherwise needed. | |
31 // We do this by allocating space for potentially more frames than requested. | |
32 // This method returns the required size for the contiguous memory block | |
33 // in bytes and outputs the adjusted number of frames via |out_aligned_frames|. | |
30 static int CalculateMemorySizeInternal(int channels, int frames, | 34 static int CalculateMemorySizeInternal(int channels, int frames, |
31 int* out_aligned_frames) { | 35 int* out_aligned_frames) { |
32 // Choose a size such that each channel will be aligned by | 36 // Since our internal sample format is float, we can guarantee the alignment |
33 // kChannelAlignment when stored in a contiguous block. | 37 // by making the number of frames an integer multiple of |
38 // AudioBus::kChannelAlignment / sizeof(float). | |
34 int aligned_frames = | 39 int aligned_frames = |
35 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & | 40 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & |
36 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); | 41 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); |
37 | 42 |
38 if (out_aligned_frames) | 43 if (out_aligned_frames) |
39 *out_aligned_frames = aligned_frames; | 44 *out_aligned_frames = aligned_frames; |
40 | 45 |
41 return sizeof(float) * channels * aligned_frames; | 46 return sizeof(float) * channels * aligned_frames; |
42 } | 47 } |
43 | 48 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 params.channels(), params.frames_per_buffer(), NULL); | 246 params.channels(), params.frames_per_buffer(), NULL); |
242 } | 247 } |
243 | 248 |
244 int AudioBus::CalculateMemorySize(int channels, int frames) { | 249 int AudioBus::CalculateMemorySize(int channels, int frames) { |
245 return CalculateMemorySizeInternal(channels, frames, NULL); | 250 return CalculateMemorySizeInternal(channels, frames, NULL); |
246 } | 251 } |
247 | 252 |
248 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { | 253 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { |
249 DCHECK(IsAligned(data)); | 254 DCHECK(IsAligned(data)); |
250 DCHECK_EQ(channel_data_.size(), 0U); | 255 DCHECK_EQ(channel_data_.size(), 0U); |
251 // Separate audio data out into channels for easy lookup later. Figure out | 256 // Initialize |channel_data_| with pointers into |data|. |
252 channel_data_.reserve(channels); | 257 channel_data_.reserve(channels); |
253 for (int i = 0; i < channels; ++i) | 258 for (int i = 0; i < channels; ++i) |
254 channel_data_.push_back(data + i * aligned_frames); | 259 channel_data_.push_back(data + i * aligned_frames); |
255 } | 260 } |
256 | 261 |
257 // TODO(dalecurtis): See if intrinsic optimizations help any here. | 262 // TODO(dalecurtis): See if intrinsic optimizations help any here. |
258 void AudioBus::FromInterleavedPartial(const void* source, int start_frame, | 263 void AudioBus::FromInterleavedPartial(const void* source, int start_frame, |
259 int frames, int bytes_per_sample) { | 264 int frames, int bytes_per_sample) { |
260 CheckOverflow(start_frame, frames, frames_); | 265 CheckOverflow(start_frame, frames, frames_); |
261 switch (bytes_per_sample) { | 266 switch (bytes_per_sample) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 void AudioBus::FromInterleaved(const void* source, int frames, | 298 void AudioBus::FromInterleaved(const void* source, int frames, |
294 int bytes_per_sample) { | 299 int bytes_per_sample) { |
295 FromInterleavedPartial(source, 0, frames, bytes_per_sample); | 300 FromInterleavedPartial(source, 0, frames, bytes_per_sample); |
296 } | 301 } |
297 | 302 |
298 void AudioBus::ToInterleaved(int frames, int bytes_per_sample, | 303 void AudioBus::ToInterleaved(int frames, int bytes_per_sample, |
299 void* dest) const { | 304 void* dest) const { |
300 ToInterleavedPartial(0, frames, bytes_per_sample, dest); | 305 ToInterleavedPartial(0, frames, bytes_per_sample, dest); |
301 } | 306 } |
302 | 307 |
308 // Interleaves |audio_bus| channels() of floats into a single output linear | |
309 // |buffer|. | |
310 void AudioBus::ToInterleavedFloat(int source_offset, | |
311 int destination_offset, | |
312 int num_samples, | |
miu
2016/06/02 20:40:21
s/num_samples/frames/ (for naming consistency with
| |
313 float* buffer) const { | |
314 CheckOverflow(source_offset, destination_offset, num_samples); | |
315 for (int ch = 0; ch < channels(); ++ch) { | |
316 const float* src = channel(ch) + source_offset; | |
317 const float* const src_end = src + num_samples; | |
318 float* dest = buffer + destination_offset + ch; | |
319 for (; src < src_end; ++src, dest += channels()) | |
320 *dest = *src; | |
321 } | |
322 } | |
323 | |
303 // TODO(dalecurtis): See if intrinsic optimizations help any here. | 324 // TODO(dalecurtis): See if intrinsic optimizations help any here. |
304 void AudioBus::ToInterleavedPartial(int start_frame, int frames, | 325 void AudioBus::ToInterleavedPartial(int start_frame, int frames, |
305 int bytes_per_sample, void* dest) const { | 326 int bytes_per_sample, void* dest) const { |
306 CheckOverflow(start_frame, frames, frames_); | 327 CheckOverflow(start_frame, frames, frames_); |
307 switch (bytes_per_sample) { | 328 switch (bytes_per_sample) { |
308 case 1: | 329 case 1: |
309 ToInterleavedInternal<uint8_t, int16_t, kUint8Bias>( | 330 ToInterleavedInternal<uint8_t, int16_t, kUint8Bias>( |
310 this, start_frame, frames, dest, std::numeric_limits<int8_t>::min(), | 331 this, start_frame, frames, dest, std::numeric_limits<int8_t>::min(), |
311 std::numeric_limits<int8_t>::max()); | 332 std::numeric_limits<int8_t>::max()); |
312 break; | 333 break; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 return scoped_refptr<AudioBusRefCounted>( | 390 return scoped_refptr<AudioBusRefCounted>( |
370 new AudioBusRefCounted(channels, frames)); | 391 new AudioBusRefCounted(channels, frames)); |
371 } | 392 } |
372 | 393 |
373 AudioBusRefCounted::AudioBusRefCounted(int channels, int frames) | 394 AudioBusRefCounted::AudioBusRefCounted(int channels, int frames) |
374 : AudioBus(channels, frames) {} | 395 : AudioBus(channels, frames) {} |
375 | 396 |
376 AudioBusRefCounted::~AudioBusRefCounted() {} | 397 AudioBusRefCounted::~AudioBusRefCounted() {} |
377 | 398 |
378 } // namespace media | 399 } // namespace media |
OLD | NEW |