Index: media/base/audio_buffer.cc |
diff --git a/media/base/audio_buffer.cc b/media/base/audio_buffer.cc |
index 9370ee891b0ec16519cd0b06ad08135847ad14f3..4bc4b48a8416ee1b650fe7a3bc8476f0a85017b4 100644 |
--- a/media/base/audio_buffer.cc |
+++ b/media/base/audio_buffer.cc |
@@ -8,6 +8,7 @@ |
#include "base/logging.h" |
#include "media/base/audio_bus.h" |
+#include "media/base/audio_sample_conversion.h" |
#include "media/base/limits.h" |
#include "media/base/timestamp_constants.h" |
@@ -165,13 +166,6 @@ scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() { |
template <typename Target, typename Dest> |
static inline Dest ConvertSample(Target value); |
-// Convert int16_t values in the range [INT16_MIN, INT16_MAX] to [-1.0, 1.0]. |
-template <> |
-inline float ConvertSample<int16_t, float>(int16_t value) { |
- return value * (value < 0 ? -1.0f / std::numeric_limits<int16_t>::min() |
- : 1.0f / std::numeric_limits<int16_t>::max()); |
-} |
- |
// Specializations for int32_t |
template <> |
inline int32_t ConvertSample<int16_t, int32_t>(int16_t value) { |
@@ -183,13 +177,6 @@ inline int32_t ConvertSample<int32_t, int32_t>(int32_t value) { |
return value; |
} |
-template <> |
-inline int32_t ConvertSample<float, int32_t>(float value) { |
- return static_cast<int32_t>( |
- value < 0 ? (-value) * std::numeric_limits<int32_t>::min() |
- : value * std::numeric_limits<int32_t>::max()); |
-} |
- |
// Specializations for int16_t |
template <> |
inline int16_t ConvertSample<int16_t, int16_t>(int16_t sample) { |
@@ -201,13 +188,6 @@ inline int16_t ConvertSample<int32_t, int16_t>(int32_t sample) { |
return sample >> 16; |
} |
-template <> |
-inline int16_t ConvertSample<float, int16_t>(float sample) { |
- return static_cast<int16_t>( |
- nearbyint(sample < 0 ? (-sample) * std::numeric_limits<int16_t>::min() |
- : sample * std::numeric_limits<int16_t>::max())); |
-} |
- |
void AudioBuffer::AdjustSampleRate(int sample_rate) { |
DCHECK(!end_of_stream_); |
sample_rate_ = sample_rate; |
@@ -258,9 +238,8 @@ void AudioBuffer::ReadFrames(int frames_to_copy, |
reinterpret_cast<const int16_t*>(channel_data_[ch]) + |
source_frame_offset; |
float* dest_data = dest->channel(ch) + dest_frame_offset; |
- for (int i = 0; i < frames_to_copy; ++i) { |
- dest_data[i] = ConvertSample<int16_t, float>(source_data[i]); |
- } |
+ DeinterleaveOneChannel<int16_t>(source_data, 0, frames_to_copy, dest_data, |
+ 1, 0); |
} |
return; |
} |
@@ -271,10 +250,8 @@ void AudioBuffer::ReadFrames(int frames_to_copy, |
source_frame_offset * channel_count_; |
for (int ch = 0; ch < channel_count_; ++ch) { |
float* dest_data = dest->channel(ch) + dest_frame_offset; |
- for (int i = 0, offset = ch; i < frames_to_copy; |
- ++i, offset += channel_count_) { |
- dest_data[i] = source_data[offset]; |
- } |
+ DeinterleaveOneChannel<float>(source_data, 0, frames_to_copy, dest_data, |
+ channel_count_, ch); |
} |
return; |
} |
@@ -307,6 +284,19 @@ void InterleaveAndConvert(const std::vector<uint8_t*>& channel_data, |
} |
template <typename Dest> |
+void InterleaveAndConvertFloat(const std::vector<uint8_t*>& channel_data, |
+ size_t frames_to_copy, |
+ int trim_start, |
+ Dest* dest_data) { |
+ for (size_t ch = 0; ch < channel_data.size(); ++ch) { |
+ const float* source_data = |
+ reinterpret_cast<const float*>(channel_data[ch]) + trim_start; |
+ InterleaveOneChannelRounded<Dest>(source_data, 0, frames_to_copy, dest_data, |
+ channel_data.size(), ch); |
+ } |
+} |
+ |
+template <typename Dest> |
void ReadFramesInterleaved(const std::vector<uint8_t*>& channel_data, |
int channel_count, |
SampleFormat sample_format, |
@@ -327,7 +317,7 @@ void ReadFramesInterleaved(const std::vector<uint8_t*>& channel_data, |
channel_data, frames_to_copy * channel_count, trim_start, dest_data); |
break; |
case kSampleFormatF32: |
- InterleaveAndConvert<float, Dest>( |
+ InterleaveAndConvertFloat<Dest>( |
channel_data, frames_to_copy * channel_count, trim_start, dest_data); |
break; |
case kSampleFormatPlanarS16: |
@@ -335,8 +325,8 @@ void ReadFramesInterleaved(const std::vector<uint8_t*>& channel_data, |
trim_start, dest_data); |
break; |
case kSampleFormatPlanarF32: |
- InterleaveAndConvert<float, Dest>(channel_data, frames_to_copy, |
- trim_start, dest_data); |
+ InterleaveAndConvertFloat<Dest>(channel_data, frames_to_copy, trim_start, |
+ dest_data); |
break; |
case kSampleFormatPlanarS32: |
InterleaveAndConvert<int32_t, Dest>(channel_data, frames_to_copy, |