/* vim:set ts=2 sw=2 sts=2 et: */ /** * \author Marcus Holland-Moritz (github@mhxnet.de) * \copyright Copyright (c) Marcus Holland-Moritz * * This file is part of dwarfs. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the “Software”), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * SPDX-License-Identifier: MIT */ #include #include #include #include #include #include namespace dwarfs { namespace { // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) template class basic_pcm_sample_transformer { public: using uint_type = std::make_unsigned_t; template static constexpr void unpack(UnpackedType* dst, uint8_t const* src, int bits) { uint_type tmp; if constexpr (End == pcm_sample_endianness::Big) { if constexpr (Bytes == 1) { tmp = (static_cast(src[0]) << 0); } if constexpr (Bytes == 2) { tmp = (static_cast(src[0]) << 8) | (static_cast(src[1]) << 0); } if constexpr (Bytes == 3) { tmp = (static_cast(src[0]) << 16) | (static_cast(src[1]) << 8) | (static_cast(src[2]) << 0); } if constexpr (Bytes == 4) { tmp = (static_cast(src[0]) << 24) | (static_cast(src[1]) << 16) | (static_cast(src[2]) << 8) | (static_cast(src[3]) << 0); } } else { if constexpr (Bytes == 1) { tmp = (static_cast(src[0]) << 0); } if constexpr (Bytes == 2) { tmp = (static_cast(src[0]) << 0) | (static_cast(src[1]) << 8); } if constexpr (Bytes == 3) { tmp = (static_cast(src[0]) << 0) | (static_cast(src[1]) << 8) | (static_cast(src[2]) << 16); } if constexpr (Bytes == 4) { tmp = (static_cast(src[0]) << 0) | (static_cast(src[1]) << 8) | (static_cast(src[2]) << 16) | (static_cast(src[3]) << 24); } } *dst = unpack_native(tmp, bits); } template static constexpr void pack(uint8_t* dst, UnpackedType const* src, int bits) { auto tmp = pack_native(*src, bits); if constexpr (End == pcm_sample_endianness::Big) { if constexpr (Bytes == 1) { dst[0] = static_cast((tmp >> 0) & 0xFF); } if constexpr (Bytes == 2) { dst[0] = static_cast((tmp >> 8) & 0xFF); dst[1] = static_cast((tmp >> 0) & 0xFF); } if constexpr (Bytes == 3) { dst[0] = static_cast((tmp >> 16) & 0xFF); dst[1] = static_cast((tmp >> 8) & 0xFF); dst[2] = static_cast((tmp >> 0) & 0xFF); } if constexpr (Bytes == 4) { dst[0] = static_cast((tmp >> 24) & 0xFF); dst[1] = static_cast((tmp >> 16) & 0xFF); dst[2] = static_cast((tmp >> 8) & 0xFF); dst[3] = static_cast((tmp >> 0) & 0xFF); } } else { if constexpr (Bytes == 1) { dst[0] = static_cast((tmp >> 0) & 0xFF); } if constexpr (Bytes == 2) { dst[0] = static_cast((tmp >> 0) & 0xFF); dst[1] = static_cast((tmp >> 8) & 0xFF); } if constexpr (Bytes == 3) { dst[0] = static_cast((tmp >> 0) & 0xFF); dst[1] = static_cast((tmp >> 8) & 0xFF); dst[2] = static_cast((tmp >> 16) & 0xFF); } if constexpr (Bytes == 4) { dst[0] = static_cast((tmp >> 0) & 0xFF); dst[1] = static_cast((tmp >> 8) & 0xFF); dst[2] = static_cast((tmp >> 16) & 0xFF); dst[3] = static_cast((tmp >> 24) & 0xFF); } } } private: template static constexpr UnpackedType unpack_native(uint_type src, int bits) { if constexpr (Pad == pcm_sample_padding::Lsb) { src >>= (8 * Bytes - bits); } if constexpr (Sig == pcm_sample_signedness::Signed) { if (bits < 8 * static_cast(sizeof(uint_type))) { if (src & (1 << (bits - 1))) { src |= (~static_cast(0)) << bits; } } return static_cast(src); } else { return static_cast(src) - (1 << (bits - 1)); } } template static constexpr uint_type pack_native(UnpackedType src, int bits) { if constexpr (Sig == pcm_sample_signedness::Unsigned) { src += (1 << (bits - 1)); } if constexpr (Pad == pcm_sample_padding::Lsb) { return static_cast(src << (8 * Bytes - bits)); } else { return static_cast(src); } } }; // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) template class pcm_sample_transformer_fixed final : public pcm_sample_transformer::impl { public: using basic_transformer = basic_pcm_sample_transformer; void unpack(std::span dst, std::span src) const override { assert(Bytes * dst.size() == src.size()); for (size_t i = 0; i < dst.size(); ++i) { basic_transformer::template unpack( &dst[i], &src[Bytes * i], Bits); } } void pack(std::span dst, std::span src) const override { assert(dst.size() == Bytes * src.size()); for (size_t i = 0; i < src.size(); ++i) { basic_transformer::template pack(&dst[Bytes * i], &src[i], Bits); } } }; template class pcm_sample_transformer_generic final : public pcm_sample_transformer::impl { public: using basic_transformer = basic_pcm_sample_transformer; explicit pcm_sample_transformer_generic(int bits) : bits_{bits} {} void unpack(std::span dst, std::span src) const override { assert(Bytes * dst.size() == src.size()); for (size_t i = 0; i < dst.size(); ++i) { basic_transformer::template unpack( &dst[i], &src[Bytes * i], bits_); } } void pack(std::span dst, std::span src) const override { assert(dst.size() == Bytes * src.size()); for (size_t i = 0; i < src.size(); ++i) { basic_transformer::template pack(&dst[Bytes * i], &src[i], bits_); } } private: int bits_; }; template std::unique_ptr::impl> make_pcm_sample_transformer(int bits) { static_assert(1 <= Bytes && Bytes <= 4); if constexpr (Bytes == 1) { if (bits == 8) { return std::make_unique< pcm_sample_transformer_fixed>(); } return std::make_unique< pcm_sample_transformer_generic>(bits); } if constexpr (Bytes == 2) { if (bits == 16) { return std::make_unique< pcm_sample_transformer_fixed>(); } return std::make_unique< pcm_sample_transformer_generic>(bits); } if constexpr (Bytes == 3) { if (bits == 20) { return std::make_unique< pcm_sample_transformer_fixed>(); } if (bits == 24) { return std::make_unique< pcm_sample_transformer_fixed>(); } return std::make_unique< pcm_sample_transformer_generic>(bits); } if constexpr (Bytes == 4) { if (bits == 20) { return std::make_unique< pcm_sample_transformer_fixed>(); } if (bits == 24) { return std::make_unique< pcm_sample_transformer_fixed>(); } if (bits == 32) { return std::make_unique< pcm_sample_transformer_fixed>(); } return std::make_unique< pcm_sample_transformer_generic>(bits); } } template std::unique_ptr::impl> make_pcm_sample_transformer(int bytes, int bits) { switch (bytes) { case 1: return make_pcm_sample_transformer(bits); case 2: return make_pcm_sample_transformer(bits); case 3: return make_pcm_sample_transformer(bits); case 4: return make_pcm_sample_transformer(bits); default: throw std::runtime_error( fmt::format("unsupported number of bytes per sample: {}", bytes)); } } template std::unique_ptr::impl> make_pcm_sample_transformer(pcm_sample_padding pad, int bytes, int bits) { switch (pad) { case pcm_sample_padding::Lsb: return make_pcm_sample_transformer(bytes, bits); case pcm_sample_padding::Msb: return make_pcm_sample_transformer(bytes, bits); } folly::assume_unreachable(); } template std::unique_ptr::impl> make_pcm_sample_transformer(pcm_sample_signedness sig, pcm_sample_padding pad, int bytes, int bits) { switch (sig) { case pcm_sample_signedness::Signed: return make_pcm_sample_transformer( pad, bytes, bits); case pcm_sample_signedness::Unsigned: return make_pcm_sample_transformer( pad, bytes, bits); } folly::assume_unreachable(); } template std::unique_ptr::impl> make_pcm_sample_transformer(pcm_sample_endianness end, pcm_sample_signedness sig, pcm_sample_padding pad, int bytes, int bits) { assert(bits <= 8 * bytes); switch (end) { case pcm_sample_endianness::Big: return make_pcm_sample_transformer(sig, pad, bytes, bits); case pcm_sample_endianness::Little: return make_pcm_sample_transformer( sig, pad, bytes, bits); } folly::assume_unreachable(); } } // namespace template pcm_sample_transformer::pcm_sample_transformer( pcm_sample_endianness end, pcm_sample_signedness sig, pcm_sample_padding pad, int bytes, int bits) : impl_{make_pcm_sample_transformer(end, sig, pad, bytes, bits)} {} template class pcm_sample_transformer; std::ostream& operator<<(std::ostream& os, pcm_sample_endianness e) { os << (e == pcm_sample_endianness::Big ? "big-endian" : "little-endian"); return os; } std::ostream& operator<<(std::ostream& os, pcm_sample_signedness s) { os << (s == pcm_sample_signedness::Signed ? "signed" : "unsigned"); return os; } std::ostream& operator<<(std::ostream& os, pcm_sample_padding p) { os << (p == pcm_sample_padding::Lsb ? "lsb-padded" : "msb-padded"); return os; } } // namespace dwarfs