OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_buffer.h" | 5 #include "media/base/audio_buffer.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "media/base/audio_bus.h" | 10 #include "media/base/audio_bus.h" |
11 #include "media/base/limits.h" | 11 #include "media/base/limits.h" |
12 #include "media/base/timestamp_constants.h" | 12 #include "media/base/timestamp_constants.h" |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 | 15 |
16 static base::TimeDelta CalculateDuration(int frames, double sample_rate) { | 16 static base::TimeDelta CalculateDuration(int frames, double sample_rate) { |
17 DCHECK_GT(sample_rate, 0); | 17 DCHECK_GT(sample_rate, 0); |
18 return base::TimeDelta::FromMicroseconds( | 18 return base::TimeDelta::FromMicroseconds( |
19 frames * base::Time::kMicrosecondsPerSecond / sample_rate); | 19 frames * base::Time::kMicrosecondsPerSecond / sample_rate); |
20 } | 20 } |
21 | 21 |
22 AudioBuffer::AudioBuffer(SampleFormat sample_format, | 22 AudioBuffer::AudioBuffer(SampleFormat sample_format, |
23 ChannelLayout channel_layout, | 23 ChannelLayout channel_layout, |
24 int channel_count, | 24 int channel_count, |
25 int sample_rate, | 25 int sample_rate, |
26 int frame_count, | 26 int frame_count, |
27 bool create_buffer, | 27 bool create_buffer, |
28 const uint8_t* const* data, | 28 const uint8_t* const* data, |
| 29 size_t data_size, |
29 const base::TimeDelta timestamp) | 30 const base::TimeDelta timestamp) |
30 : sample_format_(sample_format), | 31 : sample_format_(sample_format), |
31 channel_layout_(channel_layout), | 32 channel_layout_(channel_layout), |
32 channel_count_(channel_count), | 33 channel_count_(channel_count), |
33 sample_rate_(sample_rate), | 34 sample_rate_(sample_rate), |
34 adjusted_frame_count_(frame_count), | 35 adjusted_frame_count_(frame_count), |
35 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), | 36 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), |
36 timestamp_(timestamp), | 37 timestamp_(timestamp), |
37 duration_(end_of_stream_ | 38 duration_(end_of_stream_ |
38 ? base::TimeDelta() | 39 ? base::TimeDelta() |
39 : CalculateDuration(adjusted_frame_count_, sample_rate_)), | 40 : CalculateDuration(adjusted_frame_count_, sample_rate_)), |
40 data_size_(0) { | 41 data_size_(data_size) { |
41 CHECK_GE(channel_count_, 0); | 42 CHECK_GE(channel_count_, 0); |
42 CHECK_LE(channel_count_, limits::kMaxChannels); | 43 CHECK_LE(channel_count_, limits::kMaxChannels); |
43 CHECK_GE(frame_count, 0); | 44 CHECK_GE(frame_count, 0); |
44 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || | 45 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || |
45 ChannelLayoutToChannelCount(channel_layout) == channel_count); | 46 ChannelLayoutToChannelCount(channel_layout) == channel_count); |
46 | 47 |
47 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); | 48 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); |
48 DCHECK_LE(bytes_per_channel, kChannelAlignment); | 49 DCHECK_LE(bytes_per_channel, kChannelAlignment); |
49 | 50 |
50 // Empty buffer? | 51 // Empty buffer? |
(...skipping 21 matching lines...) Expand all Loading... |
72 if (data) | 73 if (data) |
73 memcpy(channel_data_[i], data[i], data_size_per_channel); | 74 memcpy(channel_data_[i], data[i], data_size_per_channel); |
74 } | 75 } |
75 return; | 76 return; |
76 } | 77 } |
77 | 78 |
78 // Remaining formats are interleaved data. | 79 // Remaining formats are interleaved data. |
79 DCHECK(IsInterleaved(sample_format)) << sample_format_; | 80 DCHECK(IsInterleaved(sample_format)) << sample_format_; |
80 // Allocate our own buffer and copy the supplied data into it. Buffer must | 81 // Allocate our own buffer and copy the supplied data into it. Buffer must |
81 // contain the data for all channels. | 82 // contain the data for all channels. |
82 data_size_ = data_size_per_channel * channel_count_; | 83 if (sample_format != kSampleFormatRaw) |
| 84 data_size_ = data_size_per_channel * channel_count_; |
83 data_.reset( | 85 data_.reset( |
84 static_cast<uint8_t*>(base::AlignedAlloc(data_size_, kChannelAlignment))); | 86 static_cast<uint8_t*>(base::AlignedAlloc(data_size_, kChannelAlignment))); |
85 channel_data_.reserve(1); | 87 channel_data_.reserve(1); |
86 channel_data_.push_back(data_.get()); | 88 channel_data_.push_back(data_.get()); |
87 if (data) | 89 if (data) |
88 memcpy(data_.get(), data[0], data_size_); | 90 memcpy(data_.get(), data[0], data_size_); |
89 } | 91 } |
90 | 92 |
91 AudioBuffer::~AudioBuffer() {} | 93 AudioBuffer::~AudioBuffer() {} |
92 | 94 |
93 // static | 95 // static |
94 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( | 96 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( |
95 SampleFormat sample_format, | 97 SampleFormat sample_format, |
96 ChannelLayout channel_layout, | 98 ChannelLayout channel_layout, |
97 int channel_count, | 99 int channel_count, |
98 int sample_rate, | 100 int sample_rate, |
99 int frame_count, | 101 int frame_count, |
100 const uint8_t* const* data, | 102 const uint8_t* const* data, |
101 const base::TimeDelta timestamp) { | 103 const base::TimeDelta timestamp, |
| 104 const size_t data_size) { |
102 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it. | 105 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it. |
103 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 106 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
104 CHECK(data[0]); | 107 CHECK(data[0]); |
105 return make_scoped_refptr(new AudioBuffer(sample_format, | 108 return make_scoped_refptr( |
106 channel_layout, | 109 new AudioBuffer(sample_format, channel_layout, channel_count, sample_rate, |
107 channel_count, | 110 frame_count, true, data, data_size, timestamp)); |
108 sample_rate, | |
109 frame_count, | |
110 true, | |
111 data, | |
112 timestamp)); | |
113 } | 111 } |
114 | 112 |
115 // static | 113 // static |
116 scoped_refptr<AudioBuffer> AudioBuffer::CreateBuffer( | 114 scoped_refptr<AudioBuffer> AudioBuffer::CreateBuffer( |
117 SampleFormat sample_format, | 115 SampleFormat sample_format, |
118 ChannelLayout channel_layout, | 116 ChannelLayout channel_layout, |
119 int channel_count, | 117 int channel_count, |
120 int sample_rate, | 118 int sample_rate, |
121 int frame_count) { | 119 int frame_count, |
| 120 size_t data_size) { |
122 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 121 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
123 return make_scoped_refptr( | 122 return make_scoped_refptr( |
124 new AudioBuffer(sample_format, channel_layout, channel_count, sample_rate, | 123 new AudioBuffer(sample_format, channel_layout, channel_count, sample_rate, |
125 frame_count, true, NULL, kNoTimestamp)); | 124 frame_count, true, NULL, data_size, kNoTimestamp)); |
126 } | 125 } |
127 | 126 |
128 // static | 127 // static |
129 scoped_refptr<AudioBuffer> AudioBuffer::CreateEmptyBuffer( | 128 scoped_refptr<AudioBuffer> AudioBuffer::CreateEmptyBuffer( |
130 ChannelLayout channel_layout, | 129 ChannelLayout channel_layout, |
131 int channel_count, | 130 int channel_count, |
132 int sample_rate, | 131 int sample_rate, |
133 int frame_count, | 132 int frame_count, |
134 const base::TimeDelta timestamp) { | 133 const base::TimeDelta timestamp) { |
135 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 134 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
136 // Since data == NULL, format doesn't matter. | 135 // Since data == NULL, format doesn't matter. |
137 return make_scoped_refptr(new AudioBuffer(kSampleFormatF32, | 136 return make_scoped_refptr( |
138 channel_layout, | 137 new AudioBuffer(kSampleFormatF32, channel_layout, channel_count, |
139 channel_count, | 138 sample_rate, frame_count, false, NULL, 0, timestamp)); |
140 sample_rate, | |
141 frame_count, | |
142 false, | |
143 NULL, | |
144 timestamp)); | |
145 } | 139 } |
146 | 140 |
147 // static | 141 // static |
148 scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() { | 142 scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() { |
149 return make_scoped_refptr(new AudioBuffer(kUnknownSampleFormat, | 143 return make_scoped_refptr(new AudioBuffer(kUnknownSampleFormat, |
150 CHANNEL_LAYOUT_NONE, 0, 0, 0, false, | 144 CHANNEL_LAYOUT_NONE, 0, 0, 0, false, |
151 NULL, kNoTimestamp)); | 145 NULL, 0, kNoTimestamp)); |
152 } | 146 } |
153 | 147 |
154 // Convert int16_t values in the range [INT16_MIN, INT16_MAX] to [-1.0, 1.0]. | 148 // Convert int16_t values in the range [INT16_MIN, INT16_MAX] to [-1.0, 1.0]. |
155 inline float ConvertSample(int16_t value) { | 149 inline float ConvertSample(int16_t value) { |
156 return value * (value < 0 ? -1.0f / std::numeric_limits<int16_t>::min() | 150 return value * (value < 0 ? -1.0f / std::numeric_limits<int16_t>::min() |
157 : 1.0f / std::numeric_limits<int16_t>::max()); | 151 : 1.0f / std::numeric_limits<int16_t>::max()); |
158 } | 152 } |
159 | 153 |
160 void AudioBuffer::AdjustSampleRate(int sample_rate) { | 154 void AudioBuffer::AdjustSampleRate(int sample_rate) { |
161 DCHECK(!end_of_stream_); | 155 DCHECK(!end_of_stream_); |
162 sample_rate_ = sample_rate; | 156 sample_rate_ = sample_rate; |
163 duration_ = CalculateDuration(adjusted_frame_count_, sample_rate_); | 157 duration_ = CalculateDuration(adjusted_frame_count_, sample_rate_); |
164 } | 158 } |
165 | 159 |
166 void AudioBuffer::ReadFrames(int frames_to_copy, | 160 void AudioBuffer::ReadFrames(int frames_to_copy, |
167 int source_frame_offset, | 161 int source_frame_offset, |
168 int dest_frame_offset, | 162 int dest_frame_offset, |
169 AudioBus* dest) { | 163 AudioBus* dest) { |
170 // Deinterleave each channel (if necessary) and convert to 32bit | 164 // Deinterleave each channel (if necessary) and convert to 32bit |
171 // floating-point with nominal range -1.0 -> +1.0 (if necessary). | 165 // floating-point with nominal range -1.0 -> +1.0 (if necessary). |
172 | 166 |
173 // |dest| must have the same number of channels, and the number of frames | 167 // |dest| must have the same number of channels, and the number of frames |
174 // specified must be in range. | 168 // specified must be in range. |
175 DCHECK(!end_of_stream()); | 169 DCHECK(!end_of_stream()); |
176 DCHECK_EQ(dest->channels(), channel_count_); | 170 DCHECK_EQ(dest->channels(), channel_count_); |
177 DCHECK_LE(source_frame_offset + frames_to_copy, adjusted_frame_count_); | 171 DCHECK_LE(source_frame_offset + frames_to_copy, adjusted_frame_count_); |
| 172 |
| 173 bool is_raw_format = (sample_format_ == kSampleFormatRaw); |
| 174 dest->set_is_raw_format(is_raw_format); |
| 175 |
| 176 if (is_raw_format) { |
| 177 DCHECK(!source_frame_offset); |
| 178 uint8_t* dest_data = |
| 179 reinterpret_cast<uint8_t*>(dest->channel(0)) + dest->data_size(); |
| 180 |
| 181 memcpy(dest_data, channel_data_[0], data_size()); |
| 182 dest->set_data_size(dest_frame_offset + data_size()); |
| 183 dest->set_frames(dest->frames() + frame_count()); |
| 184 return; |
| 185 } |
| 186 |
178 DCHECK_LE(dest_frame_offset + frames_to_copy, dest->frames()); | 187 DCHECK_LE(dest_frame_offset + frames_to_copy, dest->frames()); |
179 | 188 |
180 if (!data_) { | 189 if (!data_) { |
181 // Special case for an empty buffer. | 190 // Special case for an empty buffer. |
182 dest->ZeroFramesPartial(dest_frame_offset, frames_to_copy); | 191 dest->ZeroFramesPartial(dest_frame_offset, frames_to_copy); |
183 return; | 192 return; |
184 } | 193 } |
185 | 194 |
186 if (sample_format_ == kSampleFormatPlanarF32) { | 195 if (sample_format_ == kSampleFormatPlanarF32) { |
187 // Format is planar float32. Copy the data from each channel as a block. | 196 // Format is planar float32. Copy the data from each channel as a block. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 case kSampleFormatS24: | 289 case kSampleFormatS24: |
281 case kSampleFormatS32: | 290 case kSampleFormatS32: |
282 case kSampleFormatF32: { | 291 case kSampleFormatF32: { |
283 // Interleaved data can be shifted all at once. | 292 // Interleaved data can be shifted all at once. |
284 const int frame_size = channel_count_ * bytes_per_channel; | 293 const int frame_size = channel_count_ * bytes_per_channel; |
285 memmove(channel_data_[0] + start * frame_size, | 294 memmove(channel_data_[0] + start * frame_size, |
286 channel_data_[0] + end * frame_size, | 295 channel_data_[0] + end * frame_size, |
287 frame_size * frames_to_copy); | 296 frame_size * frames_to_copy); |
288 break; | 297 break; |
289 } | 298 } |
| 299 case kSampleFormatRaw: |
290 case kUnknownSampleFormat: | 300 case kUnknownSampleFormat: |
291 NOTREACHED() << "Invalid sample format!"; | 301 NOTREACHED() << "Invalid sample format!"; |
292 } | 302 } |
293 } else { | 303 } else { |
294 CHECK_EQ(frames_to_copy, 0); | 304 CHECK_EQ(frames_to_copy, 0); |
295 } | 305 } |
296 | 306 |
297 // Trim the leftover data off the end of the buffer and update duration. | 307 // Trim the leftover data off the end of the buffer and update duration. |
298 TrimEnd(frames_to_trim); | 308 TrimEnd(frames_to_trim); |
299 } | 309 } |
300 | 310 |
301 } // namespace media | 311 } // namespace media |
OLD | NEW |