| 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" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 : sample_format_(sample_format), | 30 : sample_format_(sample_format), |
| 31 channel_layout_(channel_layout), | 31 channel_layout_(channel_layout), |
| 32 channel_count_(channel_count), | 32 channel_count_(channel_count), |
| 33 sample_rate_(sample_rate), | 33 sample_rate_(sample_rate), |
| 34 adjusted_frame_count_(frame_count), | 34 adjusted_frame_count_(frame_count), |
| 35 trim_start_(0), | 35 trim_start_(0), |
| 36 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), | 36 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), |
| 37 timestamp_(timestamp), | 37 timestamp_(timestamp), |
| 38 duration_(end_of_stream_ | 38 duration_(end_of_stream_ |
| 39 ? base::TimeDelta() | 39 ? base::TimeDelta() |
| 40 : CalculateDuration(adjusted_frame_count_, sample_rate_)), | 40 : CalculateDuration(adjusted_frame_count_, sample_rate_)) { |
| 41 data_size_(0) { | |
| 42 CHECK_GE(channel_count_, 0); | 41 CHECK_GE(channel_count_, 0); |
| 43 CHECK_LE(channel_count_, limits::kMaxChannels); | 42 CHECK_LE(channel_count_, limits::kMaxChannels); |
| 44 CHECK_GE(frame_count, 0); | 43 CHECK_GE(frame_count, 0); |
| 45 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || | 44 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || |
| 46 ChannelLayoutToChannelCount(channel_layout) == channel_count); | 45 ChannelLayoutToChannelCount(channel_layout) == channel_count); |
| 47 | 46 |
| 48 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); | 47 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); |
| 49 DCHECK_LE(bytes_per_channel, kChannelAlignment); | 48 DCHECK_LE(bytes_per_channel, kChannelAlignment); |
| 49 int data_size = frame_count * bytes_per_channel; |
| 50 | 50 |
| 51 // Empty buffer? | 51 // Empty buffer? |
| 52 if (!create_buffer) | 52 if (!create_buffer) |
| 53 return; | 53 return; |
| 54 | 54 |
| 55 int data_size_per_channel = frame_count * bytes_per_channel; | 55 if (sample_format == kSampleFormatPlanarF32 || |
| 56 if (IsPlanar(sample_format)) { | 56 sample_format == kSampleFormatPlanarS16 || |
| 57 sample_format == kSampleFormatPlanarS32) { |
| 57 // Planar data, so need to allocate buffer for each channel. | 58 // Planar data, so need to allocate buffer for each channel. |
| 58 // Determine per channel data size, taking into account alignment. | 59 // Determine per channel data size, taking into account alignment. |
| 59 int block_size_per_channel = | 60 int block_size_per_channel = |
| 60 (data_size_per_channel + kChannelAlignment - 1) & | 61 (data_size + kChannelAlignment - 1) & ~(kChannelAlignment - 1); |
| 61 ~(kChannelAlignment - 1); | 62 DCHECK_GE(block_size_per_channel, data_size); |
| 62 DCHECK_GE(block_size_per_channel, data_size_per_channel); | |
| 63 | 63 |
| 64 // Allocate a contiguous buffer for all the channel data. | 64 // Allocate a contiguous buffer for all the channel data. |
| 65 data_size_ = channel_count_ * block_size_per_channel; | 65 data_.reset(static_cast<uint8*>(base::AlignedAlloc( |
| 66 data_.reset( | 66 channel_count_ * block_size_per_channel, kChannelAlignment))); |
| 67 static_cast<uint8*>(base::AlignedAlloc(data_size_, kChannelAlignment))); | |
| 68 channel_data_.reserve(channel_count_); | 67 channel_data_.reserve(channel_count_); |
| 69 | 68 |
| 70 // Copy each channel's data into the appropriate spot. | 69 // Copy each channel's data into the appropriate spot. |
| 71 for (int i = 0; i < channel_count_; ++i) { | 70 for (int i = 0; i < channel_count_; ++i) { |
| 72 channel_data_.push_back(data_.get() + i * block_size_per_channel); | 71 channel_data_.push_back(data_.get() + i * block_size_per_channel); |
| 73 if (data) | 72 if (data) |
| 74 memcpy(channel_data_[i], data[i], data_size_per_channel); | 73 memcpy(channel_data_[i], data[i], data_size); |
| 75 } | 74 } |
| 76 return; | 75 return; |
| 77 } | 76 } |
| 78 | 77 |
| 79 // Remaining formats are interleaved data. | 78 // Remaining formats are interleaved data. |
| 80 DCHECK(IsInterleaved(sample_format)) << sample_format_; | 79 DCHECK(sample_format_ == kSampleFormatU8 || |
| 80 sample_format_ == kSampleFormatS16 || |
| 81 sample_format_ == kSampleFormatS32 || |
| 82 sample_format_ == kSampleFormatF32) << sample_format_; |
| 81 // Allocate our own buffer and copy the supplied data into it. Buffer must | 83 // Allocate our own buffer and copy the supplied data into it. Buffer must |
| 82 // contain the data for all channels. | 84 // contain the data for all channels. |
| 83 data_size_ = data_size_per_channel * channel_count_; | 85 data_size *= channel_count_; |
| 84 data_.reset( | 86 data_.reset( |
| 85 static_cast<uint8*>(base::AlignedAlloc(data_size_, kChannelAlignment))); | 87 static_cast<uint8*>(base::AlignedAlloc(data_size, kChannelAlignment))); |
| 86 channel_data_.reserve(1); | 88 channel_data_.reserve(1); |
| 87 channel_data_.push_back(data_.get()); | 89 channel_data_.push_back(data_.get()); |
| 88 if (data) | 90 if (data) |
| 89 memcpy(data_.get(), data[0], data_size_); | 91 memcpy(data_.get(), data[0], data_size); |
| 90 } | 92 } |
| 91 | 93 |
| 92 AudioBuffer::~AudioBuffer() {} | 94 AudioBuffer::~AudioBuffer() {} |
| 93 | 95 |
| 94 // static | 96 // static |
| 95 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( | 97 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( |
| 96 SampleFormat sample_format, | 98 SampleFormat sample_format, |
| 97 ChannelLayout channel_layout, | 99 ChannelLayout channel_layout, |
| 98 int channel_count, | 100 int channel_count, |
| 99 int sample_rate, | 101 int sample_rate, |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 } | 418 } |
| 417 } else { | 419 } else { |
| 418 CHECK_EQ(frames_to_copy, 0); | 420 CHECK_EQ(frames_to_copy, 0); |
| 419 } | 421 } |
| 420 | 422 |
| 421 // Trim the leftover data off the end of the buffer and update duration. | 423 // Trim the leftover data off the end of the buffer and update duration. |
| 422 TrimEnd(frames_to_trim); | 424 TrimEnd(frames_to_trim); |
| 423 } | 425 } |
| 424 | 426 |
| 425 } // namespace media | 427 } // namespace media |
| OLD | NEW |