forge
Explore
Log in

Don't you dare go Hollow

Commits

Commits on 5ae8dcda660c6bb800cc81c226120039da52c60eFiltered to crates/ec-aac/src/decode.rsClear filter

2026-08-21

2026-08-20

Merge branch 'aac-obs-content'Ahmet Kaan Gümüş · f0d7d2eBrowse files
fix(aac): run PNS noise substitution before M/S and intensity stereoAhmet Kaan Gümüş · 7efd1d7
fix(aac): run PNS noise substitution before M/S and intensity stereo

ISO/IEC 14496-3 §4.6.13 fills a PNS band's spectral coefficients with
scaled noise; §4.6.8.1 M/S and §4.6.8.2 intensity stereo then combine
those coefficients like any other spectral data. BlockDecoder::finish
ran M/S and intensity stereo first, then filled PNS bands afterward,
so any band coded as PNS in one channel still read zero when the
stereo tools combined it -- the pre-fill zero got mixed into the
other channel's coefficient (M/S) or copied outright (intensity),
corrupting a real channel's data whenever the encoder mixed PNS with
stereo coding on the same band. Reordered to match every reference
decoder's decode order: PNS -> M/S -> intensity stereo -> TNS.

Diagnostic path: chased a consumer sweep's finding of 0.76-0.92 (and
one 0.0) whole-file correlation on a set of AAC-in-mp4 screen
recordings against ffmpeg. Direct ec-mp4-demux -> ec-aac-decode
measurement (crates/ec-aac/tests/obs_real_content.rs), lag-searched
per the existing sbr_real_library.rs pattern, reads corr=1.0000 on
every non-silent file both before and after this fix -- these files'
PNS usage (1-13 AUs) apparently never lands on a band the M/S mask
also marks, so the reorder is a real spec-order bug but not what
produced that sweep's numbers; the sweep's own method likely lacked
a wide-enough lag search and caught this file family's ~1024-sample
(~21ms, one AAC frame) container/decode alignment offset instead.
The single 0.0000 file is genuine digital silence in both decoders.

Also chased a second repro (a real AAC-LC-in-mp4 download, sequential
single-decoder scan, crates/ec-aac/tests/reelh_per_frame.rs):
per-AU correlation reads 1.0000 across the whole 10.4s file except
one near-silent intro AU where both decoders read ~0 RMS and the
correlation ratio is numerically meaningless -- no reproducible
mid-stream collapse found in this file at this crate's decode layer,
with or without this fix.

Added crate-level tool-usage side info (EC_AAC_TOOL_SIDEINFO_DEBUG,
mirroring sbr_chain's SbrSideInfoRow pattern) and five ffmpeg
tool-isolation fixtures (-aac_pns/-aac_is/-aac_ms/-aac_tns, one at a
time and one with all four) wired into oracle.rs, all passing at
>=0.999 correlation, to pin the fixed decode order and give any
future PNS/IS/M-S/TNS regression a per-tool fixture to land on.
fix(aac-sbr): key SbrChain per-element state by (tag, is_cpe), not tag aloneAhmet Kaan Gümüş · 2a9df8d
fix(aac-sbr): key SbrChain per-element state by (tag, is_cpe), not tag alone

ISO/IEC 14496-3 §4.4: element_instance_tag is a namespace per syntactic
element type, so an encoder may legally give an SCE and a CPE in the same
access unit the same tag value (routine in a 5.1 SCE+CPE+CPE+LFE stream).
SbrChain::elements was a HashMap<u8, Element> keyed by the bare tag, so a
same-tagged SCE and CPE silently aliased one Analysis/Synthesis/HF/noise
state -- the CPE's parse/apply would read and mutate the SCE's element
state and vice versa. Rekeyed to (tag, is_cpe); decode.rs's three
sbr.apply/apply_last call sites now pass is_cpe through from `pending`.

Also stopped LFE (raw_data_block id 3) from ever becoming a pending SBR
target: §4.6.18.1 never applies SBR to the LFE channel (no FIL follows
it), so treating it like an SCE for `pending` could let a stray fallback
branch stretch the LFE plane to double rate using a held SBR payload that
belongs to a different, same-tagged element.

New regression test `an_sce_and_a_same_tagged_cpe_do_not_share_state`
proves an SCE at tag 0 is untouched by a same-tagged CPE's apply() call
in between two of the SCE's own calls; it fails without this fix.

Diagnostic finding: the assumption that motivated this investigation --
that the failing real-file test candidate (a BluRay-rip HEVC/mkv with a
real 6-channel HE-AAC track, tests/sbr_real_library.rs candidates(),
aac_stream=1) actually exercises that 6ch stream -- is WRONG. That
candidate's AudioSpecificConfig (asc=[13,10,56,e5,98]) decodes to
audioObjectType=2 (AAC LC), channelConfiguration=2 (stereo), sf_index=6
(24000 Hz core, SBR to 48000). ffprobe confirms the container's global
stream 1 is the real 6ch AAC track and global stream 3 is a separate 2ch
AAC track, but extract_aac_track's stream_index=1 (this test's
aac_stream) resolves to the 2ch track, not the 6ch one -- our_decode's
own debug output ("2 ch, 48000 Hz") already showed this before any
digging. This real-file failure is therefore NOT a multichannel/SBR-state
bug: this fix does not move sbr_real_library_matches_reference's
correlation for that file (unchanged at -0.025253/0.010226) or
full_chain_low_band_matches_own_core (unchanged at 0.217393). The other
real fixture (a stereo HE-AAC file, same-shape stream) stays at its prior
0.987006/0.986222 -- no regression.

GATE numbers (this commit):
  sbr_real_library_matches_reference: stereo fixture 0.987006/0.986222;
    the mislabelled candidate's SBR full-band bar 0.999 still fails,
    unchanged from baseline -0.025253/0.010226
  full_chain_low_band_matches_own_core: stereo fixture 0.996713/0.996842
    (pass); mislabelled candidate 0.217393 (fail, unchanged from baseline
    0.22)
  ec-aac --lib: 45 passed (44 prior + 1 new regression test)

2026-08-19

fix(aac): SBR frames without a fresh FIL payload now reuse the last envelopeAhmet Kaan Gümüş · f505821
fix(aac): SBR frames without a fresh FIL payload now reuse the last envelope

The windowed lag-drift diagnostic (new EC_AAC_SBR_DRIFT test knob) showed
discrete lag PLATEAUS jumping by exactly 1024 samples -- the core frame
length -- rather than a smooth per-sample drift. Root cause: raw_data_block
only doubled an SCE/CPE's plane when its immediately-following FIL element
carried a fresh SBR payload this frame; any access unit whose FIL was
missing, non-SBR, or failed to parse silently left that one AU at core
(1024-sample) length, permanently shifting every sample after it out of
alignment with a reference that stayed at doubled rate throughout. 67 such
skips were happening across the two-file sweep.

SbrChain now caches each element's last successfully parsed SbrData and
apply_last() re-runs the chain with it whenever a frame's own payload is
unavailable (Amd 4.6.18's documented "hold state" behavior), so every AU
still doubles.

reeln ch0: -0.034 -> 0.973 full-band, 0.995 below crossover (bar 0.999
full / 0.9999 below still open). ch1: 0.014 -> 0.772 full-band (plateau
structure now flat, remaining gap not yet root-caused). FMJ unchanged
(separate pre-existing 11/1500-AU decode failure, documented dead end,
left for last per the queue).

Added a unit test (sbr_chain::tests) that runs two adjacent synthetic AUs
through one SbrChain element and asserts both double to the full 2048
samples with no discontinuity at the junction -- the coverage hole the
continuous-stream QMF round trip never exercised.
fix(aac-sbr): two real scale bugs behind the SBR catastrophic blowupAhmet Kaan Gümüş · 92f96f7
fix(aac-sbr): two real scale bugs behind the SBR catastrophic blowup

Root cause 1: sbr_chain fed the SBR QMF analysis bank core PCM already
at decode::OUTPUT_SCALE (1/65536), but the transmitted envelope/noise
data is calibrated against the core's un-normalized internal PCM
domain. Undo OUTPUT_SCALE before analysis and reapply it once after
synthesis so the gain match (target vs. measured QMF energy) compares
like against like.

Root cause 2: the hand-designed 32/64-band QMF pair's own round-trip
amplitude gain was never checked -- only shape (scale-invariant
correlation). It measured ~1% of unity (a real tone demodulates to
half its amplitude in a cosine/sine-modulated analysis bank, and this
prototype's polyphase overlap-add doesn't sum to a flat constant on
its own). Added analytic_gain() (exact 2/sum(h) per-tap correction,
applied to both banks) plus a measured SYNTH_TRIM for synthesis's
remaining overlap-add shortfall; round-trip amplitude ratio is now
0.999999 (was ~0.01), locked by a new amplitude-ratio assertion in
round_trip_reconstructs_the_passband and a new
analysis_alone_reads_back_the_input_tones_own_amplitude test.

Real-file sweep (crates/ec-aac/tests/sbr_real_library.rs) improved
substantially but has not cleared the 0.999/0.9999 bar yet: reeln
ch1 full-band corr rose from 0.001 to 0.834, FMJ ch1 remains blown up
(rms ~700x too large), suggesting at least one more, channel-specific
bug remains. Debug instrumentation stays env-gated
(EC_AAC_SBR_DEBUG=1) behind sbr_chain::debug_dump.
feat(aac): wire SBR HF gen + envelope adjustment into the decode chainAhmet Kaan Gümüş · 5206266
feat(aac): wire SBR HF gen + envelope adjustment into the decode chain

sbr_chain.rs ties sbr_payload/_hf/_env/_qmf together per element: a
FIL element's sbr_extension_data (explicit ASC signaling only, no
implicit ADTS detection yet) drives 32-band analysis of the preceding
SCE/CPE's core PCM, HF generation, envelope adjustment, and 64-band
resynthesis, replacing that element's plane with double-rate PCM.

SbrSupport gains a V1 variant (PS still reports CoreOnly);
output_sample_rate now reports the extension rate for a reconstructed
stream. sbr_is_reported_not_silently_upsampled is updated (not
deleted) to pin the new contract, plus a PS case.

Not yet exercised: real-file oracle correlation (next commit),
implicit ADTS SBR signaling (deferred, named follow-up per charter),
filterbank delay compensation for packet timestamps (open -- see
report).

2026-08-14

feat(aac): AAC-LC decoder, encoder and rusty_aac shimAhmet Kaan Gümüş · 9a848b4
feat(aac): AAC-LC decoder, encoder and rusty_aac shim

Decoder covers AAC-LC end to end: AudioSpecificConfig and ADTS framing,
all four window sequences and both window shapes, section and scalefactor
data, every spectral codebook including the escape one, pulse data, TNS,
M/S and intensity stereo, PNS, and SCE/CPE/LFE/DSE/PCE/FIL elements.
Output is film order FL, FR, FC, LFE, BL, BR (SL, SR), which is the order
the downmix in this family folds, so a 5.1 track needs no caller-side
permutation. HE-AAC's SBR is not reconstructed: such a stream decodes to
its AAC-LC core and reports the core rate rather than claiming the
extension's.

Encoder is a full two-loop search: 2048-point FFT masking with spectral
flatness for tonality, bark-ish spreading, a relative hearing-threshold
floor, transient-driven window switching, per-band mid/side, exact
Huffman bit counts for codebook choice, and a bit reservoir against a CBR
target. Both ADTS and bare raw_data_block output.

The normative tables were derived rather than transcribed: access units
with a chosen spectral payload were decoded by a reference decoder and
read back through the inverse MDCT, which enumerates each codebook by a
binary tree walk. Every one came out with a Kraft sum of exactly 1 and
the entry count the standard prescribes.

forbid(unsafe_code) in both crates.