use super::descriptor_body;
use crate::error::{Error, Result};
use broadcast_core::len::need_out;
use broadcast_core::{Parse, Serialize};
pub const TAG: u8 = 0x7B;
const HEADER_LEN: usize = 2;
const FIXED_LEN: usize = 5;
const SAMPLE_RATE_CODE_MAX: u8 = 0x0F; const BIT_RATE_CODE_MAX: u8 = 0x3F; const BIT_RATE_CODE_MASK: u8 = 0x1F;
const BIT_RATE_UNIT_BPS: u32 = 100;
const NBLKS_MAX: u8 = 0x7F; const FSIZE_MAX: u16 = 0x3FFF; const SURROUND_MODE_MAX: u8 = 0x3F; const EXTENDED_SURROUND_MAX: u8 = 0x03; const LFE_FLAG_MASK: u8 = 0x01;
const SAMPLE_RATE_CODE_SHIFT: u32 = 36;
const BIT_RATE_CODE_SHIFT: u32 = 30;
const NBLKS_SHIFT: u32 = 23;
const FSIZE_SHIFT: u32 = 9;
const SURROUND_MODE_SHIFT: u32 = 3;
const LFE_FLAG_SHIFT: u32 = 2;
const PACKED_BYTES: usize = core::mem::size_of::<u64>();
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct DtsDescriptor<'a> {
pub sample_rate_code: u8,
pub bit_rate_code: u8,
pub nblks: u8,
pub fsize: u16,
pub surround_mode: u8,
pub lfe_flag: bool,
pub extended_surround_flag: u8,
pub additional_info: &'a [u8],
}
impl DtsDescriptor<'_> {
#[must_use]
pub fn sample_rate_hz(&self) -> Option<u32> {
match self.sample_rate_code {
0x01 => Some(8_000),
0x02 => Some(16_000),
0x03 => Some(32_000),
0x04 => Some(64_000),
0x05 => Some(128_000),
0x06 => Some(11_025),
0x07 => Some(22_050),
0x08 => Some(44_100),
0x09 => Some(88_200),
0x0A => Some(176_400),
0x0B => Some(12_000),
0x0C => Some(24_000),
0x0D => Some(48_000),
0x0E => Some(96_000),
0x0F => Some(192_000),
_ => None,
}
}
#[must_use]
pub fn bit_rate_bps(&self) -> Option<u32> {
let hundreds_of_bps: u32 = match self.bit_rate_code & BIT_RATE_CODE_MASK {
0x05 => 1_280,
0x06 => 1_920,
0x07 => 2_240,
0x08 => 2_560,
0x09 => 3_200,
0x0A => 3_840,
0x0B => 4_480,
0x0C => 5_120,
0x0D => 5_760,
0x0E => 6_400,
0x0F => 7_680,
0x10 => 9_600,
0x11 => 10_240,
0x12 => 11_520,
0x13 => 12_800,
0x14 => 13_440,
0x15 => 14_080,
0x16 => 14_112,
0x17 => 14_720,
0x18 => 15_360,
0x19 => 19_200,
0x1A => 20_480,
0x1B => 30_720,
0x1C => 38_400,
_ => return None,
};
Some(hundreds_of_bps * BIT_RATE_UNIT_BPS)
}
}
impl<'a> Parse<'a> for DtsDescriptor<'a> {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let body = descriptor_body(
bytes,
TAG,
"DtsDescriptor",
"unexpected tag for DTS_descriptor",
)?;
if body.len() < FIXED_LEN {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "DTS_descriptor body shorter than 5 bytes",
});
}
let mut packed_bytes = [0u8; PACKED_BYTES];
packed_bytes[PACKED_BYTES - FIXED_LEN..].copy_from_slice(&body[..FIXED_LEN]);
let packed = u64::from_be_bytes(packed_bytes);
let sample_rate_code =
((packed >> SAMPLE_RATE_CODE_SHIFT) & u64::from(SAMPLE_RATE_CODE_MAX)) as u8;
let bit_rate_code = ((packed >> BIT_RATE_CODE_SHIFT) & u64::from(BIT_RATE_CODE_MAX)) as u8;
let nblks = ((packed >> NBLKS_SHIFT) & u64::from(NBLKS_MAX)) as u8;
let fsize = ((packed >> FSIZE_SHIFT) & u64::from(FSIZE_MAX)) as u16;
let surround_mode = ((packed >> SURROUND_MODE_SHIFT) & u64::from(SURROUND_MODE_MAX)) as u8;
let lfe_flag = ((packed >> LFE_FLAG_SHIFT) & u64::from(LFE_FLAG_MASK)) != 0;
let extended_surround_flag = (packed & u64::from(EXTENDED_SURROUND_MAX)) as u8;
let additional_info = &body[FIXED_LEN..];
Ok(Self {
sample_rate_code,
bit_rate_code,
nblks,
fsize,
surround_mode,
lfe_flag,
extended_surround_flag,
additional_info,
})
}
}
impl Serialize for DtsDescriptor<'_> {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + FIXED_LEN + self.additional_info.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
if self.sample_rate_code > SAMPLE_RATE_CODE_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "sample_rate_code exceeds 4 bits",
});
}
if self.bit_rate_code > BIT_RATE_CODE_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "bit_rate_code exceeds 6 bits",
});
}
if self.nblks > NBLKS_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "nblks exceeds 7 bits",
});
}
if self.fsize > FSIZE_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "fsize exceeds 14 bits",
});
}
if self.surround_mode > SURROUND_MODE_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "surround_mode exceeds 6 bits",
});
}
if self.extended_surround_flag > EXTENDED_SURROUND_MAX {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "extended_surround_flag exceeds 2 bits",
});
}
if FIXED_LEN + self.additional_info.len() > u8::MAX as usize {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "DTS_descriptor body exceeds 255 bytes",
});
}
let len = self.serialized_len();
need_out(buf, len, "DtsDescriptor")?;
crate::descriptors::write_descriptor_header(
buf,
TAG,
FIXED_LEN + self.additional_info.len(),
)?;
let packed: u64 = ((u64::from(self.sample_rate_code) & u64::from(SAMPLE_RATE_CODE_MAX))
<< SAMPLE_RATE_CODE_SHIFT)
| ((u64::from(self.bit_rate_code) & u64::from(BIT_RATE_CODE_MAX))
<< BIT_RATE_CODE_SHIFT)
| ((u64::from(self.nblks) & u64::from(NBLKS_MAX)) << NBLKS_SHIFT)
| ((u64::from(self.fsize) & u64::from(FSIZE_MAX)) << FSIZE_SHIFT)
| ((u64::from(self.surround_mode) & u64::from(SURROUND_MODE_MAX))
<< SURROUND_MODE_SHIFT)
| (u64::from(self.lfe_flag) << LFE_FLAG_SHIFT)
| (u64::from(self.extended_surround_flag) & u64::from(EXTENDED_SURROUND_MAX));
buf[HEADER_LEN..HEADER_LEN + FIXED_LEN]
.copy_from_slice(&packed.to_be_bytes()[PACKED_BYTES - FIXED_LEN..]);
buf[HEADER_LEN + FIXED_LEN..len].copy_from_slice(self.additional_info);
Ok(len)
}
}
impl<'a> crate::traits::DescriptorDef<'a> for DtsDescriptor<'a> {
const TAG: u8 = TAG;
const NAME: &'static str = "DTS";
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
#[test]
fn bit_rate_bps_follows_table_g3_and_ignores_the_reserved_msb() {
let rate = |code: u8| {
DtsDescriptor {
sample_rate_code: 0,
bit_rate_code: code,
nblks: 5,
fsize: 95,
surround_mode: 0,
lfe_flag: false,
extended_surround_flag: 0,
additional_info: &[],
}
.bit_rate_bps()
};
assert_eq!(rate(0x00), None);
assert_eq!(rate(0x04), None);
let table = [
(0x05, 128_000),
(0x06, 192_000),
(0x07, 224_000),
(0x08, 256_000),
(0x09, 320_000),
(0x0A, 384_000),
(0x0B, 448_000),
(0x0C, 512_000),
(0x0D, 576_000),
(0x0E, 640_000),
(0x0F, 768_000),
(0x10, 960_000),
(0x11, 1_024_000),
(0x12, 1_152_000),
(0x13, 1_280_000),
(0x14, 1_344_000),
(0x15, 1_408_000),
(0x16, 1_411_200),
(0x17, 1_472_000),
(0x18, 1_536_000),
(0x19, 1_920_000),
(0x1A, 2_048_000),
(0x1B, 3_072_000),
(0x1C, 3_840_000),
];
for (code, bps) in table {
assert_eq!(rate(code), Some(bps), "bit_rate_code {code:#04x}");
}
assert_eq!(rate(0x05), Some(128_000));
assert_eq!(rate(0x0A), Some(384_000));
assert_eq!(rate(0x10), Some(960_000));
assert_eq!(rate(0x11), Some(1_024_000));
assert_eq!(rate(0x16), Some(1_411_200));
assert_eq!(rate(0x1C), Some(3_840_000));
for open_variable_lossless in [0x1D, 0x1E, 0x1F] {
assert_eq!(rate(open_variable_lossless), None);
}
for code in 0x05..=0x1C {
assert_eq!(rate(code), rate(code | 0x20), "code {code:#04x}");
}
}
#[test]
fn sample_rate_hz_follows_table_g2() {
let rate = |code: u8| {
DtsDescriptor {
sample_rate_code: code,
bit_rate_code: 0,
nblks: 5,
fsize: 95,
surround_mode: 0,
lfe_flag: false,
extended_surround_flag: 0,
additional_info: &[],
}
.sample_rate_hz()
};
let table = [
(0x00, None),
(0x01, Some(8_000)),
(0x02, Some(16_000)),
(0x03, Some(32_000)),
(0x04, Some(64_000)),
(0x05, Some(128_000)),
(0x06, Some(11_025)),
(0x07, Some(22_050)),
(0x08, Some(44_100)),
(0x09, Some(88_200)),
(0x0A, Some(176_400)),
(0x0B, Some(12_000)),
(0x0C, Some(24_000)),
(0x0D, Some(48_000)),
(0x0E, Some(96_000)),
(0x0F, Some(192_000)),
];
for (code, hz) in table {
assert_eq!(rate(code), hz, "sample_rate_code {code:#04x}");
}
}
#[test]
fn sample_rate_0x09_is_88_200_hz_in_the_44k1_family() {
let rate = |code: u8| {
DtsDescriptor {
sample_rate_code: code,
bit_rate_code: 0,
nblks: 5,
fsize: 95,
surround_mode: 0,
lfe_flag: false,
extended_surround_flag: 0,
additional_info: &[],
}
.sample_rate_hz()
};
assert_eq!(rate(0x08), Some(44_100));
assert_eq!(rate(0x09), Some(88_200));
assert_eq!(rate(0x0A), Some(176_400));
assert_eq!(rate(0x09), rate(0x08).map(|r| r * 2));
assert_eq!(rate(0x0A), rate(0x09).map(|r| r * 2));
}
#[test]
fn parse_no_additional_info() {
let d_in = DtsDescriptor {
sample_rate_code: 0b1101,
bit_rate_code: 0b001010,
nblks: 8,
fsize: 1024,
surround_mode: 0b000010,
lfe_flag: true,
extended_surround_flag: 0b01,
additional_info: &[],
};
let mut buf = vec![0u8; d_in.serialized_len()];
d_in.serialize_into(&mut buf).unwrap();
let d = DtsDescriptor::parse(&buf).unwrap();
assert_eq!(d, d_in);
assert_eq!(buf[1], 5);
}
#[test]
fn parse_with_additional_info() {
let d_in = DtsDescriptor {
sample_rate_code: 0b1000,
bit_rate_code: 0b011010,
nblks: 127,
fsize: 0x3FFF,
surround_mode: 0b001001,
lfe_flag: false,
extended_surround_flag: 0b10,
additional_info: &[0xAA, 0xBB, 0xCC],
};
let mut buf = vec![0u8; d_in.serialized_len()];
d_in.serialize_into(&mut buf).unwrap();
let d = DtsDescriptor::parse(&buf).unwrap();
assert_eq!(d.additional_info, &[0xAA, 0xBB, 0xCC]);
assert_eq!(d, d_in);
}
#[test]
fn decode_sample_rate() {
let d = DtsDescriptor {
sample_rate_code: 0x0D,
bit_rate_code: 0,
nblks: 0,
fsize: 0,
surround_mode: 0,
lfe_flag: false,
extended_surround_flag: 0,
additional_info: &[],
};
assert_eq!(d.sample_rate_hz(), Some(48_000));
}
#[test]
fn decode_sample_rate_invalid() {
let d = DtsDescriptor {
sample_rate_code: 0x00,
bit_rate_code: 0,
nblks: 0,
fsize: 0,
surround_mode: 0,
lfe_flag: false,
extended_surround_flag: 0,
additional_info: &[],
};
assert_eq!(d.sample_rate_hz(), None);
}
#[test]
fn parse_rejects_wrong_tag() {
let bytes = [0x7C, 5, 0, 0, 0, 0, 0];
assert!(matches!(
DtsDescriptor::parse(&bytes).unwrap_err(),
Error::InvalidDescriptor { tag: 0x7C, .. }
));
}
#[test]
fn parse_rejects_body_too_short() {
let bytes = [TAG, 4, 0, 0, 0, 0];
assert!(matches!(
DtsDescriptor::parse(&bytes).unwrap_err(),
Error::InvalidDescriptor { .. }
));
}
#[test]
fn parse_rejects_length_overrunning_buffer() {
let bytes = [TAG, 5, 0, 0, 0];
assert!(matches!(
DtsDescriptor::parse(&bytes).unwrap_err(),
Error::BufferTooShort(..)
));
}
#[test]
fn serialize_round_trip_max_fields() {
let d = DtsDescriptor {
sample_rate_code: 0x0F,
bit_rate_code: 0x3F,
nblks: 0x7F,
fsize: 0x3FFF,
surround_mode: 0x3F,
lfe_flag: true,
extended_surround_flag: 0x03,
additional_info: &[0x01],
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(DtsDescriptor::parse(&buf).unwrap(), d);
}
#[test]
fn serialize_rejects_fsize_over_range() {
let d = DtsDescriptor {
sample_rate_code: 0,
bit_rate_code: 0,
nblks: 0,
fsize: 0x4000,
surround_mode: 0,
lfe_flag: false,
extended_surround_flag: 0,
additional_info: &[],
};
let mut buf = vec![0u8; d.serialized_len()];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::InvalidDescriptor { .. }
));
}
#[test]
fn serialize_rejects_bit_rate_code_over_range() {
let d = DtsDescriptor {
sample_rate_code: 0,
bit_rate_code: 0x40,
nblks: 0,
fsize: 0,
surround_mode: 0,
lfe_flag: false,
extended_surround_flag: 0,
additional_info: &[],
};
let mut buf = vec![0u8; d.serialized_len()];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::InvalidDescriptor { .. }
));
}
}