forge
Explore
Log in

Don't you dare go Hollow

The AAC encode seat is ec-aac -- export.rs's encode_audio and the muxer's tone harness speak ec_aac::AacEncoder on the seat's old contract (interleaved f32 push_pcm, raw raw_data_block packets one frame long, the sf_index_for_rate refusal the export turns into a user-facing message, ASC rebuilt from AacTrackParams), the caps row and both SW-encode labels read ec-aac, and a new mux witness round-trips seat-encoded mono, stereo and 5.1 through the project's own decode door (symphonia's reader for the pair, the family decoder for 5.1, folded to stereo as the door's contract states). A/B on identical synthetic program -- mono 44.1k 128k, stereo 44.1/48k 256k, stereo 48k 96k, 5.1 48k 512k, every output ADTS-framed and ffmpeg-decoded, alignment by envelope correlation with an ffmpeg-native control column: ec-aac correlates at or above the incumbent on every row (mono 0.9674 vs 0.9564, stereo 96k 0.9445 vs 0.8458, 5.1 0.6050 vs 0.6007) and matches or beats ffmpeg's own encoder at matched actual rate

Browse files at this commit

7ed056a6992a03fe021df58900b2e1b754fe75c3Ahmet Kaan GümüşParent: 6421f59
  • refactor
    • the AAC encode seat is ec-aac -- export.rs's encode_audio and the muxer's tone harness speak ec_aac::AacEncoder on the seat's old contract (interleaved f32 push_pcm, raw raw_data_block packets one frame long, the sf_index_for_rate refusal the export turns into a user-facing message, ASC rebuilt from AacTrackParams), the caps row and both SW-encode labels read ec-aac, and a new mux witness round-trips seat-encoded mono, stereo and 5.1 through the project's own decode door (symphonia's reader for the pair, the family decoder for 5.1, folded to stereo as the door's contract states). A/B on identical synthetic program -- mono 44.1k 128k, stereo 44.1/48k 256k, stereo 48k 96k, 5.1 48k 512k, every output ADTS-framed and ffmpeg-decoded, alignment by envelope correlation with an ffmpeg-native control column: ec-aac correlates at or above the incumbent on every row (mono 0.9674 vs 0.9564, stereo 96k 0.9445 vs 0.8458, 5.1 0.6050 vs 0.6007) and matches or beats ffmpeg's own encoder at matched actual rate
    • cost is +4-6% actual bits at the same target and a slower encode wall (single-threaded vs rusty_aac's frame-parallel finish: 2.0-3.7x on one core, 5.9-10.2x as-shipped on this box) -- named here per the perf bar. rusty_aac itself leaves in the next commit. Export and capability suites green, -p engine --all-targets 0 warnings
Show full message
♻️ refactor(engine): the AAC encode seat is ec-aac -- export.rs's encode_audio and the muxer's tone harness speak ec_aac::AacEncoder on the seat's old contract (interleaved f32 push_pcm, raw raw_data_block packets one frame long, the sf_index_for_rate refusal the export turns into a user-facing message, ASC rebuilt from AacTrackParams), the caps row and both SW-encode labels read ec-aac, and a new mux witness round-trips seat-encoded mono, stereo and 5.1 through the project's own decode door (symphonia's reader for the pair, the family decoder for 5.1, folded to stereo as the door's contract states). A/B on identical synthetic program -- mono 44.1k 128k, stereo 44.1/48k 256k, stereo 48k 96k, 5.1 48k 512k, every output ADTS-framed and ffmpeg-decoded, alignment by envelope correlation with an ffmpeg-native control column: ec-aac correlates at or above the incumbent on every row (mono 0.9674 vs 0.9564, stereo 96k 0.9445 vs 0.8458, 5.1 0.6050 vs 0.6007) and matches or beats ffmpeg's own encoder at matched actual rate; cost is +4-6% actual bits at the same target and a slower encode wall (single-threaded vs rusty_aac's frame-parallel finish: 2.0-3.7x on one core, 5.9-10.2x as-shipped on this box) -- named here per the perf bar. rusty_aac itself leaves in the next commit. Export and capability suites green, -p engine --all-targets 0 warnings
crates/engine/src/caps.rsmodified+3 -3
@@ -37,8 +37,8 @@ const SW_CODECS: [(&str, &str, bool, bool); 10] = [
3737 ("HEVC", "oxideav-h265 intra", false, true),
3838 ("AV1", "rav1e", false, true),
3939 // Decode is split by width: symphonia carries stereo and narrower, `ec-aac`
40- // everything wider; the encoder is `rusty_aac`.
41- ("AAC", "symphonia / ec-aac / rusty_aac", true, true),
40+ // everything wider; the encoder is `ec-aac` as well.
41+ ("AAC", "symphonia / ec-aac / ec-aac", true, true),
4242 ("MP3", "symphonia / rusty_mp3", true, true),
4343 ("FLAC", "symphonia / flacenc", true, true),
4444 ("PCM", "symphonia / hound", true, true),
@@ -159,7 +159,7 @@ mod tests {
159159 #[test]
160160 fn the_software_line_names_every_encoder_this_build_carries() {
161161 let line = software();
162- for name in ["ec-h264", "oxideav-h265 intra", "rav1e", "rusty_aac"] {
162+ for name in ["ec-h264", "oxideav-h265 intra", "rav1e", "ec-aac"] {
163163 assert!(line.contains(name), "{name} missing from {line}");
164164 }
165165 // Decode-only crates must not read as encoders, and the seat words are
crates/engine/src/export.rsmodified+10 -9
@@ -21,7 +21,7 @@
2121 //! through the encoder exactly as it always did. Where the *sound* cannot be
2222 //! copied — a second audio lane to mix in, a speeded clip, an
2323 //! equalized one, a source that is not AAC inside an mp4's own sample table —
24-//! the sound is decoded, mixed and encoded again with `rusty_aac`
24+//! the sound is decoded, mixed and encoded again with `ec-aac`
2525 //! ([`encode_audio`]). The audio-only formats have no such split: `hound` writes
2626 //! PCM, `flacenc` encodes FLAC, `rusty_mp3` encodes MP3 and `rusty_vorbis`
2727 //! 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;
159159 /// WAV, FLAC, MP3 and Ogg Vorbis are the standalone audio formats with a
160160 /// pure-Rust encoder: `hound`, `flacenc`, `rusty_mp3` and `rusty_vorbis`. Opus
161161 /// still has none -- `oxideav-opus` encodes CELT alone, which is half a codec.
162-/// (AAC has one too -- `rusty_aac`, which is what a re-encoded video track's
162+/// (AAC has one too -- `ec-aac`, which is what a re-encoded video track's
163163 /// sound leaves through -- but AAC is a container's own audio, never a file of
164164 /// its own here.) VP9 and VP8 are the
165165 /// codecs that come in through the plugin and stop there, HEVC having
@@ -864,7 +864,7 @@ fn audio_label(
864864 // has the test), which is why the condition is in this comment
865865 // instead of in the string.
866866 false if format.is_mkv() => "Opus · SW encode (ec-opus)",
867- false => "AAC · SW encode (rusty_aac)",
867+ false => "AAC · SW encode (ec-aac)",
868868 },
869869 }
870870 }
@@ -889,7 +889,7 @@ fn measured_audio_label(
889889 // Encoded, into a container that would have carried Opus, and it is not
890890 // Opus: the fidelity gate sent it to AAC ([`OPUS_MIN_FIDELITY`]), or
891891 // the mix was not 48 kHz at a width the seat is measured in.
892- (Some(false), false) if format.is_mkv() => "AAC · SW encode (rusty_aac)",
892+ (Some(false), false) if format.is_mkv() => "AAC · SW encode (ec-aac)",
893893 // `sound` is already what happened, not a prediction, so `copied` is
894894 // filled in and `ranged` never reaches the branch that reads it.
895895 _ => audio_label(project, format, sound.is_some(), sound, false),
@@ -1940,9 +1940,10 @@ impl CopyPlan {
19401940 /// the sound starts at timeline frame 0 as a copied track's does.
19411941 ///
19421942 /// corner-cut: the mix sits in memory as f32 before it is encoded (~46 MB a
1943-/// minute of 48 kHz stereo), for [`run_audio`]'s reason -- `rusty_aac` buffers
1944-/// the whole stream anyway, since it encodes its frames in parallel. Upgrade
1945-/// path is a chunked push once that encoder streams.
1943+/// minute of 48 kHz stereo), for [`run_audio`]'s reason -- sizing the track
1944+/// against the timeline, publishing the mix third of the bar as it goes and
1945+/// answering a cancel before the encode are whole-mix concerns here. The
1946+/// upgrade path is a chunked push: `ec_aac`'s encoder itself streams.
19461947 fn encode_audio(
19471948 project: &Project,
19481949 meta: &VideoMeta,
@@ -1977,7 +1978,7 @@ fn encode_audio(
19771978 else {
19781979 return Ok(None); // no audio to write, exactly as a copy of nothing is
19791980 };
1980- let freq_index = rusty_aac::sf_index_for_rate(audio.sample_rate).ok_or_else(|| {
1981+ let freq_index = ec_aac::sf_index_for_rate(audio.sample_rate).ok_or_else(|| {
19811982 format!(
19821983 "{} Hz is not an AAC sample rate: export WAV, FLAC, MP3 or OGG, which write it as \
19831984 it is",
@@ -2058,7 +2059,7 @@ fn encode_audio(
20582059
20592060 // The caller's rate, never the encoder's own 128 default -- see
20602061 // [`DEFAULT_AUDIO_KBPS`] for why the untouched figure is 256.
2061- let mut encoder = rusty_aac::AacEncoder::new(rusty_aac::AacEncoderConfig {
2062+ let mut encoder = ec_aac::AacEncoder::new(ec_aac::AacEncoderConfig {
20622063 bitrate_bps: kbps * 1_000,
20632064 ..Default::default()
20642065 });
crates/engine/src/mux.rsmodified+89 -1
@@ -2258,7 +2258,7 @@ mod tests {
22582258 pcm.push(s);
22592259 }
22602260 }
2261- let mut encoder = rusty_aac::AacEncoder::new(rusty_aac::AacEncoderConfig::default());
2261+ let mut encoder = ec_aac::AacEncoder::new(ec_aac::AacEncoderConfig::default());
22622262 encoder.push_pcm(&pcm, channels, rate).unwrap();
22632263 encoder.finish();
22642264 let mut packets = Vec::new();
@@ -2348,6 +2348,94 @@ mod tests {
23482348 std::fs::remove_file(&out).unwrap();
23492349 }
23502350
2351+ /// The seat an export encodes with, at every width the Matroska muxer can
2352+ /// carry: a track it encoded comes back through the project's own decode
2353+ /// door -- symphonia's reader for mono and stereo, the family's own
2354+ /// `ec-aac` decoder for 5.1, which is where the decode splits -- with its
2355+ /// rate and loudness intact, no delivered channel silent. A width wider
2356+ /// than a pair arrives folded to stereo ([`crate::audio::downmix`] at the
2357+ /// decoder's door), so the 5.1 leg asserts the pair, not the six.
2358+ #[test]
2359+ fn a_seat_encoded_track_round_trips_at_mono_stereo_and_five_one_widths() {
2360+ let sequence = obu(1, &[0x11, 0x22, 0x33]);
2361+ let mut key = sequence.clone();
2362+ key.extend_from_slice(&obu(6, &[0xAA; 8]));
2363+ let inter = obu(6, &[0xBB; 5]);
2364+ for (channels, name) in [(1u16, "mono"), (2, "stereo"), (6, "5.1")] {
2365+ let packets = tone_packets(48_000, channels);
2366+ let heard: usize = packets.iter().map(|p| p.samples as usize).sum();
2367+
2368+ let out = Scratch::file("ve_mkv_seat_widths", "mkv");
2369+ let mut muxer = MkvMuxer::create(
2370+ &out,
2371+ &Av1Params {
2372+ width: 640,
2373+ height: 360,
2374+ frame_rate: 30.0,
2375+ config: av1_sequence_header(&key).unwrap(),
2376+ },
2377+ Some((
2378+ &AudioParams {
2379+ freq_index: 3, // 48000
2380+ chan_conf: channels as u8,
2381+ sample_rate: 48_000,
2382+ opus_pre_skip: None,
2383+ },
2384+ packets,
2385+ )),
2386+ Vec::new(),
2387+ )
2388+ .unwrap();
2389+ muxer.write_frame(&key, true).unwrap();
2390+ for _ in 0..14 {
2391+ muxer.write_frame(&inter, false).unwrap();
2392+ }
2393+ muxer.finish().unwrap();
2394+
2395+ // What the door reports: the file's own width, folded to a pair
2396+ // for anything wider -- the door's contract, not a defect.
2397+ let want = channels.min(2);
2398+ let probe = crate::audio::AudioSession::probe(&out, 0)
2399+ .unwrap()
2400+ .expect("the file has an audio track");
2401+ assert_eq!(
2402+ (probe.sample_rate, probe.channels),
2403+ (48_000, want),
2404+ "{name}: probed with the wrong shape"
2405+ );
2406+ let (audio, chunks) = crate::audio::AudioSession::open(&out)
2407+ .unwrap()
2408+ .unwrap_or_else(|| panic!("{name}: track decodes through the project's own reader"));
2409+ assert_eq!(
2410+ (audio.sample_rate, audio.channels),
2411+ (48_000, want),
2412+ "{name}: opened with the wrong shape"
2413+ );
2414+ let samples: Vec<f32> = chunks.into_iter().flat_map(|c| c.samples).collect();
2415+ let frames = samples.len() / usize::from(want);
2416+ assert!(
2417+ frames + 1024 >= heard && frames <= heard + 2048,
2418+ "{name}: {frames} frames read back of {heard} written"
2419+ );
2420+ // Every delivered channel carries the tone -- a fold that lost a
2421+ // side or a dead decoder would starve one of these.
2422+ for c in 0..usize::from(want) {
2423+ let n = samples.len() / usize::from(want);
2424+ let rms = (samples
2425+ .iter()
2426+ .skip(c)
2427+ .step_by(usize::from(want))
2428+ .take(n)
2429+ .map(|s| s * s)
2430+ .sum::<f32>()
2431+ / n as f32)
2432+ .sqrt();
2433+ assert!(rms > 0.05, "{name}: channel {c} came back silent (rms {rms})");
2434+ }
2435+ std::fs::remove_file(&out).unwrap();
2436+ }
2437+ }
2438+
23512439 /// The same stream in the other container: AV1 into an mp4, whose `av01`
23522440 /// sample entry is written by hand, read back through our own demuxer --
23532441 /// which is the only test that the patched box tree is still an mp4.