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 | 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. | 21 // |aligned_frames| to the actual frame length of each channel array. |
22 static int CalculateMemorySizeInternal(int channels, int frames, | 22 static int CalculateMemorySizeInternal(int channels, int frames, |
23 int* aligned_frames) { | 23 int* out_aligned_frames) { |
24 CHECK(aligned_frames); | 24 // Choose a size such that each channel will be aligned by |
25 // Choose a size such that each channel will be aligned by kChannelAlignment | 25 // kChannelAlignment when stored in a contiguous block. |
26 // when stored in a contiguous block. | 26 int aligned_frames = |
27 *aligned_frames = | |
28 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & | 27 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & |
29 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); | 28 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); |
30 return sizeof(float) * channels * (*aligned_frames); | 29 |
| 30 if (out_aligned_frames) |
| 31 *out_aligned_frames = aligned_frames; |
| 32 |
| 33 return sizeof(float) * channels * aligned_frames; |
31 } | 34 } |
32 | 35 |
33 // |Format| is the destination type, |Fixed| is a type larger than |Format| | 36 // |Format| is the destination type, |Fixed| is a type larger than |Format| |
34 // such that operations can be made without overflowing. | 37 // such that operations can be made without overflowing. |
35 template<class Format, class Fixed> | 38 template<class Format, class Fixed> |
36 static void FromInterleavedInternal(const void* src, int start_frame, | 39 static void FromInterleavedInternal(const void* src, int start_frame, |
37 int frames, AudioBus* dest) { | 40 int frames, AudioBus* dest) { |
38 const Format* source = static_cast<const Format*>(src); | 41 const Format* source = static_cast<const Format*>(src); |
39 | 42 |
40 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : | 43 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 | 184 |
182 void AudioBus::ZeroFrames(int frames) { | 185 void AudioBus::ZeroFrames(int frames) { |
183 ZeroFramesPartial(0, frames); | 186 ZeroFramesPartial(0, frames); |
184 } | 187 } |
185 | 188 |
186 void AudioBus::Zero() { | 189 void AudioBus::Zero() { |
187 ZeroFrames(frames_); | 190 ZeroFrames(frames_); |
188 } | 191 } |
189 | 192 |
190 int AudioBus::CalculateMemorySize(const AudioParameters& params) { | 193 int AudioBus::CalculateMemorySize(const AudioParameters& params) { |
191 int aligned_frames = 0; | |
192 return CalculateMemorySizeInternal( | 194 return CalculateMemorySizeInternal( |
193 params.channels(), params.frames_per_buffer(), &aligned_frames); | 195 params.channels(), params.frames_per_buffer(), NULL); |
| 196 } |
| 197 |
| 198 int AudioBus::CalculateMemorySize(int channels, int frames) { |
| 199 return CalculateMemorySizeInternal(channels, frames, NULL); |
194 } | 200 } |
195 | 201 |
196 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { | 202 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { |
197 DCHECK(IsAligned(data)); | 203 DCHECK(IsAligned(data)); |
198 DCHECK_EQ(channel_data_.size(), 0U); | 204 DCHECK_EQ(channel_data_.size(), 0U); |
199 // Separate audio data out into channels for easy lookup later. Figure out | 205 // Separate audio data out into channels for easy lookup later. Figure out |
200 channel_data_.reserve(channels); | 206 channel_data_.reserve(channels); |
201 for (int i = 0; i < channels; ++i) | 207 for (int i = 0; i < channels; ++i) |
202 channel_data_.push_back(data + i * aligned_frames); | 208 channel_data_.push_back(data + i * aligned_frames); |
203 } | 209 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 DCHECK_EQ(channels(), dest->channels()); | 265 DCHECK_EQ(channels(), dest->channels()); |
260 DCHECK_EQ(frames(), dest->frames()); | 266 DCHECK_EQ(frames(), dest->frames()); |
261 | 267 |
262 // Since we don't know if the other AudioBus is wrapped or not (and we don't | 268 // 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. | 269 // want to care), just copy using the public channel() accessors. |
264 for (int i = 0; i < channels(); ++i) | 270 for (int i = 0; i < channels(); ++i) |
265 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); | 271 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); |
266 } | 272 } |
267 | 273 |
268 } // namespace media | 274 } // namespace media |
OLD | NEW |