Protect transient floors
Browse files at this commit3506a6c3d2b81cd93be554143cde51ea4c239133Parent: 78cb579- fix
- protect transient floors
1 changed file +27 -10
- crates/ec-vorbis/src/encode.rs +27 -10
crates/ec-vorbis/src/encode.rsmodified+27 -10
| @@ -617,15 +617,17 @@ impl VorbisEncoder { | |||
| 617 | // until nothing clamps, so the curve hugs the peaks the range is | 617 | // until nothing clamps, so the curve hugs the peaks the range is |
| 618 | // measured from. | 618 | // measured from. |
| 619 | let limit = CLASS_RANGE[CLASSES - 1]; | 619 | let limit = CLASS_RANGE[CLASSES - 1]; |
| 620 | - // A short block's precision is what bounds the pre-echo inside the | 620 | + // Blocks next to short windows bound pre-echo too: the transition-out |
| 621 | - // window that carries an onset, so it does not inherit whatever | 621 | + // long block still overlaps the samples just before the onset, so high |
| 622 | - // headroom the music before it left the rate loop at: at 6 ch / 96 kHz | 622 | + // bitrate transients get extra minimum precision. |
| 623 | - // the loop sat at 22 dB when the onset came and leaked -36 dB across | 623 | + let transient_headroom = if self.config.bitrate_bps >= 128_000 { |
| 624 | - // the half-window before it. A short run is 16 blocks once a second | 624 | + 54.0 |
| 625 | - // or so on music, so the extra bits are noise to the loop. | 625 | + } else { |
| 626 | - let headroom = match plan.is_long { | 626 | + HEADROOM_SHORT_MIN |
| 627 | + }; | ||
| 628 | + let headroom = match plan.is_long && plan.prev_long && plan.next_long { | ||
| 627 | true => self.headroom, | 629 | true => self.headroom, |
| 628 | - false => self.headroom.max(HEADROOM_SHORT_MIN), | 630 | + false => self.headroom.max(transient_headroom), |
| 629 | }; | 631 | }; |
| 630 | let mut lift: Vec<Vec<f64>> = vec![vec![1.0; bc.floor.x_list.len()]; channels]; | 632 | let mut lift: Vec<Vec<f64>> = vec![vec![1.0; bc.floor.x_list.len()]; channels]; |
| 631 | // Per X point, the span of bins whose curve it has a hand in. | 633 | // Per X point, the span of bins whose curve it has a hand in. |
| @@ -652,6 +654,12 @@ impl VorbisEncoder { | |||
| 652 | }) | 654 | }) |
| 653 | .collect(); | 655 | .collect(); |
| 654 | let mut passes = 0; | 656 | let mut passes = 0; |
| 657 | + let steady = plan.is_long && plan.prev_long && plan.next_long; | ||
| 658 | + let masking_offset = if !steady && self.config.bitrate_bps >= 128_000 { | ||
| 659 | + 0.0 | ||
| 660 | + } else { | ||
| 661 | + MASKING_OFFSET_DB | ||
| 662 | + }; | ||
| 655 | let (floor_values, quantised) = loop { | 663 | let (floor_values, quantised) = loop { |
| 656 | let mut curves: Vec<Vec<f32>> = vec![Vec::new(); channels]; | 664 | let mut curves: Vec<Vec<f32>> = vec![Vec::new(); channels]; |
| 657 | let mut floor_values: Vec<Vec<i32>> = vec![Vec::new(); channels]; | 665 | let mut floor_values: Vec<Vec<i32>> = vec![Vec::new(); channels]; |
| @@ -664,7 +672,14 @@ impl VorbisEncoder { | |||
| 664 | } | 672 | } |
| 665 | } | 673 | } |
| 666 | let (y, step2) = fit_floor( | 674 | let (y, step2) = fit_floor( |
| 667 | - &bc.floor, &bc.ath, &bc.bark, headroom, bc.range, bc.co_mask, &peaks, | 675 | + &bc.floor, |
| 676 | + &bc.ath, | ||
| 677 | + &bc.bark, | ||
| 678 | + headroom, | ||
| 679 | + bc.range, | ||
| 680 | + bc.co_mask, | ||
| 681 | + masking_offset, | ||
| 682 | + &peaks, | ||
| 668 | ); | 683 | ); |
| 669 | let mut curve = vec![0.0f32; half]; | 684 | let mut curve = vec![0.0f32; half]; |
| 670 | render_floor1(&bc.floor, &y, &step2, &mut curve); | 685 | render_floor1(&bc.floor, &y, &step2, &mut curve); |
| @@ -874,6 +889,7 @@ fn fit_floor( | |||
| 874 | headroom: f64, | 889 | headroom: f64, |
| 875 | range: f64, | 890 | range: f64, |
| 876 | co_mask: f64, | 891 | co_mask: f64, |
| 892 | + masking_offset: f64, | ||
| 877 | peaks: &[f64], | 893 | peaks: &[f64], |
| 878 | ) -> (Vec<i32>, Vec<bool>) { | 894 | ) -> (Vec<i32>, Vec<bool>) { |
| 879 | let db: Vec<f64> = peaks | 895 | let db: Vec<f64> = peaks |
| @@ -910,7 +926,8 @@ fn fit_floor( | |||
| 910 | .zip(ath.iter()) | 926 | .zip(ath.iter()) |
| 911 | .map(|(&level, &threshold)| { | 927 | .map(|(&level, &threshold)| { |
| 912 | let threshold = threshold.max(loudest - co_mask); | 928 | let threshold = threshold.max(loudest - co_mask); |
| 913 | - (level - headroom.min(range) + MASKING_OFFSET_DB) | 929 | + let offset = (masking_offset - (headroom - range).max(0.0)).max(0.0); |
| 930 | + (level - headroom.min(range) + offset) | ||
| 914 | .max(threshold - (headroom - range).max(0.0)) | 931 | .max(threshold - (headroom - range).max(0.0)) |
| 915 | }) | 932 | }) |
| 916 | .collect(); | 933 | .collect(); |