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

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

Powered by Google App Engine
This is Rietveld 408576698