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];
81 const RESIDUE_BOOK_DIM: [usize; CLASSES] = [0, 4, 2, 2, 1, 1, 1, 1, 1];81 const RESIDUE_BOOK_DIM: [usize; CLASSES] = [0, 4, 2, 2, 1, 1, 1, 1, 1];
82 /// The vector books save packet bits but add setup and make the rate loop spend82 /// The vector books save packet bits but add setup and make the rate loop spend
83 /// harder; this keeps the realised rate at the caller's target.83 /// 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;
85 /// Headroom in dB the widest residue book can actually state: 20 log10 12785 /// Headroom in dB the widest residue book can actually state: 20 log10 127
86 /// is 42, but a coupled angle channel is a difference of two magnitudes, so a86 /// is 42, but a coupled angle channel is a difference of two magnitudes, so a
87 /// pair's step must leave half that range — 63, 36 dB. Past this the rate loop's extra headroom lowers the absolute-threshold87 /// 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;
102 /// three tracks at 128 kbps: 50 dB kept the accuracy win without inflating the102 /// three tracks at 128 kbps: 50 dB kept the accuracy win without inflating the
103 /// files.103 /// files.
104 const CO_MASK_RANGE: f64 = 50.0;104 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;
105 /// A short block gets no co-masking cap: a bin it drops is an error spread109 /// A short block gets no co-masking cap: a bin it drops is an error spread
106 /// across its whole window, the half before an onset included, and that is110 /// across its whole window, the half before an onset included, and that is
107 /// the pre-echo the short block exists to prevent (44.1k mono, onset on the111 /// the pre-echo the short block exists to prevent (44.1k mono, onset on the
@@ -676,17 +680,22 @@ impl VorbisEncoder {
676 fit(&[channel]);680 fit(&[channel]);
677 }681 }
678 682
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.
680 let mut quantised: Vec<Vec<i32>> = (0..channels)687 let mut quantised: Vec<Vec<i32>> = (0..channels)
681 .map(|channel| {688 .map(|channel| {
682 spectra[channel]689 spectra[channel]
683 .iter()690 .iter()
684 .zip(curves[channel].iter())691 .zip(curves[channel].iter())
685- .map(|(&value, &floor)| match floor > 0.0 {692+ .map(|(&value, &floor)| {
686- true => value / floor,693+ if floor <= 0.0 || value.abs() <= floor {
687- false => 0.0,694+ 0
695+ } else {
696+ (value / floor).round() as i32
697+ }
688 })698 })
689- .map(|normalised| normalised.round() as i32)
690 .collect::<Vec<i32>>()699 .collect::<Vec<i32>>()
691 })700 })
692 .collect();701 .collect();
@@ -865,7 +874,8 @@ fn fit_floor(
865 .zip(ath.iter())874 .zip(ath.iter())
866 .map(|(&level, &threshold)| {875 .map(|(&level, &threshold)| {
867 let threshold = threshold.max(loudest - co_mask);876 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))
869 })879 })
870 .collect();880 .collect();
871 let y: Vec<i32> = target881 let y: Vec<i32> = target