pcm/PcmPack: simplify unpack_sample()

This commit is contained in:
Max Kellermann 2017-04-22 09:28:54 +02:00
parent 95b62a843a
commit 424e9cbc43

View File

@ -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;
}
}