forge
Explore
Log in

Don't you dare go Hollow

Zero masked residues

Browse files at this commit

f70e8e68ef11e1cf04441a69f0ea92deb0bde428Ahmet Kaan GümüşParent: eca8b3d
  • fix
    • zero masked residues
Show full message
fix(vorbis): zero masked residues
crates/ec-vorbis/src/encode.rsmodified+17 -7
@@ -81,7 +81,7 @@ const CLASS_RANGE: [i32; CLASSES] = [0, 1, 2, 4, 8, 16, 32, 64, 127];
8181 const RESIDUE_BOOK_DIM: [usize; CLASSES] = [0, 4, 2, 2, 1, 1, 1, 1, 1];
8282 /// The vector books save packet bits but add setup and make the rate loop spend
8383 /// harder; this keeps the realised rate at the caller's target.
84-const RATE_TARGET_SCALE: f64 = 0.99;
84+const RATE_TARGET_SCALE: f64 = 0.96;
8585 /// Headroom in dB the widest residue book can actually state: 20 log10 127
8686 /// is 42, but a coupled angle channel is a difference of two magnitudes, so a
8787 /// pair's step must leave half that range — 63, 36 dB. Past this the rate loop's extra headroom lowers the absolute-threshold
@@ -102,6 +102,10 @@ const HEADROOM_RANGE_SHORT: f64 = 42.0;
102102 /// three tracks at 128 kbps: 50 dB kept the accuracy win without inflating the
103103 /// files.
104104 const CO_MASK_RANGE: f64 = 50.0;
105+/// Extra dB above the spread threshold. This is the single long-block
106+/// rate/quality knob: raising the curve zeroes masked coefficients earlier,
107+/// while peaks still ride as residue above it.
108+const MASKING_OFFSET_DB: f64 = 9.0;
105109 /// A short block gets no co-masking cap: a bin it drops is an error spread
106110 /// across its whole window, the half before an onset included, and that is
107111 /// the pre-echo the short block exists to prevent (44.1k mono, onset on the
@@ -676,17 +680,22 @@ impl VorbisEncoder {
676680 fit(&[channel]);
677681 }
678682
679- // Normalise, couple, quantise.
683+ // Normalise, couple, quantise. The floor is the masking threshold:
684+ // bins that do not clear it are inaudible here and code as zero;
685+ // bins that do clear it keep their full rounded residue, producing
686+ // sparse small classes and larger tonal residues.
680687 let mut quantised: Vec<Vec<i32>> = (0..channels)
681688 .map(|channel| {
682689 spectra[channel]
683690 .iter()
684691 .zip(curves[channel].iter())
685- .map(|(&value, &floor)| match floor > 0.0 {
686- true => value / floor,
687- false => 0.0,
692+ .map(|(&value, &floor)| {
693+ if floor <= 0.0 || value.abs() <= floor {
694+ 0
695+ } else {
696+ (value / floor).round() as i32
697+ }
688698 })
689- .map(|normalised| normalised.round() as i32)
690699 .collect::<Vec<i32>>()
691700 })
692701 .collect();
@@ -865,7 +874,8 @@ fn fit_floor(
865874 .zip(ath.iter())
866875 .map(|(&level, &threshold)| {
867876 let threshold = threshold.max(loudest - co_mask);
868- (level - headroom.min(range)).max(threshold - (headroom - range).max(0.0))
877+ (level - headroom.min(range) + MASKING_OFFSET_DB)
878+ .max(threshold - (headroom - range).max(0.0))
869879 })
870880 .collect();
871881 let y: Vec<i32> = target