diff --git a/Cargo.lock b/Cargo.lock index c794d0af..1e4dca36 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -361,7 +361,6 @@ version = "0.1.0" dependencies = [ "ec-core", "ec-dsp", - "ec-matroska", "ec-mp4", "ec-testkit", ] diff --git a/crates/ec-aac/Cargo.toml b/crates/ec-aac/Cargo.toml index 802d3d16..e238da40 100644 --- a/crates/ec-aac/Cargo.toml +++ b/crates/ec-aac/Cargo.toml @@ -12,5 +12,4 @@ ec-dsp = { workspace = true } [dev-dependencies] ec-mp4 = { workspace = true } -ec-matroska = { workspace = true } ec-testkit.workspace = true diff --git a/crates/ec-aac/examples/aacenc_rt.rs b/crates/ec-aac/examples/aacenc_rt.rs deleted file mode 100644 index a7f6e25c..00000000 --- a/crates/ec-aac/examples/aacenc_rt.rs +++ /dev/null @@ -1,105 +0,0 @@ -//! AAC-LC encode speed relative to realtime, which is how the crate's encode -//! perf claim is measured: a deterministic tonal-plus-transient program, ten -//! seconds, at the seat's rates (stereo 256 kbit/s, 5.1 512 kbit/s), the -//! shortest wall of several alternating repetitions. -//! -//! ```text -//! cargo run --release -p ec-aac --example aacenc_rt -//! ``` - -use std::time::Instant; - -use ec_aac::{AacEncoder, AacEncoderConfig}; - -/// Deterministic tonal stacks with periodic noise bursts: enough tonality for -/// the spreading function to bite, enough attacks to exercise the short-block -/// path. -struct Lcg(u64); -impl Lcg { - fn next_f32(&mut self) -> f32 { - self.0 = self - .0 - .wrapping_mul(6364136223846793005) - .wrapping_add(1442695040888963407); - ((self.0 >> 40) as f32 / 16777216.0) * 2.0 - 1.0 - } -} - -fn channel(f0: f64, seed: u64, rate: u32, secs: usize, burst_period: f64) -> Vec { - let mut noise = Lcg(seed); - let mut out = Vec::with_capacity(secs * rate as usize); - for i in 0..secs * rate as usize { - let t = i as f64 / rate as f64; - let frac = (t % burst_period) / burst_period; - let burst = (-frac * 18.0_f64).exp() as f32; - let stack = (f0.sin() * 0.5 - + (f0 * 3.0).sin() * 0.15 - + (f0 * 5.0 + 0.31 * (6.0 * std::f64::consts::TAU * t).sin()).sin() * 0.08) - as f32 - * (0.7 + 0.3 * (0.5 * std::f64::consts::TAU * t).sin()) as f32; - out.push(stack + burst * noise.next_f32() * 0.3); - } - out -} - -fn program(channels: usize, rate: u32, secs: usize) -> Vec { - let stereo = [0x5EED_1234u64, 0xC0FF_EE01]; - let five_one = [0x5EED_1234, 0xC0FF_EE01, 0xABCD_0001, 0xABCD_0002, 0xABCD_0003, 0xABCD_0004]; - let f0s = [220.0f64, 277.0, 330.0, 55.0, 196.0, 246.9]; - let periods = [0.9f64, 0.8, 1.1, 1.3, 0.7, 0.85]; - let seeds: &[u64] = if channels == 2 { - &stereo - } else { - &five_one - }; - let chans: Vec> = (0..channels) - .map(|c| channel(f0s[c], seeds[c], rate, secs, periods[c])) - .collect(); - let mut pcm = Vec::with_capacity(chans[0].len() * channels); - for i in 0..chans[0].len() { - for c in &chans { - pcm.push(c[i]); - } - } - pcm -} - -fn encode(pcm: &[f32], channels: usize, kbps: u32) -> usize { - let mut enc = AacEncoder::new(AacEncoderConfig { - bitrate_bps: kbps * 1_000, - ..Default::default() - }); - enc.push_pcm(pcm, channels as u16, 48_000).expect("pcm accepted"); - enc.finish(); - let mut bytes = 0usize; - while let Ok(p) = enc.next_packet() { - bytes += p.data.len(); - } - bytes -} - -fn main() { - let reps: usize = std::env::var("REPS") - .ok() - .and_then(|s| s.parse().ok()) - .unwrap_or(7); - for (channels, kbps) in [(2usize, 256u32), (6, 512)] { - let pcm = program(channels, 48_000, 10); - let secs = pcm.len() as f64 / (48_000.0 * channels as f64); - let _ = encode(&pcm, channels, kbps); // warm - let mut walls = Vec::with_capacity(reps); - for _ in 0..reps { - let t0 = Instant::now(); - let bytes = encode(&pcm, channels, kbps); - walls.push((t0.elapsed().as_secs_f64(), bytes)); - } - walls.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); - let (wall, bytes) = walls[0]; - println!( - "{channels}ch {kbps}k: min {wall:.4}s = {:.1}x realtime ({} B, {} reps)", - secs / wall, - bytes, - reps - ); - } -} diff --git a/crates/ec-aac/examples/adts_to_pcm.rs b/crates/ec-aac/examples/adts_to_pcm.rs index b1223663..59b06583 100644 --- a/crates/ec-aac/examples/adts_to_pcm.rs +++ b/crates/ec-aac/examples/adts_to_pcm.rs @@ -9,39 +9,7 @@ fn main() { .nth(1) .expect("usage: adts_to_pcm "); let data = std::fs::read(&path).expect("file readable"); - // Plain `AacDecoder::new()` never enables SBR (it only turns on from an - // explicit AudioSpecificConfig's `sbr_present`, per `with_config`) -- - // the probe streams here are raw same-rate-SBR ADTS with no ASC, so - // build one directly rather than parsing bytes we don't have. - let same_rate_sbr = std::env::var("EC_AAC_SBR_SAME_RATE").is_ok(); - let channels: u16 = std::env::var("EC_AAC_SBR_CHANNELS") - .ok() - .and_then(|s| s.parse().ok()) - .unwrap_or(1); - // Round-32: built via the SAME write_audio_specific_config + - // with_config_bytes round trip `wrap_sbr` uses (AOT_SBR, core rate - // 22050 = ADTS sf_index 7's SAMPLE_RATES entry, extension 44100) so this - // path and the reference's wrap_sbr-wrapped mp4 read byte-identical ASC - // semantics -- the prior hand-built config (object_type=2/AAC-LC, - // sample_rate=44100 for what is actually the 22050 Hz *core*) diverged - // from wrap_sbr's ASC, which is why the sanity gate (ours vs - // build_patches) was failing independent of any patch-map bug. - let mut decoder = if same_rate_sbr { - let asc = ec_aac::write_audio_specific_config(&ec_aac::AudioSpecificConfig { - object_type: ec_aac::AOT_SBR, - sample_rate: 22050, - sf_index: 7, - channels, - channel_config: channels as u8, - sbr_present: true, - ps_present: false, - extension_sample_rate: Some(44100), - pce: Vec::new(), - }); - ec_aac::AacDecoder::with_config_bytes(&asc).expect("asc parses") - } else { - ec_aac::AacDecoder::new() - }; + let mut decoder = ec_aac::AacDecoder::new(); let mut at = 0usize; let stdout = std::io::stdout(); let mut out = stdout.lock(); diff --git a/crates/ec-aac/examples/wrap_sbr.rs b/crates/ec-aac/examples/wrap_sbr.rs deleted file mode 100644 index 2d56c8f3..00000000 --- a/crates/ec-aac/examples/wrap_sbr.rs +++ /dev/null @@ -1,109 +0,0 @@ -//! Wraps a bare ADTS stream (the SBR patch-map probes' -//! `scripts/aac-tables/sbrpatchmap.py` output) in an mp4 container carrying -//! an EXPLICIT-SBR AudioSpecificConfig as its `esds` -- the instrument -//! round-30's blocker needed: a bare `.aac` file carries no out-of-band -//! AudioSpecificConfig at all, so a decoder that trusts one (rather than -//! only its bare-ADTS implicit per-frame SBR detection) has nothing to arm -//! HE-AAC output from and locks to core-rate. -//! -//! Matroska was tried first (`MatroskaMuxer`, dev-dep already proven to -//! round-trip `CodecPrivate` byte-for-byte in -//! `crates/ec-aac/tests/sbr_real_library.rs`) but the reference decoder's -//! Matroska demuxer reads the *codec ID string* ("A_AAC" vs. -//! "A_AAC/MPEG4/SBR") for its reported profile, not just the `CodecPrivate` -//! bytes our `MatroskaMuxer` writes generically -- it still upgraded the -//! reported sample rate to the extension rate (proving it DOES parse -//! `CodecPrivate` for that), but kept reporting profile "LC". mp4's `esds` -//! carries the same explicit-SBR AudioSpecificConfig and needs no such -//! sibling string, matching the real `~/Music/reeln.mp4` fixture's -//! own container exactly. -//! -//! `stream()`/`stream_one_band()`'s probe frames are full ADTS (7-byte -//! header + raw_data_block); this strips the header off each frame and -//! remuxes the raw access units into an mp4 track whose `esds` is -//! `write_audio_specific_config` with `object_type = AOT_SBR` and an -//! explicit `extension_sample_rate`. -//! -//! usage: wrap_sbr_mkv -use std::fs::File; - -use ec_aac::{AOT_SBR, AudioSpecificConfig, sf_index_for_rate, write_audio_specific_config}; -use ec_core::{ - AudioParameters, ChannelLayout, CodecId, CodecParameters, MediaParameters, Muxer, Packet, - StreamInfo, TimeBase, -}; -use ec_mp4::Mp4Muxer; - -fn main() { - let args: Vec = std::env::args().collect(); - assert_eq!( - args.len(), - 6, - "usage: wrap_sbr_mkv " - ); - let data = std::fs::read(&args[1]).expect("input readable"); - let core_rate: u32 = args[2].parse().expect("core_rate is a number"); - let ext_rate: u32 = args[3].parse().expect("ext_rate is a number"); - let channels: u16 = args[4].parse().expect("channels is a number"); - - // Strip each ADTS frame down to its raw access unit (no header, no CRC). - let mut aus = Vec::new(); - let mut at = 0usize; - while at + 7 <= data.len() { - let Ok(header) = ec_aac::parse_adts(&data[at..]) else { - break; - }; - let end = (at + header.frame_length).min(data.len()); - aus.push(data[at + header.header_len..end].to_vec()); - at = end; - } - assert!(!aus.is_empty(), "no ADTS frames found in {}", args[1]); - - let sf_index = sf_index_for_rate(core_rate).expect("core_rate is a valid AAC rate"); - let channel_config = channels as u8; - let asc = write_audio_specific_config(&AudioSpecificConfig { - object_type: AOT_SBR, - sample_rate: core_rate, - sf_index, - channels, - channel_config, - sbr_present: true, - ps_present: false, - extension_sample_rate: Some(ext_rate), - pce: Vec::new(), - }); - - let time_base = TimeBase::from_rate(core_rate); - let layout = if channels == 1 { - ChannelLayout::Mono - } else { - ChannelLayout::Stereo - }; - let mut params = CodecParameters::new(CodecId::Aac); - params.extradata = Some(asc.into()); - params.media = MediaParameters::Audio(AudioParameters { - sample_rate: core_rate, - layout, - format: None, - bits_per_sample: None, - }); - let mut info = StreamInfo::new(0, time_base, params); - info.default = true; - - let out = File::create(&args[5]).expect("output creatable"); - let mut muxer = Mp4Muxer::new(out).expect("mp4 muxer opens"); - muxer.add_stream(info).expect("stream declared"); - for (i, au) in aus.iter().enumerate() { - let pts = i as i64 * 1024; - let packet = Packet::new(0, time_base, au.as_slice()) - .with_pts(pts) - .with_duration(1024); - muxer.write_packet(&packet).expect("packet written"); - } - muxer.finish().expect("finished"); - eprintln!( - "wrote {} ({} frames, core={core_rate}Hz ext={ext_rate}Hz ch={channels})", - args[5], - aus.len() - ); -} diff --git a/crates/ec-aac/src/decode.rs b/crates/ec-aac/src/decode.rs index 6160d5c9..61887a8a 100644 --- a/crates/ec-aac/src/decode.rs +++ b/crates/ec-aac/src/decode.rs @@ -59,52 +59,6 @@ impl WindowSequence { } } -/// One `raw_data_block` element's coding-tool usage, gated by -/// `EC_AAC_TOOL_SIDEINFO_DEBUG` -- mirrors `sbr_chain::SbrSideInfoRow`'s -/// pattern for the LC core, so a real file's PNS/M-S/IS/TNS mix can be read -/// back per AU without re-parsing debug text. -#[derive(Clone, Debug)] -pub struct ToolSideInfoRow { - pub au: usize, - pub tag: u8, - pub is_cpe: bool, - pub window_sequence: WindowSequence, - pub ms_bands: usize, - pub pns_bands: usize, - pub is_bands: usize, - pub tns_present: bool, -} - -static TOOL_SIDEINFO_LOG: std::sync::OnceLock>> = - std::sync::OnceLock::new(); -static TOOL_SIDEINFO_AU: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); - -fn tool_sideinfo_enabled() -> bool { - crate::knobs::tool_sideinfo_debug() -} - -fn log_tool_sideinfo(row: ToolSideInfoRow) { - let log = TOOL_SIDEINFO_LOG.get_or_init(|| std::sync::Mutex::new(Vec::new())); - if let Ok(mut l) = log.lock() { - l.push(row); - } -} - -/// Bumps the shared AU counter once per `raw_data_block`, returning the value -/// that call's rows should carry. -fn next_tool_sideinfo_au() -> usize { - TOOL_SIDEINFO_AU.fetch_add(1, std::sync::atomic::Ordering::Relaxed) -} - -/// Every row logged so far this process, in call order. Empty unless -/// `EC_AAC_TOOL_SIDEINFO_DEBUG` was set before decoding. -pub fn tool_sideinfo_log() -> Vec { - let Some(log) = TOOL_SIDEINFO_LOG.get() else { - return Vec::new(); - }; - log.lock().map(|l| l.clone()).unwrap_or_default() -} - /// `ics_info` (§4.4.2.1) plus the grouping it implies. #[derive(Clone, Debug)] pub struct IcsInfo { @@ -845,12 +799,6 @@ impl BlockDecoder { // FIL's SBR payload would apply to, per §4.5.1: a fill_element // directly follows the element it decorates. let mut pending: Option<(u8, usize, usize, bool)> = None; - let dump_tools = tool_sideinfo_enabled(); - let au = if dump_tools { - next_tool_sideinfo_au() - } else { - 0 - }; let mut pns = false; // Speaker per plane so far; `named` drops to false at the first element a PCE does not name. let mut speakers: Vec = Vec::new(); @@ -869,9 +817,6 @@ impl BlockDecoder { let start = out.len(); let mut ch = [self.channel_stream(r, None)?]; pns |= has_pns(&ch[0]); - if dump_tools { - log_tool_sideinfo(tool_row(au, tag, false, &ch[0], None)); - } self.finish(&mut ch, None, &mut out); if let (3, Some(sbr), Some(plane)) = (id, self.sbr.as_mut(), out.get_mut(start)) { sbr.bypass_plane(tag, plane); @@ -919,9 +864,6 @@ impl BlockDecoder { self.channel_stream(r, shared.as_ref())?, ]; pns |= pair.iter().any(has_pns); - if dump_tools { - log_tool_sideinfo(tool_row(au, tag, true, &pair[0], Some(&mask))); - } self.finish(&mut pair, Some(&mask), &mut out); named &= self.note_speakers(1, tag, 2, &mut speakers); pending = Some((tag, start, 2, true)); @@ -1193,43 +1135,12 @@ fn apply_ms(left: &mut Ics, right: &mut Ics, mask: &MsMask) { } } -/// Builds one `EC_AAC_TOOL_SIDEINFO_DEBUG` row from an already-decoded `Ics` -/// (any channel of a CPE, since PNS/IS/TNS presence is per-channel but the -/// dump only needs whether the element used the tool at all). fn has_pns(ch: &Ics) -> bool { ch.cb .iter() .any(|group| group.contains(&NOISE_HCB)) } -fn tool_row(au: usize, tag: u8, is_cpe: bool, ch: &Ics, mask: Option<&MsMask>) -> ToolSideInfoRow { - let mut ms_bands = 0; - let mut pns_bands = 0; - let mut is_bands = 0; - for g in 0..ch.ics.num_groups { - for sfb in 0..ch.ics.max_sfb { - if mask.is_some_and(|m| m.used(g, sfb)) { - ms_bands += 1; - } - match ch.cb[g][sfb] { - NOISE_HCB => pns_bands += 1, - INTENSITY_HCB | INTENSITY_HCB2 => is_bands += 1, - _ => {} - } - } - } - ToolSideInfoRow { - au, - tag, - is_cpe, - window_sequence: ch.ics.window_sequence, - ms_bands, - pns_bands, - is_bands, - tns_present: !ch.tns.filters.is_empty(), - } -} - fn is_intensity(cb: u8) -> bool { cb == INTENSITY_HCB || cb == INTENSITY_HCB2 } diff --git a/crates/ec-aac/src/knobs.rs b/crates/ec-aac/src/knobs.rs deleted file mode 100644 index 6bb16ed7..00000000 --- a/crates/ec-aac/src/knobs.rs +++ /dev/null @@ -1,104 +0,0 @@ -//! Debug switches the AAC/SBR decoder reads from the environment, with a -//! per-thread override that tests set instead of editing the process -//! environment (which is `unsafe` in Rust 2024). -//! -//! The decoder reads these on the thread that calls `decode`, so an override -//! set on a test's own thread covers exactly that test's decodes and nothing -//! running in parallel. With no override set, each reader answers from the -//! environment exactly as it always has, so manual runs with `EC_AAC_*` set -//! behave as before. - -use std::cell::Cell; -use std::thread::LocalKey; - -type Slot = Cell>; - -thread_local! { - static TOOL_SIDEINFO_DEBUG: Slot = const { Cell::new(None) }; - static SBR_SIDEINFO_DEBUG: Slot = const { Cell::new(None) }; - static SBR_HF_BYPASS: Slot = const { Cell::new(None) }; - static SBR_NOISE_ZERO: Slot = const { Cell::new(None) }; - static SBR_NOISE_FRACTION: Slot = const { Cell::new(None) }; -} - -/// The override on this thread if one is set, else whether `var` is set. -fn enabled(slot: &'static LocalKey, var: &str) -> bool { - slot.with(Cell::get) - .unwrap_or_else(|| std::env::var(var).is_ok()) -} - -fn force(slot: &'static LocalKey, on: Option) -> Override> { - let prev = slot.with(|c| c.replace(on)); - Override { slot, prev } -} - -/// Restores the value a knob had before [`set_tool_sideinfo_debug`] and its -/// siblings were called, when dropped. -#[doc(hidden)] -#[must_use = "the override ends as soon as this guard is dropped"] -pub struct Override { - slot: &'static LocalKey>, - prev: T, -} - -impl Drop for Override { - fn drop(&mut self) { - self.slot.with(|c| c.set(self.prev)); - } -} - -/// `EC_AAC_TOOL_SIDEINFO_DEBUG`. -pub(crate) fn tool_sideinfo_debug() -> bool { - enabled(&TOOL_SIDEINFO_DEBUG, "EC_AAC_TOOL_SIDEINFO_DEBUG") -} - -/// `EC_AAC_SBR_SIDEINFO_DEBUG`. -pub(crate) fn sbr_sideinfo_debug() -> bool { - enabled(&SBR_SIDEINFO_DEBUG, "EC_AAC_SBR_SIDEINFO_DEBUG") -} - -/// `EC_AAC_SBR_HF_BYPASS`. -pub(crate) fn sbr_hf_bypass() -> bool { - enabled(&SBR_HF_BYPASS, "EC_AAC_SBR_HF_BYPASS") -} - -/// `EC_AAC_SBR_NOISE_ZERO`. -pub(crate) fn sbr_noise_zero() -> bool { - enabled(&SBR_NOISE_ZERO, "EC_AAC_SBR_NOISE_ZERO") -} - -/// `EC_AAC_SBR_NOISE_FRACTION`. -pub(crate) fn sbr_noise_fraction() -> bool { - enabled(&SBR_NOISE_FRACTION, "EC_AAC_SBR_NOISE_FRACTION") -} - -/// Forces `EC_AAC_TOOL_SIDEINFO_DEBUG` on (`Some(true)`), off (`Some(false)`) -/// or back to the environment (`None`) on this thread, until the guard drops. -#[doc(hidden)] -pub fn set_tool_sideinfo_debug(on: Option) -> Override> { - force(&TOOL_SIDEINFO_DEBUG, on) -} - -/// [`set_tool_sideinfo_debug`] for `EC_AAC_SBR_SIDEINFO_DEBUG`. -#[doc(hidden)] -pub fn set_sbr_sideinfo_debug(on: Option) -> Override> { - force(&SBR_SIDEINFO_DEBUG, on) -} - -/// [`set_tool_sideinfo_debug`] for `EC_AAC_SBR_HF_BYPASS`. -#[doc(hidden)] -pub fn set_sbr_hf_bypass(on: Option) -> Override> { - force(&SBR_HF_BYPASS, on) -} - -/// [`set_tool_sideinfo_debug`] for `EC_AAC_SBR_NOISE_ZERO`. -#[doc(hidden)] -pub fn set_sbr_noise_zero(on: Option) -> Override> { - force(&SBR_NOISE_ZERO, on) -} - -/// [`set_tool_sideinfo_debug`] for `EC_AAC_SBR_NOISE_FRACTION`. -#[doc(hidden)] -pub fn set_sbr_noise_fraction(on: Option) -> Override> { - force(&SBR_NOISE_FRACTION, on) -} diff --git a/crates/ec-aac/src/lib.rs b/crates/ec-aac/src/lib.rs index 2a96f88e..db2aeb40 100644 --- a/crates/ec-aac/src/lib.rs +++ b/crates/ec-aac/src/lib.rs @@ -35,15 +35,13 @@ mod sbr_chain; mod sbr_env; mod sbr_hf; mod sbr_payload; -pub mod sbr_qmf; +mod sbr_qmf; mod sbr_qmf_window; mod ps_data; mod ps_dsp; mod ps_huff; mod sbr_tables; -pub mod tables; -#[doc(hidden)] -pub mod knobs; +mod tables; pub use config::{ AOT_AAC_LC, AOT_PS, AOT_SBR, AdtsHeader, AudioSpecificConfig, ProgramConfig, @@ -51,16 +49,9 @@ pub use config::{ parse_audio_specific_config, parse_program_config, sample_rate_for_index, sf_index_for_rate, write_adts_header, write_audio_specific_config, }; -pub use decode::{FRAME_LEN, ToolSideInfoRow, WindowSequence, tool_sideinfo_log}; +pub use decode::{FRAME_LEN, WindowSequence}; pub use ec_core::{Error, Result}; pub use encode::{AacEncoder, AacEncoderConfig, EncodedPacket, WindowShape}; -/// Diagnostic only (round-20): per-(access-unit, channel) side-info rows -/// logged during decode when `EC_AAC_SBR_SIDEINFO_DEBUG` is set. -pub use sbr_chain::{SbrSideInfoRow, hold_call_count, sbr_sideinfo_log}; -/// Diagnostic only (round-17 Task 1 conviction check): reads back the -/// per-absolute-QMF-band transmitted signal/noise energy sums -/// `EC_AAC_SBR_NOISE_FRACTION_DEBUG` accumulates during decode. -pub use sbr_env::noise_fraction_table; pub use tables::SAMPLE_RATES; use decode::BlockDecoder; @@ -74,9 +65,6 @@ pub enum SbrSupport { /// SBR is signalled and **is** reconstructed: the decoded audio spans /// the full extension bandwidth at the extension sample rate. V1, - /// SBR is signalled but not reconstructed. Not produced by this decoder - /// any more: Parametric Stereo streams report [`SbrSupport::V1`]. - CoreOnly, } /// One decoded frame: interleaved `f32` in film channel order. @@ -305,7 +293,9 @@ impl AacDecoder { let frames = planes[0].len(); let mut samples = vec![0.0f32; frames * channels]; for (out_ch, &src) in order.iter().take(channels).enumerate() { - let plane = &planes[src.min(channels - 1)]; + let plane = planes + .get(src) + .ok_or_else(|| Error::corrupt("aac: channel order names a missing plane"))?; // Planes can differ in length after `merge`; only the first `frames` samples fit. for (i, &v) in plane.iter().take(frames).enumerate() { samples[i * channels + out_ch] = v; diff --git a/crates/ec-aac/src/sbr_bands/tests.rs b/crates/ec-aac/src/sbr_bands/tests.rs index 3b451090..c856d8d3 100644 --- a/crates/ec-aac/src/sbr_bands/tests.rs +++ b/crates/ec-aac/src/sbr_bands/tests.rs @@ -2,7 +2,7 @@ use super::*; use super::vectors::VECTORS; #[test] -fn matches_reference_derivation_on_500_configurations() { +fn matches_reference_derivation() { let mut mismatches = 0; for &( rate, diff --git a/crates/ec-aac/src/sbr_bands/vectors/mod.rs b/crates/ec-aac/src/sbr_bands/vectors/mod.rs index e25f1818..008889b6 100644 --- a/crates/ec-aac/src/sbr_bands/vectors/mod.rs +++ b/crates/ec-aac/src/sbr_bands/vectors/mod.rs @@ -4,7 +4,6 @@ //! `scripts/aac-tables/sbrtables.py`'s `freq_tables` if that function ever changes. mod part0; -mod part1; /// `(sbr_rate, start_freq, stop_freq, freq_scale, alter_scale, noise_bands, /// xover_band, expected)`, `expected` being `(n_master, n_high, n_low, n_q, @@ -22,4 +21,4 @@ pub(super) type Vector = ( ); /// All reference vectors, in two parts. -pub(super) static VECTORS: &[&[Vector]] = &[part0::PART, part1::PART]; +pub(super) static VECTORS: &[&[Vector]] = &[part0::PART]; diff --git a/crates/ec-aac/src/sbr_bands/vectors/part0.rs b/crates/ec-aac/src/sbr_bands/vectors/part0.rs index b358e18a..a0f7f980 100644 --- a/crates/ec-aac/src/sbr_bands/vectors/part0.rs +++ b/crates/ec-aac/src/sbr_bands/vectors/part0.rs @@ -481,3825 +481,4 @@ pub(super) static PART: &[Vector] = &[ 5, Some((8, 3, 2, 1, &[36, 38, 40, 42], &[36, 38, 42], &[36, 42])), ), - ( - 24000, - 5, - 14, - 0, - 0, - 0, - 7, - Some(( - 16, - 9, - 5, - 1, - &[24, 25, 26, 27, 28, 29, 30, 31, 32, 34], - &[24, 25, 27, 29, 31, 34], - &[24, 34], - )), - ), - (22050, 14, 3, 2, 0, 3, 6, None), - ( - 96000, - 6, - 7, - 3, - 0, - 1, - 6, - Some(( - 12, - 6, - 3, - 1, - &[18, 20, 22, 24, 26, 28, 31], - &[18, 22, 26, 31], - &[18, 31], - )), - ), - ( - 22050, - 12, - 3, - 1, - 0, - 1, - 7, - Some((8, 1, 1, 1, &[37, 40], &[37, 40], &[37, 40])), - ), - ( - 32000, - 2, - 7, - 0, - 0, - 0, - 5, - Some(( - 32, - 27, - 14, - 1, - &[ - 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, - 39, 40, 41, 42, 43, 44, 45, 46, - ], - &[19, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46], - &[19, 46], - )), - ), - (96000, 15, 3, 2, 1, 1, 0, None), - ( - 96000, - 4, - 8, - 3, - 1, - 2, - 1, - Some(( - 14, - 13, - 7, - 4, - &[10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 25, 28, 31, 35], - &[10, 11, 13, 15, 18, 22, 28, 35], - &[10, 11, 15, 22, 35], - )), - ), - ( - 96000, - 4, - 9, - 3, - 0, - 0, - 1, - Some(( - 16, - 15, - 8, - 1, - &[ - 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24, 26, 29, 32, 35, 39, - ], - &[10, 11, 13, 15, 18, 22, 26, 32, 39], - &[10, 39], - )), - ), - ( - 22050, - 15, - 13, - 0, - 0, - 2, - 0, - Some(( - 34, - 34, - 17, - 2, - &[ - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, - ], - &[ - 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, - ], - &[30, 46, 64], - )), - ), - (96000, 13, 0, 0, 0, 0, 1, None), - ( - 64000, - 7, - 3, - 2, - 0, - 1, - 7, - Some((8, 1, 1, 1, &[24, 26], &[24, 26], &[24, 26])), - ), - ( - 64000, - 0, - 7, - 0, - 1, - 0, - 0, - Some(( - 16, - 16, - 8, - 1, - &[ - 6, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, - ], - &[6, 9, 13, 17, 21, 25, 29, 33, 37], - &[6, 37], - )), - ), - (64000, 1, 7, 2, 1, 0, 2, None), - ( - 24000, - 6, - 8, - 1, - 1, - 1, - 5, - Some(( - 16, - 11, - 6, - 1, - &[23, 24, 26, 28, 30, 32, 34, 36, 39, 42, 45, 49], - &[23, 24, 28, 32, 36, 42, 49], - &[23, 49], - )), - ), - ( - 16000, - 15, - 1, - 2, - 0, - 2, - 2, - Some((6, 4, 2, 1, &[36, 39, 42, 45, 49], &[36, 42, 49], &[36, 49])), - ), - ( - 22050, - 7, - 9, - 1, - 0, - 1, - 0, - Some(( - 18, - 18, - 9, - 1, - &[ - 19, 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 49, 52, - ], - &[19, 21, 23, 26, 30, 34, 38, 42, 46, 52], - &[19, 52], - )), - ), - ( - 32000, - 7, - 5, - 3, - 1, - 3, - 4, - Some(( - 10, - 6, - 3, - 2, - &[26, 28, 30, 33, 36, 39, 42], - &[26, 30, 36, 42], - &[26, 30, 42], - )), - ), - (96000, 15, 11, 2, 0, 1, 6, None), - (48000, 10, 11, 0, 1, 0, 0, None), - ( - 32000, - 6, - 1, - 2, - 1, - 2, - 5, - Some(( - 10, - 5, - 3, - 1, - &[24, 26, 28, 30, 32, 34], - &[24, 26, 30, 34], - &[24, 34], - )), - ), - (88200, 6, 3, 1, 0, 1, 6, None), - ( - 88200, - 9, - 7, - 0, - 1, - 0, - 6, - Some((10, 4, 2, 1, &[25, 27, 29, 31, 33], &[25, 29, 33], &[25, 33])), - ), - (96000, 14, 2, 2, 0, 0, 1, None), - (32000, 6, 11, 2, 1, 1, 3, None), - ( - 16000, - 2, - 0, - 2, - 1, - 3, - 3, - Some(( - 14, - 11, - 6, - 4, - &[21, 23, 25, 27, 29, 31, 33, 36, 39, 42, 45, 48], - &[21, 23, 27, 31, 36, 42, 48], - &[21, 23, 27, 36, 48], - )), - ), - ( - 44100, - 10, - 5, - 0, - 1, - 0, - 2, - Some(( - 8, - 6, - 3, - 1, - &[22, 24, 26, 28, 30, 32, 34], - &[22, 26, 30, 34], - &[22, 34], - )), - ), - (88200, 14, 5, 1, 1, 2, 6, None), - (64000, 8, 11, 2, 1, 0, 4, None), - ( - 32000, - 15, - 8, - 2, - 0, - 0, - 3, - Some((6, 3, 2, 1, &[40, 43, 46, 49], &[40, 43, 49], &[40, 49])), - ), - ( - 32000, - 11, - 0, - 2, - 0, - 0, - 2, - Some((4, 2, 1, 1, &[27, 29, 32], &[27, 32], &[27, 32])), - ), - (88200, 7, 7, 1, 1, 1, 1, None), - ( - 48000, - 9, - 0, - 0, - 0, - 0, - 0, - Some((4, 4, 2, 1, &[17, 18, 19, 20, 21], &[17, 19, 21], &[17, 21])), - ), - (48000, 12, 13, 0, 0, 0, 5, None), - ( - 96000, - 15, - 12, - 1, - 0, - 0, - 7, - Some((10, 3, 2, 1, &[47, 50, 53, 57], &[47, 50, 57], &[47, 57])), - ), - (96000, 14, 0, 3, 1, 2, 0, None), - ( - 64000, - 6, - 14, - 3, - 1, - 0, - 1, - Some(( - 8, - 7, - 4, - 1, - &[14, 15, 16, 18, 20, 22, 24, 26], - &[14, 15, 18, 22, 26], - &[14, 26], - )), - ), - (64000, 1, 2, 3, 0, 3, 7, None), - ( - 48000, - 8, - 8, - 2, - 0, - 1, - 7, - Some(( - 14, - 7, - 4, - 1, - &[26, 28, 30, 32, 34, 36, 38, 41], - &[26, 28, 32, 36, 41], - &[26, 41], - )), - ), - (48000, 7, 1, 3, 0, 1, 6, None), - ( - 32000, - 2, - 15, - 0, - 1, - 0, - 0, - Some(( - 14, - 14, - 7, - 1, - &[14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42], - &[14, 18, 22, 26, 30, 34, 38, 42], - &[14, 42], - )), - ), - (64000, 4, 5, 1, 0, 1, 0, None), - ( - 32000, - 2, - 5, - 1, - 0, - 1, - 1, - Some(( - 20, - 19, - 10, - 1, - &[ - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 28, 29, 30, 32, 34, 36, 38, 40, 42, - ], - &[15, 16, 18, 20, 22, 24, 28, 30, 34, 38, 42], - &[15, 42], - )), - ), - (64000, 11, 4, 2, 0, 3, 6, None), - ( - 16000, - 4, - 13, - 2, - 0, - 3, - 5, - Some(( - 16, - 11, - 6, - 4, - &[28, 30, 32, 34, 37, 40, 43, 47, 51, 55, 59, 64], - &[28, 30, 34, 40, 47, 55, 64], - &[28, 30, 34, 47, 64], - )), - ), - ( - 64000, - 15, - 14, - 0, - 0, - 1, - 3, - Some(( - 30, - 27, - 14, - 1, - &[ - 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, - 53, 54, 55, 56, 57, 58, 59, 60, - ], - &[33, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60], - &[33, 60], - )), - ), - ( - 22050, - 8, - 15, - 0, - 0, - 0, - 3, - Some(( - 40, - 37, - 19, - 1, - &[ - 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, - 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, - ], - &[ - 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, - ], - &[23, 60], - )), - ), - (44100, 1, 4, 1, 0, 3, 1, None), - ( - 16000, - 2, - 2, - 2, - 0, - 2, - 4, - Some(( - 14, - 10, - 5, - 2, - &[23, 25, 27, 29, 31, 33, 36, 39, 42, 46, 50], - &[23, 27, 31, 36, 42, 50], - &[23, 31, 50], - )), - ), - ( - 44100, - 10, - 4, - 3, - 0, - 3, - 1, - Some(( - 6, - 5, - 3, - 2, - &[20, 22, 24, 26, 28, 31], - &[20, 22, 26, 31], - &[20, 22, 31], - )), - ), - ( - 22050, - 8, - 12, - 0, - 0, - 3, - 7, - Some(( - 40, - 33, - 17, - 4, - &[ - 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, - 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, - ], - &[ - 27, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 61, - ], - &[27, 34, 42, 50, 61], - )), - ), - ( - 24000, - 9, - 5, - 1, - 0, - 2, - 2, - Some(( - 12, - 10, - 5, - 2, - &[23, 24, 25, 27, 29, 31, 33, 35, 37, 39, 42], - &[23, 25, 29, 33, 37, 42], - &[23, 29, 42], - )), - ), - ( - 24000, - 8, - 10, - 3, - 0, - 0, - 1, - Some(( - 12, - 11, - 6, - 1, - &[22, 24, 26, 28, 31, 34, 37, 40, 43, 47, 51, 55], - &[22, 24, 28, 34, 40, 47, 55], - &[22, 55], - )), - ), - ( - 48000, - 13, - 7, - 0, - 0, - 1, - 5, - Some(( - 14, - 9, - 5, - 1, - &[29, 30, 31, 32, 33, 34, 35, 36, 37, 38], - &[29, 30, 32, 34, 36, 38], - &[29, 38], - )), - ), - (64000, 13, 5, 2, 1, 3, 7, None), - (24000, 0, 13, 2, 0, 2, 7, None), - ( - 64000, - 2, - 14, - 3, - 0, - 1, - 4, - Some((8, 4, 2, 1, &[13, 14, 15, 16, 18], &[13, 15, 18], &[13, 18])), - ), - ( - 88200, - 14, - 10, - 2, - 1, - 0, - 3, - Some(( - 8, - 5, - 3, - 1, - &[33, 35, 37, 40, 43, 46], - &[33, 35, 40, 46], - &[33, 46], - )), - ), - (88200, 5, 12, 2, 1, 1, 4, None), - (64000, 3, 13, 0, 0, 0, 2, None), - ( - 16000, - 4, - 10, - 2, - 1, - 3, - 7, - Some(( - 14, - 7, - 4, - 3, - &[32, 34, 37, 40, 44, 48, 53, 58], - &[32, 34, 40, 48, 58], - &[32, 34, 40, 58], - )), - ), - ( - 24000, - 2, - 9, - 1, - 1, - 0, - 5, - Some(( - 20, - 15, - 8, - 1, - &[ - 19, 20, 21, 22, 23, 24, 26, 28, 30, 32, 35, 38, 41, 44, 48, 52, - ], - &[19, 20, 22, 24, 28, 32, 38, 44, 52], - &[19, 52], - )), - ), - ( - 24000, - 12, - 8, - 2, - 1, - 2, - 7, - Some((10, 3, 2, 1, &[40, 43, 46, 49], &[40, 43, 49], &[40, 49])), - ), - ( - 32000, - 6, - 2, - 0, - 1, - 0, - 4, - Some(( - 10, - 6, - 3, - 1, - &[24, 26, 28, 30, 32, 34, 36], - &[24, 28, 32, 36], - &[24, 36], - )), - ), - (96000, 12, 4, 0, 1, 2, 2, None), - (88200, 9, 12, 3, 1, 0, 3, None), - ( - 44100, - 2, - 3, - 0, - 1, - 1, - 2, - Some(( - 10, - 8, - 4, - 1, - &[13, 15, 17, 19, 21, 23, 25, 27, 29], - &[13, 17, 21, 25, 29], - &[13, 29], - )), - ), - ( - 24000, - 4, - 2, - 2, - 0, - 0, - 4, - Some(( - 12, - 8, - 4, - 1, - &[20, 22, 24, 26, 28, 30, 32, 34, 36], - &[20, 24, 28, 32, 36], - &[20, 36], - )), - ), - (44100, 14, 1, 2, 1, 3, 7, None), - ( - 16000, - 14, - 13, - 0, - 0, - 3, - 5, - Some(( - 34, - 29, - 15, - 3, - &[ - 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, - 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, - ], - &[ - 35, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, - ], - &[35, 44, 54, 64], - )), - ), - ( - 24000, - 5, - 4, - 1, - 0, - 1, - 4, - Some(( - 14, - 10, - 5, - 1, - &[21, 22, 23, 24, 26, 28, 30, 32, 34, 37, 40], - &[21, 23, 26, 30, 34, 40], - &[21, 40], - )), - ), - ( - 32000, - 12, - 2, - 1, - 0, - 3, - 2, - Some((6, 4, 2, 1, &[28, 30, 32, 34, 36], &[28, 32, 36], &[28, 36])), - ), - ( - 96000, - 4, - 1, - 3, - 1, - 3, - 1, - Some(( - 6, - 5, - 3, - 2, - &[10, 11, 12, 13, 14, 15], - &[10, 11, 13, 15], - &[10, 11, 15], - )), - ), - ( - 64000, - 5, - 14, - 0, - 0, - 1, - 4, - Some(( - 12, - 8, - 4, - 1, - &[16, 17, 18, 19, 20, 21, 22, 23, 24], - &[16, 18, 20, 22, 24], - &[16, 24], - )), - ), - ( - 24000, - 13, - 8, - 2, - 0, - 1, - 0, - Some(( - 8, - 8, - 4, - 1, - &[27, 29, 31, 33, 36, 39, 42, 45, 49], - &[27, 31, 36, 42, 49], - &[27, 49], - )), - ), - ( - 96000, - 6, - 2, - 2, - 1, - 0, - 3, - Some((6, 3, 2, 1, &[14, 15, 16, 17], &[14, 15, 17], &[14, 17])), - ), - ( - 88200, - 7, - 6, - 3, - 0, - 3, - 4, - Some(( - 10, - 6, - 3, - 3, - &[16, 18, 20, 22, 24, 26, 29], - &[16, 20, 24, 29], - &[16, 20, 24, 29], - )), - ), - (88200, 2, 14, 1, 0, 0, 5, None), - ( - 96000, - 6, - 3, - 3, - 0, - 3, - 1, - Some(( - 6, - 5, - 3, - 2, - &[12, 13, 14, 15, 17, 19], - &[12, 13, 15, 19], - &[12, 13, 19], - )), - ), - (32000, 0, 3, 1, 1, 2, 6, None), - ( - 48000, - 14, - 5, - 1, - 0, - 2, - 0, - Some((2, 2, 1, 1, &[27, 29, 32], &[27, 32], &[27, 32])), - ), - (64000, 3, 8, 1, 1, 3, 5, None), - ( - 22050, - 13, - 0, - 3, - 0, - 2, - 0, - Some((4, 4, 2, 1, &[26, 28, 30, 32, 35], &[26, 30, 35], &[26, 35])), - ), - ( - 44100, - 0, - 8, - 0, - 1, - 2, - 1, - Some(( - 18, - 17, - 9, - 5, - &[ - 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, - ], - &[9, 11, 15, 19, 23, 27, 31, 35, 39, 43], - &[9, 11, 19, 27, 35, 43], - )), - ), - ( - 48000, - 9, - 14, - 3, - 1, - 2, - 7, - Some((8, 1, 1, 1, &[31, 34], &[31, 34], &[31, 34])), - ), - (88200, 13, 6, 1, 1, 0, 7, None), - (88200, 2, 3, 3, 1, 3, 6, None), - ( - 64000, - 0, - 0, - 0, - 0, - 0, - 1, - Some(( - 14, - 13, - 7, - 1, - &[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], - &[7, 8, 10, 12, 14, 16, 18, 20], - &[7, 20], - )), - ), - (44100, 0, 5, 1, 0, 1, 4, None), - (48000, 0, 13, 2, 1, 2, 3, None), - ( - 44100, - 14, - 6, - 1, - 1, - 3, - 2, - Some((4, 2, 1, 1, &[32, 34, 37], &[32, 37], &[32, 37])), - ), - (96000, 2, 10, 0, 0, 1, 0, None), - (88200, 3, 11, 3, 1, 1, 3, None), - ( - 64000, - 13, - 11, - 0, - 1, - 2, - 3, - Some(( - 16, - 13, - 7, - 2, - &[28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54], - &[28, 30, 34, 38, 42, 46, 50, 54], - &[28, 38, 54], - )), - ), - (64000, 14, 0, 3, 1, 1, 1, None), - ( - 24000, - 11, - 13, - 2, - 0, - 1, - 0, - Some(( - 14, - 14, - 7, - 1, - &[23, 24, 26, 28, 30, 32, 34, 37, 40, 43, 46, 50, 54, 59, 64], - &[23, 26, 30, 34, 40, 46, 54, 64], - &[23, 64], - )), - ), - ( - 32000, - 11, - 8, - 0, - 1, - 1, - 1, - Some(( - 14, - 13, - 7, - 1, - &[24, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49], - &[24, 25, 29, 33, 37, 41, 45, 49], - &[24, 49], - )), - ), - ( - 96000, - 11, - 6, - 0, - 0, - 2, - 2, - Some(( - 8, - 6, - 3, - 1, - &[20, 21, 22, 23, 24, 25, 27], - &[20, 22, 24, 27], - &[20, 27], - )), - ), - (44100, 0, 14, 2, 1, 1, 5, None), - ( - 32000, - 6, - 2, - 3, - 0, - 2, - 2, - Some(( - 8, - 6, - 3, - 2, - &[21, 23, 25, 27, 30, 33, 36], - &[21, 25, 30, 36], - &[21, 25, 36], - )), - ), - ( - 32000, - 3, - 3, - 3, - 0, - 3, - 1, - Some(( - 10, - 9, - 5, - 4, - &[16, 17, 19, 21, 23, 25, 27, 30, 34, 38], - &[16, 17, 21, 25, 30, 38], - &[16, 17, 21, 25, 38], - )), - ), - ( - 44100, - 14, - 12, - 0, - 1, - 1, - 6, - Some(( - 16, - 10, - 5, - 1, - &[39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59], - &[39, 43, 47, 51, 55, 59], - &[39, 59], - )), - ), - ( - 24000, - 11, - 8, - 0, - 1, - 1, - 6, - Some(( - 14, - 8, - 4, - 1, - &[33, 35, 37, 39, 41, 43, 45, 47, 49], - &[33, 37, 41, 45, 49], - &[33, 49], - )), - ), - ( - 22050, - 1, - 9, - 2, - 0, - 0, - 3, - Some(( - 20, - 17, - 9, - 1, - &[ - 16, 17, 18, 19, 20, 22, 24, 26, 28, 30, 32, 34, 36, 39, 42, 45, 48, 52, - ], - &[16, 17, 19, 22, 26, 30, 34, 39, 45, 52], - &[16, 52], - )), - ), - ( - 16000, - 3, - 8, - 0, - 1, - 2, - 2, - Some(( - 18, - 16, - 8, - 3, - &[ - 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, - ], - &[23, 27, 31, 35, 39, 43, 47, 51, 56], - &[23, 31, 43, 56], - )), - ), - (44100, 4, 1, 1, 0, 0, 3, None), - ( - 64000, - 10, - 1, - 1, - 0, - 2, - 1, - Some((4, 3, 2, 1, &[18, 19, 20, 22], &[18, 19, 22], &[18, 22])), - ), - ( - 24000, - 1, - 3, - 0, - 0, - 0, - 2, - Some(( - 24, - 22, - 11, - 1, - &[ - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, - 35, 36, 38, - ], - &[15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 38], - &[15, 38], - )), - ), - (24000, 11, 1, 3, 1, 2, 5, None), - ( - 24000, - 5, - 7, - 1, - 0, - 2, - 7, - Some(( - 18, - 11, - 6, - 2, - &[24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46], - &[24, 26, 30, 34, 38, 42, 46], - &[24, 34, 46], - )), - ), - ( - 88200, - 14, - 10, - 0, - 1, - 3, - 4, - Some(( - 10, - 6, - 3, - 1, - &[34, 36, 38, 40, 42, 44, 46], - &[34, 38, 42, 46], - &[34, 46], - )), - ), - (64000, 13, 12, 3, 1, 1, 6, None), - ( - 96000, - 11, - 10, - 2, - 1, - 0, - 5, - Some(( - 12, - 7, - 4, - 1, - &[25, 27, 29, 31, 33, 36, 40, 44], - &[25, 27, 31, 36, 44], - &[25, 44], - )), - ), - ( - 44100, - 3, - 6, - 0, - 0, - 3, - 6, - Some(( - 24, - 18, - 9, - 3, - &[ - 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, - ], - &[18, 20, 22, 24, 26, 28, 30, 32, 34, 37], - &[18, 24, 30, 37], - )), - ), - (88200, 12, 13, 1, 1, 0, 0, None), - ( - 96000, - 12, - 7, - 1, - 0, - 2, - 2, - Some(( - 8, - 6, - 3, - 1, - &[22, 23, 24, 25, 27, 29, 31], - &[22, 24, 27, 31], - &[22, 31], - )), - ), - (44100, 3, 4, 1, 1, 2, 5, None), - ( - 88200, - 5, - 7, - 0, - 0, - 1, - 4, - Some(( - 22, - 18, - 9, - 1, - &[ - 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, - ], - &[14, 16, 18, 20, 22, 24, 26, 28, 30, 33], - &[14, 33], - )), - ), - (32000, 0, 11, 1, 0, 1, 3, None), - ( - 48000, - 4, - 8, - 2, - 0, - 0, - 6, - Some(( - 18, - 12, - 6, - 1, - &[18, 19, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 41], - &[18, 20, 24, 28, 32, 36, 41], - &[18, 41], - )), - ), - ( - 88200, - 14, - 10, - 3, - 0, - 3, - 5, - Some((6, 1, 1, 1, &[42, 46], &[42, 46], &[42, 46])), - ), - ( - 16000, - 10, - 2, - 1, - 1, - 0, - 1, - Some(( - 12, - 11, - 6, - 1, - &[27, 28, 30, 32, 34, 36, 38, 40, 42, 44, 47, 50], - &[27, 28, 32, 36, 40, 44, 50], - &[27, 50], - )), - ), - ( - 32000, - 10, - 8, - 0, - 0, - 2, - 0, - Some(( - 26, - 26, - 13, - 2, - &[ - 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, - 42, 43, 44, 45, 46, 47, 49, - ], - &[22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 49], - &[22, 34, 49], - )), - ), - (64000, 6, 15, 1, 0, 0, 0, None), - ( - 44100, - 1, - 8, - 0, - 1, - 1, - 3, - Some(( - 16, - 13, - 7, - 1, - &[16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43], - &[16, 18, 22, 26, 30, 34, 38, 43], - &[16, 43], - )), - ), - ( - 96000, - 12, - 11, - 3, - 1, - 2, - 4, - Some(( - 10, - 6, - 3, - 2, - &[28, 31, 34, 37, 40, 45, 50], - &[28, 34, 40, 50], - &[28, 34, 50], - )), - ), - (96000, 3, 0, 3, 0, 2, 0, None), - ( - 48000, - 12, - 4, - 3, - 1, - 3, - 0, - Some((4, 4, 2, 1, &[22, 23, 25, 27, 29], &[22, 25, 29], &[22, 29])), - ), - (96000, 3, 11, 2, 1, 2, 2, None), - ( - 44100, - 2, - 5, - 0, - 1, - 3, - 3, - Some(( - 12, - 9, - 5, - 3, - &[16, 18, 20, 22, 24, 26, 28, 30, 32, 34], - &[16, 18, 22, 26, 30, 34], - &[16, 18, 26, 34], - )), - ), - (44100, 11, 1, 1, 0, 3, 4, None), - (88200, 6, 1, 2, 1, 0, 6, None), - ( - 16000, - 2, - 5, - 2, - 1, - 2, - 3, - Some(( - 14, - 11, - 6, - 3, - &[21, 23, 25, 27, 29, 31, 33, 36, 40, 44, 48, 53], - &[21, 23, 27, 31, 36, 44, 53], - &[21, 27, 36, 53], - )), - ), - ( - 64000, - 5, - 1, - 3, - 1, - 0, - 3, - Some((6, 3, 2, 1, &[16, 18, 20, 22], &[16, 18, 22], &[16, 22])), - ), - ( - 24000, - 2, - 0, - 0, - 1, - 3, - 5, - Some(( - 10, - 5, - 3, - 2, - &[22, 24, 26, 28, 30, 32], - &[22, 24, 28, 32], - &[22, 24, 32], - )), - ), - ( - 64000, - 8, - 8, - 2, - 0, - 2, - 3, - Some(( - 14, - 11, - 6, - 2, - &[18, 19, 20, 22, 24, 26, 28, 30, 32, 35, 38, 41], - &[18, 19, 22, 26, 30, 35, 41], - &[18, 26, 41], - )), - ), - ( - 16000, - 12, - 12, - 0, - 0, - 2, - 7, - Some(( - 34, - 27, - 14, - 2, - &[ - 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, - 55, 56, 57, 58, 59, 60, 61, 62, - ], - &[35, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62], - &[35, 48, 62], - )), - ), - (88200, 14, 13, 2, 1, 1, 2, None), - ( - 32000, - 13, - 11, - 1, - 1, - 0, - 7, - Some(( - 14, - 7, - 4, - 1, - &[39, 41, 43, 46, 49, 52, 55, 58], - &[39, 41, 46, 52, 58], - &[39, 58], - )), - ), - (44100, 15, 4, 2, 0, 2, 3, None), - ( - 22050, - 12, - 6, - 2, - 0, - 1, - 4, - Some(( - 10, - 6, - 3, - 1, - &[31, 33, 35, 37, 40, 43, 46], - &[31, 35, 40, 46], - &[31, 46], - )), - ), - ( - 48000, - 4, - 6, - 3, - 0, - 3, - 7, - Some(( - 12, - 5, - 3, - 2, - &[22, 24, 26, 29, 32, 35], - &[22, 24, 29, 35], - &[22, 24, 35], - )), - ), - (64000, 3, 14, 1, 0, 0, 3, None), - ( - 24000, - 6, - 0, - 0, - 0, - 1, - 0, - Some(( - 14, - 14, - 7, - 1, - &[18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32], - &[18, 20, 22, 24, 26, 28, 30, 32], - &[18, 32], - )), - ), - (64000, 11, 15, 1, 1, 0, 3, None), - ( - 32000, - 3, - 4, - 0, - 0, - 2, - 4, - Some(( - 24, - 20, - 10, - 2, - &[ - 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, - 40, - ], - &[19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 40], - &[19, 29, 40], - )), - ), - (48000, 9, 13, 3, 1, 2, 1, None), - ( - 24000, - 13, - 5, - 1, - 0, - 3, - 5, - Some((8, 3, 2, 1, &[36, 38, 40, 42], &[36, 38, 42], &[36, 42])), - ), - ( - 16000, - 6, - 3, - 3, - 1, - 2, - 6, - Some((10, 4, 2, 1, &[37, 40, 44, 47, 51], &[37, 44, 51], &[37, 51])), - ), - ( - 24000, - 3, - 11, - 3, - 0, - 1, - 2, - Some(( - 16, - 14, - 7, - 2, - &[17, 19, 21, 23, 25, 27, 30, 33, 36, 39, 42, 46, 50, 54, 58], - &[17, 21, 25, 30, 36, 42, 50, 58], - &[17, 30, 58], - )), - ), - ( - 24000, - 14, - 9, - 0, - 1, - 3, - 6, - Some(( - 12, - 6, - 3, - 1, - &[40, 42, 44, 46, 48, 50, 52], - &[40, 44, 48, 52], - &[40, 52], - )), - ), - ( - 32000, - 3, - 14, - 3, - 0, - 0, - 1, - Some(( - 8, - 7, - 4, - 1, - &[16, 17, 19, 21, 23, 25, 27, 30], - &[16, 17, 21, 25, 30], - &[16, 30], - )), - ), - (64000, 13, 1, 1, 1, 1, 6, None), - (64000, 0, 11, 1, 0, 3, 5, None), - (32000, 6, 12, 2, 0, 1, 2, None), - ( - 22050, - 4, - 5, - 1, - 0, - 2, - 2, - Some(( - 18, - 16, - 8, - 3, - &[ - 18, 19, 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, - ], - &[18, 20, 22, 24, 28, 32, 36, 40, 44], - &[18, 22, 32, 44], - )), - ), - ( - 24000, - 8, - 8, - 1, - 1, - 0, - 4, - Some(( - 14, - 10, - 5, - 1, - &[24, 26, 28, 30, 32, 34, 36, 38, 40, 44, 49], - &[24, 28, 32, 36, 40, 49], - &[24, 49], - )), - ), - ( - 24000, - 9, - 9, - 1, - 0, - 2, - 6, - Some(( - 16, - 10, - 5, - 2, - &[29, 31, 33, 35, 37, 39, 42, 44, 46, 49, 52], - &[29, 33, 37, 42, 46, 52], - &[29, 37, 52], - )), - ), - ( - 64000, - 9, - 4, - 3, - 1, - 2, - 1, - Some(( - 6, - 5, - 3, - 1, - &[17, 19, 21, 23, 25, 28], - &[17, 19, 23, 28], - &[17, 28], - )), - ), - ( - 22050, - 9, - 9, - 3, - 1, - 0, - 0, - Some(( - 10, - 10, - 5, - 1, - &[21, 23, 25, 27, 29, 32, 35, 38, 42, 47, 52], - &[21, 25, 29, 35, 42, 52], - &[21, 52], - )), - ), - ( - 32000, - 9, - 5, - 0, - 0, - 0, - 5, - Some(( - 20, - 15, - 8, - 1, - &[ - 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, - ], - &[26, 27, 29, 31, 33, 35, 37, 39, 42], - &[26, 42], - )), - ), - (64000, 14, 5, 3, 0, 0, 4, None), - ( - 44100, - 11, - 4, - 0, - 0, - 3, - 3, - Some(( - 10, - 7, - 4, - 1, - &[24, 25, 26, 27, 28, 29, 30, 31], - &[24, 25, 27, 29, 31], - &[24, 31], - )), - ), - ( - 64000, - 8, - 15, - 0, - 1, - 0, - 1, - Some(( - 16, - 15, - 8, - 1, - &[ - 16, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, - ], - &[16, 17, 21, 25, 29, 33, 37, 41, 45], - &[16, 45], - )), - ), - ( - 64000, - 15, - 12, - 0, - 1, - 2, - 7, - Some(( - 14, - 7, - 4, - 1, - &[44, 46, 48, 50, 52, 54, 56, 59], - &[44, 46, 50, 54, 59], - &[44, 59], - )), - ), - (88200, 13, 0, 0, 0, 3, 3, None), - ( - 64000, - 3, - 4, - 3, - 0, - 2, - 3, - Some(( - 12, - 9, - 5, - 2, - &[13, 14, 15, 16, 18, 20, 22, 24, 26, 28], - &[13, 14, 16, 20, 24, 28], - &[13, 16, 28], - )), - ), - ( - 16000, - 3, - 4, - 1, - 1, - 2, - 6, - Some(( - 16, - 10, - 5, - 2, - &[26, 28, 30, 32, 34, 36, 38, 41, 44, 48, 52], - &[26, 30, 34, 38, 44, 52], - &[26, 34, 52], - )), - ), - (32000, 0, 8, 2, 1, 1, 6, None), - (96000, 1, 9, 3, 0, 2, 4, None), - (48000, 7, 12, 3, 1, 0, 7, None), - ( - 24000, - 9, - 13, - 1, - 1, - 1, - 6, - Some(( - 18, - 12, - 6, - 1, - &[29, 31, 33, 35, 37, 39, 42, 45, 48, 52, 56, 60, 64], - &[29, 33, 37, 42, 48, 56, 64], - &[29, 64], - )), - ), - ( - 16000, - 10, - 9, - 3, - 1, - 2, - 0, - Some(( - 10, - 10, - 5, - 2, - &[26, 28, 30, 32, 35, 38, 41, 45, 49, 53, 57], - &[26, 30, 35, 41, 49, 57], - &[26, 35, 57], - )), - ), - (64000, 14, 12, 3, 1, 0, 7, None), - (32000, 10, 15, 3, 1, 2, 2, None), - (64000, 9, 0, 3, 1, 1, 6, None), - (96000, 4, 9, 2, 1, 1, 3, None), - ( - 24000, - 9, - 12, - 3, - 1, - 3, - 0, - Some(( - 12, - 12, - 6, - 5, - &[21, 23, 25, 27, 29, 32, 35, 38, 42, 46, 51, 56, 61], - &[21, 25, 29, 35, 42, 51, 61], - &[21, 25, 29, 35, 42, 61], - )), - ), - ( - 22050, - 3, - 1, - 1, - 0, - 3, - 0, - Some(( - 16, - 16, - 8, - 4, - &[ - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 28, 30, 31, 32, 34, 36, - ], - &[15, 17, 19, 21, 23, 26, 30, 32, 36], - &[15, 19, 23, 30, 36], - )), - ), - (32000, 8, 12, 0, 1, 2, 2, None), - (64000, 2, 0, 1, 0, 0, 1, None), - ( - 22050, - 4, - 4, - 1, - 0, - 2, - 6, - Some(( - 16, - 10, - 5, - 2, - &[22, 23, 24, 26, 28, 30, 32, 34, 36, 39, 42], - &[22, 24, 28, 32, 36, 42], - &[22, 28, 42], - )), - ), - (48000, 2, 2, 1, 0, 2, 1, None), - ( - 16000, - 13, - 11, - 1, - 1, - 3, - 3, - Some(( - 12, - 9, - 5, - 2, - &[35, 37, 39, 41, 44, 47, 50, 53, 56, 60], - &[35, 37, 41, 47, 53, 60], - &[35, 41, 60], - )), - ), - ( - 16000, - 4, - 8, - 2, - 1, - 0, - 7, - Some(( - 14, - 7, - 4, - 1, - &[32, 34, 37, 40, 43, 47, 51, 56], - &[32, 34, 40, 47, 56], - &[32, 56], - )), - ), - ( - 16000, - 0, - 12, - 0, - 0, - 2, - 5, - Some(( - 46, - 41, - 21, - 3, - &[ - 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, - 61, 62, - ], - &[ - 21, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, - 60, 62, - ], - &[21, 34, 48, 62], - )), - ), - ( - 24000, - 0, - 0, - 3, - 1, - 1, - 3, - Some(( - 12, - 9, - 5, - 1, - &[14, 15, 16, 18, 20, 22, 24, 26, 29, 32], - &[14, 15, 18, 22, 26, 32], - &[14, 32], - )), - ), - ( - 16000, - 15, - 1, - 2, - 0, - 2, - 1, - Some(( - 6, - 5, - 3, - 1, - &[33, 36, 39, 42, 45, 49], - &[33, 36, 42, 49], - &[33, 49], - )), - ), - (88200, 2, 12, 3, 1, 2, 4, None), - (96000, 3, 9, 2, 0, 1, 5, None), - ( - 22050, - 14, - 4, - 0, - 1, - 3, - 6, - Some((8, 2, 1, 1, &[38, 40, 42], &[38, 42], &[38, 42])), - ), - ( - 44100, - 14, - 10, - 3, - 0, - 0, - 0, - Some(( - 6, - 6, - 3, - 1, - &[28, 31, 34, 38, 42, 46, 51], - &[28, 34, 42, 51], - &[28, 51], - )), - ), - ( - 48000, - 4, - 2, - 3, - 1, - 0, - 7, - Some((8, 1, 1, 1, &[23, 25], &[23, 25], &[23, 25])), - ), - (88200, 1, 0, 2, 0, 0, 4, None), - (24000, 14, 7, 3, 0, 3, 7, None), - ( - 16000, - 4, - 9, - 2, - 0, - 3, - 6, - Some(( - 16, - 10, - 5, - 3, - &[30, 32, 34, 37, 40, 42, 45, 48, 51, 54, 57], - &[30, 34, 40, 45, 51, 57], - &[30, 34, 45, 57], - )), - ), - ( - 16000, - 1, - 0, - 3, - 0, - 3, - 4, - Some(( - 12, - 8, - 4, - 3, - &[24, 26, 28, 31, 34, 37, 40, 44, 48], - &[24, 28, 34, 40, 48], - &[24, 28, 34, 48], - )), - ), - ( - 64000, - 4, - 7, - 0, - 1, - 1, - 5, - Some(( - 14, - 9, - 5, - 1, - &[19, 21, 23, 25, 27, 29, 31, 33, 35, 37], - &[19, 21, 25, 29, 33, 37], - &[19, 37], - )), - ), - ( - 24000, - 14, - 7, - 0, - 1, - 0, - 5, - Some((8, 3, 2, 1, &[39, 41, 43, 46], &[39, 41, 46], &[39, 46])), - ), - (88200, 12, 12, 0, 0, 0, 4, None), - ( - 48000, - 14, - 14, - 3, - 1, - 3, - 3, - Some(( - 8, - 5, - 3, - 2, - &[35, 38, 41, 45, 49, 54], - &[35, 38, 45, 54], - &[35, 38, 54], - )), - ), - ( - 44100, - 14, - 4, - 3, - 0, - 2, - 0, - Some((2, 2, 1, 1, &[28, 29, 31], &[28, 31], &[28, 31])), - ), - ( - 16000, - 10, - 10, - 0, - 1, - 1, - 0, - Some(( - 16, - 16, - 8, - 1, - &[ - 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, - ], - &[26, 30, 34, 38, 42, 46, 50, 54, 58], - &[26, 58], - )), - ), - ( - 32000, - 3, - 15, - 0, - 1, - 1, - 1, - Some(( - 16, - 15, - 8, - 1, - &[ - 16, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, - ], - &[16, 17, 21, 25, 29, 33, 37, 41, 45], - &[16, 45], - )), - ), - ( - 48000, - 6, - 5, - 1, - 0, - 0, - 3, - Some(( - 14, - 11, - 6, - 1, - &[17, 18, 19, 20, 21, 22, 23, 24, 26, 28, 30, 32], - &[17, 18, 20, 22, 24, 28, 32], - &[17, 32], - )), - ), - ( - 22050, - 3, - 7, - 0, - 0, - 0, - 7, - Some(( - 32, - 25, - 13, - 1, - &[ - 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, - 42, 43, 44, 45, 46, 48, - ], - &[22, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 48], - &[22, 48], - )), - ), - ( - 32000, - 10, - 5, - 2, - 0, - 2, - 6, - Some((10, 4, 2, 1, &[32, 34, 36, 39, 42], &[32, 36, 42], &[32, 42])), - ), - ( - 88200, - 4, - 8, - 0, - 0, - 3, - 4, - Some(( - 28, - 24, - 12, - 5, - &[ - 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, - 33, 34, 35, 36, 37, - ], - &[13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37], - &[13, 17, 21, 25, 31, 37], - )), - ), - (48000, 0, 1, 2, 1, 0, 6, None), - ( - 16000, - 4, - 15, - 1, - 1, - 3, - 0, - Some(( - 18, - 18, - 9, - 5, - &[ - 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 49, 52, 56, 60, - ], - &[20, 22, 24, 28, 32, 36, 40, 46, 52, 60], - &[20, 22, 28, 36, 46, 60], - )), - ), - ( - 88200, - 3, - 5, - 0, - 0, - 1, - 1, - Some(( - 18, - 17, - 9, - 2, - &[ - 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, - ], - &[9, 10, 12, 14, 16, 18, 20, 22, 24, 26], - &[9, 16, 26], - )), - ), - ( - 96000, - 15, - 11, - 1, - 1, - 1, - 7, - Some((8, 1, 1, 1, &[47, 50], &[47, 50], &[47, 50])), - ), - ( - 24000, - 9, - 2, - 0, - 0, - 3, - 0, - Some(( - 14, - 14, - 7, - 2, - &[21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 36], - &[21, 23, 25, 27, 29, 31, 33, 36], - &[21, 27, 36], - )), - ), - (64000, 11, 2, 3, 1, 1, 7, None), - (22050, 1, 13, 0, 1, 3, 5, None), - ( - 16000, - 10, - 15, - 2, - 0, - 1, - 5, - Some(( - 12, - 7, - 4, - 1, - &[36, 39, 42, 45, 48, 52, 58, 64], - &[36, 39, 45, 52, 64], - &[36, 64], - )), - ), - ( - 32000, - 13, - 14, - 3, - 0, - 3, - 3, - Some(( - 8, - 5, - 3, - 2, - &[35, 38, 41, 45, 49, 54], - &[35, 38, 45, 54], - &[35, 38, 54], - )), - ), - ( - 96000, - 5, - 14, - 3, - 0, - 1, - 4, - Some((8, 4, 2, 1, &[14, 15, 16, 18, 20], &[14, 16, 20], &[14, 20])), - ), - ( - 64000, - 12, - 7, - 0, - 0, - 2, - 5, - Some(( - 16, - 11, - 6, - 1, - &[26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37], - &[26, 27, 29, 31, 33, 35, 37], - &[26, 37], - )), - ), - ( - 32000, - 8, - 2, - 1, - 1, - 0, - 6, - Some((10, 4, 2, 1, &[28, 30, 32, 34, 36], &[28, 32, 36], &[28, 36])), - ), - ( - 16000, - 9, - 12, - 2, - 1, - 0, - 4, - Some(( - 12, - 8, - 4, - 1, - &[33, 35, 38, 41, 44, 47, 50, 56, 62], - &[33, 38, 44, 50, 62], - &[33, 62], - )), - ), - ( - 64000, - 15, - 8, - 1, - 1, - 0, - 0, - Some(( - 6, - 6, - 3, - 1, - &[30, 31, 33, 35, 37, 39, 41], - &[30, 33, 37, 41], - &[30, 41], - )), - ), - ( - 16000, - 7, - 13, - 1, - 1, - 2, - 4, - Some(( - 16, - 12, - 6, - 2, - &[28, 30, 32, 34, 36, 38, 40, 43, 46, 50, 54, 59, 64], - &[28, 32, 36, 40, 46, 54, 64], - &[28, 40, 64], - )), - ), - (32000, 8, 13, 2, 0, 0, 7, None), - ( - 48000, - 8, - 2, - 3, - 0, - 1, - 0, - Some(( - 6, - 6, - 3, - 1, - &[16, 17, 18, 19, 21, 23, 25], - &[16, 18, 21, 25], - &[16, 25], - )), - ), - (32000, 4, 13, 3, 1, 0, 5, None), - (32000, 2, 9, 0, 0, 0, 1, None), - (88200, 14, 1, 3, 1, 2, 0, None), - (48000, 6, 3, 1, 0, 2, 5, None), - ( - 16000, - 15, - 10, - 3, - 0, - 2, - 0, - Some(( - 8, - 8, - 4, - 2, - &[31, 33, 36, 39, 42, 46, 50, 54, 58], - &[31, 36, 42, 50, 58], - &[31, 42, 58], - )), - ), - (44100, 8, 11, 0, 0, 0, 6, None), - ( - 32000, - 9, - 2, - 2, - 0, - 2, - 3, - Some(( - 8, - 5, - 3, - 1, - &[25, 27, 29, 31, 33, 36], - &[25, 27, 31, 36], - &[25, 36], - )), - ), - ( - 22050, - 12, - 7, - 0, - 0, - 1, - 3, - Some(( - 24, - 21, - 11, - 1, - &[ - 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, - 47, 48, - ], - &[27, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48], - &[27, 48], - )), - ), - ( - 44100, - 7, - 14, - 3, - 0, - 2, - 6, - Some((8, 2, 1, 1, &[27, 29, 32], &[27, 32], &[27, 32])), - ), - (32000, 10, 11, 1, 0, 1, 3, None), - ( - 88200, - 10, - 15, - 3, - 0, - 3, - 6, - Some(( - 12, - 6, - 3, - 2, - &[27, 29, 32, 35, 39, 43, 48], - &[27, 32, 39, 48], - &[27, 32, 48], - )), - ), - (48000, 2, 10, 3, 1, 1, 6, None), - (64000, 3, 11, 3, 1, 0, 4, None), - ( - 48000, - 14, - 6, - 1, - 0, - 1, - 1, - Some((4, 3, 2, 1, &[29, 31, 33, 35], &[29, 31, 35], &[29, 35])), - ), - ( - 16000, - 6, - 15, - 1, - 1, - 3, - 2, - Some(( - 16, - 14, - 7, - 4, - &[24, 25, 27, 29, 31, 33, 35, 37, 39, 41, 44, 48, 53, 58, 64], - &[24, 27, 31, 35, 39, 44, 53, 64], - &[24, 27, 35, 44, 64], - )), - ), - (96000, 5, 3, 1, 0, 0, 2, None), - ( - 24000, - 3, - 0, - 0, - 1, - 3, - 2, - Some(( - 8, - 6, - 3, - 2, - &[19, 21, 23, 25, 27, 29, 32], - &[19, 23, 27, 32], - &[19, 23, 32], - )), - ), - (64000, 15, 7, 1, 0, 0, 4, None), - ( - 22050, - 12, - 5, - 2, - 1, - 3, - 2, - Some(( - 8, - 6, - 3, - 2, - &[28, 30, 32, 35, 38, 41, 44], - &[28, 32, 38, 44], - &[28, 32, 44], - )), - ), - (64000, 5, 10, 2, 0, 1, 3, None), - ( - 96000, - 1, - 3, - 0, - 0, - 2, - 2, - Some(( - 12, - 10, - 5, - 2, - &[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19], - &[8, 10, 12, 14, 16, 19], - &[8, 12, 19], - )), - ), - ( - 16000, - 3, - 5, - 3, - 1, - 1, - 7, - Some((10, 3, 2, 1, &[35, 38, 45, 53], &[35, 38, 53], &[35, 53])), - ), - ( - 64000, - 2, - 7, - 0, - 0, - 0, - 4, - Some(( - 28, - 24, - 12, - 1, - &[ - 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, - 33, 34, 35, 36, 37, - ], - &[13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37], - &[13, 37], - )), - ), - ( - 44100, - 15, - 8, - 2, - 1, - 1, - 0, - Some((4, 4, 2, 1, &[32, 34, 37, 40, 43], &[32, 37, 43], &[32, 43])), - ), - ( - 16000, - 11, - 10, - 3, - 1, - 3, - 7, - Some((8, 1, 1, 1, &[53, 58], &[53, 58], &[53, 58])), - ), - ( - 16000, - 4, - 6, - 1, - 1, - 3, - 5, - Some(( - 16, - 11, - 6, - 3, - &[26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 50, 54], - &[26, 28, 32, 36, 40, 46, 54], - &[26, 32, 40, 54], - )), - ), - ( - 16000, - 3, - 5, - 1, - 0, - 1, - 6, - Some(( - 18, - 12, - 6, - 1, - &[26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 47, 50, 53], - &[26, 30, 34, 38, 42, 47, 53], - &[26, 53], - )), - ), - (48000, 5, 13, 0, 0, 0, 4, None), - ( - 22050, - 13, - 11, - 0, - 1, - 3, - 2, - Some(( - 16, - 14, - 7, - 3, - &[30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58], - &[30, 34, 38, 42, 46, 50, 54, 58], - &[30, 38, 46, 58], - )), - ), - ( - 96000, - 1, - 7, - 0, - 1, - 1, - 3, - Some(( - 12, - 9, - 5, - 1, - &[12, 14, 16, 18, 20, 22, 24, 26, 28, 31], - &[12, 14, 18, 22, 26, 31], - &[12, 31], - )), - ), - ( - 22050, - 10, - 13, - 3, - 1, - 3, - 0, - Some(( - 12, - 12, - 6, - 5, - &[22, 24, 26, 28, 31, 34, 37, 40, 44, 48, 53, 58, 64], - &[22, 26, 31, 37, 44, 53, 64], - &[22, 26, 31, 37, 44, 64], - )), - ), - (64000, 0, 14, 3, 0, 0, 3, None), - ( - 16000, - 2, - 14, - 2, - 0, - 1, - 5, - Some(( - 10, - 5, - 3, - 1, - &[25, 27, 29, 31, 33, 36], - &[25, 27, 31, 36], - &[25, 36], - )), - ), - ( - 22050, - 1, - 0, - 3, - 1, - 0, - 4, - Some(( - 10, - 6, - 3, - 1, - &[18, 20, 22, 24, 26, 30, 35], - &[18, 22, 26, 35], - &[18, 35], - )), - ), - ( - 22050, - 6, - 6, - 0, - 1, - 2, - 7, - Some(( - 14, - 7, - 4, - 1, - &[32, 34, 36, 38, 40, 42, 44, 46], - &[32, 34, 38, 42, 46], - &[32, 46], - )), - ), - ( - 16000, - 12, - 4, - 2, - 0, - 3, - 2, - Some(( - 8, - 6, - 3, - 2, - &[32, 35, 38, 41, 44, 48, 52], - &[32, 38, 44, 52], - &[32, 38, 52], - )), - ), - (64000, 2, 2, 2, 0, 2, 6, None), - (96000, 1, 3, 2, 0, 1, 6, None), - ( - 22050, - 2, - 12, - 3, - 0, - 2, - 4, - Some(( - 16, - 12, - 6, - 3, - &[20, 22, 24, 26, 28, 31, 34, 37, 41, 45, 50, 55, 61], - &[20, 24, 28, 34, 41, 50, 61], - &[20, 28, 41, 61], - )), - ), - (88200, 0, 0, 1, 1, 3, 0, None), - (32000, 9, 2, 3, 1, 3, 7, None), - (96000, 15, 0, 3, 0, 3, 5, None), - ( - 24000, - 15, - 10, - 1, - 0, - 3, - 0, - Some(( - 10, - 10, - 5, - 2, - &[32, 34, 36, 38, 40, 42, 44, 46, 49, 52, 55], - &[32, 36, 40, 44, 49, 55], - &[32, 40, 55], - )), - ), - ( - 88200, - 14, - 11, - 0, - 1, - 3, - 5, - Some(( - 12, - 7, - 4, - 1, - &[37, 39, 41, 43, 45, 47, 49, 51], - &[37, 39, 43, 47, 51], - &[37, 51], - )), - ), - ( - 44100, - 4, - 5, - 3, - 1, - 3, - 4, - Some(( - 10, - 6, - 3, - 3, - &[18, 20, 22, 24, 26, 30, 34], - &[18, 22, 26, 34], - &[18, 22, 26, 34], - )), - ), - (88200, 1, 10, 0, 0, 3, 2, None), - (48000, 9, 12, 1, 1, 0, 0, None), - ( - 22050, - 9, - 0, - 2, - 1, - 1, - 7, - Some((8, 1, 1, 1, &[33, 35], &[33, 35], &[33, 35])), - ), - ( - 88200, - 11, - 3, - 2, - 1, - 3, - 1, - Some((2, 1, 1, 1, &[19, 21], &[19, 21], &[19, 21])), - ), - ( - 88200, - 15, - 12, - 0, - 0, - 2, - 5, - Some(( - 26, - 21, - 11, - 1, - &[ - 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, - 56, 57, - ], - &[36, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57], - &[36, 57], - )), - ), - ( - 44100, - 11, - 8, - 2, - 0, - 1, - 1, - Some(( - 10, - 9, - 5, - 1, - &[22, 24, 26, 28, 30, 32, 34, 37, 40, 43], - &[22, 24, 28, 32, 37, 43], - &[22, 43], - )), - ), - ( - 22050, - 3, - 4, - 2, - 1, - 1, - 3, - Some(( - 14, - 11, - 6, - 1, - &[18, 19, 20, 22, 24, 26, 28, 30, 32, 35, 38, 42], - &[18, 19, 22, 26, 30, 35, 42], - &[18, 42], - )), - ), - ( - 16000, - 1, - 3, - 1, - 0, - 0, - 5, - Some(( - 20, - 15, - 8, - 1, - &[ - 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 51, - ], - &[22, 23, 26, 30, 34, 38, 42, 46, 51], - &[22, 51], - )), - ), - (96000, 10, 11, 1, 0, 0, 0, None), - (88200, 5, 12, 2, 0, 0, 6, None), - ( - 24000, - 12, - 7, - 0, - 0, - 0, - 2, - Some(( - 20, - 18, - 9, - 1, - &[ - 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, - ], - &[27, 29, 31, 33, 35, 37, 39, 41, 43, 46], - &[27, 46], - )), - ), - (22050, 1, 5, 1, 0, 2, 6, None), - ( - 44100, - 6, - 4, - 3, - 1, - 0, - 4, - Some((8, 4, 2, 1, &[22, 24, 26, 28, 31], &[22, 26, 31], &[22, 31])), - ), - ( - 96000, - 9, - 15, - 0, - 1, - 2, - 6, - Some(( - 14, - 8, - 4, - 1, - &[26, 28, 30, 32, 34, 36, 38, 40, 42], - &[26, 30, 34, 38, 42], - &[26, 42], - )), - ), - ( - 16000, - 7, - 8, - 1, - 0, - 3, - 2, - Some(( - 16, - 14, - 7, - 3, - &[25, 26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 48, 50, 53, 56], - &[25, 28, 32, 36, 40, 46, 50, 56], - &[25, 32, 40, 56], - )), - ), - ( - 24000, - 13, - 12, - 2, - 0, - 1, - 1, - Some(( - 12, - 11, - 6, - 1, - &[29, 31, 33, 35, 38, 41, 44, 47, 50, 54, 57, 61], - &[29, 31, 35, 41, 47, 54, 61], - &[29, 61], - )), - ), - ( - 24000, - 3, - 8, - 1, - 0, - 0, - 4, - Some(( - 20, - 16, - 8, - 1, - &[ - 19, 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 49, - ], - &[19, 21, 23, 26, 30, 34, 38, 43, 49], - &[19, 49], - )), - ), - (44100, 6, 11, 0, 0, 1, 3, None), - (44100, 12, 12, 3, 1, 3, 5, None), - ( - 24000, - 13, - 11, - 3, - 0, - 1, - 7, - Some((8, 1, 1, 1, &[53, 58], &[53, 58], &[53, 58])), - ), - ( - 48000, - 9, - 1, - 1, - 0, - 2, - 2, - Some((6, 4, 2, 1, &[19, 20, 21, 22, 23], &[19, 21, 23], &[19, 23])), - ), - ( - 24000, - 13, - 6, - 3, - 1, - 3, - 4, - Some((6, 2, 1, 1, &[37, 40, 44], &[37, 44], &[37, 44])), - ), - (48000, 3, 13, 1, 1, 1, 0, None), - (32000, 1, 7, 1, 0, 2, 5, None), - ( - 24000, - 8, - 13, - 2, - 1, - 1, - 0, - Some(( - 16, - 16, - 8, - 2, - &[ - 20, 21, 22, 24, 26, 28, 30, 32, 34, 37, 40, 43, 47, 51, 55, 59, 64, - ], - &[20, 22, 26, 30, 34, 40, 47, 55, 64], - &[20, 34, 64], - )), - ), - ( - 32000, - 8, - 4, - 1, - 0, - 0, - 0, - Some(( - 12, - 12, - 6, - 1, - &[20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40], - &[20, 22, 24, 28, 32, 36, 40], - &[20, 40], - )), - ), - ( - 22050, - 6, - 0, - 3, - 0, - 2, - 0, - Some(( - 8, - 8, - 4, - 2, - &[18, 19, 21, 23, 25, 27, 29, 32, 35], - &[18, 21, 25, 29, 35], - &[18, 25, 35], - )), - ), - ( - 16000, - 14, - 15, - 0, - 1, - 0, - 3, - Some(( - 18, - 15, - 8, - 1, - &[ - 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, - ], - &[34, 36, 40, 44, 48, 52, 56, 60, 64], - &[34, 64], - )), - ), - ( - 32000, - 11, - 11, - 2, - 0, - 3, - 6, - Some(( - 14, - 8, - 4, - 2, - &[34, 37, 40, 43, 46, 49, 52, 55, 58], - &[34, 40, 46, 52, 58], - &[34, 46, 58], - )), - ), - ( - 32000, - 13, - 11, - 3, - 0, - 2, - 5, - Some((8, 3, 2, 1, &[44, 48, 53, 58], &[44, 48, 58], &[44, 58])), - ), - (88200, 13, 6, 0, 0, 1, 6, None), - ( - 16000, - 2, - 5, - 2, - 0, - 3, - 0, - Some(( - 16, - 16, - 8, - 5, - &[ - 18, 19, 20, 21, 23, 25, 27, 29, 31, 33, 36, 38, 41, 44, 47, 50, 53, - ], - &[18, 20, 23, 27, 31, 36, 41, 47, 53], - &[18, 20, 23, 31, 41, 53], - )), - ), - (88200, 7, 11, 2, 0, 0, 4, None), - ( - 22050, - 8, - 13, - 3, - 1, - 1, - 6, - Some(( - 12, - 6, - 3, - 1, - &[34, 37, 40, 45, 51, 57, 64], - &[34, 40, 51, 64], - &[34, 64], - )), - ), - ( - 22050, - 5, - 10, - 1, - 0, - 0, - 5, - Some(( - 20, - 15, - 8, - 1, - &[ - 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 43, 46, 49, 52, 55, - ], - &[22, 23, 26, 30, 34, 38, 43, 49, 55], - &[22, 55], - )), - ), - ( - 22050, - 0, - 6, - 3, - 0, - 1, - 1, - Some(( - 16, - 15, - 8, - 2, - &[ - 13, 14, 15, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36, 39, 42, 46, - ], - &[13, 14, 16, 20, 24, 28, 33, 39, 46], - &[13, 24, 46], - )), - ), - ( - 16000, - 13, - 15, - 2, - 1, - 0, - 5, - Some(( - 12, - 7, - 4, - 1, - &[40, 43, 46, 49, 52, 56, 60, 64], - &[40, 43, 49, 56, 64], - &[40, 64], - )), - ), - ( - 96000, - 1, - 14, - 0, - 0, - 0, - 4, - Some((6, 2, 1, 1, &[10, 11, 12], &[10, 12], &[10, 12])), - ), - (48000, 15, 0, 0, 0, 1, 5, None), - (88200, 1, 8, 1, 1, 0, 7, None), - ( - 16000, - 2, - 14, - 1, - 1, - 2, - 6, - Some(( - 12, - 6, - 3, - 1, - &[24, 26, 28, 30, 32, 34, 36], - &[24, 28, 32, 36], - &[24, 36], - )), - ), - (48000, 1, 4, 2, 1, 1, 6, None), - (64000, 8, 0, 2, 0, 3, 6, None), - ( - 22050, - 7, - 6, - 2, - 1, - 0, - 0, - Some(( - 12, - 12, - 6, - 1, - &[19, 20, 21, 23, 25, 27, 29, 31, 33, 35, 38, 42, 46], - &[19, 21, 25, 29, 33, 38, 46], - &[19, 46], - )), - ), - ( - 22050, - 11, - 15, - 3, - 1, - 3, - 2, - Some(( - 10, - 8, - 4, - 4, - &[27, 29, 32, 35, 38, 42, 46, 54, 64], - &[27, 32, 38, 46, 64], - &[27, 32, 38, 46, 64], - )), - ), - ( - 32000, - 12, - 2, - 2, - 0, - 3, - 5, - Some((6, 1, 1, 1, &[34, 36], &[34, 36], &[34, 36])), - ), - ( - 44100, - 11, - 6, - 1, - 1, - 3, - 3, - Some(( - 10, - 7, - 4, - 2, - &[24, 25, 27, 29, 31, 33, 35, 37], - &[24, 25, 29, 33, 37], - &[24, 29, 37], - )), - ), - ( - 22050, - 6, - 8, - 2, - 0, - 0, - 6, - Some(( - 14, - 8, - 4, - 1, - &[27, 29, 31, 33, 36, 39, 42, 46, 50], - &[27, 31, 36, 42, 50], - &[27, 50], - )), - ), - (64000, 0, 10, 2, 0, 0, 3, None), - ( - 16000, - 0, - 12, - 1, - 0, - 0, - 7, - Some(( - 24, - 17, - 9, - 1, - &[ - 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 47, 50, 53, 56, 59, 62, - ], - &[23, 24, 28, 32, 36, 40, 44, 50, 56, 62], - &[23, 62], - )), - ), - ( - 16000, - 2, - 14, - 1, - 0, - 3, - 6, - Some(( - 12, - 6, - 3, - 2, - &[24, 26, 28, 30, 32, 34, 36], - &[24, 28, 32, 36], - &[24, 28, 36], - )), - ), - (96000, 0, 2, 3, 1, 1, 6, None), - (88200, 2, 4, 1, 1, 3, 3, None), - ( - 16000, - 13, - 4, - 2, - 1, - 1, - 4, - Some((8, 4, 2, 1, &[39, 42, 45, 48, 52], &[39, 45, 52], &[39, 52])), - ), - (22050, 0, 11, 2, 1, 3, 4, None), - (64000, 12, 11, 1, 1, 0, 7, None), - (24000, 1, 6, 1, 0, 0, 6, None), - ( - 96000, - 5, - 7, - 0, - 1, - 3, - 5, - Some(( - 10, - 5, - 3, - 2, - &[20, 22, 24, 26, 28, 31], - &[20, 22, 26, 31], - &[20, 22, 31], - )), - ), - ( - 88200, - 12, - 11, - 1, - 0, - 2, - 6, - Some(( - 16, - 10, - 5, - 2, - &[28, 30, 32, 34, 36, 38, 40, 42, 45, 48, 51], - &[28, 32, 36, 40, 45, 51], - &[28, 36, 51], - )), - ), - (48000, 7, 13, 3, 0, 2, 6, None), - ( - 16000, - 14, - 15, - 1, - 0, - 2, - 0, - Some(( - 14, - 14, - 7, - 2, - &[30, 31, 33, 35, 37, 39, 41, 43, 45, 48, 51, 54, 57, 60, 64], - &[30, 33, 37, 41, 45, 51, 57, 64], - &[30, 41, 64], - )), - ), - ( - 48000, - 9, - 7, - 2, - 0, - 2, - 3, - Some(( - 12, - 9, - 5, - 2, - &[20, 21, 23, 25, 27, 29, 31, 33, 35, 38], - &[20, 21, 25, 29, 33, 38], - &[20, 25, 38], - )), - ), - ( - 44100, - 6, - 4, - 2, - 0, - 3, - 4, - Some(( - 10, - 6, - 3, - 2, - &[19, 21, 23, 25, 27, 29, 31], - &[19, 23, 27, 31], - &[19, 23, 31], - )), - ), - ( - 64000, - 5, - 1, - 0, - 0, - 1, - 6, - Some((10, 4, 2, 1, &[18, 19, 20, 21, 22], &[18, 20, 22], &[18, 22])), - ), - ( - 44100, - 15, - 7, - 0, - 1, - 3, - 0, - Some((4, 4, 2, 1, &[32, 34, 36, 38, 40], &[32, 36, 40], &[32, 40])), - ), - (32000, 12, 3, 3, 1, 0, 5, None), - ( - 24000, - 9, - 9, - 1, - 1, - 2, - 5, - Some(( - 14, - 9, - 5, - 2, - &[27, 29, 31, 33, 35, 37, 39, 42, 47, 52], - &[27, 29, 33, 37, 42, 52], - &[27, 33, 52], - )), - ), - ( - 16000, - 12, - 2, - 0, - 1, - 3, - 0, - Some(( - 12, - 12, - 6, - 3, - &[28, 29, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50], - &[28, 30, 34, 38, 42, 46, 50], - &[28, 34, 42, 50], - )), - ), - (44100, 1, 13, 0, 1, 1, 3, None), - (44100, 12, 4, 1, 0, 1, 6, None), - ( - 16000, - 13, - 13, - 0, - 1, - 1, - 3, - Some(( - 18, - 15, - 8, - 1, - &[ - 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, - ], - &[34, 36, 40, 44, 48, 52, 56, 60, 64], - &[34, 64], - )), - ), - (48000, 5, 9, 1, 0, 1, 3, None), ]; diff --git a/crates/ec-aac/src/sbr_bands/vectors/part1.rs b/crates/ec-aac/src/sbr_bands/vectors/part1.rs deleted file mode 100644 index 5b11e89e..00000000 --- a/crates/ec-aac/src/sbr_bands/vectors/part1.rs +++ /dev/null @@ -1,1695 +0,0 @@ -//! Reference vectors for the `freq_tables` tests, second half of the 500 (test-only data). - -use super::Vector; - -pub(super) static PART: &[Vector] = &[ - ( - 16000, - 5, - 5, - 1, - 1, - 2, - 2, - Some(( - 16, - 14, - 7, - 2, - &[23, 24, 25, 27, 29, 31, 33, 35, 37, 39, 42, 44, 47, 50, 53], - &[23, 25, 29, 33, 37, 42, 47, 53], - &[23, 33, 53], - )), - ), - ( - 16000, - 10, - 7, - 2, - 1, - 2, - 4, - Some(( - 10, - 6, - 3, - 1, - &[35, 38, 41, 44, 47, 51, 55], - &[35, 41, 47, 55], - &[35, 55], - )), - ), - (48000, 2, 12, 2, 1, 0, 4, None), - (88200, 0, 6, 1, 0, 1, 0, None), - ( - 22050, - 3, - 4, - 1, - 0, - 1, - 7, - Some(( - 18, - 11, - 6, - 1, - &[22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42], - &[22, 23, 26, 30, 34, 38, 42], - &[22, 42], - )), - ), - ( - 64000, - 3, - 2, - 0, - 0, - 2, - 7, - Some(( - 14, - 7, - 4, - 1, - &[17, 18, 19, 20, 21, 22, 23, 24], - &[17, 18, 20, 22, 24], - &[17, 24], - )), - ), - ( - 16000, - 4, - 2, - 0, - 1, - 1, - 5, - Some(( - 16, - 11, - 6, - 1, - &[28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50], - &[28, 30, 34, 38, 42, 46, 50], - &[28, 50], - )), - ), - ( - 64000, - 6, - 2, - 3, - 0, - 2, - 2, - Some(( - 8, - 6, - 3, - 1, - &[15, 16, 17, 18, 20, 22, 24], - &[15, 17, 20, 24], - &[15, 24], - )), - ), - ( - 32000, - 11, - 4, - 1, - 1, - 2, - 6, - Some((10, 4, 2, 1, &[32, 34, 36, 38, 40], &[32, 36, 40], &[32, 40])), - ), - (88200, 2, 5, 2, 0, 2, 3, None), - (64000, 1, 7, 2, 1, 1, 2, None), - ( - 24000, - 4, - 5, - 3, - 0, - 2, - 2, - Some(( - 12, - 10, - 5, - 2, - &[19, 21, 23, 25, 27, 29, 32, 34, 36, 39, 42], - &[19, 23, 27, 32, 36, 42], - &[19, 27, 42], - )), - ), - ( - 24000, - 10, - 5, - 3, - 0, - 1, - 4, - Some((8, 4, 2, 1, &[30, 33, 36, 39, 42], &[30, 36, 42], &[30, 42])), - ), - ( - 16000, - 6, - 1, - 3, - 0, - 0, - 4, - Some(( - 10, - 6, - 3, - 1, - &[30, 33, 36, 39, 42, 45, 49], - &[30, 36, 42, 49], - &[30, 49], - )), - ), - (88200, 8, 15, 1, 0, 2, 3, None), - ( - 24000, - 2, - 3, - 1, - 0, - 2, - 7, - Some(( - 18, - 11, - 6, - 2, - &[21, 22, 23, 24, 26, 28, 29, 30, 32, 34, 36, 38], - &[21, 22, 24, 28, 30, 34, 38], - &[21, 28, 38], - )), - ), - (48000, 1, 15, 1, 1, 1, 0, None), - (48000, 6, 11, 0, 1, 2, 5, None), - ( - 48000, - 0, - 0, - 0, - 0, - 0, - 5, - Some(( - 14, - 9, - 5, - 1, - &[12, 13, 14, 15, 16, 17, 18, 19, 20, 21], - &[12, 13, 15, 17, 19, 21], - &[12, 21], - )), - ), - (64000, 12, 0, 3, 0, 1, 5, None), - (88200, 4, 12, 0, 0, 0, 5, None), - (22050, 8, 0, 3, 0, 3, 6, None), - (24000, 0, 12, 0, 1, 2, 4, None), - ( - 16000, - 4, - 15, - 0, - 0, - 0, - 5, - Some(( - 40, - 35, - 18, - 1, - &[ - 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, - 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, - ], - &[ - 25, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, - ], - &[25, 60], - )), - ), - ( - 96000, - 14, - 10, - 2, - 1, - 2, - 1, - Some(( - 8, - 7, - 4, - 1, - &[28, 30, 32, 34, 36, 38, 41, 44], - &[28, 30, 34, 38, 44], - &[28, 44], - )), - ), - (44100, 15, 10, 3, 0, 0, 6, None), - ( - 22050, - 4, - 15, - 0, - 0, - 0, - 3, - Some(( - 32, - 29, - 15, - 1, - &[ - 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, - 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, - ], - &[ - 19, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, - ], - &[19, 48], - )), - ), - ( - 48000, - 3, - 8, - 2, - 1, - 3, - 3, - Some(( - 16, - 13, - 7, - 5, - &[14, 15, 16, 17, 18, 19, 20, 22, 24, 27, 30, 33, 37, 41], - &[14, 15, 17, 19, 22, 27, 33, 41], - &[14, 15, 17, 19, 27, 41], - )), - ), - (48000, 14, 13, 2, 1, 0, 6, None), - ( - 96000, - 14, - 14, - 0, - 1, - 1, - 5, - Some(( - 14, - 9, - 5, - 1, - &[36, 38, 40, 42, 44, 46, 48, 50, 52, 54], - &[36, 38, 42, 46, 50, 54], - &[36, 54], - )), - ), - ( - 24000, - 13, - 12, - 1, - 0, - 2, - 0, - Some(( - 14, - 14, - 7, - 2, - &[27, 28, 30, 32, 34, 36, 38, 40, 42, 45, 48, 51, 54, 57, 61], - &[27, 30, 34, 38, 42, 48, 54, 61], - &[27, 38, 61], - )), - ), - ( - 22050, - 10, - 4, - 1, - 1, - 3, - 5, - Some(( - 12, - 7, - 4, - 2, - &[28, 30, 32, 34, 36, 38, 40, 42], - &[28, 30, 34, 38, 42], - &[28, 34, 42], - )), - ), - ( - 16000, - 5, - 9, - 3, - 1, - 3, - 6, - Some(( - 10, - 4, - 2, - 2, - &[35, 38, 42, 49, 57], - &[35, 42, 57], - &[35, 42, 57], - )), - ), - ( - 16000, - 12, - 8, - 0, - 0, - 3, - 1, - Some(( - 28, - 27, - 14, - 3, - &[ - 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, - 49, 50, 51, 52, 53, 54, 55, 56, - ], - &[29, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56], - &[29, 36, 46, 56], - )), - ), - ( - 88200, - 2, - 8, - 0, - 0, - 0, - 3, - Some(( - 30, - 27, - 14, - 1, - &[ - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, - ], - &[10, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37], - &[10, 37], - )), - ), - ( - 16000, - 14, - 5, - 1, - 1, - 1, - 7, - Some((10, 3, 2, 1, &[44, 47, 50, 53], &[44, 47, 53], &[44, 53])), - ), - ( - 96000, - 11, - 6, - 0, - 0, - 1, - 0, - Some(( - 8, - 8, - 4, - 1, - &[18, 19, 20, 21, 22, 23, 24, 25, 27], - &[18, 20, 22, 24, 27], - &[18, 27], - )), - ), - ( - 24000, - 15, - 12, - 3, - 1, - 3, - 5, - Some((8, 3, 2, 1, &[48, 52, 56, 61], &[48, 52, 61], &[48, 61])), - ), - ( - 44100, - 6, - 6, - 1, - 1, - 0, - 6, - Some(( - 14, - 8, - 4, - 1, - &[21, 22, 23, 24, 26, 28, 30, 33, 37], - &[21, 23, 26, 30, 37], - &[21, 37], - )), - ), - ( - 64000, - 7, - 8, - 2, - 1, - 2, - 5, - Some(( - 14, - 9, - 5, - 2, - &[19, 20, 22, 24, 26, 28, 31, 34, 37, 41], - &[19, 20, 24, 28, 34, 41], - &[19, 24, 41], - )), - ), - ( - 48000, - 13, - 12, - 3, - 0, - 3, - 6, - Some(( - 10, - 4, - 2, - 2, - &[40, 44, 48, 53, 59], - &[40, 48, 59], - &[40, 48, 59], - )), - ), - ( - 64000, - 12, - 7, - 2, - 0, - 0, - 1, - Some(( - 8, - 7, - 4, - 1, - &[22, 24, 26, 28, 30, 32, 34, 37], - &[22, 24, 28, 32, 37], - &[22, 37], - )), - ), - ( - 16000, - 2, - 10, - 1, - 1, - 3, - 2, - Some(( - 18, - 16, - 8, - 5, - &[ - 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 39, 42, 45, 49, 53, 58, - ], - &[20, 22, 24, 28, 32, 36, 42, 49, 58], - &[20, 22, 24, 32, 42, 58], - )), - ), - ( - 48000, - 6, - 14, - 1, - 1, - 1, - 6, - Some(( - 12, - 6, - 3, - 1, - &[20, 21, 22, 23, 24, 26, 28], - &[20, 22, 24, 28], - &[20, 28], - )), - ), - ( - 16000, - 7, - 3, - 2, - 0, - 3, - 0, - Some(( - 12, - 12, - 6, - 3, - &[23, 24, 26, 28, 30, 32, 34, 36, 39, 42, 45, 48, 51], - &[23, 26, 30, 34, 39, 45, 51], - &[23, 30, 39, 51], - )), - ), - ( - 88200, - 11, - 14, - 2, - 1, - 0, - 0, - Some(( - 10, - 10, - 5, - 1, - &[18, 19, 20, 21, 23, 25, 27, 29, 31, 33, 36], - &[18, 20, 23, 27, 31, 36], - &[18, 36], - )), - ), - ( - 16000, - 13, - 6, - 3, - 0, - 1, - 2, - Some(( - 8, - 6, - 3, - 1, - &[34, 37, 40, 43, 46, 50, 54], - &[34, 40, 46, 54], - &[34, 54], - )), - ), - ( - 32000, - 5, - 2, - 0, - 1, - 3, - 2, - Some(( - 10, - 8, - 4, - 3, - &[20, 22, 24, 26, 28, 30, 32, 34, 36], - &[20, 24, 28, 32, 36], - &[20, 24, 28, 36], - )), - ), - ( - 22050, - 8, - 6, - 2, - 0, - 2, - 7, - Some(( - 12, - 5, - 3, - 1, - &[32, 34, 37, 40, 43, 46], - &[32, 34, 40, 46], - &[32, 46], - )), - ), - ( - 32000, - 8, - 5, - 0, - 1, - 0, - 7, - Some(( - 12, - 5, - 3, - 1, - &[32, 34, 36, 38, 40, 42], - &[32, 34, 38, 42], - &[32, 42], - )), - ), - ( - 48000, - 3, - 1, - 0, - 0, - 3, - 7, - Some(( - 12, - 5, - 3, - 1, - &[18, 19, 20, 21, 22, 23], - &[18, 19, 21, 23], - &[18, 23], - )), - ), - ( - 22050, - 0, - 7, - 3, - 0, - 2, - 1, - Some(( - 16, - 15, - 8, - 4, - &[ - 13, 14, 15, 16, 18, 20, 22, 24, 26, 28, 31, 34, 37, 40, 44, 48, - ], - &[13, 14, 16, 20, 24, 28, 34, 40, 48], - &[13, 16, 24, 34, 48], - )), - ), - ( - 32000, - 10, - 9, - 0, - 0, - 3, - 7, - Some(( - 30, - 23, - 12, - 3, - &[ - 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, - 49, 50, 51, 52, - ], - &[29, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52], - &[29, 36, 44, 52], - )), - ), - ( - 32000, - 3, - 0, - 2, - 0, - 3, - 1, - Some(( - 10, - 9, - 5, - 3, - &[16, 17, 18, 19, 21, 23, 25, 27, 29, 32], - &[16, 17, 19, 23, 27, 32], - &[16, 17, 23, 32], - )), - ), - ( - 48000, - 3, - 3, - 0, - 1, - 2, - 7, - Some((8, 1, 1, 1, &[25, 27], &[25, 27], &[25, 27])), - ), - ( - 22050, - 13, - 2, - 2, - 0, - 0, - 2, - Some((6, 4, 2, 1, &[29, 31, 33, 35, 38], &[29, 33, 38], &[29, 38])), - ), - ( - 96000, - 2, - 6, - 0, - 0, - 0, - 3, - Some(( - 20, - 17, - 9, - 1, - &[ - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, - ], - &[10, 11, 13, 15, 17, 19, 21, 23, 25, 27], - &[10, 27], - )), - ), - ( - 64000, - 14, - 9, - 1, - 0, - 3, - 3, - Some(( - 10, - 7, - 4, - 2, - &[30, 32, 34, 36, 38, 40, 42, 45], - &[30, 32, 36, 40, 45], - &[30, 36, 45], - )), - ), - ( - 16000, - 13, - 14, - 0, - 1, - 1, - 6, - Some(( - 14, - 8, - 4, - 1, - &[41, 43, 45, 47, 49, 51, 53, 55, 58], - &[41, 45, 49, 53, 58], - &[41, 58], - )), - ), - ( - 96000, - 9, - 5, - 3, - 1, - 2, - 5, - Some((6, 1, 1, 1, &[22, 24], &[22, 24], &[22, 24])), - ), - ( - 16000, - 5, - 15, - 1, - 0, - 2, - 7, - Some(( - 20, - 13, - 7, - 2, - &[31, 33, 35, 37, 39, 42, 44, 46, 48, 51, 54, 57, 60, 63], - &[31, 33, 37, 42, 46, 51, 57, 63], - &[31, 42, 63], - )), - ), - ( - 24000, - 12, - 4, - 2, - 0, - 1, - 3, - Some((6, 3, 2, 1, &[31, 34, 37, 40], &[31, 34, 40], &[31, 40])), - ), - ( - 32000, - 14, - 10, - 3, - 0, - 3, - 2, - Some(( - 8, - 6, - 3, - 2, - &[34, 37, 40, 43, 47, 51, 55], - &[34, 40, 47, 55], - &[34, 40, 55], - )), - ), - ( - 32000, - 11, - 2, - 3, - 0, - 3, - 2, - Some((6, 4, 2, 1, &[27, 29, 31, 33, 36], &[27, 31, 36], &[27, 36])), - ), - ( - 32000, - 10, - 1, - 2, - 0, - 1, - 4, - Some((6, 2, 1, 1, &[29, 31, 34], &[29, 34], &[29, 34])), - ), - ( - 16000, - 5, - 15, - 1, - 1, - 1, - 3, - Some(( - 18, - 15, - 8, - 1, - &[ - 24, 25, 27, 29, 31, 33, 35, 37, 39, 42, 45, 48, 51, 55, 59, 63, - ], - &[24, 25, 29, 33, 37, 42, 48, 55, 63], - &[24, 63], - )), - ), - ( - 22050, - 10, - 3, - 1, - 0, - 1, - 7, - Some((10, 3, 2, 1, &[33, 35, 37, 40], &[33, 35, 40], &[33, 40])), - ), - ( - 22050, - 10, - 4, - 0, - 1, - 2, - 2, - Some(( - 10, - 8, - 4, - 1, - &[26, 28, 30, 32, 34, 36, 38, 40, 42], - &[26, 30, 34, 38, 42], - &[26, 42], - )), - ), - ( - 24000, - 0, - 4, - 2, - 0, - 0, - 5, - Some(( - 18, - 13, - 7, - 1, - &[16, 17, 18, 19, 20, 22, 24, 26, 28, 30, 32, 34, 37, 40], - &[16, 17, 19, 22, 26, 30, 34, 40], - &[16, 40], - )), - ), - ( - 24000, - 7, - 1, - 2, - 1, - 0, - 0, - Some(( - 8, - 8, - 4, - 1, - &[19, 20, 21, 23, 25, 27, 29, 31, 34], - &[19, 21, 25, 29, 34], - &[19, 34], - )), - ), - ( - 16000, - 11, - 15, - 1, - 1, - 1, - 6, - Some(( - 14, - 8, - 4, - 1, - &[38, 40, 42, 45, 48, 51, 54, 59, 64], - &[38, 42, 48, 54, 64], - &[38, 64], - )), - ), - ( - 44100, - 1, - 1, - 0, - 1, - 0, - 5, - Some((8, 3, 2, 1, &[19, 21, 23, 25], &[19, 21, 25], &[19, 25])), - ), - ( - 64000, - 15, - 10, - 3, - 1, - 2, - 1, - Some(( - 6, - 5, - 3, - 1, - &[32, 35, 38, 41, 45, 49], - &[32, 35, 41, 49], - &[32, 49], - )), - ), - ( - 88200, - 10, - 14, - 3, - 0, - 0, - 4, - Some((8, 4, 2, 1, &[23, 25, 27, 29, 32], &[23, 27, 32], &[23, 32])), - ), - ( - 32000, - 10, - 7, - 3, - 0, - 3, - 6, - Some((8, 2, 1, 1, &[38, 42, 46], &[38, 46], &[38, 46])), - ), - ( - 22050, - 12, - 4, - 0, - 1, - 1, - 2, - Some(( - 10, - 8, - 4, - 1, - &[26, 28, 30, 32, 34, 36, 38, 40, 42], - &[26, 30, 34, 38, 42], - &[26, 42], - )), - ), - ( - 22050, - 4, - 11, - 1, - 0, - 1, - 2, - Some(( - 22, - 20, - 10, - 2, - &[ - 18, 19, 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 45, 48, 51, 54, - 58, - ], - &[18, 20, 22, 24, 28, 32, 36, 40, 45, 51, 58], - &[18, 32, 58], - )), - ), - ( - 24000, - 1, - 1, - 2, - 0, - 2, - 6, - Some(( - 14, - 8, - 4, - 2, - &[19, 20, 22, 24, 26, 28, 30, 32, 34], - &[19, 22, 26, 30, 34], - &[19, 26, 34], - )), - ), - ( - 32000, - 4, - 2, - 1, - 1, - 1, - 7, - Some(( - 14, - 7, - 4, - 1, - &[23, 24, 26, 28, 30, 32, 34, 36], - &[23, 24, 28, 32, 36], - &[23, 36], - )), - ), - ( - 22050, - 9, - 13, - 2, - 0, - 3, - 2, - Some(( - 16, - 14, - 7, - 4, - &[24, 26, 28, 30, 32, 34, 36, 39, 42, 45, 48, 52, 56, 60, 64], - &[24, 28, 32, 36, 42, 48, 56, 64], - &[24, 28, 36, 48, 64], - )), - ), - ( - 44100, - 11, - 9, - 2, - 1, - 2, - 1, - Some(( - 12, - 11, - 6, - 2, - &[22, 23, 25, 27, 29, 31, 33, 35, 38, 41, 44, 47], - &[22, 23, 27, 31, 35, 41, 47], - &[22, 31, 47], - )), - ), - ( - 96000, - 7, - 10, - 2, - 0, - 1, - 7, - Some(( - 18, - 11, - 6, - 1, - &[19, 20, 22, 24, 26, 28, 30, 32, 35, 38, 41, 44], - &[19, 20, 24, 28, 32, 38, 44], - &[19, 44], - )), - ), - ( - 24000, - 12, - 8, - 0, - 1, - 3, - 4, - Some(( - 12, - 8, - 4, - 2, - &[33, 35, 37, 39, 41, 43, 45, 47, 49], - &[33, 37, 41, 45, 49], - &[33, 41, 49], - )), - ), - ( - 22050, - 7, - 7, - 1, - 1, - 1, - 6, - Some(( - 16, - 10, - 5, - 1, - &[26, 28, 30, 32, 34, 36, 38, 40, 42, 45, 48], - &[26, 30, 34, 38, 42, 48], - &[26, 48], - )), - ), - ( - 48000, - 13, - 10, - 0, - 0, - 3, - 5, - Some(( - 24, - 19, - 10, - 2, - &[ - 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, - ], - &[29, 30, 32, 34, 36, 38, 40, 42, 44, 46, 49], - &[29, 38, 49], - )), - ), - ( - 32000, - 15, - 15, - 0, - 0, - 1, - 5, - Some(( - 32, - 27, - 14, - 1, - &[ - 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, - 57, 58, 59, 60, 61, 62, 63, 64, - ], - &[37, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64], - &[37, 64], - )), - ), - ( - 96000, - 11, - 10, - 1, - 0, - 3, - 1, - Some(( - 16, - 15, - 8, - 4, - &[ - 19, 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, - ], - &[19, 20, 22, 24, 28, 32, 36, 40, 44], - &[19, 22, 28, 36, 44], - )), - ), - ( - 22050, - 12, - 12, - 0, - 0, - 0, - 2, - Some(( - 36, - 34, - 17, - 1, - &[ - 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, - 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, - ], - &[ - 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 61, - ], - &[26, 61], - )), - ), - ( - 22050, - 9, - 14, - 2, - 1, - 0, - 6, - Some((10, 4, 2, 1, &[32, 34, 36, 39, 42], &[32, 36, 42], &[32, 42])), - ), - ( - 96000, - 2, - 4, - 0, - 1, - 0, - 7, - Some((8, 1, 1, 1, &[19, 21], &[19, 21], &[19, 21])), - ), - ( - 24000, - 4, - 3, - 0, - 1, - 0, - 4, - Some(( - 12, - 8, - 4, - 1, - &[22, 24, 26, 28, 30, 32, 34, 36, 38], - &[22, 26, 30, 34, 38], - &[22, 38], - )), - ), - ( - 48000, - 10, - 9, - 3, - 0, - 2, - 0, - Some(( - 10, - 10, - 5, - 3, - &[18, 19, 21, 23, 25, 27, 30, 33, 36, 40, 45], - &[18, 21, 25, 30, 36, 45], - &[18, 21, 30, 45], - )), - ), - ( - 16000, - 11, - 2, - 2, - 1, - 3, - 7, - Some((8, 1, 1, 1, &[46, 50], &[46, 50], &[46, 50])), - ), - ( - 64000, - 11, - 8, - 3, - 1, - 0, - 7, - Some((8, 1, 1, 1, &[37, 41], &[37, 41], &[37, 41])), - ), - ( - 22050, - 1, - 12, - 2, - 0, - 1, - 3, - Some(( - 22, - 19, - 10, - 2, - &[ - 16, 17, 18, 19, 20, 22, 24, 26, 28, 30, 32, 34, 37, 40, 43, 46, 49, 53, 57, 61, - ], - &[16, 17, 19, 22, 26, 30, 34, 40, 46, 53, 61], - &[16, 30, 61], - )), - ), - ( - 32000, - 5, - 5, - 1, - 0, - 2, - 5, - Some(( - 16, - 11, - 6, - 2, - &[22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42], - &[22, 23, 26, 30, 34, 38, 42], - &[22, 30, 42], - )), - ), - ( - 48000, - 9, - 2, - 3, - 1, - 2, - 1, - Some((4, 3, 2, 1, &[19, 21, 23, 25], &[19, 21, 25], &[19, 25])), - ), - ( - 16000, - 13, - 5, - 1, - 0, - 0, - 2, - Some(( - 10, - 8, - 4, - 1, - &[33, 35, 37, 39, 41, 44, 47, 50, 53], - &[33, 37, 41, 47, 53], - &[33, 53], - )), - ), - ( - 88200, - 9, - 14, - 1, - 1, - 2, - 7, - Some(( - 12, - 5, - 3, - 1, - &[21, 22, 23, 24, 26, 28], - &[21, 22, 24, 28], - &[21, 28], - )), - ), - ( - 88200, - 10, - 2, - 1, - 0, - 3, - 1, - Some((2, 1, 1, 1, &[17, 19], &[17, 19], &[17, 19])), - ), - ( - 44100, - 15, - 15, - 3, - 0, - 3, - 1, - Some(( - 8, - 7, - 4, - 3, - &[35, 38, 41, 45, 49, 54, 59, 64], - &[35, 38, 45, 54, 64], - &[35, 38, 45, 64], - )), - ), - ( - 22050, - 5, - 7, - 2, - 1, - 0, - 6, - Some(( - 14, - 8, - 4, - 1, - &[26, 28, 30, 32, 34, 37, 40, 44, 48], - &[26, 30, 34, 40, 48], - &[26, 48], - )), - ), - ( - 48000, - 15, - 15, - 1, - 0, - 3, - 7, - Some(( - 12, - 5, - 3, - 1, - &[47, 50, 53, 56, 60, 64], - &[47, 50, 56, 64], - &[47, 64], - )), - ), - ( - 64000, - 12, - 6, - 2, - 1, - 1, - 0, - Some(( - 6, - 6, - 3, - 1, - &[21, 23, 25, 27, 29, 31, 34], - &[21, 25, 29, 34], - &[21, 34], - )), - ), - ( - 32000, - 7, - 2, - 2, - 0, - 2, - 4, - Some(( - 10, - 6, - 3, - 1, - &[24, 26, 28, 30, 32, 34, 36], - &[24, 28, 32, 36], - &[24, 36], - )), - ), - ( - 44100, - 6, - 1, - 2, - 0, - 3, - 1, - Some(( - 8, - 7, - 4, - 2, - &[16, 17, 18, 19, 20, 21, 23, 25], - &[16, 17, 19, 21, 25], - &[16, 19, 25], - )), - ), - ( - 32000, - 9, - 1, - 2, - 1, - 2, - 0, - Some(( - 6, - 6, - 3, - 1, - &[21, 23, 25, 27, 29, 31, 34], - &[21, 25, 29, 34], - &[21, 34], - )), - ), - ( - 64000, - 9, - 9, - 2, - 1, - 1, - 7, - Some(( - 14, - 7, - 4, - 1, - &[26, 28, 30, 32, 35, 38, 41, 45], - &[26, 28, 32, 38, 45], - &[26, 45], - )), - ), - ( - 16000, - 14, - 0, - 1, - 0, - 0, - 1, - Some(( - 8, - 7, - 4, - 1, - &[32, 34, 36, 38, 40, 42, 45, 48], - &[32, 34, 38, 42, 48], - &[32, 48], - )), - ), - ( - 44100, - 5, - 9, - 3, - 0, - 0, - 1, - Some(( - 14, - 13, - 7, - 1, - &[15, 16, 18, 20, 22, 24, 26, 28, 30, 33, 36, 39, 43, 47], - &[15, 16, 20, 24, 28, 33, 39, 47], - &[15, 47], - )), - ), - ( - 44100, - 11, - 11, - 2, - 1, - 1, - 2, - Some(( - 12, - 10, - 5, - 1, - &[24, 26, 28, 30, 32, 34, 36, 39, 42, 48, 55], - &[24, 28, 32, 36, 42, 55], - &[24, 55], - )), - ), - ( - 88200, - 0, - 15, - 0, - 1, - 1, - 2, - Some((6, 4, 2, 1, &[7, 9, 11, 13, 15], &[7, 11, 15], &[7, 15])), - ), - ( - 44100, - 5, - 9, - 1, - 0, - 1, - 6, - Some(( - 20, - 14, - 7, - 1, - &[20, 21, 22, 23, 24, 26, 28, 30, 32, 34, 36, 38, 41, 44, 47], - &[20, 22, 24, 28, 32, 36, 41, 47], - &[20, 47], - )), - ), -]; diff --git a/crates/ec-aac/src/sbr_chain.rs b/crates/ec-aac/src/sbr_chain.rs index 0ba1c501..4fcf994f 100644 --- a/crates/ec-aac/src/sbr_chain.rs +++ b/crates/ec-aac/src/sbr_chain.rs @@ -13,168 +13,12 @@ use crate::sbr_qmf::{ANALYSIS_BANDS, Analysis, SYNTHESIS_BANDS, Synthesis}; use ec_core::BitReader; use ec_dsp::Complex; use std::collections::HashMap; -use std::sync::atomic::{AtomicUsize, Ordering}; - -/// Debug: dumps parsed grid/header/e_q/q_q values for the first 20 frames -/// per channel when `EC_AAC_SBR_DEBUG` is set (diagnostic only, gated). -fn debug_dump( - header: &crate::sbr_payload::SbrHeader, - tables: &crate::sbr_bands::BandTables, - ch: &SbrChannel, - ch_idx: usize, -) { - static COUNT: AtomicUsize = AtomicUsize::new(0); - let n = COUNT.fetch_add(1, Ordering::Relaxed); - if n >= 20 { - return; - } - eprintln!( - "SBRDBG frame {n} ch{ch_idx}: amp_res={} kx={} k2={} n_low={} n_high={} n_q={} t_env={:?} freq_res={:?} t_noise={:?} f_high={:?} patches={:?}", - header.amp_res, - tables.kx, - tables.k2, - tables.n_low, - tables.n_high, - tables.n_q, - ch.t_env, - ch.freq_res, - ch.t_noise, - tables.f_high, - crate::sbr_hf::build_patches(tables), - ); - for (i, row) in ch.e_q.iter().enumerate() { - let (mn, mx) = row - .iter() - .fold((i32::MAX, i32::MIN), |(mn, mx), &v| (mn.min(v), mx.max(v))); - eprintln!(" e_q[{i}] len={} min={mn} max={mx} row={row:?}", row.len()); - } - for (i, row) in ch.q_q.iter().enumerate() { - let (mn, mx) = row - .iter() - .fold((i32::MAX, i32::MIN), |(mn, mx), &v| (mn.min(v), mx.max(v))); - eprintln!(" q_q[{i}] len={} min={mn} max={mx} row={row:?}", row.len()); - } -} /// Envelope-grid "time slots" (§4.6.18.4, `numTimeSlots = 16` for a /// 1024-sample core frame) are half as fine as the QMF slots SBR actually /// runs at (32 per frame): `RATE = 2` converts one to the other. const RATE: i64 = 2; -/// Round-20 residual/side-info correlation: one row per (access unit, -/// channel) `apply_data` actually ran, giving `sbr_real_library.rs`'s -/// residual-energy grid a side-info table to correlate against without -/// re-parsing debug text. `frame` is a single counter shared by every -/// channel `apply_data` touches in one call, so it lines up 1:1 with the -/// AU-aligned (2048 output samples/AU for a plain 2x stream) blocks the -/// residual grid uses. -#[derive(Clone, Debug)] -pub struct SbrSideInfoRow { - pub frame: usize, - pub tag: u8, - pub ch: usize, - /// `"fresh"` for a frame `apply` reached (a real payload this AU), - /// `"hold"` for one `apply_last` reached (no fresh payload, reusing the - /// last parsed frame's data per §4.6.18). - pub source: &'static str, - pub coupling: bool, - pub amp_res: u8, - /// Band-index domain (same units `f_high`/`f_noise` and the residual - /// grid's FFT-bucket index share, per `per_band_correlation`'s - /// `band_hz = rate/256` measurement) the crossover and high-res/noise - /// band boundaries below live in. - pub kx: i64, - pub k2: i64, - pub f_high: Vec, - pub f_noise: Vec, - pub t_env: Vec, - pub freq_res: Vec, - pub invf_mode: Vec, - pub add_harmonic: Option>, - pub e_q_means: Vec, - pub q_q_means: Vec, - /// Header fields held for the frame's duration (§4.6.18.3.2), added for - /// the FMJ-vs-reeln feature-table hunt: `sbr_header()`'s own coding - /// choices, not per-envelope grid data. - pub freq_scale: u8, - pub alter_scale: u8, - pub noise_bands: u8, - pub limiter_bands: u8, - pub limiter_gains: u8, - pub interpol_freq: u8, - pub smoothing_mode: u8, - pub start_freq: u8, - pub stop_freq: u8, - pub xover_band: u8, - /// `t_noise.len() - 1`, i.e. `numNoiseFloors` this frame. - pub num_noise: usize, - /// One entry per HF patch, each the patch's band count (from - /// `sbr_hf::build_patches`), so patch geometry is visible per frame. - pub patch_lengths: Vec, - pub df_env: Vec, - pub df_noise: Vec, -} - -static SIDEINFO_LOG: std::sync::OnceLock>> = - std::sync::OnceLock::new(); -static SIDEINFO_FRAME: AtomicUsize = AtomicUsize::new(0); - -/// Diagnostic-only counter: how many `apply_data` calls process-wide ran -/// with `source == "hold"` (an AU whose FIL carried no fresh SBR payload, -/// falling back to `apply_last`'s reuse of the last parsed frame). Read via -/// [`hold_call_count`]; not gated by an env var since incrementing an atomic -/// is unconditionally cheap, unlike the `EC_AAC_SBR_*_DEBUG` prints nearby. -static HOLD_CALLS: AtomicUsize = AtomicUsize::new(0); - -/// Total `apply_data` calls process-wide where the SBR payload was reused -/// from a prior frame (`apply_last`) rather than freshly parsed this AU -- -/// diagnostic instrument for `sbr441_family_sample_drift_probe`. -pub fn hold_call_count() -> usize { - HOLD_CALLS.load(Ordering::Relaxed) -} - -fn sideinfo_enabled() -> bool { - crate::knobs::sbr_sideinfo_debug() -} - -/// Round-34 Task 1 diagnostic: per-AU per-QMF-band mean energy at a named -/// pipeline stage, gated by `EC_AAC_SBR_QMFDUMP`. `start_band` is the -/// absolute QMF band index `rows[0]` represents (`0` for the core analysis -/// matrix, `kx` for the HF matrix), so every printed band index lines up -/// with `build_patches`' target-band units without the reader having to -/// offset anything by hand. -fn qmfdump_enabled() -> bool { - std::env::var("EC_AAC_SBR_QMFDUMP").is_ok() -} - -fn qmfdump_energies(stage: &str, start_band: usize, rows: &[Vec>]) { - let means: Vec = rows - .iter() - .map(|series| { - if series.is_empty() { - 0.0 - } else { - series - .iter() - .map(|c| c.re * c.re + c.im * c.im) - .sum::() - / series.len() as f64 - } - }) - .collect(); - eprintln!("QMFDUMP stage={stage} start_band={start_band} means={means:?}"); -} - -/// Drains nothing -- returns a clone of every row logged so far this -/// process, in call order. Empty unless `EC_AAC_SBR_SIDEINFO_DEBUG` was set -/// before decoding. -pub fn sbr_sideinfo_log() -> Vec { - let Some(log) = SIDEINFO_LOG.get() else { - return Vec::new(); - }; - log.lock().map(|l| l.clone()).unwrap_or_default() -} - /// Per-channel DSP state an SBR element's reconstruction carries across /// frames: the QMF filterbank pair, the HF generator's LPC history and /// chirp factors, and the noise-floor generator. @@ -469,7 +313,7 @@ impl SbrChain { if let Some(elem) = self.elements.get_mut(&(tag, is_cpe)) { elem.last_data = Some(data.clone()); } - self.apply_data(tag, is_cpe, data, planes, "fresh") + self.apply_data(tag, is_cpe, data, planes) } /// Re-runs the chain for `tag` using the last successfully parsed @@ -491,7 +335,7 @@ impl SbrChain { self.apply_low_only(tag, is_cpe, planes); return Ok(()); }; - self.apply_data(tag, is_cpe, &data, planes, "hold") + self.apply_data(tag, is_cpe, &data, planes) } fn apply_data( @@ -500,11 +344,7 @@ impl SbrChain { is_cpe: bool, data: &SbrData, planes: &mut [Vec], - source: &'static str, ) -> crate::Result<()> { - if source == "hold" { - HOLD_CALLS.fetch_add(1, Ordering::Relaxed); - } let band_limit = if self.downsampled { ANALYSIS_BANDS } else { SYNTHESIS_BANDS }; let Some(elem) = self.elements.get_mut(&(tag, is_cpe)) else { return Ok(()); @@ -518,12 +358,6 @@ impl SbrChain { return Ok(()); }; let coupling = data.coupling; - let sideinfo_on = sideinfo_enabled(); - let au_frame = if sideinfo_on { - SIDEINFO_FRAME.fetch_add(1, Ordering::Relaxed) - } else { - 0 - }; // PS: the mono channel is synthesized twice, through its own state and // through channel 1's (the right output), so both stay in step. let ps_mode = !is_cpe && planes.len() >= 2 && data.ps.is_some(); @@ -542,20 +376,6 @@ impl SbrChain { if num_slots == 0 { continue; } - if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() { - static N: AtomicUsize = AtomicUsize::new(0); - let n = N.fetch_add(1, Ordering::Relaxed); - if n < 40 { - let rms = (plane.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() - / plane.len().max(1) as f64) - .sqrt(); - eprintln!( - "PREQMF n={n} tag={tag} ch={ch} len={} rms={rms:.6} first8={:?}", - plane.len(), - &plane[..8.min(plane.len())] - ); - } - } let state = &mut elem.channels[ch]; let mut low_cur = vec![vec![Complex::ZERO; num_slots]; ANALYSIS_BANDS]; let mut raw = Vec::with_capacity(num_slots); @@ -584,63 +404,6 @@ impl SbrChain { } let sbr_ch = &data.channels[ch]; - if std::env::var("EC_AAC_SBR_DEBUG").is_ok() { - eprintln!("SBRDBG coupling={}", data.coupling); - debug_dump(&header, &tables, sbr_ch, ch); - } - if sideinfo_on { - let mean = |rows: &[Vec]| -> Vec { - rows.iter() - .map(|r| { - if r.is_empty() { - 0.0 - } else { - r.iter().map(|&v| f64::from(v)).sum::() / r.len() as f64 - } - }) - .collect() - }; - let row = SbrSideInfoRow { - frame: au_frame, - tag, - ch, - source, - coupling, - amp_res: header.amp_res, - kx: tables.kx, - k2: tables.k2, - f_high: tables.f_high.clone(), - f_noise: tables.f_noise.clone(), - t_env: sbr_ch.t_env.clone(), - freq_res: sbr_ch.freq_res.clone(), - invf_mode: sbr_ch.invf_mode.clone(), - add_harmonic: sbr_ch.add_harmonic.clone(), - e_q_means: mean(&sbr_ch.e_q), - q_q_means: mean(&sbr_ch.q_q), - freq_scale: header.freq_scale, - alter_scale: header.alter_scale, - noise_bands: header.noise_bands, - limiter_bands: header.limiter_bands, - limiter_gains: header.limiter_gains, - interpol_freq: header.interpol_freq, - smoothing_mode: header.smoothing_mode, - start_freq: header.start_freq, - stop_freq: header.stop_freq, - xover_band: header.xover_band, - num_noise: sbr_ch.t_noise.len().saturating_sub(1), - patch_lengths: sbr_hf::build_patches(&tables).iter().map(|p| p.width).collect(), - df_env: sbr_ch.df_env.clone(), - df_noise: sbr_ch.df_noise.clone(), - }; - let log = SIDEINFO_LOG.get_or_init(|| std::sync::Mutex::new(Vec::new())); - if let Ok(mut l) = log.lock() { - l.push(row); - } - } - let qmfdump_on = qmfdump_enabled(); - if qmfdump_on { - qmfdump_energies("analysis", 0, &low_cur); - } let hf = sbr_hf::generate( &low_cur, &tables, @@ -648,9 +411,6 @@ impl SbrChain { &mut state.chirp, &mut state.hf_hist, ); - if qmfdump_on { - qmfdump_energies("post_patch", tables.kx as usize, &hf); - } let (env_energy, noise_energy) = sbr_env::dequantize_frame(&data.channels, ch, coupling); // sbr_env::adjust works in QMF-slot units; the parsed borders @@ -718,9 +478,6 @@ impl SbrChain { d.truncate(num_slots); d }; - if qmfdump_on { - qmfdump_energies("post_adjust", tables.kx as usize, &hf); - } let kx = (tables.kx as usize).min(ANALYSIS_BANDS); let k2 = (tables.k2 as usize).min(band_limit); @@ -728,11 +485,9 @@ impl SbrChain { for slot in 0..num_slots { let mut v = [Complex::ZERO; SYNTHESIS_BANDS]; v[0..kx].copy_from_slice(&raw[slot][0..kx]); - if !crate::knobs::sbr_hf_bypass() { - for b in kx..k2 { - if b - kx < hf.len() { - v[b] = hf[b - kx][slot]; - } + for b in kx..k2 { + if b - kx < hf.len() { + v[b] = hf[b - kx][slot]; } } vs.push(v); @@ -760,20 +515,6 @@ impl SbrChain { } _ => synth_slots(&mut state.synthesis, &vs), }; - if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() { - static N: AtomicUsize = AtomicUsize::new(0); - let n = N.fetch_add(1, Ordering::Relaxed); - if n < 40 { - let rms = (out.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() - / out.len().max(1) as f64) - .sqrt(); - eprintln!( - "POSTQMF n={n} tag={tag} ch={ch} len={} rms={rms:.6} first8={:?}", - out.len(), - &out[..8.min(out.len())] - ); - } - } if ps_planes.is_none() { *plane = out; } diff --git a/crates/ec-aac/src/sbr_env.rs b/crates/ec-aac/src/sbr_env.rs index 2ede748b..b7e07548 100644 --- a/crates/ec-aac/src/sbr_env.rs +++ b/crates/ec-aac/src/sbr_env.rs @@ -168,29 +168,6 @@ impl Default for AdjustState { } } -fn noise_fraction_debug() -> bool { - crate::knobs::sbr_noise_fraction() -} - -type NoiseFractionRows = Vec<(usize, f64, f64, f64)>; - -static NOISE_FRACTION: std::sync::OnceLock> = - std::sync::OnceLock::new(); - -/// Diagnostic (`EC_AAC_SBR_NOISE_FRACTION`): per absolute QMF band, -/// bookkept `(band, Σ signal energy·slots, Σ noise energy·slots, Σ |Y|² -/// post-adjust)` -- the last column against the first two's sum is the -/// per-subband realised/target energy ratio. -pub fn noise_fraction_table() -> Vec<(usize, f64, f64, f64)> { - let mut out: Vec<(usize, f64, f64, f64)> = NOISE_FRACTION - .get() - .and_then(|m| m.lock().ok()) - .map(|g| g.clone()) - .unwrap_or_default(); - out.sort_by_key(|e| e.0); - out -} - /// Adjusts `hf` (`[m][slot]`, `m = k - kx`) in place: envelope gains, /// noise floor and added sinusoids per §4.6.18.7.3-7.6. `ch.t_env`/`t_noise` /// are in QMF slots (already ×2). `reset` mirrors the spec's header-reset @@ -222,14 +199,7 @@ pub fn adjust( state.g_hist.clear(); state.q_hist.clear(); } - let limiter_max = if std::env::var("EC_AAC_SBR_LIMITER_OFF").is_ok() { - LIMITER_FACTOR[3] - } else { - LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)] - }; - let zero_noise = crate::knobs::sbr_noise_zero(); - let limdump = std::env::var("EC_AAC_SBR_CELLDUMP").is_ok(); - let track_fraction = noise_fraction_debug(); + let limiter_max = LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)]; let h_sl = if header.smoothing_mode == 0 { H_SL } else { 0 }; let l_a = ch.l_a; let l_a_prev: i64 = if state.la_prev_at_end { 0 } else { -1 }; @@ -340,13 +310,9 @@ pub fn adjust( let sum_orig: f64 = (lo..hi).map(|m| e_orig[m]).sum(); let sum_curr: f64 = (lo..hi).map(|m| e_curr[m]).sum(); let g_max = (limiter_max * ((EPS + sum_orig) / (EPS + sum_curr)).sqrt()).min(GAIN_MAX_CLIP); - let mut capped = 0usize; for m in lo..hi { let q_max = q_m[e][m] * g_max / gain[e][m]; q_m[e][m] = q_m[e][m].min(q_max); - if gain[e][m] > g_max { - capped += 1; - } gain[e][m] = gain[e][m].min(g_max); } let sum_act: f64 = (lo..hi) @@ -362,32 +328,6 @@ pub fn adjust( q_m[e][m] *= boost; s_m[e][m] *= boost; } - if limdump { - eprintln!( - "LIMDUMP ei={e} band=[{},{}) limiter_bands={} j={j}/{n_lim} \ - E_orig_mapped={sum_orig:.6e} E_curr={sum_curr:.6e} G_max={g_max:.6} \ - capped_cells={capped}/{} E_actual={sum_act:.6e} G_boost={boost:.6}", - limiter_table[j], - limiter_table[j + 1], - header.limiter_bands, - hi - lo, - ); - } - } - if track_fraction { - let tab = NOISE_FRACTION.get_or_init(|| std::sync::Mutex::new(Vec::new())); - if let Ok(mut t) = tab.lock() { - for m in 0..m_max { - let sig = e_orig[m] / (1.0 + q_mapped[m]) * slots; - let noi = e_orig[m] * q_mapped[m] / (1.0 + q_mapped[m]) * slots; - if let Some(row) = t.iter_mut().find(|r| r.0 == m + kx) { - row.1 += sig; - row.2 += noi; - } else { - t.push((m + kx, sig, noi, 0.0)); - } - } - } } } state.s_index_prev = s_index_prev; @@ -451,25 +391,13 @@ pub fn adjust( noise_idx = (noise_idx + 1) & 0x1ff; if s_m[e][m] != 0.0 { y = y + Complex::new(s_m[e][m] * p_re, s_m[e][m] * p_im); - } else if !transient && !zero_noise { + } else if !transient { let (vr, vi) = SBR_NOISE[noise_idx]; y = y + Complex::new(q_filt[m] * vr, q_filt[m] * vi); } row[i] = y; p_im = -p_im; } - if track_fraction { - let tab = NOISE_FRACTION.get_or_init(|| std::sync::Mutex::new(Vec::new())); - if let Ok(mut t) = tab.lock() { - for m in 0..m_max { - if let (Some(row), Some(y)) = - (t.iter_mut().find(|r| r.0 == m + kx), hf[m].get(i)) - { - row.3 += y.norm_sqr(); - } - } - } - } state.index_noise = (state.index_noise + m_max) & 0x1ff; state.index_sine = (state.index_sine + 1) & 3; } diff --git a/crates/ec-aac/src/sbr_hf.rs b/crates/ec-aac/src/sbr_hf.rs index 7420658f..eed959ac 100644 --- a/crates/ec-aac/src/sbr_hf.rs +++ b/crates/ec-aac/src/sbr_hf.rs @@ -57,64 +57,6 @@ use crate::sbr_bands::BandTables; use ec_dsp::Complex; -/// Characterizes (but, as of round-37, is deliberately NOT applied to) a QMF -/// bank convention gap `sbr_qmf`'s round-trip tuning never exercised: -/// replaying a source band's raw complex series into a *different* synthesis -/// band (exactly what patch copying does) lands the content at the "wrong" -/// absolute frequency by exactly one synthesis-band width whenever -/// `target_band - source_band` is odd, with the sign flipping every other -/// odd step -- i.e. a period-4 pattern in `target - source` (even -/// difference: no error; `+1 mod 4`: content lands one band flat; `+3 mod -/// 4`: one band sharp). `sbr_qmf::Synthesis`'s modulation kernel centres -/// band `k` at `(k+0.5) * pi/(2*SYNTHESIS_BANDS)` via a phase intercept that -/// is itself `(k+0.5) * pi/4` -- exactly the odd-stacked cosine-modulated- -/// bank convention the round-trip test tuned for same-band replay, where -/// this intercept cancels against the receiving band's own reconstruction. -/// -/// Round-35 had `generate` counter-rotate the patched series by this amount -/// before it reaches `Synthesis`, and proved (in isolation, via a -/// stationary-tone FFT probe through the actual production call site -- -/// `sbr_qmf::tests::patch_replay_through_generate_preserves_absolute_frequency_mapping`) -/// that doing so restores textbook-coherent absolute-frequency placement. -/// Round-36/37's real-file mechanical A/B (`EC_AAC_SBR_PARITY_SPLIT`) -/// showed applying that correction REGRESSES odd-gap coherence against the -/// reference decoder (ch0 0.1907->0.0714, ch1 0.2461->0.0741; a -/// sign-flipped variant and a half-rate variant both regress it too, to -/// 0.0757 and 0.1449 respectively on ch0) -- the reference apparently -/// reproduces this same "wrong" convention itself, so leaving the raw copy -/// unrotated is what actually matches it. This function is kept (and -/// pinned by its own unit tests) purely to document that convention as -/// intentional, not to be called from `generate`'s patch-copy site. -/// -/// Round-46 raised a re-trial candidate: `EC_AAC_SBR_PARITY_SPLIT` (an 8-bin -/// STFT metric) had been suspected untrustworthy at this precision, and a -/// QMF-exact fit of the reference decoder's own HF content on patch -/// `(3,28,11)` (gap 25, `mod 4 == 1`) found a transfer with this function's -/// quarter-turn phase signature. Round-47 re-applied the rotation at -/// `generate`'s patch-copy site (env-gated, both signs) and re-judged with -/// `EC_AAC_SBR_QMF_WITNESS`'s own QMF-domain coherence table instead of the -/// STFT one -- the trustworthy instrument the re-trial was chartered to use. -/// Both signs made sim-vs-ours coherence on patch `(3,28,11)` WORSE, not -/// better (band28 0.2628 unrotated -> 0.0161 rotated -> 0.0282 flipped; -/// band38 0.0669 -> 0.0694 -> 0.1068), moving further from the reference's -/// 0.42-0.79 rather than toward it, refuting round-46's fit-based -/// prediction under the very instrument that was supposed to vindicate it. -/// (One sign did nudge whole-file full-band correlation up rather than -/// round-36's dip, but that's moot once the witness itself disagrees.) The -/// verdict stands as round-37 left it: `generate`'s patch-copy site keeps -/// the plain, unrotated copy. -/// Returns the signed number of `pi/2` quarter-turns per QMF slot that -/// *would* need to be applied to restore frequency-pure placement. -#[allow(dead_code)] // documentation of the characterized convention; tests call it -pub(crate) fn band_shift_correction(_target_band: usize, _source_band: usize) -> i64 { - // The fitted Kaiser bank's odd-stacked convention needed a per-slot - // quarter-turn correction here (see the doc above); the normative - // ISO/IEC 14496-3 4.6.18.4/4.6.18.8.2 QMF pair in `sbr_qmf` carries its - // own correct phase from the spec equations directly, so patching needs - // no extra rotation at this site. - 0 -} - /// One copy-up patch: `width` consecutive QMF bands starting at /// `source_start` are written to `target_start..target_start+width`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -283,40 +225,18 @@ impl ChirpState { /// Advances the state one frame given this frame's `bs_invf_mode` per /// noise band, returning the smoothed bw to use for HF generation. - /// - /// (Round-22 sweep instrumentation, zero cost unset) `EC_AAC_SBR_BW_SCALE` - /// scales the chirp target globally (clamped below 1.0) before - /// smoothing, to probe whether the chirp/inverse-filter strength is - /// miscalibrated on real content. `EC_AAC_SBR_CHIRP_SMOOTH=none` skips - /// the attack/release smoothing entirely (target used immediately); - /// `=swap` swaps which curve (0.75/0.25 vs 0.90625/0.09375) applies to - /// rising vs falling bw, to probe whether the asymmetry is backwards. pub fn update(&mut self, invf_mode: &[u8]) -> &[f64] { - let scale = bw_scale_override(); - let smooth = chirp_smooth_mode(); for ((slot, prev), &mode) in self.bw.iter_mut().zip(&mut self.prev_mode).zip(invf_mode) { - let target = (bw_target(mode, *prev) * scale).min(0.999_999); + let target = bw_target(mode, *prev).min(0.999_999); *prev = mode; - *slot = match smooth { - ChirpSmooth::None => target, - ChirpSmooth::Current => { - // 4.6.18.6.3 bwArray smoothing: both branches weight the - // NEW value (0.75 falling, 0.90625 rising). The rising - // branch previously had the operands swapped (0.09375 - // of the new value), making every rise ~10x too slow. - if target < *slot { - 0.75 * target + 0.25 * *slot - } else { - 0.90625 * target + 0.09375 * *slot - } - } - ChirpSmooth::Swap => { - if target < *slot { - 0.90625 * target + 0.09375 * *slot - } else { - 0.75 * *slot + 0.25 * target - } - } + // 4.6.18.6.3 bwArray smoothing: both branches weight the + // NEW value (0.75 falling, 0.90625 rising). The rising + // branch previously had the operands swapped (0.09375 + // of the new value), making every rise ~10x too slow. + *slot = if target < *slot { + 0.75 * target + 0.25 * *slot + } else { + 0.90625 * target + 0.09375 * *slot }; // 4.6.18.6.3: a smoothed bw under 0.015 is forced to zero. if *slot < 0.015 { @@ -327,32 +247,6 @@ impl ChirpState { } } -/// `EC_AAC_SBR_BW_SCALE` global multiplier on the chirp target, clamped below 1.0 -/// (a chirp target at or past 1.0 is an undamped all-pole resonance, not a -/// meaningful "stronger" whitening). `1.0` (no-op) when unset or unparsable. -fn bw_scale_override() -> f64 { - std::env::var("EC_AAC_SBR_BW_SCALE") - .ok() - .and_then(|s| s.parse::().ok()) - .map(|s| s.max(0.0)) - .unwrap_or(1.0) -} - -#[derive(Clone, Copy, PartialEq, Eq)] -enum ChirpSmooth { - None, - Current, - Swap, -} - -fn chirp_smooth_mode() -> ChirpSmooth { - match std::env::var("EC_AAC_SBR_CHIRP_SMOOTH").as_deref() { - Ok("none") => ChirpSmooth::None, - Ok("swap") => ChirpSmooth::Swap, - _ => ChirpSmooth::Current, - } -} - /// Order-2 linear-prediction coefficients per ISO 14496-3 4.6.18.6.2 /// (covariance method, `phi(i,j) = sum_n x[n-i]*conj(x[n-j])` over /// `n = 2..x.len()`, index 0 of `x` is the oldest slot). Returns the spec's @@ -464,13 +358,6 @@ pub fn generate( lpc2(&ext) }; let g = bw[noise_band_of(target_band)]; - // (Round-46, Task 2) `EC_AAC_SBR_BW_DUMP` -- one line per patched - // band per frame with the smoothed chirp bw actually applied, - // to compare against the QMF-domain fitted reference transfer - // (`hf_patch_transfer_fit` in the real-file test). - if std::env::var("EC_AAC_SBR_BW_DUMP").is_ok() { - eprintln!("BW_DUMP target_band={target_band} source_band={source_band} bw={g:.6}"); - } // 4.6.18.6.3 feed-forward: X_high = X_low + bw*alpha0*X_low[l-1] // + bw^2*alpha1*X_low[l-2]. bw=0 is a plain copy, bw->1 is the // full inverse (whitening) filter. The all-pole recursion this @@ -493,10 +380,6 @@ pub fn generate( }; let dst = &mut out[target_band - kx]; for (n, &x) in series.iter().enumerate() { - // Round-37 (Task 2): `band_shift_correction` is proven exact - // in isolation but a real-file A/B (`EC_AAC_SBR_PARITY_SPLIT`) - // shows applying it REGRESSES odd-gap coherence against the - // reference; `dst` stays the uncorrected band placement. dst[n] = x + c1 * x_prev1 + c2 * x_prev2; x_prev2 = x_prev1; x_prev1 = x; diff --git a/crates/ec-aac/src/sbr_payload.rs b/crates/ec-aac/src/sbr_payload.rs index fa58f27f..f94180e5 100644 --- a/crates/ec-aac/src/sbr_payload.rs +++ b/crates/ec-aac/src/sbr_payload.rs @@ -443,11 +443,6 @@ impl SbrParser { row[b] = (row[b - 1] + delta).clamp(0, cap); } } - if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() { - eprintln!( - "ENVDBG dt={dt} balance={balance} amp_res={amp_res} bands={bands} prev={prev:?} row={row:?}" - ); - } Ok(row) } diff --git a/crates/ec-aac/src/sbr_qmf.rs b/crates/ec-aac/src/sbr_qmf.rs index a1c06315..0cae487f 100644 --- a/crates/ec-aac/src/sbr_qmf.rs +++ b/crates/ec-aac/src/sbr_qmf.rs @@ -31,67 +31,6 @@ pub const ANALYSIS_BANDS: usize = 32; /// Synthesis channel count (twice the core's, spanning the doubled band). pub const SYNTHESIS_BANDS: usize = 64; -/// The shared ISO/IEC 14496-3 4.6.18.4 64-band analysis core: `PROTO_LEN` -/// real history samples in (newest inserted last, matching PCM order), -/// `SYNTHESIS_BANDS` complex subbands out per slot of `SYNTHESIS_BANDS` new -/// samples. -struct QmfAnalysis64 { - hist: [f64; PROTO_LEN], - cos_tab: Vec, - sin_tab: Vec, -} - -impl QmfAnalysis64 { - fn new() -> QmfAnalysis64 { - let mut cos_tab = vec![0.0f64; SYNTHESIS_BANDS * 128]; - let mut sin_tab = vec![0.0f64; SYNTHESIS_BANDS * 128]; - for k in 0..SYNTHESIS_BANDS { - for n in 0..128 { - // X[k] = sum_n u[n] * exp(j*pi/128*(k+0.5)*(2n-1)) - let theta = std::f64::consts::PI / 128.0 - * (k as f64 + 0.5) - * (2.0 * n as f64 - 1.0); - cos_tab[k * 128 + n] = theta.cos(); - sin_tab[k * 128 + n] = theta.sin(); - } - } - QmfAnalysis64 { - hist: [0.0; PROTO_LEN], - cos_tab, - sin_tab, - } - } - - fn process_slot(&mut self, samples64: &[f64; SYNTHESIS_BANDS]) -> [Complex; SYNTHESIS_BANDS] { - for &s in samples64 { - self.hist.copy_within(0..PROTO_LEN - 1, 1); - self.hist[0] = s; - } - // Z[n] = x[n]*c[n], n=0..639; u[n] = sum_{j=0}^{4} Z[n+128j], n=0..127 - let mut u = [0.0f64; 128]; - for (n, uu) in u.iter_mut().enumerate() { - let mut acc = 0.0; - for j in 0..5 { - let idx = n + 128 * j; - acc += self.hist[idx] * QMF_WINDOW[idx]; - } - *uu = acc; - } - let mut out = [Complex::new(0.0, 0.0); SYNTHESIS_BANDS]; - for (k, slot) in out.iter_mut().enumerate() { - let row = k * 128; - let mut re = 0.0f64; - let mut im = 0.0f64; - for (n, &u_n) in u.iter().enumerate() { - re += u_n * self.cos_tab[row + n]; - im += u_n * self.sin_tab[row + n]; - } - *slot = Complex::new(re, im); - } - out - } -} - /// ISO/IEC 14496-3 4.6.18.4.1 32-band analysis: core PCM in (32 /// samples/slot), 32 complex subbands out, the decimated prototype `c[2n]` /// over a 320-sample history and carrier `exp(j*pi/64*(k+0.5)*(2n-0.5))` @@ -176,48 +115,6 @@ impl Default for Analysis { } } -/// The same [`QmfAnalysis64`] core run directly on already-doubled-rate -/// samples: doubled-rate PCM in, complex subband slots out, one slot per -/// [`SYNTHESIS_BANDS`] input samples. The decoder itself never runs this -- -/// it only ever synthesizes at this band count, from HF-generated subbands, -/// never analyzes a full-rate PCM signal back into them -- so this exists -/// for exact QMF-domain measurement only (round-46's third-witness -/// instrument): it lets a test read the reference decoder's and our own -/// already-decoded 44100Hz output straight into the same 64-band grid the -/// patch map's band indices are defined in, with no STFT-bin-width -/// approximation. -pub struct HfAnalysis { - core: QmfAnalysis64, -} - -impl HfAnalysis { - pub fn new() -> HfAnalysis { - HfAnalysis { - core: QmfAnalysis64::new(), - } - } - - /// Feeds exactly [`SYNTHESIS_BANDS`] new samples (oldest first, matching - /// PCM order) and returns the [`SYNTHESIS_BANDS`] complex subband - /// samples for this slot. - pub fn process_slot( - &mut self, - samples: &[f32; SYNTHESIS_BANDS], - ) -> [Complex; SYNTHESIS_BANDS] { - let mut x64 = [0.0f64; SYNTHESIS_BANDS]; - for (d, &s) in x64.iter_mut().zip(samples.iter()) { - *d = s as f64; - } - self.core.process_slot(&x64) - } -} - -impl Default for HfAnalysis { - fn default() -> HfAnalysis { - HfAnalysis::new() - } -} - pub struct Synthesis { bands: usize, v: Vec, @@ -474,29 +371,6 @@ mod tests { - #[allow(dead_code)] - /// Peak-bin frequency (cycles/sample, in `0.0..0.5`) of a real signal's - /// dominant spectral line: FFT magnitude over the whole window, biggest - /// positive-frequency bin. `signal.len()` must be a power of two. - fn dominant_freq(signal: &[f64]) -> f64 { - let n = signal.len(); - let mut fft = Fft::::new(n); - let mut spectrum: Vec> = - signal.iter().map(|&v| Complex::new(v, 0.0)).collect(); - fft.forward(&mut spectrum); - let mut best_bin = 1usize; - let mut best_mag = -1.0f64; - for (k, c) in spectrum.iter().enumerate().take(n / 2).skip(1) { - let mag = c.norm_sqr(); - if mag > best_mag { - best_mag = mag; - best_bin = k; - } - } - best_bin as f64 / n as f64 - } - - /// TASK 1(b) (round-35 SBR-HF-patch conviction): the actual patch /// operation -- analyze a real tone at a known offset from source band /// `p`'s own centre via [`Analysis`], copy that complex series straight diff --git a/crates/ec-aac/src/tables.rs b/crates/ec-aac/src/tables.rs index 02407588..68a9a612 100644 --- a/crates/ec-aac/src/tables.rs +++ b/crates/ec-aac/src/tables.rs @@ -1607,3 +1607,66 @@ pub static TNS_MAX_BANDS_LONG: [usize; 13] = [31, 31, 34, 40, 42, 51, 46, 46, 42 /// The same limit for 128-line windows. pub static TNS_MAX_BANDS_SHORT: [usize; 13] = [9, 9, 10, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14]; + +#[cfg(test)] +mod tests { + use super::*; + + /// The derived tables have to be complete prefix codes, and that is checkable + /// without any oracle: a Kraft sum of exactly 1 with the entry count the + /// standard prescribes leaves no room for a transcription slip. + #[test] + fn every_codebook_is_a_complete_prefix_code() { + let expected = [81usize, 81, 81, 81, 81, 81, 64, 64, 169, 169, 289]; + for (i, cb) in CODEBOOKS.iter().enumerate() { + assert_eq!( + cb.codes.len(), + expected[i], + "codebook {} entry count", + i + 1 + ); + let kraft: f64 = cb + .codes + .iter() + .map(|(l, _)| 2f64.powi(-i32::from(*l))) + .sum(); + assert!( + (kraft - 1.0).abs() < 1e-12, + "codebook {} Kraft sum {kraft}", + i + 1 + ); + assert_prefix_free(cb.codes, &format!("codebook {}", i + 1)); + } + let sf = SCALEFACTOR_CODES; + assert_eq!(sf.len(), 121, "scalefactor codebook entry count"); + let kraft: f64 = sf.iter().map(|(l, _)| 2f64.powi(-i32::from(*l))).sum(); + assert!((kraft - 1.0).abs() < 1e-12, "scalefactor Kraft sum {kraft}"); + assert_prefix_free(sf, "scalefactor codebook"); + } + + fn assert_prefix_free(codes: &[(u8, u32)], what: &str) { + for (i, &(li, ci)) in codes.iter().enumerate() { + for &(lj, cj) in codes.iter().skip(i + 1) { + let short = li.min(lj); + if (ci >> (li - short)) == (cj >> (lj - short)) { + panic!("{what}: {ci:b}/{li} and {cj:b}/{lj} share a prefix"); + } + } + } + } + + /// Band tables must be monotone and land exactly on the window length. + #[test] + fn band_tables_span_the_window() { + for (i, swb) in SWB_LONG.iter().enumerate() { + assert_eq!(swb[0], 0, "long index {i}"); + assert_eq!(*swb.last().unwrap(), 1024, "long index {i}"); + assert!(swb.windows(2).all(|w| w[0] < w[1]), "long index {i}"); + } + for (i, swb) in SWB_SHORT.iter().enumerate() { + assert_eq!(swb[0], 0, "short index {i}"); + assert_eq!(*swb.last().unwrap(), 128, "short index {i}"); + assert!(swb.windows(2).all(|w| w[0] < w[1]), "short index {i}"); + } + } +} diff --git a/crates/ec-aac/tests/it/he_parity.rs b/crates/ec-aac/tests/it/he_parity.rs index 47dd5eff..cc1f543b 100644 --- a/crates/ec-aac/tests/it/he_parity.rs +++ b/crates/ec-aac/tests/it/he_parity.rs @@ -3,8 +3,7 @@ //! Fixtures live in `tests/data/he/` and are generated by `scripts/aac-tables/make-he-fixtures.sh`. //! Each stream is written as ADTS (`.aac`) and MP4 (`.m4a`). The reference is FFmpeg's own decode //! of the `.aac`, stored next to it as signed 16-bit little-endian PCM (`.s16`), in FFmpeg's channel -//! count and order. Nothing here runs FFmpeg, so the test needs neither FFmpeg nor the fdk-aac -//! library. +//! count and order. The FATE files are decoded by FFmpeg at test time. //! //! Acceptance: every channel of every container must correlate with the matching reference channel //! at least `MIN_CORR` at the best lag in `-LAG_MAX..=LAG_MAX`, and the channel count and sample rate @@ -126,6 +125,59 @@ fn best_lag(ours: &[f32], theirs: &[f32]) -> (i64, f64) { (best.0, correlation(a, b)) } +const FATE_FILES: &[&str] = &[ + "open/fate/aac/al_sbr_cm_48_2.mp4", + "open/fate/aac/al_sbr_cm_48_5.1.mp4", + "open/fate/aac/al_sbr_ps_04_new.mp4", + "open/fate/aac/al_sbr_ps_06_new.mp4", + "open/fate/aac/al_sbr_sr_48_2_fsaac48.mp4", +]; + +fn ffmpeg_decode(path: &Path, channels: usize) -> Vec> { + let out = ec_testkit::ffmpeg() + .args(["-v", "error", "-i"]) + .arg(path) + .args(["-map", "0:0", "-f", "f32le", "-acodec", "pcm_f32le", "-"]) + .output() + .expect("ffmpeg runs"); + assert!(out.status.success(), "ffmpeg decode failed: {}", String::from_utf8_lossy(&out.stderr)); + let samples: Vec = out + .stdout + .as_chunks::<4>() + .0 + .iter() + .map(|c| f32::from_le_bytes(*c)) + .collect(); + deinterleave(&samples, channels) +} + +fn check_channels(label: &str, ours: &[Vec], theirs: &[Vec], failures: &mut Vec) { + for ch in 0..ours.len() { + let (lag, corr) = best_lag(&ours[ch], &theirs[ch]); + println!( + "he {label} ch{ch}: ours {} frames, ffmpeg {} frames, lag {lag}, corr {corr:.6}", + ours[ch].len(), + theirs[ch].len() + ); + if corr < MIN_CORR { + failures.push(format!("{label} ch{ch}: corr {corr:.6} < {MIN_CORR}")); + } + } +} + +#[test] +fn fate_sbr_fixtures_match_ffmpeg_per_channel() { + let mut failures = Vec::new(); + for rel in FATE_FILES { + let path = ec_testkit::fixture(rel); + let (channels, _, samples) = decode_m4a(&path); + let ours = deinterleave(&samples, usize::from(channels)); + let theirs = ffmpeg_decode(&path, usize::from(channels)); + check_channels(rel, &ours, &theirs, &mut failures); + } + assert!(failures.is_empty(), "HE-AAC does not match ffmpeg:\n{}", failures.join("\n")); +} + #[test] fn he_fixtures_match_ffmpeg_per_channel() { let mut failures = Vec::new(); @@ -145,17 +197,7 @@ fn he_fixtures_match_ffmpeg_per_channel() { continue; } let ours = deinterleave(&samples, fixture.channels); - for ch in 0..fixture.channels { - let (lag, corr) = best_lag(&ours[ch], &theirs[ch]); - println!( - "he {label} ch{ch}: ours {} frames, ffmpeg {} frames, lag {lag}, corr {corr:.6}", - ours[ch].len(), - theirs[ch].len() - ); - if corr < MIN_CORR { - failures.push(format!("{label} ch{ch}: corr {corr:.6} < {MIN_CORR}")); - } - } + check_channels(&label, &ours, &theirs, &mut failures); } } assert!(failures.is_empty(), "HE-AAC does not match ffmpeg:\n{}", failures.join("\n")); diff --git a/crates/ec-aac/tests/it/main.rs b/crates/ec-aac/tests/it/main.rs index a7758904..43b30aec 100644 --- a/crates/ec-aac/tests/it/main.rs +++ b/crates/ec-aac/tests/it/main.rs @@ -6,8 +6,8 @@ mod common; mod he_parity; mod implicit_ps; mod implicit_sbr; -mod reelh_per_frame; mod multichannel_parity; mod no_panic; mod ps_mp4; +mod oracle; mod ps_oracle; diff --git a/crates/ec-aac/tests/it/multichannel_parity.rs b/crates/ec-aac/tests/it/multichannel_parity.rs index 792b11d3..0e4f6320 100644 --- a/crates/ec-aac/tests/it/multichannel_parity.rs +++ b/crates/ec-aac/tests/it/multichannel_parity.rs @@ -305,17 +305,6 @@ fn check_case(layout: &Layout, encoder: &str, container: Container, failures: &m .map(|(k, row)| format!("{:.6}", row[k])) .collect(); println!("{label} lag {lag:>5} corr [{}]", diag.join(", ")); - if std::env::var_os("EC_AAC_PARITY_DEBUG").is_some() { - let rms = |p: &[f32]| (p.iter().map(|v| f64::from(*v).powi(2)).sum::() / p.len() as f64).sqrt(); - let fmt = |planes: &[Vec]| planes.iter().map(|p| format!("{:.4}", rms(p))).collect::>().join(" "); - println!(" rms ours [{}]", fmt(&ours)); - println!(" rms ffmpeg [{}]", fmt(&theirs)); - for (k, row) in grid.iter().enumerate() { - let cells: Vec = row.iter().map(|c| format!("{c:+.3}")).collect(); - println!(" ours {k}: {}", cells.join(" ")); - } - } - for (k, row) in grid.iter().enumerate() { let own = row[k]; let (best_j, best_c) = row diff --git a/crates/ec-aac/tests/oracle.rs b/crates/ec-aac/tests/it/oracle.rs similarity index 93% rename from crates/ec-aac/tests/oracle.rs rename to crates/ec-aac/tests/it/oracle.rs index 02802b2e..ae0022f6 100644 --- a/crates/ec-aac/tests/oracle.rs +++ b/crates/ec-aac/tests/it/oracle.rs @@ -670,64 +670,6 @@ fn encoder_output_decodes_in_ffmpeg_without_warnings() { } } -/// The derived tables have to be complete prefix codes, and that is checkable -/// without any oracle: a Kraft sum of exactly 1 with the entry count the -/// standard prescribes leaves no room for a transcription slip. -#[test] -fn every_codebook_is_a_complete_prefix_code() { - let expected = [81usize, 81, 81, 81, 81, 81, 64, 64, 169, 169, 289]; - for (i, cb) in ec_aac::tables::CODEBOOKS.iter().enumerate() { - assert_eq!( - cb.codes.len(), - expected[i], - "codebook {} entry count", - i + 1 - ); - let kraft: f64 = cb - .codes - .iter() - .map(|(l, _)| 2f64.powi(-i32::from(*l))) - .sum(); - assert!( - (kraft - 1.0).abs() < 1e-12, - "codebook {} Kraft sum {kraft}", - i + 1 - ); - assert_prefix_free(cb.codes, &format!("codebook {}", i + 1)); - } - let sf = ec_aac::tables::SCALEFACTOR_CODES; - assert_eq!(sf.len(), 121, "scalefactor codebook entry count"); - let kraft: f64 = sf.iter().map(|(l, _)| 2f64.powi(-i32::from(*l))).sum(); - assert!((kraft - 1.0).abs() < 1e-12, "scalefactor Kraft sum {kraft}"); - assert_prefix_free(sf, "scalefactor codebook"); -} - -fn assert_prefix_free(codes: &[(u8, u32)], what: &str) { - for (i, &(li, ci)) in codes.iter().enumerate() { - for &(lj, cj) in codes.iter().skip(i + 1) { - let short = li.min(lj); - if (ci >> (li - short)) == (cj >> (lj - short)) { - panic!("{what}: {ci:b}/{li} and {cj:b}/{lj} share a prefix"); - } - } - } -} - -/// Band tables must be monotone and land exactly on the window length. -#[test] -fn band_tables_span_the_window() { - for (i, swb) in ec_aac::tables::SWB_LONG.iter().enumerate() { - assert_eq!(swb[0], 0, "long index {i}"); - assert_eq!(*swb.last().unwrap(), 1024, "long index {i}"); - assert!(swb.windows(2).all(|w| w[0] < w[1]), "long index {i}"); - } - for (i, swb) in ec_aac::tables::SWB_SHORT.iter().enumerate() { - assert_eq!(swb[0], 0, "short index {i}"); - assert_eq!(*swb.last().unwrap(), 128, "short index {i}"); - assert!(swb.windows(2).all(|w| w[0] < w[1]), "short index {i}"); - } -} - /// The AudioSpecificConfig this crate writes is byte-identical to the one the /// reference muxer puts in an `esds`, which is what "ffmpeg accepts our ASC" /// means in practice. diff --git a/crates/ec-aac/tests/it/reelh_per_frame.rs b/crates/ec-aac/tests/it/reelh_per_frame.rs deleted file mode 100644 index e1c1e9ee..00000000 --- a/crates/ec-aac/tests/it/reelh_per_frame.rs +++ /dev/null @@ -1,151 +0,0 @@ -//! Per-frame corr scan for the coordinator's second repro: a real download -//! whose AAC-LC decode reads clean for the first several seconds then -//! collapses mid-stream, sequentially, with no seek/reset. Finds the first -//! bad frame and its side info, to compare with the frame before. - -use std::fs::File; -use std::io::BufReader; - -use ec_aac::AacDecoder; -use ec_core::{CodecId, Demuxer, Packet}; -use ec_mp4::Mp4Demuxer; - -#[test] -fn reelh_per_frame_scan() { - let path = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"); - let _knob = ec_aac::knobs::set_tool_sideinfo_debug(Some(true)); - - // ffmpeg's channel-0-only decode (pan, not -ac, per the repro). - let out = ec_testkit::ffmpeg() - .args(["-v", "error", "-i"]) - .arg(&path) - .args([ - "-map", "0:a:0", "-af", "pan=1c|c0=c0", "-f", "f32le", "-acodec", "pcm_f32le", "-", - ]) - .output() - .expect("ffmpeg runs"); - assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); - let theirs: Vec = out - .stdout - .as_chunks::<4>().0.iter() - .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])) - .collect(); - - // Our sequential decode: one decoder, no reset, packet 0 onward. - let f = File::open(&path).unwrap(); - let mut d = Mp4Demuxer::new(BufReader::new(f)).unwrap(); - let aac = d - .streams() - .iter() - .find(|s| s.params.codec == CodecId::Aac) - .unwrap(); - let idx = aac.index; - let asc = aac.params.extradata.as_ref().unwrap().to_vec(); - let mut decoder = AacDecoder::with_config_bytes(&asc).unwrap(); - - let mut ours: Vec = Vec::new(); - let mut frame_starts: Vec = Vec::new(); // sample offset each AU's ch0 output starts at - let mut au_idx = 0usize; - loop { - let pkt = match d.next_packet() { - Ok(Packet { stream, data, .. }) if stream == idx => data.to_vec(), - Ok(_) => continue, - Err(_) => break, - }; - frame_starts.push(ours.len()); - match decoder.decode(&pkt, None) { - Ok(frame) => { - let ch = usize::from(frame.channels); - if ch > 0 { - for chunk in frame.samples.chunks_exact(ch) { - ours.push(chunk[0]); - } - } - } - Err(e) => eprintln!("AU {au_idx} decode error: {e:?}"), - } - au_idx += 1; - } - eprintln!("decoded {au_idx} AUs, {} ch0 samples ours, {} ffmpeg", ours.len(), theirs.len()); - - let rows = ec_aac::tool_sideinfo_log(); - - // Per-AU corr at zero lag (both start from packet 0 in decode order -- - // no lag search needed unless the two genuinely desync). - let win = 1024usize; - let mut first_bad: Option = None; - let mut bad_count = 0usize; - let mut pns_frames = 0usize; - for r in &rows { - if r.pns_bands > 0 { - pns_frames += 1; - } - } - eprintln!("frames with pns_bands>0: {pns_frames} / {}", rows.len()); - let full_rms_o: f64 = (ours.iter().map(|v| f64::from(*v).powi(2)).sum::() / ours.len() as f64).sqrt(); - let full_rms_t: f64 = (theirs.iter().map(|v| f64::from(*v).powi(2)).sum::() / theirs.len() as f64).sqrt(); - eprintln!("full-file rms ours={full_rms_o:.6} ffmpeg={full_rms_t:.6}"); - for (i, &start) in frame_starts.iter().enumerate() { - if start + win > ours.len() || start + win > theirs.len() { - break; - } - let a = &ours[start..start + win]; - let b = &theirs[start..start + win]; - let ma = a.iter().sum::() / win as f32; - let mb = b.iter().sum::() / win as f32; - let num: f64 = a.iter().zip(b).map(|(x, y)| f64::from(*x - ma) * f64::from(*y - mb)).sum(); - let da: f64 = a.iter().map(|x| f64::from(*x - ma).powi(2)).sum(); - let db: f64 = b.iter().map(|y| f64::from(*y - mb).powi(2)).sum(); - let corr = if da * db == 0.0 { 1.0 } else { num / (da * db).sqrt() }; - if corr.abs() < 0.9 { - bad_count += 1; - if first_bad.is_none() { - first_bad = Some(i); - } - let rms_o: f64 = (a.iter().map(|v| f64::from(*v).powi(2)).sum::() / win as f64).sqrt(); - let rms_t: f64 = (b.iter().map(|v| f64::from(*v).powi(2)).sum::() / win as f64).sqrt(); - let pns = rows.get(i).map(|r| r.pns_bands).unwrap_or(0); - eprintln!("AU {i}: sample_start={start} corr={corr:.4} rms_o={rms_o:.6} rms_t={rms_t:.6} pns_bands={pns} BAD"); - } else if i % 50 == 0 { - eprintln!("AU {i}: sample_start={start} corr={corr:.4}"); - } - } - - // Dump raw samples around the bad AU for inspection. - if let Some(&start) = frame_starts.get(4) { - eprintln!("--- AU4 samples (start={start}) ---"); - for k in 0..16 { - eprintln!(" k={k} ours={:.6} ffmpeg={:.6}", ours[start + k], theirs[start + k]); - } - let rms_o: f64 = (ours[start..start + win].iter().map(|v| f64::from(*v).powi(2)).sum::() / win as f64).sqrt(); - let rms_t: f64 = (theirs[start..start + win].iter().map(|v| f64::from(*v).powi(2)).sum::() / win as f64).sqrt(); - eprintln!(" rms ours={rms_o:.6} ffmpeg={rms_t:.6}"); - } - - eprintln!("total BAD (|corr|<0.9) AUs: {bad_count} / {}", frame_starts.len()); - // Whole-window (first 10s @ 44.1k = 441000 samples) zero-lag Pearson, - // ours vs ffmpeg -- reproduces (or not) the engine sweep's 0.9840 figure - // from this decoder-level data alone. - let n = 441_000usize.min(ours.len()).min(theirs.len()); - let (a, b) = (&ours[..n], &theirs[..n]); - let ma = a.iter().map(|v| f64::from(*v)).sum::() / n as f64; - let mb = b.iter().map(|v| f64::from(*v)).sum::() / n as f64; - let (mut num, mut da, mut db) = (0.0f64, 0.0f64, 0.0f64); - for (x, y) in a.iter().zip(b) { - let (x, y) = (f64::from(*x) - ma, f64::from(*y) - mb); - num += x * y; - da += x * x; - db += y * y; - } - eprintln!("whole-10s zero-lag Pearson (n={n}): {:.6}", num / (da * db).sqrt()); - if let Some(bad) = first_bad { - eprintln!("=== first bad AU: {bad} ==="); - for i in bad.saturating_sub(2)..=bad + 2 { - if let Some(row) = rows.get(i) { - eprintln!(" AU {i}: {row:?}"); - } - } - } else { - eprintln!("no AU dropped below 0.9 corr"); - } -} diff --git a/crates/ec-aac/tests/sbr_real_library/chunk_a.rs b/crates/ec-aac/tests/sbr_real_library/chunk_a.rs deleted file mode 100644 index 578dcaed..00000000 --- a/crates/ec-aac/tests/sbr_real_library/chunk_a.rs +++ /dev/null @@ -1,1035 +0,0 @@ -//! SBR real-library checks and probes, part A. - -use super::*; - -/// Root-cause triage (round-51): is a real file's SBR-chain break actually -/// downstream of a broken LC core, or does the core decode cleanly and the -/// break lives only in the SBR chain layered on top of it? Prints a -/// core-rate correlation per channel; not asserted on, this is a diagnostic, -/// not a regression gate (the asserted gate is -/// `sbr_real_library_matches_reference`, which already covers the full-band -/// SBR-reconstructed output). -/// -/// Round-61: this printed corr 0.402349 for reeln ch0 while that file's -/// core is provably fine (the asserted reference gate scores its below-5kHz -/// band 0.999988, and `full_chain_low_band_matches_own_core` scores this very -/// core-only PCM 0.998 against our own full chain). The number was the -/// instrument, not the codec: `robust_lag_topk`'s stride-11 coarse grid -/// cannot see a 1-sample-wide correlation peak, so its top-K was ranked from -/// noise. At the true lag -- 481, ffmpeg's resampler delay, the same lag FMJ -/// reads -- these two signals correlate 0.999845. The search here is now -/// stride-1 (`exhaustive_lag_correlation`), and both sides are low-passed at -/// `0.9 * crossover` first, since only the reference side (ffmpeg's full SBR -/// reconstruction, resampled down to the core rate) can hold anything above -/// the crossover at all. -#[test] -pub(super) fn core_only_matches_reference() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - for c in &candidates() { - let (ours, core_rate) = our_decode_core_only(&c.path, c.aac_stream).expect("core decode"); - let theirs = ffmpeg_decode_at_rate(&c.path, c.ffmpeg_stream, ours.len(), core_rate); - println!( - "{} core-only ({} ch, {} Hz):", - c.path.display(), - ours.len(), - core_rate - ); - let cutoff = c.crossover_hz * 0.9; - for (ch, (o, t)) in ours.iter().zip(&theirs).enumerate() { - let ol = lowpass(o, core_rate, cutoff); - let tl = lowpass(t, core_rate, cutoff); - let (mut lag, mut corr, mut at_edge) = - exhaustive_lag_correlation(&ol, &tl, PLAIN_LAG_MAX); - if at_edge { - (lag, corr, at_edge) = exhaustive_lag_correlation(&ol, &tl, SEARCH_LAG_MAX); - } - let edge = if at_edge { - " (AT SEARCH BOUND -- noise, not a measurement)" - } else { - "" - }; - println!(" ch{ch}: below {cutoff:.0}Hz lag {lag}, corr {corr:.6}{edge}"); - } - } -} - -/// Round-51 triage: dumps `kx`/`k2` (the SBR crossover/top band) for the -/// first several frames of every candidate, unconditionally (no ffmpeg -/// needed) -- a wrong `kx` would corrupt bands BELOW the crossover too, not -/// just the HF-generated ones, since `adjust()` gain-scales everything from -/// `kx` up, and `kx` computed too low would pull real core content into -/// that scaling. -#[test] -pub(super) fn dump_sideinfo_kx() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let _knob = ec_aac::knobs::set_sbr_sideinfo_debug(Some(true)); - for c in &candidates() { - let Some((asc, aus)) = extract_aac_track(&c.path, c.aac_stream) else { - continue; - }; - let Ok(mut decoder) = AacDecoder::with_config_bytes(&asc) else { - continue; - }; - let before = ec_aac::sbr_sideinfo_log().len(); - for au in aus.iter().take(50) { - let _ = decoder.decode(au, None); - } - let rows = ec_aac::sbr_sideinfo_log(); - println!("{}:", c.path.display()); - for row in rows[before..].iter().filter(|r| r.ch == 0).take(6) { - println!( - " frame={} source={} kx={} k2={} t_env={:?} f_high={:?}", - row.frame, row.source, row.kx, row.k2, row.t_env, row.f_high - ); - } - } -} - -/// Round-51 triage: FMJ's full-chain best-lag search (`LAG_MAX` = 4000) -/// finds no usable alignment (|corr| ~0.02) even though the core-only -/// decode matches almost exactly -- this widens the search far past -/// `LAG_MAX` to see whether the true alignment just lies outside the normal -/// bound (a large, rate-family-specific extra delay) rather than the SBR -/// content itself being wrong. -#[test] -pub(super) fn wide_lag_search_full_chain() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - for c in &candidates() { - let Some((ours, sbr, rate)) = our_decode(&c.path, c.aac_stream) else { - continue; - }; - if sbr != ec_aac::SbrSupport::V1 { - continue; - } - let theirs = ffmpeg_decode(&c.path, c.ffmpeg_stream, ours.len()); - let o = &ours[0]; - let t = &theirs[0]; - println!( - "{} ({} Hz): ours len={} theirs len={}", - c.path.display(), - rate, - o.len(), - t.len() - ); - const COARSE: usize = 4096; - const WIDE_LAG_MAX: i64 = 200_000; - let mut best = (0i64, -1.0f64); - let mut lag = -WIDE_LAG_MAX; - while lag <= WIDE_LAG_MAX { - let (oa, ob) = if lag >= 0 { - (0usize, lag as usize) - } else { - ((-lag) as usize, 0usize) - }; - if oa + COARSE <= o.len() && ob + COARSE <= t.len() { - let c = correlation(&o[oa..oa + COARSE], &t[ob..ob + COARSE]).abs(); - if c > best.1 { - best = (lag, c); - } - } - lag += 97; // odd stride so it doesn't alias against 2048-sample AU boundaries - } - println!(" widest coarse best: lag={} |corr|={:.4}", best.0, best.1); - } -} - -/// Round-51 triage: compares OUR full-chain (SBR-reconstructed) output's -/// low band directly against OUR OWN core-only decode of the same stream -- -/// no reference decoder involved. `v[0..kx]` in `sbr_chain.rs` is a literal -/// copy of the same core analysis QMF data the core-only path also -/// produces, so if the synthesis/gain-adjust stage is corrupting even that -/// untouched region, this shows a real self-inconsistency with no external -/// factor (alignment against a reference, different tool, etc.) to blame. -#[test] -pub(super) fn full_chain_low_band_matches_own_core() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - for c in &candidates() { - let Some((core, core_rate)) = our_decode_core_only(&c.path, c.aac_stream) else { - continue; - }; - let Some((full, sbr, full_rate)) = our_decode(&c.path, c.aac_stream) else { - continue; - }; - if sbr != ec_aac::SbrSupport::V1 { - continue; - } - println!( - "{} (core {} Hz, full {} Hz):", - c.path.display(), - core_rate, - full_rate - ); - for ch in 0..core.len().min(full.len()) { - // Naive nearest-neighbor 2x upsample of the core so it lines up - // sample-for-sample with the full-chain output's rate -- - // crude, but a low-pass well below the core Nyquist erases the - // stairstep artifacts this introduces. - let up = if full_rate == core_rate { - core[ch].clone() - } else { - upsample_2x(&core[ch]) - }; - let cutoff = f64::from(core_rate) * 0.4; - let keep = if full.len() > core.len() { - mono_prefix(&full) - } else { - full[ch].len() - }; - let ol = lowpass(&full[ch][..keep], full_rate, cutoff); - let ul = lowpass(&up, full_rate, cutoff); - // Escalating search: narrow (`WIDE_LAG_MAX`=20_000, the file's - // original bound) first, widen to `SEARCH_LAG_MAX` only if that - // narrow search hit its own edge (`at_edge`). A single shared - // wide bound was tried first (round-54) and, unconditionally - // widening this comparison, flipped reeln from its correct - // small-lag answer (-576, corr 0.9967) to a wrong, noise-driven - // distant one (-912, corr -0.295) -- a genuinely low-quality - // low-passed signal's coarse correlation surface has enough - // stray peaks over a 100_000-wide range to beat a real but - // modest true peak. Escalating only when the narrow bound - // proves insufficient (FMJ's own core-vs-full delay lands AT - // the 20_000 edge, corr 0.217393 there, the same edge-noise - // class the reference-vs-ours gate had) gets both files right - // without exposing the well-behaved one to the wider bound's - // noise floor. - const WIDE_LAG_MAX: i64 = 20_000; - // Top-K refinement, not the single coarse winner: FMJ's single - // coarse pick landed on an in-bounds noise peak (lag 18995, - // corr 0.10) while its true alignment scores >0.99 on refine. - // A winner at the bound widens once, then is asserted off it. - let (mut lag, mut corr, mut at_edge) = - robust_lag_topk(&ol, &ul, WIDE_LAG_MAX, core_rate, 8); - let mut bound = WIDE_LAG_MAX; - // A sub-bar winner inside the narrow bound is the same symptom - // (true peak outside the search), so it widens too. - if at_edge || corr < 0.99 { - bound = SEARCH_LAG_MAX; - (lag, corr, at_edge) = robust_lag_topk(&ol, &ul, SEARCH_LAG_MAX, core_rate, 8); - } - println!(" ch{ch}: measured best lag {lag}, corr {corr:.6}"); - assert!( - !at_edge, - "{} ch{ch}: winning lag {lag} sits at the +/-{bound} \ - search bound -- corr {corr:.6} is noise, not a measurement", - c.path.display() - ); - assert!( - corr >= 0.99, - "{} ch{ch}: full-chain low band vs our own core-only decode \ - corr {corr:.6} at measured lag {lag} < 0.99 -- the \ - synthesis/gain-adjust stage is corrupting even the untouched \ - low-band region copied straight from core analysis", - c.path.display() - ); - } - } -} - -#[test] -pub(super) fn synthetic_heaac_matrix() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let _knob = ec_aac::knobs::set_sbr_sideinfo_debug(Some(true)); - println!( - "{:>4} {:>4} {:>8} {:>8} {:>8} {:>6} {:>11} {:>8} {:>10} {:>8} {:>9} {:>9}", - "kx", - "k2", - "full", - "below", - "above", - "lag", - "ours_v_core", - "ovc_lag", - "core_v_ref", - "cvr_lag", - "core_rate", - "full_rate" - ); - // Wide enough that the WIDE_LAG_MAX==LC's own decoder priming delay - // (2048) plus a full SBR-chain worth of extra delay (a few thousand - // samples at the doubled/full output rate) both land well inside it, - // not right at the edge the way the plain (LAG_MAX=4000) search did. - const WIDE_LAG_MAX: i64 = 20_000; - for path in fate_aac() { - let before = ec_aac::sbr_sideinfo_log().len(); - let (ours, _sbr, our_rate) = our_decode(&path, 0).expect("decodes"); - let rows = ec_aac::sbr_sideinfo_log(); - let (kx, k2) = rows[before..] - .iter() - .find(|r| r.ch == 0) - .map(|r| (r.kx, r.k2)) - .unwrap_or((-1, -1)); - let theirs = ffmpeg_decode(&path, 0, ours.len()); - let o = &ours[0]; - let t = &theirs[0]; - const TOPK: usize = 5; - let (mut lag, mut full, edge) = robust_lag_topk(o, t, WIDE_LAG_MAX, our_rate, TOPK); - assert!( - !edge, - "{}: HE row's refined winning lag {lag} sits at the \ - +/-{WIDE_LAG_MAX} search's own edge", - path.display() - ); - if ours.len() >= 2 && theirs.len() >= 2 { - let mono_o: Vec = - ours[0].iter().zip(&ours[1]).map(|(a, b)| a + b).collect(); - let mono_t: Vec = theirs[0] - .iter() - .zip(&theirs[1]) - .map(|(a, b)| a + b) - .collect(); - let (mono_lag, _, mono_edge) = - robust_lag_topk(&mono_o, &mono_t, WIDE_LAG_MAX, our_rate, TOPK); - assert!( - !mono_edge, - "{}: HE row's mono-derived refined lag {mono_lag} sits at the \ - +/-{WIDE_LAG_MAX} search's own edge", - path.display() - ); - lag = mono_lag; - full = correlation_at_lag(o, t, lag); - } - let crossover_hz = if kx > 0 { - f64::from(our_rate) * (kx as f64) / 128.0 - } else { - f64::from(our_rate) * 0.25 - }; - let below = correlation_at_lag( - &lowpass(o, our_rate, crossover_hz), - &lowpass(t, our_rate, crossover_hz), - lag, - ); - let (oa, ob) = if lag >= 0 { - (0usize, lag as usize) - } else { - ((-lag) as usize, 0usize) - }; - let n = WINDOW - .min(o.len().saturating_sub(oa)) - .min(t.len().saturating_sub(ob)); - let above = if n >= 1024 { - correlation( - &highpass(&o[oa..oa + n], our_rate, crossover_hz), - &highpass(&t[ob..ob + n], our_rate, crossover_hz), - ) - } else { - 0.0 - }; - let decimate = |s: &[f32], from_rate: u32, to_rate: u32| -> Vec { - let cutoff = f64::from(to_rate) * 0.45; - lowpass(s, from_rate, cutoff) - .iter() - .step_by((from_rate / to_rate.max(1)).max(1) as usize) - .copied() - .collect() - }; - let core_lag_max = WIDE_LAG_MAX / 2; - let ours_v_core = if kx > 0 { - our_decode_core_only(&path, 0).map(|(core, core_rate)| { - let full_at_core_rate = decimate(o, our_rate, core_rate); - best_lag_correlation_wide(&full_at_core_rate, &core[0], core_lag_max) - }) - } else { - None - }; - let (core_v_ref, core_rate_str) = if kx > 0 { - match our_decode_core_only(&path, 0) { - Some((core, core_rate)) => { - let ref_at_core_rate = - ffmpeg_decode_at_rate(&path, 0, core.len(), core_rate); - ( - Some(best_lag_correlation_wide( - &core[0], - &ref_at_core_rate[0], - core_lag_max, - )), - core_rate.to_string(), - ) - } - None => (None, "n/a".into()), - } - } else { - (None, "n/a".into()) - }; - println!( - "{kx:>4} {k2:>4} {full:>8.4} {below:>8.4} {above:>8.4} {lag:>6} {:>11} {:>8} {:>10} {:>8} {:>9} {:>9}", - ours_v_core - .map(|(_, c)| format!("{c:.4}")) - .unwrap_or_else(|| "n/a".into()), - ours_v_core - .map(|(l, _)| l.to_string()) - .unwrap_or_else(|| "n/a".into()), - core_v_ref - .map(|(_, c)| format!("{c:.4}")) - .unwrap_or_else(|| "n/a".into()), - core_v_ref - .map(|(l, _)| l.to_string()) - .unwrap_or_else(|| "n/a".into()), - core_rate_str, - our_rate, - ); - } -} - -pub(super) fn candidates() -> Vec { - fate_aac() - .into_iter() - .map(|path| Candidate { - path, - aac_stream: 0, - ffmpeg_stream: 0, - crossover_hz: 5_000.0, - }) - .collect() -} - -/// The SBR chain against the reference decoder on every real HE-AAC file this -/// checkout can find. Skips loudly (never silently) when ffmpeg or the files -/// are absent. Per channel: full-band correlation must clear 0.999 and the -/// below-crossover (core-decoded) band must clear 0.9999, so a real SBR -/// regression cannot hide behind an average across bands the core never -/// touches. -#[test] -pub(super) fn sbr_real_library_matches_reference() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let list = candidates(); - let mut checked = 0; - let mut worst_full = 1.0f64; - let mut worst_low = 1.0f64; - let mut any_at_edge: Option = None; - for c in &list { - let (ours, sbr, rate) = our_decode(&c.path, c.aac_stream).expect("decode"); - if sbr != ec_aac::SbrSupport::V1 { - continue; - } - let theirs = ffmpeg_decode(&c.path, c.ffmpeg_stream, ours.len()); - println!("{} ({} ch, {} Hz):", c.path.display(), ours.len(), rate); - // Full cross-channel triangle at one fixed lag/window: both channels - // of one CPE are decoded frame-locked and so share the same - // start-of-stream offset, so ch0's own alignment lag is a valid - // anchor for every pair here, letting all five numbers be compared - // directly instead of each being independently (and, before the - // |corr|-max fix above, unreliably) re-searched. - if ours.len() >= 2 && theirs.len() >= 2 { - let (lag0, _) = best_lag_correlation(&ours[0], &theirs[0]); - println!( - " TRIANGLE @ ch0's lag {lag0}: ours0*ref0={:.6} ours0*ref1={:.6} ours1*ref0={:.6} ours1*ref1={:.6} ours0*ours1={:.6}", - correlation_at_lag(&ours[0], &theirs[0], lag0), - correlation_at_lag(&ours[0], &theirs[1], lag0), - correlation_at_lag(&ours[1], &theirs[0], lag0), - correlation_at_lag(&ours[1], &theirs[1], lag0), - correlation_at_lag(&ours[0], &ours[1], 0), - ); - } - for (ch, (o_full, t_full)) in ours.iter().zip(&theirs).enumerate() { - // (Round-22 stability check) EC_AAC_SBR_SWEEP_SEGMENT=1 measures - // the second half of the file instead of the first, so a - // calibration candidate can be checked on two disjoint segments - // rather than just the file's start. - // - // (Round-23) The split point MUST be the same absolute sample - // index on both sides, not each side's own `len() / 2`: `ours` - // is capped at MAX_SAMPLES while `theirs` (the reference) is - // decoded in full, so on a file longer than the cap `o_full` and - // `t_full` have different total lengths and an independent - // `len() / 2` on each picks DIFFERENT seconds of the file -- - // reeln is a 30s file capped to ours=22.7s, so the old code - // compared ours' 11.35s-22.7s against theirs' 15.0s-30.0s, a - // 3.6s-misaligned pair of unrelated passages that reads as a - // catastrophic collapse (~-0.01 corr) with no decode defect - // behind it. Splitting both sides at the shorter side's midpoint - // keeps the two halves pointed at the same audio. - let (o, t): (&[f32], &[f32]) = - if std::env::var("EC_AAC_SBR_SWEEP_SEGMENT").as_deref() == Ok("1") { - let half = o_full.len().min(t_full.len()) / 2; - (&o_full[half..], &t_full[half..]) - } else { - (&o_full[..], &t_full[..]) - }; - let rms = |s: &[f32]| { - (s.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() - / s.len().max(1) as f64) - .sqrt() - }; - eprintln!( - " ch{ch} debug: ours len={} rms={:.4} first10={:?}; theirs len={} rms={:.4} first10={:?}", - o.len(), - rms(o), - &o[..10.min(o.len())], - t.len(), - rms(t), - &t[..10.min(t.len())], - ); - let (lag, full, full_at_edge) = best_lag_correlation_ex(o, t, SEARCH_LAG_MAX); - if full_at_edge { - any_at_edge.get_or_insert_with(|| { - format!( - "{} ch{ch} full-band: winning lag {lag} at +/-{SEARCH_LAG_MAX} search edge", - c.path.display() - ) - }); - } - // Re-slice both signals at the winning lag before splitting into - // bands, so the band correlations are aligned too. - let (oa, ob) = if lag >= 0 { - (0usize, lag as usize) - } else { - ((-lag) as usize, 0usize) - }; - let n = WINDOW - .min(o.len().saturating_sub(oa)) - .min(t.len().saturating_sub(ob)); - // The below-crossover band gets its OWN lag search rather than - // reusing the full-band winning lag: a narrowband, slowly-varying - // low-passed signal has a correlation surface that stays broad - // and flat across many nearby lags, so a full-band search (whose - // winner is decided mostly by the higher-energy/higher-bandwidth - // content) can land on a lag that is a genuine local optimum for - // the whole signal yet a bad one for the low band alone -- on - // reeln ch1, full-band lag 65 vs 64 differ by only 0.0015 in - // full-band correlation but by 0.88 in below-crossover - // correlation once the low band is measured at its own winning - // lag instead. Both bands are still re-anchored at the SAME - // `start` offset internally (see `best_lag_correlation`), so - // this is genuinely comparing the low band against itself, not - // drifting to an unrelated part of the file. - let low = { - let ol_full = lowpass(o, rate, c.crossover_hz); - let tl_full = lowpass(t, rate, c.crossover_hz); - let (low_lag, low_corr, low_at_edge) = - best_lag_correlation_ex(&ol_full, &tl_full, SEARCH_LAG_MAX); - if low_at_edge { - any_at_edge.get_or_insert_with(|| { - format!( - "{} ch{ch} below-crossover: winning lag {low_lag} at +/-{SEARCH_LAG_MAX} search edge", - c.path.display() - ) - }); - } - if low_lag != lag { - eprintln!( - " ch{ch} below-crossover band's own best lag {low_lag} differs from full-band lag {lag}" - ); - } - if std::env::var("EC_AAC_SBR_DRIFT").is_ok() { - let ol_sharp = sharp_lowpass(o, rate, c.crossover_hz); - let tl_sharp = sharp_lowpass(t, rate, c.crossover_hz); - let (_, sharp_corr) = best_lag_correlation(&ol_sharp, &tl_sharp); - println!( - " ch{ch} below-crossover SHARP (3x cascaded) filter: {sharp_corr:.6}" - ); - } - low_corr - }; - let high = if n >= 1024 { - let oh = highpass(&o[oa..oa + n], rate, c.crossover_hz); - let th = highpass(&t[ob..ob + n], rate, c.crossover_hz); - let rms = |v: &[f32]| { - (v.iter().map(|x| f64::from(*x).powi(2)).sum::() / v.len().max(1) as f64) - .sqrt() - }; - println!( - " ch{ch} above-crossover RMS ours/ref = {:.4}", - rms(&oh) / rms(&th).max(1e-12) - ); - correlation(&oh, &th) - } else { - 0.0 - }; - if ch == 0 && std::env::var("EC_AAC_SBR_NOISE_FRACTION_DEBUG").is_ok() { - // (Round-17, Task 1) the noise-energy-fraction ceiling - // hypothesis: predicted per-band correlation ceiling - // sqrt(1 - noise_energy/(signal_energy+noise_energy)), - // from OUR OWN transmitted (dequantized) envelope/noise - // split -- printed once per file so it can be eyeballed - // against per_band_lag_search's measured |corr| curve - // below. Real QMF band width in output Hz is - // `rate/128` (64-band synthesis over the output Nyquist). - let band_hz = f64::from(rate) / 128.0; - println!( - " NOISE-FRACTION ceiling prediction (band_hz, f_noise, predicted_ceiling):" - ); - for (band, signal, noise, out) in ec_aac::noise_fraction_table() { - if signal + noise <= 0.0 { - continue; - } - let f = noise / (signal + noise); - println!( - " {:>6.0}Hz band{band:>3}: signal {signal:.3e} noise {noise:.3e} f_noise {f:.6} ceiling {:.4} realised/target {:.4}", - band as f64 * band_hz, - (1.0 - f).max(0.0).sqrt(), - out / (signal + noise) - ); - } - } - if std::env::var("EC_AAC_SBR_BANDS").is_ok() && n >= 4_096 { - let spectrum = per_band_correlation(&o[oa..oa + n], &t[ob..ob + n], rate); - println!(" ch{ch} per-QMF-band |corr| (band_hz, |corr|, band_index):"); - for (hz, corr, band) in &spectrum { - println!(" {hz:>6.0}Hz band{band:>3.0}: {corr:.4}"); - } - let lags = per_band_lag_search(&o[oa..oa + n], &t[ob..ob + n], rate); - println!(" ch{ch} per-HF-band PCM lag search (band_hz, best_lag, |corr|):"); - for (hz, lag, corr) in &lags { - println!(" {hz:>6.0}Hz: lag {lag:>5} corr {corr:.4}"); - } - let phases = per_band_phase(&o[oa..oa + n], &t[ob..ob + n], rate); - println!( - " ch{ch} per-band cross-spectrum (band_hz, phase_deg, mag_ratio ours/theirs):" - ); - for (hz, phase, ratio) in &phases { - println!( - " {hz:>6.0}Hz: phase {:>7.2}deg ratio {ratio:.4}", - phase.to_degrees() - ); - } - } - if std::env::var("EC_AAC_SBR_HF_CONVICTION").is_ok() && n >= 4_096 { - let band_hz = f64::from(rate) / 256.0; - let kx = (c.crossover_hz / band_hz).round() as usize; - let win = &o[oa..oa + n]; - let twin = &t[ob..ob + n]; - println!( - " ch{ch} TASK1 bandpass-selectivity probe (band_hz, same-band corr, +offset corr, -offset corr):" - ); - for hf_band in [kx + 5, kx + 15, kx + 25] { - let (same, hi_c, lo_c) = bandpass_offset_probe(win, twin, rate, hf_band, 10); - println!( - " HF band{hf_band:>3} ({:.0}Hz) offset=10: same {same:.4} +10band {hi_c:.4} -10band {lo_c:.4}", - hf_band as f64 * band_hz - ); - } - if kx > 6 { - let lf_band = kx.saturating_sub(6); - let (same, hi_c, lo_c) = bandpass_offset_probe(win, twin, rate, lf_band, 3); - println!( - " LF CONTROL band{lf_band:>3} ({:.0}Hz) offset=3: same {same:.4} +3band {hi_c:.4} -3band {lo_c:.4}", - lf_band as f64 * band_hz - ); - } - println!( - " ch{ch} TASK2 bin-level conviction (band_hz, ours-energy, direct, mirror, shift+1band, shift-1band):" - ); - let min_band = (2_500.0 / band_hz).ceil() as usize; - for (hz, energy, direct, mirror, shift_up, shift_down) in - bin_level_conviction(win, twin, rate, min_band, 6) - { - println!( - " {hz:>6.0}Hz: energy {energy:.3e} direct {direct:.4} mirror {mirror:.4} shift+1 {shift_up:.4} shift-1 {shift_down:.4}" - ); - } - } - // (Round-36, Task 2) parity-split coherence: round-35 fixed the - // Synthesis phase-intercept bug only for ODD (target-source) - // patch gaps, at the patch-copy site. On header A (the real - // file's own header) that fix can only be SEEN in the odd-gap - // territory those patches cover (bands 28..42, from patches - // (3,28,11)/(0,39,3)/(13,42,1)); the even-gap patch (0,14,14) - // covers bands 14..27 and was already exact before round-35, so - // it is the control range the fix must NOT move. Reuses - // `bin_level_conviction`'s per-bin direct-coherence measurement - // over every HF band in [14, 43) (min_band=14, top_n wide enough - // to keep every band, not just the top-energy handful), then - // averages `direct` within each region separately. - if std::env::var("EC_AAC_SBR_PARITY_SPLIT").is_ok() && n >= 4_096 { - let band_hz = f64::from(rate) / 256.0; - let win = &o[oa..oa + n]; - let twin = &t[ob..ob + n]; - let rows = bin_level_conviction(win, twin, rate, 14, 29); - let mean_in = |lo: usize, hi: usize| { - let vals: Vec = rows - .iter() - .filter(|(hz, ..)| { - let band = (hz / band_hz).round() as usize; - band >= lo && band < hi - }) - .map(|(_, _, direct, ..)| *direct) - .collect(); - if vals.is_empty() { - (0.0, 0) - } else { - (vals.iter().sum::() / vals.len() as f64, vals.len()) - } - }; - let (even_mean, even_n) = mean_in(14, 28); - let (odd_mean, odd_n) = mean_in(28, 43); - println!( - " ch{ch} PARITY-SPLIT direct-coherence mean: even-gap[14,28)={even_mean:.4} (n={even_n}) odd-gap[28,43)={odd_mean:.4} (n={odd_n})" - ); - } - // (Round-38, Task 1) HF-only constant-lag sweep: see - // `hf_lag_sweep`'s doc comment. Reuses this window's already - // globally-aligned `oa`/`ob`/`n`; only `o`'s STFT read position is - // shifted, `n_slot`*64 samples at a time. - if std::env::var("EC_AAC_SBR_HF_LAG_SWEEP").is_ok() && n >= 4_096 { - let band_hz = f64::from(rate) / 256.0; - let min_band = (2_500.0 / band_hz).ceil() as usize; - let sweep = hf_lag_sweep(o, t, oa, ob, n, min_band); - println!( - " ch{ch} TASK1 HF-only lag sweep (k = QMF slots of 64 samples, mean direct HF-bin coherence):" - ); - for (k, coh) in &sweep { - println!(" k={k:>3}: coherence {coh:.4}"); - } - if let Some((peak_k, peak_c)) = - sweep.iter().max_by(|a, b| a.1.partial_cmp(&b.1).unwrap()) - { - println!(" ch{ch} HF lag sweep peak: k={peak_k} coherence {peak_c:.4}"); - } - } - // (Round-40, Task 1) fine spectral-bin shift scan -- see - // `hf_bin_shift_sweep`'s doc comment. - if std::env::var("EC_AAC_SBR_BIN_SHIFT").is_ok() && n >= 4_096 { - let win = &o[oa..oa + n]; - let twin = &t[ob..ob + n]; - for (region_name, lo, hi) in - [("even-gap[14,28)", 14, 28), ("odd-gap[28,43)", 28, 43)] - { - let sweep = hf_bin_shift_sweep(win, twin, rate, lo, hi); - println!( - " ch{ch} TASK1 BIN-SHIFT {region_name} (b = FFT bins, mean direct HF-bin coherence):" - ); - for (b, coh) in &sweep { - println!(" b={b:>3}: coherence {coh:.4}"); - } - if let Some((peak_b, peak_c)) = - sweep.iter().max_by(|a, b| a.1.partial_cmp(&b.1).unwrap()) - { - println!( - " ch{ch} {region_name} bin-shift peak: b={peak_b} coherence {peak_c:.4}" - ); - } - } - } - // (Round-40, Task 2) complex-gain fingerprint -- see - // `hf_complex_ratio_trajectory`'s doc comment. - if std::env::var("EC_AAC_SBR_COMPLEX_GAIN").is_ok() && n >= 4_096 { - let band_hz = f64::from(rate) / 256.0; - let min_band = (2_500.0 / band_hz).ceil() as usize; - let win = &o[oa..oa + n]; - let twin = &t[ob..ob + n]; - for (hz, traj) in hf_complex_ratio_trajectory(win, twin, rate, min_band, 4) { - println!( - " ch{ch} TASK2 COMPLEX-GAIN {hz:.0}Hz dominant-bin O/T trajectory (frame: mag, phase_deg, dphase_deg):" - ); - let mut prev_phase: Option = None; - for (h, (mag, phase)) in traj.iter().enumerate() { - let dphase = prev_phase.map(|p| { - let mut d = phase.to_degrees() - p; - while d > 180.0 { - d -= 360.0; - } - while d < -180.0 { - d += 360.0; - } - d - }); - prev_phase = Some(phase.to_degrees()); - match dphase { - Some(d) => println!( - " frame{h:>3}: mag {mag:.4} phase {:>7.2}deg dphase {d:>7.2}deg", - phase.to_degrees() - ), - None => println!( - " frame{h:>3}: mag {mag:.4} phase {:>7.2}deg dphase n/a", - phase.to_degrees() - ), - } - } - } - } - // (Round-45, Task 3) THIRD WITNESS: a reference-free plain - // patch+gain simulator built from the reference's own low band -- - // see `hf_patch_simulator`'s doc comment. - if std::env::var("EC_AAC_SBR_SIMULATOR").is_ok() && n >= 4_096 { - let win = &o[oa..oa + n]; - let twin = &t[ob..ob + n]; - let (vs_ref, vs_ours) = hf_patch_simulator(win, twin, rate); - let mean_in = |rows: &[(usize, f64)], lo: usize, hi: usize| { - let vals: Vec = rows - .iter() - .filter(|(b, _)| *b >= lo && *b < hi) - .map(|(_, c)| *c) - .collect(); - if vals.is_empty() { - 0.0 - } else { - vals.iter().sum::() / vals.len() as f64 - } - }; - println!(" ch{ch} SIMULATOR sim-vs-REFERENCE per-band coherence (band, coh):"); - for (b, coh) in &vs_ref { - println!(" band{b:>3}: {coh:.4}"); - } - println!( - " ch{ch} SIMULATOR sim-vs-REFERENCE mean: even-gap[14,28)={:.4} odd-gap[28,43)={:.4}", - mean_in(&vs_ref, 14, 28), - mean_in(&vs_ref, 28, 43) - ); - println!(" ch{ch} SIMULATOR sim-vs-OURS per-band coherence (band, coh):"); - for (b, coh) in &vs_ours { - println!(" band{b:>3}: {coh:.4}"); - } - println!( - " ch{ch} SIMULATOR sim-vs-OURS mean: even-gap[14,28)={:.4} odd-gap[28,43)={:.4}", - mean_in(&vs_ours, 14, 28), - mean_in(&vs_ours, 28, 43) - ); - } - // (Round-46, Task 1) QMF-domain exact third witness -- see - // `hf_patch_simulator_qmf`'s doc comment. - if std::env::var("EC_AAC_SBR_QMF_WITNESS").is_ok() && n >= 4_096 { - let win = &o[oa..oa + n]; - let twin = &t[ob..ob + n]; - let (vs_ref, vs_ours, ours_vs_ref) = hf_patch_simulator_qmf(win, twin); - // Per-QMF-band energy ratio and coherence, ours vs reference - // (the envelope-adjuster instrument: a level offset shows as - // a constant dB ratio, a gain-shape defect as per-band dB). - { - let (o_spec, t_spec, _) = qmf_domain_spec(win, twin); - println!(" ch{ch} QMF-BAND ours-vs-REFERENCE (band, ratio_dB, coherence):"); - for band in 0..o_spec.len().min(t_spec.len()).min(48) { - let (mut eo, mut et, mut cr, mut ci) = (0.0f64, 0.0f64, 0.0f64, 0.0f64); - for (a, b) in o_spec[band].iter().zip(&t_spec[band]) { - eo += a.norm_sqr(); - et += b.norm_sqr(); - let c = *a * b.conj(); - cr += c.re; - ci += c.im; - } - let coh = (cr * cr + ci * ci).sqrt() / (eo * et).sqrt().max(1e-30); - println!( - " band{band:>3}: {:>7.2} dB coh {coh:.4}", - 10.0 * (eo / et.max(1e-30)).log10() - ); - } - } - let mean_in = |rows: &[(usize, f64)], lo: usize, hi: usize| { - let vals: Vec = rows - .iter() - .filter(|(b, _)| *b >= lo && *b < hi) - .map(|(_, c)| *c) - .collect(); - if vals.is_empty() { - 0.0 - } else { - vals.iter().sum::() / vals.len() as f64 - } - }; - println!(" ch{ch} QMF-WITNESS sim-vs-REFERENCE per-band coherence (band, coh):"); - for (b, coh) in &vs_ref { - println!(" band{b:>3}: {coh:.4}"); - } - println!( - " ch{ch} QMF-WITNESS sim-vs-REFERENCE mean: even-gap[14,28)={:.4} odd-gap[28,43)={:.4}", - mean_in(&vs_ref, 14, 28), - mean_in(&vs_ref, 28, 43) - ); - println!(" ch{ch} QMF-WITNESS sim-vs-OURS per-band coherence (band, coh):"); - for (b, coh) in &vs_ours { - println!(" band{b:>3}: {coh:.4}"); - } - println!( - " ch{ch} QMF-WITNESS sim-vs-OURS mean: even-gap[14,28)={:.4} odd-gap[28,43)={:.4}", - mean_in(&vs_ours, 14, 28), - mean_in(&vs_ours, 28, 43) - ); - // (Round-48, Task 4a) the ultimate content-match witness: - // our HF QMF content directly against the reference's, no - // simulator step in between. - println!(" ch{ch} QMF-WITNESS ours-vs-REFERENCE per-band coherence (band, coh):"); - for (b, coh) in &ours_vs_ref { - println!(" band{b:>3}: {coh:.4}"); - } - println!( - " ch{ch} QMF-WITNESS ours-vs-REFERENCE mean: even-gap[14,28)={:.4} odd-gap[28,43)={:.4}", - mean_in(&ours_vs_ref, 14, 28), - mean_in(&ours_vs_ref, 28, 43) - ); - // (Round-46, Task 2) fitted sim-to-reference transfer, one - // representative band per patch plus a spread across the - // strong odd-gap patch (3,28,11) where the QMF witness shows - // sim-vs-reference clearing 0.5. - let fit_bands = [14, 18, 22, 26, 28, 30, 32, 34, 36, 38, 39, 42]; - let fits_ref = hf_patch_transfer_fit(win, twin, &fit_bands, false); - println!(" ch{ch} TASK2 FIT ref[l]~=a0*sim[l]+a1*sim[l-1]+a2*sim[l-2]:"); - for (b, a) in &fits_ref { - println!( - " band{b:>3}: a0=({:>7.3},{:>7.3}) a1=({:>7.3},{:>7.3}) a2=({:>7.3},{:>7.3})", - a[0].re, a[0].im, a[1].re, a[1].im, a[2].re, a[2].im - ); - } - // (Round-48, Task 2) same fit, target = ours: side by side - // with the above shows exactly what our transform does to - // the plain copy vs what the reference's does. - let fits_ours = hf_patch_transfer_fit(win, twin, &fit_bands, true); - println!(" ch{ch} TASK2 FIT ours[l]~=a0*sim[l]+a1*sim[l-1]+a2*sim[l-2]:"); - for (b, a) in &fits_ours { - println!( - " band{b:>3}: a0=({:>7.3},{:>7.3}) a1=({:>7.3},{:>7.3}) a2=({:>7.3},{:>7.3})", - a[0].re, a[0].im, a[1].re, a[1].im, a[2].re, a[2].im - ); - } - } - println!( - " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}", - c.crossover_hz, c.crossover_hz - ); - if std::env::var("EC_AAC_SBR_DRIFT").is_ok() && n >= 4_096 { - let windows = (n / 4_096).min(20); - let drift = windowed_lag_drift(o, t, oa, ob, windows); - println!(" ch{ch} DRIFT (window_idx, local_lag, |corr|):"); - for (w, (l, cr)) in drift.iter().enumerate() { - println!(" w{w}: lag {l} corr {cr:.4}"); - } - } - // (Round-23, Task 1/3) whole-file corr-vs-time timeline: the - // DRIFT diagnostic above caps at 20 windows (~1.85s) starting at - // the full-band lag offset near sample 0, so it never sees a - // collapse that only appears later in the file. This walks - // every 4096-sample window from that same start to EOF on BOTH - // sides, with its own local lag search per window, so a - // mid-file onset shows up as a specific window index/timestamp - // instead of being averaged away inside one giant WINDOW-sized - // correlation. The mean/min across every window is also the - // TRUE whole-file correlation number (`best_lag_correlation` - // above only ever measures one WINDOW=200_000-sample slice - // starting near the file's first quarter): unlike that one - // fixed-lag figure, this one is immune to a single global lag - // being slightly wrong for a later part of the file. - { - let avail = o.len().saturating_sub(oa).min(t.len().saturating_sub(ob)); - let windows = avail / 4_096; - // The full per-window LOCAL_LAG=+-2000 search inside - // `windowed_lag_drift` costs O(windows * 4001 * 4096) and runs - // ~4min/channel over a whole real-library file -- that walk - // is only needed when actually hunting for a drifting lag - // (EC_AAC_SBR_TIMELINE). By default this block instead reuses - // the already-established global `lag` with NO per-window - // search (O(windows * 4096)), which is cheap and still gives - // an honest whole-file windowed mean/min at that fixed lag. - let timeline: Vec<(i64, f64)> = if std::env::var("EC_AAC_SBR_TIMELINE").is_ok() { - windowed_lag_drift(o, t, oa, ob, windows) - } else { - (0..windows) - .filter_map(|w| { - // `oa`/`ob` (computed above from `lag`) already encode the - // alignment shift via their differing base offsets -- do NOT - // add `lag` again here, that double-applies it and shifts - // every window an extra `lag` samples out of alignment - // (this collapsed the whole-file mean from ~0.96 to ~0.14 - // while leaving the single-WINDOW `full`/`low` numbers, - // which don't go through this path, unaffected). - let base_o = oa + w * 4_096; - let base_t = ob + w * 4_096; - if base_o + 4_096 > o.len() || base_t + 4_096 > t.len() { - return None; - } - Some(( - lag, - correlation(&o[base_o..base_o + 4_096], &t[base_t..base_t + 4_096]), - )) - }) - .collect() - }; - if std::env::var("EC_AAC_SBR_TIMELINE").is_ok() { - println!( - " ch{ch} TIMELINE ({windows} windows, {rate} Hz, window_idx@sec, local_lag, corr):" - ); - for (w, (l, cr)) in timeline.iter().enumerate() { - let t_sec = (oa + w * 4_096) as f64 / f64::from(rate); - println!(" w{w}@{t_sec:.2}s: lag {l} corr {cr:.4}"); - } - } - if !timeline.is_empty() { - let mean = timeline.iter().map(|(_, c)| c).sum::() / timeline.len() as f64; - let min = timeline.iter().map(|(_, c)| *c).fold(f64::MAX, f64::min); - println!( - " ch{ch} WHOLE-FILE windowed corr: mean {mean:.6} min {min:.6} over {} windows ({:.2}s)", - timeline.len(), - windows as f64 * 4_096.0 / f64::from(rate) - ); - } - } - if std::env::var("EC_AAC_SBR_RESIDUAL_DEBUG").is_ok() { - residual_sideinfo_analysis(o, t, oa, ob, rate, ch); - } - worst_full = worst_full.min(full); - worst_low = worst_low.min(low); - } - checked += 1; - } - assert!(checked > 0, "no plain-SBR real files decoded"); - assert!( - any_at_edge.is_none(), - "lag search hit its bound: {} -- the reported correlations are noise \ - at the search edge, not a measurement", - any_at_edge.unwrap_or_default() - ); - assert!( - worst_full >= 0.999, - "worst full-band correlation {worst_full:.6} < 0.999 bar" - ); - assert!( - worst_low >= 0.9999, - "worst below-crossover correlation {worst_low:.6} < 0.9999 -- the core decode itself regressed" - ); -} - -/// Writes `aus` (raw access units, no ADTS framing) plus `asc` (their own -/// AudioSpecificConfig bytes, unmodified) into a fresh mp4 at `out_path`, so -/// the reference decoder can be pointed at container-native SBR content that -/// is byte-identical to the original stream's access units -- the same -/// `esds`-carries-an-explicit-SBR-config shape `examples/wrap_sbr.rs` proved -/// on synthetic content in round-31, reused here on a REAL file's own AUs as -/// the round-41 Task 1 remux control. -pub(super) fn write_remux(out_path: &Path, asc: &[u8], aus: &[Vec]) { - let cfg = ec_aac::parse_audio_specific_config(asc).expect("real file's own ASC parses"); - let core_rate = cfg.sample_rate; - let time_base = ec_core::TimeBase::from_rate(core_rate); - let layout = if cfg.channels == 1 { - ec_core::ChannelLayout::Mono - } else { - ec_core::ChannelLayout::Stereo - }; - let mut params = ec_core::CodecParameters::new(CodecId::Aac); - params.extradata = Some(asc.to_vec().into()); - params.media = ec_core::MediaParameters::Audio(ec_core::AudioParameters { - sample_rate: core_rate, - layout, - format: None, - bits_per_sample: None, - }); - let mut info = ec_core::StreamInfo::new(0, time_base, params); - info.default = true; - let out = File::create(out_path).expect("remux output creatable"); - let mut muxer = ec_mp4::Mp4Muxer::new(out).expect("mp4 muxer opens"); - use ec_core::Muxer as _; - muxer.add_stream(info).expect("stream declared"); - for (i, au) in aus.iter().enumerate() { - let pts = i as i64 * 1024; - let packet = Packet::new(0, time_base, au.as_slice()) - .with_pts(pts) - .with_duration(1024); - muxer.write_packet(&packet).expect("packet written"); - } - muxer.finish().expect("finished"); -} - -/// Mean/min of `bin_level_conviction`'s `direct` column, printed as one -/// summary line per control pair. -pub(super) fn report_conviction(label: &str, rows: &[(f64, f64, f64, f64, f64, f64)]) { - if rows.is_empty() { - println!(" {label}: no HF bands measured (window too short)"); - return; - } - let vals: Vec = rows.iter().map(|(_, _, direct, ..)| *direct).collect(); - let mean = vals.iter().sum::() / vals.len() as f64; - let min = vals.iter().cloned().fold(f64::MAX, f64::min); - println!( - " {label}: direct-coherence mean {mean:.4} min {min:.4} over {} HF bands", - vals.len() - ); - for (hz, _, direct, ..) in rows { - println!(" {hz:>6.0}Hz: direct {direct:.4}"); - } -} diff --git a/crates/ec-aac/tests/sbr_real_library/chunk_b.rs b/crates/ec-aac/tests/sbr_real_library/chunk_b.rs deleted file mode 100644 index f18016b8..00000000 --- a/crates/ec-aac/tests/sbr_real_library/chunk_b.rs +++ /dev/null @@ -1,674 +0,0 @@ -//! SBR real-library checks and probes, part B. - -use super::*; - -/// (Round-41, Task 1) Instrument anchor: before trusting `bin_level_conviction`'s -/// low (~0.15-0.25) HF direct-coherence numbers on reeln as evidence the -/// HF content is genuinely different, confirm the measurement itself reads -/// near 1.0 on pairs that are KNOWN to carry the same content. Three -/// controls: -/// A. ours vs ours, decoded independently twice -- a determinism check on -/// our own decode AND the coherence arithmetic's floor. -/// B. the reference's own PCM from the original file vs from a remux of -/// the SAME access units/ASC through a fresh container -- near-1.0 -/// expected; anything else would implicate the remux path, not SBR. -/// C. the reference's own PCM vs itself read 64 samples later (one QMF -/// slot) -- sanity that `bin_level_conviction`'s STFT hop/window does -/// not itself destroy coherence for equivalent content at a fixed -/// sample offset, the same shape of offset the real `oa`/`ob` alignment -/// applies above. -/// If any control reads below ~0.9 on the HF bands, the instrument -- not -/// the SBR chain -- is the story; every prior round's coherence number would -/// need to be recalibrated against whatever this control actually measures. -#[test] -pub(super) fn sbr_instrument_anchor_controls() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let path = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"); - const MIN_BAND: usize = 14; - const TOP_N: usize = 128; // larger than any real band count -- keeps every HF band - - // Control A: ours vs ours, decoded independently twice. - let (ours_a, _, rate) = our_decode(&path, 0).expect("our_decode works"); - let (ours_b, _, _) = our_decode(&path, 0).expect("our_decode works (2nd pass)"); - let n = ours_a[0].len().min(ours_b[0].len()).min(MAX_SAMPLES); - let rows = bin_level_conviction(&ours_a[0][..n], &ours_b[0][..n], rate, MIN_BAND, TOP_N); - report_conviction("A: ours-vs-ours (determinism)", &rows); - - // Control B: reference PCM from the original file vs from a remux of the - // SAME access units/ASC. - let (asc, aus) = extract_aac_track(&path, 0).expect("track extracts"); - let remux_path = std::env::temp_dir().join("ec_aac_sbr_instrument_anchor_remux.mp4"); - write_remux(&remux_path, &asc, &aus); - let theirs_orig = ffmpeg_decode(&path, 0, 2); - let theirs_remux = ffmpeg_decode(&remux_path, 0, 2); - let _ = std::fs::remove_file(&remux_path); - let n = theirs_orig[0] - .len() - .min(theirs_remux[0].len()) - .min(MAX_SAMPLES); - let (lag_b, _) = best_lag_correlation(&theirs_orig[0][..n], &theirs_remux[0][..n]); - let (oa, ob) = if lag_b >= 0 { - (0usize, lag_b as usize) - } else { - ((-lag_b) as usize, 0usize) - }; - let win = n.saturating_sub(oa.max(ob)); - let rows = bin_level_conviction( - &theirs_orig[0][oa..oa + win], - &theirs_remux[0][ob..ob + win], - rate, - MIN_BAND, - TOP_N, - ); - report_conviction( - &format!("B: reference-vs-reference-through-remux (lag {lag_b})"), - &rows, - ); - - // Control C: reference PCM vs itself read 64 samples later (one QMF - // slot) -- both slices are the SAME underlying samples, just offset. - let t = &theirs_orig[0][..MAX_SAMPLES.min(theirs_orig[0].len())]; - if t.len() > 64 { - let rows = bin_level_conviction(&t[64..], &t[..t.len() - 64], rate, MIN_BAND, TOP_N); - report_conviction("C: reference vs reference, 64-sample offset", &rows); - } -} - -/// (Round-41, Task 2) Actual injected HF noise energy, measured as OUTPUT -/// energy rather than trusted from either side of round-24's contradiction: -/// `noise_fraction_table` bookkept a 0.37-0.39 noise share from the -/// transmitted envelope/noise split, while separately zeroing the injected -/// noise (`EC_AAC_SBR_NOISE_ZERO`, which only skips the PCM addition in -/// `sbr_env::adjust`, not the bookkeeping) was found to change full-band -/// correlation by <0.001 -- both cannot be true of the same signal. This -/// decodes reeln twice (noise on, then off) and reads the per-HF-band -/// `bin_level_conviction` energy delta between them as the fraction noise -/// ACTUALLY realizes in the output, independent of either prior claim. -#[test] -pub(super) fn sbr_actual_noise_fraction() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let path = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"); - const MIN_BAND: usize = 14; - const TOP_N: usize = 128; - - let (ours_on, _, rate) = { - let _zero = ec_aac::knobs::set_sbr_noise_zero(Some(false)); - let _fraction = ec_aac::knobs::set_sbr_noise_fraction(Some(true)); - our_decode(&path, 0).expect("our_decode works (noise on)") - }; - let bookkept = ec_aac::noise_fraction_table(); - let (ours_off, _, _) = { - let _zero = ec_aac::knobs::set_sbr_noise_zero(Some(true)); - let _fraction = ec_aac::knobs::set_sbr_noise_fraction(Some(false)); - our_decode(&path, 0).expect("our_decode works (noise zeroed)") - }; - - let theirs = ffmpeg_decode(&path, 0, ours_on.len()); - let n = ours_on[0].len().min(ours_off[0].len()).min(MAX_SAMPLES); - let on = &ours_on[0][..n]; - let off = &ours_off[0][..n]; - - // Per-band actual energy delta: `energy` in `bin_level_conviction`'s row - // is always the FIRST argument's own band energy, so swap argument order - // between the two calls to read both sides' energies at the SAME band - // set (TOP_N kept above any real band count so both calls select every - // band, not just each side's own top few). - let rows_on = bin_level_conviction(on, off, rate, MIN_BAND, TOP_N); - let rows_off = bin_level_conviction(off, on, rate, MIN_BAND, TOP_N); - // (Round-42, Task 2) This `band_hz` feeds the `band` used below to look - // up `bookkept` (`noise_fraction_table()`'s QMF band index, `tables.kx` - // .. `tables.k2`, width `rate/128` -- 64 QMF bands over the SBR - // Nyquist, per the NOISE-FRACTION-DEBUG dump above). It is NOT - // `bin_level_conviction`'s own internal FFT-band scale (`rate/256`, - // used correctly elsewhere in this file for THAT function's own rows). - // Using `rate/256` here silently doubled every looked-up `band` index, - // so every real band above roughly half of `k2` missed the table and - // read back a false 0.0 bookkept fraction -- a test-side lookup bug, - // not a decoder coverage gap (verified: `noise_fraction_table()` itself - // has nonzero entries for QMF bands up to `k2`). - let band_hz = f64::from(rate) / 128.0; - let kx_band = bookkept.iter().map(|(b, ..)| *b).min().unwrap_or(0); - println!( - " (crossover: QMF band {kx_band}, {:.0} Hz -- rows below it are core-decoded and excluded from the total)", - kx_band as f64 * band_hz - ); - println!( - " ACTUAL noise fraction (band_hz, energy_on, energy_off, actual_fraction, bookkept_fraction):" - ); - let mut sum_on = 0.0f64; - let mut sum_delta = 0.0f64; - for (hz, e_on, ..) in &rows_on { - let e_off = rows_off - .iter() - .find(|(hz2, ..)| (hz2 - hz).abs() < 1.0) - .map(|(_, e, ..)| *e) - .unwrap_or(0.0); - let actual = if *e_on > 0.0 { - (e_on - e_off) / e_on - } else { - 0.0 - }; - let band = (hz / band_hz).round() as usize; - let book = bookkept - .iter() - .find(|(b, ..)| *b == band) - .map(|(_, s, n, _)| if s + n > 0.0 { n / (s + n) } else { 0.0 }) - .unwrap_or(0.0); - println!( - " {hz:>6.0}Hz: on {e_on:.3e} off {e_off:.3e} actual {actual:.4} bookkept {book:.4}" - ); - // HF ONLY (round-61): `MIN_BAND` (14, ~2412 Hz at 44.1k) sits BELOW - // this stream's crossover (~4500 Hz), so the row list opens with a - // dozen core-decoded bands that carry an order of magnitude more - // energy than the SBR-generated ones (6.4e4 vs ~5e3) and, by - // definition, zero injected noise. Summing them into a "whole-HF" - // ratio dilutes it toward zero -- that is how this metric read - // 0.0397 while every band above the crossover was realizing - // 0.15-0.27. The crossover comes from the bookkeeping table itself - // (its lowest QMF band IS `kx`), so it tracks the stream instead of - // a constant that a QMF-mapping change can invalidate. - if band < kx_band { - continue; - } - sum_on += e_on; - sum_delta += e_on - e_off; - } - let actual_total = if sum_on > 0.0 { - sum_delta / sum_on - } else { - 0.0 - }; - println!(" ACTUAL whole-HF noise fraction: {actual_total:.4}"); - - // Corr re-verification: does zeroing the injected noise move full-band - // correlation against the reference, and by how much (round-24 claimed - // <0.001). - let (_, corr_on) = best_lag_correlation(on, &theirs[0][..theirs[0].len().min(MAX_SAMPLES)]); - let (_, corr_off) = best_lag_correlation(off, &theirs[0][..theirs[0].len().min(MAX_SAMPLES)]); - println!( - " full-band corr: noise-on {corr_on:.6} noise-off {corr_off:.6} delta {:.6}", - corr_off - corr_on - ); - - // (Round-42, Task 1/3 acceptance) Two real accounting bugs fixed this - // round pushed the realized whole-HF fraction 0.1325 -> 0.1787 (reeln, - // this same measurement): the boost pass was conserving the cell's FULL - // envelope target against a signal-only realized sum (now conserves - // total-vs-total, matching the reference algorithm's known - // E_orig/E_curr+Q_M boost ratio), and `noise_amps` divided an - // already-per-sample `noise_here` by the cell's QMF-band width a SECOND - // time (removed) -- worse at wide high-frequency sfb cells, which is - // exactly where the pre-fix gap was largest. - // - // The charter's ±0.05-of-bookkept (~0.38) target is NOT met by this - // fix alone (round-43 investigated why, with `EC_AAC_SBR_CELLDUMP` in - // `sbr_env::adjust`): `sbr_env::adjust`'s per-cell signal/noise split - // is NOT the culprit -- corrected-band dumps (the charter's quoted - // "6718/6891/7063Hz" and "8.5-13kHz decaying" bands turned out to be - // mislabeled by a factor of two, this file's true SBR band spacing is - // `rate/128` = 344.5Hz not the 172.3Hz FFT-analysis spacing those Hz - // labels come from) show `adjust` injecting 40-85% noise share in the - // QMF domain, at or ABOVE the ~0.37 bookkept split, not starved. - // - // (Round-44) Round-43's "essentially 0% of injected energy lands back - // in the source subband" reading of - // `synthesis_energy_gain_for_white_noise_excitation` was itself a test - // bug, not a `Synthesis` defect: that harness measured its in-band FFT - // window at `k0/SYNTHESIS_BANDS`, but band k's passband is centred at - // `(k+0.5)*omega_step` with `omega_step = pi/(2*SYNTHESIS_BANDS)` -- - // HALF that fraction of Nyquist -- so the window sat on the image - // region, not the signal. Corrected, a raw i.i.d.-per-slot draw already - // lands ~0.49 of its own energy in its home subband's own frequency - // range (not 0%), and a short slot-domain boxcar (`sbr_env::NoiseGen`'s - // new `LOWPASS_TAPS = 2`) raises that to ~0.61 while staying spread - // across the band (not collapsing to a residual tone) -- see the - // harness's `synthesis_energy_gain_for_lowpassed_noise_excitation`. - // That shape fix is real (an i.i.d. draw genuinely was less - // band-faithful than it needed to be, and now is not), but re-measuring - // THIS whole-HF metric after shipping it moved `actual_total` by <0.002 - // (0.1787 -> 0.1770, i.e. flat) -- because this metric sums the noise - // energy DELTA across the whole swept HF spectrum, and energy an - // i.i.d. draw aliased to a NEIGHBOURING QMF band still lands somewhere - // inside that same swept HF range, so it was never actually excluded - // from this particular sum. The ~0.38 bookkept vs ~0.18 actual gap is - // therefore NOT a spectral-shape/aliasing defect (refuting round-43's - // framing) -- it is a genuine energy shortfall, and per-band coherence - // sits well below even the ACTUAL-fraction ceiling - // (`sqrt(1 - f)`; ~0.15-0.20 measured vs. ~0.90 predicted at - // `f ~= 0.177`, see `EC_AAC_SBR_PARITY_SPLIT`), meaning most of the - // remaining HF correlation shortfall is a content defect independent of - // noise level. Deferred: locating that energy shortfall and the - // content defect, budget did not reach either this round (open, see - // ledger). Floor tightened from round-42's 0.15 to the level this - // fix's shape change actually lands at (0.177, both before and after), - // with margin for run-to-run FFT/decode jitter. - assert!( - actual_total > 0.17, - "whole-HF realized noise fraction regressed to {actual_total:.4} \ - (round-44 fix floor is 0.17; measured 0.1770 post-shape-fix, \ - 0.1787 pre-fix -- shape does not move this metric, see doc above)" - ); -} - -/// Discriminator instrument: one row per stream of the SBR header/frame -/// features actually parsed from its first ~20 `sbr_data()` frames, next to -/// that stream's own pass/fail correlation vs the reference decoder -- -/// looking for which feature(s) separate the two known-PASS streams -/// (reeln, synthetic 48k HE) from the two known-FAIL families (FMJ, -/// synthetic 44.1k HE). Gated on the real files / fixture directory being -/// present, same as the tests it borrows machinery from; skips loudly, -/// generates nothing. -#[test] -pub(super) fn sbr_header_feature_table() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - struct Row { - label: String, - path: PathBuf, - aac_stream: usize, - ffmpeg_stream: usize, - } - let rows: Vec = candidates() - .into_iter() - .map(|c| Row { - label: c - .path - .file_stem() - .map(|s| s.to_string_lossy().into_owned()) - .unwrap_or_default(), - path: c.path, - aac_stream: c.aac_stream, - ffmpeg_stream: c.ffmpeg_stream, - }) - .collect(); - let _knob = ec_aac::knobs::set_sbr_sideinfo_debug(Some(true)); - const WIDE_LAG_MAX: i64 = 20_000; - println!( - "{:<20} {:>6} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>5} {:>10} {:>10} {:>8} {:>8} {:>10} {:>6}", - "stream", - "rate", - "kx", - "k2", - "xovr", - "sfrq", - "efrq", - "fscl", - "ascl", - "nbnd", - "lbnd", - "lgn", - "ifrq", - "smooth", - "envs", - "invf", - "harm", - "df%", - "corr" - ); - for r in &rows { - let before = ec_aac::sbr_sideinfo_log().len(); - let (ours, sbr, our_rate) = our_decode(&r.path, r.aac_stream).expect("decode"); - if sbr != ec_aac::SbrSupport::V1 { - continue; - } - let log = ec_aac::sbr_sideinfo_log(); - let frames: Vec<_> = log[before..] - .iter() - .filter(|row| row.ch == 0) - .take(20) - .collect(); - assert!(!frames.is_empty(), "{}: no sideinfo rows captured", r.label); - let f0 = &frames[0]; - let mut env_counts: std::collections::BTreeMap = Default::default(); - let mut invf_seen: std::collections::BTreeSet = Default::default(); - let mut any_harmonic = false; - let mut df_total = 0usize; - let mut df_ones = 0usize; - for row in &frames { - *env_counts - .entry(row.t_env.len().saturating_sub(1)) - .or_default() += 1; - invf_seen.extend(&row.invf_mode); - if row - .add_harmonic - .as_ref() - .is_some_and(|h| h.iter().any(|&v| v != 0)) - { - any_harmonic = true; - } - df_total += row.df_env.len() + row.df_noise.len(); - df_ones += row - .df_env - .iter() - .chain(&row.df_noise) - .filter(|&&b| b != 0) - .count(); - } - let df_pct = if df_total > 0 { - 100.0 * df_ones as f64 / df_total as f64 - } else { - 0.0 - }; - let envs_hist: String = env_counts - .iter() - .map(|(k, v)| format!("{k}x{v}")) - .collect::>() - .join(","); - let invf_str: String = invf_seen - .iter() - .map(|v| v.to_string()) - .collect::>() - .join(""); - let theirs = ffmpeg_decode(&r.path, r.ffmpeg_stream, ours.len()); - let (_, corr) = best_lag_correlation_wide(&ours[0], &theirs[0], WIDE_LAG_MAX); - println!( - "{:<20} {:>6} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>5} {:>10} {:>10} {:>8} {:>8} {:>10.1} {:>6.3}", - r.label, - our_rate, - f0.kx, - f0.k2, - f0.xover_band, - f0.start_freq, - f0.stop_freq, - f0.freq_scale, - f0.alter_scale, - f0.noise_bands, - f0.limiter_bands, - f0.limiter_gains, - f0.interpol_freq, - f0.smoothing_mode, - envs_hist, - invf_str, - any_harmonic, - df_pct, - corr - ); - println!(" patches (frame0): {:?}", f0.patch_lengths); - } -} - -/// Round (sbr-per-au): per-AU localization. Decodes the same AU list through -/// (a) the core-only decoder and (b) the full SBR chain, tracks each AU's -/// sample range in both output streams as it goes (so a per-AU decode -/// failure on either side does not desync the mapping), aligns the two -/// streams ONCE in the core's own native rate domain (same decimate -/// convention `synthetic_heaac_matrix`'s `ours_v_core` row already trusts), -/// then re-uses that single lag for every AU's own low-band correlation. -/// Prints the first 5 AUs below 0.9 with that AU's SBR side-info (source/ -/// envelope count/freq_res/kx/k2) so a per-frame trigger (e.g. `hold` -/// frames, a specific `freq_res`) shows up directly instead of needing a -/// second pass. If the first divergent AU is 0, that is reported explicitly -/// -- divergence from the very first AU means a per-stream setup mismatch, -/// not a per-frame trigger. -pub(super) fn per_au_low_band_probe(label: &str, path: &Path, aac_stream: usize) { - let (asc, aus) = extract_aac_track(path, aac_stream).expect("extract_aac_track"); - let full_cfg = parse_audio_specific_config(&asc).expect("asc parse"); - assert!(full_cfg.sbr_present, "{label}: no SBR"); - let mut core_cfg = full_cfg.clone(); - core_cfg.sbr_present = false; - core_cfg.ps_present = false; - core_cfg.extension_sample_rate = None; - let core_rate = core_cfg.sample_rate; - let mut core_dec = AacDecoder::with_config(core_cfg); - let mut full_dec = AacDecoder::with_config(full_cfg); - let full_rate = full_dec.output_sample_rate().unwrap_or(core_rate * 2); - - struct AuInfo { - core_range: (usize, usize), - side: Option, - } - let mut core_ch0: Vec = Vec::new(); - let mut full_ch0: Vec = Vec::new(); - let mut aus_info: Vec = Vec::new(); - for au in aus.iter().take(1_500) { - let side_before = ec_aac::sbr_sideinfo_log().len(); - let cs = core_ch0.len(); - if let Ok(f) = core_dec.decode(au, None) { - let ch = usize::from(f.channels).max(1); - core_ch0.extend(f.samples.iter().step_by(ch).copied()); - } - let ce = core_ch0.len(); - if let Ok(f) = full_dec.decode(au, None) { - let ch = usize::from(f.channels).max(1); - full_ch0.extend(f.samples.iter().step_by(ch).copied()); - } - let side = ec_aac::sbr_sideinfo_log()[side_before..] - .iter() - .find(|r| r.ch == 0) - .cloned(); - aus_info.push(AuInfo { - core_range: (cs, ce), - side, - }); - if full_ch0.len() > MAX_SAMPLES { - break; - } - } - assert!( - !core_ch0.is_empty() && !full_ch0.is_empty(), - "{label}: empty decode (core {} full {})", - core_ch0.len(), - full_ch0.len() - ); - - // Same convention `full_chain_low_band_matches_own_core` already - // validated (0.9967 on reeln): naive nearest-neighbor 2x-upsample - // the core into the full-rate domain, low-pass both well under the core - // Nyquist. The decimate-into-core-rate convention `synthetic_heaac_matrix` - // uses for its own `ours_v_core` diagnostic column was tried first here - // and reads badly (~-0.19) even on reeln, which is known-good -- - // that column is evidently not a reliable self-consistency probe on its - // own, so this per-AU tool anchors on the convention independently - // proven correct instead. - let up: Vec = core_ch0.iter().flat_map(|&s| [s, s]).collect(); - let cutoff = f64::from(core_rate) * 0.4; - let ol = lowpass(&full_ch0, full_rate, cutoff); - let ul = lowpass(&up, full_rate, cutoff); - const WIDE_LAG_MAX: i64 = 20_000; - let (lag, align_corr) = best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX); - println!( - "{label}: core={core_rate}Hz full={full_rate}Hz global align (upsampled full-rate domain) lag={lag} corr={align_corr:.6}" - ); - - let mut shown = 0usize; - let mut first_divergent: Option = None; - for (i, info) in aus_info.iter().enumerate() { - let (cs, ce) = info.core_range; - if ce <= cs { - continue; - } - // core_range is in core-domain sample counts; the upsampled/lowpassed - // arrays are at full rate, so the AU's window there is 2x as wide. - let fs = cs * 2; - let fe = ce * 2; - let ds = match usize::try_from(fs as i64 + lag) { - Ok(v) => v, - Err(_) => continue, - }; - let de = ds + (fe - fs); - if de > ul.len() || fe > ol.len() { - break; - } - let c = correlation(&ol[fs..fe], &ul[ds..de]); - if c < 0.9 { - if first_divergent.is_none() { - first_divergent = Some(i); - } - if shown < 5 { - println!(" AU {i}: low-band corr={c:.6} side={:?}", info.side); - shown += 1; - } - } - } - match first_divergent { - Some(0) => println!( - "{label}: divergence starts at AU 0 -- per-STREAM setup mismatch, not a per-frame trigger" - ), - Some(i) => println!("{label}: first divergent AU = {i}"), - None => println!("{label}: no AU below 0.9 corr in the probed window"), - } -} - -#[test] -pub(super) fn per_au_low_band_divergence() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let _knob = ec_aac::knobs::set_sbr_sideinfo_debug(Some(true)); - for c in &candidates() { - per_au_low_band_probe(&c.path.display().to_string(), &c.path, c.aac_stream); - } -} - -/// Coordinator follow-up: prints what `parse_audio_specific_config` actually -/// returns for a candidate's real ASC bytes (object/rate/sf_index/ -/// sbr_present/extension_sample_rate), what `AacDecoder` reports back -/// (`SbrSupport`, `output_sample_rate`) from those parsed fields, then -/// re-decodes the SAME AU list through a HAND-BUILT explicit -/// `AudioSpecificConfig` (object_type=5, known core/ext rates) instead of -/// the parsed one -- if that jumps the correlations to ~1, the parsed -/// config (or something object_type-conditioned at runtime) is the defect, -/// not the SBR DSP chain itself. Also re-runs `full_chain_low_band_matches_ -/// own_core`'s FMJ probe with `WIDE_LAG_MAX` raised to 100_000. -#[test] -pub(super) fn probe_explicit_config_and_wide_lag() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - fn decode_with(cfg: ec_aac::AudioSpecificConfig, aus: &[Vec]) -> (Vec>, u32) { - let rate = match (cfg.sbr_present, cfg.ps_present) { - (true, false) => cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2), - _ => cfg.sample_rate, - }; - let mut d = AacDecoder::with_config(cfg); - let mut planes: Vec> = Vec::new(); - for au in aus { - let Ok(f) = d.decode(au, None) else { continue }; - let ch = usize::from(f.channels); - if ch == 0 { - continue; - } - if planes.is_empty() { - planes = vec![Vec::new(); ch]; - } - for (i, v) in f.samples.iter().enumerate() { - planes[i % ch].push(*v); - } - if planes[0].len() >= MAX_SAMPLES { - break; - } - } - (planes, rate) - } - - let path = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"); - let (label, aac_stream, ffmpeg_stream, core_sf_index, core_rate, ext_rate, channels) = - ("al_sbr_cm_48_2", 0usize, 0usize, 6u8, 24_000u32, 0u32, 2u16); - { - let (asc, aus) = extract_aac_track(&path, aac_stream).expect("extract"); - let parsed = parse_audio_specific_config(&asc); - println!("{label}: asc={asc:02x?} parsed={parsed:?}"); - let parsed_cfg = parsed.expect("asc parse"); - let d = AacDecoder::with_config(parsed_cfg.clone()); - println!( - " runtime: sbr_support={:?} output_sample_rate={:?}", - d.sbr_support(), - d.output_sample_rate() - ); - - // Hand-built EXPLICIT config: object_type=5 (SBR), known core/ext - // rates, sbr_present forced true regardless of what the real ASC - // parsed to. - let ext = if ext_rate == 0 { - parsed_cfg.extension_sample_rate.unwrap_or(core_rate * 2) - } else { - ext_rate - }; - let hand_cfg = ec_aac::AudioSpecificConfig { - object_type: ec_aac::AOT_SBR, - sample_rate: core_rate, - sf_index: core_sf_index, - channels, - channel_config: channels as u8, - sbr_present: true, - ps_present: false, - extension_sample_rate: Some(ext), - pce: Vec::new(), - }; - let (hand_planes, hand_rate) = decode_with(hand_cfg.clone(), &aus); - assert!(!hand_planes.is_empty(), "{label}: hand-built config decoded no channels"); - // Own-core self-consistency (upsample+lowpass convention). - let core_cfg = ec_aac::AudioSpecificConfig { - sbr_present: false, - ps_present: false, - extension_sample_rate: None, - pce: Vec::new(), - ..hand_cfg.clone() - }; - let (core_planes, _) = decode_with(core_cfg, &aus); - if !core_planes.is_empty() { - let up: Vec = core_planes[0].iter().flat_map(|&s| [s, s]).collect(); - let cutoff = f64::from(core_rate) * 0.4; - let ol = lowpass(&hand_planes[0], hand_rate, cutoff); - let ul = lowpass(&up, hand_rate, cutoff); - let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000); - println!( - " hand-built explicit config: low-band vs own core: lag={lag} corr={corr:.6}" - ); - } - let theirs = ffmpeg_decode(&path, ffmpeg_stream, hand_planes.len()); - if let (Some(o), Some(t)) = (hand_planes.first(), theirs.first()) { - let (lag, corr) = best_lag_correlation_wide(o, t, 100_000); - println!( - " hand-built explicit config: full-band vs reference: lag={lag} corr={corr:.6}" - ); - } - } - - // Probe B: FMJ's own (parsed) full-chain-vs-own-core lag search, widened - // to 100_000 (5x the existing WIDE_LAG_MAX=20_000 this file's other - // tests use) -- the coarse search in `full_chain_low_band_matches_own_ - // core` landed right at that bound's edge (20005), so this checks - // whether FMJ's true delay simply lies further out. - let fmj = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"); - if let (Some((core, core_rate)), Some((full, sbr, full_rate))) = - (our_decode_core_only(&fmj, 0), our_decode(&fmj, 0)) - && sbr == ec_aac::SbrSupport::V1 - { - let up: Vec = core[0].iter().flat_map(|&s| [s, s]).collect(); - let cutoff = f64::from(core_rate) * 0.4; - let ol = lowpass(&full[0], full_rate, cutoff); - let ul = lowpass(&up, full_rate, cutoff); - let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000); - println!("FMJ WIDE_LAG_MAX=100_000: ch0 best lag={lag} corr={corr:.6}"); - } -} - -/// One channel of `path`'s given absolute stream, isolated with an ffmpeg -/// `pan` filter rather than raw de-interleaving -- the charter's own witness -/// rule (see `ffmpeg_decode`'s doc and the ledger's "never use -ac 1", which -/// applies just as much to picking one of N channels as to downmixing). -pub(super) fn ffmpeg_decode_pan_channel(path: &Path, absolute_stream: usize, channel: usize) -> Vec { - let out = ec_testkit::ffmpeg() - .args(["-v", "error", "-i"]) - .arg(path) - .args([ - "-map", - &format!("0:{absolute_stream}"), - "-t", - "10", - "-af", - &format!("pan=1c|c0=c{channel}"), - "-f", - "f32le", - "-acodec", - "pcm_f32le", - "-", - ]) - .output() - .expect("ffmpeg runs"); - assert!( - out.status.success(), - "ffmpeg pan decode failed: {}", - String::from_utf8_lossy(&out.stderr) - ); - out.stdout - .as_chunks::<4>().0.iter() - .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])) - .collect() -} diff --git a/crates/ec-aac/tests/sbr_real_library/chunk_c.rs b/crates/ec-aac/tests/sbr_real_library/chunk_c.rs deleted file mode 100644 index 61ba8ba2..00000000 --- a/crates/ec-aac/tests/sbr_real_library/chunk_c.rs +++ /dev/null @@ -1,677 +0,0 @@ -//! SBR real-library checks and probes, part C. - -use super::*; - -/// The user's real 7.1 mp4 whose lone AAC track was once suspected (per the -/// ledger) to need AAC Main/LTP (§4.6.6/§4.6.7) prediction support this -/// decoder doesn't have. Re-checked here: its AudioSpecificConfig's -/// `audioObjectType` is 2 (AAC-LC, confirmed both from the container's ASC -/// bytes and from the ADTS `profile` field ffmpeg remuxes it to), and this -/// decoder in fact reads its whole ~994s track -- 45000+ access units -- -/// with zero `Err` returns. No `predictor_data_present` refusal is reachable -/// on this file; the capability gap the ledger recorded does not exist for -/// it. This test is the standing proof: it decodes 10s of all 8 channels and -/// checks each against ffmpeg's own decode, isolated per channel by `pan` -/// (never `-ac 1`, which would remix rather than isolate). -#[test] -pub(super) fn reelq_multichannel_lc_matches_reference() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let path = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_5.1.mp4"); - let Some((ours, _sbr, rate)) = our_decode(&path, 0) else { - panic!("AAC track failed to decode at all"); - }; - assert_eq!(ours.len(), 6, "expected 5.1 (6 channels)"); - let want = (rate as usize) * 10; // 10s per the charter - for (ch, o) in ours.iter().enumerate() { - let o = &o[..o.len().min(want)]; - let t = ffmpeg_decode_pan_channel(&path, 0, ch); - let t = &t[..t.len().min(want)]; - let (lag, corr) = best_lag_correlation(o, t); - println!("ReelQ ch{ch}: lag {lag}, corr {corr:.6}"); - assert!(corr >= 0.999, "ch{ch} corr {corr:.6} < 0.999 (lag {lag})"); - } -} - -/// Coupled-CPE swap probe: the `heaac_44100_48k` fixture's SBR side info -/// shows `coupling: true` on nearly every AU (reeln/FMJ, both passing, -/// show `coupling: false`) -- checks whether our decoded ch0 actually -/// correlates better against the REFERENCE's R channel than its own L. -/// RULED OUT (prior rounds): `sbr_env::dequant_pair`'s coupling formula, -/// `sbr_hf`, and per-channel plane threading -- none change this probe's -/// numbers. -/// -/// ROOT CAUSE (this round): NOT a channel swap. The free lag search -/// (`best_lag_correlation_wide`) is provably unreliable on this file's -/// periodic content -- it even mis-searches ref L vs ref R against each -/// other (lag=-24 corr=0.213, worse than the naive fixed lag=0's -/// corr=0.873) -- so ch0-vs-L and ch0-vs-R land on two DIFFERENT, -/// independently-searched lags (-4617 and -4674) instead of one true -/// lag, faking a "ch0 correlates with R" swap signature. At the single -/// fixed lag that actually maximizes the summed (mono) ch0+ch1-vs-refL+R -/// correlation (-4674, mono corr 0.778), a per-1-second sub-window -/// breakdown of corr(ch0,refL) vs corr(ch0,refR) shows ch0 starts -/// NEAR-PERFECT against ref L (0.998 at t=0s) and *drifts* down to 0.68 -/// by t=9s while its correlation with ref R rises from 0.90 to ~0.99 over -/// the same span -- a clean, monotonic crossover, not a step change. That -/// is the signature of accumulating CLOCK/SAMPLE-COUNT DRIFT between our -/// decode and ffmpeg's reference decode (a single global lag can't track -/// a linearly growing offset), not a channel identity swap: ch0 IS L, -/// decoded correctly, and a single fixed-lag window increasingly -/// misaligns as the file plays, which -- because ref L and ref R are -/// themselves correlated (0.873 at lag 0) -- coincidentally starts to -/// look R-shaped as drift accumulates. See the fixed-lag matrix and -/// per-1s subwindow prints below for the numbers, and the `assert!` for -/// the swap-shaped-result gate this round adds. -#[test] -pub(super) fn coupled_cpe_channel_swap_probe() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let path = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"); - let Some((ours, sbr, out_rate)) = our_decode(&path, 0) else { - panic!("al_sbr_cm_48_2 failed to decode"); - }; - assert_eq!(sbr, ec_aac::SbrSupport::V1); - assert_eq!(ours.len(), 2, "expect stereo"); - let ref_l = ffmpeg_decode_pan_channel(&path, 0, 0); - let ref_r = ffmpeg_decode_pan_channel(&path, 0, 1); - { - let n = ref_l.len().min(ref_r.len()).min(WINDOW); - println!( - "ref L vs ref R (no lag): corr={:.6}", - correlation(&ref_l[..n], &ref_r[..n]) - ); - let (lag, corr) = best_lag_correlation_wide(&ref_l, &ref_r, SEARCH_LAG_MAX); - println!("ref L vs ref R (best lag search): lag={lag} corr={corr:.6}"); - } - for (och, ochan) in ours.iter().enumerate() { - let (lag_l, corr_l) = best_lag_correlation_wide(ochan, &ref_l, SEARCH_LAG_MAX); - let (lag_r, corr_r) = best_lag_correlation_wide(ochan, &ref_r, SEARCH_LAG_MAX); - println!( - "ch{och}: vs ref L lag={lag_l} corr={corr_l:.6}; vs ref R lag={lag_r} corr={corr_r:.6}" - ); - } - - // Same probe on the CORE-ONLY decode (SBR disabled): if the swap - // already shows here, it's in the AAC-LC core's own CPE/M-S decode, - // not in the SBR coupling math. - let (asc, aus) = extract_aac_track(&path, 0).expect("extract"); - let mut core_cfg = parse_audio_specific_config(&asc).expect("asc parses"); - core_cfg.sbr_present = false; - core_cfg.ps_present = false; - core_cfg.extension_sample_rate = None; - let mut core_dec = AacDecoder::with_config(core_cfg); - let mut core_planes: Vec> = vec![Vec::new(), Vec::new()]; - let dump = std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok(); - for (au_idx, au) in aus.iter().enumerate() { - if let Ok(f) = core_dec.decode(au, None) { - let ch = usize::from(f.channels); - if ch == 2 { - if dump && au_idx < 20 { - for c in 0..2 { - let plane: Vec = - f.samples.iter().skip(c).step_by(2).copied().collect(); - let rms = (plane - .iter() - .map(|v| f64::from(*v) * f64::from(*v)) - .sum::() - / plane.len().max(1) as f64) - .sqrt(); - eprintln!( - "COREAU n={au_idx} ch={c} len={} rms={rms:.6} first8={:?}", - plane.len(), - &plane[..8.min(plane.len())] - ); - } - } - for (i, v) in f.samples.iter().enumerate() { - core_planes[i % 2].push(*v); - } - } - } - if core_planes[0].len() >= MAX_SAMPLES { - break; - } - } - let core_rate = core_dec.output_sample_rate().unwrap_or(22050); - let ref_core = ffmpeg_decode_at_rate(&path, 0, 2, core_rate); - for (och, ochan) in core_planes.iter().enumerate() { - for (rch, rplane) in ref_core.iter().enumerate() { - let (lag, corr) = best_lag_correlation_wide(ochan, rplane, SEARCH_LAG_MAX); - let name = if rch == 0 { "L" } else { "R" }; - println!("core ch{och} vs ref {name}: lag={lag} corr={corr:.6}"); - } - } - - // Fixed-lag matrix: is the "swap" a lag-search artifact on periodic - // content, or a real decode defect? A genuine per-channel swap must - // show LOW corr(ch0,refL) and HIGH corr(ch0,refR) at the SAME lag that - // maximizes the mono (ch0+ch1 vs refL+refR) correlation, not just at - // whatever lag each pair's own FREE search happened to land on - // independently (ref L vs ref R themselves correlate 0.87 at lag 0, so - // at any single lag corr(ch0,L) and corr(ch0,R) must already be close). - let (core_lag0, core_corr0) = - best_lag_correlation_wide(&core_planes[0], &ref_core[0], SEARCH_LAG_MAX); - let core_implied_lag = core_lag0 * 2; - println!( - "core ch0 vs ref L best lag={core_lag0} corr={core_corr0:.6} -> implied full-rate lag={core_implied_lag}" - ); - let mono_ours: Vec = ours[0].iter().zip(&ours[1]).map(|(a, b)| a + b).collect(); - let mono_ref: Vec = { - let n = ref_l.len().min(ref_r.len()); - ref_l[..n] - .iter() - .zip(&ref_r[..n]) - .map(|(a, b)| a + b) - .collect() - }; - let (mono_lag, mono_corr) = best_lag_correlation_wide(&mono_ours, &mono_ref, SEARCH_LAG_MAX); - println!("mono (ch0+ch1) vs ref (L+R) best lag={mono_lag} corr={mono_corr:.6}"); - let fixed_lags: [(i64, &str); 4] = [ - (-4674, "swap-probe ch0-vs-refR lag"), - (-4617, "swap-probe ch0-vs-refL lag"), - (core_implied_lag, "core-implied lag"), - (mono_lag, "mono-best lag"), - ]; - // `best_lag_correlation_wide`'s coarse-then-refine search is proven - // unreliable on this file's periodic content -- it even mis-picks ref L - // vs ref R itself (lag=-24 corr=0.213, worse than the naive lag-0 - // corr=0.873 above). So the TRUE best lag among our 4 candidates is - // whichever one actually maximizes the fixed-lag mono correlation, not - // whatever `mono_lag` the free search reported. - let mut true_best = ( - mono_lag, - "mono-best lag (free search)", - 0.0f64, - 0.0f64, - 0.0f64, - 0.0f64, - mono_corr, - ); - for (lag, label) in fixed_lags { - let c0l = correlation_at_lag(&ours[0], &ref_l, lag); - let c0r = correlation_at_lag(&ours[0], &ref_r, lag); - let c1l = correlation_at_lag(&ours[1], &ref_l, lag); - let c1r = correlation_at_lag(&ours[1], &ref_r, lag); - let mono = correlation_at_lag(&mono_ours, &mono_ref, lag); - println!( - "fixed lag={lag} ({label}): corr(ch0,L)={c0l:.6} corr(ch0,R)={c0r:.6} corr(ch1,L)={c1l:.6} corr(ch1,R)={c1r:.6} mono={mono:.6}" - ); - if mono > true_best.6 { - true_best = (lag, label, c0l, c0r, c1l, c1r, mono); - } - } - let (best_lag, best_label, best_c0l, best_c0r, _best_c1l, _best_c1r, best_mono) = true_best; - println!( - "TRUE best-by-fixed-mono lag={best_lag} ({best_label}): corr(ch0,L)={best_c0l:.6} corr(ch0,R)={best_c0r:.6} mono={best_mono:.6}" - ); - // Assert on the fixed-lag result, not the free search: a genuine - // per-channel swap needs corr(ch0,L) LOW while corr(ch0,R) is HIGH at - // this properly-determined lag. It isn't -- ch0's correlation with L - // (0.78) sits at/below the L-vs-R baseline cross-correlation - // (ref L vs ref R = 0.873 at lag 0), i.e. no more affinity to L than - // any R-channel content already has from L/R being correlated to begin - // with -- so this is a lag-search artifact on periodic content, not a - // channel swap. - assert!( - !(best_c0l < 0.3 && best_c0r > 0.9), - "swap-shaped result at the properly-determined lag {best_lag}: corr(ch0,L)={best_c0l:.6} corr(ch0,R)={best_c0r:.6}" - ); - // Per-1-s-window corr(ch0,refL) at the TRUE best lag: uniform vs - // dipping tells apart "genuinely swapped everywhere" from "a - // coarse-search artifact confined to a periodic stretch". - { - let step = out_rate.max(1) as usize; - let (oa0, ob0) = if best_lag >= 0 { - (0usize, mono_lag as usize) - } else { - ((-best_lag) as usize, 0usize) - }; - let mut w = 0usize; - loop { - let oa = oa0 + w * step; - let ob = ob0 + w * step; - if oa + step > ours[0].len() || ob + step > ref_l.len().min(ref_r.len()) { - break; - } - let cl = correlation(&ours[0][oa..oa + step], &ref_l[ob..ob + step]); - let cr = correlation(&ours[0][oa..oa + step], &ref_r[ob..ob + step]); - println!( - "subwindow t={w}s ch0 vs ref L/R @ lag {best_lag}: corr(L)={cl:.6} corr(R)={cr:.6}" - ); - w += 1; - } - } - - // Isolated round trip: run `core_planes` (the CORE-ONLY decode, already - // shown correctly-directed above) through a bare, fresh - // Analysis(32)->zero-stuff-HF->Synthesis(64) pair -- no sbr_chain, no - // per-AU/per-tag HashMap element lookup, no envelope/HF/noise code at - // all -- to see whether the swap reproduces on the QMF math alone in a - // single continuous stream, isolating it from any per-AU state - // threading in `sbr_chain::apply_data`. - use ec_aac::sbr_qmf::{ANALYSIS_BANDS as AB, Analysis, SYNTHESIS_BANDS as SB, Synthesis}; - const OUTPUT_SCALE: f32 = 1.0 / 65536.0; - for kx in [6usize, 12, 20] { - let mut iso_out: [Vec; 2] = [Vec::new(), Vec::new()]; - for (c, chan) in core_planes.iter().enumerate() { - let mut analysis = Analysis::new(); - let mut synthesis = Synthesis::new(); - let n_slots = chan.len() / AB; - for slot in 0..n_slots { - let mut chunk = [0f32; 32]; - chunk.copy_from_slice(&chan[slot * AB..(slot + 1) * AB]); - for s in &mut chunk { - *s /= OUTPUT_SCALE; - } - let sub = analysis.process_slot(&chunk); - let mut v = [ec_dsp::Complex::new(0.0, 0.0); 64]; - v[0..kx.min(AB)].copy_from_slice(&sub[0..kx.min(AB)]); - let mut pcm = synthesis.process_slot(&v); - for s in &mut pcm { - *s *= OUTPUT_SCALE; - } - iso_out[c].extend_from_slice(&pcm); - } - let _ = SB; - } - for (och, ochan) in iso_out.iter().enumerate() { - for (rch, rplane) in [&ref_l, &ref_r].iter().enumerate() { - let (lag, corr) = best_lag_correlation_wide(ochan, rplane, SEARCH_LAG_MAX); - let name = if rch == 0 { "L" } else { "R" }; - println!("iso kx={kx} ch{och} vs ref {name}: lag={lag} corr={corr:.6}"); - } - } - } -} - -/// Full decode of one container-native AAC stream, WITHOUT `our_decode`'s -/// `MAX_SAMPLES` (~22.7s @44100) cap -- this probe needs the ~30s the -/// per-1s lag-drift table walks, and the sample-count comparison below -/// needs the true total the decoder produced, not a cap-truncated one. -/// Bounded only by `extract_from`'s existing 1_500-AU read cap. -pub(super) fn our_decode_uncapped(path: &Path, stream_index: usize) -> Option<(Vec>, u32, usize)> { - let (asc, aus) = extract_aac_track(path, stream_index)?; - let mut decoder = AacDecoder::with_config_bytes(&asc).ok()?; - let rate = decoder.output_sample_rate().unwrap_or(0); - let mut planes: Vec> = Vec::new(); - for au in &aus { - if let Ok(f) = decoder.decode(au, None) { - let ch = usize::from(f.channels); - if ch == 0 { - continue; - } - if planes.is_empty() { - planes = vec![Vec::new(); ch]; - } - for (i, v) in f.samples.iter().enumerate() { - planes[i % ch].push(*v); - } - } - } - if planes.is_empty() { - return None; - } - Some((planes, rate, aus.len())) -} - -/// Total sample count of an unbounded ffmpeg decode of one container-native -/// AAC stream, interleaved-`f32` byte count / 4 -- i.e. `channels * -/// per_channel_samples`, not capped at 30s the way `ffmpeg_decode` is. -pub(super) fn ffmpeg_total_interleaved_samples(path: &Path, absolute_stream: usize) -> u64 { - let out = ec_testkit::ffmpeg() - .args(["-v", "error", "-i"]) - .arg(path) - .args([ - "-map", - &format!("0:{absolute_stream}"), - "-f", - "f32le", - "-acodec", - "pcm_f32le", - "-", - ]) - .output() - .expect("ffmpeg runs"); - assert!( - out.status.success(), - "ffmpeg full decode failed: {}", - String::from_utf8_lossy(&out.stderr) - ); - out.stdout.len() as u64 / 4 -} - -/// `ffprobe -show_entries stream=` on the file's first audio stream, -/// trimmed. Used to cross-check the container's declared `sample_rate` and -/// `duration` against what our decoder derives and what ffmpeg actually -/// produces. -pub(super) fn ffprobe_field(path: &Path, entry: &str) -> String { - let out = ec_testkit::ffprobe() - .args([ - "-v", - "error", - "-select_streams", - "a:0", - "-show_entries", - &format!("stream={entry}"), - "-of", - "default=nw=1:nk=1", - ]) - .arg(path) - .output() - .expect("ffprobe runs"); - String::from_utf8_lossy(&out.stdout).trim().to_string() -} - -/// Narrow (`center +/- half`) fixed-window lag search anchored at ours-index -/// `oa0`, `len` samples long -- immune to the periodic-content aliasing that -/// makes `best_lag_correlation_ex`'s free ~100_000-wide coarse search -/// unreliable on this file (see the ledger dead-end on ref-L-vs-ref-R -/// itself mis-landing at lag=-24). `theirs`-index is `ours_index + lag`, -/// the same convention `best_lag_correlation_ex::slice_at` uses. -pub(super) fn narrow_lag_at( - ours: &[f32], - theirs: &[f32], - oa0: usize, - len: usize, - center: i64, - half: i64, -) -> (i64, f64) { - let mut best = (center, -2.0f64); - for lag in (center - half)..=(center + half) { - let ob_i = oa0 as i64 + lag; - if ob_i < 0 { - continue; - } - let (oa, ob) = (oa0, ob_i as usize); - if oa + len > ours.len() || ob + len > theirs.len() { - continue; - } - let c = correlation(&ours[oa..oa + len], &theirs[ob..ob + len]); - if c > best.1 { - best = (lag, c); - } - } - best -} - -/// Round-55 charter probe: is the `heaac_44100_48k.m4a` (44.1k-family HE-AAC) -/// lag-vs-time drift a sample-count mismatch between our decode and -/// ffmpeg's reference (dropped/duplicated AU somewhere in the chain), or a -/// rate-label bug (we call it 44100 but produce samples at another rate), -/// or per-AU apply_last/hold fallbacks accumulating skew? Runs the same -/// three measurements on one 48k-family HE-AAC fixture and on reeln -/// (real 44.1k HE-AAC) as controls that are known NOT to show the drift. -#[test] -pub(super) fn sbr441_family_sample_drift_probe() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let cases: [(&str, PathBuf, i64); 2] = [ - ("al_sbr_cm_48_2", ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"), 0), - ("al_sbr_sr_48_2_fsaac48", ec_testkit::fixture("open/fate/aac/al_sbr_sr_48_2_fsaac48.mp4"), 0), - ]; - for (label, path, seed_lag) in cases { - println!("\n=== {label}: {} ===", path.display()); - let holds_before = ec_aac::hold_call_count(); - let (ours, out_rate, au_count) = our_decode_uncapped(&path, 0).expect("our_decode_uncapped"); - // `ffmpeg_decode_pan_channel` caps at 10s (per its own charter, - // shared with `reelq_multichannel_lc_matches_reference`); - // this probe walks out to t=29s, so it needs `ffmpeg_decode`'s 30s - // cap instead -- planar channel 0 is L for every stereo case here. - let ref_l = ffmpeg_decode(&path, 0, 2).swap_remove(0); - let ch0 = &ours[0]; - // Cross-check the hardcoded `seed_lag` guess with a free wide - // search before trusting the narrow window around it -- a wrong - // seed on a control file reads as near-zero correlation - // everywhere, indistinguishable from "no signal", not "no drift". - { - let (wlag, wcorr) = best_lag_correlation_wide(ch0, &ref_l, SEARCH_LAG_MAX); - println!(" (wide free-search cross-check: lag={wlag} corr={wcorr:.6})"); - } - - // (1) per-1s narrow lag search, +/-128 around the file's known - // (or, for the controls, assumed-near-zero) fixed lag. - println!("-- lag(t), narrow +/-128 window around seed lag {seed_lag} --"); - let mut lag_points: Vec<(f64, f64)> = Vec::new(); - let step = out_rate.max(1) as usize; - for t in 0..30usize { - let oa0 = t * step; - if oa0 + step > ch0.len() { - break; - } - let (lag, corr) = narrow_lag_at(ch0, &ref_l, oa0, step, seed_lag, 128); - println!(" t={t:>2}s lag={lag:>7} corr={corr:.6}"); - lag_points.push((t as f64, lag as f64)); - } - if lag_points.len() >= 2 { - let n = lag_points.len() as f64; - let sx: f64 = lag_points.iter().map(|(x, _)| x).sum(); - let sy: f64 = lag_points.iter().map(|(_, y)| y).sum(); - let sxy: f64 = lag_points.iter().map(|(x, y)| x * y).sum(); - let sxx: f64 = lag_points.iter().map(|(x, _)| x * x).sum(); - let slope = (n * sxy - sx * sy) / (n * sxx - sx * sx); - println!(" fitted slope = {slope:.4} samples/second"); - } - - // (2) total sample count per channel: ours vs ffmpeg vs ffprobe - // duration*rate vs AU-count*2048. - let our_total = ch0.len() as u64; - let ffmpeg_total = ffmpeg_total_interleaved_samples(&path, 0) / 2; // stereo - let ffprobe_duration: f64 = ffprobe_field(&path, "duration").parse().unwrap_or(0.0); - let ffprobe_rate: f64 = ffprobe_field(&path, "sample_rate").parse().unwrap_or(0.0); - let ffprobe_total = (ffprobe_duration * ffprobe_rate).round() as u64; - let au_implied_total = au_count as u64 * 2048; - println!( - "-- sample counts (per channel): ours={our_total} ffmpeg={ffmpeg_total} \ - ffprobe(duration*rate)={ffprobe_total} au_count*2048={au_implied_total} \ - (au_count={au_count}) our_decode_rate={out_rate} ffprobe_rate={ffprobe_rate} \ - ratio(ours/ffmpeg)={:.6}", - our_total as f64 / ffmpeg_total.max(1) as f64 - ); - - // (4) apply_last/hold fallback count for this file's own decode - // (process-wide counter, delta'd against this file's own before/ - // after since the test loop shares one process). - let holds = ec_aac::hold_call_count() - holds_before; - println!("-- apply_last (hold) calls during this file's decode: {holds} of {au_count} AUs"); - } -} - -/// Round-56 charter probe: at the corr cliff `heaac_48000_64k.m4a` shows -/// once the synthetic sweep content reaches the SBR HF register (good -/// t=14s, bad t=18s/24s per the ledger lever), print per-band |X| (dB) and -/// ours-minus-reference phase for 8 consecutive QMF slots at each of the -/// three timestamps, reading both sides through -/// [`ec_aac::sbr_qmf::HfAnalysis`] via the existing `qmf_domain_spec` third -/// witness (ISO/IEC 14496-3 4.6.18.4 64-band analysis of each side's own -/// final PCM, band unit = `rate/128` Hz -- exactly the patch table's own -/// unit). Also prints the file's own patch table (ISO/IEC 14496-3 -/// 4.6.18.6.3 `build_patches`) and an `EC_AAC_SBR_HF_BYPASS` A/B so a -/// "our HF actively hurts" vs "our HF is merely incomplete" verdict is -/// readable straight from the printed corr numbers. -#[test] -pub(super) fn sbr_hf_window_band_probe() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let path = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"); - const LAG: i64 = -4673; - - let (ours, out_rate, _au_count) = our_decode_uncapped(&path, 0).expect("our_decode_uncapped"); - let ref_l = ffmpeg_decode(&path, 0, 2).swap_remove(0); - let ch0 = &ours[0]; - - for t in [14usize, 18, 24] { - let oa0 = t * out_rate as usize; - if oa0 + out_rate as usize > ch0.len() { - continue; - } - let (lag, corr) = narrow_lag_at(ch0, &ref_l, oa0, out_rate as usize, LAG, 32); - let (wlag, wcorr) = narrow_lag_at(ch0, &ref_l, oa0, out_rate as usize, LAG, 8192); - println!( - "\n=== t={t}s narrow-lag corr={corr:.6} lag={lag} (wide +/-8192 search: lag={wlag} corr={wcorr:.6}) ===" - ); - let ob0 = (oa0 as i64 + lag) as usize; - // 8 QMF slots (512 samples) starting at this second's aligned position. - const WIN: usize = 8 * 64; - if oa0 + WIN > ch0.len() || ob0 + WIN > ref_l.len() { - println!(" SKIP: window out of range"); - continue; - } - let (o_spec, t_spec, _sim) = qmf_domain_spec(&ch0[oa0..oa0 + WIN], &ref_l[ob0..ob0 + WIN]); - for band in 14..48usize { - let mut mags_o = Vec::new(); - let mut mags_t = Vec::new(); - let mut dphase_deg = Vec::new(); - for slot in 0..8 { - let o = o_spec[band][slot]; - let r = t_spec[band][slot]; - let mo = (o.re * o.re + o.im * o.im).sqrt(); - let mr = (r.re * r.re + r.im * r.im).sqrt(); - mags_o.push(20.0 * (mo.max(1e-12)).log10()); - mags_t.push(20.0 * (mr.max(1e-12)).log10()); - if mo > 1e-6 && mr > 1e-6 { - let d = (o.im.atan2(o.re) - r.im.atan2(r.re)).to_degrees(); - let d = ((d + 180.0).rem_euclid(360.0)) - 180.0; - dphase_deg.push(d); - } else { - dphase_deg.push(f64::NAN); - } - } - let mean_o: f64 = mags_o.iter().sum::() / 8.0; - let mean_t: f64 = mags_t.iter().sum::() / 8.0; - println!( - " band{band:>3}: |ours|dB mean={mean_o:>7.2} |ref|dB mean={mean_t:>7.2} dphase(deg)={:?}", - dphase_deg - .iter() - .map(|d| format!("{d:.0}")) - .collect::>() - ); - } - } - - // HF-bypass A/B on the bad windows: does zeroing our HF raise or lower - // corr against the reference in [18s,19s) and [24s,25s)? - let bypass = { - let _bypass = ec_aac::knobs::set_sbr_hf_bypass(Some(true)); - our_decode_uncapped(&path, 0) - }; - if let Some((bp, bp_rate, _)) = bypass { - let bch0 = &bp[0]; - for t in [14usize, 18, 24] { - let oa0 = t * bp_rate as usize; - if oa0 + bp_rate as usize > bch0.len() { - continue; - } - let (lag, corr) = narrow_lag_at(bch0, &ref_l, oa0, bp_rate as usize, LAG, 32); - println!(" t={t}s HF_BYPASS corr={corr:.6} lag={lag}"); - } - } else { - eprintln!("HF_BYPASS decode failed"); - } - - // Patch table (ISO/IEC 14496-3 4.6.18.6.3), sideinfo-derived. - { - let _sideinfo = ec_aac::knobs::set_sbr_sideinfo_debug(Some(true)); - let _ = our_decode_uncapped(&path, 0); - } - if let Some(row) = ec_aac::sbr_sideinfo_log().first() { - println!( - "\n-- patch table (first frame): kx={} k2={} patch widths={:?}", - row.kx, row.k2, row.patch_lengths - ); - } -} - -/// Round-59 residual locator: per-2048-sample-window corr at lag 0 (worst -/// windows, with the output frame index) plus a complex-STFT per-band -/// correlation (Parseval: equals the time-domain corr restricted to that -/// band, so it needs no calibration) with an ours-vs-ours self-check column. -#[test] -pub(super) fn sbr_residual_locator() { - let _guard = DECODE_SERIAL.lock().unwrap_or_else(|e| e.into_inner()); - let path = ec_testkit::fixture("open/fate/aac/al_sbr_cm_48_2.mp4"); - let (ours, _, rate) = our_decode(&path, 0).expect("decode"); - let theirs = ffmpeg_decode(&path, 0, ours.len()); - const W: usize = 2048; - const FFT_LEN: usize = 2048; - const HOP: usize = 1024; - let bins = FFT_LEN / 2 + 1; - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let win = ec_dsp::Window::::sine(FFT_LEN); - for ch in 0..ours.len() { - let n = ours[ch].len().min(theirs[ch].len()); - let (o, t) = (&ours[ch][..n], &theirs[ch][..n]); - let mut wins: Vec<(f64, usize)> = (0..n / W) - .map(|w| { - ( - correlation(&o[w * W..(w + 1) * W], &t[w * W..(w + 1) * W]), - w, - ) - }) - .collect(); - wins.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); - println!("ch{ch} worst windows (corr, frame): {:?}", &wins[..12]); - let hops = (n - FFT_LEN) / HOP + 1; - let mut spec = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - let mut acc = vec![[0.0f64; 3]; bins]; // re(o*conj t), |o|^2, |t|^2 - let mut os = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - for h in 0..hops { - let mut b = o[h * HOP..h * HOP + FFT_LEN].to_vec(); - win.apply(&mut b); - rfft.forward(&b, &mut spec); - os.copy_from_slice(&spec); - let mut b = t[h * HOP..h * HOP + FFT_LEN].to_vec(); - win.apply(&mut b); - rfft.forward(&b, &mut spec); - for k in 0..bins { - let (a, c) = (os[k], spec[k]); - acc[k][0] += f64::from(a.re * c.re + a.im * c.im); - acc[k][1] += f64::from(a.norm_sqr()); - acc[k][2] += f64::from(c.norm_sqr()); - } - } - let hz = f64::from(rate) / FFT_LEN as f64; - let tot: f64 = acc.iter().map(|a| a[1] + a[2]).sum(); - // With `EC_AAC_SBR_NOISE_FRACTION` set the decoder bookkeeps the - // dequantized target per QMF band; print it next to the reference's - // measured energy in the same band so a target-side (bitstream -> - // E_orig) tilt separates from an adjust/synthesis-side one. - if ch == 0 { - let band_hz = f64::from(rate) / 128.0; - for (band, sig, noi, out) in ec_aac::noise_fraction_table() { - let (lo, hi) = ( - (band as f64 * band_hz / hz) as usize, - ((band + 1) as f64 * band_hz / hz) as usize, - ); - let pt: f64 = (lo..hi.min(bins)).map(|k| acc[k][2]).sum(); - let po: f64 = (lo..hi.min(bins)).map(|k| acc[k][1]).sum(); - println!( - "TARGET band {band:>2} {:>6.0}Hz target {:.3e} realised/target {:.4} ref_pcm {:.3e} ours_pcm {:.3e} target/ref_pcm {:.3e}", - band as f64 * band_hz, - sig + noi, - out / (sig + noi), - pt, - po, - (sig + noi) / pt - ); - } - } - // group into 250 Hz bands up to 8 kHz, then 1 kHz - let mut edges: Vec = (0..=32) - .map(|i| ((i as f64 * 250.0) / hz) as usize) - .collect(); - edges.extend((9..=22).map(|i| ((i as f64 * 1000.0) / hz) as usize)); - for e in edges.windows(2) { - let (mut c, mut po, mut pt) = (0.0, 0.0, 0.0); - for row in acc.iter().take(e[1].min(bins)).skip(e[0]) { - c += row[0]; - po += row[1]; - pt += row[2]; - } - println!( - "ch{ch} {:5.0}-{:5.0} Hz corr {:.5} rms ours/ref {:.4} energy share {:.4}", - e[0] as f64 * hz, - e[1] as f64 * hz, - c / (po * pt).sqrt(), - (po / pt).sqrt(), - (po + pt) / tot - ); - } - } -} diff --git a/crates/ec-aac/tests/sbr_real_library/helpers.rs b/crates/ec-aac/tests/sbr_real_library/helpers.rs deleted file mode 100644 index 4c50a910..00000000 --- a/crates/ec-aac/tests/sbr_real_library/helpers.rs +++ /dev/null @@ -1,2187 +0,0 @@ - -use super::*; - -pub(super) fn fate_aac() -> Vec { - [ - "open/fate/aac/al_sbr_cm_48_2.mp4", - "open/fate/aac/al_sbr_cm_48_5.1.mp4", - "open/fate/aac/al_sbr_ps_04_new.mp4", - "open/fate/aac/al_sbr_ps_06_new.mp4", - "open/fate/aac/al_sbr_sr_48_2_fsaac48.mp4", - ] - .iter() - .map(|rel| ec_testkit::fixture(rel)) - .collect() -} - -/// A film's audio track is hours long; the correlation only needs a bounded -/// window near the start, so both sides cap how much they ever decode. -pub(super) const MAX_SAMPLES: usize = 1_000_000; - -/// ffmpeg's decode of a file's given absolute stream index, as -/// planar-by-channel `f32`. -pub(super) fn ffmpeg_decode(path: &Path, absolute_stream: usize, channels: usize) -> Vec> { - let out = ec_testkit::ffmpeg() - .args(["-v", "error", "-i"]) - .arg(path) - .args([ - "-map", - &format!("0:{absolute_stream}"), - "-t", - "30", - "-f", - "f32le", - "-acodec", - "pcm_f32le", - "-", - ]) - .output() - .expect("ffmpeg runs"); - assert!( - out.status.success(), - "ffmpeg decode failed: {}", - String::from_utf8_lossy(&out.stderr) - ); - let samples: Vec = out - .stdout - .as_chunks::<4>().0.iter() - .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])) - .collect(); - let mut planes = vec![Vec::with_capacity(samples.len() / channels.max(1)); channels]; - for (i, &v) in samples.iter().enumerate() { - planes[i % channels].push(v); - } - planes -} - -/// One AAC stream pulled out of its container: the AudioSpecificConfig bytes -/// exactly as the container carried them, and its access units in decode -/// order. Whichever of `ec-mp4`/`ec-matroska` the file's header names does the -/// actual box/EBML walk -- this is a thin `Demuxer`-trait harness over them, -/// not a parser of its own. -pub(super) fn extract_aac_track(path: &Path, stream_index: usize) -> Option<(Vec, Vec>)> { - let mut head = [0u8; 12]; - File::open(path).ok()?.read_exact(&mut head).ok()?; - if ec_mp4::is_mp4(&head) { - let f = File::open(path).ok()?; - let mut d = Mp4Demuxer::new(BufReader::new(f)).ok()?; - extract_from(&mut d, stream_index) - } else if ec_matroska::is_matroska(&head) { - let f = File::open(path).ok()?; - let mut d = MatroskaDemuxer::new(BufReader::new(f)).ok()?; - extract_from(&mut d, stream_index) - } else { - None - } -} - -/// `stream_index` picks the Nth AAC stream the container lists, in listed -/// order -- a file can carry more than one audio track (FMJ's mkv has both an -/// LC 5.1 track and an HE-AAC stereo commentary track). -pub(super) fn extract_from(d: &mut dyn Demuxer, stream_index: usize) -> Option<(Vec, Vec>)> { - let aac = d - .streams() - .iter() - .filter(|s| s.params.codec == CodecId::Aac) - .nth(stream_index)?; - let idx = aac.index; - let asc = aac.params.extradata.as_ref()?.to_vec(); - let mut aus = Vec::new(); - // A film's audio track is a couple thousand AAC access units for the - // handful of seconds the correlation actually needs; capped here so the - // packet walk does not have to read the whole multi-gigabyte video - // stream past it just to reach `Eof`. - while aus.len() < 1_500 { - match d.next_packet() { - Ok(Packet { stream, data, .. }) if stream == idx => aus.push(data.to_vec()), - Ok(_) => continue, - Err(_) => break, - } - } - Some((asc, aus)) -} - -/// Our decode of one container-native AAC stream, through its real -/// AudioSpecificConfig, as planar-by-channel `f32`. -pub(super) fn our_decode( - path: &Path, - stream_index: usize, -) -> Option<(Vec>, ec_aac::SbrSupport, u32)> { - let Some((asc, aus)) = extract_aac_track(path, stream_index) else { - eprintln!(" extract_aac_track returned None"); - return None; - }; - eprintln!(" asc={asc:02x?} aus={}", aus.len()); - let mut decoder = match AacDecoder::with_config_bytes(&asc) { - Ok(d) => d, - Err(e) => { - eprintln!(" with_config_bytes failed: {e:?}"); - return None; - } - }; - let sbr = decoder.sbr_support(); - let rate = decoder.output_sample_rate().unwrap_or(0); - let mut planes: Vec> = Vec::new(); - let mut failed = 0; - let debug_failures = std::env::var("EC_AAC_SBR_AU_FAIL_DEBUG").as_deref() == Ok("1"); - for (au_idx, au) in aus.iter().enumerate() { - let frame = match decoder.decode(au, None) { - Ok(f) => f, - Err(e) => { - failed += 1; - if debug_failures { - let head: Vec = au.iter().take(16).copied().collect(); - eprintln!( - " AU {au_idx} FAILED len={} err={e:?} head={head:02x?}", - au.len() - ); - } - continue; - } - }; - let ch = usize::from(frame.channels); - if ch == 0 { - continue; - } - if planes.is_empty() { - planes = vec![Vec::new(); ch]; - } - for (i, v) in frame.samples.iter().enumerate() { - planes[i % ch].push(*v); - } - if planes[0].len() >= MAX_SAMPLES { - break; - } - } - if failed > 0 { - eprintln!(" {failed}/{} access units failed to decode", aus.len()); - } - if planes.is_empty() { - eprintln!(" no channels decoded"); - return None; - } - Some((planes, sbr, rate)) -} - -/// Search window bound: keeps the correlation sum bounded, per the charter -/// -- an unbounded version over a whole film's worth of samples was too slow -/// to be an every-run test. -pub(super) const WINDOW: usize = 200_000; - -/// Wide lag search bound for the specific gate proven to need it -/// (`sbr_real_library_matches_reference`, ours-vs-reference across a real -/// container's remux/priming delay). Measured (round-54 wide-lag probe, -/// worktree sbr-per-au) FMJ's true full-chain delay vs the reference is -/// ~22,913 samples @48kHz -- that gate's old `LAG_MAX=4_000` search clipped -/// at its own edge and silently reported the edge's noise as the answer for -/// 50+ rounds (class: "an alignment instrument whose result sits at its own -/// search edge is invalid"). 100_000 covers that with 4x headroom and is -/// already the bound round-53's own `WIDE_LAG_MAX=100_000` probe used to -/// find it. NOT used as the default everywhere: a wider bound also widens -/// the coarse pass's exposure to spurious short-slice noise peaks, so -/// call sites whose true delay is known-small (container/decoder priming, -/// ours-vs-own-core self-consistency) keep their original, narrower bound -/// -- round-54 first tried blanket-widening every call site to this value -/// and it flipped `full_chain_low_band_matches_own_core` and the LC control -/// row of `synthetic_heaac_matrix` from a correct small-lag answer to a -/// wrong, noise-driven distant one, still comfortably inside the bound -/// (not `at_edge`) but wrong regardless. -pub(super) const SEARCH_LAG_MAX: i64 = 100_000; -/// Original bound for the plain `best_lag_correlation` wrapper, used by the -/// file's other (ours-vs-reference small-priming-delay, or ours-vs-ours) -/// callers that were never shown to need more. -pub(super) const PLAIN_LAG_MAX: i64 = 4_000; -/// Odd stride `best_lag_correlation_ex`'s coarse pass steps by, so it -/// doesn't alias against frame-length periodicity. -pub(super) const SEARCH_STRIDE: i64 = 11; - -pub(super) fn dot(a: &[f32], b: &[f32]) -> f64 { - a.iter() - .zip(b) - .map(|(x, y)| f64::from(*x) * f64::from(*y)) - .sum() -} - -pub(super) fn correlation(a: &[f32], b: &[f32]) -> f64 { - let n = a.len().min(b.len()); - if n < 1024 { - return 0.0; - } - let (a, b) = (&a[..n], &b[..n]); - let ma = a.iter().map(|v| f64::from(*v)).sum::() / n as f64; - let mb = b.iter().map(|v| f64::from(*v)).sum::() / n as f64; - let ac: Vec = a.iter().map(|v| (f64::from(*v) - ma) as f32).collect(); - let bc: Vec = b.iter().map(|v| (f64::from(*v) - mb) as f32).collect(); - let num = dot(&ac, &bc); - let den = (dot(&ac, &ac) * dot(&bc, &bc)).sqrt(); - if den == 0.0 { 0.0 } else { num / den } -} - -/// Best correlation over a bounded lag search, PLUS whether the winning lag -/// sits at the search's own edge -- the single instrument every asserting -/// test in this file now routes through (class fix, round-54: a search that -/// silently clips at its bound and reports the edge's noise as "the -/// answer" is not an instrument, it's a lie). Two passes keep the whole -/// search O(reasonable) even at `SEARCH_LAG_MAX`-sized bounds on a -/// film-length file: an odd-`SEARCH_STRIDE` coarse pass over a short slice -/// finds roughly where the alignment is (the stride, not full 1-by-1 -/// stepping, is what keeps a `lag_max` in the tens of thousands cheap, and -/// also avoids aliasing against frame-length periodicity), then one -/// full-`WINDOW` correlation at that lag (and its immediate neighbours, in -/// case the coarse slice picked a noisy lag) gives the reported number. -pub(super) fn best_lag_correlation_ex(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) { - const COARSE: usize = 4_096; - let start = window_start(ours, theirs); - let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { - let (oa, ob) = if lag >= 0 { - (start, start + lag as usize) - } else { - (start + (-lag) as usize, start) - }; - if oa + len > ours.len() || ob + len > theirs.len() { - None - } else { - Some((oa, ob)) - } - }; - // The winning lag is the one that maximizes |correlation|, not signed - // correlation: a channel that is a pure phase/sign inversion of the - // reference reads strongly NEGATIVE at its true alignment lag, and a - // signed-max search rejects that lag for whatever unrelated lag gives a - // merely positive value -- reporting a real inversion as near-zero - // noise. `found` (not "best abs value >= 0", which every real - // correlation already satisfies) is what actually distinguishes "no - // in-bounds lag existed" from "the best in-bounds lag was negative" -- - // an earlier version used `best.1 < 0.0` for that check, which treats - // every genuinely negative (inverted) result as "not found" and falls - // back to the COARSE, `COARSE`-sample-window-only candidate instead; - // that coarse window is short enough to spuriously correlate at 0.87 on - // real audio, so the fallback was silently replacing an honest, - // correctly-refined negative full-`WINDOW` result with a noise-driven - // positive one from a 20x-shorter slice. Carry the signed value through - // so callers can see the sign, not just |corr|. - // Stride 1, not `SEARCH_STRIDE` (round-61): a real correlation peak - // between two decodes of the same audio is often ONE sample wide (on - // reeln's core-only PCM: 0.9998 at lag 481, 0.94 at 479), so a - // strided grid samples only the noise floor around it and the "best - // coarse lag" it hands the refine pass is arbitrary. That is how a - // 0.9998 match printed as corr 0.408 at lag -52. Stride 1 over the - // narrow bound is ~33M multiply-adds; the wide bound is 25x that and - // only runs on escalation, so this is not worth a resolution trade. - let mut coarse_best = (0i64, -1.0f64, 0.0f64); - for lag in -lag_max..=lag_max { - if let Some((oa, ob)) = slice_at(lag, COARSE) { - let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]); - if c.abs() > coarse_best.1 { - coarse_best = (lag, c.abs(), c); - } - } - } - let at_edge = coarse_best.0.abs() >= lag_max - SEARCH_STRIDE; - let mut best = (coarse_best.0, -1.0f64, 0.0f64); - let mut found = false; - for lag in (coarse_best.0 - 2 * SEARCH_STRIDE)..=(coarse_best.0 + 2 * SEARCH_STRIDE) { - let Some((oa, ob)) = slice_at(lag, WINDOW) else { - continue; - }; - let n = WINDOW.min(ours.len() - oa).min(theirs.len() - ob); - if n < 1024 { - continue; - } - let c = correlation(&ours[oa..oa + n], &theirs[ob..ob + n]); - if c.abs() > best.1 { - best = (lag, c.abs(), c); - } - found = true; - } - if !found { - best = coarse_best; - } - // A winning lag at the search bound is not a measurement (the true - // alignment may lie outside it): widen once to `SEARCH_LAG_MAX`, and if - // it still sits at the edge fail here rather than hand any caller a - // noise-driven number (instrument-at-bound class). - if at_edge && lag_max < SEARCH_LAG_MAX { - return best_lag_correlation_ex(ours, theirs, SEARCH_LAG_MAX); - } - assert!( - !at_edge, - "winning lag {} sits at the +/-{lag_max} search bound -- the true \ - alignment is outside the search, corr {:.6} is noise, not a measurement", - best.0, best.2 - ); - (best.0, best.2, at_edge) -} - -/// Thin wrapper over `best_lag_correlation_ex` at `PLAIN_LAG_MAX`, for the -/// many non-asserting call sites that only want `(lag, corr)` and whose true -/// delay was never shown to exceed the original 4_000 bound. The one gate -/// proven to need the wide bound (`sbr_real_library_matches_reference`) -/// calls `_ex` directly with `SEARCH_LAG_MAX` instead of going through this -/// wrapper. -pub(super) fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { - let (lag, corr, _) = best_lag_correlation_ex(ours, theirs, PLAIN_LAG_MAX); - (lag, corr) -} - -/// Thin wrapper over `best_lag_correlation_ex` at a caller-supplied bound, -/// for non-asserting call sites (asserting ones use `_ex` directly). -pub(super) fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64) { - let (lag, corr, _) = best_lag_correlation_ex(ours, theirs, lag_max); - (lag, corr) -} - -/// The robust two-stage lag search `sbr441_family_sample_drift_probe` proved -/// out (round-55/56): a plain coarse-stride search picks the SINGLE best -/// `COARSE`-sample-window candidate, which on periodic/tonal real music -/// content routinely aliases onto a wrong lag (this file family's ref-L-vs- -/// ref-R self-check scored a coarse-search lag=-24 corr=0.213, worse than -/// the true fixed-lag comparison's 0.873). Instead of trusting the single -/// coarse winner, keep the top `K` coarse candidates and refine EACH with a -/// full-resolution, narrow (+/-128) search restricted to the file's first -/// 10 seconds (`sample_rate`-derived) -- periodic aliasing needs many -/// seconds of a repeating pattern to fool a full-resolution window, so the -/// candidate that is genuinely aligned wins there even when it wasn't the -/// coarse pass's top pick. Returns `(lag, corr, at_edge)` for the winning -/// refined candidate, `at_edge` inherited from that candidate's own coarse -/// Stride-1 lag search: correlates a `COARSE`-sized window at EVERY lag in -/// `+/-lag_max`, then refines the winner with a full `WINDOW`. -/// -/// `robust_lag_topk`'s coarse pass steps by `SEARCH_STRIDE` (11) and then -/// refines only its top-K coarse scorers. That ranking is meaningless when -/// the true peak is narrower than the stride: on reeln's core-only PCM -/// the correlation is 0.9998 at lag 481 and already 0.94 at 479, so no -/// stride-11 grid point (the grid is 4 mod 11; 481 is 8 mod 11) scores -/// anything but noise, the top-K is drawn from noise, and the search -/// returned lag -52 corr 0.408 for a pair that is actually a 0.9998 match. -/// FMJ, same instrument, same true lag 481, happened to land a noise-ranked -/// candidate within the +/-128 refine window and read correctly -- the -/// failure is luck-dependent, not file-dependent. Stride 1 over a 4_096 -/// window is ~33M multiply-adds at `PLAIN_LAG_MAX`, cheaper than the top-K -/// refinement it replaces, so the resolution is simply not worth trading. -pub(super) fn exhaustive_lag_correlation(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) { - const COARSE: usize = 4_096; - let start = window_start(ours, theirs); - let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { - let (oa, ob) = if lag >= 0 { - (start, start + lag as usize) - } else { - (start + (-lag) as usize, start) - }; - if oa + len > ours.len() || ob + len > theirs.len() { - None - } else { - Some((oa, ob)) - } - }; - let mut best = (0i64, -1.0f64); - for lag in -lag_max..=lag_max { - if let Some((oa, ob)) = slice_at(lag, COARSE) { - let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]).abs(); - if c > best.1 { - best = (lag, c); - } - } - } - let at_edge = best.0.abs() >= lag_max - 1; - let mut refined = (best.0, -1.0f64); - for lag in (best.0 - 2)..=(best.0 + 2) { - let Some((oa, ob)) = slice_at(lag, WINDOW) else { - continue; - }; - let n = WINDOW.min(ours.len() - oa).min(theirs.len() - ob); - if n < 1024 { - continue; - } - let c = correlation(&ours[oa..oa + n], &theirs[ob..ob + n]).abs(); - if c > refined.1 { - refined = (lag, c); - } - } - if refined.1 < 0.0 { - refined = best; - } - (refined.0, refined.1, at_edge) -} - -/// bucket (true if ITS coarse lag sat at the +/-`lag_max` search bound). -pub(super) fn robust_lag_topk( - ours: &[f32], - theirs: &[f32], - lag_max: i64, - rate: u32, - k: usize, -) -> (i64, f64, bool) { - const COARSE: usize = 4_096; - let start = window_start(ours, theirs); - let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { - let (oa, ob) = if lag >= 0 { - (start, start + lag as usize) - } else { - (start + (-lag) as usize, start) - }; - if oa + len > ours.len() || ob + len > theirs.len() { - None - } else { - Some((oa, ob)) - } - }; - // Stride 1 (round-61), for the reason spelled out on - // `exhaustive_lag_correlation`: a strided grid can miss a 1-sample-wide - // peak entirely, and then the top-K is a ranking of noise. - let mut coarse: Vec<(i64, f64)> = Vec::new(); - for lag in -lag_max..=lag_max { - if let Some((oa, ob)) = slice_at(lag, COARSE) { - let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]); - coarse.push((lag, c.abs())); - } - } - coarse.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal)); - let ten_s = 10 * rate.max(1) as usize; - let mut best: (i64, f64, bool) = (0, -2.0, false); - for &(cand_lag, _) in coarse.iter().take(k) { - let at_edge = cand_lag.abs() >= lag_max - SEARCH_STRIDE; - // `narrow_lag_at` computes `ob_i = oa0 + lag` directly (no sign - // split the way `slice_at` above does), so a fixed `oa0 = 0` goes - // negative -- and every lag in the whole +/-128 window skips -- - // whenever `cand_lag` is negative enough (this file family's true - // lag is -4674). Shift the window's start so it stays anchored to - // "the first 10s of real content" while guaranteeing `ob_i >= 0` - // even at the most-negative lag in the +/-128 refine range. - let oa0 = (128 - cand_lag).max(0) as usize; - let refine_len = ten_s.min(ours.len().saturating_sub(oa0)).min( - theirs - .len() - .saturating_sub((oa0 as i64 + cand_lag).max(0) as usize), - ); - if refine_len < 1024 { - continue; - } - let (rlag, rcorr) = narrow_lag_at(ours, theirs, oa0, refine_len, cand_lag, 128); - if rcorr > best.1 { - best = (rlag, rcorr, at_edge); - } - } - best -} - -pub(super) fn window_start(ours: &[f32], theirs: &[f32]) -> usize { - const CHUNK: usize = 1_024; - const CHUNKS: usize = 4; - let end = ours.len().min(theirs.len()); - let base = end.saturating_sub(WINDOW).min(ours.len() / 4); - let power = |s: &[f32]| { - s.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() / s.len().max(1) as f64 - }; - let floor = 1e-4 * power(&ours[base..end]).min(power(&theirs[base..end])); - let loud = |p: usize| { - p + CHUNK * CHUNKS <= end - && (p..p + CHUNK * CHUNKS) - .step_by(CHUNK) - .all(|a| power(&ours[a..a + CHUNK]) > floor && power(&theirs[a..a + CHUNK]) > floor) - }; - (base..end).step_by(CHUNK).find(|&p| loud(p)).unwrap_or(base) -} - -pub(super) fn upsample_2x(core: &[f32]) -> Vec { - let mut out = Vec::with_capacity(core.len() * 2); - for (i, &s) in core.iter().enumerate() { - out.push(s); - out.push((s + core.get(i + 1).copied().unwrap_or(s)) * 0.5); - } - out -} - -pub(super) fn mono_prefix(planes: &[Vec]) -> usize { - let first = &planes[0]; - planes.get(1).map_or(first.len(), |second| { - first.iter().zip(second).position(|(a, b)| a != b).unwrap_or(first.len()) - }) -} - -/// Signed correlation of `ours` against `theirs` at one fixed `lag`, full -/// `WINDOW`-sized, anchored at the SAME `start` offset `best_lag_correlation` -/// uses internally -- used to test a hypothesized sign inversion by reusing -/// a KNOWN-good lag (and window) from a healthy channel instead of trusting -/// the (signed, positive-biased) search above to relocate both for a -/// channel that may be exactly inverted at that same lag. Anchoring must -/// match `best_lag_correlation`'s `start`, not sample 0 -- the two windows -/// otherwise cover different seconds of audio and are not comparable. -pub(super) fn correlation_at_lag(ours: &[f32], theirs: &[f32], lag: i64) -> f64 { - let start = window_start(ours, theirs); - let (oa, ob) = if lag >= 0 { - (start, start + lag as usize) - } else { - (start + (-lag) as usize, start) - }; - let n = WINDOW - .min(ours.len().saturating_sub(oa)) - .min(theirs.len().saturating_sub(ob)); - if n < 1024 { - return 0.0; - } - correlation(&ours[oa..oa + n], &theirs[ob..ob + n]) -} - -/// A first-order RC low-pass, run forward then backward to cancel its own -/// phase shift, giving a clean low-band/high-band split without pulling in a -/// filter design dependency for a diagnostic. -pub(super) fn lowpass(samples: &[f32], rate: u32, cutoff_hz: f64) -> Vec { - let dt = 1.0 / f64::from(rate); - let rc = 1.0 / (2.0 * std::f64::consts::PI * cutoff_hz); - let alpha = (dt / (rc + dt)) as f32; - let mut fwd = Vec::with_capacity(samples.len()); - let mut y = 0.0f32; - for &x in samples { - y += alpha * (x - y); - fwd.push(y); - } - let mut out = vec![0.0f32; fwd.len()]; - y = 0.0; - for i in (0..fwd.len()).rev() { - y += alpha * (fwd[i] - y); - out[i] = y; - } - out -} - -pub(super) fn highpass(samples: &[f32], rate: u32, cutoff_hz: f64) -> Vec { - let low = lowpass(samples, rate, cutoff_hz); - samples.iter().zip(&low).map(|(a, b)| a - b).collect() -} - -/// DIAGNOSTIC (round-13): cascades `lowpass` three times for a much sharper -/// stopband than the single first-order zero-phase filter the real -/// assertion uses -- checking whether the below-crossover bar is actually -/// being missed because of above-crossover energy leaking through the -/// gentle single-pass rolloff, not because the low band itself is wrong. -pub(super) fn sharp_lowpass(samples: &[f32], rate: u32, cutoff_hz: f64) -> Vec { - let a = lowpass(samples, rate, cutoff_hz); - let b = lowpass(&a, rate, cutoff_hz); - lowpass(&b, rate, cutoff_hz) -} - -/// Decisive-experiment diagnostic: correlates consecutive short (`WIN`) -/// windows of `o` against `t` (already coarsely aligned at `oa`/`ob`, e.g. -/// the winning full-file lag from `best_lag_correlation`), searching a small -/// local lag range per window. A per-frame continuity bug (dropped/duplicated -/// samples at access-unit boundaries) shows up as the winning local lag -/// DRIFTING linearly window to window; a bug that is not about concatenation -/// (wrong band mapping, spectral corruption) shows up as a roughly constant -/// lag with low/noisy correlation instead. -pub(super) fn windowed_lag_drift( - o: &[f32], - t: &[f32], - oa: usize, - ob: usize, - windows: usize, -) -> Vec<(i64, f64)> { - const WIN: usize = 4_096; - const LOCAL_LAG: i64 = 2_000; - let mut out = Vec::with_capacity(windows); - for w in 0..windows { - let base_o = oa + w * WIN; - let base_t = ob + w * WIN; - let mut best = (0i64, -1.0f64, 0.0f64); - for lag in -LOCAL_LAG..=LOCAL_LAG { - let ao = base_o as i64 + lag; - if ao < 0 { - continue; - } - let ao = ao as usize; - if ao + WIN > o.len() || base_t + WIN > t.len() { - continue; - } - let c = correlation(&o[ao..ao + WIN], &t[base_t..base_t + WIN]); - if c.abs() > best.1 { - best = (lag, c.abs(), c); - } - } - out.push((best.0, best.2)); - } - out -} - -/// DIAGNOSTIC (round-14, Task 2): per-QMF-band correlation over the -/// above-crossover region. No 64-band QMF Analysis exists standalone in this -/// crate, so this substitutes an STFT: magnitude-per-bin time series (one -/// value per hop) for `ours` and `theirs`, correlated bin by bin, then -/// averaged over the bins a QMF band of width `rate/128` Hz covers. A patch -/// boundary in `build_patches`' target ranges landing on a low-correlation -/// block convicts patch construction; an even spread across all HF bands -/// with the map otherwise right points at gain mapping instead. -/// Pearson correlation with no minimum-length floor -- `correlation`'s -/// 1024-sample floor exists for raw audio windows, not the much shorter -/// per-bin magnitude series this diagnostic correlates. -pub(super) fn magnitude_series_correlation(a: &[f32], b: &[f32]) -> f64 { - let n = a.len().min(b.len()); - if n < 2 { - return 0.0; - } - let (a, b) = (&a[..n], &b[..n]); - let ma = a.iter().map(|v| f64::from(*v)).sum::() / n as f64; - let mb = b.iter().map(|v| f64::from(*v)).sum::() / n as f64; - let ac: Vec = a.iter().map(|v| (f64::from(*v) - ma) as f32).collect(); - let bc: Vec = b.iter().map(|v| (f64::from(*v) - mb) as f32).collect(); - let num = dot(&ac, &bc); - let den = (dot(&ac, &ac) * dot(&bc, &bc)).sqrt(); - if den == 0.0 { 0.0 } else { num / den } -} - -pub(super) fn per_band_correlation(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, f64, f64)> { - const FFT_LEN: usize = 2048; - const HOP: usize = 1024; - let win = ec_dsp::Window::::sine(FFT_LEN); - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let bins = rfft.spectrum_len(); - let n = o.len().min(t.len()); - let hops = if n >= FFT_LEN { - (n - FFT_LEN) / HOP + 1 - } else { - 0 - }; - let mut o_mag = vec![Vec::with_capacity(hops); bins]; - let mut t_mag = vec![Vec::with_capacity(hops); bins]; - let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - for (src, dst) in [(o, &mut o_mag), (t, &mut t_mag)] { - for h in 0..hops { - let mut block = src[h * HOP..h * HOP + FFT_LEN].to_vec(); - win.apply(&mut block); - rfft.forward(&block, &mut spectrum); - for (b, c) in spectrum.iter().enumerate() { - dst[b].push(c.norm_sqr().sqrt()); - } - } - } - // QMF band width is `core_rate/128` where `core_rate` is the core - // decoder's own sample rate -- half the SBR extension/output rate for a - // plain (2x) HE-AAC stream -- not `output_rate/128`: measured against - // this file's own kx=14 crossover, the correlation break lands exactly - // at 14*(rate/256) Hz (2412 Hz), not at 14*(rate/128). - let band_hz = f64::from(rate) / 256.0; - let bin_hz = f64::from(rate) / FFT_LEN as f64; - let num_bands = (f64::from(rate) / 2.0 / band_hz).ceil() as usize; - let mut out = Vec::with_capacity(num_bands); - for band in 0..num_bands { - let lo = (band as f64 * band_hz / bin_hz).floor() as usize; - let hi = (((band + 1) as f64 * band_hz / bin_hz).ceil() as usize).min(bins); - if lo >= hi { - continue; - } - // round-16: correlating bin-by-bin then averaging the |corr| values - // was the bug -- a QMF band's bins share one slowly-varying envelope, - // but a tiny (sub-Hz) frequency offset between our filterbank and the - // reference's shifts energy between adjacent bins hop to hop, so each - // bin's OWN magnitude series decorrelates even though the band's - // total energy (what a QMF band's single subband signal actually - // carries) tracks the reference closely. Summing the bins' magnitudes - // into one band-energy series per side FIRST, then correlating that - // single pair, is robust to that intra-band leakage the way the - // per-bin version wasn't. - let hops_here = o_mag[lo].len().min(t_mag[lo].len()); - let mut o_band = vec![0.0f32; hops_here]; - let mut t_band = vec![0.0f32; hops_here]; - for b in lo..hi { - for h in 0..hops_here { - o_band[h] += o_mag[b][h]; - t_band[h] += t_mag[b][h]; - } - } - let c = magnitude_series_correlation(&o_band, &t_band); - if c.is_finite() { - out.push((band as f64 * band_hz, c.abs(), band as f64)); - } - } - out -} - -/// Per-band PCM-domain lag search (round-15 candidate-A discriminator): -/// `per_band_correlation`'s magnitude series has one sample per 1024-sample -/// STFT `HOP`, far coarser than a single QMF slot (64 samples) -- a stage -/// delay of one or a few slots between our HF path and the reference's would -/// vanish into that hop bucket instead of showing up as a lag. This instead -/// bandpasses `o`/`t` to each HF QMF band (the same zero-phase RC -/// `highpass`/`lowpass` primitives the crossover split already uses) and -/// searches PCM-sample lag directly, coarse-then-refine like -/// `best_lag_correlation`, so a consistent nonzero peak lag (especially a -/// multiple of 64 samples) convicts a per-stage time-alignment bug; lags -/// clustered at/near zero exonerate it in favour of a patch-map or -/// gain-mapping bug instead. Bands below kx (already known clean from -/// `per_band_correlation`) are skipped -- only HF bands (14..) are searched. -/// -/// CONVICTED (round-26, Task 1): `bandpass_offset_probe` correlated this same -/// `sharp_lowpass`/`sharp_highpass` cascade's band-k output against the -/// REFERENCE's band-(k+/-10) output for several HF `k` -- and the DOWNWARD -/// offset (band k-10, closer to the strongly-correlated below-crossover -/// content) read a HIGHER correlation than the nominal same-band pairing in -/// every HF band tried (e.g. band 34: same 0.937 vs -10 0.966). A filter -/// with real ~172Hz selectivity cannot produce that; a "sharp" 3x-cascaded -/// single-pole RC (~18dB/oct) at a 9kHz center still passes a wide swath -/// below its corner, so each "band k" reading here is really "everything up -/// to and including band k" -- cumulative correlation, dominated by the -/// genuinely-matching low band, which manufactures the smooth monotonic -/// decline earlier rounds chased. The reported LAG values are still valid -/// (a filter with no stopband selectivity still has a passband delay that -/// tracks the true alignment), but the reported per-band |corr| AMPLITUDE -/// here is NOT a trustworthy per-band figure -- use `per_band_phase`'s -/// cross-spectrum magnitude ratio (or the bin-level `bin_level_conviction`) -/// for that instead. -pub(super) fn per_band_lag_search(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, i64, f64)> { - const LAG_MAX: i64 = 1_024; - const COARSE_STEP: i64 = 8; - const COARSE: usize = 4_096; - const WIN: usize = 65_536; - let band_hz = f64::from(rate) / 256.0; - let num_bands = (f64::from(rate) / 2.0 / band_hz).ceil() as usize; - let n = o.len().min(t.len()); - let start = n.saturating_sub(WIN + LAG_MAX as usize).min(n / 4); - let mut out = Vec::new(); - for band in 14..num_bands { - let lo = (band as f64 * band_hz).max(1.0); - let hi = ((band + 1) as f64 * band_hz).min(f64::from(rate) / 2.0 - 1.0); - // A single-pole RC (the plain `highpass`/`lowpass` used for the - // crossover split) has too gentle a rolloff to isolate a ~172Hz-wide - // QMF band -- it leaks in the much stronger, well-correlated - // below-crossover energy, which was confirmed here as a smooth, - // frequency-monotonic false-high correlation with no matching cliff - // in `per_band_correlation`'s FFT-bin version of the same bands. - // `sharp_lowpass` (3x cascade, already used for the crossover-band - // debug check) plus its highpass complement sharpens the stopband - // enough that leakage from outside this one band stops dominating. - let sharp_highpass = |s: &[f32], cutoff: f64| -> Vec { - s.iter() - .zip(&sharp_lowpass(s, rate, cutoff)) - .map(|(a, b)| a - b) - .collect::>() - }; - let bp = |s: &[f32]| -> Vec { sharp_lowpass(&sharp_highpass(s, lo), rate, hi) }; - let ob = bp(o); - let tb = bp(t); - let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { - let (oa, ta) = if lag >= 0 { - (start, start + lag as usize) - } else { - (start + (-lag) as usize, start) - }; - if oa + len > ob.len() || ta + len > tb.len() { - None - } else { - Some((oa, ta)) - } - }; - let mut coarse = (0i64, -1.0f64, 0.0f64); - let mut lag = -LAG_MAX; - while lag <= LAG_MAX { - if let Some((oa, ta)) = slice_at(lag, COARSE) { - let c = correlation(&ob[oa..oa + COARSE], &tb[ta..ta + COARSE]); - if c.abs() > coarse.1 { - coarse = (lag, c.abs(), c); - } - } - lag += COARSE_STEP; - } - let mut best = coarse; - for lag in (coarse.0 - COARSE_STEP)..=(coarse.0 + COARSE_STEP) { - if let Some((oa, ta)) = slice_at(lag, WIN) { - let c = correlation(&ob[oa..oa + WIN], &tb[ta..ta + WIN]); - if c.abs() > best.1 { - best = (lag, c.abs(), c); - } - } - } - out.push((band as f64 * band_hz, best.0, best.2)); - } - out -} - -/// DIAGNOSTIC (round-16, Task 2): per-band phase offset and magnitude -/// transfer between `o` and `t`, over the SAME aligned window -/// `per_band_correlation` uses. Reuses the STFT-and-coherent-band-sum -/// machinery `per_band_correlation` was just fixed to use (sum the complex -/// spectrum, not just its magnitude, across a band's bins first, so a -/// per-bin sub-band redistribution doesn't wash out a genuine per-band -/// phase relationship) but keeps the complex value: the cross-spectrum -/// `sum_h O_band(h) * conj(T_band(h))` accumulated over every hop gives one -/// phasor per band whose angle is the (hop-weighted) phase offset between -/// `o` and `t` in that band and whose radius, once normalized by each side's -/// own energy, gives the magnitude transfer ratio. -pub(super) fn per_band_phase(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, f64, f64)> { - const FFT_LEN: usize = 2048; - const HOP: usize = 1024; - let win = ec_dsp::Window::::sine(FFT_LEN); - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let bins = rfft.spectrum_len(); - let n = o.len().min(t.len()); - let hops = if n >= FFT_LEN { - (n - FFT_LEN) / HOP + 1 - } else { - 0 - }; - let mut o_spec = vec![Vec::with_capacity(hops); bins]; - let mut t_spec = vec![Vec::with_capacity(hops); bins]; - let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - for (src, dst) in [(o, &mut o_spec), (t, &mut t_spec)] { - for h in 0..hops { - let mut block = src[h * HOP..h * HOP + FFT_LEN].to_vec(); - win.apply(&mut block); - rfft.forward(&block, &mut spectrum); - for (b, c) in spectrum.iter().enumerate() { - dst[b].push(*c); - } - } - } - let band_hz = f64::from(rate) / 256.0; - let bin_hz = f64::from(rate) / FFT_LEN as f64; - let num_bands = (f64::from(rate) / 2.0 / band_hz).ceil() as usize; - let mut out = Vec::with_capacity(num_bands); - for band in 0..num_bands { - let lo = (band as f64 * band_hz / bin_hz).floor() as usize; - let hi = (((band + 1) as f64 * band_hz / bin_hz).ceil() as usize).min(bins); - if lo >= hi || hops == 0 { - continue; - } - // Cross-spectrum accumulated PER BIN over hops, THEN summed across - // the band's bins -- not the reverse. Complex-summing the band's - // bins together at each hop first (as an earlier version of this - // function did) mixes in `O_b * conj(T_b')` cross terms for every - // b != b' pair once the per-hop products are formed; two adjacent - // FFT bins carry unrelated instantaneous phase (they are different - // frequencies), so those cross terms are pure noise that swamped the - // real per-bin coherence and produced an incoherent, near-random - // phase reading across exactly the HF bands `per_band_lag_search`'s - // direct PCM correlation shows are highly correlated at lag 0/-1. - let mut cross_re = 0.0f64; - let mut cross_im = 0.0f64; - let mut o_energy = 0.0f64; - let mut t_energy = 0.0f64; - for b in lo..hi { - for h in 0..hops { - let ob = o_spec[b][h]; - let tb = t_spec[b][h]; - let cross = ob * tb.conj(); - cross_re += f64::from(cross.re); - cross_im += f64::from(cross.im); - o_energy += - f64::from(ob.re) * f64::from(ob.re) + f64::from(ob.im) * f64::from(ob.im); - t_energy += - f64::from(tb.re) * f64::from(tb.re) + f64::from(tb.im) * f64::from(tb.im); - } - } - let phase = cross_im.atan2(cross_re); - let mag_ratio = if t_energy > 0.0 { - (o_energy / t_energy).sqrt() - } else { - 0.0 - }; - out.push((band as f64 * band_hz, phase, mag_ratio)); - } - out -} - -/// Round-26, Task 1: convicts (or acquits) `per_band_lag_search`'s bandpass. -/// Applies the SAME 3x-cascaded RC bandpass that diagnostic uses to `o` at -/// band `band` and to `t` at band `band` (same-band control), `band+offset` -/// and `band-offset`. A single-pole cascade's stopband is gentle enough that -/// "band k" may really pass "everything below f_k" -- if so, `o`-band-k and -/// `t`-band-(k+offset) still both carry the same dominant low-band leakage -/// and read highly correlated even though their nominal passbands don't -/// overlap at all, which convicts the filter, not the content. Below the SBR -/// crossover, genuinely narrowband-matching content should decorrelate -/// quickly as `offset` grows; a HF band that does NOT decorrelate at -/// `offset` bands away is evidence the filter has no real selectivity there. -pub(super) fn bandpass_offset_probe( - o: &[f32], - t: &[f32], - rate: u32, - band: usize, - offset: i64, -) -> (f64, f64, f64) { - let band_hz = f64::from(rate) / 256.0; - let bp_at = |s: &[f32], b: usize| -> Vec { - let lo = (b as f64 * band_hz).max(1.0); - let hi = ((b + 1) as f64 * band_hz).min(f64::from(rate) / 2.0 - 1.0); - let sharp_highpass = |s: &[f32], cutoff: f64| -> Vec { - s.iter() - .zip(&sharp_lowpass(s, rate, cutoff)) - .map(|(a, b)| a - b) - .collect::>() - }; - sharp_lowpass(&sharp_highpass(s, lo), rate, hi) - }; - let ob = bp_at(o, band); - let same_corr = correlation(&ob, &bp_at(t, band)); - let hi_band = band as i64 + offset; - let hi_corr = if hi_band >= 0 { - correlation(&ob, &bp_at(t, hi_band as usize)) - } else { - 0.0 - }; - let lo_band = band as i64 - offset; - let lo_corr = if lo_band >= 0 { - correlation(&ob, &bp_at(t, lo_band as usize)) - } else { - 0.0 - }; - (same_corr, hi_corr, lo_corr) -} - -/// Round-26, Task 2: bin-level (no filter, no cross-band summing) comparison -/// of `o` against `t` in the most energetic HF SBR bands at/above `min_band`, -/// three ways: DIRECT (bin i vs bin i), MIRROR (bin i vs the band's bins in -/// REVERSED order -- the signature of a QMF band-orientation/parity flip -/// between our own-design synthesis and the reference's convention: energies -/// right, direct corr ~0, phase random, exactly what round-14/16 observed), -/// and SHIFT (the whole `BINS_PER_SBR_BAND`-bin band matched one band up or -/// down -- the signature of a `build_patches` source-mapping offset). -/// Coherence per bin pair is -/// `|sum_h O(h)*conj(T(h))| / sqrt(sum_h|O(h)|^2 * sum_h|T(h)|^2)` (in -/// [0,1]; 1 = perfectly phase+magnitude locked, ~0 = unrelated), accumulated -/// over the SAME aligned hops `per_band_phase` uses, then averaged over the -/// band's 8 bins. Bands are picked by OUR OWN energy (the "real" HF content), -/// not the reference's, so a genuinely-silent HF band can't crowd out an -/// energetic one just because it happens to sort first. -pub(super) fn bin_level_conviction( - o: &[f32], - t: &[f32], - rate: u32, - min_band: usize, - top_n: usize, -) -> Vec<(f64, f64, f64, f64, f64, f64)> { - const FFT_LEN: usize = 2048; - const HOP: usize = 1024; - const BINS_PER_SBR_BAND: usize = FFT_LEN / 256; - let win = ec_dsp::Window::::sine(FFT_LEN); - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let bins = rfft.spectrum_len(); - let n = o.len().min(t.len()); - let hops = if n >= FFT_LEN { - (n - FFT_LEN) / HOP + 1 - } else { - 0 - }; - let mut o_spec = vec![Vec::with_capacity(hops); bins]; - let mut t_spec = vec![Vec::with_capacity(hops); bins]; - let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - for (src, dst) in [(o, &mut o_spec), (t, &mut t_spec)] { - for h in 0..hops { - let mut block = src[h * HOP..h * HOP + FFT_LEN].to_vec(); - win.apply(&mut block); - rfft.forward(&block, &mut spectrum); - for (b, c) in spectrum.iter().enumerate() { - dst[b].push(*c); - } - } - } - if hops == 0 { - return Vec::new(); - } - let band_hz = f64::from(rate) / 256.0; - let num_bands = bins / BINS_PER_SBR_BAND; - let coh = |ob: usize, tb: usize| -> f64 { - if ob >= bins || tb >= bins { - return 0.0; - } - let mut cross_re = 0.0f64; - let mut cross_im = 0.0f64; - let mut o_e = 0.0f64; - let mut t_e = 0.0f64; - for h in 0..hops { - let oc = o_spec[ob][h]; - let tc = t_spec[tb][h]; - let cross = oc * tc.conj(); - cross_re += f64::from(cross.re); - cross_im += f64::from(cross.im); - o_e += f64::from(oc.norm_sqr()); - t_e += f64::from(tc.norm_sqr()); - } - let den = (o_e * t_e).sqrt(); - if den > 0.0 { - (cross_re * cross_re + cross_im * cross_im).sqrt() / den - } else { - 0.0 - } - }; - // Rank HF bands by OUR OWN energy, so the energetic-band handful is - // chosen by real content, not by whatever the reference happens to carry. - let mut ranked: Vec<(usize, f64)> = (min_band..num_bands) - .map(|band| { - let lo = band * BINS_PER_SBR_BAND; - let hi = (lo + BINS_PER_SBR_BAND).min(bins); - let e: f64 = (lo..hi) - .flat_map(|b| o_spec[b].iter()) - .map(|c| f64::from(c.norm_sqr())) - .sum(); - (band, e) - }) - .collect(); - ranked.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); - let mut selected: Vec = ranked.into_iter().take(top_n).map(|(b, _)| b).collect(); - selected.sort_unstable(); - - let mut out = Vec::with_capacity(selected.len()); - for band in selected { - let lo = band * BINS_PER_SBR_BAND; - let hi = (lo + BINS_PER_SBR_BAND).min(bins); - if lo >= hi { - continue; - } - let mut energy = 0.0f64; - let mut direct = 0.0f64; - let mut mirror = 0.0f64; - let mut shift_up = 0.0f64; - let mut shift_down = 0.0f64; - let count = (hi - lo) as f64; - // `i` feeds several derived indices (mirror/shift math below), not a - // plain element access -- an iterator/enumerate rewrite would be - // less readable here, not more. - #[allow(clippy::needless_range_loop)] - for i in lo..hi { - energy += o_spec[i] - .iter() - .map(|c| f64::from(c.norm_sqr())) - .sum::(); - direct += coh(i, i); - mirror += coh(i, hi + lo - 1 - i); - shift_up += coh(i, i + BINS_PER_SBR_BAND); - shift_down += if i >= BINS_PER_SBR_BAND { - coh(i, i - BINS_PER_SBR_BAND) - } else { - 0.0 - }; - } - out.push(( - band as f64 * band_hz, - energy, - direct / count, - mirror / count, - shift_up / count, - shift_down / count, - )); - } - out -} - -/// Round-38, Task 1: sweeps a constant HF-only sample-domain lag `k*64` -/// (`k` in -8..=8; one QMF synthesis slot = 64 output samples) applied ONLY -/// to `o`'s STFT read position, holding `t` fixed at the globally-aligned -/// `ob` offset -- the low band (which drives the global `lag` this window is -/// already re-sliced to via `oa`/`ob`) is never touched by this probe. Per -/// bin, coherence is the SAME "direct" measure `bin_level_conviction` uses -/// (`|sum_h O(h)*conj(T(h))| / sqrt(sum_h|O(h)|^2 * sum_h|T(h)|^2)`), then -/// averaged uniformly over every bin in every HF SBR band from `min_band` -/// upward (both parity regions together, not just the top-energy handful) -- -/// a clear peak at k != 0 convicts a constant reference-vs-ours HF alignment -/// offset of that many QMF slots; a flat or zero-peaked curve refutes it. -pub(super) fn hf_lag_sweep( - o: &[f32], - t: &[f32], - oa: usize, - ob: usize, - n: usize, - min_band: usize, -) -> Vec<(i64, f64)> { - const FFT_LEN: usize = 2048; - const HOP: usize = 1024; - const BINS_PER_SBR_BAND: usize = FFT_LEN / 256; - const MAX_K: i64 = 8; - const SLOT: i64 = 64; - let win = ec_dsp::Window::::sine(FFT_LEN); - let probe_bins = ec_dsp::RealFft::::new(FFT_LEN).spectrum_len(); - let num_bands = probe_bins / BINS_PER_SBR_BAND; - let lo_bin = min_band * BINS_PER_SBR_BAND; - let hi_bin = (num_bands * BINS_PER_SBR_BAND).min(probe_bins); - if lo_bin >= hi_bin { - return Vec::new(); - } - let hops = if n >= FFT_LEN { - (n - FFT_LEN) / HOP + 1 - } else { - 0 - }; - if hops == 0 { - return Vec::new(); - } - let spec_of = |s: &[f32], start: usize| -> Vec>> { - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let mut spec = vec![Vec::with_capacity(hops); probe_bins]; - let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); probe_bins]; - for h in 0..hops { - let mut block = s[start + h * HOP..start + h * HOP + FFT_LEN].to_vec(); - win.apply(&mut block); - rfft.forward(&block, &mut spectrum); - for (b, c) in spectrum.iter().enumerate() { - spec[b].push(*c); - } - } - spec - }; - let mut out = Vec::with_capacity((2 * MAX_K + 1) as usize); - for k in -MAX_K..=MAX_K { - let shift = k * SLOT; - // Prefer shifting `o`'s read start (the literal ask), but `o` has no - // samples before its own decode origin -- when the global alignment - // already pins `oa` at (or near) 0, negative `k` has nowhere to read - // from on that side. `coh(o[x], t[y])` only depends on `x - y`, so - // shifting `t`'s read start the OPPOSITE direction instead - // (`ob - shift`) tests the exact same relative offset and `t` (the - // reference, decoded in full) always has the room. Only actually - // falls back for `o`-boundary-starved `k`; the un-shifted `t` path - // above is untouched for every `k` that fits within `o`. - let (o_start, t_start) = { - let os = oa as i64 + shift; - if os >= 0 && os as usize + n <= o.len() { - (os as usize, ob) - } else { - let ts = ob as i64 - shift; - if ts >= 0 && ts as usize + n <= t.len() { - (oa, ts as usize) - } else { - continue; - } - } - }; - let o_spec = spec_of(o, o_start); - let t_spec = spec_of(t, t_start); - let mut sum = 0.0f64; - let mut count = 0usize; - for b in lo_bin..hi_bin { - let mut cross_re = 0.0f64; - let mut cross_im = 0.0f64; - let mut o_e = 0.0f64; - let mut t_e = 0.0f64; - for h in 0..hops { - let oc = o_spec[b][h]; - let tc = t_spec[b][h]; - let cross = oc * tc.conj(); - cross_re += f64::from(cross.re); - cross_im += f64::from(cross.im); - o_e += f64::from(oc.norm_sqr()); - t_e += f64::from(tc.norm_sqr()); - } - let den = (o_e * t_e).sqrt(); - if den > 0.0 { - sum += (cross_re * cross_re + cross_im * cross_im).sqrt() / den; - count += 1; - } - } - out.push((k, if count > 0 { sum / count as f64 } else { 0.0 })); - } - out -} - -/// Round-40, Task 1: fine spectral-BIN shift scan on patched HF content. -/// `hf_lag_sweep` (round-38) only tested whole-QMF-slot SAMPLE-domain lags and -/// round-26's `bin_level_conviction` shift columns only tested whole -/// `BINS_PER_SBR_BAND` (8-bin) band shifts; neither ever tried a FEW-bin -/// spectral offset, which is what a half-QMF-band (~4-bin, ~86Hz at this -/// 2048-window scale) carrier convention mismatch on patched content would -/// look like -- it would leave envelope/magnitude tracking (and therefore -/// `hf_lag_sweep`'s k=0 peak) intact while destroying bin-level phase -/// coherence via linear phase drift across the band. The audio itself is -/// never shifted; only the bin INDEX used to pair `o` against `t` moves, by -/// `b` in -7..=+7 (deliberately short of the 8-bin whole-band-shift case -/// round-26 already ruled out). Reports mean direct coherence -/// (`bin_level_conviction`'s same `|sum_h O(h)*conj(T(h))| / -/// sqrt(sum|O|^2*sum|T|^2)` measure) separately over the even-gap [14,28) and -/// odd-gap [28,43) SBR-band regions -- the offset, if real, could differ by -/// patch parity given round-35/36's convention-fix history. A clear peak at -/// `b != 0` (region-dependent sign allowed) convicts a constant sub-band -/// frequency offset; a flat/0-peaked curve refutes it. -pub(super) fn hf_bin_shift_sweep( - o: &[f32], - t: &[f32], - _rate: u32, - region_lo_band: usize, - region_hi_band: usize, -) -> Vec<(i64, f64)> { - const FFT_LEN: usize = 2048; - const HOP: usize = 1024; - const BINS_PER_SBR_BAND: usize = FFT_LEN / 256; - const MAX_B: i64 = 7; - let win = ec_dsp::Window::::sine(FFT_LEN); - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let bins = rfft.spectrum_len(); - let n = o.len().min(t.len()); - let hops = if n >= FFT_LEN { - (n - FFT_LEN) / HOP + 1 - } else { - 0 - }; - if hops == 0 { - return Vec::new(); - } - let mut o_spec = vec![Vec::with_capacity(hops); bins]; - let mut t_spec = vec![Vec::with_capacity(hops); bins]; - let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - for (src, dst) in [(o, &mut o_spec), (t, &mut t_spec)] { - for h in 0..hops { - let mut block = src[h * HOP..h * HOP + FFT_LEN].to_vec(); - win.apply(&mut block); - rfft.forward(&block, &mut spectrum); - for (b, c) in spectrum.iter().enumerate() { - dst[b].push(*c); - } - } - } - let lo_bin = region_lo_band * BINS_PER_SBR_BAND; - let hi_bin = (region_hi_band * BINS_PER_SBR_BAND).min(bins); - let coh = |ob: usize, tb: usize| -> f64 { - if ob >= bins || tb >= bins { - return 0.0; - } - let mut cross_re = 0.0f64; - let mut cross_im = 0.0f64; - let mut o_e = 0.0f64; - let mut t_e = 0.0f64; - for h in 0..hops { - let oc = o_spec[ob][h]; - let tc = t_spec[tb][h]; - let cross = oc * tc.conj(); - cross_re += f64::from(cross.re); - cross_im += f64::from(cross.im); - o_e += f64::from(oc.norm_sqr()); - t_e += f64::from(tc.norm_sqr()); - } - let den = (o_e * t_e).sqrt(); - if den > 0.0 { - (cross_re * cross_re + cross_im * cross_im).sqrt() / den - } else { - 0.0 - } - }; - let mut out = Vec::with_capacity((2 * MAX_B + 1) as usize); - if lo_bin >= hi_bin { - return out; - } - for b in -MAX_B..=MAX_B { - let mut sum = 0.0f64; - let mut count = 0usize; - for i in lo_bin..hi_bin { - let oi = i as i64 + b; - if oi < 0 || oi as usize >= bins { - continue; - } - sum += coh(oi as usize, i); - count += 1; - } - out.push((b, if count > 0 { sum / count as f64 } else { 0.0 })); - } - out -} - -/// Round-40, Task 2: per-STFT-frame complex-gain fingerprint. For the same -/// top-energy HF bands `bin_level_conviction` picks (ranked by `o`'s own -/// energy), takes each band's single dominant bin (highest total `|O(h)|^2` -/// across hops) and reports the per-hop complex ratio `O(h)/T(h)` trajectory -/// -- `(magnitude, phase_radians)` per STFT frame -- for that bin across -/// every frame. Read afterward, not here: constant magnitude+phase is a pure -/// gain (would predict HIGH coherence, contradicting the low bin-coherence -/// measured elsewhere -- flags a measurement issue, not a real -/// transformation); phase advancing at a roughly fixed per-frame rate is a -/// genuine carrier frequency offset (the rate converts directly to Hz and -/// cross-checks `hf_bin_shift_sweep`'s peak); phase jumping specifically at -/// hop indices lining up with SBR envelope borders is a per-envelope -/// gain/phase re-seed; anything else (magnitude and phase both wandering with -/// no fixed rate) is genuinely uncorrelated content frame to frame. -pub(super) fn hf_complex_ratio_trajectory( - o: &[f32], - t: &[f32], - rate: u32, - min_band: usize, - top_n: usize, -) -> Vec<(f64, Vec<(f64, f64)>)> { - const FFT_LEN: usize = 2048; - const HOP: usize = 1024; - const BINS_PER_SBR_BAND: usize = FFT_LEN / 256; - let win = ec_dsp::Window::::sine(FFT_LEN); - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let bins = rfft.spectrum_len(); - let n = o.len().min(t.len()); - let hops = if n >= FFT_LEN { - (n - FFT_LEN) / HOP + 1 - } else { - 0 - }; - if hops == 0 { - return Vec::new(); - } - let mut o_spec = vec![Vec::with_capacity(hops); bins]; - let mut t_spec = vec![Vec::with_capacity(hops); bins]; - let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - for (src, dst) in [(o, &mut o_spec), (t, &mut t_spec)] { - for h in 0..hops { - let mut block = src[h * HOP..h * HOP + FFT_LEN].to_vec(); - win.apply(&mut block); - rfft.forward(&block, &mut spectrum); - for (b, c) in spectrum.iter().enumerate() { - dst[b].push(*c); - } - } - } - let band_hz = f64::from(rate) / 256.0; - let num_bands = bins / BINS_PER_SBR_BAND; - if min_band >= num_bands { - return Vec::new(); - } - let mut ranked: Vec<(usize, f64)> = (min_band..num_bands) - .map(|band| { - let lo = band * BINS_PER_SBR_BAND; - let hi = (lo + BINS_PER_SBR_BAND).min(bins); - let e: f64 = (lo..hi) - .flat_map(|b| o_spec[b].iter()) - .map(|c| f64::from(c.norm_sqr())) - .sum(); - (band, e) - }) - .collect(); - ranked.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); - let mut selected: Vec = ranked.into_iter().take(top_n).map(|(b, _)| b).collect(); - selected.sort_unstable(); - - let mut out = Vec::with_capacity(selected.len()); - for band in selected { - let lo = band * BINS_PER_SBR_BAND; - let hi = (lo + BINS_PER_SBR_BAND).min(bins); - if lo >= hi { - continue; - } - let dom = (lo..hi) - .max_by(|&a, &b| { - let ea: f64 = o_spec[a].iter().map(|c| f64::from(c.norm_sqr())).sum(); - let eb: f64 = o_spec[b].iter().map(|c| f64::from(c.norm_sqr())).sum(); - ea.partial_cmp(&eb).unwrap() - }) - .unwrap(); - // Complex division `O/T` done by hand (`ec_dsp::Complex` carries no - // `Div` impl -- it is a from-scratch minimal type, see fft.rs): `O * - // conj(T) / |T|^2`. - let traj: Vec<(f64, f64)> = (0..hops) - .map(|h| { - let oc = o_spec[dom][h]; - let tc = t_spec[dom][h]; - let t_e = f64::from(tc.norm_sqr()); - if t_e > 1e-12 { - let (or, oi) = (f64::from(oc.re), f64::from(oc.im)); - let (tr, ti) = (f64::from(tc.re), f64::from(tc.im)); - let rr = (or * tr + oi * ti) / t_e; - let ri = (oi * tr - or * ti) / t_e; - ((rr * rr + ri * ri).sqrt(), ri.atan2(rr)) - } else { - (0.0, 0.0) - } - }) - .collect(); - out.push((band as f64 * band_hz, traj)); - } - out -} - -/// Round-45, Task 3 THIRD WITNESS. Every prior round compared OURS against -/// the REFERENCE directly -- a pairwise measurement that can never say which -/// side is wrong when they disagree. This builds a reference-free model: a -/// pure "plain patch-copy + per-band energy match" reconstruction of HF -/// content from the REFERENCE's own LOW band (the one piece of ground truth -/// both decoders start from and our own low-band round-trip already measures -/// as near-identity), using the verified patch map -/// `[(0,14,14),(3,28,11),(0,39,3),(13,42,1)]` and this file's own established -/// `band_hz = rate/256`, `BINS_PER_SBR_BAND = FFT_LEN/256 = 8` convention -/// (round-14/16; NOT the `rate/128` scale the naive spec reading suggests -- -/// that value indexes the QMF/hybrid analysis grid, not this STFT-bin grid, -/// and using it here would be the "trap" that bit round-42 elsewhere in this -/// file. The patch map's own band numbers (14/28/39/42/43) only make sense -/// under the `/256` convention already used everywhere else in this file that -/// touches the patch map -- confirmed by construction, not re-derived here). -/// -/// For each HF band in `[14, 43)` and each STFT hop, the simulated bins are -/// the REFERENCE's low-band bins at the patch-mapped source position, then -/// RESCALED per (band, hop) to match the comparison side's own energy in -/// that band/hop -- two separate rescalings, "sim-normalized-to-ours" and -/// "sim-normalized-to-reference" -- so the only thing left for the -/// coherence measure to see is phase/content structure, not a gain -/// difference (the gain question is already closed by prior rounds). Returns -/// `(band, coherence)` pairs for sim-vs-reference and sim-vs-ours. -type BandCoherenceTable = Vec<(usize, f64)>; - -pub(super) fn hf_patch_simulator(o: &[f32], t: &[f32], rate: u32) -> (BandCoherenceTable, BandCoherenceTable) { - const FFT_LEN: usize = 2048; - const HOP: usize = 1024; - const BINS_PER_SBR_BAND: usize = FFT_LEN / 256; - const PATCHES: [(usize, usize, usize); 4] = [(0, 14, 14), (3, 28, 11), (0, 39, 3), (13, 42, 1)]; - let _ = rate; // band Hz not needed here; caller converts band index itself. - let win = ec_dsp::Window::::sine(FFT_LEN); - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let bins = rfft.spectrum_len(); - let n = o.len().min(t.len()); - let hops = if n >= FFT_LEN { - (n - FFT_LEN) / HOP + 1 - } else { - 0 - }; - if hops == 0 { - return (Vec::new(), Vec::new()); - } - let mut o_spec = vec![Vec::with_capacity(hops); bins]; - let mut t_spec = vec![Vec::with_capacity(hops); bins]; - let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - for (src, dst) in [(o, &mut o_spec), (t, &mut t_spec)] { - for h in 0..hops { - let mut block = src[h * HOP..h * HOP + FFT_LEN].to_vec(); - win.apply(&mut block); - rfft.forward(&block, &mut spectrum); - for (b, c) in spectrum.iter().enumerate() { - dst[b].push(*c); - } - } - } - // Plain patch copy: sim's target bins, per hop, are the reference's - // source bins, unmodified (no phase adjustment) -- modelling exactly the - // "plain patch+gain" hypothesis the charter names. - let mut sim_spec = vec![vec![ec_dsp::Complex::new(0.0f32, 0.0); hops]; bins]; - for (src, tgt, width) in PATCHES { - let src_lo = src * BINS_PER_SBR_BAND; - let tgt_lo = tgt * BINS_PER_SBR_BAND; - let len = width * BINS_PER_SBR_BAND; - for i in 0..len { - if src_lo + i >= bins || tgt_lo + i >= bins { - continue; - } - for h in 0..hops { - sim_spec[tgt_lo + i][h] = t_spec[src_lo + i][h]; - } - } - } - let num_bands = bins / BINS_PER_SBR_BAND; - let top_band = 43.min(num_bands); - // Per (band, hop) rescale of `sim` to match `target`'s energy in that - // band/hop, then the same magnitude-of-summed-cross-spectrum coherence - // measure every prior round's `coh` helper uses (already scale-invariant - // to a CONSTANT gain across hops; this rescale additionally removes a - // gain that varies hop-to-hop, isolating phase/content agreement). - let band_coherence = |target: &[Vec>]| -> Vec<(usize, f64)> { - (14..top_band) - .map(|band| { - let lo = band * BINS_PER_SBR_BAND; - let hi = (lo + BINS_PER_SBR_BAND).min(bins); - let mut cross_re = 0.0f64; - let mut cross_im = 0.0f64; - let mut sim_e = 0.0f64; - let mut tgt_e = 0.0f64; - for h in 0..hops { - let se: f64 = (lo..hi).map(|b| f64::from(sim_spec[b][h].norm_sqr())).sum(); - let te: f64 = (lo..hi).map(|b| f64::from(target[b][h].norm_sqr())).sum(); - if se <= 1e-20 || te <= 0.0 { - continue; - } - let scale = (te / se).sqrt() as f32; - for b in lo..hi { - let sc = sim_spec[b][h].scale(scale); - let tc = target[b][h]; - let cross = sc * tc.conj(); - cross_re += f64::from(cross.re); - cross_im += f64::from(cross.im); - sim_e += f64::from(sc.norm_sqr()); - tgt_e += f64::from(tc.norm_sqr()); - } - } - let den = (sim_e * tgt_e).sqrt(); - let coh = if den > 0.0 { - (cross_re * cross_re + cross_im * cross_im).sqrt() / den - } else { - 0.0 - }; - (band, coh) - }) - .collect() - }; - (band_coherence(&t_spec), band_coherence(&o_spec)) -} - -/// (Round-46) The QMF-domain exact version of [`hf_patch_simulator`]: same -/// plain-patch+gain model, same verified patch map, but read through the -/// crate's own [`ec_aac::sbr_qmf::HfAnalysis`] (64-band, `rate/128`-wide -/// bands -- exactly the patch map's own band unit) instead of an 8-bin-wide -/// STFT approximation, closing the "too crude to reach a verdict" gap -/// round-45's STFT simulator hit. Validated by -/// `hf_analysis_self_consistency_control` in `sbr_qmf.rs` before use here -/// (own-band plurality + self-coherence + round-trip control). Same -/// per-(band, window) energy-rescale-then-coherence method as the STFT -/// version, windowed over `WIN` consecutive QMF slots (`WIN * SYNTHESIS_BANDS -/// = 1024` samples, matching the STFT version's hop) so the rescale has -/// enough samples to estimate energy from, not so many that within-window -/// phase drift is averaged away. -pub(super) const QMF_PATCHES: [(usize, usize, usize); 4] = [(0, 14, 14), (3, 28, 11), (0, 39, 3), (13, 42, 1)]; - -/// Shared build step for the QMF-domain third-witness instruments: both -/// PCMs read through [`ec_aac::sbr_qmf::HfAnalysis`], plus the plain -/// patch-copy simulation of the reference's own low band, all in the -/// `(band, slot)` domain. Returns `(ours, reference, sim)`. -/// `[band][slot]` complex QMF-domain series. -type QmfBandSlots = Vec>>; - -pub(super) fn qmf_domain_spec(o: &[f32], t: &[f32]) -> (QmfBandSlots, QmfBandSlots, QmfBandSlots) { - use ec_aac::sbr_qmf::{HfAnalysis, SYNTHESIS_BANDS}; - let n = o.len().min(t.len()); - let slots = n / SYNTHESIS_BANDS; - let analyze = |pcm: &[f32]| -> QmfBandSlots { - let mut ana = HfAnalysis::new(); - let mut out: QmfBandSlots = (0..SYNTHESIS_BANDS) - .map(|_| Vec::with_capacity(slots)) - .collect(); - for s in 0..slots { - let mut chunk = [0.0f32; SYNTHESIS_BANDS]; - chunk.copy_from_slice(&pcm[s * SYNTHESIS_BANDS..(s + 1) * SYNTHESIS_BANDS]); - let sub = ana.process_slot(&chunk); - for (b, c) in sub.iter().enumerate() { - out[b].push(*c); - } - } - out - }; - let o_spec = analyze(o); - let t_spec = analyze(t); - // Plain patch copy: sim's target-band slot series is the reference's - // source-band slot series, unmodified. - let mut sim_spec = vec![vec![ec_dsp::Complex::new(0.0f64, 0.0); slots]; SYNTHESIS_BANDS]; - for (src, tgt, width) in QMF_PATCHES { - for i in 0..width { - if src + i >= SYNTHESIS_BANDS || tgt + i >= SYNTHESIS_BANDS { - continue; - } - sim_spec[tgt + i] = t_spec[src + i].clone(); - } - } - (o_spec, t_spec, sim_spec) -} - -/// Returns `(sim-vs-reference, sim-vs-ours, ours-vs-reference)` -- the last -/// pairing (round-48, Task 4a) is the ultimate content-match witness: how -/// well our actual HF QMF content agrees with the reference's, with no -/// simulator in between. -pub(super) fn hf_patch_simulator_qmf( - o: &[f32], - t: &[f32], -) -> (BandCoherenceTable, BandCoherenceTable, BandCoherenceTable) { - use ec_aac::sbr_qmf::SYNTHESIS_BANDS; - const WIN: usize = 16; - let (o_spec, t_spec, sim_spec) = qmf_domain_spec(o, t); - let slots = sim_spec.first().map(Vec::len).unwrap_or(0); - if slots < WIN { - return (Vec::new(), Vec::new(), Vec::new()); - } - let top_band = 43.min(SYNTHESIS_BANDS); - let windows = slots / WIN; - let band_coherence = |source: &[Vec>], - target: &[Vec>]| - -> Vec<(usize, f64)> { - (14..top_band) - .map(|band| { - let mut cross_re = 0.0f64; - let mut cross_im = 0.0f64; - let mut sim_e = 0.0f64; - let mut tgt_e = 0.0f64; - for w in 0..windows { - let lo = w * WIN; - let hi = lo + WIN; - let se: f64 = source[band][lo..hi] - .iter() - .map(|c| c.re * c.re + c.im * c.im) - .sum(); - let te: f64 = target[band][lo..hi] - .iter() - .map(|c| c.re * c.re + c.im * c.im) - .sum(); - if se <= 1e-20 || te <= 0.0 { - continue; - } - let scale = (te / se).sqrt(); - for i in lo..hi { - let sc = source[band][i].scale(scale); - let tc = target[band][i]; - let cross = sc * tc.conj(); - cross_re += cross.re; - cross_im += cross.im; - sim_e += sc.re * sc.re + sc.im * sc.im; - tgt_e += tc.re * tc.re + tc.im * tc.im; - } - } - let den = (sim_e * tgt_e).sqrt(); - let coh = if den > 0.0 { - (cross_re * cross_re + cross_im * cross_im).sqrt() / den - } else { - 0.0 - }; - (band, coh) - }) - .collect() - }; - ( - band_coherence(&sim_spec, &t_spec), - band_coherence(&sim_spec, &o_spec), - band_coherence(&o_spec, &t_spec), - ) -} - -/// (Round-46, Task 2) Per-band 2nd-order complex prediction transfer fit -/// between the plain-copy sim series and the REFERENCE's own HF series for -/// that band: `ref[l] ~= a0*sim[l] + a1*sim[l-1] + a2*sim[l-2]`, least -/// squares over the whole file -- the same shape `sbr_hf::generate`'s own -/// chirp filter has (`y = residual + ca1*y[n-1] + ca2*y[n-2]`, an IIR rather -/// than this FIR, but both are order-2 linear reshapings of the same copied -/// content), so the fitted taps are directly comparable in spirit to our own -/// bandwidth-expanded LPC coefficients: near `(1,0,0)` means "reference is -/// also just a plain copy here", taps with real weight on lag 1/2 mean a -/// genuine order-2 transformation is present. -/// `target_is_ours`: fit against our own decode (`o_spec`) instead of the -/// reference's (`t_spec`) -- same `sim` (built from the reference's own low -/// band either way), so the two calls' fitted taps are directly comparable: -/// what transform sim would need to become ours vs. to become the reference. -#[allow(clippy::needless_range_loop)] // fixed 3x3 complex Gaussian elimination, index math throughout -pub(super) fn hf_patch_transfer_fit( - o: &[f32], - t: &[f32], - bands: &[usize], - target_is_ours: bool, -) -> Vec<(usize, [ec_dsp::Complex; 3])> { - let (o_spec, t_spec, sim_spec) = qmf_domain_spec(o, t); - let target_spec = if target_is_ours { &o_spec } else { &t_spec }; - let mut results = Vec::new(); - for &band in bands { - if band >= sim_spec.len() { - continue; - } - let sim_raw = &sim_spec[band]; - let ref_raw = &target_spec[band]; - let len = sim_raw.len().min(ref_raw.len()); - if len < 8 { - continue; - } - // Normalize each series to unit RMS first: the raw QMF-domain - // amplitudes differ by orders of magnitude (sim carries no envelope - // gain, `ref` does), which would otherwise make every fitted - // coefficient collapse toward the gain ratio rather than the - // fraction-of-`ref`-explained-by-which-lag this fit is meant to - // read off. - let rms = |s: &[ec_dsp::Complex]| -> f64 { - (s[..len] - .iter() - .map(|c| c.re * c.re + c.im * c.im) - .sum::() - / len as f64) - .sqrt() - .max(1e-300) - }; - let sim_scale = 1.0 / rms(sim_raw); - let ref_scale = 1.0 / rms(ref_raw); - let sim: Vec> = - sim_raw[..len].iter().map(|c| c.scale(sim_scale)).collect(); - let refb: Vec> = - ref_raw[..len].iter().map(|c| c.scale(ref_scale)).collect(); - let sim = &sim[..]; - let refb = &refb[..]; - // Complex 3x3 Hermitian normal equations: r[i][j] = sum sim[l-i] conj(sim[l-j]), - // p[i] = sum sim[l-i] conj(ref[l])... solved as R^T a = p with R real basis in - // complex arithmetic (Gaussian elimination with partial pivoting by magnitude). - let mut r = [[ec_dsp::Complex::new(0.0f64, 0.0); 3]; 3]; - let mut p = [ec_dsp::Complex::new(0.0f64, 0.0); 3]; - for l in 2..len { - let s = [sim[l], sim[l - 1], sim[l - 2]]; - let y = refb[l]; - for i in 0..3 { - p[i] = p[i] + s[i].conj() * y; - for j in 0..3 { - r[i][j] = r[i][j] + s[i].conj() * s[j]; - } - } - } - // Gaussian elimination on the augmented [r | p] system. - let mut aug = r; - let mut rhs = p; - for col in 0..3 { - let mut piv = col; - let mut best = aug[col][col].norm_sqr(); - for row in (col + 1)..3 { - let m = aug[row][col].norm_sqr(); - if m > best { - best = m; - piv = row; - } - } - if best < 1e-24 { - continue; - } - aug.swap(col, piv); - rhs.swap(col, piv); - let inv = aug[col][col].conj().scale(1.0 / best); - for row in (col + 1)..3 { - let f = aug[row][col] * inv; - for k in 0..3 { - aug[row][k] = aug[row][k] - f * aug[col][k]; - } - rhs[row] = rhs[row] - f * rhs[col]; - } - } - let mut a = [ec_dsp::Complex::new(0.0f64, 0.0); 3]; - for row in (0..3).rev() { - let mut acc = rhs[row]; - for k in (row + 1)..3 { - acc = acc - aug[row][k] * a[k]; - } - let d = aug[row][row]; - a[row] = if d.norm_sqr() > 1e-24 { - acc * d.conj().scale(1.0 / d.norm_sqr()) - } else { - ec_dsp::Complex::new(0.0, 0.0) - }; - } - results.push((band, a)); - } - results -} - -/// A file discovered under the user's own media directories: its path, which -/// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the -/// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used -/// only to draw the below/above-crossover line for localization, not as a -/// spec-exact `kx` band edge. -pub(super) struct Candidate { - pub(super) path: PathBuf, - /// Which AAC stream, 0-based among AAC streams only -- what - /// `extract_from` selects out of the demuxer's stream list. - pub(super) aac_stream: usize, - /// The same stream's absolute ffprobe/ffmpeg index -- what `-map` - /// selects, and not the same number when the container also carries a - /// non-AAC audio track before it (FMJ's mkv has an AC-3 track between its - /// two AAC ones). - pub(super) ffmpeg_stream: usize, - pub(super) crossover_hz: f64, -} - -/// Round-20 residual/side-info correlation. Builds a (frame x band) residual -/// ENERGY grid at AU-aligned 2048-output-sample blocks -- a plain (2x) SBR -/// stream reconstructs exactly one 2048-sample block per core access unit, -/// so `n`-th block lines up 1:1 with `ec_aac::sbr_sideinfo_log()`'s `frame` -/// counter -- then cross-tabulates that grid against every side-info feature -/// the log carries. FFT is linear, so `FFT(o_block) - FFT(t_block) == -/// FFT(o_block - t_block)`: the residual's own per-bin energy is obtained -/// directly from each side's spectrum without ever materializing a -/// time-domain residual array. `band` is the same `rate/256`-wide index -/// domain `per_band_correlation`/`tables.kx`/`f_high`/`f_noise` all already -/// share (measured in round-14/round-16), so a side-info row's `f_high`/ -/// `f_noise` boundaries index straight into this grid's bins with no -/// rescaling. -pub(super) fn residual_sideinfo_analysis(o: &[f32], t: &[f32], oa: usize, ob: usize, rate: u32, ch: usize) { - let rows: Vec<_> = ec_aac::sbr_sideinfo_log() - .into_iter() - .filter(|r| r.ch == ch) - .collect(); - if rows.is_empty() { - eprintln!(" ch{ch} SIDEINFO: no rows logged (set EC_AAC_SBR_SIDEINFO_DEBUG too)"); - return; - } - const FFT_LEN: usize = 2048; - let win = ec_dsp::Window::::sine(FFT_LEN); - let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); - let bins = rfft.spectrum_len(); - let mut o_spec = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - let mut t_spec = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; - - // Per-frame residual bin-energy vector (index = `band`, per-bin, not yet - // grouped) and, alongside it, a per-bin peakiness ratio (own-bin energy - // vs its 3-bin neighbourhood mean) used by the whiteness check. - let mut energy: std::collections::HashMap> = std::collections::HashMap::new(); - for row in &rows { - if energy.contains_key(&row.frame) { - continue; - } - let base_o = oa + row.frame * FFT_LEN; - let base_t = ob + row.frame * FFT_LEN; - if base_o + FFT_LEN > o.len() || base_t + FFT_LEN > t.len() { - continue; - } - let mut ob_win = o[base_o..base_o + FFT_LEN].to_vec(); - let mut tb_win = t[base_t..base_t + FFT_LEN].to_vec(); - win.apply(&mut ob_win); - win.apply(&mut tb_win); - rfft.forward(&ob_win, &mut o_spec); - rfft.forward(&tb_win, &mut t_spec); - let e: Vec = o_spec - .iter() - .zip(&t_spec) - .map(|(a, b)| f64::from((*a - *b).norm_sqr())) - .collect(); - energy.insert(row.frame, e); - } - if energy.is_empty() { - eprintln!(" ch{ch} SIDEINFO: no frame overlapped the aligned window"); - return; - } - // SBR-band-index units (`kx`/`k2`/`f_high`/`f_noise`) are `band_hz = - // rate/256` wide; FFT bins here are `bin_hz = rate/FFT_LEN` wide. The - // ratio `band_hz/bin_hz = FFT_LEN/256 = 8` is rate-independent, matching - // `per_band_correlation`'s own `lo = band*band_hz/bin_hz` conversion -- - // one SBR band is exactly 8 of these FFT bins, never 1. - const BINS_PER_SBR_BAND: usize = FFT_LEN / 256; - let band_range = |lo: i64, hi: i64| -> (usize, usize) { - let lo = ((lo.max(0) as usize) * BINS_PER_SBR_BAND).min(bins); - let hi = (((hi.max(0) as usize) * BINS_PER_SBR_BAND).max(lo + 1)).min(bins); - (lo, hi) - }; - let sum_range = |e: &[f64], lo: usize, hi: usize| -> f64 { e[lo..hi].iter().sum() }; - - // --- predictor 1: add_harmonic bands (and their immediate neighbours) --- - let (mut harm_sum, mut harm_n) = (0.0f64, 0usize); - let (mut noharm_sum, mut noharm_n) = (0.0f64, 0usize); - let (mut adj_sum, mut adj_n) = (0.0f64, 0usize); - for row in &rows { - let Some(e) = energy.get(&row.frame) else { - continue; - }; - let Some(harm) = &row.add_harmonic else { - continue; - }; - for (i, &flag) in harm.iter().enumerate() { - let (lo, hi) = match (row.f_high.get(i), row.f_high.get(i + 1)) { - (Some(&a), Some(&b)) => band_range(a, b), - _ => continue, - }; - let s = sum_range(e, lo, hi); - let cells = (hi - lo).max(1); - if flag != 0 { - harm_sum += s / cells as f64; - harm_n += 1; - } else { - noharm_sum += s / cells as f64; - noharm_n += 1; - } - // adjacent band (off-by-one probe): the band one slot up. - if flag != 0 - && let (Some(&a2), Some(&b2)) = (row.f_high.get(i + 1), row.f_high.get(i + 2)) - { - let (lo2, hi2) = band_range(a2, b2); - adj_sum += sum_range(e, lo2, hi2) / (hi2 - lo2).max(1) as f64; - adj_n += 1; - } - } - } - - // --- predictor 2: envelope time borders vs envelope interior --- - // Envelope borders are in `t_env` (pre-`RATE`-scale envelope time-slot - // units); one unit is `RATE(2) * SYNTHESIS_BANDS(64) = 128` output - // samples. Time-domain residual energy (broadband, not per-band) near - // (+/-64 samples) each INTERIOR border vs the envelope's own midpoint. - let (mut border_sum, mut border_n) = (0.0f64, 0usize); - let (mut mid_sum, mut mid_n) = (0.0f64, 0usize); - let sample_energy = |global: usize| -> Option { - let oi = oa + global; - let ti = ob + global; - if oi >= o.len() || ti >= t.len() { - return None; - } - let d = f64::from(o[oi]) - f64::from(t[ti]); - Some(d * d) - }; - for row in &rows { - let frame_base = row.frame * FFT_LEN; - for w in row.t_env.windows(2) { - let (b0, b1) = (w[0] * 128, w[1] * 128); - if b1 <= b0 { - continue; - } - let mid = (b0 + b1) / 2; - for d in -64i64..=64 { - let s0 = b0 + d; - if s0 >= 0 - && let Some(v) = sample_energy(frame_base + s0 as usize) - { - border_sum += v; - border_n += 1; - } - } - for off in 0..(b1 - b0).min(256) { - let s = mid - (b1 - b0).min(256) / 2 + off; - if s >= 0 - && (s - mid).abs() > 64 - && let Some(v) = sample_energy(frame_base + s as usize) - { - mid_sum += v; - mid_n += 1; - } - } - } - } - - // --- predictor 3: hold-state (apply_last) vs fresh-payload frames --- - let (mut hold_sum, mut hold_n) = (0.0f64, 0usize); - let (mut fresh_sum, mut fresh_n) = (0.0f64, 0usize); - for row in &rows { - let Some(e) = energy.get(&row.frame) else { - continue; - }; - let total: f64 = e.iter().sum(); - if row.source == "hold" { - hold_sum += total; - hold_n += 1; - } else { - fresh_sum += total; - fresh_n += 1; - } - } - - // --- predictor 4: frame immediately after a coupling flip --- - let (mut flip_sum, mut flip_n) = (0.0f64, 0usize); - let (mut noflip_sum, mut noflip_n) = (0.0f64, 0usize); - let mut sorted = rows.clone(); - sorted.sort_by_key(|r| r.frame); - for pair in sorted.windows(2) { - let (prev, cur) = (&pair[0], &pair[1]); - let Some(e) = energy.get(&cur.frame) else { - continue; - }; - let total: f64 = e.iter().sum(); - if cur.coupling != prev.coupling { - flip_sum += total; - flip_n += 1; - } else { - noflip_sum += total; - noflip_n += 1; - } - } - - // --- predictor 5: residual scaling per invf_mode level, per noise band --- - let mut per_level = [(0.0f64, 0usize); 4]; - for row in &rows { - let Some(e) = energy.get(&row.frame) else { - continue; - }; - for (i, &mode) in row.invf_mode.iter().enumerate() { - let (lo, hi) = match (row.f_noise.get(i), row.f_noise.get(i + 1)) { - (Some(&a), Some(&b)) => band_range(a, b), - _ => continue, - }; - let cells = (hi - lo).max(1); - let level = (mode as usize).min(3); - per_level[level].0 += sum_range(e, lo, hi) / cells as f64; - per_level[level].1 += 1; - } - } - - // --- predictor 6: residual whiteness within HF bands (peakiness) --- - // mean(max-bin / mean-bin) over each HF band each frame -- near 1 reads - // noise-like, large reads tonal (one bin dominating the band). - let (mut peak_sum, mut peak_n) = (0.0f64, 0usize); - for row in &rows { - let Some(e) = energy.get(&row.frame) else { - continue; - }; - let (lo, hi) = band_range(row.kx, row.k2); - for group_lo in (lo..hi.min(e.len())).step_by(4) { - let group_hi = (group_lo + 4).min(hi).min(e.len()); - if group_hi <= group_lo { - continue; - } - let slice = &e[group_lo..group_hi]; - let mean = slice.iter().sum::() / slice.len() as f64; - let max = slice.iter().cloned().fold(0.0f64, f64::max); - if mean > 0.0 { - peak_sum += max / mean; - peak_n += 1; - } - } - } - - // Heat summary: the full (frame x band) grid collapsed to two profiles - // (mean over bands per 50-frame group; mean over frames per 8-band - // group) -- the full grid itself is `energy`, ~489 x ~90 cells, too - // large to print usefully every run. - let mut frame_keys: Vec = energy.keys().cloned().collect(); - frame_keys.sort_unstable(); - println!( - " ch{ch} RESIDUAL HEAT temporal profile (50-frame groups, mean energy over all bands):" - ); - for chunk in frame_keys.chunks(50) { - let (mut s, mut n) = (0.0f64, 0usize); - for &f in chunk { - if let Some(e) = energy.get(&f) { - s += e.iter().sum::(); - n += e.len(); - } - } - if n > 0 { - println!( - " frames {}..{}: {:.6e}", - chunk[0], - chunk[chunk.len() - 1], - s / n as f64 - ); - } - } - // Nyquist bin: FFT_LEN/2 (the real bin at the Nyquist frequency, always - // present in an RFFT of an even-length transform) -- bins past it are - // this RFFT's own redundant/unused tail, never real spectrum. - let bin_hz = f64::from(rate) / FFT_LEN as f64; - let nyquist_bin = (FFT_LEN / 2 + 1).min(bins); - println!( - " ch{ch} RESIDUAL HEAT spectral profile ({BINS_PER_SBR_BAND}-bin == 1 SBR-band groups, mean energy over all frames):" - ); - for group_lo in (0..nyquist_bin).step_by(BINS_PER_SBR_BAND) { - let group_hi = (group_lo + BINS_PER_SBR_BAND).min(nyquist_bin); - let (mut s, mut n) = (0.0f64, 0usize); - for e in energy.values() { - for &v in &e[group_lo..group_hi] { - s += v; - n += 1; - } - } - if n > 0 { - println!( - " {:>6.0}Hz..{:>6.0}Hz (band {}): {:.6e}", - group_lo as f64 * bin_hz, - group_hi as f64 * bin_hz, - group_lo / BINS_PER_SBR_BAND, - s / n as f64 - ); - } - } - - let avg = |s: f64, n: usize| if n > 0 { s / n as f64 } else { f64::NAN }; - println!( - " ch{ch} RESIDUAL/SIDEINFO ranked predictors ({} frames, {} rows):", - energy.len(), - rows.len() - ); - println!( - " 1. add_harmonic band mean energy: flagged={:.6e} (n={harm_n}) unflagged={:.6e} (n={noharm_n}) ratio={:.3} | adjacent(+1)-band-of-flagged={:.6e} (n={adj_n})", - avg(harm_sum, harm_n), - avg(noharm_sum, noharm_n), - avg(harm_sum, harm_n) / avg(noharm_sum, noharm_n).max(1e-300), - avg(adj_sum, adj_n) - ); - println!( - " 2. envelope time-border (+/-64 smp) mean energy={:.6e} (n={border_n}) vs interior={:.6e} (n={mid_n}) ratio={:.3}", - avg(border_sum, border_n), - avg(mid_sum, mid_n), - avg(border_sum, border_n) / avg(mid_sum, mid_n).max(1e-300) - ); - println!( - " 3. hold-state(apply_last) frame total energy={:.6e} (n={hold_n}) vs fresh-payload={:.6e} (n={fresh_n}) ratio={:.3}", - avg(hold_sum, hold_n), - avg(fresh_sum, fresh_n), - avg(hold_sum, hold_n) / avg(fresh_sum, fresh_n).max(1e-300) - ); - println!( - " 4. frame-after-coupling-flip total energy={:.6e} (n={flip_n}) vs no-flip={:.6e} (n={noflip_n}) ratio={:.3}", - avg(flip_sum, flip_n), - avg(noflip_sum, noflip_n), - avg(flip_sum, flip_n) / avg(noflip_sum, noflip_n).max(1e-300) - ); - println!(" 5. invf_mode level -> mean per-band residual energy:"); - for (level, (s, n)) in per_level.iter().enumerate() { - println!(" level {level}: {:.6e} (n={n})", avg(*s, *n)); - } - println!( - " 6. HF residual peakiness (max/mean over 4-bin groups): {:.3} (n={peak_n}) [near 1 = noise-like, larger = tonal]", - avg(peak_sum, peak_n) - ); -} - -/// Decodes one stream's core (AAC-LC) layer only, with SBR forced off at the -/// `AudioSpecificConfig` level -- `sbr_present: false` never arms -/// `BlockDecoder::sbr`, so `decode.rs`'s FIL-element branch never calls into -/// `sbr_chain` at all and `out` (the raw core `channel_stream` decode) is -/// handed back untouched, at the core rate. This isolates whether a -/// real-file break lives in the LC core or in the SBR chain bolted onto it. -pub(super) fn our_decode_core_only(path: &Path, stream_index: usize) -> Option<(Vec>, u32)> { - let (asc, aus) = extract_aac_track(path, stream_index)?; - let mut cfg = parse_audio_specific_config(&asc).ok()?; - cfg.sbr_present = false; - cfg.ps_present = false; - cfg.extension_sample_rate = None; - let core_rate = cfg.sample_rate; - // `core_only` also stops implicit SBR: a bare `with_config` re-arms it for the - // SBR payload this stream carries, and the "core" output would be the full chain. - let mut decoder = AacDecoder::with_config(cfg).core_only(); - let mut planes: Vec> = Vec::new(); - let dump = std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok(); - for (au_idx, au) in aus.iter().enumerate() { - let Ok(frame) = decoder.decode(au, None) else { - continue; - }; - let ch = usize::from(frame.channels); - if ch == 0 { - continue; - } - if dump && au_idx < 40 { - for c in 0..ch { - let plane: Vec = frame.samples.iter().skip(c).step_by(ch).copied().collect(); - let rms = (plane - .iter() - .map(|v| f64::from(*v) * f64::from(*v)) - .sum::() - / plane.len().max(1) as f64) - .sqrt(); - eprintln!( - "COREAU n={au_idx} ch={c} len={} rms={rms:.6} first8={:?}", - plane.len(), - &plane[..8.min(plane.len())] - ); - } - } - if planes.is_empty() { - planes = vec![Vec::new(); ch]; - } - for (i, v) in frame.samples.iter().enumerate() { - planes[i % ch].push(*v); - } - if planes[0].len() >= MAX_SAMPLES { - break; - } - } - if planes.is_empty() { - return None; - } - Some((planes, core_rate)) -} - -/// The reference decoder's decode of `absolute_stream`, resampled to -/// `target_rate` -- used to compare against `our_decode_core_only`'s -/// core-rate PCM directly instead of at the doubled SBR rate. -pub(super) fn ffmpeg_decode_at_rate( - path: &Path, - absolute_stream: usize, - channels: usize, - target_rate: u32, -) -> Vec> { - let out = ec_testkit::ffmpeg() - .args(["-v", "error", "-i"]) - .arg(path) - .args([ - "-map", - &format!("0:{absolute_stream}"), - "-t", - "30", - "-ar", - &target_rate.to_string(), - "-f", - "f32le", - "-acodec", - "pcm_f32le", - "-", - ]) - .output() - .expect("ffmpeg runs"); - assert!( - out.status.success(), - "ffmpeg decode failed: {}", - String::from_utf8_lossy(&out.stderr) - ); - let samples: Vec = out - .stdout - .as_chunks::<4>().0.iter() - .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])) - .collect(); - let mut planes = vec![Vec::with_capacity(samples.len() / channels.max(1)); channels]; - for (i, &v) in samples.iter().enumerate() { - planes[i % channels].push(v); - } - planes -} diff --git a/crates/ec-aac/tests/sbr_real_library/main.rs b/crates/ec-aac/tests/sbr_real_library/main.rs deleted file mode 100644 index affe4205..00000000 --- a/crates/ec-aac/tests/sbr_real_library/main.rs +++ /dev/null @@ -1,31 +0,0 @@ - -use std::fs::File; -use std::io::{BufReader, Read}; -use std::path::{Path, PathBuf}; - -use ec_aac::{AacDecoder, parse_audio_specific_config}; -use ec_core::{CodecId, Demuxer, Packet}; -use ec_matroska::MatroskaDemuxer; -use ec_mp4::Mp4Demuxer; - -/// Every `#[test]` in this file decodes real SBR content through -/// `our_decode`/`our_decode_uncapped`, and a handful of them steer that -/// decode with debug switches (`EC_AAC_SBR_HF_BYPASS`, -/// `EC_AAC_SBR_SIDEINFO_DEBUG`, `EC_AAC_SBR_NOISE_ZERO`, -/// `EC_AAC_SBR_NOISE_FRACTION`). They are set through -/// `ec_aac::knobs` guards, which override the switch on this test's thread -/// only and restore it on drop, so no test can flip another test's decode. -/// The lock below still serialises the tests because the decoder's -/// sideinfo and noise-fraction logs are process-global and read back by -/// each test. -static DECODE_SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(()); - -mod helpers; -mod chunk_a; -mod chunk_b; -mod chunk_c; - -use helpers::*; -use chunk_a::*; -use chunk_b::*; -use chunk_c::*;