use crate::error::{Error, Result};
use crate::traits::Descriptor;
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x7C;
const HEADER_LEN: usize = 2;
const FLAG_AAC_TYPE: u8 = 0x80;
const FLAG_SAOC_DE: u8 = 0x40;
const RESERVED_ZERO_MASK: u8 = 0x3F;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct AacExtension<'a> {
pub saoc_de_flag: bool,
pub aac_type: Option<u8>,
pub additional_info: &'a [u8],
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct AacDescriptor<'a> {
pub profile_and_level: u8,
pub extension: Option<AacExtension<'a>>,
}
impl<'a> Parse<'a> for AacDescriptor<'a> {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
if bytes.len() < HEADER_LEN + 1 {
return Err(Error::BufferTooShort {
need: HEADER_LEN + 1,
have: bytes.len(),
what: "AacDescriptor header+profile",
});
}
if bytes[0] != TAG {
return Err(Error::InvalidDescriptor {
tag: bytes[0],
reason: "unexpected tag for AAC_descriptor",
});
}
let length = bytes[1] as usize;
let end = HEADER_LEN + length;
if bytes.len() < end {
return Err(Error::BufferTooShort {
need: end,
have: bytes.len(),
what: "AacDescriptor body",
});
}
if length < 1 {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "AAC_descriptor body shorter than 1 byte",
});
}
let body = &bytes[HEADER_LEN..end];
let profile_and_level = body[0];
let extension = if length > 1 {
let flags = body[1];
let aac_type_flag = (flags & FLAG_AAC_TYPE) != 0;
let saoc_de_flag = (flags & FLAG_SAOC_DE) != 0;
let mut pos = 2;
let aac_type = if aac_type_flag {
if pos >= body.len() {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "AAC_type_flag set but AAC_type byte missing",
});
}
let t = body[pos];
pos += 1;
Some(t)
} else {
None
};
let additional_info = &body[pos..];
Some(AacExtension {
saoc_de_flag,
aac_type,
additional_info,
})
} else {
None
};
Ok(Self {
profile_and_level,
extension,
})
}
}
impl Serialize for AacDescriptor<'_> {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
let body = 1 + match &self.extension {
None => 0,
Some(ext) => 1 + usize::from(ext.aac_type.is_some()) + ext.additional_info.len(),
};
HEADER_LEN + body
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let body_len = self.serialized_len() - HEADER_LEN;
if body_len > u8::MAX as usize {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "AAC_descriptor body exceeds 255 bytes",
});
}
let len = self.serialized_len();
if buf.len() < len {
return Err(Error::OutputBufferTooSmall {
need: len,
have: buf.len(),
});
}
buf[0] = TAG;
buf[1] = body_len as u8;
buf[2] = self.profile_and_level;
let mut pos = 3;
if let Some(ext) = &self.extension {
let mut flags = 0u8;
if ext.aac_type.is_some() {
flags |= FLAG_AAC_TYPE;
}
if ext.saoc_de_flag {
flags |= FLAG_SAOC_DE;
}
buf[pos] = flags & !RESERVED_ZERO_MASK;
pos += 1;
if let Some(t) = ext.aac_type {
buf[pos] = t;
pos += 1;
}
buf[pos..pos + ext.additional_info.len()].copy_from_slice(ext.additional_info);
}
Ok(len)
}
}
impl<'a> Descriptor<'a> for AacDescriptor<'a> {
const TAG: u8 = TAG;
fn descriptor_length(&self) -> u8 {
(self.serialized_len() - HEADER_LEN) as u8
}
}
impl<'a> crate::traits::DescriptorDef<'a> for AacDescriptor<'a> {
const TAG: u8 = TAG;
const NAME: &'static str = "AAC";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_profile_only() {
let bytes = [TAG, 1, 0x50];
let d = AacDescriptor::parse(&bytes).unwrap();
assert_eq!(d.profile_and_level, 0x50);
assert!(d.extension.is_none());
}
#[test]
fn parse_with_flags_no_aac_type() {
let bytes = [TAG, 2, 0x51, FLAG_SAOC_DE];
let d = AacDescriptor::parse(&bytes).unwrap();
let ext = d.extension.unwrap();
assert!(ext.saoc_de_flag);
assert!(ext.aac_type.is_none());
assert!(ext.additional_info.is_empty());
}
#[test]
fn parse_with_aac_type() {
let bytes = [TAG, 3, 0x52, FLAG_AAC_TYPE, 0x03];
let d = AacDescriptor::parse(&bytes).unwrap();
let ext = d.extension.unwrap();
assert!(!ext.saoc_de_flag);
assert_eq!(ext.aac_type, Some(0x03));
assert!(ext.additional_info.is_empty());
}
#[test]
fn parse_with_aac_type_and_additional_info() {
let bytes = [TAG, 5, 0x52, FLAG_AAC_TYPE | FLAG_SAOC_DE, 0x05, 0xAA, 0xBB];
let d = AacDescriptor::parse(&bytes).unwrap();
let ext = d.extension.unwrap();
assert!(ext.saoc_de_flag);
assert_eq!(ext.aac_type, Some(0x05));
assert_eq!(ext.additional_info, &[0xAA, 0xBB]);
}
#[test]
fn parse_rejects_wrong_tag() {
let bytes = [0x7B, 1, 0x50];
assert!(matches!(
AacDescriptor::parse(&bytes).unwrap_err(),
Error::InvalidDescriptor { tag: 0x7B, .. }
));
}
#[test]
fn parse_rejects_aac_type_flag_without_byte() {
let bytes = [TAG, 2, 0x50, FLAG_AAC_TYPE];
assert!(matches!(
AacDescriptor::parse(&bytes).unwrap_err(),
Error::InvalidDescriptor { .. }
));
}
#[test]
fn parse_rejects_length_overrunning_buffer() {
let bytes = [TAG, 4, 0x50];
assert!(matches!(
AacDescriptor::parse(&bytes).unwrap_err(),
Error::BufferTooShort { .. }
));
}
#[test]
fn serialize_round_trip_profile_only() {
let d = AacDescriptor {
profile_and_level: 0x58,
extension: None,
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(buf, vec![TAG, 1, 0x58]);
assert_eq!(AacDescriptor::parse(&buf).unwrap(), d);
}
#[test]
fn serialize_round_trip_full() {
let d = AacDescriptor {
profile_and_level: 0x52,
extension: Some(AacExtension {
saoc_de_flag: true,
aac_type: Some(0x40),
additional_info: &[0xFE, 0xED],
}),
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(AacDescriptor::parse(&buf).unwrap(), d);
}
#[test]
fn serialize_emits_reserved_bits_zero() {
let d = AacDescriptor {
profile_and_level: 0x50,
extension: Some(AacExtension {
saoc_de_flag: false,
aac_type: None,
additional_info: &[],
}),
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(buf[3] & RESERVED_ZERO_MASK, 0);
assert_eq!(buf[3], 0x00);
}
#[cfg(feature = "serde")]
#[test]
fn serde_serializes_to_stable_json() {
let d = AacDescriptor {
profile_and_level: 0x52,
extension: Some(AacExtension {
saoc_de_flag: true,
aac_type: Some(0x03),
additional_info: &[0x11],
}),
};
let j = serde_json::to_string(&d).unwrap();
let _v: serde_json::Value = serde_json::from_str(&j).unwrap();
assert!(j.contains("profile_and_level"));
}
}