diff --git a/crates/ec-ac3/src/decode.rs b/crates/ec-ac3/src/decode.rs index 3ec3842f..363957b1 100644 --- a/crates/ec-ac3/src/decode.rs +++ b/crates/ec-ac3/src/decode.rs @@ -35,6 +35,15 @@ pub enum Syntax { Eac3, } +impl Syntax { + /// The syntax of the syncframe at `data`, read from bsid, which sits in + /// the sixth byte of both. `None` when there are not six bytes yet. + pub(crate) fn of(data: &[u8]) -> Option { + let bsid = *data.get(5)? >> 3; + Some(if bsid <= 10 { Syntax::Ac3 } else { Syntax::Eac3 }) + } +} + /// The decoder state a stream carries across blocks and frames. pub(crate) struct Core { pub(crate) syntax: Syntax, diff --git a/crates/ec-ac3/src/decoder.rs b/crates/ec-ac3/src/decoder.rs index 3bcf5aef..3c69d80c 100644 --- a/crates/ec-ac3/src/decoder.rs +++ b/crates/ec-ac3/src/decoder.rs @@ -175,7 +175,7 @@ impl Ac3Decoder { let mut pos = first.consumed; while begins_syncframe(&data[pos..]) { let tail = &data[pos..]; - if tail[5] >> 3 > 10 { + if Syntax::of(tail) == Some(Syntax::Eac3) { let hdr = eac3::parse_from(&mut ec_core::BitReader::new(tail))?; if hdr.substreamid != 0 { return Err(Error::unsupported( @@ -270,9 +270,8 @@ impl Ac3Decoder { // two for E-AC-3 — so read it where both agree it can be found: after // the sync word, E-AC-3 puts strmtyp/substreamid/frmsiz first, and // bsid is at a fixed offset from the frame start in both. - let bsid = data[5] >> 3; let mut r = ec_core::BitReader::new(data); - if bsid <= 10 { + if Syntax::of(data) == Some(Syntax::Ac3) { let sync = syncinfo::parse_from(&mut r)?; let bsi = bsi::parse_from(&mut r)?; if data.len() < sync.frame_size { @@ -543,7 +542,7 @@ fn begins_syncframe(tail: &[u8]) -> bool { if tail.len() < 6 || u16::from_be_bytes([tail[0], tail[1]]) != syncinfo::SYNCWORD { return false; } - if tail[5] >> 3 > 10 { + if Syntax::of(tail) == Some(Syntax::Eac3) { eac3::parse_from(&mut ec_core::BitReader::new(tail)).is_ok() } else { syncinfo::parse(tail).is_ok() diff --git a/crates/ec-ac3/src/dependent.rs b/crates/ec-ac3/src/dependent.rs index 3078a53a..4dcc98b5 100644 --- a/crates/ec-ac3/src/dependent.rs +++ b/crates/ec-ac3/src/dependent.rs @@ -29,6 +29,7 @@ use ec_core::{BitReader, ChannelPosition, Error, Result}; use crate::bsi::Acmod; +use crate::decode::Syntax; use crate::eac3::{self, Eac3Bsi, StreamType}; use crate::syncinfo::SYNCWORD; @@ -46,7 +47,7 @@ pub(crate) struct Channel { pub(crate) fn peek(data: &[u8]) -> Option { // Six bytes are needed for the sync word and bsid; AC-3 (bsid <= 10) has // no dependent substreams. - if data.len() < 6 || u16::from_be_bytes([data[0], data[1]]) != SYNCWORD || data[5] >> 3 <= 10 { + if Syntax::of(data) != Some(Syntax::Eac3) || u16::from_be_bytes([data[0], data[1]]) != SYNCWORD { return None; } let hdr = eac3::parse_from(&mut BitReader::new(data)).ok()?; diff --git a/crates/ec-ac3/src/lib.rs b/crates/ec-ac3/src/lib.rs index da701f30..aed668fa 100644 --- a/crates/ec-ac3/src/lib.rs +++ b/crates/ec-ac3/src/lib.rs @@ -100,7 +100,7 @@ pub fn frame_size(data: &[u8]) -> ec_core::Result { "AC-3: no syncword at the start of the frame", )); } - if data[5] >> 3 <= 10 { + if decode::Syntax::of(data) == Some(decode::Syntax::Ac3) { Ok(syncinfo::parse(data)?.frame_size) } else { Ok(eac3::parse(&data[2..])?.frame_size)