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

Unified Diff: media/base/audio_bus.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_bus.cc
diff --git a/media/base/audio_bus.cc b/media/base/audio_bus.cc
index 2b0379a7542bc4672abd51d256394b4700f7c94d..449b43c5eecf3c780f1f98fcc8e506bbb42f5227 100644
--- a/media/base/audio_bus.cc
+++ b/media/base/audio_bus.cc
@@ -12,13 +12,12 @@
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "media/audio/audio_parameters.h"
+#include "media/base/audio_sample_conversion.h"
#include "media/base/limits.h"
#include "media/base/vector_math.h"
namespace media {
-static const uint8_t kUint8Bias = 128;
-
static bool IsAligned(void* ptr) {
return (reinterpret_cast<uintptr_t>(ptr) &
(AudioBus::kChannelAlignment - 1)) == 0U;
@@ -40,54 +39,6 @@ static int CalculateMemorySizeInternal(int channels, int frames,
return sizeof(float) * channels * aligned_frames;
}
-// |Format| is the destination type. If a bias is present, |Fixed| must be a
-// type larger than |Format| such that operations can be made without
-// overflowing. Without a bias |Fixed| must be the same as |Format|.
-template<class Format, class Fixed, Format Bias>
-static void FromInterleavedInternal(const void* src, int start_frame,
- int frames, AudioBus* dest,
- float min, float max) {
- static_assert((Bias == 0 && sizeof(Fixed) == sizeof(Format)) ||
- sizeof(Fixed) > sizeof(Format), "invalid deinterleave types");
- const Format* source = static_cast<const Format*>(src);
- const int channels = dest->channels();
- for (int ch = 0; ch < channels; ++ch) {
- float* channel_data = dest->channel(ch);
- for (int i = start_frame, offset = ch; i < start_frame + frames;
- ++i, offset += channels) {
- const Fixed v = static_cast<Fixed>(source[offset]) - Bias;
- channel_data[i] = v * (v < 0 ? -min : max);
- }
- }
-}
-
-// |Format| is the destination type. If a bias is present, |Fixed| must be a
-// type larger than |Format| such that operations can be made without
-// overflowing. Without a bias |Fixed| must be the same as |Format|.
-template<class Format, class Fixed, Format Bias>
-static void ToInterleavedInternal(const AudioBus* source, int start_frame,
- int frames, void* dst, Fixed min, Fixed max) {
- static_assert((Bias == 0 && sizeof(Fixed) == sizeof(Format)) ||
- sizeof(Fixed) > sizeof(Format), "invalid interleave types");
- Format* dest = static_cast<Format*>(dst);
- const int channels = source->channels();
- for (int ch = 0; ch < channels; ++ch) {
- const float* channel_data = source->channel(ch);
- for (int i = start_frame, offset = ch; i < start_frame + frames;
- ++i, offset += channels) {
- const float v = channel_data[i];
-
- Fixed sample;
- if (v < 0)
- sample = v <= -1 ? min : static_cast<Fixed>(-v * min);
- else
- sample = v >= 1 ? max : static_cast<Fixed>(v * max);
-
- dest[offset] = static_cast<Format>(sample) + Bias;
- }
- }
-}
-
static void ValidateConfig(int channels, int frames) {
CHECK_GT(frames, 0);
CHECK_GT(channels, 0);
@@ -257,22 +208,19 @@ void AudioBus::FromInterleavedPartial(const void* source, int start_frame,
CheckOverflow(start_frame, frames, frames_);
switch (bytes_per_sample) {
case 1:
- FromInterleavedInternal<uint8_t, int16_t, kUint8Bias>(
- source, start_frame, frames, this,
- 1.0f / std::numeric_limits<int8_t>::min(),
- 1.0f / std::numeric_limits<int8_t>::max());
+ InterleavedToPlanar<uint8_t, float>(
+ reinterpret_cast<const uint8_t*>(source), frames, start_frame,
+ this->channel_data_);
break;
case 2:
- FromInterleavedInternal<int16_t, int16_t, 0>(
- source, start_frame, frames, this,
- 1.0f / std::numeric_limits<int16_t>::min(),
- 1.0f / std::numeric_limits<int16_t>::max());
+ InterleavedToPlanar<int16_t, float>(
+ reinterpret_cast<const int16_t*>(source), frames, start_frame,
+ this->channel_data_);
break;
case 4:
- FromInterleavedInternal<int32_t, int32_t, 0>(
- source, start_frame, frames, this,
- 1.0f / std::numeric_limits<int32_t>::min(),
- 1.0f / std::numeric_limits<int32_t>::max());
+ InterleavedToPlanar<int32_t, float>(
+ reinterpret_cast<const int32_t*>(source), frames, start_frame,
+ this->channel_data_);
break;
default:
NOTREACHED() << "Unsupported bytes per sample encountered.";
@@ -303,19 +251,16 @@ void AudioBus::ToInterleavedPartial(int start_frame, int frames,
CheckOverflow(start_frame, frames, frames_);
switch (bytes_per_sample) {
case 1:
- ToInterleavedInternal<uint8_t, int16_t, kUint8Bias>(
- this, start_frame, frames, dest, std::numeric_limits<int8_t>::min(),
- std::numeric_limits<int8_t>::max());
+ PlanarToInterleaved<float>(this->channel_data_, frames, start_frame,
+ reinterpret_cast<uint8_t*>(dest));
break;
case 2:
- ToInterleavedInternal<int16_t, int16_t, 0>(
- this, start_frame, frames, dest, std::numeric_limits<int16_t>::min(),
- std::numeric_limits<int16_t>::max());
+ PlanarToInterleaved<float>(this->channel_data_, frames, start_frame,
+ reinterpret_cast<int16_t*>(dest));
break;
case 4:
- ToInterleavedInternal<int32_t, int32_t, 0>(
- this, start_frame, frames, dest, std::numeric_limits<int32_t>::min(),
- std::numeric_limits<int32_t>::max());
+ PlanarToInterleaved<float>(this->channel_data_, frames, start_frame,
+ reinterpret_cast<int32_t*>(dest));
break;
default:
NOTREACHED() << "Unsupported bytes per sample encountered.";

Powered by Google App Engine
This is Rietveld 408576698