Keep masked band energy
Browse files at this commite0b760b5fe010cb9d786568d70553be500beb41bParent: f70e8e6- fix
- keep masked band energy
1 changed file +28 -1
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 | 81 | const RESIDUE_BOOK_DIM: [usize; CLASSES] = [0, 4, 2, 2, 1, 1, 1, 1, 1]; |
| 82 | 82 | /// The vector books save packet bits but add setup and make the rate loop spend |
| 83 | 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 | 85 | /// Headroom in dB the widest residue book can actually state: 20 log10 127 |
| 86 | 86 | /// is 42, but a coupled angle channel is a difference of two magnitudes, so a |
| 87 | 87 | /// 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 | 106 | /// rate/quality knob: raising the curve zeroes masked coefficients earlier, |
| 107 | 107 | /// while peaks still ride as residue above it. |
| 108 | 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 | 112 | /// A short block gets no co-masking cap: a bin it drops is an error spread |
| 110 | 113 | /// across its whole window, the half before an onset included, and that is |
| 111 | 114 | /// the pre-echo the short block exists to prevent (44.1k mono, onset on the |
| @@ -699,6 +702,30 @@ impl VorbisEncoder { | ||
| 699 | 702 | .collect::<Vec<i32>>() |
| 700 | 703 | }) |
| 701 | 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 | 729 | for &(magnitude, angle) in &self.coupling { |
| 703 | 730 | let mut left = std::mem::take(&mut quantised[magnitude]); |
| 704 | 731 | let mut right = std::mem::take(&mut quantised[angle]); |