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" |
11 | 11 |
12 namespace media { | 12 namespace media { |
13 | 13 |
14 AudioBuffer::AudioBuffer(SampleFormat sample_format, | 14 AudioBuffer::AudioBuffer(SampleFormat sample_format, |
15 ChannelLayout channel_layout, | 15 ChannelLayout channel_layout, |
| 16 int channel_count, |
16 int sample_rate, | 17 int sample_rate, |
17 int frame_count, | 18 int frame_count, |
18 bool create_buffer, | 19 bool create_buffer, |
19 const uint8* const* data, | 20 const uint8* const* data, |
20 const base::TimeDelta timestamp, | 21 const base::TimeDelta timestamp, |
21 const base::TimeDelta duration) | 22 const base::TimeDelta duration) |
22 : sample_format_(sample_format), | 23 : sample_format_(sample_format), |
23 channel_layout_(channel_layout), | 24 channel_layout_(channel_layout), |
24 channel_count_(ChannelLayoutToChannelCount(channel_layout)), | 25 channel_count_(channel_count), |
25 sample_rate_(sample_rate), | 26 sample_rate_(sample_rate), |
26 adjusted_frame_count_(frame_count), | 27 adjusted_frame_count_(frame_count), |
27 trim_start_(0), | 28 trim_start_(0), |
28 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), | 29 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), |
29 timestamp_(timestamp), | 30 timestamp_(timestamp), |
30 duration_(duration) { | 31 duration_(duration) { |
31 CHECK_GE(channel_count_, 0); | 32 CHECK_GE(channel_count_, 0); |
32 CHECK_LE(channel_count_, limits::kMaxChannels); | 33 CHECK_LE(channel_count_, limits::kMaxChannels); |
33 CHECK_GE(frame_count, 0); | 34 CHECK_GE(frame_count, 0); |
| 35 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || |
| 36 ChannelLayoutToChannelCount(channel_layout) == channel_count); |
| 37 |
34 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); | 38 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); |
35 DCHECK_LE(bytes_per_channel, kChannelAlignment); | 39 DCHECK_LE(bytes_per_channel, kChannelAlignment); |
36 int data_size = frame_count * bytes_per_channel; | 40 int data_size = frame_count * bytes_per_channel; |
37 | 41 |
38 // Empty buffer? | 42 // Empty buffer? |
39 if (!create_buffer) | 43 if (!create_buffer) |
40 return; | 44 return; |
41 | 45 |
42 if (sample_format == kSampleFormatPlanarF32 || | 46 if (sample_format == kSampleFormatPlanarF32 || |
43 sample_format == kSampleFormatPlanarS16) { | 47 sample_format == kSampleFormatPlanarS16) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 if (data) | 80 if (data) |
77 memcpy(data_.get(), data[0], data_size); | 81 memcpy(data_.get(), data[0], data_size); |
78 } | 82 } |
79 | 83 |
80 AudioBuffer::~AudioBuffer() {} | 84 AudioBuffer::~AudioBuffer() {} |
81 | 85 |
82 // static | 86 // static |
83 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( | 87 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( |
84 SampleFormat sample_format, | 88 SampleFormat sample_format, |
85 ChannelLayout channel_layout, | 89 ChannelLayout channel_layout, |
| 90 int channel_count, |
86 int sample_rate, | 91 int sample_rate, |
87 int frame_count, | 92 int frame_count, |
88 const uint8* const* data, | 93 const uint8* const* data, |
89 const base::TimeDelta timestamp, | 94 const base::TimeDelta timestamp, |
90 const base::TimeDelta duration) { | 95 const base::TimeDelta duration) { |
91 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it. | 96 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it. |
92 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 97 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
93 CHECK(data[0]); | 98 CHECK(data[0]); |
94 return make_scoped_refptr(new AudioBuffer(sample_format, | 99 return make_scoped_refptr(new AudioBuffer(sample_format, |
95 channel_layout, | 100 channel_layout, |
| 101 channel_count, |
96 sample_rate, | 102 sample_rate, |
97 frame_count, | 103 frame_count, |
98 true, | 104 true, |
99 data, | 105 data, |
100 timestamp, | 106 timestamp, |
101 duration)); | 107 duration)); |
102 } | 108 } |
103 | 109 |
104 // static | 110 // static |
105 scoped_refptr<AudioBuffer> AudioBuffer::CreateBuffer( | 111 scoped_refptr<AudioBuffer> AudioBuffer::CreateBuffer( |
106 SampleFormat sample_format, | 112 SampleFormat sample_format, |
107 ChannelLayout channel_layout, | 113 ChannelLayout channel_layout, |
| 114 int channel_count, |
108 int sample_rate, | 115 int sample_rate, |
109 int frame_count) { | 116 int frame_count) { |
110 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 117 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
111 return make_scoped_refptr(new AudioBuffer(sample_format, | 118 return make_scoped_refptr(new AudioBuffer(sample_format, |
112 channel_layout, | 119 channel_layout, |
| 120 channel_count, |
113 sample_rate, | 121 sample_rate, |
114 frame_count, | 122 frame_count, |
115 true, | 123 true, |
116 NULL, | 124 NULL, |
117 kNoTimestamp(), | 125 kNoTimestamp(), |
118 kNoTimestamp())); | 126 kNoTimestamp())); |
119 } | 127 } |
120 | 128 |
121 // static | 129 // static |
122 scoped_refptr<AudioBuffer> AudioBuffer::CreateEmptyBuffer( | 130 scoped_refptr<AudioBuffer> AudioBuffer::CreateEmptyBuffer( |
123 ChannelLayout channel_layout, | 131 ChannelLayout channel_layout, |
| 132 int channel_count, |
124 int sample_rate, | 133 int sample_rate, |
125 int frame_count, | 134 int frame_count, |
126 const base::TimeDelta timestamp, | 135 const base::TimeDelta timestamp, |
127 const base::TimeDelta duration) { | 136 const base::TimeDelta duration) { |
128 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 137 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
129 // Since data == NULL, format doesn't matter. | 138 // Since data == NULL, format doesn't matter. |
130 return make_scoped_refptr(new AudioBuffer(kSampleFormatF32, | 139 return make_scoped_refptr(new AudioBuffer(kSampleFormatF32, |
131 channel_layout, | 140 channel_layout, |
| 141 channel_count, |
132 sample_rate, | 142 sample_rate, |
133 frame_count, | 143 frame_count, |
134 false, | 144 false, |
135 NULL, | 145 NULL, |
136 timestamp, | 146 timestamp, |
137 duration)); | 147 duration)); |
138 } | 148 } |
139 | 149 |
140 // static | 150 // static |
141 scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() { | 151 scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() { |
142 return make_scoped_refptr(new AudioBuffer(kUnknownSampleFormat, | 152 return make_scoped_refptr(new AudioBuffer(kUnknownSampleFormat, |
143 CHANNEL_LAYOUT_NONE, | 153 CHANNEL_LAYOUT_NONE, |
144 0, | 154 0, |
145 0, | 155 0, |
| 156 0, |
146 false, | 157 false, |
147 NULL, | 158 NULL, |
148 kNoTimestamp(), | 159 kNoTimestamp(), |
149 kNoTimestamp())); | 160 kNoTimestamp())); |
150 } | 161 } |
151 | 162 |
152 // Convert int16 values in the range [kint16min, kint16max] to [-1.0, 1.0]. | 163 // Convert int16 values in the range [kint16min, kint16max] to [-1.0, 1.0]. |
153 static inline float ConvertS16ToFloat(int16 value) { | 164 static inline float ConvertS16ToFloat(int16 value) { |
154 return value * (value < 0 ? -1.0f / kint16min : 1.0f / kint16max); | 165 return value * (value < 0 ? -1.0f / kint16min : 1.0f / kint16max); |
155 } | 166 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 frames_to_trim / adjusted_frame_count_; | 269 frames_to_trim / adjusted_frame_count_; |
259 base::TimeDelta offset_as_time = | 270 base::TimeDelta offset_as_time = |
260 base::TimeDelta::FromMicroseconds(static_cast<int64>(offset)); | 271 base::TimeDelta::FromMicroseconds(static_cast<int64>(offset)); |
261 duration_ -= offset_as_time; | 272 duration_ -= offset_as_time; |
262 | 273 |
263 // Finally adjust the number of frames in this buffer. | 274 // Finally adjust the number of frames in this buffer. |
264 adjusted_frame_count_ -= frames_to_trim; | 275 adjusted_frame_count_ -= frames_to_trim; |
265 } | 276 } |
266 | 277 |
267 } // namespace media | 278 } // namespace media |
OLD | NEW |