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

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: Comments. Created 8 years, 3 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
« no previous file with comments | « media/base/audio_bus.h ('k') | media/base/audio_bus_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 19 matching lines...) Expand all
80 else if (sample < kMinValue) 81 else if (sample < kMinValue)
81 sample = kMinValue; 82 sample = kMinValue;
82 83
83 dest[offset] = static_cast<Format>(sample) + kBias; 84 dest[offset] = static_cast<Format>(sample) + kBias;
84 } 85 }
85 } 86 }
86 } 87 }
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_GT(channels, 0); 91 CHECK_GT(channels, 0);
92 CHECK_LE(channels, limits::kMaxChannels); 92 CHECK_LE(channels, limits::kMaxChannels);
93 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, 93 }
94 std::numeric_limits<int>::max()); 94
95 static void CheckOverflow(int start_frame, int frames, int total_frames) {
96 CHECK_GE(start_frame, 0);
97 CHECK_GE(frames, 0);
98 CHECK_GT(total_frames, 0);
99 int sum = start_frame + frames;
Chris Rogers 2012/08/29 05:16:19 It looks like if sum overflows then it becomes neg
100 CHECK_LE(sum, total_frames);
101 CHECK_GE(sum, 0);
95 } 102 }
96 103
97 AudioBus::AudioBus(int channels, int frames) 104 AudioBus::AudioBus(int channels, int frames)
98 : frames_(frames) { 105 : frames_(frames) {
99 ValidateConfig(channels, frames_); 106 ValidateConfig(channels, frames_);
100 107
101 int aligned_frames = 0; 108 int aligned_frames = 0;
102 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames); 109 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames);
103 110
104 data_.reset(static_cast<float*>(base::AlignedAlloc( 111 data_.reset(static_cast<float*>(base::AlignedAlloc(
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 160
154 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params, 161 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params,
155 void* data) { 162 void* data) {
156 // |data| must be aligned by AudioBus::kChannelAlignment. 163 // |data| must be aligned by AudioBus::kChannelAlignment.
157 CHECK(IsAligned(data)); 164 CHECK(IsAligned(data));
158 return scoped_ptr<AudioBus>(new AudioBus( 165 return scoped_ptr<AudioBus>(new AudioBus(
159 params.channels(), params.frames_per_buffer(), 166 params.channels(), params.frames_per_buffer(),
160 static_cast<float*>(data))); 167 static_cast<float*>(data)));
161 } 168 }
162 169
170 void AudioBus::ZeroFramesPartial(int start_frame, int frames) {
171 CheckOverflow(start_frame, frames, frames_);
172
173 if (frames <= 0)
174 return;
175
176 for (size_t i = 0; i < channel_data_.size(); ++i) {
177 memset(channel_data_[i] + start_frame, 0,
178 frames * sizeof(*channel_data_[i]));
179 }
180 }
181
163 void AudioBus::ZeroFrames(int frames) { 182 void AudioBus::ZeroFrames(int frames) {
164 DCHECK_LE(frames, frames_); 183 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 } 184 }
168 185
169 void AudioBus::Zero() { 186 void AudioBus::Zero() {
170 ZeroFrames(frames_); 187 ZeroFrames(frames_);
171 } 188 }
172 189
173 int AudioBus::CalculateMemorySize(const AudioParameters& params) { 190 int AudioBus::CalculateMemorySize(const AudioParameters& params) {
174 int aligned_frames = 0; 191 int aligned_frames = 0;
175 return CalculateMemorySizeInternal( 192 return CalculateMemorySizeInternal(
176 params.channels(), params.frames_per_buffer(), &aligned_frames); 193 params.channels(), params.frames_per_buffer(), &aligned_frames);
177 } 194 }
178 195
179 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { 196 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) {
180 DCHECK(IsAligned(data)); 197 DCHECK(IsAligned(data));
181 DCHECK_EQ(channel_data_.size(), 0U); 198 DCHECK_EQ(channel_data_.size(), 0U);
182 // Separate audio data out into channels for easy lookup later. Figure out 199 // Separate audio data out into channels for easy lookup later. Figure out
183 channel_data_.reserve(channels); 200 channel_data_.reserve(channels);
184 for (int i = 0; i < channels; ++i) 201 for (int i = 0; i < channels; ++i)
185 channel_data_.push_back(data + i * aligned_frames); 202 channel_data_.push_back(data + i * aligned_frames);
186 } 203 }
187 204
188 // TODO(dalecurtis): See if intrinsic optimizations help any here. 205 // TODO(dalecurtis): See if intrinsic optimizations help any here.
189 void AudioBus::FromInterleaved(const void* source, int frames, 206 void AudioBus::FromInterleavedPartial(const void* source, int start_frame,
190 int bytes_per_sample) { 207 int frames, int bytes_per_sample) {
191 DCHECK_LE(frames, frames_); 208 CheckOverflow(start_frame, frames, frames_);
192 switch (bytes_per_sample) { 209 switch (bytes_per_sample) {
193 case 1: 210 case 1:
194 FromInterleavedInternal<uint8, int16>(source, frames, this); 211 FromInterleavedInternal<uint8, int16>(source, start_frame, frames, this);
195 break; 212 break;
196 case 2: 213 case 2:
197 FromInterleavedInternal<int16, int32>(source, frames, this); 214 FromInterleavedInternal<int16, int32>(source, start_frame, frames, this);
198 break; 215 break;
199 case 4: 216 case 4:
200 FromInterleavedInternal<int32, int64>(source, frames, this); 217 FromInterleavedInternal<int32, int64>(source, start_frame, frames, this);
201 break; 218 break;
202 default: 219 default:
203 NOTREACHED() << "Unsupported bytes per sample encountered."; 220 NOTREACHED() << "Unsupported bytes per sample encountered.";
204 Zero(); 221 ZeroFramesPartial(start_frame, frames);
205 return; 222 return;
206 } 223 }
207 224
208 // Zero any remaining frames. 225 // Don't clear remaining frames if this is a partial deinterleave.
209 int remaining_frames = (frames_ - frames); 226 if (!start_frame) {
210 if (remaining_frames) { 227 // Zero any remaining frames.
211 for (int ch = 0; ch < channels(); ++ch) 228 ZeroFramesPartial(frames, frames_ - frames);
212 memset(channel(ch) + frames, 0, sizeof(*channel(ch)) * remaining_frames);
213 } 229 }
214 } 230 }
215 231
232 void AudioBus::FromInterleaved(const void* source, int frames,
233 int bytes_per_sample) {
234 FromInterleavedPartial(source, 0, frames, bytes_per_sample);
235 }
236
216 // TODO(dalecurtis): See if intrinsic optimizations help any here. 237 // TODO(dalecurtis): See if intrinsic optimizations help any here.
217 void AudioBus::ToInterleaved(int frames, int bytes_per_sample, 238 void AudioBus::ToInterleaved(int frames, int bytes_per_sample,
218 void* dest) const { 239 void* dest) const {
219 DCHECK_LE(frames, frames_); 240 CheckOverflow(0, frames, frames_);
220 switch (bytes_per_sample) { 241 switch (bytes_per_sample) {
221 case 1: 242 case 1:
222 ToInterleavedInternal<uint8, int16>(this, frames, dest); 243 ToInterleavedInternal<uint8, int16>(this, frames, dest);
223 break; 244 break;
224 case 2: 245 case 2:
225 ToInterleavedInternal<int16, int32>(this, frames, dest); 246 ToInterleavedInternal<int16, int32>(this, frames, dest);
226 break; 247 break;
227 case 4: 248 case 4:
228 ToInterleavedInternal<int32, int64>(this, frames, dest); 249 ToInterleavedInternal<int32, int64>(this, frames, dest);
229 break; 250 break;
230 default: 251 default:
231 NOTREACHED() << "Unsupported bytes per sample encountered."; 252 NOTREACHED() << "Unsupported bytes per sample encountered.";
232 memset(dest, 0, frames * bytes_per_sample); 253 memset(dest, 0, frames * bytes_per_sample);
233 return; 254 return;
234 } 255 }
235 } 256 }
236 257
237 void AudioBus::CopyTo(AudioBus* dest) const { 258 void AudioBus::CopyTo(AudioBus* dest) const {
238 DCHECK_EQ(channels(), dest->channels()); 259 DCHECK_EQ(channels(), dest->channels());
239 DCHECK_EQ(frames(), dest->frames()); 260 DCHECK_EQ(frames(), dest->frames());
240 261
241 // Since we don't know if the other AudioBus is wrapped or not (and we don't 262 // 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. 263 // want to care), just copy using the public channel() accessors.
243 for (int i = 0; i < channels(); ++i) 264 for (int i = 0; i < channels(); ++i)
244 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames()); 265 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames());
245 } 266 }
246 267
247 } // namespace media 268 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_bus.h ('k') | media/base/audio_bus_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698