forge
Explore
Log in

Don't you dare go Hollow

Keep masked band energy

Browse files at this commit

e0b760b5fe010cb9d786568d70553be500beb41bAhmet Kaan GümüşParent: f70e8e6
  • fix
    • keep masked band energy
Show full message
fix(vorbis): keep masked band energy
crates/ec-vorbis/src/encode.rsmodified+28 -1
@@ -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.96;84+const RATE_TARGET_SCALE: f64 = 0.97;
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
@@ -106,6 +106,9 @@ const CO_MASK_RANGE: f64 = 50.0;
106 /// rate/quality knob: raising the curve zeroes masked coefficients earlier,106 /// rate/quality knob: raising the curve zeroes masked coefficients earlier,
107 /// while peaks still ride as residue above it.107 /// while peaks still ride as residue above it.
108 const MASKING_OFFSET_DB: f64 = 9.0;108 const MASKING_OFFSET_DB: f64 = 9.0;
109+/// All-zero partition guard: if the masked band still has a bin within this
110+/// fraction of the floor, keep that bin as one sign-only residue sample.
111+const NOISE_NORMALISE_MIN_RATIO: f32 = 0.5;
109 /// A short block gets no co-masking cap: a bin it drops is an error spread112 /// A short block gets no co-masking cap: a bin it drops is an error spread
110 /// across its whole window, the half before an onset included, and that is113 /// across its whole window, the half before an onset included, and that is
111 /// the pre-echo the short block exists to prevent (44.1k mono, onset on the114 /// the pre-echo the short block exists to prevent (44.1k mono, onset on the
@@ -699,6 +702,30 @@ impl VorbisEncoder {
699 .collect::<Vec<i32>>()702 .collect::<Vec<i32>>()
700 })703 })
701 .collect();704 .collect();
705+ for channel in 0..channels {
706+ for (band, values) in quantised[channel].chunks_mut(partition).enumerate() {
707+ if values.iter().any(|&value| value != 0) {
708+ continue;
709+ }
710+ let start = band * partition;
711+ let end = (start + values.len()).min(half);
712+ let mut peak = (NOISE_NORMALISE_MIN_RATIO, 0usize, 0i32);
713+ for bin in start..end {
714+ let floor = curves[channel][bin];
715+ if floor <= 0.0 {
716+ continue;
717+ }
718+ let value = spectra[channel][bin];
719+ let ratio = value.abs() / floor;
720+ if ratio > peak.0 {
721+ peak = (ratio, bin - start, value.signum() as i32);
722+ }
723+ }
724+ if peak.2 != 0 {
725+ values[peak.1] = peak.2;
726+ }
727+ }
728+ }
702 for &(magnitude, angle) in &self.coupling {729 for &(magnitude, angle) in &self.coupling {
703 let mut left = std::mem::take(&mut quantised[magnitude]);730 let mut left = std::mem::take(&mut quantised[magnitude]);
704 let mut right = std::mem::take(&mut quantised[angle]);731 let mut right = std::mem::take(&mut quantised[angle]);