| 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 static base::TimeDelta CalculateDuration(int frames, double sample_rate) { |
| 15 DCHECK_GT(sample_rate, 0); |
| 16 return base::TimeDelta::FromMicroseconds( |
| 17 frames * base::Time::kMicrosecondsPerSecond / sample_rate); |
| 18 } |
| 19 |
| 14 AudioBuffer::AudioBuffer(SampleFormat sample_format, | 20 AudioBuffer::AudioBuffer(SampleFormat sample_format, |
| 15 ChannelLayout channel_layout, | 21 ChannelLayout channel_layout, |
| 16 int channel_count, | 22 int channel_count, |
| 17 int sample_rate, | 23 int sample_rate, |
| 18 int frame_count, | 24 int frame_count, |
| 19 bool create_buffer, | 25 bool create_buffer, |
| 20 const uint8* const* data, | 26 const uint8* const* data, |
| 21 const base::TimeDelta timestamp, | 27 const base::TimeDelta timestamp) |
| 22 const base::TimeDelta duration) | |
| 23 : sample_format_(sample_format), | 28 : sample_format_(sample_format), |
| 24 channel_layout_(channel_layout), | 29 channel_layout_(channel_layout), |
| 25 channel_count_(channel_count), | 30 channel_count_(channel_count), |
| 26 sample_rate_(sample_rate), | 31 sample_rate_(sample_rate), |
| 27 adjusted_frame_count_(frame_count), | 32 adjusted_frame_count_(frame_count), |
| 28 trim_start_(0), | 33 trim_start_(0), |
| 29 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), | 34 end_of_stream_(!create_buffer && data == NULL && frame_count == 0), |
| 30 timestamp_(timestamp), | 35 timestamp_(timestamp), |
| 31 duration_(duration) { | 36 duration_(end_of_stream_ |
| 37 ? base::TimeDelta() |
| 38 : CalculateDuration(adjusted_frame_count_, sample_rate_)) { |
| 32 CHECK_GE(channel_count_, 0); | 39 CHECK_GE(channel_count_, 0); |
| 33 CHECK_LE(channel_count_, limits::kMaxChannels); | 40 CHECK_LE(channel_count_, limits::kMaxChannels); |
| 34 CHECK_GE(frame_count, 0); | 41 CHECK_GE(frame_count, 0); |
| 35 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || | 42 DCHECK(channel_layout == CHANNEL_LAYOUT_DISCRETE || |
| 36 ChannelLayoutToChannelCount(channel_layout) == channel_count); | 43 ChannelLayoutToChannelCount(channel_layout) == channel_count); |
| 37 | 44 |
| 38 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); | 45 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format); |
| 39 DCHECK_LE(bytes_per_channel, kChannelAlignment); | 46 DCHECK_LE(bytes_per_channel, kChannelAlignment); |
| 40 int data_size = frame_count * bytes_per_channel; | 47 int data_size = frame_count * bytes_per_channel; |
| 41 | 48 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 AudioBuffer::~AudioBuffer() {} | 91 AudioBuffer::~AudioBuffer() {} |
| 85 | 92 |
| 86 // static | 93 // static |
| 87 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( | 94 scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom( |
| 88 SampleFormat sample_format, | 95 SampleFormat sample_format, |
| 89 ChannelLayout channel_layout, | 96 ChannelLayout channel_layout, |
| 90 int channel_count, | 97 int channel_count, |
| 91 int sample_rate, | 98 int sample_rate, |
| 92 int frame_count, | 99 int frame_count, |
| 93 const uint8* const* data, | 100 const uint8* const* data, |
| 94 const base::TimeDelta timestamp, | 101 const base::TimeDelta timestamp) { |
| 95 const base::TimeDelta duration) { | |
| 96 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it. | 102 // If you hit this CHECK you likely have a bug in a demuxer. Go fix it. |
| 97 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 103 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
| 98 CHECK(data[0]); | 104 CHECK(data[0]); |
| 99 return make_scoped_refptr(new AudioBuffer(sample_format, | 105 return make_scoped_refptr(new AudioBuffer(sample_format, |
| 100 channel_layout, | 106 channel_layout, |
| 101 channel_count, | 107 channel_count, |
| 102 sample_rate, | 108 sample_rate, |
| 103 frame_count, | 109 frame_count, |
| 104 true, | 110 true, |
| 105 data, | 111 data, |
| 106 timestamp, | 112 timestamp)); |
| 107 duration)); | |
| 108 } | 113 } |
| 109 | 114 |
| 110 // static | 115 // static |
| 111 scoped_refptr<AudioBuffer> AudioBuffer::CreateBuffer( | 116 scoped_refptr<AudioBuffer> AudioBuffer::CreateBuffer( |
| 112 SampleFormat sample_format, | 117 SampleFormat sample_format, |
| 113 ChannelLayout channel_layout, | 118 ChannelLayout channel_layout, |
| 114 int channel_count, | 119 int channel_count, |
| 115 int sample_rate, | 120 int sample_rate, |
| 116 int frame_count) { | 121 int frame_count) { |
| 117 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 122 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
| 118 return make_scoped_refptr(new AudioBuffer(sample_format, | 123 return make_scoped_refptr(new AudioBuffer(sample_format, |
| 119 channel_layout, | 124 channel_layout, |
| 120 channel_count, | 125 channel_count, |
| 121 sample_rate, | 126 sample_rate, |
| 122 frame_count, | 127 frame_count, |
| 123 true, | 128 true, |
| 124 NULL, | 129 NULL, |
| 125 kNoTimestamp(), | |
| 126 kNoTimestamp())); | 130 kNoTimestamp())); |
| 127 } | 131 } |
| 128 | 132 |
| 129 // static | 133 // static |
| 130 scoped_refptr<AudioBuffer> AudioBuffer::CreateEmptyBuffer( | 134 scoped_refptr<AudioBuffer> AudioBuffer::CreateEmptyBuffer( |
| 131 ChannelLayout channel_layout, | 135 ChannelLayout channel_layout, |
| 132 int channel_count, | 136 int channel_count, |
| 133 int sample_rate, | 137 int sample_rate, |
| 134 int frame_count, | 138 int frame_count, |
| 135 const base::TimeDelta timestamp, | 139 const base::TimeDelta timestamp) { |
| 136 const base::TimeDelta duration) { | |
| 137 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. | 140 CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer. |
| 138 // Since data == NULL, format doesn't matter. | 141 // Since data == NULL, format doesn't matter. |
| 139 return make_scoped_refptr(new AudioBuffer(kSampleFormatF32, | 142 return make_scoped_refptr(new AudioBuffer(kSampleFormatF32, |
| 140 channel_layout, | 143 channel_layout, |
| 141 channel_count, | 144 channel_count, |
| 142 sample_rate, | 145 sample_rate, |
| 143 frame_count, | 146 frame_count, |
| 144 false, | 147 false, |
| 145 NULL, | 148 NULL, |
| 146 timestamp, | 149 timestamp)); |
| 147 duration)); | |
| 148 } | 150 } |
| 149 | 151 |
| 150 // static | 152 // static |
| 151 scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() { | 153 scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() { |
| 152 return make_scoped_refptr(new AudioBuffer(kUnknownSampleFormat, | 154 return make_scoped_refptr(new AudioBuffer(kUnknownSampleFormat, |
| 153 CHANNEL_LAYOUT_NONE, | 155 CHANNEL_LAYOUT_NONE, |
| 154 0, | 156 0, |
| 155 0, | 157 0, |
| 156 0, | 158 0, |
| 157 false, | 159 false, |
| 158 NULL, | 160 NULL, |
| 159 kNoTimestamp(), | |
| 160 kNoTimestamp())); | 161 kNoTimestamp())); |
| 161 } | 162 } |
| 162 | 163 |
| 163 // Convert int16 values in the range [kint16min, kint16max] to [-1.0, 1.0]. | 164 // Convert int16 values in the range [kint16min, kint16max] to [-1.0, 1.0]. |
| 164 static inline float ConvertS16ToFloat(int16 value) { | 165 static inline float ConvertS16ToFloat(int16 value) { |
| 165 return value * (value < 0 ? -1.0f / kint16min : 1.0f / kint16max); | 166 return value * (value < 0 ? -1.0f / kint16min : 1.0f / kint16max); |
| 166 } | 167 } |
| 167 | 168 |
| 168 void AudioBuffer::ReadFrames(int frames_to_copy, | 169 void AudioBuffer::ReadFrames(int frames_to_copy, |
| 169 int source_frame_offset, | 170 int source_frame_offset, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 int frame_size = channel_count_ * bytes_per_channel; | 240 int frame_size = channel_count_ * bytes_per_channel; |
| 240 const uint8* source_data = data_.get() + source_frame_offset * frame_size; | 241 const uint8* source_data = data_.get() + source_frame_offset * frame_size; |
| 241 dest->FromInterleavedPartial( | 242 dest->FromInterleavedPartial( |
| 242 source_data, dest_frame_offset, frames_to_copy, bytes_per_channel); | 243 source_data, dest_frame_offset, frames_to_copy, bytes_per_channel); |
| 243 } | 244 } |
| 244 | 245 |
| 245 void AudioBuffer::TrimStart(int frames_to_trim) { | 246 void AudioBuffer::TrimStart(int frames_to_trim) { |
| 246 CHECK_GE(frames_to_trim, 0); | 247 CHECK_GE(frames_to_trim, 0); |
| 247 CHECK_LE(frames_to_trim, adjusted_frame_count_); | 248 CHECK_LE(frames_to_trim, adjusted_frame_count_); |
| 248 | 249 |
| 249 // Adjust timestamp_ and duration_ to reflect the smaller number of frames. | 250 // Adjust the number of frames in this buffer and where the start really is. |
| 250 double offset = static_cast<double>(duration_.InMicroseconds()) * | |
| 251 frames_to_trim / adjusted_frame_count_; | |
| 252 base::TimeDelta offset_as_time = | |
| 253 base::TimeDelta::FromMicroseconds(static_cast<int64>(offset)); | |
| 254 timestamp_ += offset_as_time; | |
| 255 duration_ -= offset_as_time; | |
| 256 | |
| 257 // Finally adjust the number of frames in this buffer and where the start | |
| 258 // really is. | |
| 259 adjusted_frame_count_ -= frames_to_trim; | 251 adjusted_frame_count_ -= frames_to_trim; |
| 260 trim_start_ += frames_to_trim; | 252 trim_start_ += frames_to_trim; |
| 253 |
| 254 // Adjust timestamp_ and duration_ to reflect the smaller number of frames. |
| 255 const base::TimeDelta old_duration = duration_; |
| 256 duration_ = CalculateDuration(adjusted_frame_count_, sample_rate_); |
| 257 timestamp_ += old_duration - duration_; |
| 261 } | 258 } |
| 262 | 259 |
| 263 void AudioBuffer::TrimEnd(int frames_to_trim) { | 260 void AudioBuffer::TrimEnd(int frames_to_trim) { |
| 264 CHECK_GE(frames_to_trim, 0); | 261 CHECK_GE(frames_to_trim, 0); |
| 265 CHECK_LE(frames_to_trim, adjusted_frame_count_); | 262 CHECK_LE(frames_to_trim, adjusted_frame_count_); |
| 266 | 263 |
| 267 // Adjust duration_ only to reflect the smaller number of frames. | 264 // Adjust the number of frames and duration for this buffer. |
| 268 double offset = static_cast<double>(duration_.InMicroseconds()) * | |
| 269 frames_to_trim / adjusted_frame_count_; | |
| 270 base::TimeDelta offset_as_time = | |
| 271 base::TimeDelta::FromMicroseconds(static_cast<int64>(offset)); | |
| 272 duration_ -= offset_as_time; | |
| 273 | |
| 274 // Finally adjust the number of frames in this buffer. | |
| 275 adjusted_frame_count_ -= frames_to_trim; | 265 adjusted_frame_count_ -= frames_to_trim; |
| 266 duration_ = CalculateDuration(adjusted_frame_count_, sample_rate_); |
| 276 } | 267 } |
| 277 | 268 |
| 278 } // namespace media | 269 } // namespace media |
| OLD | NEW |