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

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

Issue 2466463005: Support (E)AC3 passthrough
Patch Set: Add unit tests Created 3 years, 6 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
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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 void AudioBus::CheckOverflow(int start_frame, int frames, int total_frames) { 55 void AudioBus::CheckOverflow(int start_frame, int frames, int total_frames) {
56 CHECK_GE(start_frame, 0); 56 CHECK_GE(start_frame, 0);
57 CHECK_GE(frames, 0); 57 CHECK_GE(frames, 0);
58 CHECK_GT(total_frames, 0); 58 CHECK_GT(total_frames, 0);
59 int sum = start_frame + frames; 59 int sum = start_frame + frames;
60 CHECK_LE(sum, total_frames); 60 CHECK_LE(sum, total_frames);
61 CHECK_GE(sum, 0); 61 CHECK_GE(sum, 0);
62 } 62 }
63 63
64 AudioBus::AudioBus(int channels, int frames) 64 AudioBus::AudioBus(int channels, int frames)
65 : frames_(frames), 65 : data_size_(0),
66 is_bitstream_format_(false),
67 frames_(frames),
66 can_set_channel_data_(false) { 68 can_set_channel_data_(false) {
67 ValidateConfig(channels, frames_); 69 ValidateConfig(channels, frames_);
68 70
69 int aligned_frames = 0; 71 int aligned_frames = 0;
70 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames); 72 int size = CalculateMemorySizeInternal(channels, frames, &aligned_frames);
71 73
72 data_.reset(static_cast<float*>(base::AlignedAlloc( 74 data_.reset(static_cast<float*>(base::AlignedAlloc(
73 size, AudioBus::kChannelAlignment))); 75 size, AudioBus::kChannelAlignment)));
74 76
75 BuildChannelData(channels, aligned_frames, data_.get()); 77 BuildChannelData(channels, aligned_frames, data_.get());
76 } 78 }
77 79
78 AudioBus::AudioBus(int channels, int frames, float* data) 80 AudioBus::AudioBus(int channels, int frames, float* data)
79 : frames_(frames), 81 : data_size_(0),
82 is_bitstream_format_(false),
83 frames_(frames),
80 can_set_channel_data_(false) { 84 can_set_channel_data_(false) {
81 // Since |data| may have come from an external source, ensure it's valid. 85 // Since |data| may have come from an external source, ensure it's valid.
82 CHECK(data); 86 CHECK(data);
83 ValidateConfig(channels, frames_); 87 ValidateConfig(channels, frames_);
84 88
85 int aligned_frames = 0; 89 int aligned_frames = 0;
86 CalculateMemorySizeInternal(channels, frames, &aligned_frames); 90 CalculateMemorySizeInternal(channels, frames, &aligned_frames);
87 91
88 BuildChannelData(channels, aligned_frames, data); 92 BuildChannelData(channels, aligned_frames, data);
89 } 93 }
90 94
91 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data) 95 AudioBus::AudioBus(int frames, const std::vector<float*>& channel_data)
92 : channel_data_(channel_data), 96 : data_size_(0),
97 is_bitstream_format_(false),
98 channel_data_(channel_data),
93 frames_(frames), 99 frames_(frames),
94 can_set_channel_data_(false) { 100 can_set_channel_data_(false) {
95 ValidateConfig( 101 ValidateConfig(
96 base::checked_cast<int>(channel_data_.size()), frames_); 102 base::checked_cast<int>(channel_data_.size()), frames_);
97 103
98 // Sanity check wrapped vector for alignment and channel count. 104 // Sanity check wrapped vector for alignment and channel count.
99 for (size_t i = 0; i < channel_data_.size(); ++i) 105 for (size_t i = 0; i < channel_data_.size(); ++i)
100 DCHECK(IsAligned(channel_data_[i])); 106 DCHECK(IsAligned(channel_data_[i]));
101 } 107 }
102 108
103 AudioBus::AudioBus(int channels) 109 AudioBus::AudioBus(int channels)
104 : channel_data_(channels), 110 : data_size_(0),
111 is_bitstream_format_(false),
112 channel_data_(channels),
105 frames_(0), 113 frames_(0),
106 can_set_channel_data_(true) { 114 can_set_channel_data_(true) {
107 CHECK_GT(channels, 0); 115 CHECK_GT(channels, 0);
108 for (size_t i = 0; i < channel_data_.size(); ++i) 116 for (size_t i = 0; i < channel_data_.size(); ++i)
109 channel_data_[i] = NULL; 117 channel_data_[i] = NULL;
110 } 118 }
111 119
112 AudioBus::~AudioBus() {} 120 AudioBus::~AudioBus() {}
113 121
114 std::unique_ptr<AudioBus> AudioBus::Create(int channels, int frames) { 122 std::unique_ptr<AudioBus> AudioBus::Create(int channels, int frames) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 void AudioBus::SetChannelData(int channel, float* data) { 159 void AudioBus::SetChannelData(int channel, float* data) {
152 CHECK(can_set_channel_data_); 160 CHECK(can_set_channel_data_);
153 CHECK(data); 161 CHECK(data);
154 CHECK_GE(channel, 0); 162 CHECK_GE(channel, 0);
155 CHECK_LT(static_cast<size_t>(channel), channel_data_.size()); 163 CHECK_LT(static_cast<size_t>(channel), channel_data_.size());
156 DCHECK(IsAligned(data)); 164 DCHECK(IsAligned(data));
157 channel_data_[channel] = data; 165 channel_data_[channel] = data;
158 } 166 }
159 167
160 void AudioBus::set_frames(int frames) { 168 void AudioBus::set_frames(int frames) {
161 CHECK(can_set_channel_data_); 169 if (!is_bitstream_format()) {
DaleCurtis 2017/06/15 21:46:32 Why? Just change to CHECK(can_set_channel_data_ ||
AndyWu 2017/08/02 01:43:40 Done.
162 ValidateConfig(static_cast<int>(channel_data_.size()), frames); 170 CHECK(can_set_channel_data_);
171 ValidateConfig(static_cast<int>(channel_data_.size()), frames);
172 }
163 frames_ = frames; 173 frames_ = frames;
164 } 174 }
165 175
166 void AudioBus::ZeroFramesPartial(int start_frame, int frames) { 176 void AudioBus::ZeroFramesPartial(int start_frame, int frames) {
167 CheckOverflow(start_frame, frames, frames_); 177 CheckOverflow(start_frame, frames, frames_);
168 178
169 if (frames <= 0) 179 if (frames <= 0)
170 return; 180 return;
171 181
172 for (size_t i = 0; i < channel_data_.size(); ++i) { 182 for (size_t i = 0; i < channel_data_.size(); ++i) {
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 ToInterleavedPartial<SignedInt32SampleTypeTraits>( 311 ToInterleavedPartial<SignedInt32SampleTypeTraits>(
302 start_frame, frames, reinterpret_cast<int32_t*>(dest)); 312 start_frame, frames, reinterpret_cast<int32_t*>(dest));
303 break; 313 break;
304 default: 314 default:
305 NOTREACHED() << "Unsupported bytes per sample encountered: " 315 NOTREACHED() << "Unsupported bytes per sample encountered: "
306 << bytes_per_sample; 316 << bytes_per_sample;
307 } 317 }
308 } 318 }
309 319
310 void AudioBus::CopyTo(AudioBus* dest) const { 320 void AudioBus::CopyTo(AudioBus* dest) const {
321 dest->set_is_bitstream_format(is_bitstream_format());
DaleCurtis 2017/06/15 21:46:32 Move inside? I don't think we need to worry about
AndyWu 2017/08/02 01:43:40 Not sure it's a good idea to assume the state of a
322 if (is_bitstream_format()) {
323 dest->set_data_size(data_size());
324 dest->set_frames(frames());
325 memcpy(dest->channel(0), channel(0), data_size());
326 return;
327 }
328
311 CopyPartialFramesTo(0, frames(), 0, dest); 329 CopyPartialFramesTo(0, frames(), 0, dest);
312 } 330 }
313 331
314 void AudioBus::CopyPartialFramesTo(int source_start_frame, 332 void AudioBus::CopyPartialFramesTo(int source_start_frame,
315 int frame_count, 333 int frame_count,
316 int dest_start_frame, 334 int dest_start_frame,
317 AudioBus* dest) const { 335 AudioBus* dest) const {
318 CHECK_EQ(channels(), dest->channels()); 336 CHECK_EQ(channels(), dest->channels());
319 CHECK_LE(source_start_frame + frame_count, frames()); 337 CHECK_LE(source_start_frame + frame_count, frames());
320 CHECK_LE(dest_start_frame + frame_count, dest->frames()); 338 CHECK_LE(dest_start_frame + frame_count, dest->frames());
(...skipping 17 matching lines...) Expand all
338 } 356 }
339 357
340 void AudioBus::SwapChannels(int a, int b) { 358 void AudioBus::SwapChannels(int a, int b) {
341 DCHECK(a < channels() && a >= 0); 359 DCHECK(a < channels() && a >= 0);
342 DCHECK(b < channels() && b >= 0); 360 DCHECK(b < channels() && b >= 0);
343 DCHECK_NE(a, b); 361 DCHECK_NE(a, b);
344 std::swap(channel_data_[a], channel_data_[b]); 362 std::swap(channel_data_[a], channel_data_[b]);
345 } 363 }
346 364
347 } // namespace media 365 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698