From fc0891facc9eaa7a64e0da798bafd2e3dc80398c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 12 Sep 2026 21:25:59 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(engine):=20the=20multichannel?= =?UTF-8?q?=20AAC=20decode=20seat=20is=20an=20ec-aac=20native=20=E2=80=94?= =?UTF-8?q?=20rusty=5Faac=20stays=20encoder-only?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - the >2-channel seat (a film's 5.1/7.1 track, symphonia refusing anything wider than stereo) now decodes with the family's own ec-aac; the same construction and decode call sites, the same seat shape, the same film channel order FL FR FC LFE BL BR the downmix folds - witness: fold correlation +1.00000 against the retired rusty_aac decoder on 2000 identical AUs of a 5.1 film remux (max|diff| 5.96e-8, RMS ratio 1.0000, channel matrix diagonal-dominant); stereo fixture +1.00000 / 5.46e-4 (PNS noise seed differs); new ffmpeg-correlated film witness a_seek_into_real_five_one_aac_film... scores 0.929/0.858/0.922 landing on the asked second (threshold 0.5) - perf: 12.0-13.2 Msamples/s (250-275x realtime) vs rusty 2.8-3.6 (58-75x) at 5.1 on identical packets; 26-32 Msamples/s (580-719x) vs 6.7-7.7 (151-174x) on the stereo fixture -- ~3.5x the retired decoder, bar was >=0.9x - known behaviour delta, honest in both directions: HE-AAC v1 (SBR) now reconstructs at the extension rate where rusty band-limited everything to the core Nyquist; PS streams decode core-only at the core rate in BOTH --- Cargo.lock | 9 ++++ crates/engine/Cargo.toml | 27 ++++++------ crates/engine/src/audio.rs | 16 +++---- crates/engine/src/caps.rs | 4 +- crates/engine/src/waveform.rs | 2 +- crates/engine/tests/audio_seek.rs | 69 +++++++++++++++++++++++++++++++ crates/engine/tests/hevc_mkv.rs | 2 +- 7 files changed, 106 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 944a3f4..c8bc57e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1588,6 +1588,14 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" +[[package]] +name = "ec-aac" +version = "0.1.0" +dependencies = [ + "ec-core", + "ec-dsp", +] + [[package]] name = "ec-core" version = "0.1.0" @@ -1673,6 +1681,7 @@ checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" name = "engine" version = "0.1.0" dependencies = [ + "ec-aac", "ec-core", "ec-h264", "ec-opus", diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 3d5069a..3d2ba89 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -58,18 +58,21 @@ ec-h264 = { path = "../../../edith_codecs/crates/ec-h264" } # Its shared IR (packets, frames, the registry decoder contract the seat is # written against), direct because decode.rs names those types itself. ec-core = { path = "../../../edith_codecs/crates/ec-core" } -# Both halves of AAC, and the only crate here that is asked for both. The -# *encoder*, so an mp4's sound can be a mix rather than a copy: an equalized (or -# otherwise processed) lane has to be decoded, and what is decoded has to be -# encoded again. The *decoder*, because symphonia's own refuses anything wider -# than stereo outright (`aac/mod.rs:93`, "aac: aac too complex") and a 5.1 film -# soundtrack in an mkv is exactly that -- this one hands 5.1 back already -# reordered into FL, FR, FC, LFE, BL, BR, which is the order the downmix in -# `audio` folds. Pure Rust with no C or FFI, Apache-2.0, the same Mata Network -# family the retired `rusty_h264` came from. Nothing else in the ecosystem -# encodes AAC without linking -# Fraunhofer's C library, which this project's no-install rule forbids. Built -# from `vendor/rusty_aac` -- see the patch table in the workspace manifest. +# The multichannel AAC decode seat: symphonia's own refuses anything wider than +# stereo outright (`aac/mod.rs:93`, "aac: aac too complex"), and a 5.1 or 7.1 +# film soundtrack is exactly that. `ec-aac`, the family's own decoder, took the +# seat the vendored `rusty_aac` held -- it hands 5.1 back already reordered +# into FL, FR, FC, LFE, BL, BR, the order the downmix in `audio` folds, the +# same one the seat always delivered. +ec-aac = { path = "../../../edith_codecs/crates/ec-aac" } +# The AAC *encoder*, and its last seat in the tree: so an mp4's sound can be a +# mix rather than a copy, an equalized (or otherwise processed) lane has to be +# decoded, and what is decoded has to be encoded again. Pure Rust with no C or +# FFI, Apache-2.0, the same Mata Network family the retired `rusty_h264` came +# from. Nothing else in the ecosystem encodes AAC without linking Fraunhofer's +# C library, which this project's no-install rule forbids. Built from +# `vendor/rusty_aac` -- see the patch table in the workspace manifest. Its +# *decoder* left the tree when `ec-aac` above took the multichannel seat. rusty_aac = "0.5" # The MP3 encoder, so that format is a row a user can pick rather than a licence # note: pure Rust, Apache-2.0, CBR and VBR. The decoder side of mp3 is diff --git a/crates/engine/src/audio.rs b/crates/engine/src/audio.rs index 42e0be7..53966ef 100644 --- a/crates/engine/src/audio.rs +++ b/crates/engine/src/audio.rs @@ -1077,7 +1077,7 @@ impl AudioSession { // The profile still gates -- the writer rebuilds an // AudioSpecificConfig and calls it LC -- but the layout no // longer does: a 5.1 or 7.1 AAC track decodes through - // `rusty_aac` and folds to the pair the timeline carries + // `ec-aac` and folds to the pair the timeline carries // ([`aac_decoder`]). decodable: ac3.is_some() || aac @@ -1699,7 +1699,7 @@ impl SymTrack { /// Which decoder is not the caller's business, and it is not always /// symphonia's: its AAC decoder refuses anything wider than stereo outright /// (`aac/mod.rs:93`, "aac: aac too complex"), which is exactly the 5.1 and - /// 7.1 tracks of a film, so those go to `rusty_aac` instead + /// 7.1 tracks of a film, so those go to `ec-aac` instead /// ([`aac_decoder`]), and it has no Opus decoder at all, so that one goes to /// `ec-opus` ([`SymDecoder::Opus`]). fn decoder(&self) -> crate::Result { @@ -1836,7 +1836,7 @@ enum SymDecoder { Sym(Box, usize), /// Boxed because the decoder carries its own IMDCT tables and this enum is /// moved into the worker. - Aac(Box, usize), + Aac(Box, usize), /// Every Opus track, mono to 7.1: one multistream decoder -- the streams of /// a packet, the channel mapping table and the routing between them are all /// its business -- and the same width. @@ -1875,7 +1875,7 @@ impl SymDecoder { /// The decoder an AAC track of `from` channels needs: symphonia's own refuses /// anything wider than stereo outright (`aac/mod.rs:93`, "aac: aac too /// complex"), which is exactly the 5.1 and 7.1 tracks of a film, so those go to -/// `rusty_aac` instead -- the same routing [`SymTrack::decoder`] does for a +/// `ec-aac` instead -- the same routing [`SymTrack::decoder`] does for a /// Matroska file, asked once here so an mp4's multichannel track takes it too. fn aac_decoder(params: &AudioCodecParameters, from: usize) -> crate::Result { if from > 2 { @@ -1883,7 +1883,7 @@ fn aac_decoder(params: &AudioCodecParameters, from: usize) -> crate::Result = Vec::with_capacity(meta.sample_rate as usize); + for chunk in &rx { + probe.extend( + chunk + .samples + .chunks_exact(channels) + .map(|f| f.iter().sum::() / channels as f32), + ); + if probe.len() >= meta.sample_rate as usize { + break; + } + } + drop(rx); + probe.truncate(meta.sample_rate as usize); + assert_eq!(probe.len(), meta.sample_rate as usize, "a second to locate"); + + let from = want - SEARCH; + let reference = ffmpeg_mono(&film, from, 2.0 * SEARCH + 2.0, meta.sample_rate); + let (lag, score) = best_lag(&probe, &reference); + let at = from + lag as f64 / rate; + eprintln!("asked {want}s, content at {at:.3}s (correlation {score:.3})"); + assert!( + score > 0.5, + "asked {want}s: nothing in the reference window correlates ({score:.3}) -- \ + the measurement, not the seek, is what failed" + ); + assert!( + (at - want).abs() <= 0.05, + "asked {want}s, heard {at:.3}s -- {:+.3}s of sound against picture", + at - want + ); + } +} + /// `dur` seconds of `path`'s first audio track from `start`, decoded by ffmpeg, /// summed to mono at `rate`. The reference this engine's own landing is measured /// against, because a decoder cannot be its own witness about where it landed. diff --git a/crates/engine/tests/hevc_mkv.rs b/crates/engine/tests/hevc_mkv.rs index e149161..3055cac 100644 --- a/crates/engine/tests/hevc_mkv.rs +++ b/crates/engine/tests/hevc_mkv.rs @@ -4,7 +4,7 @@ //! Three things meet here and each is checked on its own: the demuxer reads an //! `hvcC` out of a `CodecPrivate` and reframes Matroska blocks to Annex-B, the //! plugin decodes Main 10 through a P010 surface pool, and the sound -- 5.1 AAC -//! at 48 kHz -- is read by symphonia's mkv reader, decoded by `rusty_aac` and +//! at 48 kHz -- is read by symphonia's mkv reader, decoded by `ec-aac` and //! folded to the stereo one output device carries. //! //! The container and audio checks need nothing installed. The decode twins need