From 424e9cbc4321e5b5cf07f1e611929eaa66fcd6da Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 22 Apr 2017 09:28:54 +0200 Subject: [PATCH] pcm/PcmPack: simplify unpack_sample() --- src/pcm/PcmPack.cxx | 49 +++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/pcm/PcmPack.cxx b/src/pcm/PcmPack.cxx index 18ddfc6d7..4da6c950f 100644 --- a/src/pcm/PcmPack.cxx +++ b/src/pcm/PcmPack.cxx @@ -45,22 +45,45 @@ pcm_pack_24(uint8_t *dest, const int32_t *src, const int32_t *src_end) } } -static void -unpack_sample(int32_t *dest0, const uint8_t *src) +/** + * Construct a signed 24 bit integer from three bytes into a int32_t. + */ +static constexpr int32_t +ConstructS24(uint8_t low, uint8_t mid, uint8_t high) { - uint8_t *dest = (uint8_t *)dest0; + return int32_t(low) | (int32_t(mid) << 8) | (int32_t(high) << 16) | + /* extend the sign bit */ + (high & 0x80 ? ~int32_t(0xffffff) : 0); +} - if (IsBigEndian()) - /* extend the sign bit to the most fourth byte */ - *dest++ = *src & 0x80 ? 0xff : 0x00; +/** + * Read a packed signed little-endian 24 bit integer. + */ +gcc_pure +static int32_t +ReadS24LE(const uint8_t *src) +{ + return ConstructS24(src[0], src[1], src[2]); +} - *dest++ = *src++; - *dest++ = *src++; - *dest++ = *src; +/** + * Read a packed signed big-endian 24 bit integer. + */ +gcc_pure +static int32_t +ReadS24BE(const uint8_t *src) +{ + return ConstructS24(src[2], src[1], src[0]); +} - if (IsLittleEndian()) - /* extend the sign bit to the most fourth byte */ - *dest++ = *src & 0x80 ? 0xff : 0x00; +/** + * Read a packed signed native-endian 24 bit integer. + */ +gcc_pure +static int32_t +ReadS24(const uint8_t *src) +{ + return IsBigEndian() ? ReadS24BE(src) : ReadS24LE(src); } void @@ -70,7 +93,7 @@ pcm_unpack_24(int32_t *dest, const uint8_t *src, const uint8_t *src_end) parameter to the unpack_sample() inline function) */ while (src < src_end) { - unpack_sample(dest++, src); + *dest++ = ReadS24(src); src += 3; } }