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

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: Have separate functions for rounding and non-rounding Created 4 years, 8 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
« no previous file with comments | « media/base/audio_buffer.cc ('k') | media/base/audio_sample_conversion.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/audio_bus.cc
diff --git a/media/base/audio_bus.cc b/media/base/audio_bus.cc
index 2b0379a7542bc4672abd51d256394b4700f7c94d..c808bcd81d92b9fc7509600013e98d35b057187a 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,51 +39,29 @@ 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");
+template <class Format>
+static void FromInterleavedInternal(const void* src,
+ int start_frame,
+ int frames,
+ AudioBus* dest) {
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);
- }
+ DeinterleaveOneChannel(source, start_frame, frames, dest->channel(ch),
+ channels, ch);
}
}
-// |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");
+template <class Format>
+static void ToInterleavedInternal(const AudioBus* source,
+ int start_frame,
+ int frames,
+ void* dst) {
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;
- }
+ InterleaveOneChannel(source->channel(ch), start_frame, frames, dest,
+ channels, ch);
}
}
@@ -257,22 +234,13 @@ 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());
+ FromInterleavedInternal<uint8_t>(source, start_frame, frames, this);
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());
+ FromInterleavedInternal<int16_t>(source, start_frame, frames, this);
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());
+ FromInterleavedInternal<int32_t>(source, start_frame, frames, this);
break;
default:
NOTREACHED() << "Unsupported bytes per sample encountered.";
@@ -303,19 +271,13 @@ 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());
+ ToInterleavedInternal<uint8_t>(this, start_frame, frames, 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());
+ ToInterleavedInternal<int16_t>(this, start_frame, frames, 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());
+ ToInterleavedInternal<int32_t>(this, start_frame, frames, dest);
break;
default:
NOTREACHED() << "Unsupported bytes per sample encountered.";
« no previous file with comments | « media/base/audio_buffer.cc ('k') | media/base/audio_sample_conversion.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698