42 dB residue range in short blocks, with a clamp-guarded floor refit
Browse files at this commita554dfafa70ae009c0619d8fbab021a4d6786fc8Parent: 14b96be- fix
- 42 dB residue range in short blocks, with a clamp-guarded floor refit
- onset test sweeps phase, hop offset and per-channel content
2 changed files +133 -37
- crates/ec-vorbis/src/encode.rs +112 -26
- crates/ec-vorbis/tests/oracle.rs +21 -11
crates/ec-vorbis/src/encode.rsmodified+112 -26
| @@ -78,6 +78,12 @@ const CLASS_RANGE: [i32; CLASSES] = [0, 1, 4, 16, 127]; | |||
| 78 | /// the threshold and code as class 0 whatever the step — gains bins to spend | 78 | /// the threshold and code as class 0 whatever the step — gains bins to spend |
| 79 | /// bits on rather than a finer step nothing can represent. | 79 | /// bits on rather than a finer step nothing can represent. |
| 80 | const HEADROOM_RANGE: f64 = 36.0; | 80 | const HEADROOM_RANGE: f64 = 36.0; |
| 81 | +/// A short block's range: the full 42 dB. Its step is what sets the pre-echo | ||
| 82 | +/// left inside the block that carries an onset, and 6 dB of it is the | ||
| 83 | +/// difference between a leak twice the reference's and one under it. | ||
| 84 | +/// Anything the wider range cannot state (a pair's angle, a peak at a floor | ||
| 85 | +/// region's edge) is caught by the clamp refit in `encode_block`. | ||
| 86 | +const HEADROOM_RANGE_SHORT: f64 = 42.0; | ||
| 81 | /// Samples per transient-detection tick — the short block's own hop, so a | 87 | /// Samples per transient-detection tick — the short block's own hop, so a |
| 82 | /// tick lines up with the finest time resolution the encoder has. | 88 | /// tick lines up with the finest time resolution the encoder has. |
| 83 | const TICK: usize = BLOCK_SHORT / 4; | 89 | const TICK: usize = BLOCK_SHORT / 4; |
| @@ -167,6 +173,8 @@ struct BlockConfig { | |||
| 167 | partition: usize, | 173 | partition: usize, |
| 168 | floor: Floor1, | 174 | floor: Floor1, |
| 169 | ath: Vec<f64>, | 175 | ath: Vec<f64>, |
| 176 | + /// Headroom the residue range lets this block size state. | ||
| 177 | + range: f64, | ||
| 170 | } | 178 | } |
| 171 | 179 | ||
| 172 | /// Vorbis I encoder. | 180 | /// Vorbis I encoder. |
| @@ -236,8 +244,20 @@ impl VorbisEncoder { | |||
| 236 | let floor_book = design_floor_book(); | 244 | let floor_book = design_floor_book(); |
| 237 | let class_book = design_class_book(); | 245 | let class_book = design_class_book(); |
| 238 | let residue_books = design_residue_books(); | 246 | let residue_books = design_residue_books(); |
| 239 | - let long = build_block_config(HALF_LONG, PARTITION_LONG, FLOOR_POINTS_LONG, &config); | 247 | + let long = build_block_config( |
| 240 | - let short = build_block_config(HALF_SHORT, PARTITION_SHORT, FLOOR_POINTS_SHORT, &config); | 248 | + HALF_LONG, |
| 249 | + PARTITION_LONG, | ||
| 250 | + FLOOR_POINTS_LONG, | ||
| 251 | + HEADROOM_RANGE, | ||
| 252 | + &config, | ||
| 253 | + ); | ||
| 254 | + let short = build_block_config( | ||
| 255 | + HALF_SHORT, | ||
| 256 | + PARTITION_SHORT, | ||
| 257 | + FLOOR_POINTS_SHORT, | ||
| 258 | + HEADROOM_RANGE_SHORT, | ||
| 259 | + &config, | ||
| 260 | + ); | ||
| 241 | 261 | ||
| 242 | let headers = write_headers( | 262 | let headers = write_headers( |
| 243 | &config, | 263 | &config, |
| @@ -522,32 +542,66 @@ impl VorbisEncoder { | |||
| 522 | 542 | ||
| 523 | // Floor per channel — one shared floor per coupled pair, so the two | 543 | // Floor per channel — one shared floor per coupled pair, so the two |
| 524 | // normalised spectra are on the same scale and the angle channel is | 544 | // normalised spectra are on the same scale and the angle channel is |
| 525 | - // small where the pair agrees. | 545 | + // small where the pair agrees. The floor is set per X point from the |
| 546 | + // peak of the region that point owns, then drawn as straight lines | ||
| 547 | + // between points: a peak at a region's edge can sit further under the | ||
| 548 | + // curve than the range states and clamp. Such a block lifts the two | ||
| 549 | + // points either side of every clamping bin by the excess and refits | ||
| 550 | + // until nothing clamps, so the curve hugs the peaks the range is | ||
| 551 | + // measured from. | ||
| 552 | + let limit = CLASS_RANGE[CLASSES - 1]; | ||
| 553 | + let mut lift: Vec<Vec<f64>> = vec![vec![1.0; bc.floor.x_list.len()]; channels]; | ||
| 554 | + // Per X point, the span of bins whose curve it has a hand in. | ||
| 555 | + let spans: Vec<(usize, usize)> = bc | ||
| 556 | + .floor | ||
| 557 | + .x_list | ||
| 558 | + .iter() | ||
| 559 | + .map(|&x| { | ||
| 560 | + let below = bc | ||
| 561 | + .floor | ||
| 562 | + .x_list | ||
| 563 | + .iter() | ||
| 564 | + .filter(|&&o| o < x) | ||
| 565 | + .max() | ||
| 566 | + .map_or(0, |&o| o as usize); | ||
| 567 | + let above = bc | ||
| 568 | + .floor | ||
| 569 | + .x_list | ||
| 570 | + .iter() | ||
| 571 | + .filter(|&&o| o > x) | ||
| 572 | + .min() | ||
| 573 | + .map_or(half, |&o| o as usize); | ||
| 574 | + (below, above) | ||
| 575 | + }) | ||
| 576 | + .collect(); | ||
| 577 | + let mut passes = 0; | ||
| 578 | + let (floor_values, quantised) = loop { | ||
| 526 | let mut curves: Vec<Vec<f32>> = vec![Vec::new(); channels]; | 579 | let mut curves: Vec<Vec<f32>> = vec![Vec::new(); channels]; |
| 527 | let mut floor_values: Vec<Vec<i32>> = vec![Vec::new(); channels]; | 580 | let mut floor_values: Vec<Vec<i32>> = vec![Vec::new(); channels]; |
| 528 | - let mut coded = vec![false; channels]; | 581 | + let mut fit = |group: &[usize]| { |
| 529 | - for &(magnitude, angle) in &self.coupling { | 582 | + let group_spectra: Vec<&Vec<f32>> = group.iter().map(|&c| &spectra[c]).collect(); |
| 530 | - let peaks = peaks_of(&bc.floor, half, &[&spectra[magnitude], &spectra[angle]]); | 583 | + let mut peaks = peaks_of(&bc.floor, half, &group_spectra); |
| 531 | - let (y, step2) = fit_floor(&bc.floor, &bc.ath, self.headroom, &peaks); | 584 | + for &c in group { |
| 585 | + for (peak, &boost) in peaks.iter_mut().zip(&lift[c]) { | ||
| 586 | + *peak *= boost; | ||
| 587 | + } | ||
| 588 | + } | ||
| 589 | + let (y, step2) = fit_floor(&bc.floor, &bc.ath, self.headroom, bc.range, &peaks); | ||
| 532 | let mut curve = vec![0.0f32; half]; | 590 | let mut curve = vec![0.0f32; half]; |
| 533 | render_floor1(&bc.floor, &y, &step2, &mut curve); | 591 | render_floor1(&bc.floor, &y, &step2, &mut curve); |
| 534 | - curves[magnitude] = curve.clone(); | 592 | + for &c in group { |
| 535 | - curves[angle] = curve; | 593 | + curves[c] = curve.clone(); |
| 536 | - floor_values[magnitude] = y.clone(); | 594 | + floor_values[c] = y.clone(); |
| 537 | - floor_values[angle] = y; | 595 | + } |
| 596 | + }; | ||
| 597 | + let mut coded = vec![false; channels]; | ||
| 598 | + for &(magnitude, angle) in &self.coupling { | ||
| 599 | + fit(&[magnitude, angle]); | ||
| 538 | coded[magnitude] = true; | 600 | coded[magnitude] = true; |
| 539 | coded[angle] = true; | 601 | coded[angle] = true; |
| 540 | } | 602 | } |
| 541 | - for channel in 0..channels { | 603 | + for channel in (0..channels).filter(|&c| !coded[c]) { |
| 542 | - if coded[channel] { | 604 | + fit(&[channel]); |
| 543 | - continue; | ||
| 544 | - } | ||
| 545 | - let peaks = peaks_of(&bc.floor, half, &[&spectra[channel]]); | ||
| 546 | - let (y, step2) = fit_floor(&bc.floor, &bc.ath, self.headroom, &peaks); | ||
| 547 | - let mut curve = vec![0.0f32; half]; | ||
| 548 | - render_floor1(&bc.floor, &y, &step2, &mut curve); | ||
| 549 | - curves[channel] = curve; | ||
| 550 | - floor_values[channel] = y; | ||
| 551 | } | 605 | } |
| 552 | 606 | ||
| 553 | // Normalise, couple, quantise. | 607 | // Normalise, couple, quantise. |
| @@ -575,12 +629,32 @@ impl VorbisEncoder { | |||
| 575 | quantised[magnitude] = left; | 629 | quantised[magnitude] = left; |
| 576 | quantised[angle] = right; | 630 | quantised[angle] = right; |
| 577 | } | 631 | } |
| 578 | - let limit = CLASS_RANGE[CLASSES - 1]; | 632 | + let mut clamps = false; |
| 633 | + for (channel, values) in quantised.iter().enumerate() { | ||
| 634 | + for (bin, &value) in values.iter().enumerate() { | ||
| 635 | + if value.abs() <= limit { | ||
| 636 | + continue; | ||
| 637 | + } | ||
| 638 | + clamps = true; | ||
| 639 | + let excess = f64::from(value.abs()) / f64::from(limit) * 1.05; | ||
| 640 | + for (point, &(below, above)) in spans.iter().enumerate() { | ||
| 641 | + if bin >= below && bin <= above { | ||
| 642 | + lift[channel][point] = lift[channel][point].max(excess); | ||
| 643 | + } | ||
| 644 | + } | ||
| 645 | + } | ||
| 646 | + } | ||
| 647 | + passes += 1; | ||
| 648 | + // Eight lifts is 8 x 6 dB past anything a 42 dB range can leave over. | ||
| 649 | + if !clamps || passes == 8 { | ||
| 579 | for channel in quantised.iter_mut() { | 650 | for channel in quantised.iter_mut() { |
| 580 | for value in channel.iter_mut() { | 651 | for value in channel.iter_mut() { |
| 581 | *value = (*value).clamp(-limit, limit); | 652 | *value = (*value).clamp(-limit, limit); |
| 582 | } | 653 | } |
| 583 | } | 654 | } |
| 655 | + break (floor_values, quantised); | ||
| 656 | + } | ||
| 657 | + }; | ||
| 584 | 658 | ||
| 585 | let mut out = BitsOut::new(); | 659 | let mut out = BitsOut::new(); |
| 586 | out.bit(false); | 660 | out.bit(false); |
| @@ -658,7 +732,13 @@ fn peaks_of(floor: &Floor1, half: usize, spectra: &[&Vec<f32>]) -> Vec<f64> { | |||
| 658 | /// loop's headroom, then hold the absolute threshold of hearing as a lower | 732 | /// loop's headroom, then hold the absolute threshold of hearing as a lower |
| 659 | /// bound: below it the residue quantises to zero and costs a class-0 | 733 | /// bound: below it the residue quantises to zero and costs a class-0 |
| 660 | /// partition. | 734 | /// partition. |
| 661 | -fn fit_floor(floor: &Floor1, ath: &[f64], headroom: f64, peaks: &[f64]) -> (Vec<i32>, Vec<bool>) { | 735 | +fn fit_floor( |
| 736 | + floor: &Floor1, | ||
| 737 | + ath: &[f64], | ||
| 738 | + headroom: f64, | ||
| 739 | + range: f64, | ||
| 740 | + peaks: &[f64], | ||
| 741 | +) -> (Vec<i32>, Vec<bool>) { | ||
| 662 | let mut db: Vec<f64> = peaks | 742 | let mut db: Vec<f64> = peaks |
| 663 | .iter() | 743 | .iter() |
| 664 | .map(|&peak| 20.0 * (peak.max(1e-9)).log10()) | 744 | .map(|&peak| 20.0 * (peak.max(1e-9)).log10()) |
| @@ -676,7 +756,7 @@ fn fit_floor(floor: &Floor1, ath: &[f64], headroom: f64, peaks: &[f64]) -> (Vec< | |||
| 676 | .iter() | 756 | .iter() |
| 677 | .zip(ath.iter()) | 757 | .zip(ath.iter()) |
| 678 | .map(|(&level, &threshold)| { | 758 | .map(|(&level, &threshold)| { |
| 679 | - (level - headroom.min(HEADROOM_RANGE)).max(threshold - (headroom - HEADROOM_RANGE).max(0.0)) | 759 | + (level - headroom.min(range)).max(threshold - (headroom - range).max(0.0)) |
| 680 | }) | 760 | }) |
| 681 | .collect(); | 761 | .collect(); |
| 682 | let y: Vec<i32> = target | 762 | let y: Vec<i32> = target |
| @@ -900,7 +980,13 @@ fn build_floor(half: usize, points: usize) -> Floor1 { | |||
| 900 | } | 980 | } |
| 901 | 981 | ||
| 902 | /// Build one block size's floor and ATH curve. | 982 | /// Build one block size's floor and ATH curve. |
| 903 | -fn build_block_config(half: usize, partition: usize, points: usize, config: &EncoderConfig) -> BlockConfig { | 983 | +fn build_block_config( |
| 984 | + half: usize, | ||
| 985 | + partition: usize, | ||
| 986 | + points: usize, | ||
| 987 | + range: f64, | ||
| 988 | + config: &EncoderConfig, | ||
| 989 | +) -> BlockConfig { | ||
| 904 | let floor = build_floor(half, points); | 990 | let floor = build_floor(half, points); |
| 905 | let ath = floor | 991 | let ath = floor |
| 906 | .x_list | 992 | .x_list |
| @@ -910,7 +996,7 @@ fn build_block_config(half: usize, partition: usize, points: usize, config: &Enc | |||
| 910 | absolute_threshold(hz) | 996 | absolute_threshold(hz) |
| 911 | }) | 997 | }) |
| 912 | .collect(); | 998 | .collect(); |
| 913 | - BlockConfig { half, partition, floor, ath } | 999 | + BlockConfig { half, partition, floor, ath, range } |
| 914 | } | 1000 | } |
| 915 | 1001 | ||
| 916 | /// Folded floor amplitudes: mostly small corrections, occasionally an escape | 1002 | /// Folded floor amplitudes: mostly small corrections, occasionally an escape |
crates/ec-vorbis/tests/oracle.rsmodified+21 -11
| @@ -561,21 +561,31 @@ fn busy(channels: usize, rate: u32, samples: usize) -> Vec<Vec<f32>> { | |||
| 561 | /// does. | 561 | /// does. |
| 562 | #[test] | 562 | #[test] |
| 563 | fn onset_after_silence_has_no_pre_echo() { | 563 | fn onset_after_silence_has_no_pre_echo() { |
| 564 | - // Two onsets: one off the block grid (0.9137 s lands mid short block) and | 564 | + // Onsets swept by phase (fractions of a second, so they land anywhere |
| 565 | - // one on it. The gap is measured over the WHOLE silence up to the onset, | 565 | + // relative to the long hop) and by offset from the long-hop grid itself, |
| 566 | - // so a leak in the last few hundred samples before it cannot hide behind a | 566 | + // since the short run's placement is decided per hop. Stereo carries |
| 567 | - // guard band, and the peak is barred too. | 567 | + // distinct content per channel — a different tone on the right, starting |
| 568 | + // 11 samples later — so coupling cannot hide a per-channel leak. The gap | ||
| 569 | + // is measured over the WHOLE silence up to the onset, so a leak in the | ||
| 570 | + // last few hundred samples before it cannot hide behind a guard band, and | ||
| 571 | + // the peak is barred too. | ||
| 572 | + let amp = 0.7f64; | ||
| 568 | for rate in [44_100u32, 48_000] { | 573 | for rate in [44_100u32, 48_000] { |
| 574 | + let grid = 16 * 1024usize; | ||
| 575 | + let onsets = [0.3333f64, 0.5, 0.7321, 0.9137, 1.0] | ||
| 576 | + .into_iter() | ||
| 577 | + .map(|s| (s * f64::from(rate)) as usize) | ||
| 578 | + .chain([0usize, 37, 512, 1000, 1023].into_iter().map(|o| grid + o)); | ||
| 579 | + for onset in onsets { | ||
| 569 | for channels in [1usize, 2] { | 580 | for channels in [1usize, 2] { |
| 570 | - for onset_s in [0.9137f64, 1.0] { | ||
| 571 | - let onset = (onset_s * f64::from(rate)) as usize; | ||
| 572 | let tone_len = rate as usize; | 581 | let tone_len = rate as usize; |
| 573 | - let mut source = vec![vec![0.0f32; onset + tone_len]; channels]; | 582 | + let mut source = vec![vec![0.0f32; onset + tone_len + 11]; channels]; |
| 583 | + for (c, plane) in source.iter_mut().enumerate() { | ||
| 584 | + let hz = 1_000.0 + 370.0 * c as f64; | ||
| 585 | + let start = onset + 11 * c; | ||
| 574 | for i in 0..tone_len { | 586 | for i in 0..tone_len { |
| 575 | let t = i as f64 / f64::from(rate); | 587 | let t = i as f64 / f64::from(rate); |
| 576 | - let v = (0.7 * (2.0 * std::f64::consts::PI * 1_000.0 * t).sin()) as f32; | 588 | + plane[start + i] = (amp * (2.0 * std::f64::consts::PI * hz * t).sin()) as f32; |
| 577 | - for plane in &mut source { | ||
| 578 | - plane[onset + i] = v; | ||
| 579 | } | 589 | } |
| 580 | } | 590 | } |
| 581 | let name = format!("onset-{rate}-{channels}ch-{onset}.ogg"); | 591 | let name = format!("onset-{rate}-{channels}ch-{onset}.ogg"); |
| @@ -607,7 +617,7 @@ fn onset_after_silence_has_no_pre_echo() { | |||
| 607 | gap_rms <= 0.002 * tone_rms, | 617 | gap_rms <= 0.002 * tone_rms, |
| 608 | "{name}/{label} ch{c}: gap RMS {gap_rms:.6} vs tone RMS {tone_rms:.6}" | 618 | "{name}/{label} ch{c}: gap RMS {gap_rms:.6} vs tone RMS {tone_rms:.6}" |
| 609 | ); | 619 | ); |
| 610 | - assert!(peak <= 0.007, "{name}/{label} ch{c}: gap peak {peak:.4}"); | 620 | + assert!(f64::from(peak) <= 0.01 * amp, "{name}/{label} ch{c}: gap peak {peak:.4}"); |
| 611 | } | 621 | } |
| 612 | }; | 622 | }; |
| 613 | let (ours, _) = our_decode(&path); | 623 | let (ours, _) = our_decode(&path); |