diff --git a/crates/ec-ac3/src/decode.rs b/crates/ec-ac3/src/decode.rs index 3ec3842f..e5a91c15 100644 --- a/crates/ec-ac3/src/decode.rs +++ b/crates/ec-ac3/src/decode.rs @@ -35,6 +35,13 @@ pub enum Syntax { Eac3, } +impl Syntax { + 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 fa56bfdc..94e00880 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 { @@ -398,26 +397,25 @@ impl Ac3Decoder { /// The layout of a frame whose channels sit at `positions` (canonical order). fn layout(&self, positions: &[ChannelPosition]) -> ChannelLayout { match self.options.downmix { - Downmix::Native => layout_of(positions), + Downmix::Native => match positions { + [ChannelPosition::FrontCenter] => ChannelLayout::Mono, + [ChannelPosition::FrontLeft, ChannelPosition::FrontRight] => ChannelLayout::Stereo, + _ if positions == ChannelLayout::Surround5_1.positions() => { + ChannelLayout::Surround5_1 + } + _ if positions == ChannelLayout::Surround7_1.positions() => { + ChannelLayout::Surround7_1 + } + _ => ChannelLayout::Custom(positions.to_vec()), + }, Downmix::Stereo => ChannelLayout::Stereo, Downmix::Mono => ChannelLayout::Mono, } } - /// Apply the requested downmix, if any (§7.8.2). - fn fold(&self, samples: Vec, info: &FrameInfo, positions: &[ChannelPosition]) -> Vec { - if self.options.downmix == Downmix::Native { - return samples; - } - let channels = positions.len(); - if channels == 0 { - return samples; - } + fn downmix_gains(info: &FrameInfo, positions: &[ChannelPosition]) -> (Vec, Vec) { let clev = info.center_mix_level.unwrap_or(0.707); let slev = info.surround_mix_level.unwrap_or(0.707); - // §7.8.2 with the standard's own coefficients, then one scaling so a - // full-scale input cannot overflow the downmix — the standard's fixed - // -7.65 dB would quieten every stereo listener instead. let (mut left, mut right) = (Vec::new(), Vec::new()); for &p in positions { let (l, r) = match p { @@ -438,6 +436,22 @@ impl Ac3Decoder { left.push(l); right.push(r); } + (left, right) + } + + /// Apply the requested downmix, if any (§7.8.2). + fn fold(&self, samples: Vec, info: &FrameInfo, positions: &[ChannelPosition]) -> Vec { + if self.options.downmix == Downmix::Native { + return samples; + } + let channels = positions.len(); + if channels == 0 { + return samples; + } + // The standard's own coefficients, then one scaling so a full-scale + // input cannot overflow the downmix — the standard's fixed -7.65 dB + // would quieten every stereo listener instead. + let (left, right) = Self::downmix_gains(info, positions); let peak = left .iter() .sum::() @@ -513,18 +527,6 @@ impl Decoder for Ac3Decoder { } } -/// The layout of canonically ordered `positions`: the named layouts where they -/// match, a custom one otherwise. -fn layout_of(positions: &[ChannelPosition]) -> ChannelLayout { - match positions { - [ChannelPosition::FrontCenter] => ChannelLayout::Mono, - [ChannelPosition::FrontLeft, ChannelPosition::FrontRight] => ChannelLayout::Stereo, - _ if positions == ChannelLayout::Surround5_1.positions() => ChannelLayout::Surround5_1, - _ if positions == ChannelLayout::Surround7_1.positions() => ChannelLayout::Surround7_1, - _ => ChannelLayout::Custom(positions.to_vec()), - } -} - /// One syncframe as decoded: its interleaved samples in the requested layout, /// the header summary, and the bytes its independent and dependent substreams /// took. @@ -545,7 +547,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) diff --git a/crates/ec-flac/src/encode.rs b/crates/ec-flac/src/encode.rs index 67ad8ff9..c501d387 100644 --- a/crates/ec-flac/src/encode.rs +++ b/crates/ec-flac/src/encode.rs @@ -9,7 +9,7 @@ use ec_core::bitio::BitWriter; use ec_core::error::{Error, Result}; use crate::checksum::{crc8, crc16, md5_of_samples}; -use crate::decode::StreamInfo; +use crate::decode::{ChannelAssignment, StreamInfo}; /// Encoder settings. [`EncoderConfig::default`] is the level the family ships: /// 4096-sample blocks, LPC to order 12, Rice partitions to order 6. @@ -222,7 +222,7 @@ fn encode_frame( .iter() .map(|plane| plan_subframe(config, plane, bits_per_sample, window)) .collect(); - let mut assignment = channels as u32 - 1; + let mut assignment = ChannelAssignment::Independent(channels as u8); if stereo { let mid = plan_subframe(config, &scratch.mid, bits_per_sample, window); // The side channel needs one bit more than the stream's depth, which is @@ -230,23 +230,23 @@ fn encode_frame( let side = plan_subframe(config, &scratch.side, bits_per_sample + 1, window); let (l, r) = (plans[0].bits, plans[1].bits); let (_, best) = [ - (l + r, 1u32), - (l + side.bits, 8), - (side.bits + r, 9), - (mid.bits + side.bits, 10), + (l + r, ChannelAssignment::Independent(2)), + (l + side.bits, ChannelAssignment::LeftSide), + (side.bits + r, ChannelAssignment::RightSide), + (mid.bits + side.bits, ChannelAssignment::MidSide), ] .into_iter() - .min() + .min_by_key(|candidate| candidate.0) .expect("four candidates"); assignment = best; match best { - 8 => plans[1] = side, - 9 => plans[0] = side, - 10 => { + ChannelAssignment::LeftSide => plans[1] = side, + ChannelAssignment::RightSide => plans[0] = side, + ChannelAssignment::MidSide => { plans[0] = mid; plans[1] = side; } - _ => {} + ChannelAssignment::Independent(_) => {} } } @@ -263,7 +263,12 @@ fn encode_frame( let crc = crc8(w.as_bytes()); w.write_bits(u32::from(crc), 8); for (c, plan) in plans.iter().enumerate() { - let side = matches!((assignment, c), (8, 1) | (10, 1) | (9, 0)); + let side = matches!( + (assignment, c), + (ChannelAssignment::LeftSide, 1) + | (ChannelAssignment::MidSide, 1) + | (ChannelAssignment::RightSide, 0) + ); write_subframe(&mut w, plan, bits_per_sample + u32::from(side)); } w.align_to_byte(); @@ -272,11 +277,20 @@ fn encode_frame( w.into_bytes() } +fn assignment_code(assignment: ChannelAssignment) -> u32 { + match assignment { + ChannelAssignment::Independent(n) => u32::from(n) - 1, + ChannelAssignment::LeftSide => 8, + ChannelAssignment::RightSide => 9, + ChannelAssignment::MidSide => 10, + } +} + fn write_frame_header( w: &mut BitWriter, block_size: usize, sample_rate: u32, - assignment: u32, + assignment: ChannelAssignment, bits_per_sample: u32, number: u64, ) { @@ -330,7 +344,7 @@ fn write_frame_header( w.write_bit(false); // fixed block size: the coded number is a frame number w.write_bits(bs_code, 4); w.write_bits(sr_code, 4); - w.write_bits(assignment, 4); + w.write_bits(assignment_code(assignment), 4); w.write_bits(bps_code, 3); w.write_bit(false); // reserved write_utf8_number(w, number); diff --git a/crates/ec-opus/src/celt.rs b/crates/ec-opus/src/celt.rs index 158c3a72..bb44cc56 100644 --- a/crates/ec-opus/src/celt.rs +++ b/crates/ec-opus/src/celt.rs @@ -3001,7 +3001,7 @@ pub(crate) fn exp_rotation( } } -fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { +pub(crate) fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { for i in 0..len.saturating_sub(stride) { let x1 = x[i]; let x2 = x[i + stride]; diff --git a/crates/ec-opus/src/celt_flp/vq.rs b/crates/ec-opus/src/celt_flp/vq.rs index 3cfc2cae..6dac576e 100644 --- a/crates/ec-opus/src/celt_flp/vq.rs +++ b/crates/ec-opus/src/celt_flp/vq.rs @@ -1,5 +1,5 @@ use super::{EPSILON, celt_atan2p_norm, celt_cos_norm, celt_rcp, celt_rsqrt, celt_sqrt}; -use crate::celt::{pvq_u, pvq_v}; +use crate::celt::{exp_rotation1, pvq_u, pvq_v}; use crate::range::RangeEncoder; const SPREAD_FACTOR: [i32; 3] = [15, 10, 5]; @@ -13,22 +13,6 @@ fn inner_prod(x: &[f32], y: &[f32], n: usize) -> f32 { xy } -fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) { - let ms = -s; - for i in 0..len.saturating_sub(stride) { - let x1 = x[i]; - let x2 = x[i + stride]; - x[i + stride] = c * x2 + s * x1; - x[i] = c * x1 + ms * x2; - } - for i in (0..len.saturating_sub(2 * stride)).rev() { - let x1 = x[i]; - let x2 = x[i + stride]; - x[i + stride] = c * x2 + s * x1; - x[i] = c * x1 + ms * x2; - } -} - /// `exp_rotation`. pub(crate) fn exp_rotation( x: &mut [f32],