Skip to content

Commit

Permalink
[spo] roc-streaminggh-731: Sample spec & PCM format refinements
Browse files Browse the repository at this point in the history
- add native_alias and portable_alias
- renames

Sponsored-by: waspd
  • Loading branch information
gavv committed Jul 20, 2024
1 parent a1d38c2 commit ccc6de5
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 141 deletions.
702 changes: 598 additions & 104 deletions src/internal_modules/roc_audio/pcm_format.cpp

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions src/internal_modules/roc_audio/pcm_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,18 @@ struct PcmTraits {
//! True for little-endian formats.
bool is_little;

//! Canonical identifier of the same format.
PcmFormat canon_id;
//! True if format has same endian as current CPU.
bool is_native;

//! Same format, but with removed _Be or _Le suffix.
//! Value depends on current CPU.
//! Set only if format's endian matches current CPU's endian.
PcmFormat native_alias;

//! Same format, but with added _Be or _Le suffix.
//! Value depends on current CPU.
//! Always set. If format already had suffix, set to the same format.
PcmFormat portable_alias;

//! Number of significant bits per sample.
size_t bit_depth;
Expand All @@ -225,7 +235,9 @@ struct PcmTraits {
, is_integer(false)
, is_signed(false)
, is_little(false)
, canon_id(PcmFormat_Invalid)
, is_native(false)
, native_alias(PcmFormat_Invalid)
, portable_alias(PcmFormat_Invalid)
, bit_depth(0)
, bit_width(0) {
}
Expand Down
32 changes: 27 additions & 5 deletions src/internal_modules/roc_audio/pcm_format_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,14 +979,36 @@ def nth_chars(codes, prefix=()):
{% if endian == 'Native' %}
#if ROC_CPU_ENDIAN == ROC_CPU_BE
traits.is_little = false;
traits.canon_id = {{ make_enum_name(code, 'Big') }};
#else
traits.is_little = true;
traits.canon_id = {{ make_enum_name(code, 'Little') }};
#endif
{% else %}
traits.is_little = {{ str(endian == 'Little').lower() }};
traits.canon_id = {{ make_enum_name(code, endian) }};
traits.is_native = true;
traits.native_alias = {{ make_enum_name(code, endian) }};
#if ROC_CPU_ENDIAN == ROC_CPU_BE
traits.portable_alias = {{ make_enum_name(code, 'Big') }};
#else
traits.portable_alias = {{ make_enum_name(code, 'Little') }};
#endif
{% elif endian == 'Big' %}
traits.is_little = false;
#if ROC_CPU_ENDIAN == ROC_CPU_BE
traits.is_native = true;
traits.native_alias = {{ make_enum_name(code, 'Native') }};
#else
traits.is_native = false;
traits.native_alias = PcmFormat_Invalid;
#endif
traits.portable_alias = {{ make_enum_name(code, endian) }};
{% elif endian == 'Little' %}
traits.is_little = true;
#if ROC_CPU_ENDIAN == ROC_CPU_BE
traits.is_native = false;
traits.native_alias = PcmFormat_Invalid;
#else
traits.is_native = true;
traits.native_alias = {{ make_enum_name(code, 'Native') }};
#endif
traits.portable_alias = {{ make_enum_name(code, endian) }};
{% endif %}
traits.bit_depth = {{ code.depth }};
traits.bit_width = {{ code.packed_width }};
Expand Down
54 changes: 30 additions & 24 deletions src/internal_modules/roc_audio/sample_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ core::nanoseconds_t nsamples_2_ns(const float n_samples, const size_t sample_rat
return (core::nanoseconds_t)val;
}

PcmFormat get_pcm_canon_format(PcmFormat fmt) {
PcmFormat get_pcm_portable_format(PcmFormat fmt) {
if (fmt == PcmFormat_Invalid) {
return PcmFormat_Invalid;
}

const PcmTraits traits = pcm_format_traits(fmt);
return traits.canon_id;
return traits.portable_alias;
}

size_t get_pcm_sample_width(PcmFormat fmt) {
Expand All @@ -86,43 +86,49 @@ SampleSpec::SampleSpec()
: sample_rate_(0)
, sample_fmt_(SampleFormat_Invalid)
, pcm_fmt_(PcmFormat_Invalid)
, pcm_width_(0) {
, pcm_width_(0)
, channel_set_() {
}

SampleSpec::SampleSpec(const size_t sample_rate,
const PcmFormat pcm_fmt,
const ChannelSet& channel_set)
: sample_rate_(sample_rate)
, sample_fmt_(SampleFormat_Pcm)
, pcm_fmt_(pcm_fmt)
, pcm_width_(get_pcm_sample_width(pcm_fmt))
: sample_rate_(0)
, sample_fmt_(SampleFormat_Invalid)
, pcm_fmt_(PcmFormat_Invalid)
, pcm_width_(0)
, channel_set_(channel_set) {
roc_panic_if_msg(sample_rate_ == 0, "sample spec: invalid sample rate");
roc_panic_if_msg(pcm_fmt_ == PcmFormat_Invalid || pcm_width_ == 0,
"sample spec: invalid pcm format");
roc_panic_if_msg(!channel_set_.is_valid(), "sample spec: invalid channel set");
set_sample_format(SampleFormat_Pcm);
set_pcm_format(pcm_fmt);
set_sample_rate(sample_rate);

roc_panic_if_msg(!is_valid(), "sample spec: attempt to construct invalid spec: %s",
sample_spec_to_str(*this).c_str());
}

SampleSpec::SampleSpec(const size_t sample_rate,
const PcmFormat pcm_fmt,
const ChannelLayout channel_layout,
ChannelOrder channel_order,
const ChannelMask channel_mask)
: sample_rate_(sample_rate)
, sample_fmt_(SampleFormat_Pcm)
, pcm_fmt_(pcm_fmt)
, pcm_width_(get_pcm_sample_width(pcm_fmt))
: sample_rate_(0)
, sample_fmt_(SampleFormat_Invalid)
, pcm_fmt_(PcmFormat_Invalid)
, pcm_width_(0)
, channel_set_(channel_layout, channel_order, channel_mask) {
roc_panic_if_msg(sample_rate_ == 0, "sample spec: invalid sample rate");
roc_panic_if_msg(pcm_fmt_ == PcmFormat_Invalid || pcm_width_ == 0,
"sample spec: invalid pcm format");
roc_panic_if_msg(!channel_set_.is_valid(), "sample spec: invalid channel set");
set_sample_format(SampleFormat_Pcm);
set_pcm_format(pcm_fmt);
set_sample_rate(sample_rate);

roc_panic_if_msg(!is_valid(), "sample spec: attempt to construct invalid spec: %s",
sample_spec_to_str(*this).c_str());
}

bool SampleSpec::operator==(const SampleSpec& other) const {
return sample_fmt_ == other.sample_fmt_
&& (sample_fmt_ != SampleFormat_Pcm || pcm_fmt_ == other.pcm_fmt_
|| get_pcm_canon_format(pcm_fmt_) == get_pcm_canon_format(other.pcm_fmt_))
&& (sample_fmt_ != SampleFormat_Pcm
|| get_pcm_portable_format(pcm_fmt_)
== get_pcm_portable_format(other.pcm_fmt_))
&& sample_rate_ == other.sample_rate_ && channel_set_ == other.channel_set_;
}

Expand All @@ -132,7 +138,7 @@ bool SampleSpec::operator!=(const SampleSpec& other) const {

bool SampleSpec::is_valid() const {
return sample_fmt_ != SampleFormat_Invalid
&& ((sample_fmt_ == SampleFormat_Pcm) == (pcm_fmt_ != PcmFormat_Invalid))
&& (sample_fmt_ != SampleFormat_Pcm || pcm_fmt_ != PcmFormat_Invalid)
&& sample_rate_ != 0 && channel_set_.is_valid();
}

Expand All @@ -143,7 +149,7 @@ bool SampleSpec::is_empty() const {

bool SampleSpec::is_raw() const {
return sample_fmt_ == SampleFormat_Pcm
&& get_pcm_canon_format(pcm_fmt_) == get_pcm_canon_format(Sample_RawFormat);
&& get_pcm_portable_format(pcm_fmt_) == get_pcm_portable_format(Sample_RawFormat);
}

void SampleSpec::clear() {
Expand Down Expand Up @@ -388,7 +394,7 @@ void SampleSpec::validate_frame(Frame& frame) const {
}
}

bool SampleSpec::validate_frame_size(size_t n_bytes) {
bool SampleSpec::is_valid_frame_size(size_t n_bytes) {
roc_panic_if_msg(!is_valid(), "sample spec: attempt to use invalid spec: %s",
sample_spec_to_str(*this).c_str());

Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_audio/sample_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class SampleSpec {

//! Check if frame size is multiple of sample size and channel count.
//! Returns false if size is invalid.
ROC_ATTR_NODISCARD bool validate_frame_size(size_t n_bytes);
bool is_valid_frame_size(size_t n_bytes);

//! Cap duration to fit given buffer size in bytes.
//! Returns @p duration or smaller value.
Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_node/receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ status::StatusCode Receiver::read_frame(void* bytes, size_t n_bytes) {
roc_panic_if(!bytes);
roc_panic_if(n_bytes == 0);

if (!sample_spec_.validate_frame_size(n_bytes)) {
if (!sample_spec_.is_valid_frame_size(n_bytes)) {
return status::StatusBadBuffer;
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_node/receiver_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ status::StatusCode ReceiverDecoder::read_frame(void* bytes, size_t n_bytes) {
roc_panic_if(!bytes);
roc_panic_if(n_bytes == 0);

if (!sample_spec_.validate_frame_size(n_bytes)) {
if (!sample_spec_.is_valid_frame_size(n_bytes)) {
return status::StatusBadBuffer;
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_node/sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ status::StatusCode Sender::write_frame(const void* bytes, size_t n_bytes) {
roc_panic_if(!bytes);
roc_panic_if(n_bytes == 0);

if (!sample_spec_.validate_frame_size(n_bytes)) {
if (!sample_spec_.is_valid_frame_size(n_bytes)) {
return status::StatusBadBuffer;
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_node/sender_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ status::StatusCode SenderEncoder::write_frame(const void* bytes, size_t n_bytes)
roc_panic_if(!bytes);
roc_panic_if(n_bytes == 0);

if (!sample_spec_.validate_frame_size(n_bytes)) {
if (!sample_spec_.is_valid_frame_size(n_bytes)) {
return status::StatusBadBuffer;
}

Expand Down

0 comments on commit ccc6de5

Please sign in to comment.