From a06b46bd9e92df0af2538aa75947147a3691daa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:14:42 +0300 Subject: [PATCH] S10: aac encoder passes WindowSequence instead of a short: bool; Coded drops its duplicate short field (derived from seq); same values --- crates/ec-aac/src/encode.rs | 54 +++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/crates/ec-aac/src/encode.rs b/crates/ec-aac/src/encode.rs index 9ebfae4b..1bd1e082 100644 --- a/crates/ec-aac/src/encode.rs +++ b/crates/ec-aac/src/encode.rs @@ -538,11 +538,11 @@ impl AacEncoder { let mut ms = vec![false; max_sfb]; if *element == Element::Cpe && self.config.mid_side { for b in 0..max_sfb { - if ms_wins(&pair[0], &pair[1], swb, b, short) { + if ms_wins(&pair[0], &pair[1], swb, b, seq) { ms[b] = true; } } - apply_ms(&mut pair, &ms, swb, max_sfb, short); + apply_ms(&mut pair, &ms, swb, max_sfb, seq); } elements.push(ElementPlan { element: *element, @@ -563,7 +563,7 @@ impl AacEncoder { .map(|plan| { plan.frames .iter() - .map(|frame| band_profile(frame, swb, max_sfb, short)) + .map(|frame| band_profile(frame, swb, max_sfb, seq)) .collect() }) .collect(); @@ -759,14 +759,14 @@ impl AacEncoder { max_sfb, b, floor[b], - || self.band_noise(frame, swb, b, floor[b], windows, short), + || self.band_noise(frame, swb, b, floor[b], windows, seq), ) <= target { let (mut lo, mut hi) = (floor[b], 255); while lo < hi { let mid = lo + (hi - lo + 1) / 2; if memo.noise(ch_slot, max_sfb, b, mid, || { - self.band_noise(frame, swb, b, mid, windows, short) + self.band_noise(frame, swb, b, mid, windows, seq) }) <= target { lo = mid; } else { @@ -848,7 +848,7 @@ impl AacEncoder { if books[b] == 0 { continue; } - bits += band_bits(&quant, swb, b, books[b], windows, short); + bits += band_bits(&quant, swb, b, books[b], windows, seq); let value = prev.map_or(60, |p| (60 + sf[b] - p).clamp(0, 120)); bits += i32::from(SCALEFACTOR_CODES[value as usize].0); prev = Some(sf[b]); @@ -871,7 +871,6 @@ impl AacEncoder { books, quant, bits, - short, } } @@ -883,8 +882,9 @@ impl AacEncoder { band: usize, sf: i32, windows: usize, - short: bool, + seq: WindowSequence, ) -> f32 { + let short = seq == WindowSequence::EightShort; let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1])); let gain = pow_quarter(sf - SF_OFFSET); let gain_inv = pow_quarter(SF_OFFSET - sf); @@ -922,12 +922,18 @@ struct Coded { books: Vec, quant: Vec, bits: i32, - short: bool, } /// True when mid/side splits this band's energy more lopsidedly than left/right /// does, which is when it costs fewer bits. -fn ms_wins(l: &ChannelFrame, r: &ChannelFrame, swb: &[u16], band: usize, short: bool) -> bool { +fn ms_wins( + l: &ChannelFrame, + r: &ChannelFrame, + swb: &[u16], + band: usize, + seq: WindowSequence, +) -> bool { + let short = seq == WindowSequence::EightShort; let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1])); let windows = if short { 8 } else { 1 }; let (mut el, mut er, mut em, mut es) = (0.0f64, 0.0f64, 0.0f64, 0.0f64); @@ -958,7 +964,13 @@ struct BandProfile { floor: Vec, } -fn band_profile(frame: &ChannelFrame, swb: &[u16], max_sfb: usize, short: bool) -> BandProfile { +fn band_profile( + frame: &ChannelFrame, + swb: &[u16], + max_sfb: usize, + seq: WindowSequence, +) -> BandProfile { + let short = seq == WindowSequence::EightShort; let windows = if short { 8 } else { 1 }; let mut peak = vec![0.0f32; max_sfb]; let mut energy = vec![0.0f32; max_sfb]; @@ -1024,7 +1036,14 @@ impl NoiseMemo { } } -fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) { +fn apply_ms( + pair: &mut [ChannelFrame], + ms: &[bool], + swb: &[u16], + max_sfb: usize, + seq: WindowSequence, +) { + let short = seq == WindowSequence::EightShort; let windows = if short { 8 } else { 1 }; for b in 0..max_sfb { if !ms[b] { @@ -1125,8 +1144,9 @@ fn band_bits( band: usize, book: u8, windows: usize, - short: bool, + seq: WindowSequence, ) -> i32 { + let short = seq == WindowSequence::EightShort; let cb = &CODEBOOKS[usize::from(book) - 1]; let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1])); let dim = usize::from(cb.dim); @@ -1218,7 +1238,7 @@ fn write_ics_info(w: &mut BitWriter, c: &Coded) { }; w.write_bits(seq, 2); w.write_bit(false); // sine window shape - if c.short { + if c.seq == WindowSequence::EightShort { w.write_bits(c.max_sfb as u32, 4); w.write_bits(0x7F, 7); // one group of eight windows } else { @@ -1235,7 +1255,7 @@ fn write_ics(w: &mut BitWriter, c: &Coded, own_info: bool) { write_ics_info(w, c); } // section_data - let esc_bits = if c.short { 3 } else { 5 }; + let esc_bits = if c.seq == WindowSequence::EightShort { 3 } else { 5 }; let esc = (1u32 << esc_bits) - 1; let mut b = 0usize; while b < c.max_sfb { @@ -1267,7 +1287,7 @@ fn write_ics(w: &mut BitWriter, c: &Coded, own_info: bool) { w.write_bit(false); // tns_data_present w.write_bit(false); // gain_control_data_present // spectral_data - let windows = if c.short { 8 } else { 1 }; + let windows = if c.seq == WindowSequence::EightShort { 8 } else { 1 }; for b in 0..c.max_sfb { let book = c.books[b]; if book == 0 { @@ -1277,7 +1297,7 @@ fn write_ics(w: &mut BitWriter, c: &Coded, own_info: bool) { let dim = usize::from(cb.dim); let (lo, hi) = (usize::from(c.swb[b]), usize::from(c.swb[b + 1])); for win in 0..windows { - let base = if c.short { win * SHORT_LEN } else { 0 }; + let base = if c.seq == WindowSequence::EightShort { win * SHORT_LEN } else { 0 }; let mut k = lo; while k < hi { let end = (k + dim).min(hi);