Skip to content

Commit

Permalink
roc-streaminggh-608 Rework pcm format helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Nov 16, 2023
1 parent dd95ed4 commit 9015c09
Show file tree
Hide file tree
Showing 38 changed files with 3,602 additions and 2,386 deletions.
2 changes: 1 addition & 1 deletion .fmtignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
src/internal_modules/roc_audio/pcm_mapper_func.h
src/internal_modules/roc_audio/pcm_funcs.h
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
2 changes: 1 addition & 1 deletion src/internal_modules/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FILE_PATTERNS = *.h *.dox

EXCLUDE = \
roc_core/target_posix/roc_core/cpu_traits.h \
roc_audio/pcm_mapper_func.h
roc_audio/pcm_funcs.h

FULL_PATH_NAMES = YES
SOURCE_BROWSER = YES
Expand Down
39 changes: 39 additions & 0 deletions src/internal_modules/roc_audio/pcm_format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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
106 changes: 70 additions & 36 deletions src/internal_modules/roc_audio/pcm_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,42 @@
#ifndef ROC_AUDIO_PCM_FORMAT_H_
#define ROC_AUDIO_PCM_FORMAT_H_

#include "roc_packet/units.h"
#include "roc_core/attributes.h"
#include "roc_core/stddefs.h"

namespace roc {
namespace audio {

//! PCM sample encoding.
enum PcmEncoding {
PcmEncoding_SInt8, //!< 8-bit signed integer.
PcmEncoding_UInt8, //!< 8-bit unsigned integer.
PcmEncoding_SInt16, //!< 16-bit signed integer.
PcmEncoding_UInt16, //!< 16-bit unsigned integer.
PcmEncoding_SInt18, //!< 18-bit signed integer (2.25 bytes).
PcmEncoding_UInt18, //!< 18-bit unsigned integer (2.25 bytes).
PcmEncoding_SInt18_3B, //!< 18-bit signed integer, in low bits of 3-byte container.
PcmEncoding_UInt18_3B, //!< 18-bit unsigned integer, in low bits of 3-byte container.
PcmEncoding_SInt18_4B, //!< 18-bit signed integer, in low bits of 4-byte container.
PcmEncoding_UInt18_4B, //!< 18-bit unsigned integer, in low bits of 4-byte container.
PcmEncoding_SInt20, //!< 20-bit signed integer (2.5 bytes).
PcmEncoding_UInt20, //!< 20-bit unsigned integer (2.5 bytes).
PcmEncoding_SInt20_3B, //!< 20-bit signed integer, in low bits of 3-byte container.
PcmEncoding_UInt20_3B, //!< 20-bit unsigned integer, in low bits of 3-byte container.
PcmEncoding_SInt20_4B, //!< 20-bit signed integer, in low bits of 4-byte container.
PcmEncoding_UInt20_4B, //!< 20-bit unsigned integer, in low bits of 4-byte container.
PcmEncoding_SInt24, //!< 24-bit signed integer (3 bytes).
PcmEncoding_UInt24, //!< 24-bit unsigned integer (3 bytes).
PcmEncoding_SInt24_4B, //!< 24-bit signed integer, in low bits of 4-byte container.
PcmEncoding_UInt24_4B, //!< 24-bit unsigned integer, in low bits of 4-byte container.
PcmEncoding_SInt32, //!< 32-bit signed integer.
PcmEncoding_UInt32, //!< 32-bit unsigned integer.
PcmEncoding_SInt64, //!< 64-bit signed integer.
PcmEncoding_UInt64, //!< 64-bit unsigned integer.
PcmEncoding_Float32, //!< 32-bit IEEE-754 float in range [-1.0; +1.0].
PcmEncoding_Float64 //!< 64-bit IEEE-754 float in range [-1.0; +1.0].
//! 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 sample endianess.
Expand All @@ -56,27 +59,27 @@ enum PcmEndian {

//! PCM format description.
struct PcmFormat {
//! Sample encoding.
PcmEncoding encoding;
//! Sample binary code.
PcmCode code;

//! Sample endian.
//! Sample endianess.
PcmEndian endian;

//! Initialize.
PcmFormat()
: encoding()
: code()
, endian() {
}

//! Initialize.
PcmFormat(PcmEncoding enc, PcmEndian end)
: encoding(enc)
PcmFormat(PcmCode enc, PcmEndian end)
: code(enc)
, endian(end) {
}

//! Check two formats for equality.
bool operator==(const PcmFormat& other) const {
return encoding == other.encoding && endian == other.endian;
return code == other.code && endian == other.endian;
}

//! Check two formats for equality.
Expand All @@ -85,6 +88,37 @@ struct PcmFormat {
}
};

//! 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_integer(false)
, is_signed(false) {
}
};

//! Get string name of PCM format.
const char* pcm_format_to_str(const PcmFormat& fmt);

//! Parse PCM format from string name.
ROC_ATTR_NODISCARD bool pcm_format_parse(const char* str, PcmFormat& fmt);

//! Get traits for PCM format.
PcmTraits pcm_format_traits(const PcmFormat& fmt);

} // namespace audio
} // namespace roc

Expand Down
Loading

0 comments on commit 9015c09

Please sign in to comment.