1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
media / base / audio_bus.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "media/base/audio_bus.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <limits>
#include <utility>
#include "base/bits.h"
#include "base/check_op.h"
#include "base/memory/aligned_memory.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "media/base/audio_parameters.h"
#include "media/base/limits.h"
#include "media/base/vector_math.h"
namespace media {
// Returns `frames` rounded up to the nearest number which allows full rows of
// SIMD instructions.
static size_t AlignFramesUp(size_t frames) {
// Since our internal sample format is float, we can guarantee the alignment
// by making the number of frames an integer multiple of
// AudioBus::kChannelAlignment / sizeof(float).
return base::bits::AlignUp(frames * sizeof(float),
AudioBus::kChannelAlignment) /
sizeof(float);
}
// In order to guarantee that the memory block for each channel starts at an
// aligned address when splitting a contiguous block of memory into one block
// per channel, we may have to make these blocks larger than otherwise needed.
// We do this by allocating space for potentially more frames than requested.
// This method returns the required size for the contiguous memory block
// in bytes and outputs the adjusted number of frames via |out_aligned_frames|.
static size_t CalculateMemorySizeInternal(int channels, size_t frames) {
return sizeof(float) * channels * AlignFramesUp(frames);
}
static bool IsValidChannelCount(int channels) {
CHECK_GT(channels, 0);
return base::checked_cast<size_t>(channels) <=
static_cast<size_t>(limits::kMaxChannels);
}
void AudioBus::CheckOverflow(int start_frame, int frames, int total_frames) {
CHECK_GE(start_frame, 0);
CHECK_GE(frames, 0);
CHECK_GT(total_frames, 0);
int sum = start_frame + frames;
CHECK_LE(sum, total_frames);
CHECK_GE(sum, 0);
}
AudioBus::AudioBus(int channels, int frames)
: frames_(base::checked_cast<size_t>(frames)), is_wrapper_(false) {
CHECK(IsValidChannelCount(channels));
size_t size = CalculateMemorySizeInternal(channels, frames_);
data_ = base::AlignedUninit<float>(size, AudioBus::kChannelAlignment);
BuildChannelData(channels, data_);
}
AudioBus::AudioBus(int channels, int frames, float* data)
: frames_(base::checked_cast<size_t>(frames)), is_wrapper_(false) {
CHECK(IsValidChannelCount(channels));
// Since |data| may have come from an external source, ensure it's valid.
CHECK(data);
// `data` must be at least CalculateMemorySizeInternal(), per interface
// contract.
BuildChannelData(channels, base::span(data, CalculateMemorySizeInternal(
channels, frames_)));
}
AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data)
: frames_(base::checked_cast<size_t>(frames)), is_wrapper_(false) {
channel_data_.reserve(channel_data.size());
for (float* data : channel_data) {
CHECK(IsAligned(data));
channel_data_.emplace_back(data, frames_);
}
}
AudioBus::AudioBus(int channels)
: channel_data_(channels), frames_(0U), is_wrapper_(true) {
CHECK(IsValidChannelCount(channels));
}
AudioBus::~AudioBus() {
if (wrapped_data_deleter_cb_) {
std::move(wrapped_data_deleter_cb_).Run();
}
}
std::unique_ptr<AudioBus> AudioBus::Create(int channels, int frames) {
return base::WrapUnique(new AudioBus(channels, frames));
}
std::unique_ptr<AudioBus> AudioBus::Create(const AudioParameters& params) {
return base::WrapUnique(
new AudioBus(params.channels(), params.frames_per_buffer()));
}
std::unique_ptr<AudioBus> AudioBus::CreateWrapper(int channels) {
return base::WrapUnique(new AudioBus(channels));
}
std::unique_ptr<AudioBus> AudioBus::WrapVector(
int frames,
const std::vector<float*>& channel_data) {
return base::WrapUnique(new AudioBus(frames, channel_data));
}
std::unique_ptr<AudioBus> AudioBus::WrapMemory(int channels,
int frames,
void* data) {
// |data| must be aligned by AudioBus::kChannelAlignment.
CHECK(IsAligned(data));
return base::WrapUnique(
new AudioBus(channels, frames, static_cast<float*>(data)));
}
std::unique_ptr<AudioBus> AudioBus::WrapMemory(const AudioParameters& params,
void* data) {
// |data| must be aligned by AudioBus::kChannelAlignment.
CHECK(IsAligned(data));
return base::WrapUnique(new AudioBus(params.channels(),
params.frames_per_buffer(),
static_cast<float*>(data)));
}
std::unique_ptr<const AudioBus> AudioBus::WrapReadOnlyMemory(int channels,
int frames,
const void* data) {
// Note: const_cast is generally dangerous but is used in this case since
// AudioBus accommodates both read-only and read/write use cases. A const
// AudioBus object is returned to ensure no one accidentally writes to the
// read-only data.
return WrapMemory(channels, frames, const_cast<void*>(data));
}
std::unique_ptr<const AudioBus> AudioBus::WrapReadOnlyMemory(
const AudioParameters& params,
const void* data) {
// Note: const_cast is generally dangerous but is used in this case since
// AudioBus accommodates both read-only and read/write use cases. A const
// AudioBus object is returned to ensure no one accidentally writes to the
// read-only data.
return WrapMemory(params, const_cast<void*>(data));
}
void AudioBus::SetChannelData(int channel, Channel data) {
CHECK(is_wrapper_);
// Make sure `set_frames()` was called first.
CHECK(frames_);
CHECK_EQ(data.size(), frames_);
CHECK_GE(channel, 0);
CHECK_LT(static_cast<size_t>(channel), channel_data_.size());
CHECK(IsAligned(data));
channel_data_[channel] = data;
}
void AudioBus::SetAllChannels(const ChannelVector& channels) {
CHECK(is_wrapper_);
// Make sure `set_frames()` was called first.
CHECK(frames_);
CHECK(!channels.empty());
CHECK_EQ(channels.size(), channel_data_.size());
for (auto channel : channels) {
CHECK(IsAligned(channel));
CHECK_EQ(channel.size(), frames_);
}
channel_data_ = channels;
}
void AudioBus::set_frames(int frames) {
CHECK(is_wrapper_);
frames_ = base::checked_cast<size_t>(frames);
}
void AudioBus::SetWrappedDataDeleter(base::OnceClosure deleter) {
CHECK(is_wrapper_);
DCHECK(!wrapped_data_deleter_cb_);
wrapped_data_deleter_cb_ = std::move(deleter);
}
size_t AudioBus::GetBitstreamDataSize() const {
DCHECK(is_bitstream_format_);
return bitstream_data_size_;
}
void AudioBus::SetBitstreamDataSize(size_t data_size) {
DCHECK(is_bitstream_format_);
bitstream_data_size_ = data_size;
}
int AudioBus::GetBitstreamFrames() const {
DCHECK(is_bitstream_format_);
return bitstream_frames_;
}
void AudioBus::SetBitstreamFrames(int frames) {
DCHECK(is_bitstream_format_);
bitstream_frames_ = frames;
}
void AudioBus::ZeroFramesPartial(int start, int count) {
// TODO(crbug.com/373960632): Update the parameters to be `size_t`.
size_t start_frame = base::checked_cast<size_t>(start);
size_t frames = base::checked_cast<size_t>(count);
CheckOverflow(start_frame, frames, frames_);
if (!frames) {
// Nothing to do.
return;
}
if (is_bitstream_format_) {
const size_t bitstream_frames =
base::checked_cast<size_t>(bitstream_frames_);
// No need to clean unused region for bitstream formats.
if (start_frame >= bitstream_frames) {
return;
}
// Cannot clean partial frames.
DCHECK_EQ(start_frame, 0U);
DCHECK(frames >= bitstream_frames);
// For compressed bitstream, zeroed buffer is not valid and would be
// discarded immediately. It is faster and makes more sense to reset
// |bitstream_data_size_| and |is_bitstream_format_| so that the buffer
// contains no data instead of zeroed data.
SetBitstreamDataSize(0);
SetBitstreamFrames(0);
return;
}
for (auto channel : channel_data_) {
std::ranges::fill(channel.subspan(start_frame, frames), 0);
}
}
void AudioBus::ZeroFrames(int frames) {
ZeroFramesPartial(0, frames);
}
void AudioBus::Zero() {
ZeroFrames(frames_);
}
bool AudioBus::AreFramesZero() const {
DCHECK(!is_bitstream_format_);
for (Channel channel : channel_data_) {
if (std::ranges::any_of(channel, [](float frame) { return frame != 0; })) {
return false;
}
}
return true;
}
// static
int AudioBus::CalculateMemorySize(const AudioParameters& params) {
return base::checked_cast<int>(CalculateMemorySizeInternal(
params.channels(),
base::checked_cast<size_t>(params.frames_per_buffer())));
}
// static
int AudioBus::CalculateMemorySize(int channels, int frames) {
return base::checked_cast<int>(CalculateMemorySizeInternal(
channels, base::checked_cast<size_t>(frames)));
}
// static
bool AudioBus::IsAligned(void* ptr) {
return base::IsAligned(ptr, kChannelAlignment);
}
// static
bool AudioBus::IsAligned(base::span<float> span) {
return IsAligned(span.data());
}
void AudioBus::BuildChannelData(int channels, base::span<float> data) {
CHECK(!is_bitstream_format_);
CHECK(frames_);
CHECK(IsValidChannelCount(channels));
CHECK_GE(data.size(), CalculateMemorySizeInternal(channels, frames_));
CHECK(IsAligned(data));
CHECK(channel_data_.empty());
// Initialize |channel_data_| with pointers into |data|.
channel_data_.reserve(channels);
const size_t frames_per_channel = AlignFramesUp(frames_);
for (int i = 0; i < channels; ++i) {
// We might have over-allocated memory for alignment purposes, but we only
// want to expose the first `frames_`.
channel_data_.push_back(data.subspan(i * frames_per_channel, frames_));
CHECK(IsAligned(channel_data_.back().data()));
}
}
void AudioBus::CopyTo(AudioBus* dest) const {
dest->set_is_bitstream_format(is_bitstream_format());
if (is_bitstream_format()) {
dest->SetBitstreamDataSize(GetBitstreamDataSize());
dest->SetBitstreamFrames(GetBitstreamFrames());
memcpy(dest->channel(0), channel(0), GetBitstreamDataSize());
return;
}
CopyPartialFramesTo(0, frames(), 0, dest);
}
void AudioBus::CopyAndClipTo(AudioBus* dest) const {
DCHECK(!is_bitstream_format_);
CHECK_EQ(channels(), dest->channels());
CHECK_LE(frames(), dest->frames());
for (int i = 0; i < channels(); ++i) {
float* dest_ptr = dest->channel(i);
const float* source_ptr = channel(i);
for (int j = 0; j < frames(); ++j)
dest_ptr[j] = Float32SampleTypeTraits::FromFloat(source_ptr[j]);
}
}
void AudioBus::CopyPartialFramesTo(int source_start_frame,
int frame_count,
int dest_start_frame,
AudioBus* dest) const {
DCHECK(!is_bitstream_format_);
CHECK_EQ(channels(), dest->channels());
CHECK_LE(source_start_frame + frame_count, frames());
CHECK_LE(dest_start_frame + frame_count, dest->frames());
// Since we don't know if the other AudioBus is wrapped or not (and we don't
// want to care), just copy using the public channel() accessors.
for (int i = 0; i < channels(); ++i) {
memcpy(dest->channel(i) + dest_start_frame,
channel(i) + source_start_frame,
sizeof(*channel(i)) * frame_count);
}
}
void AudioBus::Scale(float volume) {
DCHECK(!is_bitstream_format_);
if (volume > 0 && volume != 1) {
for (int i = 0; i < channels(); ++i)
vector_math::FMUL(channel(i), volume, frames(), channel(i));
} else if (volume == 0) {
Zero();
}
}
void AudioBus::SwapChannels(int a, int b) {
DCHECK(!is_bitstream_format_);
DCHECK(a < channels() && a >= 0);
DCHECK(b < channels() && b >= 0);
DCHECK_NE(a, b);
std::swap(channel_data_[a], channel_data_[b]);
}
const AudioBus::ChannelVector& AudioBus::AllChannels() const {
return channel_data_;
}
AudioBus::ChannelVector AudioBus::AllChannelsSubspan(size_t offset,
size_t count) const {
ChannelVector sub_channels;
for (Channel channel : channel_data_) {
sub_channels.push_back(channel.subspan(offset, count));
}
return sub_channels;
}
} // namespace media