From 8088bc8081133a12abc97bc30558beb681d8a1f4 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Thu, 25 Oct 2012 23:09:40 +1300 Subject: [PATCH] Bug 805254. Part 9: Get rid of some more #ifdefs in nsWaveDecoder. r=kinetik --- content/media/wave/nsWaveReader.cpp | 40 ++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/content/media/wave/nsWaveReader.cpp b/content/media/wave/nsWaveReader.cpp index 4b4ea62485dc..5f2ef1a893ce 100644 --- a/content/media/wave/nsWaveReader.cpp +++ b/content/media/wave/nsWaveReader.cpp @@ -146,6 +146,31 @@ nsresult nsWaveReader::ReadMetadata(nsVideoInfo* aInfo, return NS_OK; } +template T UnsignedByteToAudioSample(uint8_t aValue); +template T SignedShortToAudioSample(int16_t aValue); + +template <> inline float +UnsignedByteToAudioSample(uint8_t aValue) +{ + return aValue * (2.0f / UINT8_MAX) - 1.0f; +} +template <> inline int16_t +UnsignedByteToAudioSample(uint8_t aValue) +{ + return int16_t(aValue * UINT16_MAX / UINT8_MAX + INT16_MIN); +} + +template <> inline float +SignedShortToAudioSample(int16_t aValue) +{ + return AudioSampleToFloat(aValue); +} +template <> inline int16_t +SignedShortToAudioSample(int16_t aValue) +{ + return aValue; +} + bool nsWaveReader::DecodeAudioData() { NS_ASSERTION(mDecoder->OnDecodeThread(), "Should be on decode thread."); @@ -178,19 +203,10 @@ bool nsWaveReader::DecodeAudioData() for (unsigned int j = 0; j < mChannels; ++j) { if (mSampleFormat == FORMAT_U8) { uint8_t v = ReadUint8(&d); -#if defined(MOZ_SAMPLE_TYPE_S16) - *s++ = (v * (1.F/UINT8_MAX)) * UINT16_MAX + INT16_MIN; -#elif defined(MOZ_SAMPLE_TYPE_FLOAT32) - *s++ = (v * (1.F/UINT8_MAX)) * 2.F - 1.F; -#endif - } - else if (mSampleFormat == FORMAT_S16) { + *s++ = UnsignedByteToAudioSample(v); + } else if (mSampleFormat == FORMAT_S16) { int16_t v = ReadInt16LE(&d); -#if defined(MOZ_SAMPLE_TYPE_S16) - *s++ = v; -#elif defined(MOZ_SAMPLE_TYPE_FLOAT32) - *s++ = (int32_t(v) - INT16_MIN) / float(UINT16_MAX) * 2.F - 1.F; -#endif + *s++ = SignedShortToAudioSample(v); } } }