diff --git a/crates/audio/src/aac/config.rs b/crates/audio/src/aac/config.rs index 88be1e214..b81f56f3e 100644 --- a/crates/audio/src/aac/config.rs +++ b/crates/audio/src/aac/config.rs @@ -343,6 +343,48 @@ impl AudioSpecificConfig { } } +/// Why an AudioSpecificConfig prefix cannot be written as an ADTS header. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AdtsConfigError { + /// Fewer than two bytes were supplied. + Short, + /// The prefix names a profile, indexed rate or channel configuration ADTS cannot carry. + Unsupported { + /// Audio object type from the prefix. + object_type: u8, + /// Sampling frequency index from the prefix. + sf_index: u8, + /// Channel configuration from the prefix. + channel_config: u8, + }, +} + +/// ADTS fixed fields taken from the prefix of an AudioSpecificConfig. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct AdtsConfig { + object_type: u8, + sf_index: u8, + channel_config: u8, +} +impl AdtsConfig { + /// Parse only the first two ASC bytes. Unlike a decoder's full ASC parser, this accepts all + /// four object types representable by ADTS and rejects configurations needing a PCE. + pub fn from_asc_prefix(asc: &[u8]) -> std::result::Result { + let (Some(&a), Some(&b)) = (asc.first(), asc.get(1)) else { return Err(AdtsConfigError::Short) }; + let object_type = a >> 3; + let sf_index = ((a & 7) << 1) | (b >> 7); + let channel_config = (b >> 3) & 15; + if !(1..=4).contains(&object_type) || sf_index > 12 || !(1..=7).contains(&channel_config) { + return Err(AdtsConfigError::Unsupported { object_type, sf_index, channel_config }); + } + Ok(Self { object_type, sf_index, channel_config }) + } + /// Write a no-CRC ADTS header with variable-rate buffer fullness for one payload. + pub fn header(self, payload_len: usize) -> Result<[u8; 7]> { + AdtsHeader::write(self.object_type, self.sf_index, self.channel_config, payload_len, 0x7ff) + } +} + /// A parsed ADTS header. #[non_exhaustive] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -519,6 +561,15 @@ mod tests { assert_eq!(AudioSpecificConfig::parse(&asc.to_bytes().unwrap()).unwrap(), asc); } + #[test] + fn adts_config_preserves_prefix_and_frame_limits() { + let config = AdtsConfig::from_asc_prefix(&[0x12, 0x10]).unwrap(); + assert_eq!(config.header(100).unwrap(), AdtsHeader::write(2, 4, 2, 100, 0x7ff).unwrap()); + assert!(config.header(8185).is_err()); + assert_eq!(AdtsConfig::from_asc_prefix(&[]), Err(AdtsConfigError::Short)); + assert!(matches!(AdtsConfig::from_asc_prefix(&[0x2a, 0x10]), Err(AdtsConfigError::Unsupported { object_type: 5, .. }))); + } + #[test] fn adts_headers_refuse_fields_they_cannot_hold() { for (object_type, sf_index, channel_config) in [(0, 4, 2), (5, 4, 2), (29, 4, 2), (2, 13, 2), (2, 15, 2), (2, 4, 8), (2, 4, 11), (2, 4, 12)] { diff --git a/crates/audio/src/aac/mod.rs b/crates/audio/src/aac/mod.rs index f423a4e22..fb79ae743 100644 --- a/crates/audio/src/aac/mod.rs +++ b/crates/audio/src/aac/mod.rs @@ -60,7 +60,7 @@ mod tns; #[cfg(feature = "aac-decode")] pub(crate) use config::split_adts_upto; -pub use config::{AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, config_layout, split_adts}; +pub use config::{AdtsConfig, AdtsConfigError, AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, config_layout, split_adts}; #[cfg(feature = "aac-decode")] pub use decoder::Decoder; #[cfg(feature = "aac-encode")]