From 7ed056a6992a03fe021df58900b2e1b754fe75c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 13 Sep 2026 01:34:36 +0300 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(engine):=20the=20?= =?UTF-8?q?AAC=20encode=20seat=20is=20ec-aac=20--=20export.rs's=20encode?= =?UTF-8?q?=5Faudio=20and=20the=20muxer's=20tone=20harness=20speak=20ec=5F?= =?UTF-8?q?aac::AacEncoder=20on=20the=20seat's=20old=20contract=20(interle?= =?UTF-8?q?aved=20f32=20push=5Fpcm,=20raw=20raw=5Fdata=5Fblock=20packets?= =?UTF-8?q?=20one=20frame=20long,=20the=20sf=5Findex=5Ffor=5Frate=20refusa?= =?UTF-8?q?l=20the=20export=20turns=20into=20a=20user-facing=20message,=20?= =?UTF-8?q?ASC=20rebuilt=20from=20AacTrackParams),=20the=20caps=20row=20an?= =?UTF-8?q?d=20both=20SW-encode=20labels=20read=20ec-aac,=20and=20a=20new?= =?UTF-8?q?=20mux=20witness=20round-trips=20seat-encoded=20mono,=20stereo?= =?UTF-8?q?=20and=205.1=20through=20the=20project's=20own=20decode=20door?= =?UTF-8?q?=20(symphonia's=20reader=20for=20the=20pair,=20the=20family=20d?= =?UTF-8?q?ecoder=20for=205.1,=20folded=20to=20stereo=20as=20the=20door's?= =?UTF-8?q?=20contract=20states).=20A/B=20on=20identical=20synthetic=20pro?= =?UTF-8?q?gram=20--=20mono=2044.1k=20128k,=20stereo=2044.1/48k=20256k,=20?= =?UTF-8?q?stereo=2048k=2096k,=205.1=2048k=20512k,=20every=20output=20ADTS?= =?UTF-8?q?-framed=20and=20ffmpeg-decoded,=20alignment=20by=20envelope=20c?= =?UTF-8?q?orrelation=20with=20an=20ffmpeg-native=20control=20column:=20ec?= =?UTF-8?q?-aac=20correlates=20at=20or=20above=20the=20incumbent=20on=20ev?= =?UTF-8?q?ery=20row=20(mono=200.9674=20vs=200.9564,=20stereo=2096k=200.94?= =?UTF-8?q?45=20vs=200.8458,=205.1=200.6050=20vs=200.6007)=20and=20matches?= =?UTF-8?q?=20or=20beats=20ffmpeg's=20own=20encoder=20at=20matched=20actua?= =?UTF-8?q?l=20rate;=20cost=20is=20+4-6%=20actual=20bits=20at=20the=20same?= =?UTF-8?q?=20target=20and=20a=20slower=20encode=20wall=20(single-threaded?= =?UTF-8?q?=20vs=20rusty=5Faac's=20frame-parallel=20finish:=202.0-3.7x=20o?= =?UTF-8?q?n=20one=20core,=205.9-10.2x=20as-shipped=20on=20this=20box)=20-?= =?UTF-8?q?-=20named=20here=20per=20the=20perf=20bar.=20rusty=5Faac=20itse?= =?UTF-8?q?lf=20leaves=20in=20the=20next=20commit.=20Export=20and=20capabi?= =?UTF-8?q?lity=20suites=20green,=20-p=20engine=20--all-targets=200=20warn?= =?UTF-8?q?ings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/engine/src/caps.rs | 6 +-- crates/engine/src/export.rs | 19 ++++---- crates/engine/src/mux.rs | 90 ++++++++++++++++++++++++++++++++++++- 3 files changed, 102 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/caps.rs b/crates/engine/src/caps.rs index 34e9c94..ff2e814 100644 --- a/crates/engine/src/caps.rs +++ b/crates/engine/src/caps.rs @@ -37,8 +37,8 @@ const SW_CODECS: [(&str, &str, bool, bool); 10] = [ ("HEVC", "oxideav-h265 intra", false, true), ("AV1", "rav1e", false, true), // Decode is split by width: symphonia carries stereo and narrower, `ec-aac` - // everything wider; the encoder is `rusty_aac`. - ("AAC", "symphonia / ec-aac / rusty_aac", true, true), + // everything wider; the encoder is `ec-aac` as well. + ("AAC", "symphonia / ec-aac / ec-aac", true, true), ("MP3", "symphonia / rusty_mp3", true, true), ("FLAC", "symphonia / flacenc", true, true), ("PCM", "symphonia / hound", true, true), @@ -159,7 +159,7 @@ mod tests { #[test] fn the_software_line_names_every_encoder_this_build_carries() { let line = software(); - for name in ["ec-h264", "oxideav-h265 intra", "rav1e", "rusty_aac"] { + for name in ["ec-h264", "oxideav-h265 intra", "rav1e", "ec-aac"] { assert!(line.contains(name), "{name} missing from {line}"); } // Decode-only crates must not read as encoders, and the seat words are diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index 8829d83..a7fbc6c 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -21,7 +21,7 @@ //! through the encoder exactly as it always did. Where the *sound* cannot be //! copied — a second audio lane to mix in, a speeded clip, an //! equalized one, a source that is not AAC inside an mp4's own sample table — -//! the sound is decoded, mixed and encoded again with `rusty_aac` +//! the sound is decoded, mixed and encoded again with `ec-aac` //! ([`encode_audio`]). The audio-only formats have no such split: `hound` writes //! PCM, `flacenc` encodes FLAC, `rusty_mp3` encodes MP3 and `rusty_vorbis` //! encodes Vorbis, so those are always *decoded* out of the timeline. The worker owns everything: the caller gets an @@ -159,7 +159,7 @@ const HEVC_QP_MAX: i32 = 40; /// WAV, FLAC, MP3 and Ogg Vorbis are the standalone audio formats with a /// pure-Rust encoder: `hound`, `flacenc`, `rusty_mp3` and `rusty_vorbis`. Opus /// still has none -- `oxideav-opus` encodes CELT alone, which is half a codec. -/// (AAC has one too -- `rusty_aac`, which is what a re-encoded video track's +/// (AAC has one too -- `ec-aac`, which is what a re-encoded video track's /// sound leaves through -- but AAC is a container's own audio, never a file of /// its own here.) VP9 and VP8 are the /// codecs that come in through the plugin and stop there, HEVC having @@ -864,7 +864,7 @@ fn audio_label( // has the test), which is why the condition is in this comment // instead of in the string. false if format.is_mkv() => "Opus · SW encode (ec-opus)", - false => "AAC · SW encode (rusty_aac)", + false => "AAC · SW encode (ec-aac)", }, } } @@ -889,7 +889,7 @@ fn measured_audio_label( // Encoded, into a container that would have carried Opus, and it is not // Opus: the fidelity gate sent it to AAC ([`OPUS_MIN_FIDELITY`]), or // the mix was not 48 kHz at a width the seat is measured in. - (Some(false), false) if format.is_mkv() => "AAC · SW encode (rusty_aac)", + (Some(false), false) if format.is_mkv() => "AAC · SW encode (ec-aac)", // `sound` is already what happened, not a prediction, so `copied` is // filled in and `ranged` never reaches the branch that reads it. _ => audio_label(project, format, sound.is_some(), sound, false), @@ -1940,9 +1940,10 @@ impl CopyPlan { /// the sound starts at timeline frame 0 as a copied track's does. /// /// corner-cut: the mix sits in memory as f32 before it is encoded (~46 MB a -/// minute of 48 kHz stereo), for [`run_audio`]'s reason -- `rusty_aac` buffers -/// the whole stream anyway, since it encodes its frames in parallel. Upgrade -/// path is a chunked push once that encoder streams. +/// minute of 48 kHz stereo), for [`run_audio`]'s reason -- sizing the track +/// against the timeline, publishing the mix third of the bar as it goes and +/// answering a cancel before the encode are whole-mix concerns here. The +/// upgrade path is a chunked push: `ec_aac`'s encoder itself streams. fn encode_audio( project: &Project, meta: &VideoMeta, @@ -1977,7 +1978,7 @@ fn encode_audio( else { return Ok(None); // no audio to write, exactly as a copy of nothing is }; - let freq_index = rusty_aac::sf_index_for_rate(audio.sample_rate).ok_or_else(|| { + let freq_index = ec_aac::sf_index_for_rate(audio.sample_rate).ok_or_else(|| { format!( "{} Hz is not an AAC sample rate: export WAV, FLAC, MP3 or OGG, which write it as \ it is", @@ -2058,7 +2059,7 @@ fn encode_audio( // The caller's rate, never the encoder's own 128 default -- see // [`DEFAULT_AUDIO_KBPS`] for why the untouched figure is 256. - let mut encoder = rusty_aac::AacEncoder::new(rusty_aac::AacEncoderConfig { + let mut encoder = ec_aac::AacEncoder::new(ec_aac::AacEncoderConfig { bitrate_bps: kbps * 1_000, ..Default::default() }); diff --git a/crates/engine/src/mux.rs b/crates/engine/src/mux.rs index 828da74..5b042fa 100644 --- a/crates/engine/src/mux.rs +++ b/crates/engine/src/mux.rs @@ -2258,7 +2258,7 @@ mod tests { pcm.push(s); } } - let mut encoder = rusty_aac::AacEncoder::new(rusty_aac::AacEncoderConfig::default()); + let mut encoder = ec_aac::AacEncoder::new(ec_aac::AacEncoderConfig::default()); encoder.push_pcm(&pcm, channels, rate).unwrap(); encoder.finish(); let mut packets = Vec::new(); @@ -2348,6 +2348,94 @@ mod tests { std::fs::remove_file(&out).unwrap(); } + /// The seat an export encodes with, at every width the Matroska muxer can + /// carry: a track it encoded comes back through the project's own decode + /// door -- symphonia's reader for mono and stereo, the family's own + /// `ec-aac` decoder for 5.1, which is where the decode splits -- with its + /// rate and loudness intact, no delivered channel silent. A width wider + /// than a pair arrives folded to stereo ([`crate::audio::downmix`] at the + /// decoder's door), so the 5.1 leg asserts the pair, not the six. + #[test] + fn a_seat_encoded_track_round_trips_at_mono_stereo_and_five_one_widths() { + let sequence = obu(1, &[0x11, 0x22, 0x33]); + let mut key = sequence.clone(); + key.extend_from_slice(&obu(6, &[0xAA; 8])); + let inter = obu(6, &[0xBB; 5]); + for (channels, name) in [(1u16, "mono"), (2, "stereo"), (6, "5.1")] { + let packets = tone_packets(48_000, channels); + let heard: usize = packets.iter().map(|p| p.samples as usize).sum(); + + let out = Scratch::file("ve_mkv_seat_widths", "mkv"); + let mut muxer = MkvMuxer::create( + &out, + &Av1Params { + width: 640, + height: 360, + frame_rate: 30.0, + config: av1_sequence_header(&key).unwrap(), + }, + Some(( + &AudioParams { + freq_index: 3, // 48000 + chan_conf: channels as u8, + sample_rate: 48_000, + opus_pre_skip: None, + }, + packets, + )), + Vec::new(), + ) + .unwrap(); + muxer.write_frame(&key, true).unwrap(); + for _ in 0..14 { + muxer.write_frame(&inter, false).unwrap(); + } + muxer.finish().unwrap(); + + // What the door reports: the file's own width, folded to a pair + // for anything wider -- the door's contract, not a defect. + let want = channels.min(2); + let probe = crate::audio::AudioSession::probe(&out, 0) + .unwrap() + .expect("the file has an audio track"); + assert_eq!( + (probe.sample_rate, probe.channels), + (48_000, want), + "{name}: probed with the wrong shape" + ); + let (audio, chunks) = crate::audio::AudioSession::open(&out) + .unwrap() + .unwrap_or_else(|| panic!("{name}: track decodes through the project's own reader")); + assert_eq!( + (audio.sample_rate, audio.channels), + (48_000, want), + "{name}: opened with the wrong shape" + ); + let samples: Vec = chunks.into_iter().flat_map(|c| c.samples).collect(); + let frames = samples.len() / usize::from(want); + assert!( + frames + 1024 >= heard && frames <= heard + 2048, + "{name}: {frames} frames read back of {heard} written" + ); + // Every delivered channel carries the tone -- a fold that lost a + // side or a dead decoder would starve one of these. + for c in 0..usize::from(want) { + let n = samples.len() / usize::from(want); + let rms = (samples + .iter() + .skip(c) + .step_by(usize::from(want)) + .take(n) + .map(|s| s * s) + .sum::() + / n as f32) + .sqrt(); + assert!(rms > 0.05, "{name}: channel {c} came back silent (rms {rms})"); + } + std::fs::remove_file(&out).unwrap(); + } + } + /// The same stream in the other container: AV1 into an mp4, whose `av01` /// sample entry is written by hand, read back through our own demuxer -- /// which is the only test that the patched box tree is still an mp4.