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> |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 void AudioBus::CheckOverflow(int start_frame, int frames, int total_frames) { | 55 void AudioBus::CheckOverflow(int start_frame, int frames, int total_frames) { |
56 CHECK_GE(start_frame, 0); | 56 CHECK_GE(start_frame, 0); |
57 CHECK_GE(frames, 0); | 57 CHECK_GE(frames, 0); |
58 CHECK_GT(total_frames, 0); | 58 CHECK_GT(total_frames, 0); |
59 int sum = start_frame + frames; | 59 int sum = start_frame + frames; |
60 CHECK_LE(sum, total_frames); | 60 CHECK_LE(sum, total_frames); |
61 CHECK_GE(sum, 0); | 61 CHECK_GE(sum, 0); |
62 } | 62 } |
63 | 63 |
64 AudioBus::AudioBus(int channels, int frames) | 64 AudioBus::AudioBus(int channels, int frames) |
65 : frames_(frames), | 65 : data_size_(0), |
| 66 is_raw_format_(false), |
| 67 frames_(frames), |
66 can_set_channel_data_(false) { | 68 can_set_channel_data_(false) { |
67 ValidateConfig(channels, frames_); | 69 ValidateConfig(channels, frames_); |
68 | 70 |
69 int aligned_frames = 0; | 71 int aligned_frames = 0; |
70 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames); | 72 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames); |
71 | 73 |
72 data_.reset(static_cast<float*>(base::AlignedAlloc( | 74 data_.reset(static_cast<float*>(base::AlignedAlloc( |
73 size, AudioBus::kChannelAlignment))); | 75 size, AudioBus::kChannelAlignment))); |
74 | 76 |
75 BuildChannelData(channels, aligned_frames, data_.get()); | 77 BuildChannelData(channels, aligned_frames, data_.get()); |
76 } | 78 } |
77 | 79 |
78 AudioBus::AudioBus(int channels, int frames, float* data) | 80 AudioBus::AudioBus(int channels, int frames, float* data) |
79 : frames_(frames), | 81 : data_size_(0), |
| 82 is_raw_format_(false), |
| 83 frames_(frames), |
80 can_set_channel_data_(false) { | 84 can_set_channel_data_(false) { |
81 // Since |data| may have come from an external source, ensure it's valid. | 85 // Since |data| may have come from an external source, ensure it's valid. |
82 CHECK(data); | 86 CHECK(data); |
83 ValidateConfig(channels, frames_); | 87 ValidateConfig(channels, frames_); |
84 | 88 |
85 int aligned_frames = 0; | 89 int aligned_frames = 0; |
86 CalculateMemorySizeInternal(channels, frames, &aligned_frames); | 90 CalculateMemorySizeInternal(channels, frames, &aligned_frames); |
87 | 91 |
88 BuildChannelData(channels, aligned_frames, data); | 92 BuildChannelData(channels, aligned_frames, data); |
89 } | 93 } |
90 | 94 |
91 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) | 95 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) |
92 : channel_data_(channel_data), | 96 : data_size_(0), |
| 97 is_raw_format_(false), |
| 98 channel_data_(channel_data), |
93 frames_(frames), | 99 frames_(frames), |
94 can_set_channel_data_(false) { | 100 can_set_channel_data_(false) { |
95 ValidateConfig( | 101 ValidateConfig( |
96 base::checked_cast<int>(channel_data_.size()), frames_); | 102 base::checked_cast<int>(channel_data_.size()), frames_); |
97 | 103 |
98 // Sanity check wrapped vector for alignment and channel count. | 104 // Sanity check wrapped vector for alignment and channel count. |
99 for (size_t i = 0; i < channel_data_.size(); ++i) | 105 for (size_t i = 0; i < channel_data_.size(); ++i) |
100 DCHECK(IsAligned(channel_data_[i])); | 106 DCHECK(IsAligned(channel_data_[i])); |
101 } | 107 } |
102 | 108 |
103 AudioBus::AudioBus(int channels) | 109 AudioBus::AudioBus(int channels) |
104 : channel_data_(channels), | 110 : data_size_(0), |
| 111 is_raw_format_(false), |
| 112 channel_data_(channels), |
105 frames_(0), | 113 frames_(0), |
106 can_set_channel_data_(true) { | 114 can_set_channel_data_(true) { |
107 CHECK_GT(channels, 0); | 115 CHECK_GT(channels, 0); |
108 for (size_t i = 0; i < channel_data_.size(); ++i) | 116 for (size_t i = 0; i < channel_data_.size(); ++i) |
109 channel_data_[i] = NULL; | 117 channel_data_[i] = NULL; |
110 } | 118 } |
111 | 119 |
112 AudioBus::~AudioBus() {} | 120 AudioBus::~AudioBus() {} |
113 | 121 |
114 std::unique_ptr<AudioBus> AudioBus::Create(int channels, int frames) { | 122 std::unique_ptr<AudioBus> AudioBus::Create(int channels, int frames) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 158 |
151 void AudioBus::SetChannelData(int channel, float* data) { | 159 void AudioBus::SetChannelData(int channel, float* data) { |
152 CHECK(can_set_channel_data_); | 160 CHECK(can_set_channel_data_); |
153 CHECK(data); | 161 CHECK(data); |
154 CHECK_GE(channel, 0); | 162 CHECK_GE(channel, 0); |
155 CHECK_LT(static_cast<size_t>(channel), channel_data_.size()); | 163 CHECK_LT(static_cast<size_t>(channel), channel_data_.size()); |
156 DCHECK(IsAligned(data)); | 164 DCHECK(IsAligned(data)); |
157 channel_data_[channel] = data; | 165 channel_data_[channel] = data; |
158 } | 166 } |
159 | 167 |
| 168 void AudioBus::set_data_size(int data_size) { |
| 169 data_size_ = data_size; |
| 170 } |
| 171 |
| 172 void AudioBus::set_is_raw_format(bool is_raw_format) { |
| 173 is_raw_format_ = is_raw_format; |
| 174 } |
| 175 |
160 void AudioBus::set_frames(int frames) { | 176 void AudioBus::set_frames(int frames) { |
161 CHECK(can_set_channel_data_); | 177 if (!is_raw_format()) { |
162 ValidateConfig(static_cast<int>(channel_data_.size()), frames); | 178 CHECK(can_set_channel_data_); |
| 179 ValidateConfig(static_cast<int>(channel_data_.size()), frames); |
| 180 } |
163 frames_ = frames; | 181 frames_ = frames; |
164 } | 182 } |
165 | 183 |
166 void AudioBus::ZeroFramesPartial(int start_frame, int frames) { | 184 void AudioBus::ZeroFramesPartial(int start_frame, int frames) { |
167 CheckOverflow(start_frame, frames, frames_); | 185 CheckOverflow(start_frame, frames, frames_); |
168 | 186 |
169 if (frames <= 0) | 187 if (frames <= 0) |
170 return; | 188 return; |
171 | 189 |
172 for (size_t i = 0; i < channel_data_.size(); ++i) { | 190 for (size_t i = 0; i < channel_data_.size(); ++i) { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 ToInterleavedPartial<SignedInt32SampleTypeTraits>( | 319 ToInterleavedPartial<SignedInt32SampleTypeTraits>( |
302 start_frame, frames, reinterpret_cast<int32_t*>(dest)); | 320 start_frame, frames, reinterpret_cast<int32_t*>(dest)); |
303 break; | 321 break; |
304 default: | 322 default: |
305 NOTREACHED() << "Unsupported bytes per sample encountered: " | 323 NOTREACHED() << "Unsupported bytes per sample encountered: " |
306 << bytes_per_sample; | 324 << bytes_per_sample; |
307 } | 325 } |
308 } | 326 } |
309 | 327 |
310 void AudioBus::CopyTo(AudioBus* dest) const { | 328 void AudioBus::CopyTo(AudioBus* dest) const { |
| 329 dest->set_is_raw_format(is_raw_format()); |
| 330 if (is_raw_format()) { |
| 331 dest->set_data_size(data_size()); |
| 332 dest->set_frames(frames()); |
| 333 memcpy(dest->channel(0), channel(0), data_size()); |
| 334 return; |
| 335 } |
| 336 |
311 CopyPartialFramesTo(0, frames(), 0, dest); | 337 CopyPartialFramesTo(0, frames(), 0, dest); |
312 } | 338 } |
313 | 339 |
314 void AudioBus::CopyPartialFramesTo(int source_start_frame, | 340 void AudioBus::CopyPartialFramesTo(int source_start_frame, |
315 int frame_count, | 341 int frame_count, |
316 int dest_start_frame, | 342 int dest_start_frame, |
317 AudioBus* dest) const { | 343 AudioBus* dest) const { |
318 CHECK_EQ(channels(), dest->channels()); | 344 CHECK_EQ(channels(), dest->channels()); |
319 CHECK_LE(source_start_frame + frame_count, frames()); | 345 CHECK_LE(source_start_frame + frame_count, frames()); |
320 CHECK_LE(dest_start_frame + frame_count, dest->frames()); | 346 CHECK_LE(dest_start_frame + frame_count, dest->frames()); |
(...skipping 28 matching lines...) Expand all Loading... |
349 return scoped_refptr<AudioBusRefCounted>( | 375 return scoped_refptr<AudioBusRefCounted>( |
350 new AudioBusRefCounted(channels, frames)); | 376 new AudioBusRefCounted(channels, frames)); |
351 } | 377 } |
352 | 378 |
353 AudioBusRefCounted::AudioBusRefCounted(int channels, int frames) | 379 AudioBusRefCounted::AudioBusRefCounted(int channels, int frames) |
354 : AudioBus(channels, frames) {} | 380 : AudioBus(channels, frames) {} |
355 | 381 |
356 AudioBusRefCounted::~AudioBusRefCounted() {} | 382 AudioBusRefCounted::~AudioBusRefCounted() {} |
357 | 383 |
358 } // namespace media | 384 } // namespace media |
OLD | NEW |