| 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 static bool IsAligned(void* ptr) { | 15 static bool IsAligned(void* ptr) { |
| 16 return (reinterpret_cast<uintptr_t>(ptr) & | 16 return (reinterpret_cast<uintptr_t>(ptr) & |
| 17 (AudioBus::kChannelAlignment - 1)) == 0U; | 17 (AudioBus::kChannelAlignment - 1)) == 0U; |
| 18 } | 18 } |
| 19 | 19 |
| 20 // Calculates the required size for an AudioBus with the given params, sets | |
| 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 will be aligned by kChannelAlignment | |
| 26 // when stored in a contiguous block. | |
| 27 *aligned_frames = | |
| 28 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & | |
| 29 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); | |
| 30 return sizeof(float) * channels * (*aligned_frames); | |
| 31 } | |
| 32 | |
| 33 // |Format| is the destination type, |Fixed| is a type larger than |Format| | 20 // |Format| is the destination type, |Fixed| is a type larger than |Format| |
| 34 // such that operations can be made without overflowing. | 21 // such that operations can be made without overflowing. |
| 35 template<class Format, class Fixed> | 22 template<class Format, class Fixed> |
| 36 static void FromInterleavedInternal(const void* src, int start_frame, | 23 static void FromInterleavedInternal(const void* src, int start_frame, |
| 37 int frames, AudioBus* dest) { | 24 int frames, AudioBus* dest) { |
| 38 const Format* source = static_cast<const Format*>(src); | 25 const Format* source = static_cast<const Format*>(src); |
| 39 | 26 |
| 40 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : | 27 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : |
| 41 std::numeric_limits<Format>::max() / 2 + 1; | 28 std::numeric_limits<Format>::max() / 2 + 1; |
| 42 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 : | 29 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 : |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 int sum = start_frame + frames; | 86 int sum = start_frame + frames; |
| 100 CHECK_LE(sum, total_frames); | 87 CHECK_LE(sum, total_frames); |
| 101 CHECK_GE(sum, 0); | 88 CHECK_GE(sum, 0); |
| 102 } | 89 } |
| 103 | 90 |
| 104 AudioBus::AudioBus(int channels, int frames) | 91 AudioBus::AudioBus(int channels, int frames) |
| 105 : frames_(frames) { | 92 : frames_(frames) { |
| 106 ValidateConfig(channels, frames_); | 93 ValidateConfig(channels, frames_); |
| 107 | 94 |
| 108 int aligned_frames = 0; | 95 int aligned_frames = 0; |
| 109 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames); | 96 int size = CalculateMemorySizeFromChannels(channels, frames, &aligned_frames); |
| 110 | 97 |
| 111 data_.reset(static_cast<float*>(base::AlignedAlloc( | 98 data_.reset(static_cast<float*>(base::AlignedAlloc( |
| 112 size, AudioBus::kChannelAlignment))); | 99 size, AudioBus::kChannelAlignment))); |
| 113 | 100 |
| 114 BuildChannelData(channels, aligned_frames, data_.get()); | 101 BuildChannelData(channels, aligned_frames, data_.get()); |
| 115 } | 102 } |
| 116 | 103 |
| 117 AudioBus::AudioBus(int channels, int frames, float* data) | 104 AudioBus::AudioBus(int channels, int frames, float* data) |
| 118 : frames_(frames) { | 105 : frames_(frames) { |
| 119 ValidateConfig(channels, frames_); | 106 ValidateConfig(channels, frames_); |
| 120 | 107 |
| 121 int aligned_frames = 0; | 108 int aligned_frames = 0; |
| 122 CalculateMemorySizeInternal(channels, frames, &aligned_frames); | 109 CalculateMemorySizeFromChannels(channels, frames, &aligned_frames); |
| 123 | 110 |
| 124 BuildChannelData(channels, aligned_frames, data); | 111 BuildChannelData(channels, aligned_frames, data); |
| 125 } | 112 } |
| 126 | 113 |
| 127 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) | 114 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) |
| 128 : channel_data_(channel_data), | 115 : channel_data_(channel_data), |
| 129 frames_(frames) { | 116 frames_(frames) { |
| 130 ValidateConfig(channel_data_.size(), frames_); | 117 ValidateConfig(channel_data_.size(), frames_); |
| 131 | 118 |
| 132 // Sanity check wrapped vector for alignment and channel count. | 119 // Sanity check wrapped vector for alignment and channel count. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 } | 167 } |
| 181 | 168 |
| 182 void AudioBus::ZeroFrames(int frames) { | 169 void AudioBus::ZeroFrames(int frames) { |
| 183 ZeroFramesPartial(0, frames); | 170 ZeroFramesPartial(0, frames); |
| 184 } | 171 } |
| 185 | 172 |
| 186 void AudioBus::Zero() { | 173 void AudioBus::Zero() { |
| 187 ZeroFrames(frames_); | 174 ZeroFrames(frames_); |
| 188 } | 175 } |
| 189 | 176 |
| 177 // Calculates the required size for an AudioBus with the given params, sets |
| 178 // |aligned_frames| to the actual frame length of each channel array. |
| 179 int AudioBus::CalculateMemorySizeFromChannels(int channels, int frames, |
| 180 int* out_aligned_frames) { |
| 181 // Choose a size such that each channel will be aligned by |
| 182 // kChannelAlignment when stored in a contiguous block. |
| 183 int aligned_frames = |
| 184 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & |
| 185 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); |
| 186 |
| 187 if (out_aligned_frames) |
| 188 *out_aligned_frames = aligned_frames; |
| 189 |
| 190 return sizeof(float) * channels * aligned_frames; |
| 191 } |
| 192 |
| 190 int AudioBus::CalculateMemorySize(const AudioParameters& params) { | 193 int AudioBus::CalculateMemorySize(const AudioParameters& params) { |
| 191 int aligned_frames = 0; | 194 int aligned_frames = 0; |
| 192 return CalculateMemorySizeInternal( | 195 return CalculateMemorySizeFromChannels( |
| 193 params.channels(), params.frames_per_buffer(), &aligned_frames); | 196 params.channels(), params.frames_per_buffer(), &aligned_frames); |
| 194 } | 197 } |
| 195 | 198 |
| 196 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { | 199 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { |
| 197 DCHECK(IsAligned(data)); | 200 DCHECK(IsAligned(data)); |
| 198 DCHECK_EQ(channel_data_.size(), 0U); | 201 DCHECK_EQ(channel_data_.size(), 0U); |
| 199 // Separate audio data out into channels for easy lookup later. Figure out | 202 // Separate audio data out into channels for easy lookup later. Figure out |
| 200 channel_data_.reserve(channels); | 203 channel_data_.reserve(channels); |
| 201 for (int i = 0; i < channels; ++i) | 204 for (int i = 0; i < channels; ++i) |
| 202 channel_data_.push_back(data + i * aligned_frames); | 205 channel_data_.push_back(data + i * aligned_frames); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 DCHECK_EQ(channels(), dest->channels()); | 262 DCHECK_EQ(channels(), dest->channels()); |
| 260 DCHECK_EQ(frames(), dest->frames()); | 263 DCHECK_EQ(frames(), dest->frames()); |
| 261 | 264 |
| 262 // Since we don't know if the other AudioBus is wrapped or not (and we don't | 265 // Since we don't know if the other AudioBus is wrapped or not (and we don't |
| 263 // want to care), just copy using the public channel() accessors. | 266 // want to care), just copy using the public channel() accessors. |
| 264 for (int i = 0; i < channels(); ++i) | 267 for (int i = 0; i < channels(); ++i) |
| 265 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); | 268 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); |
| 266 } | 269 } |
| 267 | 270 |
| 268 } // namespace media | 271 } // namespace media |
| OLD | NEW |