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

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

Issue 10824304: Upgrade AudioBus to support wrapping, interleaving. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. 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"
11 #include "media/base/limits.h" 11 #include "media/base/limits.h"
12 12
13 namespace media { 13 namespace media {
14 14
15 // Ensure each channel is 16-byte aligned for easy SSE optimizations.
16 static const int kChannelAlignment = 16;
17
18 static bool IsAligned(void* ptr) { 15 static bool IsAligned(void* ptr) {
19 return (reinterpret_cast<uintptr_t>(ptr) & (kChannelAlignment - 1)) == 0U; 16 return (reinterpret_cast<uintptr_t>(ptr) &
17 (AudioBus::kChannelAlignment - 1)) == 0U;
20 } 18 }
21 19
22 AudioBus::AudioBus(int channels, int frames) 20 // Calculates the required size for an AudioBus with the given params, sets
23 : frames_(frames) { 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|
34 // such that operations can be made without overflowing.
35 template<class Format, class Fixed>
36 static void FromInterleavedInternal(const void* src, int frames,
37 AudioBus* dest) {
38 const Format* source = static_cast<const Format*>(src);
39
40 static const Fixed kBias = std::numeric_limits<Format>::is_signed ? 0 :
41 std::numeric_limits<Format>::max() / 2 + 1;
42 static const float kMaxScale = 1.0f / (kBias ? kBias - 1 :
43 std::numeric_limits<Format>::max());
44 static const float kMinScale = 1.0f / (kBias ? kBias :
45 -static_cast<Fixed>(std::numeric_limits<Format>::min()));
46
47 int channels = dest->channels();
48 for (int ch = 0; ch < channels; ++ch) {
49 float* channel_data = dest->channel(ch);
50 for (int i = 0, offset = ch; i < frames; ++i, offset += channels) {
51 Fixed v = static_cast<Fixed>(source[offset]) - kBias;
52 channel_data[i] = v * (v < 0 ? kMinScale : kMaxScale);
53 }
54 }
55 }
56
57 // |Format| is the destination type, |Fixed| is a type larger than |Format|
58 // such that operations can be made without overflowing.
59 template<class Format, class Fixed>
60 static void ToInterleavedInternal(const AudioBus* source, int frames,
61 void* dst) {
62 Format* dest = static_cast<Format*>(dst);
63
64 static const Format kBias = std::numeric_limits<Format>::is_signed ? 0 :
65 std::numeric_limits<Format>::max() / 2 + 1;
66 static const Fixed kMaxValue = kBias ? kBias - 1 :
67 std::numeric_limits<Format>::max();
68 static const Fixed kMinValue = kBias ? -kBias :
69 std::numeric_limits<Format>::min();
70
71 int channels = source->channels();
72 for (int ch = 0; ch < channels; ++ch) {
73 const float* channel_data = source->channel(ch);
74 for (int i = 0, offset = ch; i < frames; ++i, offset += channels) {
75 float v = channel_data[i];
76 Fixed sample = v * (v < 0 ? -kMinValue : kMaxValue);
77
78 if (sample > kMaxValue)
79 sample = kMaxValue;
80 else if (sample < kMinValue)
81 sample = kMinValue;
82
83 dest[offset] = static_cast<Format>(sample) + kBias;
84 }
85 }
86 }
87
88 static void ValidateConfig(int channels, int frames) {
24 CHECK_GT(frames, 0); 89 CHECK_GT(frames, 0);
25 CHECK_LE(frames, limits::kMaxSamplesPerPacket); 90 CHECK_LE(frames, limits::kMaxSamplesPerPacket);
26 CHECK_GT(channels, 0); 91 CHECK_GT(channels, 0);
27 CHECK_LE(channels, limits::kMaxChannels); 92 CHECK_LE(channels, limits::kMaxChannels);
28 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels, 93 DCHECK_LT(limits::kMaxSamplesPerPacket * limits::kMaxChannels,
29 std::numeric_limits<int>::max()); 94 std::numeric_limits<int>::max());
95 }
30 96
31 // Choose a size such that each channel is aligned by kChannelAlignment. 97 AudioBus::AudioBus(int channels, int frames)
32 int aligned_frames = 98 : frames_(frames) {
33 (frames_ + kChannelAlignment - 1) & ~(kChannelAlignment - 1); 99 ValidateConfig(channels, frames_);
34 data_size_ = sizeof(float) * channels * aligned_frames; 100
101 int aligned_frames = 0;
102 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames);
35 103
36 data_.reset(static_cast<float*>(base::AlignedAlloc( 104 data_.reset(static_cast<float*>(base::AlignedAlloc(
37 data_size_, kChannelAlignment))); 105 size, AudioBus::kChannelAlignment)));
38 106
39 // Separate audio data out into channels for easy lookup later. 107 BuildChannelData(channels, aligned_frames, data_.get());
40 channel_data_.reserve(channels); 108 }
41 for (int i = 0; i < channels; ++i) 109
42 channel_data_.push_back(data_.get() + i * aligned_frames); 110 AudioBus::AudioBus(int channels, int frames, float* data)
111 : frames_(frames) {
112 ValidateConfig(channels, frames_);
113
114 int aligned_frames = 0;
115 CalculateMemorySizeInternal(channels, frames, &aligned_frames);
116
117 BuildChannelData(channels, aligned_frames, data);
43 } 118 }
44 119
45 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) 120 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data)
46 : data_size_(0), 121 : channel_data_(channel_data),
47 channel_data_(channel_data),
48 frames_(frames) { 122 frames_(frames) {
123 ValidateConfig(channel_data_.size(), frames_);
124
49 // Sanity check wrapped vector for alignment and channel count. 125 // Sanity check wrapped vector for alignment and channel count.
50 for (size_t i = 0; i < channel_data_.size(); ++i) 126 for (size_t i = 0; i < channel_data_.size(); ++i)
51 DCHECK(IsAligned(channel_data_[i])); 127 DCHECK(IsAligned(channel_data_[i]));
52 } 128 }
53 129
54 AudioBus::~AudioBus() {} 130 AudioBus::~AudioBus() {}
55 131
56 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) { 132 scoped_ptr<AudioBus> AudioBus::Create(int channels, int frames) {
57 return scoped_ptr<AudioBus>(new AudioBus(channels, frames)); 133 return scoped_ptr<AudioBus>(new AudioBus(channels, frames));
58 } 134 }
59 135
60 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) { 136 scoped_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) {
61 return scoped_ptr<AudioBus>(new AudioBus( 137 return scoped_ptr<AudioBus>(new AudioBus(
62 params.channels(), params.frames_per_buffer())); 138 params.channels(), params.frames_per_buffer()));
63 } 139 }
64 140
65 scoped_ptr<AudioBus> AudioBus::WrapVector( 141 scoped_ptr<AudioBus> AudioBus::WrapVector(
66 int frames, const std::vector<float*>& channel_data) { 142 int frames, const std::vector<float*>& channel_data) {
67 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data)); 143 return scoped_ptr<AudioBus>(new AudioBus(frames, channel_data));
68 } 144 }
69 145
70 void* AudioBus::data() { 146 scoped_ptr<AudioBus> AudioBus::WrapMemory(int channels, int frames,
DaleCurtis 2012/08/20 22:26:21 Ended up needing this for PPAPI since they don't h
71 DCHECK(data_.get()); 147 void* data) {
72 return data_.get(); 148 // |data| must be aligned by AudioBus::kChannelAlignment.
149 CHECK(IsAligned(data));
150 return scoped_ptr<AudioBus>(new AudioBus(
151 channels, frames, static_cast<float*>(data)));
73 } 152 }
74 153
75 int AudioBus::data_size() const { 154 scoped_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params,
76 DCHECK(data_.get()); 155 void* data) {
77 return data_size_; 156 // |data| must be aligned by AudioBus::kChannelAlignment.
157 CHECK(IsAligned(data));
158 return scoped_ptr<AudioBus>(new AudioBus(
159 params.channels(), params.frames_per_buffer(),
160 static_cast<float*>(data)));
78 } 161 }
79 162
80 void AudioBus::ZeroFrames(int frames) { 163 void AudioBus::ZeroFrames(int frames) {
81 DCHECK_LE(frames, frames_); 164 DCHECK_LE(frames, frames_);
82 for (size_t i = 0; i < channel_data_.size(); ++i) 165 for (size_t i = 0; i < channel_data_.size(); ++i)
83 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i])); 166 memset(channel_data_[i], 0, frames * sizeof(*channel_data_[i]));
84 } 167 }
85 168
86 void AudioBus::Zero() { 169 void AudioBus::Zero() {
87 ZeroFrames(frames_); 170 ZeroFrames(frames_);
88 } 171 }
89 172
173 int AudioBus::CalculateMemorySize(const AudioParameters& params) {
174 int aligned_frames = 0;
175 return CalculateMemorySizeInternal(
176 params.channels(), params.frames_per_buffer(), &aligned_frames);
177 }
178
179 void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) {
180 DCHECK(IsAligned(data));
181 DCHECK_EQ(channel_data_.size(), 0U);
182 // Separate audio data out into channels for easy lookup later. Figure out
183 channel_data_.reserve(channels);
184 for (int i = 0; i < channels; ++i)
185 channel_data_.push_back(data + i * aligned_frames);
186 }
187
188 // TODO(dalecurtis): See if intrinsic optimizations help any here.
189 void AudioBus::FromInterleaved(const void* source, int frames,
190 int bytes_per_sample) {
191 DCHECK_LE(frames, frames_);
192 switch (bytes_per_sample) {
193 case 1:
194 FromInterleavedInternal<uint8, int16>(source, frames, this);
195 break;
196 case 2:
197 FromInterleavedInternal<int16, int32>(source, frames, this);
198 break;
199 case 4:
200 FromInterleavedInternal<int32, int64>(source, frames, this);
201 break;
202 default:
203 NOTREACHED() << "Unsupported bytes per sample encountered.";
204 Zero();
205 return;
206 }
207
208 // Zero any remaining frames.
209 int remaining_frames = (frames_ - frames);
210 if (remaining_frames) {
211 for (int ch = 0; ch < channels(); ++ch)
212 memset(channel(ch) + frames, 0, sizeof(*channel(ch)) * remaining_frames);
213 }
214 }
215
216 // TODO(dalecurtis): See if intrinsic optimizations help any here.
217 void AudioBus::ToInterleaved(int frames, int bytes_per_sample,
218 void* dest) const {
219 DCHECK_LE(frames, frames_);
220 switch (bytes_per_sample) {
221 case 1:
222 ToInterleavedInternal<uint8, int16>(this, frames, dest);
223 break;
224 case 2:
225 ToInterleavedInternal<int16, int32>(this, frames, dest);
226 break;
227 case 4:
228 ToInterleavedInternal<int32, int64>(this, frames, dest);
229 break;
230 default:
231 NOTREACHED() << "Unsupported bytes per sample encountered.";
232 memset(dest, 0, frames * bytes_per_sample);
233 return;
234 }
235 }
236
237 void AudioBus::CopyTo(AudioBus* dest) const {
238 DCHECK_EQ(channels(), dest->channels());
239 DCHECK_EQ(frames(), dest->frames());
240
241 // 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.
243 for (int i = 0; i < channels(); ++i)
244 memcpy(dest->channel(i), channel(i), sizeof(*channel(i)) * frames());
245 }
246
90 } // namespace media 247 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698