From f48f65e7e738a48cc47dc1a9887d236c3c4a6614 Mon Sep 17 00:00:00 2001 From: Victor Gaydov Date: Thu, 30 Nov 2023 13:42:43 +0400 Subject: [PATCH] gh-547 Make PcmFormat enum instead of struct Instead of a struct with code + endian, use one enum with all combinations, to simplify the user code. --- .fmtignore | 2 +- SConstruct | 2 + .../roc_audio/pcm_decoder.cpp | 6 +- src/internal_modules/roc_audio/pcm_decoder.h | 7 +- .../roc_audio/pcm_encoder.cpp | 6 +- src/internal_modules/roc_audio/pcm_encoder.h | 7 +- src/internal_modules/roc_audio/pcm_format.cpp | 39 - src/internal_modules/roc_audio/pcm_format.h | 293 +- .../{pcm_funcs.h => pcm_format_funcs.cpp} | 2608 +++++++++++------ .../{pcm_funcs_gen.py => pcm_format_gen.py} | 255 +- src/internal_modules/roc_audio/pcm_mapper.cpp | 37 +- src/internal_modules/roc_audio/pcm_mapper.h | 18 +- .../roc_audio/print_supported.cpp | 98 +- src/internal_modules/roc_audio/sample.cpp | 3 +- src/internal_modules/roc_audio/sample.h | 12 +- .../roc_audio/sample_format.h | 36 + src/internal_modules/roc_rtp/encoding.h | 6 +- src/internal_modules/roc_rtp/encoding_map.cpp | 4 +- src/public_api/src/context.cpp | 3 +- src/tests/roc_audio/test_depacketizer.cpp | 2 +- .../roc_audio/test_frame_encoder_decoder.cpp | 16 +- src/tests/roc_audio/test_packetizer.cpp | 2 +- src/tests/roc_audio/test_pcm_mapper.cpp | 92 +- src/tests/roc_audio/test_pcm_samples.cpp | 27 +- .../test_samples/generate_samples.py | 11 +- .../roc_audio/test_samples/pcm_float32_be.h | 3 +- .../roc_audio/test_samples/pcm_float32_le.h | 3 +- .../roc_audio/test_samples/pcm_sint16_be.h | 3 +- .../roc_audio/test_samples/pcm_sint16_le.h | 3 +- .../roc_audio/test_samples/pcm_sint24_be.h | 3 +- .../roc_audio/test_samples/pcm_sint24_le.h | 3 +- .../roc_audio/test_samples/pcm_sint32_be.h | 3 +- .../roc_audio/test_samples/pcm_sint32_le.h | 3 +- .../roc_audio/test_samples/pcm_sint8_be.h | 3 +- .../roc_audio/test_samples/pcm_sint8_le.h | 3 +- .../roc_audio/test_samples/pcm_uint16_be.h | 3 +- .../roc_audio/test_samples/pcm_uint16_le.h | 3 +- .../roc_audio/test_samples/pcm_uint24_be.h | 3 +- .../roc_audio/test_samples/pcm_uint24_le.h | 3 +- .../roc_audio/test_samples/pcm_uint32_be.h | 3 +- .../roc_audio/test_samples/pcm_uint32_le.h | 3 +- .../roc_audio/test_samples/pcm_uint8_be.h | 3 +- .../roc_audio/test_samples/pcm_uint8_le.h | 3 +- .../roc_audio/test_samples/sample_info.h | 3 +- src/tests/roc_rtp/test_encoding_map.cpp | 15 +- src/tests/roc_rtp/test_populator.cpp | 4 +- 46 files changed, 2186 insertions(+), 1482 deletions(-) delete mode 100644 src/internal_modules/roc_audio/pcm_format.cpp rename src/internal_modules/roc_audio/{pcm_funcs.h => pcm_format_funcs.cpp} (86%) rename src/internal_modules/roc_audio/{pcm_funcs_gen.py => pcm_format_gen.py} (83%) create mode 100644 src/internal_modules/roc_audio/sample_format.h diff --git a/.fmtignore b/.fmtignore index c3aa6cd63..0697b2843 100644 --- a/.fmtignore +++ b/.fmtignore @@ -1,4 +1,4 @@ -src/internal_modules/roc_audio/pcm_funcs.h +src/internal_modules/roc_audio/pcm_format.cpp src/internal_modules/roc_core/target_libatomic_ops/roc_core/atomic_ops.h src/tests/roc_audio/test_samples/*.h src/tests/roc_rtp/test_packets/*.h diff --git a/SConstruct b/SConstruct index 8826ffc82..b699a5c71 100644 --- a/SConstruct +++ b/SConstruct @@ -1012,6 +1012,7 @@ if meta.compiler == 'gcc': '-Wno-shadow', '-Wno-stringop-overflow', '-Wno-system-headers', + '-Wno-unused-const-variable', ]}) env.Append(CXXFLAGS=[ @@ -1061,6 +1062,7 @@ if meta.compiler == 'clang': '-Wno-psabi', '-Wno-shadow', '-Wno-system-headers', + '-Wno-unused-const-variable', ]}) env.Append(CXXFLAGS=[ diff --git a/src/internal_modules/roc_audio/pcm_decoder.cpp b/src/internal_modules/roc_audio/pcm_decoder.cpp index 962dc8a85..6d2b4a26e 100644 --- a/src/internal_modules/roc_audio/pcm_decoder.cpp +++ b/src/internal_modules/roc_audio/pcm_decoder.cpp @@ -13,13 +13,13 @@ namespace roc { namespace audio { IFrameDecoder* PcmDecoder::construct(core::IArena& arena, - const PcmFormat& pcm_format, + PcmFormat pcm_format, const SampleSpec& sample_spec) { return new (arena) PcmDecoder(pcm_format, sample_spec); } -PcmDecoder::PcmDecoder(const PcmFormat& pcm_format, const SampleSpec& sample_spec) - : pcm_mapper_(pcm_format, SampleFormat) +PcmDecoder::PcmDecoder(PcmFormat pcm_format, const SampleSpec& sample_spec) + : pcm_mapper_(pcm_format, SampleFmt) , n_chans_(sample_spec.num_channels()) , stream_pos_(0) , stream_avail_(0) diff --git a/src/internal_modules/roc_audio/pcm_decoder.h b/src/internal_modules/roc_audio/pcm_decoder.h index 9912c3d77..4c2cce33f 100644 --- a/src/internal_modules/roc_audio/pcm_decoder.h +++ b/src/internal_modules/roc_audio/pcm_decoder.h @@ -24,12 +24,11 @@ namespace audio { class PcmDecoder : public IFrameDecoder, public core::NonCopyable<> { public: //! Construction function. - static IFrameDecoder* construct(core::IArena& arena, - const PcmFormat& pcm_format, - const SampleSpec& sample_spec); + static IFrameDecoder* + construct(core::IArena& arena, PcmFormat pcm_format, const SampleSpec& sample_spec); //! Initialize. - PcmDecoder(const PcmFormat& pcm_format, const SampleSpec& sample_spec); + PcmDecoder(PcmFormat pcm_format, const SampleSpec& sample_spec); //! Get current stream position. virtual packet::stream_timestamp_t position() const; diff --git a/src/internal_modules/roc_audio/pcm_encoder.cpp b/src/internal_modules/roc_audio/pcm_encoder.cpp index 7ed835271..880d38834 100644 --- a/src/internal_modules/roc_audio/pcm_encoder.cpp +++ b/src/internal_modules/roc_audio/pcm_encoder.cpp @@ -13,13 +13,13 @@ namespace roc { namespace audio { IFrameEncoder* PcmEncoder::construct(core::IArena& arena, - const PcmFormat& pcm_format, + PcmFormat pcm_format, const SampleSpec& sample_spec) { return new (arena) PcmEncoder(pcm_format, sample_spec); } -PcmEncoder::PcmEncoder(const PcmFormat& pcm_format, const SampleSpec& sample_spec) - : pcm_mapper_(SampleFormat, pcm_format) +PcmEncoder::PcmEncoder(PcmFormat pcm_format, const SampleSpec& sample_spec) + : pcm_mapper_(SampleFmt, pcm_format) , n_chans_(sample_spec.num_channels()) , frame_data_(NULL) , frame_byte_size_(0) diff --git a/src/internal_modules/roc_audio/pcm_encoder.h b/src/internal_modules/roc_audio/pcm_encoder.h index 416e0dcb3..de83cebaf 100644 --- a/src/internal_modules/roc_audio/pcm_encoder.h +++ b/src/internal_modules/roc_audio/pcm_encoder.h @@ -24,12 +24,11 @@ namespace audio { class PcmEncoder : public IFrameEncoder, public core::NonCopyable<> { public: //! Construction function. - static IFrameEncoder* construct(core::IArena& arena, - const PcmFormat& pcm_format, - const SampleSpec& sample_spec); + static IFrameEncoder* + construct(core::IArena& arena, PcmFormat pcm_format, const SampleSpec& sample_spec); //! Initialize. - PcmEncoder(const PcmFormat& pcm_format, const SampleSpec& sample_spec); + PcmEncoder(PcmFormat pcm_format, const SampleSpec& sample_spec); //! Get encoded frame size in bytes for given number of samples per channel. virtual size_t encoded_byte_count(size_t num_samples) const; diff --git a/src/internal_modules/roc_audio/pcm_format.cpp b/src/internal_modules/roc_audio/pcm_format.cpp deleted file mode 100644 index 6e71622f9..000000000 --- a/src/internal_modules/roc_audio/pcm_format.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2023 Roc Streaming authors - * - * 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/. - */ - -#include "roc_audio/pcm_format.h" -#include "roc_audio/pcm_funcs.h" -#include "roc_core/panic.h" - -namespace roc { -namespace audio { - -const char* pcm_format_to_str(const PcmFormat& fmt) { - return pcm_to_str(fmt.code, fmt.endian); -} - -bool pcm_format_parse(const char* str, PcmFormat& fmt) { - if (!str) { - roc_panic("pcm: string is null"); - } - return pcm_from_str(str, fmt.code, fmt.endian); -} - -PcmTraits pcm_format_traits(const PcmFormat& fmt) { - PcmTraits traits; - - traits.bit_depth = pcm_bit_depth(fmt.code); - traits.bit_width = pcm_bit_width(fmt.code); - traits.is_integer = pcm_is_integer(fmt.code); - traits.is_signed = pcm_is_signed(fmt.code); - - return traits; -} - -} // namespace audio -} // namespace roc diff --git a/src/internal_modules/roc_audio/pcm_format.h b/src/internal_modules/roc_audio/pcm_format.h index 72058c5db..eaacf63d1 100644 --- a/src/internal_modules/roc_audio/pcm_format.h +++ b/src/internal_modules/roc_audio/pcm_format.h @@ -12,114 +12,239 @@ #ifndef ROC_AUDIO_PCM_FORMAT_H_ #define ROC_AUDIO_PCM_FORMAT_H_ -#include "roc_core/attributes.h" #include "roc_core/stddefs.h" namespace roc { namespace audio { -//! PCM sample binary code. -enum PcmCode { - PcmCode_SInt8, //!< 8-bit signed integer. - PcmCode_UInt8, //!< 8-bit unsigned integer. - PcmCode_SInt16, //!< 16-bit signed integer. - PcmCode_UInt16, //!< 16-bit unsigned integer. - PcmCode_SInt18, //!< 18-bit signed integer (2.25 bytes). - PcmCode_UInt18, //!< 18-bit unsigned integer (2.25 bytes). - PcmCode_SInt18_3, //!< 18-bit signed integer, in low bits of 3-byte container. - PcmCode_UInt18_3, //!< 18-bit unsigned integer, in low bits of 3-byte container. - PcmCode_SInt18_4, //!< 18-bit signed integer, in low bits of 4-byte container. - PcmCode_UInt18_4, //!< 18-bit unsigned integer, in low bits of 4-byte container. - PcmCode_SInt20, //!< 20-bit signed integer (2.5 bytes). - PcmCode_UInt20, //!< 20-bit unsigned integer (2.5 bytes). - PcmCode_SInt20_3, //!< 20-bit signed integer, in low bits of 3-byte container. - PcmCode_UInt20_3, //!< 20-bit unsigned integer, in low bits of 3-byte container. - PcmCode_SInt20_4, //!< 20-bit signed integer, in low bits of 4-byte container. - PcmCode_UInt20_4, //!< 20-bit unsigned integer, in low bits of 4-byte container. - PcmCode_SInt24, //!< 24-bit signed integer (3 bytes). - PcmCode_UInt24, //!< 24-bit unsigned integer (3 bytes). - PcmCode_SInt24_4, //!< 24-bit signed integer, in low bits of 4-byte container. - PcmCode_UInt24_4, //!< 24-bit unsigned integer, in low bits of 4-byte container. - PcmCode_SInt32, //!< 32-bit signed integer. - PcmCode_UInt32, //!< 32-bit unsigned integer. - PcmCode_SInt64, //!< 64-bit signed integer. - PcmCode_UInt64, //!< 64-bit unsigned integer. - PcmCode_Float32, //!< 32-bit IEEE-754 float in range [-1.0; +1.0]. - PcmCode_Float64, //!< 64-bit IEEE-754 float in range [-1.0; +1.0]. - - PcmCode_Max //!< Maximum value. +//! PCM format. +//! Defines PCM sample coding and endian. +enum PcmFormat { + //! Invalid format. + PcmFormat_Invalid, + + //! 8-bit signed integer, native endian. + PcmFormat_SInt8, + //! 8-bit signed integer, big endian. + PcmFormat_SInt8_Be, + //! 8-bit signed integer, little endian. + PcmFormat_SInt8_Le, + //! 8-bit unsigned integer, native endian. + PcmFormat_UInt8, + //! 8-bit unsigned integer, big endian. + PcmFormat_UInt8_Be, + //! 8-bit unsigned integer, little endian. + PcmFormat_UInt8_Le, + + //! 16-bit signed integer, native endian. + PcmFormat_SInt16, + //! 16-bit signed integer, big endian. + PcmFormat_SInt16_Be, + //! 16-bit signed integer, little endian. + PcmFormat_SInt16_Le, + //! 16-bit unsigned integer, native endian. + PcmFormat_UInt16, + //! 16-bit unsigned integer, big endian. + PcmFormat_UInt16_Be, + //! 16-bit unsigned integer, little endian. + PcmFormat_UInt16_Le, + + //! 18-bit signed integer (2.25 bytes), native endian. + PcmFormat_SInt18, + //! 18-bit signed integer (2.25 bytes), big endian. + PcmFormat_SInt18_Be, + //! 18-bit signed integer (2.25 bytes), little endian. + PcmFormat_SInt18_Le, + //! 18-bit unsigned integer (2.25 bytes), native endian. + PcmFormat_UInt18, + //! 18-bit unsigned integer (2.25 bytes), big endian. + PcmFormat_UInt18_Be, + //! 18-bit unsigned integer (2.25 bytes), little endian. + PcmFormat_UInt18_Le, + + //! 18-bit signed integer, in low bits of 3-byte container, native endian. + PcmFormat_SInt18_3, + //! 18-bit signed integer, in low bits of 3-byte container, big endian. + PcmFormat_SInt18_3_Be, + //! 18-bit signed integer, in low bits of 3-byte container, little endian. + PcmFormat_SInt18_3_Le, + //! 18-bit unsigned integer, in low bits of 3-byte container, native endian. + PcmFormat_UInt18_3, + //! 18-bit unsigned integer, in low bits of 3-byte container, big endian. + PcmFormat_UInt18_3_Be, + //! 18-bit unsigned integer, in low bits of 3-byte container, little endian. + PcmFormat_UInt18_3_Le, + + //! 18-bit signed integer, in low bits of 4-byte container, native endian. + PcmFormat_SInt18_4, + //! 18-bit signed integer, in low bits of 4-byte container, big endian. + PcmFormat_SInt18_4_Be, + //! 18-bit signed integer, in low bits of 4-byte container, little endian. + PcmFormat_SInt18_4_Le, + //! 18-bit unsigned integer, in low bits of 4-byte container, native endian. + PcmFormat_UInt18_4, + //! 18-bit unsigned integer, in low bits of 4-byte container, big endian. + PcmFormat_UInt18_4_Be, + //! 18-bit unsigned integer, in low bits of 4-byte container, little endian. + PcmFormat_UInt18_4_Le, + + //! 20-bit signed integer (2.5 bytes), native endian. + PcmFormat_SInt20, + //! 20-bit signed integer (2.5 bytes), big endian. + PcmFormat_SInt20_Be, + //! 20-bit signed integer (2.5 bytes), little endian. + PcmFormat_SInt20_Le, + //! 20-bit unsigned integer (2.5 bytes), native endian. + PcmFormat_UInt20, + //! 20-bit unsigned integer (2.5 bytes), big endian. + PcmFormat_UInt20_Be, + //! 20-bit unsigned integer (2.5 bytes), little endian. + PcmFormat_UInt20_Le, + + //! 20-bit signed integer, in low bits of 3-byte container, native endian. + PcmFormat_SInt20_3, + //! 20-bit signed integer, in low bits of 3-byte container, big endian. + PcmFormat_SInt20_3_Be, + //! 20-bit signed integer, in low bits of 3-byte container, little endian. + PcmFormat_SInt20_3_Le, + //! 20-bit unsigned integer, in low bits of 3-byte container, native endian. + PcmFormat_UInt20_3, + //! 20-bit unsigned integer, in low bits of 3-byte container, big endian. + PcmFormat_UInt20_3_Be, + //! 20-bit unsigned integer, in low bits of 3-byte container, little endian. + PcmFormat_UInt20_3_Le, + + //! 20-bit signed integer, in low bits of 4-byte container, native endian. + PcmFormat_SInt20_4, + //! 20-bit signed integer, in low bits of 4-byte container, big endian. + PcmFormat_SInt20_4_Be, + //! 20-bit signed integer, in low bits of 4-byte container, little endian. + PcmFormat_SInt20_4_Le, + //! 20-bit unsigned integer, in low bits of 4-byte container, native endian. + PcmFormat_UInt20_4, + //! 20-bit unsigned integer, in low bits of 4-byte container, big endian. + PcmFormat_UInt20_4_Be, + //! 20-bit unsigned integer, in low bits of 4-byte container, little endian. + PcmFormat_UInt20_4_Le, + + //! 24-bit signed integer (3 bytes), native endian. + PcmFormat_SInt24, + //! 24-bit signed integer (3 bytes), big endian. + PcmFormat_SInt24_Be, + //! 24-bit signed integer (3 bytes), little endian. + PcmFormat_SInt24_Le, + //! 24-bit unsigned integer (3 bytes), native endian. + PcmFormat_UInt24, + //! 24-bit unsigned integer (3 bytes), big endian. + PcmFormat_UInt24_Be, + //! 24-bit unsigned integer (3 bytes), little endian. + PcmFormat_UInt24_Le, + + //! 24-bit signed integer, in low bits of 4-byte container, native endian. + PcmFormat_SInt24_4, + //! 24-bit signed integer, in low bits of 4-byte container, big endian. + PcmFormat_SInt24_4_Be, + //! 24-bit signed integer, in low bits of 4-byte container, little endian. + PcmFormat_SInt24_4_Le, + //! 24-bit unsigned integer, in low bits of 4-byte container, native endian. + PcmFormat_UInt24_4, + //! 24-bit unsigned integer, in low bits of 4-byte container, big endian. + PcmFormat_UInt24_4_Be, + //! 24-bit unsigned integer, in low bits of 4-byte container, little endian. + PcmFormat_UInt24_4_Le, + + //! 32-bit signed integer, native endian. + PcmFormat_SInt32, + //! 32-bit signed integer, big endian. + PcmFormat_SInt32_Be, + //! 32-bit signed integer, little endian. + PcmFormat_SInt32_Le, + //! 32-bit unsigned integer, native endian. + PcmFormat_UInt32, + //! 32-bit unsigned integer, big endian. + PcmFormat_UInt32_Be, + //! 32-bit unsigned integer, little endian. + PcmFormat_UInt32_Le, + + //! 64-bit signed integer, native endian. + PcmFormat_SInt64, + //! 64-bit signed integer, big endian. + PcmFormat_SInt64_Be, + //! 64-bit signed integer, little endian. + PcmFormat_SInt64_Le, + //! 64-bit unsigned integer, native endian. + PcmFormat_UInt64, + //! 64-bit unsigned integer, big endian. + PcmFormat_UInt64_Be, + //! 64-bit unsigned integer, little endian. + PcmFormat_UInt64_Le, + + //! 32-bit IEEE-754 float in range [-1.0; +1.0], native endian. + PcmFormat_Float32, + //! 32-bit IEEE-754 float in range [-1.0; +1.0], big endian. + PcmFormat_Float32_Be, + //! 32-bit IEEE-754 float in range [-1.0; +1.0], little endian. + PcmFormat_Float32_Le, + + //! 64-bit IEEE-754 float in range [-1.0; +1.0], native endian. + PcmFormat_Float64, + //! 64-bit IEEE-754 float in range [-1.0; +1.0], big endian. + PcmFormat_Float64_Be, + //! 64-bit IEEE-754 float in range [-1.0; +1.0], little endian. + PcmFormat_Float64_Le, + + //! Maximum enum value. + PcmFormat_Max }; -//! PCM sample endianess. -enum PcmEndian { - PcmEndian_Native, //!< Endian native to current CPU. - PcmEndian_Big, //!< Big endian. - PcmEndian_Little, //!< Little endian. - - PcmEndian_Max //!< Maximum value. -}; - -//! PCM format description. -struct PcmFormat { - //! Sample binary code. - PcmCode code; - - //! Sample endianess. - PcmEndian endian; - - //! Initialize. - PcmFormat() - : code() - , endian() { - } +//! PCM format meta-information. +struct PcmTraits { + //! If traits are valid. + bool is_valid; - //! Initialize. - PcmFormat(PcmCode enc, PcmEndian end) - : code(enc) - , endian(end) { - } + //! True for integers, false for floating point. + bool is_integer; - //! Check two formats for equality. - bool operator==(const PcmFormat& other) const { - return code == other.code && endian == other.endian; - } + //! True for signed integers and floating point. + bool is_signed; - //! Check two formats for equality. - bool operator!=(const PcmFormat& other) const { - return !(*this == other); - } -}; + //! True for little-endian formats. + bool is_little; -//! PCM format meta-information. -struct PcmTraits { //! Number of significant bits per sample. size_t bit_depth; //! Number of total bits per sample in packed form. size_t bit_width; - //! True for integers, false for floating point. - bool is_integer; - - //! True for signed integers and floating point. - bool is_signed; - PcmTraits() - : bit_depth(0) - , bit_width(0) + : is_valid(false) , is_integer(false) - , is_signed(false) { + , is_signed(false) + , is_little(false) + , bit_depth(0) + , bit_width(0) { } }; -//! Get string name of PCM format. -const char* pcm_format_to_str(const PcmFormat& fmt); +//! PCM mapping function. +typedef void (*PcmMapFn)(const uint8_t* in_data, + size_t& in_bit_off, + uint8_t* out_data, + size_t& out_bit_off, + size_t n_samples); -//! Parse PCM format from string name. -ROC_ATTR_NODISCARD bool pcm_format_parse(const char* str, PcmFormat& fmt); +//! Get mapping function for given PCM format pair. +PcmMapFn pcm_format_mapfn(PcmFormat in_format, PcmFormat out_format); + +//! Get format traits for given PCM format. +PcmTraits pcm_format_traits(PcmFormat format); + +//! Get string name of PCM format. +const char* pcm_format_to_str(PcmFormat format); -//! Get traits for PCM format. -PcmTraits pcm_format_traits(const PcmFormat& fmt); +//! Get PCM format from string name. +PcmFormat pcm_format_from_str(const char* str); } // namespace audio } // namespace roc diff --git a/src/internal_modules/roc_audio/pcm_funcs.h b/src/internal_modules/roc_audio/pcm_format_funcs.cpp similarity index 86% rename from src/internal_modules/roc_audio/pcm_funcs.h rename to src/internal_modules/roc_audio/pcm_format_funcs.cpp index fa8d55f9f..caa653223 100644 --- a/src/internal_modules/roc_audio/pcm_funcs.h +++ b/src/internal_modules/roc_audio/pcm_format_funcs.cpp @@ -1,10 +1,7 @@ /* - * THIS FILE IS AUTO-GENERATED USING `pcm_funcs_gen.py'. DO NOT EDIT! + * THIS FILE IS AUTO-GENERATED USING `pcm_format_gen.py'. DO NOT EDIT! */ -#ifndef ROC_AUDIO_PCM_FUNCS_H_ -#define ROC_AUDIO_PCM_FUNCS_H_ - #include "roc_audio/pcm_format.h" #include "roc_core/attributes.h" #include "roc_core/cpu_traits.h" @@ -13,6 +10,47 @@ namespace roc { namespace audio { +namespace { + +// PCM codes. +enum PcmCode { + PcmCode_SInt8, + PcmCode_UInt8, + PcmCode_SInt16, + PcmCode_UInt16, + PcmCode_SInt18, + PcmCode_UInt18, + PcmCode_SInt18_3, + PcmCode_UInt18_3, + PcmCode_SInt18_4, + PcmCode_UInt18_4, + PcmCode_SInt20, + PcmCode_UInt20, + PcmCode_SInt20_3, + PcmCode_UInt20_3, + PcmCode_SInt20_4, + PcmCode_UInt20_4, + PcmCode_SInt24, + PcmCode_UInt24, + PcmCode_SInt24_4, + PcmCode_UInt24_4, + PcmCode_SInt32, + PcmCode_UInt32, + PcmCode_SInt64, + PcmCode_UInt64, + PcmCode_Float32, + PcmCode_Float64, + PcmCode_Max +}; + +// PCM endians. +enum PcmEndian { + PcmEndian_Native, + PcmEndian_Big, + PcmEndian_Little, + PcmEndian_Max +}; + // SInt8 value range const int8_t pcm_sint8_min = -127 - 1; const int8_t pcm_sint8_max = 127; @@ -12719,14 +12757,14 @@ template <> struct pcm_packer { } }; -// Map code and endian of samples -template +// Mapping function implementation +template struct pcm_mapper { - static inline void map(const uint8_t* in_data, - size_t& in_bit_off, - uint8_t* out_data, - size_t& out_bit_off, - size_t n_samples) { + static void map(const uint8_t* in_data, + size_t& in_bit_off, + uint8_t* out_data, + size_t& out_bit_off, + size_t n_samples) { for (size_t n = 0; n < n_samples; n++) { pcm_packer::pack( out_data, out_bit_off, @@ -12736,1246 +12774,1872 @@ struct pcm_mapper { } }; -// Mapping function -typedef void (*pcm_map_func_t)( - const uint8_t* in_data, - size_t& in_bit_off, - uint8_t* out_data, - size_t& out_bit_off, - size_t n_samples); - // Select mapping function -template -pcm_map_func_t pcm_map_func() { - return &pcm_mapper::map; +template +PcmMapFn pcm_format_mapfn() { + return &pcm_mapper::map; } // Select mapping function -template -pcm_map_func_t pcm_map_func(PcmEndian out_endian) { - switch (out_endian) { - case PcmEndian_Native: +template +PcmMapFn pcm_format_mapfn(PcmFormat out_format) { + switch (out_format) { + case PcmFormat_SInt8: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt8_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt8_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt8: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt8_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt8_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt16: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt16_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt16_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt16: #if ROC_CPU_ENDIAN == ROC_CPU_BE - return pcm_map_func(); + return pcm_format_mapfn(); #else - return pcm_map_func(); + return pcm_format_mapfn(); #endif - case PcmEndian_Big: - return pcm_map_func(); - case PcmEndian_Little: - return pcm_map_func(); - case PcmEndian_Max: + case PcmFormat_UInt16_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt16_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt18: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt18_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt18_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt18: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt18_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt18_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt18_3: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt18_3_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt18_3_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt18_3: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt18_3_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt18_3_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt18_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt18_4_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt18_4_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt18_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt18_4_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt18_4_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt20: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt20_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt20_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt20: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt20_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt20_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt20_3: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt20_3_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt20_3_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt20_3: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt20_3_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt20_3_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt20_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt20_4_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt20_4_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt20_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt20_4_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt20_4_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt24: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt24_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt24_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt24: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt24_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt24_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt24_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt24_4_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt24_4_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt24_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt24_4_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt24_4_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt32: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt32_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt32_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt32: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt32_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt32_Le: + return pcm_format_mapfn(); + case PcmFormat_SInt64: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_SInt64_Be: + return pcm_format_mapfn(); + case PcmFormat_SInt64_Le: + return pcm_format_mapfn(); + case PcmFormat_UInt64: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_UInt64_Be: + return pcm_format_mapfn(); + case PcmFormat_UInt64_Le: + return pcm_format_mapfn(); + case PcmFormat_Float32: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_Float32_Be: + return pcm_format_mapfn(); + case PcmFormat_Float32_Le: + return pcm_format_mapfn(); + case PcmFormat_Float64: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(); +#else + return pcm_format_mapfn(); +#endif + case PcmFormat_Float64_Be: + return pcm_format_mapfn(); + case PcmFormat_Float64_Le: + return pcm_format_mapfn(); + default: break; } return NULL; } +} // namespace + // Select mapping function -template -pcm_map_func_t pcm_map_func(PcmEndian in_endian, PcmEndian out_endian) { - switch (in_endian) { - case PcmEndian_Native: +PcmMapFn pcm_format_mapfn(PcmFormat in_format, PcmFormat out_format) { + switch (in_format) { + case PcmFormat_SInt8: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt8_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt8_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt8: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt8_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt8_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt16: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt16_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt16_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt16: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt16_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt16_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt18: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt18_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt18_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt18: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt18_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt18_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt18_3: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt18_3_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt18_3_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt18_3: #if ROC_CPU_ENDIAN == ROC_CPU_BE - return pcm_map_func(out_endian); + return pcm_format_mapfn(out_format); #else - return pcm_map_func(out_endian); + return pcm_format_mapfn(out_format); #endif - case PcmEndian_Big: - return pcm_map_func(out_endian); - case PcmEndian_Little: - return pcm_map_func(out_endian); - case PcmEndian_Max: + case PcmFormat_UInt18_3_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt18_3_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt18_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt18_4_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt18_4_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt18_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt18_4_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt18_4_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt20: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt20_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt20_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt20: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt20_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt20_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt20_3: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt20_3_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt20_3_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt20_3: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt20_3_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt20_3_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt20_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt20_4_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt20_4_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt20_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt20_4_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt20_4_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt24: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt24_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt24_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt24: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt24_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt24_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt24_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt24_4_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt24_4_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt24_4: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt24_4_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt24_4_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt32: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt32_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt32_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt32: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt32_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt32_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt64: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_SInt64_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_SInt64_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt64: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_UInt64_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_UInt64_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_Float32: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_Float32_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_Float32_Le: + return pcm_format_mapfn(out_format); + case PcmFormat_Float64: +#if ROC_CPU_ENDIAN == ROC_CPU_BE + return pcm_format_mapfn(out_format); +#else + return pcm_format_mapfn(out_format); +#endif + case PcmFormat_Float64_Be: + return pcm_format_mapfn(out_format); + case PcmFormat_Float64_Le: + return pcm_format_mapfn(out_format); + default: break; } return NULL; } -// Select mapping function -template -inline pcm_map_func_t pcm_map_func(PcmCode out_code, - PcmEndian in_endian, - PcmEndian out_endian) { - switch (out_code) { - case PcmCode_SInt8: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt8: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt16: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt16: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt18: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt18: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt18_3: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt18_3: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt18_4: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt18_4: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt20: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt20: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt20_3: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt20_3: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt20_4: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt20_4: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt24: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt24: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt24_4: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt24_4: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt32: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt32: - return pcm_map_func(in_endian, out_endian); - case PcmCode_SInt64: - return pcm_map_func(in_endian, out_endian); - case PcmCode_UInt64: - return pcm_map_func(in_endian, out_endian); - case PcmCode_Float32: - return pcm_map_func(in_endian, out_endian); - case PcmCode_Float64: - return pcm_map_func(in_endian, out_endian); - case PcmCode_Max: +// Get format traits +PcmTraits pcm_format_traits(PcmFormat format) { + PcmTraits traits; + + switch (format) { + case PcmFormat_SInt8: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 8; + traits.bit_width = 8; break; - } - return NULL; -} -// Select mapping function -inline pcm_map_func_t pcm_map_func(PcmCode in_code, - PcmCode out_code, - PcmEndian in_endian, - PcmEndian out_endian) { - switch (in_code) { - case PcmCode_SInt8: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt8: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt16: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt16: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt18: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt18: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt18_3: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt18_3: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt18_4: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt18_4: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt20: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt20: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt20_3: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt20_3: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt20_4: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt20_4: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt24: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt24: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt24_4: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt24_4: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt32: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt32: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_SInt64: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_UInt64: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_Float32: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_Float64: - return pcm_map_func(out_code, in_endian, out_endian); - case PcmCode_Max: + case PcmFormat_SInt8_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 8; + traits.bit_width = 8; break; - } - return NULL; -} -// Get number of meaningful bits per sample -inline size_t pcm_bit_depth(PcmCode code) { - switch (code) { - case PcmCode_SInt8: - return 8; - case PcmCode_UInt8: - return 8; - case PcmCode_SInt16: - return 16; - case PcmCode_UInt16: - return 16; - case PcmCode_SInt18: - return 18; - case PcmCode_UInt18: - return 18; - case PcmCode_SInt18_3: - return 18; - case PcmCode_UInt18_3: - return 18; - case PcmCode_SInt18_4: - return 18; - case PcmCode_UInt18_4: - return 18; - case PcmCode_SInt20: - return 20; - case PcmCode_UInt20: - return 20; - case PcmCode_SInt20_3: - return 20; - case PcmCode_UInt20_3: - return 20; - case PcmCode_SInt20_4: - return 20; - case PcmCode_UInt20_4: - return 20; - case PcmCode_SInt24: - return 24; - case PcmCode_UInt24: - return 24; - case PcmCode_SInt24_4: - return 24; - case PcmCode_UInt24_4: - return 24; - case PcmCode_SInt32: - return 32; - case PcmCode_UInt32: - return 32; - case PcmCode_SInt64: - return 64; - case PcmCode_UInt64: - return 64; - case PcmCode_Float32: - return 32; - case PcmCode_Float64: - return 64; - case PcmCode_Max: + case PcmFormat_SInt8_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 8; + traits.bit_width = 8; break; - } - return 0; -} -// Get number of total bits per sample -inline size_t pcm_bit_width(PcmCode code) { - switch (code) { - case PcmCode_SInt8: - return 8; - case PcmCode_UInt8: - return 8; - case PcmCode_SInt16: - return 16; - case PcmCode_UInt16: - return 16; - case PcmCode_SInt18: - return 18; - case PcmCode_UInt18: - return 18; - case PcmCode_SInt18_3: - return 24; - case PcmCode_UInt18_3: - return 24; - case PcmCode_SInt18_4: - return 32; - case PcmCode_UInt18_4: - return 32; - case PcmCode_SInt20: - return 20; - case PcmCode_UInt20: - return 20; - case PcmCode_SInt20_3: - return 24; - case PcmCode_UInt20_3: - return 24; - case PcmCode_SInt20_4: - return 32; - case PcmCode_UInt20_4: - return 32; - case PcmCode_SInt24: - return 24; - case PcmCode_UInt24: - return 24; - case PcmCode_SInt24_4: - return 32; - case PcmCode_UInt24_4: - return 32; - case PcmCode_SInt32: - return 32; - case PcmCode_UInt32: - return 32; - case PcmCode_SInt64: - return 64; - case PcmCode_UInt64: - return 64; - case PcmCode_Float32: - return 32; - case PcmCode_Float64: - return 64; - case PcmCode_Max: + case PcmFormat_UInt8: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 8; + traits.bit_width = 8; break; - } - return 0; -} -// Check if code is integer -inline size_t pcm_is_integer(PcmCode code) { - switch (code) { - case PcmCode_SInt8: - return true; - case PcmCode_UInt8: - return true; - case PcmCode_SInt16: - return true; - case PcmCode_UInt16: - return true; - case PcmCode_SInt18: - return true; - case PcmCode_UInt18: - return true; - case PcmCode_SInt18_3: - return true; - case PcmCode_UInt18_3: - return true; - case PcmCode_SInt18_4: - return true; - case PcmCode_UInt18_4: - return true; - case PcmCode_SInt20: - return true; - case PcmCode_UInt20: - return true; - case PcmCode_SInt20_3: - return true; - case PcmCode_UInt20_3: - return true; - case PcmCode_SInt20_4: - return true; - case PcmCode_UInt20_4: - return true; - case PcmCode_SInt24: - return true; - case PcmCode_UInt24: - return true; - case PcmCode_SInt24_4: - return true; - case PcmCode_UInt24_4: - return true; - case PcmCode_SInt32: - return true; - case PcmCode_UInt32: - return true; - case PcmCode_SInt64: - return true; - case PcmCode_UInt64: - return true; - case PcmCode_Float32: - return false; - case PcmCode_Float64: - return false; - case PcmCode_Max: + case PcmFormat_UInt8_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 8; + traits.bit_width = 8; break; - } - return false; -} -// Check if code is signed -inline size_t pcm_is_signed(PcmCode code) { - switch (code) { - case PcmCode_SInt8: - return true; - case PcmCode_UInt8: - return false; - case PcmCode_SInt16: - return true; - case PcmCode_UInt16: - return false; - case PcmCode_SInt18: - return true; - case PcmCode_UInt18: - return false; - case PcmCode_SInt18_3: - return true; - case PcmCode_UInt18_3: - return false; - case PcmCode_SInt18_4: - return true; - case PcmCode_UInt18_4: - return false; - case PcmCode_SInt20: - return true; - case PcmCode_UInt20: - return false; - case PcmCode_SInt20_3: - return true; - case PcmCode_UInt20_3: - return false; - case PcmCode_SInt20_4: - return true; - case PcmCode_UInt20_4: - return false; - case PcmCode_SInt24: - return true; - case PcmCode_UInt24: - return false; - case PcmCode_SInt24_4: - return true; - case PcmCode_UInt24_4: - return false; - case PcmCode_SInt32: - return true; - case PcmCode_UInt32: - return false; - case PcmCode_SInt64: - return true; - case PcmCode_UInt64: - return false; - case PcmCode_Float32: - return true; - case PcmCode_Float64: - return true; - case PcmCode_Max: + case PcmFormat_UInt8_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 8; + traits.bit_width = 8; break; - } - return false; -} -// Code and endian to string -inline const char* pcm_to_str(PcmCode code, PcmEndian endian) { - switch (code) { - case PcmCode_SInt8: - switch (endian) { - case PcmEndian_Native: - return "s8"; - case PcmEndian_Big: - return "s8_be"; - case PcmEndian_Little: - return "s8_le"; - case PcmEndian_Max: - break; - } + case PcmFormat_SInt16: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 16; + traits.bit_width = 16; break; - case PcmCode_UInt8: - switch (endian) { - case PcmEndian_Native: - return "u8"; - case PcmEndian_Big: - return "u8_be"; - case PcmEndian_Little: - return "u8_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt16_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 16; + traits.bit_width = 16; break; - case PcmCode_SInt16: - switch (endian) { - case PcmEndian_Native: - return "s16"; - case PcmEndian_Big: - return "s16_be"; - case PcmEndian_Little: - return "s16_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt16_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 16; + traits.bit_width = 16; break; - case PcmCode_UInt16: - switch (endian) { - case PcmEndian_Native: - return "u16"; - case PcmEndian_Big: - return "u16_be"; - case PcmEndian_Little: - return "u16_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt16: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 16; + traits.bit_width = 16; break; - case PcmCode_SInt18: - switch (endian) { - case PcmEndian_Native: - return "s18"; - case PcmEndian_Big: - return "s18_be"; - case PcmEndian_Little: - return "s18_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt16_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 16; + traits.bit_width = 16; break; - case PcmCode_UInt18: - switch (endian) { - case PcmEndian_Native: - return "u18"; - case PcmEndian_Big: - return "u18_be"; - case PcmEndian_Little: - return "u18_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt16_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 16; + traits.bit_width = 16; break; - case PcmCode_SInt18_3: - switch (endian) { - case PcmEndian_Native: - return "s18_3"; - case PcmEndian_Big: - return "s18_3be"; - case PcmEndian_Little: - return "s18_3le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 18; + traits.bit_width = 18; break; - case PcmCode_UInt18_3: - switch (endian) { - case PcmEndian_Native: - return "u18_3"; - case PcmEndian_Big: - return "u18_3be"; - case PcmEndian_Little: - return "u18_3le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 18; + traits.bit_width = 18; break; - case PcmCode_SInt18_4: - switch (endian) { - case PcmEndian_Native: - return "s18_4"; - case PcmEndian_Big: - return "s18_4be"; - case PcmEndian_Little: - return "s18_4le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 18; + traits.bit_width = 18; break; - case PcmCode_UInt18_4: - switch (endian) { - case PcmEndian_Native: - return "u18_4"; - case PcmEndian_Big: - return "u18_4be"; - case PcmEndian_Little: - return "u18_4le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 18; + traits.bit_width = 18; break; - case PcmCode_SInt20: - switch (endian) { - case PcmEndian_Native: - return "s20"; - case PcmEndian_Big: - return "s20_be"; - case PcmEndian_Little: - return "s20_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 18; + traits.bit_width = 18; break; - case PcmCode_UInt20: - switch (endian) { - case PcmEndian_Native: - return "u20"; - case PcmEndian_Big: - return "u20_be"; - case PcmEndian_Little: - return "u20_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 18; + traits.bit_width = 18; break; - case PcmCode_SInt20_3: - switch (endian) { - case PcmEndian_Native: - return "s20_3"; - case PcmEndian_Big: - return "s20_3be"; - case PcmEndian_Little: - return "s20_3le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18_3: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 18; + traits.bit_width = 24; break; - case PcmCode_UInt20_3: - switch (endian) { - case PcmEndian_Native: - return "u20_3"; - case PcmEndian_Big: - return "u20_3be"; - case PcmEndian_Little: - return "u20_3le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18_3_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 18; + traits.bit_width = 24; break; - case PcmCode_SInt20_4: - switch (endian) { - case PcmEndian_Native: - return "s20_4"; - case PcmEndian_Big: - return "s20_4be"; - case PcmEndian_Little: - return "s20_4le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18_3_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 18; + traits.bit_width = 24; break; - case PcmCode_UInt20_4: - switch (endian) { - case PcmEndian_Native: - return "u20_4"; - case PcmEndian_Big: - return "u20_4be"; - case PcmEndian_Little: - return "u20_4le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18_3: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 18; + traits.bit_width = 24; break; - case PcmCode_SInt24: - switch (endian) { - case PcmEndian_Native: - return "s24"; - case PcmEndian_Big: - return "s24_be"; - case PcmEndian_Little: - return "s24_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18_3_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 18; + traits.bit_width = 24; break; - case PcmCode_UInt24: - switch (endian) { - case PcmEndian_Native: - return "u24"; - case PcmEndian_Big: - return "u24_be"; - case PcmEndian_Little: - return "u24_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18_3_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 18; + traits.bit_width = 24; break; - case PcmCode_SInt24_4: - switch (endian) { - case PcmEndian_Native: - return "s24_4"; - case PcmEndian_Big: - return "s24_4be"; - case PcmEndian_Little: - return "s24_4le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18_4: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 18; + traits.bit_width = 32; break; - case PcmCode_UInt24_4: - switch (endian) { - case PcmEndian_Native: - return "u24_4"; - case PcmEndian_Big: - return "u24_4be"; - case PcmEndian_Little: - return "u24_4le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18_4_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 18; + traits.bit_width = 32; break; - case PcmCode_SInt32: - switch (endian) { - case PcmEndian_Native: - return "s32"; - case PcmEndian_Big: - return "s32_be"; - case PcmEndian_Little: - return "s32_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt18_4_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 18; + traits.bit_width = 32; break; - case PcmCode_UInt32: - switch (endian) { - case PcmEndian_Native: - return "u32"; - case PcmEndian_Big: - return "u32_be"; - case PcmEndian_Little: - return "u32_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18_4: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 18; + traits.bit_width = 32; break; - case PcmCode_SInt64: - switch (endian) { - case PcmEndian_Native: - return "s64"; - case PcmEndian_Big: - return "s64_be"; - case PcmEndian_Little: - return "s64_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18_4_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 18; + traits.bit_width = 32; break; - case PcmCode_UInt64: - switch (endian) { - case PcmEndian_Native: - return "u64"; - case PcmEndian_Big: - return "u64_be"; - case PcmEndian_Little: - return "u64_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_UInt18_4_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 18; + traits.bit_width = 32; break; - case PcmCode_Float32: - switch (endian) { - case PcmEndian_Native: - return "f32"; - case PcmEndian_Big: - return "f32_be"; - case PcmEndian_Little: - return "f32_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt20: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 20; + traits.bit_width = 20; break; - case PcmCode_Float64: - switch (endian) { - case PcmEndian_Native: - return "f64"; - case PcmEndian_Big: - return "f64_be"; - case PcmEndian_Little: - return "f64_le"; - case PcmEndian_Max: - break; - } + + case PcmFormat_SInt20_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 20; + traits.bit_width = 20; + break; + + case PcmFormat_SInt20_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 20; + traits.bit_width = 20; + break; + + case PcmFormat_UInt20: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 20; + traits.bit_width = 20; + break; + + case PcmFormat_UInt20_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 20; + traits.bit_width = 20; + break; + + case PcmFormat_UInt20_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 20; + traits.bit_width = 20; + break; + + case PcmFormat_SInt20_3: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 20; + traits.bit_width = 24; + break; + + case PcmFormat_SInt20_3_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 20; + traits.bit_width = 24; + break; + + case PcmFormat_SInt20_3_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 20; + traits.bit_width = 24; + break; + + case PcmFormat_UInt20_3: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 20; + traits.bit_width = 24; + break; + + case PcmFormat_UInt20_3_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 20; + traits.bit_width = 24; + break; + + case PcmFormat_UInt20_3_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 20; + traits.bit_width = 24; + break; + + case PcmFormat_SInt20_4: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 20; + traits.bit_width = 32; + break; + + case PcmFormat_SInt20_4_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 20; + traits.bit_width = 32; + break; + + case PcmFormat_SInt20_4_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 20; + traits.bit_width = 32; + break; + + case PcmFormat_UInt20_4: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 20; + traits.bit_width = 32; + break; + + case PcmFormat_UInt20_4_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 20; + traits.bit_width = 32; break; - case PcmCode_Max: + + case PcmFormat_UInt20_4_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 20; + traits.bit_width = 32; + break; + + case PcmFormat_SInt24: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 24; + traits.bit_width = 24; + break; + + case PcmFormat_SInt24_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 24; + traits.bit_width = 24; + break; + + case PcmFormat_SInt24_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 24; + traits.bit_width = 24; + break; + + case PcmFormat_UInt24: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 24; + traits.bit_width = 24; + break; + + case PcmFormat_UInt24_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 24; + traits.bit_width = 24; + break; + + case PcmFormat_UInt24_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 24; + traits.bit_width = 24; + break; + + case PcmFormat_SInt24_4: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 24; + traits.bit_width = 32; + break; + + case PcmFormat_SInt24_4_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 24; + traits.bit_width = 32; + break; + + case PcmFormat_SInt24_4_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 24; + traits.bit_width = 32; + break; + + case PcmFormat_UInt24_4: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 24; + traits.bit_width = 32; + break; + + case PcmFormat_UInt24_4_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 24; + traits.bit_width = 32; + break; + + case PcmFormat_UInt24_4_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 24; + traits.bit_width = 32; + break; + + case PcmFormat_SInt32: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_SInt32_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_SInt32_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_UInt32: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_UInt32_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_UInt32_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_SInt64: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + case PcmFormat_SInt64_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + case PcmFormat_SInt64_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + case PcmFormat_UInt64: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + case PcmFormat_UInt64_Be: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = false; + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + case PcmFormat_UInt64_Le: + traits.is_valid = true; + traits.is_integer = true; + traits.is_signed = false; + traits.is_little = true; + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + case PcmFormat_Float32: + traits.is_valid = true; + traits.is_integer = false; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_Float32_Be: + traits.is_valid = true; + traits.is_integer = false; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_Float32_Le: + traits.is_valid = true; + traits.is_integer = false; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 32; + traits.bit_width = 32; + break; + + case PcmFormat_Float64: + traits.is_valid = true; + traits.is_integer = false; + traits.is_signed = true; +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + case PcmFormat_Float64_Be: + traits.is_valid = true; + traits.is_integer = false; + traits.is_signed = true; + traits.is_little = false; + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + case PcmFormat_Float64_Le: + traits.is_valid = true; + traits.is_integer = false; + traits.is_signed = true; + traits.is_little = true; + traits.bit_depth = 64; + traits.bit_width = 64; + break; + + default: + break; + } + + return traits; +} + +const char* pcm_format_to_str(PcmFormat format) { + switch (format) { + case PcmFormat_SInt8: + return "s8"; + case PcmFormat_SInt8_Be: + return "s8_be"; + case PcmFormat_SInt8_Le: + return "s8_le"; + case PcmFormat_UInt8: + return "u8"; + case PcmFormat_UInt8_Be: + return "u8_be"; + case PcmFormat_UInt8_Le: + return "u8_le"; + case PcmFormat_SInt16: + return "s16"; + case PcmFormat_SInt16_Be: + return "s16_be"; + case PcmFormat_SInt16_Le: + return "s16_le"; + case PcmFormat_UInt16: + return "u16"; + case PcmFormat_UInt16_Be: + return "u16_be"; + case PcmFormat_UInt16_Le: + return "u16_le"; + case PcmFormat_SInt18: + return "s18"; + case PcmFormat_SInt18_Be: + return "s18_be"; + case PcmFormat_SInt18_Le: + return "s18_le"; + case PcmFormat_UInt18: + return "u18"; + case PcmFormat_UInt18_Be: + return "u18_be"; + case PcmFormat_UInt18_Le: + return "u18_le"; + case PcmFormat_SInt18_3: + return "s18_3"; + case PcmFormat_SInt18_3_Be: + return "s18_3be"; + case PcmFormat_SInt18_3_Le: + return "s18_3le"; + case PcmFormat_UInt18_3: + return "u18_3"; + case PcmFormat_UInt18_3_Be: + return "u18_3be"; + case PcmFormat_UInt18_3_Le: + return "u18_3le"; + case PcmFormat_SInt18_4: + return "s18_4"; + case PcmFormat_SInt18_4_Be: + return "s18_4be"; + case PcmFormat_SInt18_4_Le: + return "s18_4le"; + case PcmFormat_UInt18_4: + return "u18_4"; + case PcmFormat_UInt18_4_Be: + return "u18_4be"; + case PcmFormat_UInt18_4_Le: + return "u18_4le"; + case PcmFormat_SInt20: + return "s20"; + case PcmFormat_SInt20_Be: + return "s20_be"; + case PcmFormat_SInt20_Le: + return "s20_le"; + case PcmFormat_UInt20: + return "u20"; + case PcmFormat_UInt20_Be: + return "u20_be"; + case PcmFormat_UInt20_Le: + return "u20_le"; + case PcmFormat_SInt20_3: + return "s20_3"; + case PcmFormat_SInt20_3_Be: + return "s20_3be"; + case PcmFormat_SInt20_3_Le: + return "s20_3le"; + case PcmFormat_UInt20_3: + return "u20_3"; + case PcmFormat_UInt20_3_Be: + return "u20_3be"; + case PcmFormat_UInt20_3_Le: + return "u20_3le"; + case PcmFormat_SInt20_4: + return "s20_4"; + case PcmFormat_SInt20_4_Be: + return "s20_4be"; + case PcmFormat_SInt20_4_Le: + return "s20_4le"; + case PcmFormat_UInt20_4: + return "u20_4"; + case PcmFormat_UInt20_4_Be: + return "u20_4be"; + case PcmFormat_UInt20_4_Le: + return "u20_4le"; + case PcmFormat_SInt24: + return "s24"; + case PcmFormat_SInt24_Be: + return "s24_be"; + case PcmFormat_SInt24_Le: + return "s24_le"; + case PcmFormat_UInt24: + return "u24"; + case PcmFormat_UInt24_Be: + return "u24_be"; + case PcmFormat_UInt24_Le: + return "u24_le"; + case PcmFormat_SInt24_4: + return "s24_4"; + case PcmFormat_SInt24_4_Be: + return "s24_4be"; + case PcmFormat_SInt24_4_Le: + return "s24_4le"; + case PcmFormat_UInt24_4: + return "u24_4"; + case PcmFormat_UInt24_4_Be: + return "u24_4be"; + case PcmFormat_UInt24_4_Le: + return "u24_4le"; + case PcmFormat_SInt32: + return "s32"; + case PcmFormat_SInt32_Be: + return "s32_be"; + case PcmFormat_SInt32_Le: + return "s32_le"; + case PcmFormat_UInt32: + return "u32"; + case PcmFormat_UInt32_Be: + return "u32_be"; + case PcmFormat_UInt32_Le: + return "u32_le"; + case PcmFormat_SInt64: + return "s64"; + case PcmFormat_SInt64_Be: + return "s64_be"; + case PcmFormat_SInt64_Le: + return "s64_le"; + case PcmFormat_UInt64: + return "u64"; + case PcmFormat_UInt64_Be: + return "u64_be"; + case PcmFormat_UInt64_Le: + return "u64_le"; + case PcmFormat_Float32: + return "f32"; + case PcmFormat_Float32_Be: + return "f32_be"; + case PcmFormat_Float32_Le: + return "f32_le"; + case PcmFormat_Float64: + return "f64"; + case PcmFormat_Float64_Be: + return "f64_be"; + case PcmFormat_Float64_Le: + return "f64_le"; + default: break; } return NULL; } -// Code and endian from string -inline bool pcm_from_str(const char* str, PcmCode& code, PcmEndian& endian) { +PcmFormat pcm_format_from_str(const char* str) { + if (!str) { + return PcmFormat_Invalid; + } if (str[0] == 'f') { if (str[1] == '3') { if (str[2] == '2') { if (strcmp(str, "f32") == 0) { - code = PcmCode_Float32; - endian = PcmEndian_Native; - return true; + return PcmFormat_Float32; } if (strcmp(str, "f32_be") == 0) { - code = PcmCode_Float32; - endian = PcmEndian_Big; - return true; + return PcmFormat_Float32_Be; } if (strcmp(str, "f32_le") == 0) { - code = PcmCode_Float32; - endian = PcmEndian_Little; - return true; + return PcmFormat_Float32_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '6') { if (str[2] == '4') { if (strcmp(str, "f64") == 0) { - code = PcmCode_Float64; - endian = PcmEndian_Native; - return true; + return PcmFormat_Float64; } if (strcmp(str, "f64_be") == 0) { - code = PcmCode_Float64; - endian = PcmEndian_Big; - return true; + return PcmFormat_Float64_Be; } if (strcmp(str, "f64_le") == 0) { - code = PcmCode_Float64; - endian = PcmEndian_Little; - return true; + return PcmFormat_Float64_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[0] == 's') { if (str[1] == '1') { if (str[2] == '6') { if (strcmp(str, "s16") == 0) { - code = PcmCode_SInt16; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt16; } if (strcmp(str, "s16_be") == 0) { - code = PcmCode_SInt16; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt16_Be; } if (strcmp(str, "s16_le") == 0) { - code = PcmCode_SInt16; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt16_Le; } - return false; + return PcmFormat_Invalid; } if (str[2] == '8') { if (strcmp(str, "s18") == 0) { - code = PcmCode_SInt18; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt18; } if (strcmp(str, "s18_be") == 0) { - code = PcmCode_SInt18; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt18_Be; } if (strcmp(str, "s18_le") == 0) { - code = PcmCode_SInt18; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt18_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "s18_3") == 0) { - code = PcmCode_SInt18_3; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt18_3; } if (strcmp(str, "s18_3be") == 0) { - code = PcmCode_SInt18_3; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt18_3_Be; } if (strcmp(str, "s18_3le") == 0) { - code = PcmCode_SInt18_3; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt18_3_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "s18_4") == 0) { - code = PcmCode_SInt18_4; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt18_4; } if (strcmp(str, "s18_4be") == 0) { - code = PcmCode_SInt18_4; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt18_4_Be; } if (strcmp(str, "s18_4le") == 0) { - code = PcmCode_SInt18_4; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt18_4_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '2') { if (str[2] == '0') { if (strcmp(str, "s20") == 0) { - code = PcmCode_SInt20; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt20; } if (strcmp(str, "s20_be") == 0) { - code = PcmCode_SInt20; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt20_Be; } if (strcmp(str, "s20_le") == 0) { - code = PcmCode_SInt20; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt20_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "s20_3") == 0) { - code = PcmCode_SInt20_3; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt20_3; } if (strcmp(str, "s20_3be") == 0) { - code = PcmCode_SInt20_3; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt20_3_Be; } if (strcmp(str, "s20_3le") == 0) { - code = PcmCode_SInt20_3; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt20_3_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "s20_4") == 0) { - code = PcmCode_SInt20_4; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt20_4; } if (strcmp(str, "s20_4be") == 0) { - code = PcmCode_SInt20_4; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt20_4_Be; } if (strcmp(str, "s20_4le") == 0) { - code = PcmCode_SInt20_4; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt20_4_Le; } - return false; + return PcmFormat_Invalid; } if (str[2] == '4') { if (strcmp(str, "s24") == 0) { - code = PcmCode_SInt24; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt24; } if (strcmp(str, "s24_be") == 0) { - code = PcmCode_SInt24; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt24_Be; } if (strcmp(str, "s24_le") == 0) { - code = PcmCode_SInt24; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt24_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "s24_4") == 0) { - code = PcmCode_SInt24_4; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt24_4; } if (strcmp(str, "s24_4be") == 0) { - code = PcmCode_SInt24_4; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt24_4_Be; } if (strcmp(str, "s24_4le") == 0) { - code = PcmCode_SInt24_4; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt24_4_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '3') { if (str[2] == '2') { if (strcmp(str, "s32") == 0) { - code = PcmCode_SInt32; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt32; } if (strcmp(str, "s32_be") == 0) { - code = PcmCode_SInt32; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt32_Be; } if (strcmp(str, "s32_le") == 0) { - code = PcmCode_SInt32; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt32_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '6') { if (str[2] == '4') { if (strcmp(str, "s64") == 0) { - code = PcmCode_SInt64; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt64; } if (strcmp(str, "s64_be") == 0) { - code = PcmCode_SInt64; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt64_Be; } if (strcmp(str, "s64_le") == 0) { - code = PcmCode_SInt64; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt64_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '8') { if (strcmp(str, "s8") == 0) { - code = PcmCode_SInt8; - endian = PcmEndian_Native; - return true; + return PcmFormat_SInt8; } if (strcmp(str, "s8_be") == 0) { - code = PcmCode_SInt8; - endian = PcmEndian_Big; - return true; + return PcmFormat_SInt8_Be; } if (strcmp(str, "s8_le") == 0) { - code = PcmCode_SInt8; - endian = PcmEndian_Little; - return true; + return PcmFormat_SInt8_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[0] == 'u') { if (str[1] == '1') { if (str[2] == '6') { if (strcmp(str, "u16") == 0) { - code = PcmCode_UInt16; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt16; } if (strcmp(str, "u16_be") == 0) { - code = PcmCode_UInt16; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt16_Be; } if (strcmp(str, "u16_le") == 0) { - code = PcmCode_UInt16; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt16_Le; } - return false; + return PcmFormat_Invalid; } if (str[2] == '8') { if (strcmp(str, "u18") == 0) { - code = PcmCode_UInt18; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt18; } if (strcmp(str, "u18_be") == 0) { - code = PcmCode_UInt18; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt18_Be; } if (strcmp(str, "u18_le") == 0) { - code = PcmCode_UInt18; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt18_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "u18_3") == 0) { - code = PcmCode_UInt18_3; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt18_3; } if (strcmp(str, "u18_3be") == 0) { - code = PcmCode_UInt18_3; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt18_3_Be; } if (strcmp(str, "u18_3le") == 0) { - code = PcmCode_UInt18_3; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt18_3_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "u18_4") == 0) { - code = PcmCode_UInt18_4; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt18_4; } if (strcmp(str, "u18_4be") == 0) { - code = PcmCode_UInt18_4; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt18_4_Be; } if (strcmp(str, "u18_4le") == 0) { - code = PcmCode_UInt18_4; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt18_4_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '2') { if (str[2] == '0') { if (strcmp(str, "u20") == 0) { - code = PcmCode_UInt20; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt20; } if (strcmp(str, "u20_be") == 0) { - code = PcmCode_UInt20; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt20_Be; } if (strcmp(str, "u20_le") == 0) { - code = PcmCode_UInt20; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt20_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "u20_3") == 0) { - code = PcmCode_UInt20_3; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt20_3; } if (strcmp(str, "u20_3be") == 0) { - code = PcmCode_UInt20_3; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt20_3_Be; } if (strcmp(str, "u20_3le") == 0) { - code = PcmCode_UInt20_3; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt20_3_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "u20_4") == 0) { - code = PcmCode_UInt20_4; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt20_4; } if (strcmp(str, "u20_4be") == 0) { - code = PcmCode_UInt20_4; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt20_4_Be; } if (strcmp(str, "u20_4le") == 0) { - code = PcmCode_UInt20_4; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt20_4_Le; } - return false; + return PcmFormat_Invalid; } if (str[2] == '4') { if (strcmp(str, "u24") == 0) { - code = PcmCode_UInt24; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt24; } if (strcmp(str, "u24_be") == 0) { - code = PcmCode_UInt24; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt24_Be; } if (strcmp(str, "u24_le") == 0) { - code = PcmCode_UInt24; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt24_Le; } - return false; + return PcmFormat_Invalid; if (strcmp(str, "u24_4") == 0) { - code = PcmCode_UInt24_4; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt24_4; } if (strcmp(str, "u24_4be") == 0) { - code = PcmCode_UInt24_4; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt24_4_Be; } if (strcmp(str, "u24_4le") == 0) { - code = PcmCode_UInt24_4; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt24_4_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '3') { if (str[2] == '2') { if (strcmp(str, "u32") == 0) { - code = PcmCode_UInt32; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt32; } if (strcmp(str, "u32_be") == 0) { - code = PcmCode_UInt32; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt32_Be; } if (strcmp(str, "u32_le") == 0) { - code = PcmCode_UInt32; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt32_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '6') { if (str[2] == '4') { if (strcmp(str, "u64") == 0) { - code = PcmCode_UInt64; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt64; } if (strcmp(str, "u64_be") == 0) { - code = PcmCode_UInt64; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt64_Be; } if (strcmp(str, "u64_le") == 0) { - code = PcmCode_UInt64; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt64_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } if (str[1] == '8') { if (strcmp(str, "u8") == 0) { - code = PcmCode_UInt8; - endian = PcmEndian_Native; - return true; + return PcmFormat_UInt8; } if (strcmp(str, "u8_be") == 0) { - code = PcmCode_UInt8; - endian = PcmEndian_Big; - return true; + return PcmFormat_UInt8_Be; } if (strcmp(str, "u8_le") == 0) { - code = PcmCode_UInt8; - endian = PcmEndian_Little; - return true; + return PcmFormat_UInt8_Le; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } - return false; + return PcmFormat_Invalid; } } // namespace audio } // namespace roc - -#endif // ROC_AUDIO_PCM_FUNCS_H_ diff --git a/src/internal_modules/roc_audio/pcm_funcs_gen.py b/src/internal_modules/roc_audio/pcm_format_gen.py similarity index 83% rename from src/internal_modules/roc_audio/pcm_funcs_gen.py rename to src/internal_modules/roc_audio/pcm_format_gen.py index a8508e766..afecfc5ad 100755 --- a/src/internal_modules/roc_audio/pcm_funcs_gen.py +++ b/src/internal_modules/roc_audio/pcm_format_gen.py @@ -87,12 +87,28 @@ def compute_octets(code): return significant_octets, packed_octets, unpacked_octets -# generate short name for pcm code + endianess +# generate enum name for pcm code + endian # e.g.: -# sint18_3, native => s18_3 -# sint18_3, little => s18_3le -# sint18_3, big => s18_3be -def short_name(code, endian): +# SInt18_3, Native => PcmFormat_SInt18_3 +# SInt18_3, Little => PcmFormat_SInt18_3_Le +# SInt18_3, Big => PcmFormat_SInt18_3_Be +def make_enum_name(code, endian): + name = code['code'] + + if endian != 'Native': + if endian == 'Little': + name += '_Le' + if endian == 'Big': + name += '_Be' + + return 'PcmFormat_' + name + +# generate string name for pcm code + endian +# e.g.: +# SInt18_3, Native => s18_3 +# SInt18_3, Little => s18_3le +# SInt18_3, Big => s18_3be +def make_str_name(code, endian): name = code['short_name'] if endian != 'Native': @@ -500,12 +516,9 @@ def nth_chars(codes, prefix=()): template = env.from_string(''' /* - * THIS FILE IS AUTO-GENERATED USING `pcm_funcs_gen.py'. DO NOT EDIT! + * THIS FILE IS AUTO-GENERATED USING `pcm_format_gen.py'. DO NOT EDIT! */ -#ifndef ROC_AUDIO_PCM_FUNCS_H_ -#define ROC_AUDIO_PCM_FUNCS_H_ - #include "roc_audio/pcm_format.h" #include "roc_core/attributes.h" #include "roc_core/cpu_traits.h" @@ -514,6 +527,24 @@ def nth_chars(codes, prefix=()): namespace roc { namespace audio { +namespace { + +// PCM codes. +enum PcmCode { +{% for code in CODES %} + PcmCode_{{ code.code }}, +{% endfor %} + PcmCode_Max +}; + +// PCM endians. +enum PcmEndian { +{% for endian in ENDIANS %} + PcmEndian_{{ endian }}, +{% endfor %} + PcmEndian_Max +}; + {% for code in CODES %} {% if code.is_integer %} // {{ code.code }} value range @@ -795,14 +826,14 @@ def nth_chars(codes, prefix=()): {% endfor %} {% endfor %} -// Map code and endian of samples -template +// Mapping function implementation +template struct pcm_mapper { - static inline void map(const uint8_t* in_data, - size_t& in_bit_off, - uint8_t* out_data, - size_t& out_bit_off, - size_t n_samples) { + static void map(const uint8_t* in_data, + size_t& in_bit_off, + uint8_t* out_data, + size_t& out_bit_off, + size_t n_samples) { for (size_t n = 0; n < n_samples; n++) { pcm_packer::pack( out_data, out_bit_off, @@ -812,172 +843,112 @@ def nth_chars(codes, prefix=()): } }; -// Mapping function -typedef void (*pcm_map_func_t)( - const uint8_t* in_data, - size_t& in_bit_off, - uint8_t* out_data, - size_t& out_bit_off, - size_t n_samples); - // Select mapping function -template -pcm_map_func_t pcm_map_func() { - return &pcm_mapper::map; +template +PcmMapFn pcm_format_mapfn() { + return &pcm_mapper::map; } // Select mapping function -template -pcm_map_func_t pcm_map_func(PcmEndian out_endian) { - switch (out_endian) { +template +PcmMapFn pcm_format_mapfn(PcmFormat out_format) { + switch (out_format) { +{% for code in CODES %} {% for endian in ENDIANS %} - case PcmEndian_{{ endian }}: + case {{ make_enum_name(code, endian) }}: {% if endian == 'Native' %} #if ROC_CPU_ENDIAN == ROC_CPU_BE - return pcm_map_func(); + return pcm_format_mapfn(); #else - return pcm_map_func(); + return pcm_format_mapfn(); #endif {% else %} - return pcm_map_func(); + return pcm_format_mapfn(); {% endif %} {% endfor %} - case PcmEndian_Max: +{% endfor %} + default: break; } return NULL; } +} // namespace + // Select mapping function -template -pcm_map_func_t pcm_map_func(PcmEndian in_endian, PcmEndian out_endian) { - switch (in_endian) { +PcmMapFn pcm_format_mapfn(PcmFormat in_format, PcmFormat out_format) { + switch (in_format) { +{% for code in CODES %} {% for endian in ENDIANS %} - case PcmEndian_{{ endian }}: + case {{ make_enum_name(code, endian) }}: {% if endian == 'Native' %} #if ROC_CPU_ENDIAN == ROC_CPU_BE - return pcm_map_func(out_endian); + return pcm_format_mapfn(out_format); #else - return pcm_map_func(out_endian); + return pcm_format_mapfn(out_format); #endif {% else %} - return pcm_map_func(out_endian); + return pcm_format_mapfn(out_format); {% endif %} {% endfor %} - case PcmEndian_Max: - break; - } - return NULL; -} - -// Select mapping function -template -inline pcm_map_func_t pcm_map_func(PcmCode out_code, - PcmEndian in_endian, - PcmEndian out_endian) { - switch (out_code) { -{% for code in CODES %} - case PcmCode_{{ code.code }}: - return pcm_map_func(in_endian, out_endian); {% endfor %} - case PcmCode_Max: + default: break; } return NULL; } -// Select mapping function -inline pcm_map_func_t pcm_map_func(PcmCode in_code, - PcmCode out_code, - PcmEndian in_endian, - PcmEndian out_endian) { - switch (in_code) { -{% for code in CODES %} - case PcmCode_{{ code.code }}: - return pcm_map_func(out_code, \ -in_endian, out_endian); -{% endfor %} - case PcmCode_Max: - break; - } - return NULL; -} +// Get format traits +PcmTraits pcm_format_traits(PcmFormat format) { + PcmTraits traits; -// Get number of meaningful bits per sample -inline size_t pcm_bit_depth(PcmCode code) { - switch (code) { + switch (format) { {% for code in CODES %} - case PcmCode_{{ code.code }}: - return {{ code.width }}; -{% endfor %} - case PcmCode_Max: +{% for endian in ENDIANS %} + case {{ make_enum_name(code, endian) }}: + traits.is_valid = true; + traits.is_integer = {{ str(code.is_integer).lower() }}; + traits.is_signed = {{ str(code.is_signed).lower() }}; +{% if endian == 'Native' %} +#if ROC_CPU_ENDIAN == ROC_CPU_BE + traits.is_little = false; +#else + traits.is_little = true; +#endif +{% else %} + traits.is_little = {{ str(endian == 'Little').lower() }}; +{% endif %} + traits.bit_depth = {{ code.width }}; + traits.bit_width = {{ code.packed_width }}; break; - } - return 0; -} -// Get number of total bits per sample -inline size_t pcm_bit_width(PcmCode code) { - switch (code) { -{% for code in CODES %} - case PcmCode_{{ code.code }}: - return {{ code.packed_width }}; {% endfor %} - case PcmCode_Max: - break; - } - return 0; -} - -// Check if code is integer -inline size_t pcm_is_integer(PcmCode code) { - switch (code) { -{% for code in CODES %} - case PcmCode_{{ code.code }}: - return {{ str(code.is_integer).lower() }}; {% endfor %} - case PcmCode_Max: + default: break; } - return false; -} -// Check if code is signed -inline size_t pcm_is_signed(PcmCode code) { - switch (code) { -{% for code in CODES %} - case PcmCode_{{ code.code }}: - return {{ str(code.is_signed).lower() }}; -{% endfor %} - case PcmCode_Max: - break; - } - return false; + return traits; } -// Code and endian to string -inline const char* pcm_to_str(PcmCode code, PcmEndian endian) { - switch (code) { +const char* pcm_format_to_str(PcmFormat format) { + switch (format) { {% for code in CODES %} - case PcmCode_{{ code.code }}: - switch (endian) { {% for endian in ENDIANS %} - case PcmEndian_{{ endian }}: - return "{{ short_name(code, endian) }}"; + case {{ make_enum_name(code, endian) }}: + return "{{ make_str_name(code, endian) }}"; {% endfor %} - case PcmEndian_Max: - break; - } - break; {% endfor %} - case PcmCode_Max: + default: break; } return NULL; } -// Code and endian from string -inline bool pcm_from_str(const char* str, PcmCode& code, PcmEndian& endian) { +PcmFormat pcm_format_from_str(const char* str) { + if (!str) { + return PcmFormat_Invalid; + } {% for c0 in nth_chars(CODES) %} if (str[0] == '{{ c0 }}') { {% for c1 in nth_chars(CODES, (c0,)) %} @@ -987,13 +958,11 @@ def nth_chars(codes, prefix=()): {% for code in CODES %} {% if tuple(code.short_name[:3]) == (c0, c1, c2) %} {% for endian in ENDIANS %} - if (strcmp(str, "{{ short_name(code, endian) }}") == 0) { - code = PcmCode_{{ code.code }}; - endian = PcmEndian_{{ endian }}; - return true; + if (strcmp(str, "{{ make_str_name(code, endian) }}") == 0) { + return {{ make_enum_name(code, endian) }}; } {% endfor %} - return false; + return PcmFormat_Invalid; {% endif %} {% endfor %} } @@ -1001,27 +970,23 @@ def nth_chars(codes, prefix=()): {% for code in CODES %} {% if tuple(code.short_name) == (c0, c1) %} {% for endian in ENDIANS %} - if (strcmp(str, "{{ short_name(code, endian) }}") == 0) { - code = PcmCode_{{ code.code }}; - endian = PcmEndian_{{ endian }}; - return true; + if (strcmp(str, "{{ make_str_name(code, endian) }}") == 0) { + return {{ make_enum_name(code, endian) }}; } {% endfor %} {% endif %} {% endfor %} - return false; + return PcmFormat_Invalid; } {% endfor %} - return false; + return PcmFormat_Invalid; } {% endfor %} - return false; + return PcmFormat_Invalid; } } // namespace audio } // namespace roc - -#endif // ROC_AUDIO_PCM_FUNCS_H_ '''.strip()) text = template.render( @@ -1030,5 +995,5 @@ def nth_chars(codes, prefix=()): os.chdir(os.path.dirname(os.path.abspath(__file__))) -with open('pcm_funcs.h', 'w') as fp: +with open('pcm_format.cpp', 'w') as fp: print(text, file=fp) diff --git a/src/internal_modules/roc_audio/pcm_mapper.cpp b/src/internal_modules/roc_audio/pcm_mapper.cpp index 7d863ba1c..70d966db6 100644 --- a/src/internal_modules/roc_audio/pcm_mapper.cpp +++ b/src/internal_modules/roc_audio/pcm_mapper.cpp @@ -7,55 +7,59 @@ */ #include "roc_audio/pcm_mapper.h" -#include "roc_audio/pcm_funcs.h" #include "roc_core/panic.h" #include "roc_core/stddefs.h" namespace roc { namespace audio { -PcmMapper::PcmMapper(const PcmFormat& input_fmt, const PcmFormat& output_fmt) +PcmMapper::PcmMapper(PcmFormat input_fmt, PcmFormat output_fmt) : input_fmt_(input_fmt) , output_fmt_(output_fmt) - , input_sample_bits_(pcm_bit_width(input_fmt.code)) - , output_sample_bits_(pcm_bit_width(output_fmt.code)) - , map_func_(pcm_map_func( - input_fmt_.code, output_fmt_.code, input_fmt_.endian, output_fmt_.endian)) { + , input_traits_(pcm_format_traits(input_fmt)) + , output_traits_(pcm_format_traits(output_fmt)) + , map_func_(pcm_format_mapfn(input_fmt, output_fmt)) { + if (!input_traits_.is_valid) { + roc_panic("pcm mapper: input format is not a pcm format"); + } + if (!output_traits_.is_valid) { + roc_panic("pcm mapper: output format is not a pcm format"); + } if (!map_func_) { roc_panic("pcm mapper: unable to select mapping function"); } } -const PcmFormat& PcmMapper::input_format() const { +PcmFormat PcmMapper::input_format() const { return input_fmt_; } -const PcmFormat& PcmMapper::output_format() const { +PcmFormat PcmMapper::output_format() const { return output_fmt_; } size_t PcmMapper::input_sample_count(size_t input_bytes) const { - return input_bytes * 8 / input_sample_bits_; + return input_bytes * 8 / input_traits_.bit_width; } size_t PcmMapper::output_sample_count(size_t output_bytes) const { - return output_bytes * 8 / output_sample_bits_; + return output_bytes * 8 / output_traits_.bit_width; } size_t PcmMapper::input_byte_count(size_t input_samples) const { - return (input_samples * input_sample_bits_ + 7) / 8; + return (input_samples * input_traits_.bit_width + 7) / 8; } size_t PcmMapper::output_byte_count(size_t output_samples) const { - return (output_samples * output_sample_bits_ + 7) / 8; + return (output_samples * output_traits_.bit_width + 7) / 8; } size_t PcmMapper::input_bit_count(size_t input_samples) const { - return input_samples * input_sample_bits_; + return input_samples * input_traits_.bit_width; } size_t PcmMapper::output_bit_count(size_t output_samples) const { - return output_samples * output_sample_bits_; + return output_samples * output_traits_.bit_width; } size_t PcmMapper::map(const void* in_data, @@ -73,9 +77,10 @@ size_t PcmMapper::map(const void* in_data, roc_panic_if_msg(out_bit_off > out_byte_size * 8, "pcm mapper: output offset out of bounds"); - n_samples = std::min(n_samples, (in_byte_size * 8 - in_bit_off) / input_sample_bits_); n_samples = - std::min(n_samples, (out_byte_size * 8 - out_bit_off) / output_sample_bits_); + std::min(n_samples, (in_byte_size * 8 - in_bit_off) / input_traits_.bit_width); + n_samples = + std::min(n_samples, (out_byte_size * 8 - out_bit_off) / output_traits_.bit_width); if (n_samples != 0) { map_func_((const uint8_t*)in_data, in_bit_off, (uint8_t*)out_data, out_bit_off, diff --git a/src/internal_modules/roc_audio/pcm_mapper.h b/src/internal_modules/roc_audio/pcm_mapper.h index 7c450e64b..f3b768886 100644 --- a/src/internal_modules/roc_audio/pcm_mapper.h +++ b/src/internal_modules/roc_audio/pcm_mapper.h @@ -24,13 +24,15 @@ namespace audio { class PcmMapper : public core::NonCopyable<> { public: //! Initialize. - PcmMapper(const PcmFormat& input_fmt, const PcmFormat& output_fmt); + //! @pre + //! @p input_fmt and @p output_fmt should be PCM formats. + PcmMapper(PcmFormat input_fmt, PcmFormat output_fmt); //! Get input format. - const PcmFormat& input_format() const; + PcmFormat input_format() const; //! Get output format. - const PcmFormat& output_format() const; + PcmFormat output_format() const; //! Get number of input samples per channel for given number of bytes. size_t input_sample_count(size_t input_bytes) const; @@ -76,14 +78,10 @@ class PcmMapper : public core::NonCopyable<> { const PcmFormat input_fmt_; const PcmFormat output_fmt_; - const size_t input_sample_bits_; - const size_t output_sample_bits_; + const PcmTraits input_traits_; + const PcmTraits output_traits_; - void (*const map_func_)(const uint8_t* in_data, - size_t& in_bit_off, - uint8_t* out_data, - size_t& out_bit_off, - size_t n_samples); + PcmMapFn map_func_; }; } // namespace audio diff --git a/src/internal_modules/roc_audio/print_supported.cpp b/src/internal_modules/roc_audio/print_supported.cpp index a65f370d2..22f2b957e 100644 --- a/src/internal_modules/roc_audio/print_supported.cpp +++ b/src/internal_modules/roc_audio/print_supported.cpp @@ -10,6 +10,7 @@ #include "roc_audio/channel_defs.h" #include "roc_audio/channel_tables.h" #include "roc_audio/pcm_format.h" +#include "roc_audio/sample_format.h" #include "roc_core/macro_helpers.h" #include "roc_core/printer.h" @@ -18,32 +19,53 @@ namespace audio { namespace { -void print_pcm_codes(core::Printer& prn) { +void print_pcm_formats(core::Printer& prn) { PcmTraits prev_traits, curr_traits; - for (int code = 0; code < PcmCode_Max; code++) { - for (int endian = 0; endian < PcmEndian_Max; endian++) { - PcmFormat fmt; - fmt.code = (PcmCode)code; - fmt.endian = (PcmEndian)endian; - - curr_traits = pcm_format_traits(fmt); - - if (prev_traits.bit_depth != curr_traits.bit_depth - || prev_traits.bit_width != curr_traits.bit_width) { - if (curr_traits.bit_width % 8 == 0) { - prn.writef("\n %2d bit (%d byte) ", (int)curr_traits.bit_depth, - (int)curr_traits.bit_width / 8); - } else { - prn.writef("\n %d bit (%.2f byte) ", (int)curr_traits.bit_depth, - (double)curr_traits.bit_width / 8.); - } + for (int n = 0; n < PcmFormat_Max; n++) { + const PcmFormat fmt = (PcmFormat)n; + + curr_traits = pcm_format_traits(fmt); + if (!curr_traits.is_valid) { + continue; + } + + if (prev_traits.bit_depth != curr_traits.bit_depth + || prev_traits.bit_width != curr_traits.bit_width) { + if (curr_traits.bit_width % 8 == 0) { + prn.writef("\n %2d bit (%d byte) ", (int)curr_traits.bit_depth, + (int)curr_traits.bit_width / 8); + } else { + prn.writef("\n %d bit (%.2f byte) ", (int)curr_traits.bit_depth, + (double)curr_traits.bit_width / 8.); } + } + + prev_traits = curr_traits; + + prn.writef(" %s", pcm_format_to_str(fmt)); + } +} + +void print_channel_masks(core::Printer& prn) { + for (size_t i = 0; i < ROC_ARRAY_SIZE(ChanMaskNames); i++) { + const ChannelMask ch_mask = ChanMaskNames[i].mask; - prev_traits = curr_traits; + prn.writef(" %-13s (", channel_mask_to_str(ch_mask)); - prn.writef(" %s", pcm_format_to_str(fmt)); + bool first = true; + + for (int ch = 0; ch < ChanPos_Max; ch++) { + if (ch_mask & (1 << ch)) { + if (!first) { + prn.writef(" "); + } + first = false; + prn.writef("%s", channel_pos_to_str((ChannelPosition)ch)); + } } + + prn.writef(")\n"); } } @@ -57,46 +79,20 @@ void print_channel_names(core::Printer& prn) { prn.writef(" low freq LFE\n"); } -void print_channel_mask(core::Printer& prn, ChannelMask ch_mask) { - prn.writef(" %-13s (", channel_mask_to_str(ch_mask)); - - bool first = true; - - for (int ch = 0; ch < ChanPos_Max; ch++) { - if (ch_mask & (1 << ch)) { - if (!first) { - prn.writef(" "); - } - first = false; - prn.writef("%s", channel_pos_to_str((ChannelPosition)ch)); - } - } - - prn.writef(")\n"); -} - } // namespace bool print_supported() { core::Printer prn; - prn.writef("\nsupported formats for audio devices:\n"); - prn.writef(" pcm\n"); - - prn.writef("\nsupported formats for network packets:\n"); - prn.writef(" pcm\n"); + prn.writef("\nsupported pcm formats:"); + print_pcm_formats(prn); - prn.writef("\nsupported pcm codes:"); - print_pcm_codes(prn); + prn.writef("\npre-defined channel layouts:\n"); + print_channel_masks(prn); - prn.writef("\n\nsupported channel names:\n"); + prn.writef("\n\npre-defined channel names:\n"); print_channel_names(prn); - prn.writef("\npre-defined channel masks:\n"); - for (size_t i = 0; i < ROC_ARRAY_SIZE(ChanMaskNames); i++) { - print_channel_mask(prn, ChanMaskNames[i].mask); - } - return true; } diff --git a/src/internal_modules/roc_audio/sample.cpp b/src/internal_modules/roc_audio/sample.cpp index aa095b0c7..95f336033 100644 --- a/src/internal_modules/roc_audio/sample.cpp +++ b/src/internal_modules/roc_audio/sample.cpp @@ -8,8 +8,7 @@ #include "roc_audio/sample.h" -const roc::audio::PcmFormat roc::audio::SampleFormat(roc::audio::PcmCode_Float32, - roc::audio::PcmEndian_Native); +const roc::audio::PcmFormat roc::audio::SampleFmt = roc::audio::PcmFormat_Float32; const roc::audio::sample_t roc::audio::SampleMin = -1; const roc::audio::sample_t roc::audio::SampleMax = 1; diff --git a/src/internal_modules/roc_audio/sample.h b/src/internal_modules/roc_audio/sample.h index 3788174a5..45374fd63 100644 --- a/src/internal_modules/roc_audio/sample.h +++ b/src/internal_modules/roc_audio/sample.h @@ -12,22 +12,22 @@ #ifndef ROC_AUDIO_SAMPLE_H_ #define ROC_AUDIO_SAMPLE_H_ -#include "roc_audio/pcm_format.h" +#include "roc_audio/sample_format.h" #include "roc_core/stddefs.h" namespace roc { namespace audio { -//! Audio sample. +//! Raw audio sample. typedef float sample_t; -//! Sample format description. -extern const PcmFormat SampleFormat; +//! Format description for raw audio samples. +extern const PcmFormat SampleFmt; -//! Minimum possible value of a sample. +//! Minimum possible value of a raw sample. extern const sample_t SampleMin; -//! Maximum possible value of a sample. +//! Maximum possible value of a raw sample. extern const sample_t SampleMax; } // namespace audio diff --git a/src/internal_modules/roc_audio/sample_format.h b/src/internal_modules/roc_audio/sample_format.h new file mode 100644 index 000000000..c7bcd54c1 --- /dev/null +++ b/src/internal_modules/roc_audio/sample_format.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Roc Streaming authors + * + * 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 roc_audio/sample_format.h +//! @brief Sample format. + +#ifndef ROC_AUDIO_SAMPLE_FORMAT_H_ +#define ROC_AUDIO_SAMPLE_FORMAT_H_ + +#include "roc_audio/pcm_format.h" + +namespace roc { +namespace audio { + +//! Sample format. +//! Defines representation of samples in memory. +//! Does not define sample rate and channel set. +enum SampleFormat { + //! Invalid format. + SampleFormat_Invalid, + + //! Interleaved PCM format. + //! What specific PCM coding and endian is used is defined + //! by PcmFormat enum. + SampleFormat_Pcm, +}; + +} // namespace audio +} // namespace roc + +#endif // ROC_AUDIO_SAMPLE_FORMAT_H_ diff --git a/src/internal_modules/roc_rtp/encoding.h b/src/internal_modules/roc_rtp/encoding.h index 00c506cec..e7f51fe0a 100644 --- a/src/internal_modules/roc_rtp/encoding.h +++ b/src/internal_modules/roc_rtp/encoding.h @@ -38,17 +38,19 @@ struct Encoding { //! Create frame encoder. audio::IFrameEncoder* (*new_encoder)(core::IArena& arena, - const audio::PcmFormat& pcm_format, + audio::PcmFormat pcm_format, const audio::SampleSpec& sample_spec); //! Create frame decoder. audio::IFrameDecoder* (*new_decoder)(core::IArena& arena, - const audio::PcmFormat& pcm_format, + audio::PcmFormat pcm_format, const audio::SampleSpec& sample_spec); //! Initialize. Encoding() : payload_type(0) + , sample_spec() + , pcm_format() , packet_flags(0) , new_encoder(NULL) , new_decoder(NULL) { diff --git a/src/internal_modules/roc_rtp/encoding_map.cpp b/src/internal_modules/roc_rtp/encoding_map.cpp index a73d080c0..be8bd5d5f 100644 --- a/src/internal_modules/roc_rtp/encoding_map.cpp +++ b/src/internal_modules/roc_rtp/encoding_map.cpp @@ -20,7 +20,7 @@ EncodingMap::EncodingMap(core::IArena& arena) { Encoding enc; enc.payload_type = PayloadType_L16_Mono; - enc.pcm_format = audio::PcmFormat(audio::PcmCode_SInt16, audio::PcmEndian_Big); + enc.pcm_format = audio::PcmFormat_SInt16_Be; enc.sample_spec = audio::SampleSpec(44100, audio::ChanLayout_Surround, audio::ChanOrder_Smpte, audio::ChanMask_Surround_Mono); @@ -33,7 +33,7 @@ EncodingMap::EncodingMap(core::IArena& arena) { Encoding enc; enc.payload_type = PayloadType_L16_Stereo; - enc.pcm_format = audio::PcmFormat(audio::PcmCode_SInt16, audio::PcmEndian_Big); + enc.pcm_format = audio::PcmFormat_SInt16_Be; enc.sample_spec = audio::SampleSpec(44100, audio::ChanLayout_Surround, audio::ChanOrder_Smpte, audio::ChanMask_Surround_Stereo); diff --git a/src/public_api/src/context.cpp b/src/public_api/src/context.cpp index 6b5ab75dc..8b98d5381 100644 --- a/src/public_api/src/context.cpp +++ b/src/public_api/src/context.cpp @@ -87,8 +87,7 @@ int roc_context_register_encoding(roc_context* context, enc.payload_type = (unsigned)encoding_id; enc.packet_flags = packet::Packet::FlagAudio; - enc.pcm_format.code = audio::PcmCode_SInt16; - enc.pcm_format.endian = audio::PcmEndian_Big; + enc.pcm_format = audio::PcmFormat_SInt16_Be; if (!api::sample_spec_from_user(enc.sample_spec, *encoding)) { roc_log( diff --git a/src/tests/roc_audio/test_depacketizer.cpp b/src/tests/roc_audio/test_depacketizer.cpp index 3333131e1..d8be2b00a 100644 --- a/src/tests/roc_audio/test_depacketizer.cpp +++ b/src/tests/roc_audio/test_depacketizer.cpp @@ -38,7 +38,7 @@ enum { }; const SampleSpec SampleSpecs(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChMask); -const PcmFormat PcmFmt(PcmCode_SInt16, PcmEndian_Big); +const PcmFormat PcmFmt = PcmFormat_SInt16_Be; const core::nanoseconds_t NsPerPacket = SampleSpecs.samples_overall_2_ns(SamplesSize); const core::nanoseconds_t Now = 1691499037871419405; diff --git a/src/tests/roc_audio/test_frame_encoder_decoder.cpp b/src/tests/roc_audio/test_frame_encoder_decoder.cpp index 019a527af..b2ce6b0f2 100644 --- a/src/tests/roc_audio/test_frame_encoder_decoder.cpp +++ b/src/tests/roc_audio/test_frame_encoder_decoder.cpp @@ -50,25 +50,25 @@ IFrameEncoder* new_encoder(size_t id) { switch (id) { case Codec_PCM_SInt16_1ch: return new (arena) - PcmEncoder(PcmFormat(PcmCode_SInt16, PcmEndian_Big), + PcmEncoder(PcmFormat_SInt16_Be, SampleSpec(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChanMask_Surround_Mono)); case Codec_PCM_SInt16_2ch: return new (arena) - PcmEncoder(PcmFormat(PcmCode_SInt16, PcmEndian_Big), + PcmEncoder(PcmFormat_SInt16_Be, SampleSpec(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChanMask_Surround_Stereo)); case Codec_PCM_SInt24_1ch: return new (arena) - PcmEncoder(PcmFormat(PcmCode_SInt24, PcmEndian_Big), + PcmEncoder(PcmFormat_SInt16_Be, SampleSpec(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChanMask_Surround_Mono)); case Codec_PCM_SInt24_2ch: return new (arena) - PcmEncoder(PcmFormat(PcmCode_SInt24, PcmEndian_Big), + PcmEncoder(PcmFormat_SInt16_Be, SampleSpec(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChanMask_Surround_Stereo)); @@ -83,25 +83,25 @@ IFrameDecoder* new_decoder(size_t id) { switch (id) { case Codec_PCM_SInt16_1ch: return new (arena) - PcmDecoder(PcmFormat(PcmCode_SInt16, PcmEndian_Big), + PcmDecoder(PcmFormat_SInt16_Be, SampleSpec(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChanMask_Surround_Mono)); case Codec_PCM_SInt16_2ch: return new (arena) - PcmDecoder(PcmFormat(PcmCode_SInt16, PcmEndian_Big), + PcmDecoder(PcmFormat_SInt16_Be, SampleSpec(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChanMask_Surround_Stereo)); case Codec_PCM_SInt24_1ch: return new (arena) - PcmDecoder(PcmFormat(PcmCode_SInt24, PcmEndian_Big), + PcmDecoder(PcmFormat_SInt16_Be, SampleSpec(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChanMask_Surround_Mono)); case Codec_PCM_SInt24_2ch: return new (arena) - PcmDecoder(PcmFormat(PcmCode_SInt24, PcmEndian_Big), + PcmDecoder(PcmFormat_SInt16_Be, SampleSpec(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChanMask_Surround_Stereo)); diff --git a/src/tests/roc_audio/test_packetizer.cpp b/src/tests/roc_audio/test_packetizer.cpp index 550d0d9c4..371a9cfb6 100644 --- a/src/tests/roc_audio/test_packetizer.cpp +++ b/src/tests/roc_audio/test_packetizer.cpp @@ -44,7 +44,7 @@ const core::nanoseconds_t PacketDuration = SamplesPerPacket * core::Second / Sam const core::nanoseconds_t Now = 1691499037871419405; const SampleSpec SampleSpecs(SampleRate, ChanLayout_Surround, ChanOrder_Smpte, ChMask); -const PcmFormat PcmFmt(PcmCode_SInt16, PcmEndian_Big); +const PcmFormat PcmFmt = PcmFormat_SInt16_Be; core::HeapArena arena; core::BufferFactory sample_buffer_factory(arena, MaxBufSize); diff --git a/src/tests/roc_audio/test_pcm_mapper.cpp b/src/tests/roc_audio/test_pcm_mapper.cpp index a204829a3..ae3ceee7a 100644 --- a/src/tests/roc_audio/test_pcm_mapper.cpp +++ b/src/tests/roc_audio/test_pcm_mapper.cpp @@ -28,18 +28,8 @@ void map(const void* input, size_t in_bytes, size_t out_bytes, size_t n_samples, - PcmCode in_encoding, - PcmCode out_encoding, - PcmEndian in_endian = PcmEndian_Native, - PcmEndian out_endian = PcmEndian_Native) { - PcmFormat in_fmt; - in_fmt.code = in_encoding; - in_fmt.endian = in_endian; - - PcmFormat out_fmt; - out_fmt.code = out_encoding; - out_fmt.endian = out_endian; - + PcmFormat in_fmt, + PcmFormat out_fmt) { PcmMapper mapper(in_fmt, out_fmt); UNSIGNED_LONGS_EQUAL(n_samples, mapper.input_sample_count(in_bytes)); @@ -117,7 +107,7 @@ TEST(pcm_mapper, int16_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt16); + PcmFormat_SInt16, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -135,7 +125,7 @@ TEST(pcm_mapper, int16_to_int8) { int8_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt8); + PcmFormat_SInt16, PcmFormat_SInt8); compare(expected_output, actual_output, NumSamples); } @@ -153,7 +143,7 @@ TEST(pcm_mapper, int8_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt8, PcmCode_SInt16); + PcmFormat_SInt8, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -171,7 +161,7 @@ TEST(pcm_mapper, int16_to_int32) { int32_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt32); + PcmFormat_SInt16, PcmFormat_SInt32); compare(expected_output, actual_output, NumSamples); } @@ -199,7 +189,7 @@ TEST(pcm_mapper, int32_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt32, PcmCode_SInt16); + PcmFormat_SInt32, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -225,7 +215,7 @@ TEST(pcm_mapper, int16_to_int64) { int64_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt64); + PcmFormat_SInt16, PcmFormat_SInt64); compare(expected_output, actual_output, NumSamples); } @@ -251,7 +241,7 @@ TEST(pcm_mapper, int64_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt64, PcmCode_SInt16); + PcmFormat_SInt64, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -269,7 +259,7 @@ TEST(pcm_mapper, int16_to_float32) { float actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_Float32); + PcmFormat_SInt16, PcmFormat_Float32); compare(expected_output, actual_output, NumSamples); } @@ -287,7 +277,7 @@ TEST(pcm_mapper, float32_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_Float32, PcmCode_SInt16); + PcmFormat_Float32, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -317,7 +307,7 @@ TEST(pcm_mapper, int32_to_float32) { float actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt32, PcmCode_Float32); + PcmFormat_SInt32, PcmFormat_Float32); compare(expected_output, actual_output, NumSamples); } @@ -345,7 +335,7 @@ TEST(pcm_mapper, float32_to_int32) { int32_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_Float32, PcmCode_SInt32); + PcmFormat_Float32, PcmFormat_SInt32); compare(expected_output, actual_output, NumSamples); } @@ -375,7 +365,7 @@ TEST(pcm_mapper, int32_to_float64) { double actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt32, PcmCode_Float64); + PcmFormat_SInt32, PcmFormat_Float64); compare(expected_output, actual_output, NumSamples); } @@ -403,7 +393,7 @@ TEST(pcm_mapper, float64_to_int32) { int32_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_Float64, PcmCode_SInt32); + PcmFormat_Float64, PcmFormat_SInt32); compare(expected_output, actual_output, NumSamples); } @@ -425,7 +415,7 @@ TEST(pcm_mapper, uint16_to_uint32) { uint32_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_UInt16, PcmCode_UInt32); + PcmFormat_UInt16, PcmFormat_UInt32); compare(expected_output, actual_output, NumSamples); } @@ -449,7 +439,7 @@ TEST(pcm_mapper, uint32_to_uint16) { uint16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_UInt32, PcmCode_UInt16); + PcmFormat_UInt32, PcmFormat_UInt16); compare(expected_output, actual_output, NumSamples); } @@ -471,7 +461,7 @@ TEST(pcm_mapper, uint16_to_int32) { int32_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_UInt16, PcmCode_SInt32); + PcmFormat_UInt16, PcmFormat_SInt32); compare(expected_output, actual_output, NumSamples); } @@ -495,7 +485,7 @@ TEST(pcm_mapper, int16_to_uint32) { uint32_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_UInt32); + PcmFormat_SInt16, PcmFormat_UInt32); compare(expected_output, actual_output, NumSamples); } @@ -513,7 +503,7 @@ TEST(pcm_mapper, uint32_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_UInt32, PcmCode_SInt16); + PcmFormat_UInt32, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -537,7 +527,7 @@ TEST(pcm_mapper, int32_to_uint16) { uint16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt32, PcmCode_UInt16); + PcmFormat_SInt32, PcmFormat_UInt16); compare(expected_output, actual_output, NumSamples); } @@ -562,7 +552,7 @@ TEST(pcm_mapper, int16_to_int18b4) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt18_4, PcmEndian_Native, PcmEndian_Big); + PcmFormat_SInt16, PcmFormat_SInt18_4_Be); compare(expected_output, actual_output, NumOutputBytes); } @@ -587,7 +577,7 @@ TEST(pcm_mapper, int18b4_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt18_4, PcmCode_SInt16, PcmEndian_Big, PcmEndian_Native); + PcmFormat_SInt18_4_Be, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -612,7 +602,7 @@ TEST(pcm_mapper, int16_to_int20b4) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt20_4, PcmEndian_Native, PcmEndian_Big); + PcmFormat_SInt16, PcmFormat_SInt20_4_Be); compare(expected_output, actual_output, NumOutputBytes); } @@ -634,7 +624,7 @@ TEST(pcm_mapper, int20b4_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt20_4, PcmCode_SInt16, PcmEndian_Big, PcmEndian_Native); + PcmFormat_SInt20_4_Be, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -659,7 +649,7 @@ TEST(pcm_mapper, int16_to_int24b4) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt24_4, PcmEndian_Native, PcmEndian_Big); + PcmFormat_SInt16, PcmFormat_SInt24_4_Be); compare(expected_output, actual_output, NumOutputBytes); } @@ -681,7 +671,7 @@ TEST(pcm_mapper, int24b4_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt24_4, PcmCode_SInt16, PcmEndian_Big, PcmEndian_Native); + PcmFormat_SInt24_4_Be, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -705,7 +695,7 @@ TEST(pcm_mapper, uint24b4_to_int20b4) { uint8_t actual_output[NumBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_UInt24_4, PcmCode_SInt20_4, PcmEndian_Big, PcmEndian_Big); + PcmFormat_UInt24_4_Be, PcmFormat_SInt20_4_Be); compare(expected_output, actual_output, NumBytes); } @@ -729,7 +719,7 @@ TEST(pcm_mapper, int20b4_to_uint24b4) { uint8_t actual_output[NumBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt20_4, PcmCode_UInt24_4, PcmEndian_Big, PcmEndian_Big); + PcmFormat_SInt20_4_Be, PcmFormat_UInt24_4_Be); compare(expected_output, actual_output, NumBytes); } @@ -764,7 +754,7 @@ TEST(pcm_mapper, int32_to_int20) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt32, PcmCode_SInt20, PcmEndian_Native, PcmEndian_Big); + PcmFormat_SInt32, PcmFormat_SInt20_Be); compare(expected_output, actual_output, NumOutputBytes); } @@ -796,7 +786,7 @@ TEST(pcm_mapper, int20_to_int16) { int32_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt20, PcmCode_SInt32, PcmEndian_Big, PcmEndian_Native); + PcmFormat_SInt20_Be, PcmFormat_SInt32); compare(expected_output, actual_output, NumSamples); } @@ -821,7 +811,7 @@ TEST(pcm_mapper, int16_to_int20b3) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt20_3, PcmEndian_Native, PcmEndian_Big); + PcmFormat_SInt16, PcmFormat_SInt20_3_Be); compare(expected_output, actual_output, NumOutputBytes); } @@ -843,7 +833,7 @@ TEST(pcm_mapper, int20b3_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt20_3, PcmCode_SInt16, PcmEndian_Big, PcmEndian_Native); + PcmFormat_SInt20_3_Be, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -868,7 +858,7 @@ TEST(pcm_mapper, int16_to_int24) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt24, PcmEndian_Native, PcmEndian_Big); + PcmFormat_SInt16, PcmFormat_SInt24_Be); compare(expected_output, actual_output, NumOutputBytes); } @@ -890,7 +880,7 @@ TEST(pcm_mapper, int24_to_int16) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt24, PcmCode_SInt16, PcmEndian_Big, PcmEndian_Native); + PcmFormat_SInt24_Be, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -915,7 +905,7 @@ TEST(pcm_mapper, native_to_be) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt16, PcmEndian_Native, PcmEndian_Big); + PcmFormat_SInt16, PcmFormat_SInt16_Be); compare(expected_output, actual_output, NumOutputBytes); } @@ -940,7 +930,7 @@ TEST(pcm_mapper, native_to_le) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt16, PcmEndian_Native, PcmEndian_Little); + PcmFormat_SInt16, PcmFormat_SInt16_Le); compare(expected_output, actual_output, NumOutputBytes); } @@ -962,7 +952,7 @@ TEST(pcm_mapper, be_to_native) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt16, PcmEndian_Big, PcmEndian_Native); + PcmFormat_SInt16_Be, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -984,7 +974,7 @@ TEST(pcm_mapper, le_to_native) { int16_t actual_output[NumSamples] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt16, PcmEndian_Little, PcmEndian_Native); + PcmFormat_SInt16_Le, PcmFormat_SInt16); compare(expected_output, actual_output, NumSamples); } @@ -1013,7 +1003,7 @@ TEST(pcm_mapper, be_to_le) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt16, PcmEndian_Big, PcmEndian_Little); + PcmFormat_SInt16_Be, PcmFormat_SInt16_Le); compare(expected_output, actual_output, NumOutputBytes); } @@ -1042,7 +1032,7 @@ TEST(pcm_mapper, le_to_be) { uint8_t actual_output[NumOutputBytes] = {}; map(input, actual_output, sizeof(input), sizeof(actual_output), NumSamples, - PcmCode_SInt16, PcmCode_SInt16, PcmEndian_Little, PcmEndian_Big); + PcmFormat_SInt16_Le, PcmFormat_SInt16_Be); compare(expected_output, actual_output, NumOutputBytes); } diff --git a/src/tests/roc_audio/test_pcm_samples.cpp b/src/tests/roc_audio/test_pcm_samples.cpp index 1e96704f8..fa2a0d694 100644 --- a/src/tests/roc_audio/test_pcm_samples.cpp +++ b/src/tests/roc_audio/test_pcm_samples.cpp @@ -58,13 +58,8 @@ TEST(pcm_samples, decode) { for (size_t idx = 0; idx < ROC_ARRAY_SIZE(test_samples); idx++) { roc_log(LogDebug, "mapping %s to native", test_samples[idx]->name); - PcmFormat in_fmt; - in_fmt.code = test_samples[idx]->encoding; - in_fmt.endian = test_samples[idx]->endian; - - PcmFormat out_fmt; - out_fmt.code = PcmCode_Float64; - out_fmt.endian = PcmEndian_Native; + PcmFormat in_fmt = test_samples[idx]->format; + PcmFormat out_fmt = PcmFormat_Float64; PcmMapper mapper(in_fmt, out_fmt); @@ -109,13 +104,8 @@ TEST(pcm_samples, recode) { roc_log(LogDebug, "mapping %s to %s", test_samples[idx1]->name, test_samples[idx2]->name); - PcmFormat in_fmt; - in_fmt.code = test_samples[idx1]->encoding; - in_fmt.endian = test_samples[idx1]->endian; - - PcmFormat out_fmt; - out_fmt.code = test_samples[idx2]->encoding; - out_fmt.endian = test_samples[idx2]->endian; + PcmFormat in_fmt = test_samples[idx1]->format; + PcmFormat out_fmt = test_samples[idx2]->format; PcmMapper mapper(in_fmt, out_fmt); @@ -154,13 +144,8 @@ TEST(pcm_samples, recode) { { roc_log(LogDebug, "mapping %s to native", test_samples[idx2]->name); - PcmFormat in_fmt; - in_fmt.code = test_samples[idx2]->encoding; - in_fmt.endian = test_samples[idx2]->endian; - - PcmFormat out_fmt; - out_fmt.code = PcmCode_Float64; - out_fmt.endian = PcmEndian_Native; + PcmFormat in_fmt = test_samples[idx2]->format; + PcmFormat out_fmt = PcmFormat_Float64; PcmMapper mapper(in_fmt, out_fmt); diff --git a/src/tests/roc_audio/test_samples/generate_samples.py b/src/tests/roc_audio/test_samples/generate_samples.py index 79577525f..a40c1387b 100755 --- a/src/tests/roc_audio/test_samples/generate_samples.py +++ b/src/tests/roc_audio/test_samples/generate_samples.py @@ -3,6 +3,10 @@ import os.path import subprocess +def format_name(encoding, endian): + return 'PcmFormat_' + \ + encoding['pcm_encoding'] + '_' + endian['pcm_endian'] + def format_array(array, maxlen=8, indent=1): if len(array) > maxlen: ret = "{\n" @@ -89,8 +93,7 @@ def generate_header(encoding, endian, name, datfile, rawfile, hdrfile): static SampleInfo sample_{name} = {{ /* name */ "{name}", - /* encoding */ PcmCode_{encoding['pcm_encoding']}, - /* endian */ PcmEndian_{endian['pcm_endian']}, + /* format */ {format_name(encoding, endian)}, /* num_samples */ {len(samples)}, /* samples */ {format_samples(samples)}, @@ -156,11 +159,11 @@ def generate_header(encoding, endian, name, datfile, rawfile, hdrfile): endians = [ { - 'pcm_endian': 'Big', + 'pcm_endian': 'Be', 'sox_endian': 'big', }, { - 'pcm_endian': 'Little', + 'pcm_endian': 'Le', 'sox_endian': 'little', }, ] diff --git a/src/tests/roc_audio/test_samples/pcm_float32_be.h b/src/tests/roc_audio/test_samples/pcm_float32_be.h index 9fef334eb..636979fc8 100644 --- a/src/tests/roc_audio/test_samples/pcm_float32_be.h +++ b/src/tests/roc_audio/test_samples/pcm_float32_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_float32_be = { /* name */ "pcm_float32_be", - /* encoding */ PcmCode_Float32, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_Float32_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_float32_le.h b/src/tests/roc_audio/test_samples/pcm_float32_le.h index 19a47121e..0917b8b74 100644 --- a/src/tests/roc_audio/test_samples/pcm_float32_le.h +++ b/src/tests/roc_audio/test_samples/pcm_float32_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_float32_le = { /* name */ "pcm_float32_le", - /* encoding */ PcmCode_Float32, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_Float32_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_sint16_be.h b/src/tests/roc_audio/test_samples/pcm_sint16_be.h index baf1e218a..1d0254ebc 100644 --- a/src/tests/roc_audio/test_samples/pcm_sint16_be.h +++ b/src/tests/roc_audio/test_samples/pcm_sint16_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_sint16_be = { /* name */ "pcm_sint16_be", - /* encoding */ PcmCode_SInt16, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_SInt16_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_sint16_le.h b/src/tests/roc_audio/test_samples/pcm_sint16_le.h index 1b43e45df..f54739b45 100644 --- a/src/tests/roc_audio/test_samples/pcm_sint16_le.h +++ b/src/tests/roc_audio/test_samples/pcm_sint16_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_sint16_le = { /* name */ "pcm_sint16_le", - /* encoding */ PcmCode_SInt16, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_SInt16_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_sint24_be.h b/src/tests/roc_audio/test_samples/pcm_sint24_be.h index b322ccdbc..902534906 100644 --- a/src/tests/roc_audio/test_samples/pcm_sint24_be.h +++ b/src/tests/roc_audio/test_samples/pcm_sint24_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_sint24_be = { /* name */ "pcm_sint24_be", - /* encoding */ PcmCode_SInt24, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_SInt24_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_sint24_le.h b/src/tests/roc_audio/test_samples/pcm_sint24_le.h index 172ddd7e7..cdfb84432 100644 --- a/src/tests/roc_audio/test_samples/pcm_sint24_le.h +++ b/src/tests/roc_audio/test_samples/pcm_sint24_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_sint24_le = { /* name */ "pcm_sint24_le", - /* encoding */ PcmCode_SInt24, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_SInt24_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_sint32_be.h b/src/tests/roc_audio/test_samples/pcm_sint32_be.h index abf2bea72..adcc78048 100644 --- a/src/tests/roc_audio/test_samples/pcm_sint32_be.h +++ b/src/tests/roc_audio/test_samples/pcm_sint32_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_sint32_be = { /* name */ "pcm_sint32_be", - /* encoding */ PcmCode_SInt32, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_SInt32_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_sint32_le.h b/src/tests/roc_audio/test_samples/pcm_sint32_le.h index 95abd7f2a..bd7f41413 100644 --- a/src/tests/roc_audio/test_samples/pcm_sint32_le.h +++ b/src/tests/roc_audio/test_samples/pcm_sint32_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_sint32_le = { /* name */ "pcm_sint32_le", - /* encoding */ PcmCode_SInt32, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_SInt32_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_sint8_be.h b/src/tests/roc_audio/test_samples/pcm_sint8_be.h index 61a0b7bec..de57d10b0 100644 --- a/src/tests/roc_audio/test_samples/pcm_sint8_be.h +++ b/src/tests/roc_audio/test_samples/pcm_sint8_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_sint8_be = { /* name */ "pcm_sint8_be", - /* encoding */ PcmCode_SInt8, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_SInt8_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_sint8_le.h b/src/tests/roc_audio/test_samples/pcm_sint8_le.h index 02660d546..6c4de4a68 100644 --- a/src/tests/roc_audio/test_samples/pcm_sint8_le.h +++ b/src/tests/roc_audio/test_samples/pcm_sint8_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_sint8_le = { /* name */ "pcm_sint8_le", - /* encoding */ PcmCode_SInt8, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_SInt8_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_uint16_be.h b/src/tests/roc_audio/test_samples/pcm_uint16_be.h index 32b8ece04..fe3cd6b28 100644 --- a/src/tests/roc_audio/test_samples/pcm_uint16_be.h +++ b/src/tests/roc_audio/test_samples/pcm_uint16_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_uint16_be = { /* name */ "pcm_uint16_be", - /* encoding */ PcmCode_UInt16, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_UInt16_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_uint16_le.h b/src/tests/roc_audio/test_samples/pcm_uint16_le.h index 123cfaa82..ebeaa195d 100644 --- a/src/tests/roc_audio/test_samples/pcm_uint16_le.h +++ b/src/tests/roc_audio/test_samples/pcm_uint16_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_uint16_le = { /* name */ "pcm_uint16_le", - /* encoding */ PcmCode_UInt16, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_UInt16_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_uint24_be.h b/src/tests/roc_audio/test_samples/pcm_uint24_be.h index 10cb80499..5274e0f2f 100644 --- a/src/tests/roc_audio/test_samples/pcm_uint24_be.h +++ b/src/tests/roc_audio/test_samples/pcm_uint24_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_uint24_be = { /* name */ "pcm_uint24_be", - /* encoding */ PcmCode_UInt24, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_UInt24_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_uint24_le.h b/src/tests/roc_audio/test_samples/pcm_uint24_le.h index 084302e5e..c36f3e3fa 100644 --- a/src/tests/roc_audio/test_samples/pcm_uint24_le.h +++ b/src/tests/roc_audio/test_samples/pcm_uint24_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_uint24_le = { /* name */ "pcm_uint24_le", - /* encoding */ PcmCode_UInt24, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_UInt24_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_uint32_be.h b/src/tests/roc_audio/test_samples/pcm_uint32_be.h index 85b863b51..c87ee7c6c 100644 --- a/src/tests/roc_audio/test_samples/pcm_uint32_be.h +++ b/src/tests/roc_audio/test_samples/pcm_uint32_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_uint32_be = { /* name */ "pcm_uint32_be", - /* encoding */ PcmCode_UInt32, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_UInt32_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_uint32_le.h b/src/tests/roc_audio/test_samples/pcm_uint32_le.h index 3574a7013..a8846f356 100644 --- a/src/tests/roc_audio/test_samples/pcm_uint32_le.h +++ b/src/tests/roc_audio/test_samples/pcm_uint32_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_uint32_le = { /* name */ "pcm_uint32_le", - /* encoding */ PcmCode_UInt32, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_UInt32_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_uint8_be.h b/src/tests/roc_audio/test_samples/pcm_uint8_be.h index 58958bf5c..e17f613b2 100644 --- a/src/tests/roc_audio/test_samples/pcm_uint8_be.h +++ b/src/tests/roc_audio/test_samples/pcm_uint8_be.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_uint8_be = { /* name */ "pcm_uint8_be", - /* encoding */ PcmCode_UInt8, - /* endian */ PcmEndian_Big, + /* format */ PcmFormat_UInt8_Be, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/pcm_uint8_le.h b/src/tests/roc_audio/test_samples/pcm_uint8_le.h index 785dcf590..0443ce002 100644 --- a/src/tests/roc_audio/test_samples/pcm_uint8_le.h +++ b/src/tests/roc_audio/test_samples/pcm_uint8_le.h @@ -14,8 +14,7 @@ namespace test { static SampleInfo sample_pcm_uint8_le = { /* name */ "pcm_uint8_le", - /* encoding */ PcmCode_UInt8, - /* endian */ PcmEndian_Little, + /* format */ PcmFormat_UInt8_Le, /* num_samples */ 240, /* samples */ { diff --git a/src/tests/roc_audio/test_samples/sample_info.h b/src/tests/roc_audio/test_samples/sample_info.h index 5ea10a90d..c9bf63d82 100644 --- a/src/tests/roc_audio/test_samples/sample_info.h +++ b/src/tests/roc_audio/test_samples/sample_info.h @@ -21,8 +21,7 @@ struct SampleInfo { const char* name; - PcmCode encoding; - PcmEndian endian; + PcmFormat format; size_t num_samples; double samples[MaxSamples]; diff --git a/src/tests/roc_rtp/test_encoding_map.cpp b/src/tests/roc_rtp/test_encoding_map.cpp index a37f811d6..fd4a0ab73 100644 --- a/src/tests/roc_rtp/test_encoding_map.cpp +++ b/src/tests/roc_rtp/test_encoding_map.cpp @@ -35,8 +35,7 @@ TEST(encoding_map, find_by_pt) { LONGS_EQUAL(PayloadType_L16_Mono, enc->payload_type); - CHECK(enc->pcm_format - == audio::PcmFormat(audio::PcmCode_SInt16, audio::PcmEndian_Big)); + CHECK(enc->pcm_format == audio::PcmFormat_SInt16_Be); CHECK(enc->sample_spec.is_valid()); CHECK(enc->sample_spec @@ -56,8 +55,7 @@ TEST(encoding_map, find_by_pt) { LONGS_EQUAL(PayloadType_L16_Stereo, enc->payload_type); - CHECK(enc->pcm_format - == audio::PcmFormat(audio::PcmCode_SInt16, audio::PcmEndian_Big)); + CHECK(enc->pcm_format == audio::PcmFormat_SInt16_Be); CHECK(enc->sample_spec.is_valid()); CHECK(enc->sample_spec @@ -111,8 +109,7 @@ TEST(encoding_map, add_encoding) { Encoding enc; enc.payload_type = (PayloadType)100; enc.packet_flags = packet::Packet::FlagAudio; - enc.pcm_format = - audio::PcmFormat(audio::PcmCode_Float32, audio::PcmEndian_Native); + enc.pcm_format = audio::PcmFormat_Float32; enc.sample_spec = audio::SampleSpec(48000, audio::ChanLayout_Surround, audio::ChanOrder_Smpte, audio::ChanMask_Surround_Stereo); @@ -128,8 +125,7 @@ TEST(encoding_map, add_encoding) { LONGS_EQUAL(100, enc->payload_type); - CHECK(enc->pcm_format - == audio::PcmFormat(audio::PcmCode_Float32, audio::PcmEndian_Native)); + CHECK(enc->pcm_format == audio::PcmFormat_Float32); CHECK(enc->sample_spec == audio::SampleSpec(48000, audio::ChanLayout_Surround, @@ -150,8 +146,7 @@ TEST(encoding_map, add_encoding) { LONGS_EQUAL(100, enc->payload_type); - CHECK(enc->pcm_format - == audio::PcmFormat(audio::PcmCode_Float32, audio::PcmEndian_Native)); + CHECK(enc->pcm_format == audio::PcmFormat_Float32); CHECK(enc->sample_spec == audio::SampleSpec(48000, audio::ChanLayout_Surround, diff --git a/src/tests/roc_rtp/test_populator.cpp b/src/tests/roc_rtp/test_populator.cpp index 8ebfde60e..abeaf71bd 100644 --- a/src/tests/roc_rtp/test_populator.cpp +++ b/src/tests/roc_rtp/test_populator.cpp @@ -25,13 +25,13 @@ namespace rtp { namespace { -const audio::PcmFormat PcmFmt(audio::PcmCode_SInt16, audio::PcmEndian_Big); - enum { ChMask = 3, PacketSz = 128, SampleRate = 10000 }; const audio::SampleSpec SampleSpec = audio::SampleSpec( SampleRate, audio::ChanLayout_Surround, audio::ChanOrder_Smpte, ChMask); +const audio::PcmFormat PcmFmt = audio::PcmFormat_SInt16_Be; + core::HeapArena arena; packet::PacketFactory packet_factory(arena); core::BufferFactory buffer_factory(arena, PacketSz);