diff --git a/Cargo.lock b/Cargo.lock index 14b61940..4ae08db7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -372,7 +372,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/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() - ); -}