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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/base/audio_bus.h" | 8 #include "media/base/audio_bus.h" |
9 #include "media/base/buffers.h" | 9 #include "media/base/buffers.h" |
10 #include "media/base/limits.h" | 10 #include "media/base/limits.h" |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 DCHECK(sample_format_ == kSampleFormatU8 || | 236 DCHECK(sample_format_ == kSampleFormatU8 || |
237 sample_format_ == kSampleFormatS16 || | 237 sample_format_ == kSampleFormatS16 || |
238 sample_format_ == kSampleFormatS32); | 238 sample_format_ == kSampleFormatS32); |
239 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format_); | 239 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format_); |
240 int frame_size = channel_count_ * bytes_per_channel; | 240 int frame_size = channel_count_ * bytes_per_channel; |
241 const uint8* source_data = data_.get() + source_frame_offset * frame_size; | 241 const uint8* source_data = data_.get() + source_frame_offset * frame_size; |
242 dest->FromInterleavedPartial( | 242 dest->FromInterleavedPartial( |
243 source_data, dest_frame_offset, frames_to_copy, bytes_per_channel); | 243 source_data, dest_frame_offset, frames_to_copy, bytes_per_channel); |
244 } | 244 } |
245 | 245 |
246 static inline int32 ConvertS16ToS32(int16 value) { | |
247 return static_cast<int32>(value) << 16; | |
248 } | |
249 | |
250 static inline int32 ConvertF32ToS32(float value) { | |
251 return static_cast<int32>(value < 0 | |
252 ? (-value) * std::numeric_limits<int32>::min() | |
253 : value * std::numeric_limits<int32>::max()); | |
254 } | |
255 | |
256 template <class Target, typename Converter> | |
257 void InterleaveToS32(const std::vector<uint8*>& channel_data, | |
258 size_t frames_to_copy, | |
259 int trim_start, | |
260 int32* dest_data, | |
261 Converter convert_func) { | |
262 for (size_t ch = 0; ch < channel_data.size(); ++ch) { | |
263 const Target* source_data = | |
264 reinterpret_cast<const Target*>(channel_data[ch]) + trim_start; | |
265 for (size_t i = 0, offset = ch; i < frames_to_copy; | |
266 ++i, offset += channel_data.size()) | |
DaleCurtis
2014/09/29 23:01:32
{} since the for() spans multiple lines.
pwestin(chromium)
2014/09/30 00:15:17
Done.
| |
267 dest_data[offset] = convert_func(source_data[i]); | |
268 } | |
269 } | |
270 | |
271 void AudioBuffer::ReadFramesInterleavedS32(int frames_to_copy, | |
272 int32* dest_data) { | |
273 DCHECK_LE(frames_to_copy, adjusted_frame_count_); | |
274 | |
275 switch (sample_format_) { | |
276 case kSampleFormatU8: | |
277 NOTIMPLEMENTED(); | |
278 break; | |
279 case kSampleFormatS16: | |
280 // Format is interleaved signed16. Convert each value into int32 and | |
281 // insert into output channel data. | |
282 InterleaveToS32<int16>(channel_data_, | |
283 frames_to_copy * channel_count_, | |
284 trim_start_, | |
285 dest_data, | |
286 ConvertS16ToS32); | |
287 break; | |
288 case kSampleFormatS32: { | |
289 // Format is interleaved signed32; just copy the data. | |
290 const int32* source_data = | |
291 reinterpret_cast<const int32*>(channel_data_[0]) + trim_start_; | |
292 memcpy(dest_data, | |
293 source_data, | |
294 frames_to_copy * channel_count_ * sizeof(int32)); | |
295 } break; | |
296 case kSampleFormatF32: | |
297 // Format is interleaved float. Convert each value into int32 and insert | |
298 // into output channel data. | |
299 InterleaveToS32<float>(channel_data_, | |
300 frames_to_copy * channel_count_, | |
301 trim_start_, | |
302 dest_data, | |
303 ConvertF32ToS32); | |
304 break; | |
305 case kSampleFormatPlanarS16: | |
306 // Format is planar signed 16 bit. Convert each value into int32 and | |
307 // insert into output channel data. | |
308 InterleaveToS32<int16>(channel_data_, | |
309 frames_to_copy, | |
310 trim_start_, | |
311 dest_data, | |
312 ConvertS16ToS32); | |
313 break; | |
314 case kSampleFormatPlanarF32: | |
315 // Format is planar float. Convert each value into int32 and insert into | |
316 // output channel data. | |
317 InterleaveToS32<float>(channel_data_, | |
318 frames_to_copy, | |
319 trim_start_, | |
320 dest_data, | |
321 ConvertF32ToS32); | |
322 break; | |
323 case kUnknownSampleFormat: | |
324 NOTREACHED(); | |
325 break; | |
326 } | |
327 } | |
328 | |
246 void AudioBuffer::TrimStart(int frames_to_trim) { | 329 void AudioBuffer::TrimStart(int frames_to_trim) { |
247 CHECK_GE(frames_to_trim, 0); | 330 CHECK_GE(frames_to_trim, 0); |
248 CHECK_LE(frames_to_trim, adjusted_frame_count_); | 331 CHECK_LE(frames_to_trim, adjusted_frame_count_); |
249 | 332 |
250 // Adjust the number of frames in this buffer and where the start really is. | 333 // Adjust the number of frames in this buffer and where the start really is. |
251 adjusted_frame_count_ -= frames_to_trim; | 334 adjusted_frame_count_ -= frames_to_trim; |
252 trim_start_ += frames_to_trim; | 335 trim_start_ += frames_to_trim; |
253 | 336 |
254 // Adjust timestamp_ and duration_ to reflect the smaller number of frames. | 337 // Adjust timestamp_ and duration_ to reflect the smaller number of frames. |
255 const base::TimeDelta old_duration = duration_; | 338 const base::TimeDelta old_duration = duration_; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 } | 386 } |
304 } else { | 387 } else { |
305 CHECK_EQ(frames_to_copy, 0); | 388 CHECK_EQ(frames_to_copy, 0); |
306 } | 389 } |
307 | 390 |
308 // Trim the leftover data off the end of the buffer and update duration. | 391 // Trim the leftover data off the end of the buffer and update duration. |
309 TrimEnd(frames_to_trim); | 392 TrimEnd(frames_to_trim); |
310 } | 393 } |
311 | 394 |
312 } // namespace media | 395 } // namespace media |
OLD | NEW |