From 40a355a42d4a9444dc753c04c6608dade2f06a23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:27 +0200 Subject: Adding upstream version 125.0.1. Signed-off-by: Daniel Baumann --- dom/media/AudioSampleFormat.h | 234 ++++++++++++++++++++++++++++-------------- 1 file changed, 159 insertions(+), 75 deletions(-) (limited to 'dom/media/AudioSampleFormat.h') diff --git a/dom/media/AudioSampleFormat.h b/dom/media/AudioSampleFormat.h index 1cec31a385..7a329d06df 100644 --- a/dom/media/AudioSampleFormat.h +++ b/dom/media/AudioSampleFormat.h @@ -1,13 +1,16 @@ /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* vim: set ts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #ifndef MOZILLA_AUDIOSAMPLEFORMAT_H_ #define MOZILLA_AUDIOSAMPLEFORMAT_H_ #include "mozilla/Assertions.h" +#include "mozilla/PodOperations.h" #include +#include +#include namespace mozilla { @@ -62,113 +65,191 @@ class AudioSampleTypeToFormat { static const AudioSampleFormat Format = AUDIO_FORMAT_S16; }; -// Single-sample conversion -/* - * Use "2^N" conversion since it's simple, fast, "bit transparent", used by - * many other libraries and apparently behaves reasonably. - * http://blog.bjornroche.com/2009/12/int-float-int-its-jungle-out-there.html - * http://blog.bjornroche.com/2009/12/linearity-and-dynamic-range-in-int.html - */ -inline float AudioSampleToFloat(float aValue) { return aValue; } -inline float AudioSampleToFloat(int16_t aValue) { - return static_cast(aValue) / 32768.0f; -} -inline float AudioSampleToFloat(int32_t aValue) { - return static_cast(aValue) / (float)(1U << 31); +template +constexpr float MaxAsFloat() { + return static_cast(std::numeric_limits::max()); } template -T FloatToAudioSample(float aValue); - -template <> -inline float FloatToAudioSample(float aValue) { - return aValue; -} -template <> -inline int16_t FloatToAudioSample(float aValue) { - float v = aValue * 32768.0f; - float clamped = std::max(-32768.0f, std::min(32767.0f, v)); - return int16_t(clamped); +constexpr float LowestAsFloat() { + return static_cast(std::numeric_limits::lowest()); } +// The maximum value for an audio sample. If T is signed, the absolute value of +// this number is smaller (by exactly 1) than ::Min(). template -T UInt8bitToAudioSample(uint8_t aValue); - -template <> -inline float UInt8bitToAudioSample(uint8_t aValue) { - return static_cast(aValue) * (static_cast(2) / UINT8_MAX) - - static_cast(1); -} -template <> -inline int16_t UInt8bitToAudioSample(uint8_t aValue) { - return static_cast((aValue << 8) + aValue + INT16_MIN); +constexpr T Max() { + return std::numeric_limits::max(); } +// The minimum value for an audio sample. If T is signed, the absolute value of +// this number is greater (by exactly 1) than ::Max() template -T IntegerToAudioSample(int16_t aValue); +constexpr T Min() { + return std::numeric_limits::lowest(); +} template <> -inline float IntegerToAudioSample(int16_t aValue) { - return static_cast(aValue) / 32768.0f; +constexpr float Max() { + return 1.0f; } + template <> -inline int16_t IntegerToAudioSample(int16_t aValue) { - return aValue; +constexpr float Min() { + return -1.0f; } +// The bias value is the middle of the range. In linear PCM audio, if the +// values are all equal to the bias value, the audio is silent. template -T Int24bitToAudioSample(int32_t aValue); +constexpr T Bias() { + return 0; +} template <> -inline float Int24bitToAudioSample(int32_t aValue) { - return static_cast(aValue) / static_cast(1 << 23); +constexpr uint8_t Bias() { + return 128; } -template <> -inline int16_t Int24bitToAudioSample(int32_t aValue) { - return static_cast(aValue / 256); + +// Clip a floating point audio sample to its nominal range. This is +// destructive, and is only used here for avoiding overflow in some edge cases, +// so it's not going to be generally audible. +inline float Clip(float aValue) { return std::clamp(aValue, -1.0f, 1.0f); } + +template +T FloatToAudioSample(float aValue) { + if constexpr (std::is_same_v) { + return aValue; + } + if constexpr (std::is_same_v) { + return static_cast(std::clamp((aValue + 1.0f) * 128.f, + LowestAsFloat(), MaxAsFloat())); + } else if constexpr (std::is_same_v) { + // This produces correct results accross the range. + return static_cast(std::clamp(aValue * -LowestAsFloat(), + LowestAsFloat(), MaxAsFloat())); + } else if constexpr (std::is_same_v) { + // We need to handle this differently because of rounding between INT32_MAX + // and float 32-bits, to maximise precision. + if (aValue >= 0.) { + // if the input sample is greater OR EQUAL to 1.0, then clip and return + // the max value. + if (aValue >= 1.0) { + return std::numeric_limits::max(); + } + // otherwise cast to a double and map to the positive range. + // float 32-bits cannot represent int32_max (but can represent int32_min) + constexpr double magnitudePos = std::numeric_limits::max(); + return static_cast(aValue * magnitudePos); + } + // Similarly for the negative range. + if (aValue <= -1.0) { + return std::numeric_limits::lowest(); + } + constexpr double magnitudeNegative = + -1.0 * std::numeric_limits::lowest(); + return static_cast(aValue * magnitudeNegative); + } } -template -inline void ConvertAudioSample(SrcT aIn, DstT& aOut); +template +T UInt8bitToAudioSample(uint8_t aValue) { + if constexpr (std::is_same_v) { + return aValue; + } else if constexpr (std::is_same_v) { + return (static_cast(aValue) << 8) - (1 << 15); + } else if constexpr (std::is_same_v) { + return (static_cast(aValue) << 24) - (1 << 31); + } else if constexpr (std::is_same_v) { + float biased = static_cast(aValue) - Bias(); + if (aValue >= Bias()) { + return Clip(biased / MaxAsFloat()); + } + return Clip(biased / -LowestAsFloat()); + } +} -template <> -inline void ConvertAudioSample(int16_t aIn, int16_t& aOut) { - aOut = aIn; +template +T Int16ToAudioSample(int16_t aValue) { + if constexpr (std::is_same_v) { + return static_cast(aValue >> 8) + 128; + } else if constexpr (std::is_same_v) { + return aValue; + } else if constexpr (std::is_same_v) { + return aValue << 16; + } else if constexpr (std::is_same_v) { + if (aValue >= 0) { + return Clip(static_cast(aValue) / MaxAsFloat()); + } + return Clip(static_cast(aValue) / -LowestAsFloat()); + } } -template <> -inline void ConvertAudioSample(int16_t aIn, float& aOut) { - aOut = AudioSampleToFloat(aIn); +// 24-bits audio samples are stored in 32-bits variables. +template +T Int24ToAudioSample(int32_t aValue) { + if constexpr (std::is_same_v) { + return static_cast(aValue >> 16) + 128; + } else if constexpr (std::is_same_v) { + return static_cast(aValue >> 8); + } else if constexpr (std::is_same_v) { + return aValue << 8; + } else if constexpr (std::is_same_v) { + const int32_t min = -(2 << 22); + const int32_t max = (2 << 22) - 1; + if (aValue >= 0) { + return Clip(static_cast(aValue) / static_cast(max)); + } + return Clip(static_cast(aValue) / -static_cast(min)); + } } -template <> -inline void ConvertAudioSample(float aIn, float& aOut) { - aOut = aIn; +template +T Int32ToAudioSample(int32_t aValue) { + if constexpr (std::is_same_v) { + return static_cast(aValue >> 24) + 128; + } else if constexpr (std::is_same_v) { + return aValue >> 16; + } else if constexpr (std::is_same_v) { + return aValue; + } else if constexpr (std::is_same_v) { + if (aValue >= 0) { + return Clip(static_cast(aValue) / MaxAsFloat()); + } + return Clip(static_cast(aValue) / -LowestAsFloat()); + } } -template <> -inline void ConvertAudioSample(float aIn, int16_t& aOut) { - aOut = FloatToAudioSample(aIn); +// This does not handle 24-bits audio, call the function explicitly when +// needed. +template +inline D ConvertAudioSample(const S& aSource) { + if constexpr (std::is_same_v) { + return aSource; + } else if constexpr (std::is_same_v) { + return UInt8bitToAudioSample(aSource); + } else if constexpr (std::is_same_v) { + return Int16ToAudioSample(aSource); + } else if constexpr (std::is_same_v) { + return Int32ToAudioSample(aSource); + } else if constexpr (std::is_same_v) { + return FloatToAudioSample(aSource); + } } // Sample buffer conversion - template inline void ConvertAudioSamples(const From* aFrom, To* aTo, int aCount) { + if constexpr (std::is_same_v) { + PodCopy(aTo, aFrom, aCount); + return; + } for (int i = 0; i < aCount; ++i) { - aTo[i] = FloatToAudioSample(AudioSampleToFloat(aFrom[i])); + aTo[i] = ConvertAudioSample(aFrom[i]); } } -inline void ConvertAudioSamples(const int16_t* aFrom, int16_t* aTo, - int aCount) { - memcpy(aTo, aFrom, sizeof(*aTo) * aCount); -} -inline void ConvertAudioSamples(const float* aFrom, float* aTo, int aCount) { - memcpy(aTo, aFrom, sizeof(*aTo) * aCount); -} // Sample buffer conversion with scale - template inline void ConvertAudioSamplesWithScale(const From* aFrom, To* aTo, int aCount, float aScale) { @@ -177,7 +258,8 @@ inline void ConvertAudioSamplesWithScale(const From* aFrom, To* aTo, int aCount, return; } for (int i = 0; i < aCount; ++i) { - aTo[i] = FloatToAudioSample(AudioSampleToFloat(aFrom[i]) * aScale); + aTo[i] = + ConvertAudioSample(ConvertAudioSample(aFrom[i]) * aScale); } } inline void ConvertAudioSamplesWithScale(const int16_t* aFrom, int16_t* aTo, @@ -194,7 +276,8 @@ inline void ConvertAudioSamplesWithScale(const int16_t* aFrom, int16_t* aTo, return; } for (int i = 0; i < aCount; ++i) { - aTo[i] = FloatToAudioSample(AudioSampleToFloat(aFrom[i]) * aScale); + aTo[i] = FloatToAudioSample(ConvertAudioSample(aFrom[i]) * + aScale); } } @@ -202,8 +285,9 @@ template inline void AddAudioSamplesWithScale(const From* aFrom, To* aTo, int aCount, float aScale) { for (int i = 0; i < aCount; ++i) { - aTo[i] = FloatToAudioSample(AudioSampleToFloat(aTo[i]) + - AudioSampleToFloat(aFrom[i]) * aScale); + aTo[i] = + ConvertAudioSample(ConvertAudioSample(aTo[i]) + + ConvertAudioSample(aFrom[i]) * aScale); } } -- cgit v1.2.3