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) { | |
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 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 if (sample_format == kSampleFormatPlanarF32 || | 55 int data_size_per_channel = frame_count * bytes_per_channel; |
56 sample_format == kSampleFormatPlanarS16 || | 56 if (IsPlanar(sample_format)) { |
57 sample_format == kSampleFormatPlanarS32) { | |
58 // Planar data, so need to allocate buffer for each channel. | 57 // Planar data, so need to allocate buffer for each channel. |
59 // Determine per channel data size, taking into account alignment. | 58 // Determine per channel data size, taking into account alignment. |
60 int block_size_per_channel = | 59 int block_size_per_channel = |
61 (data_size + kChannelAlignment - 1) & ~(kChannelAlignment - 1); | 60 (data_size_per_channel + kChannelAlignment - 1) & |
62 DCHECK_GE(block_size_per_channel, data_size); | 61 ~(kChannelAlignment - 1); |
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_.reset(static_cast<uint8*>(base::AlignedAlloc( | 65 data_size_ = channel_count_ * block_size_per_channel; |
66 channel_count_ * block_size_per_channel, kChannelAlignment))); | 66 data_.reset( |
67 static_cast<uint8*>(base::AlignedAlloc(data_size_, kChannelAlignment))); | |
67 channel_data_.reserve(channel_count_); | 68 channel_data_.reserve(channel_count_); |
68 | 69 |
69 // Copy each channel's data into the appropriate spot. | 70 // Copy each channel's data into the appropriate spot. |
70 for (int i = 0; i < channel_count_; ++i) { | 71 for (int i = 0; i < channel_count_; ++i) { |
71 channel_data_.push_back(data_.get() + i * block_size_per_channel); | 72 channel_data_.push_back(data_.get() + i * block_size_per_channel); |
72 if (data) | 73 if (data) |
73 memcpy(channel_data_[i], data[i], data_size); | 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(sample_format_ == kSampleFormatU8 || | 80 DCHECK(sample_format_ == kSampleFormatU8 || |
80 sample_format_ == kSampleFormatS16 || | 81 sample_format_ == kSampleFormatS16 || |
81 sample_format_ == kSampleFormatS32 || | 82 sample_format_ == kSampleFormatS32 || |
82 sample_format_ == kSampleFormatF32) << sample_format_; | 83 sample_format_ == kSampleFormatF32) << sample_format_; |
xhwang
2015/11/24 05:59:13
!IsPlanar()?
Actually we could also have kUnknown
jrummell
2015/11/24 20:51:05
Done.
| |
83 // Allocate our own buffer and copy the supplied data into it. Buffer must | 84 // Allocate our own buffer and copy the supplied data into it. Buffer must |
84 // contain the data for all channels. | 85 // contain the data for all channels. |
85 data_size *= channel_count_; | 86 data_size_ = data_size_per_channel * channel_count_; |
86 data_.reset( | 87 data_.reset( |
87 static_cast<uint8*>(base::AlignedAlloc(data_size, kChannelAlignment))); | 88 static_cast<uint8*>(base::AlignedAlloc(data_size_, kChannelAlignment))); |
88 channel_data_.reserve(1); | 89 channel_data_.reserve(1); |
89 channel_data_.push_back(data_.get()); | 90 channel_data_.push_back(data_.get()); |
90 if (data) | 91 if (data) |
91 memcpy(data_.get(), data[0], data_size); | 92 memcpy(data_.get(), data[0], data_size_); |
92 } | 93 } |
93 | 94 |
94 AudioBuffer::~AudioBuffer() {} | 95 AudioBuffer::~AudioBuffer() {} |
95 | 96 |
96 // static | 97 // static |
97 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( | 98 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( |
98 SampleFormat sample_format, | 99 SampleFormat sample_format, |
99 ChannelLayout channel_layout, | 100 ChannelLayout channel_layout, |
100 int channel_count, | 101 int channel_count, |
101 int sample_rate, | 102 int sample_rate, |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 } | 419 } |
419 } else { | 420 } else { |
420 CHECK_EQ(frames_to_copy, 0); | 421 CHECK_EQ(frames_to_copy, 0); |
421 } | 422 } |
422 | 423 |
423 // Trim the leftover data off the end of the buffer and update duration. | 424 // Trim the leftover data off the end of the buffer and update duration. |
424 TrimEnd(frames_to_trim); | 425 TrimEnd(frames_to_trim); |
425 } | 426 } |
426 | 427 |
427 } // namespace media | 428 } // namespace media |
OLD | NEW |