Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1104)

Side by Side Diff: media/base/audio_bus.cc

Issue 10871051: Convert WebAudio file handlers to use AudioBus. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update webkit glue gyp. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 15 matching lines...) Expand all
26 // when stored in a contiguous block. 26 // when stored in a contiguous block.
27 *aligned_frames = 27 *aligned_frames =
28 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & 28 ((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) &
29 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float); 29 ~(AudioBus::kChannelAlignment - 1)) / sizeof(float);
30 return sizeof(float) * channels * (*aligned_frames); 30 return sizeof(float) * channels * (*aligned_frames);
31 } 31 }
32 32
33 // |Format| is the destination type, |Fixed| is a type larger than |Format| 33 // |Format| is the destination type, |Fixed| is a type larger than |Format|
34 // such that operations can be made without overflowing. 34 // such that operations can be made without overflowing.
35 template<class Format, class Fixed> 35 template<class Format, class Fixed>
36 static void FromInterleavedInternal(const void* src, int frames, 36 static void FromInterleavedInternal(const void* src, int start_frame,
37 AudioBus* dest) { 37 int frames, AudioBus* dest) {
38 const Format* source = static_cast<const Format*>(src); 38 const Format* source = static_cast<const Format*>(src);
39 39
40 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 : 40 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 :
41 std::numeric_limits<Format>::max() / 2 + 1; 41 std::numeric_limits<Format>::max() / 2 + 1;
42 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 : 42 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 :
43 std::numeric_limits<Format>::max()); 43 std::numeric_limits<Format>::max());
44 static const float kMinScale = 1.0f / (kBias ? kBias : 44 static const float kMinScale = 1.0f / (kBias ? kBias :
45 -static_cast<Fixed>(std::numeric_limits<Format>::min())); 45 -static_cast<Fixed>(std::numeric_limits<Format>::min()));
46 46
47 int channels = dest->channels(); 47 int channels = dest->channels();
48 for (int ch = 0; ch < channels; ++ch) { 48 for (int ch = 0; ch < channels; ++ch) {
49 float* channel_data = dest->channel(ch); 49 float* channel_data = dest->channel(ch);
50 for (int i = 0, offset = ch; i < frames; ++i, offset += channels) { 50 for (int i = start_frame, offset = ch; i < start_frame + frames;
51 ++i, offset += channels) {
51 Fixed v = static_cast<Fixed>(source[offset]) - kBias; 52 Fixed v = static_cast<Fixed>(source[offset]) - kBias;
52 channel_data[i] = v * (v < 0 ? kMinScale : kMaxScale); 53 channel_data[i] = v * (v < 0 ? kMinScale : kMaxScale);
53 } 54 }
54 } 55 }
55 } 56 }
56 57
57 // |Format| is the destination type, |Fixed| is a type larger than |Format| 58 // |Format| is the destination type, |Fixed| is a type larger than |Format|
58 // such that operations can be made without overflowing. 59 // such that operations can be made without overflowing.
59 template<class Format, class Fixed> 60 template<class Format, class Fixed>
60 static void ToInterleavedInternal(const AudioBus* source, int frames, 61 static void ToInterleavedInternal(const AudioBus* source, int frames,
(...skipping 26 matching lines...) Expand all
87 88
88 static void ValidateConfig(int channels, int frames) { 89 static void ValidateConfig(int channels, int frames) {
89 CHECK_GT(frames, 0); 90 CHECK_GT(frames, 0);
90 CHECK_LE(frames, limits::kMaxSamplesPerPacket); 91 CHECK_LE(frames, limits::kMaxSamplesPerPacket);
91 CHECK_GT(channels, 0); 92 CHECK_GT(channels, 0);
92 CHECK_LE(channels, limits::kMaxChannels); 93 CHECK_LE(channels, limits::kMaxChannels);
93 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, 94 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels,
94 std::numeric_limits<int>::max()); 95 std::numeric_limits<int>::max());
95 } 96 }
96 97
98 static bool CheckOverflow(int start_frame, int frames, int total_frames) {
99 DCHECK_GE(start_frame, 0);
100 DCHECK_GE(frames, 0);
101 DCHECK_GT(total_frames, 0);
102 int sum = start_frame + frames;
103 DCHECK_LE(sum, total_frames);
104 DCHECK_GE(sum, 0);
105
106 return (start_frame >= 0 && frames >= 0 && total_frames > 0 &&
vrk (LEFT CHROMIUM) 2012/08/28 20:25:12 Remove the return bool and have this a void method
DaleCurtis 2012/08/29 03:43:13 I have it this way since I wasn't sure if those DC
DaleCurtis 2012/08/29 04:47:11 Feels safer with CHECK, going that route.
107 sum <= total_frames && sum >= 0);
108 }
109
97 AudioBus::AudioBus(int channels, int frames) 110 AudioBus::AudioBus(int channels, int frames)
98 : frames_(frames) { 111 : frames_(frames) {
99 ValidateConfig(channels, frames_); 112 ValidateConfig(channels, frames_);
100 113
101 int aligned_frames = 0; 114 int aligned_frames = 0;
102 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames); 115 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames);
103 116
104 data_.reset(static_cast<float*>(base::AlignedAlloc( 117 data_.reset(static_cast<float*>(base::AlignedAlloc(
105 size, AudioBus::kChannelAlignment))); 118 size, AudioBus::kChannelAlignment)));
106 119
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 166
154 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params, 167 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params,
155 void* data) { 168 void* data) {
156 // |data| must be aligned by AudioBus::kChannelAlignment. 169 // |data| must be aligned by AudioBus::kChannelAlignment.
157 CHECK(IsAligned(data)); 170 CHECK(IsAligned(data));
158 return scoped_ptr<AudioBus>(new AudioBus( 171 return scoped_ptr<AudioBus>(new AudioBus(
159 params.channels(), params.frames_per_buffer(), 172 params.channels(), params.frames_per_buffer(),
160 static_cast<float*>(data))); 173 static_cast<float*>(data)));
161 } 174 }
162 175
176 void AudioBus::ZeroFramesPartial(int start_frame, int frames) {
177 DCHECK(CheckOverflow(start_frame, frames, frames_));
178
179 if (frames > 0) {
vrk (LEFT CHROMIUM) 2012/08/28 20:25:12 nit: reverse test and return early
DaleCurtis 2012/08/29 04:47:11 Done.
180 for (size_t i = 0; i < channel_data_.size(); ++i) {
181 memset(channel_data_[i] + start_frame, 0,
182 frames * sizeof(*channel_data_[i]));
183 }
184 }
185 }
186
163 void AudioBus::ZeroFrames(int frames) { 187 void AudioBus::ZeroFrames(int frames) {
164 DCHECK_LE(frames, frames_); 188 ZeroFramesPartial(0, frames);
165 for (size_t i = 0; i < channel_data_.size(); ++i)
166 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i]));
167 } 189 }
168 190
169 void AudioBus::Zero() { 191 void AudioBus::Zero() {
170 ZeroFrames(frames_); 192 ZeroFrames(frames_);
171 } 193 }
172 194
173 int AudioBus::CalculateMemorySize(const AudioParameters& params) { 195 int AudioBus::CalculateMemorySize(const AudioParameters& params) {
174 int aligned_frames = 0; 196 int aligned_frames = 0;
175 return CalculateMemorySizeInternal( 197 return CalculateMemorySizeInternal(
176 params.channels(), params.frames_per_buffer(), &aligned_frames); 198 params.channels(), params.frames_per_buffer(), &aligned_frames);
177 } 199 }
178 200
179 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { 201 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) {
180 DCHECK(IsAligned(data)); 202 DCHECK(IsAligned(data));
181 DCHECK_EQ(channel_data_.size(), 0U); 203 DCHECK_EQ(channel_data_.size(), 0U);
182 // Separate audio data out into channels for easy lookup later. Figure out 204 // Separate audio data out into channels for easy lookup later. Figure out
183 channel_data_.reserve(channels); 205 channel_data_.reserve(channels);
184 for (int i = 0; i < channels; ++i) 206 for (int i = 0; i < channels; ++i)
185 channel_data_.push_back(data + i * aligned_frames); 207 channel_data_.push_back(data + i * aligned_frames);
186 } 208 }
187 209
188 // TODO(dalecurtis): See if intrinsic optimizations help any here. 210 // TODO(dalecurtis): See if intrinsic optimizations help any here.
189 void AudioBus::FromInterleaved(const void* source, int frames, 211 void AudioBus::FromInterleavedPartial(const void* source, int start_frame,
190 int bytes_per_sample) { 212 int frames, int bytes_per_sample) {
191 DCHECK_LE(frames, frames_); 213 DCHECK(CheckOverflow(start_frame, frames, frames_));
192 switch (bytes_per_sample) { 214 switch (bytes_per_sample) {
193 case 1: 215 case 1:
194 FromInterleavedInternal<uint8, int16>(source, frames, this); 216 FromInterleavedInternal<uint8, int16>(source, start_frame, frames, this);
195 break; 217 break;
196 case 2: 218 case 2:
197 FromInterleavedInternal<int16, int32>(source, frames, this); 219 FromInterleavedInternal<int16, int32>(source, start_frame, frames, this);
198 break; 220 break;
199 case 4: 221 case 4:
200 FromInterleavedInternal<int32, int64>(source, frames, this); 222 FromInterleavedInternal<int32, int64>(source, start_frame, frames, this);
201 break; 223 break;
202 default: 224 default:
203 NOTREACHED() << "Unsupported bytes per sample encountered."; 225 NOTREACHED() << "Unsupported bytes per sample encountered.";
204 Zero(); 226 ZeroFramesPartial(start_frame, frames);
205 return; 227 return;
206 } 228 }
207 229
208 // Zero any remaining frames. 230 // Don't clear remaining frames if this is a partial deinterleave.
209 int remaining_frames = (frames_ - frames); 231 if (!start_frame) {
210 if (remaining_frames) { 232 // Zero any remaining frames.
211 for (int ch = 0; ch < channels(); ++ch) 233 ZeroFramesPartial(frames, frames_ - frames);
212 memset(channel(ch) + frames, 0, sizeof(*channel(ch)) * remaining_frames);
213 } 234 }
214 } 235 }
215 236
237 void AudioBus::FromInterleaved(const void* source, int frames,
238 int bytes_per_sample) {
239 FromInterleavedPartial(source, 0, frames, bytes_per_sample);
240 }
241
216 // TODO(dalecurtis): See if intrinsic optimizations help any here. 242 // TODO(dalecurtis): See if intrinsic optimizations help any here.
217 void AudioBus::ToInterleaved(int frames, int bytes_per_sample, 243 void AudioBus::ToInterleaved(int frames, int bytes_per_sample,
218 void* dest) const { 244 void* dest) const {
219 DCHECK_LE(frames, frames_); 245 DCHECK(CheckOverflow(0, frames, frames_));
220 switch (bytes_per_sample) { 246 switch (bytes_per_sample) {
221 case 1: 247 case 1:
222 ToInterleavedInternal<uint8, int16>(this, frames, dest); 248 ToInterleavedInternal<uint8, int16>(this, frames, dest);
223 break; 249 break;
224 case 2: 250 case 2:
225 ToInterleavedInternal<int16, int32>(this, frames, dest); 251 ToInterleavedInternal<int16, int32>(this, frames, dest);
226 break; 252 break;
227 case 4: 253 case 4:
228 ToInterleavedInternal<int32, int64>(this, frames, dest); 254 ToInterleavedInternal<int32, int64>(this, frames, dest);
229 break; 255 break;
230 default: 256 default:
231 NOTREACHED() << "Unsupported bytes per sample encountered."; 257 NOTREACHED() << "Unsupported bytes per sample encountered.";
232 memset(dest, 0, frames * bytes_per_sample); 258 memset(dest, 0, frames * bytes_per_sample);
233 return; 259 return;
234 } 260 }
235 } 261 }
236 262
237 void AudioBus::CopyTo(AudioBus* dest) const { 263 void AudioBus::CopyTo(AudioBus* dest) const {
238 DCHECK_EQ(channels(), dest->channels()); 264 DCHECK_EQ(channels(), dest->channels());
239 DCHECK_EQ(frames(), dest->frames()); 265 DCHECK_EQ(frames(), dest->frames());
240 266
241 // Since we don't know if the other AudioBus is wrapped or not (and we don't 267 // Since we don't know if the other AudioBus is wrapped or not (and we don't
242 // want to care), just copy using the public channel() accessors. 268 // want to care), just copy using the public channel() accessors.
243 for (int i = 0; i < channels(); ++i) 269 for (int i = 0; i < channels(); ++i)
244 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); 270 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames());
245 } 271 }
246 272
247 } // namespace media 273 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698