Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Unified Diff: media/base/audio_buffer.cc

Issue 1854433002: Clamp AudioBuffer float to int{16,32} conversion (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix handling of 32-bit, fix asymmetric scale Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/base/audio_buffer.cc
diff --git a/media/base/audio_buffer.cc b/media/base/audio_buffer.cc
index 9370ee891b0ec16519cd0b06ad08135847ad14f3..3c4c88e41bdc892819e7f468b6722182e54958db 100644
--- a/media/base/audio_buffer.cc
+++ b/media/base/audio_buffer.cc
@@ -4,10 +4,12 @@
#include "media/base/audio_buffer.h"
+#include <algorithm>
#include <cmath>
#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"
@@ -162,52 +164,6 @@ scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() {
kNoTimestamp()));
}
-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) {
- return static_cast<int32_t>(value) << 16;
-}
-
-template <>
-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) {
- return sample;
-}
-
-template <>
-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;
@@ -291,21 +247,6 @@ void AudioBuffer::ReadFrames(int frames_to_copy,
source_data, dest_frame_offset, frames_to_copy, bytes_per_channel);
}
-template <class Target, typename Dest>
-void InterleaveAndConvert(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 Target* source_data =
- reinterpret_cast<const Target*>(channel_data[ch]) + trim_start;
- for (size_t i = 0, offset = ch; i < frames_to_copy;
- ++i, offset += channel_data.size()) {
- dest_data[offset] = ConvertSample<Target, Dest>(source_data[i]);
- }
- }
-}
-
template <typename Dest>
void ReadFramesInterleaved(const std::vector<uint8_t*>& channel_data,
int channel_count,
@@ -318,29 +259,29 @@ void ReadFramesInterleaved(const std::vector<uint8_t*>& channel_data,
NOTREACHED();
break;
case kSampleFormatS16:
- InterleaveAndConvert<int16_t, Dest>(
+ PlanarToInterleaved<int16_t, Dest>(
channel_data, frames_to_copy * channel_count, trim_start, dest_data);
break;
case kSampleFormatS24:
case kSampleFormatS32:
- InterleaveAndConvert<int32_t, Dest>(
+ PlanarToInterleaved<int32_t, Dest>(
channel_data, frames_to_copy * channel_count, trim_start, dest_data);
break;
case kSampleFormatF32:
- InterleaveAndConvert<float, Dest>(
+ PlanarToInterleaved<float, Dest>(
channel_data, frames_to_copy * channel_count, trim_start, dest_data);
break;
case kSampleFormatPlanarS16:
- InterleaveAndConvert<int16_t, Dest>(channel_data, frames_to_copy,
- trim_start, dest_data);
+ PlanarToInterleaved<int16_t, Dest>(channel_data, frames_to_copy,
+ trim_start, dest_data);
break;
case kSampleFormatPlanarF32:
- InterleaveAndConvert<float, Dest>(channel_data, frames_to_copy,
- trim_start, dest_data);
+ PlanarToInterleaved<float, Dest>(channel_data, frames_to_copy, trim_start,
+ dest_data);
break;
case kSampleFormatPlanarS32:
- InterleaveAndConvert<int32_t, Dest>(channel_data, frames_to_copy,
- trim_start, dest_data);
+ PlanarToInterleaved<int32_t, Dest>(channel_data, frames_to_copy,
+ trim_start, dest_data);
break;
case kUnknownSampleFormat:
NOTREACHED();

Powered by Google App Engine
This is Rietveld 408576698