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 +170 -74
- crates/ec-vorbis/src/encode.rs +146 -60
- crates/ec-vorbis/tests/oracle.rs +24 -14
crates/ec-vorbis/src/encode.rsmodified+146 -60
| @@ -78,6 +78,12 @@ const CLASS_RANGE: [i32; CLASSES] = [0, 1, 4, 16, 127]; | ||
| 78 | 78 | /// the threshold and code as class 0 whatever the step — gains bins to spend |
| 79 | 79 | /// bits on rather than a finer step nothing can represent. |
| 80 | 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 | 87 | /// Samples per transient-detection tick — the short block's own hop, so a |
| 82 | 88 | /// tick lines up with the finest time resolution the encoder has. |
| 83 | 89 | const TICK: usize = BLOCK_SHORT / 4; |
| @@ -167,6 +173,8 @@ struct BlockConfig { | ||
| 167 | 173 | partition: usize, |
| 168 | 174 | floor: Floor1, |
| 169 | 175 | ath: Vec<f64>, |
| 176 | + /// Headroom the residue range lets this block size state. | |
| 177 | + range: f64, | |
| 170 | 178 | } |
| 171 | 179 | |
| 172 | 180 | /// Vorbis I encoder. |
| @@ -236,8 +244,20 @@ impl VorbisEncoder { | ||
| 236 | 244 | let floor_book = design_floor_book(); |
| 237 | 245 | let class_book = design_class_book(); |
| 238 | 246 | let residue_books = design_residue_books(); |
| 239 | - let long = build_block_config(HALF_LONG, PARTITION_LONG, FLOOR_POINTS_LONG, &config); | |
| 240 | - let short = build_block_config(HALF_SHORT, PARTITION_SHORT, FLOOR_POINTS_SHORT, &config); | |
| 247 | + let long = build_block_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 | 262 | let headers = write_headers( |
| 243 | 263 | &config, |
| @@ -522,65 +542,119 @@ impl VorbisEncoder { | ||
| 522 | 542 | |
| 523 | 543 | // Floor per channel — one shared floor per coupled pair, so the two |
| 524 | 544 | // normalised spectra are on the same scale and the angle channel is |
| 525 | - // small where the pair agrees. | |
| 526 | - let mut curves: Vec<Vec<f32>> = vec![Vec::new(); channels]; | |
| 527 | - let mut floor_values: Vec<Vec<i32>> = vec![Vec::new(); channels]; | |
| 528 | - let mut coded = vec![false; channels]; | |
| 529 | - for &(magnitude, angle) in &self.coupling { | |
| 530 | - let peaks = peaks_of(&bc.floor, half, &[&spectra[magnitude], &spectra[angle]]); | |
| 531 | - let (y, step2) = fit_floor(&bc.floor, &bc.ath, self.headroom, &peaks); | |
| 532 | - let mut curve = vec![0.0f32; half]; | |
| 533 | - render_floor1(&bc.floor, &y, &step2, &mut curve); | |
| 534 | - curves[magnitude] = curve.clone(); | |
| 535 | - curves[angle] = curve; | |
| 536 | - floor_values[magnitude] = y.clone(); | |
| 537 | - floor_values[angle] = y; | |
| 538 | - coded[magnitude] = true; | |
| 539 | - coded[angle] = true; | |
| 540 | - } | |
| 541 | - for channel in 0..channels { | |
| 542 | - if coded[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 | - } | |
| 552 | - | |
| 553 | - // Normalise, couple, quantise. | |
| 554 | - let mut quantised: Vec<Vec<i32>> = (0..channels) | |
| 555 | - .map(|channel| { | |
| 556 | - spectra[channel] | |
| 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 | |
| 557 | 570 | .iter() |
| 558 | - .zip(curves[channel].iter()) | |
| 559 | - .map(|(&value, &floor)| match floor > 0.0 { | |
| 560 | - true => value / floor, | |
| 561 | - false => 0.0, | |
| 562 | - }) | |
| 563 | - .map(|normalised| normalised.round() as i32) | |
| 564 | - .collect::<Vec<i32>>() | |
| 571 | + .filter(|&&o| o > x) | |
| 572 | + .min() | |
| 573 | + .map_or(half, |&o| o as usize); | |
| 574 | + (below, above) | |
| 565 | 575 | }) |
| 566 | 576 | .collect(); |
| 567 | - for &(magnitude, angle) in &self.coupling { | |
| 568 | - let mut left = std::mem::take(&mut quantised[magnitude]); | |
| 569 | - let mut right = std::mem::take(&mut quantised[angle]); | |
| 570 | - for (l, r) in left.iter_mut().zip(right.iter_mut()) { | |
| 571 | - let (m, a) = couple(*l, *r); | |
| 572 | - *l = m; | |
| 573 | - *r = a; | |
| 577 | + let mut passes = 0; | |
| 578 | + let (floor_values, quantised) = loop { | |
| 579 | + let mut curves: Vec<Vec<f32>> = vec![Vec::new(); channels]; | |
| 580 | + let mut floor_values: Vec<Vec<i32>> = vec![Vec::new(); channels]; | |
| 581 | + let mut fit = |group: &[usize]| { | |
| 582 | + let group_spectra: Vec<&Vec<f32>> = group.iter().map(|&c| &spectra[c]).collect(); | |
| 583 | + let mut peaks = peaks_of(&bc.floor, half, &group_spectra); | |
| 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); | |
| 590 | + let mut curve = vec![0.0f32; half]; | |
| 591 | + render_floor1(&bc.floor, &y, &step2, &mut curve); | |
| 592 | + for &c in group { | |
| 593 | + curves[c] = curve.clone(); | |
| 594 | + floor_values[c] = y.clone(); | |
| 595 | + } | |
| 596 | + }; | |
| 597 | + let mut coded = vec![false; channels]; | |
| 598 | + for &(magnitude, angle) in &self.coupling { | |
| 599 | + fit(&[magnitude, angle]); | |
| 600 | + coded[magnitude] = true; | |
| 601 | + coded[angle] = true; | |
| 574 | 602 | } |
| 575 | - quantised[magnitude] = left; | |
| 576 | - quantised[angle] = right; | |
| 577 | - } | |
| 578 | - let limit = CLASS_RANGE[CLASSES - 1]; | |
| 579 | - for channel in quantised.iter_mut() { | |
| 580 | - for value in channel.iter_mut() { | |
| 581 | - *value = (*value).clamp(-limit, limit); | |
| 603 | + for channel in (0..channels).filter(|&c| !coded[c]) { | |
| 604 | + fit(&[channel]); | |
| 582 | 605 | } |
| 583 | - } | |
| 606 | + | |
| 607 | + // Normalise, couple, quantise. | |
| 608 | + let mut quantised: Vec<Vec<i32>> = (0..channels) | |
| 609 | + .map(|channel| { | |
| 610 | + spectra[channel] | |
| 611 | + .iter() | |
| 612 | + .zip(curves[channel].iter()) | |
| 613 | + .map(|(&value, &floor)| match floor > 0.0 { | |
| 614 | + true => value / floor, | |
| 615 | + false => 0.0, | |
| 616 | + }) | |
| 617 | + .map(|normalised| normalised.round() as i32) | |
| 618 | + .collect::<Vec<i32>>() | |
| 619 | + }) | |
| 620 | + .collect(); | |
| 621 | + for &(magnitude, angle) in &self.coupling { | |
| 622 | + let mut left = std::mem::take(&mut quantised[magnitude]); | |
| 623 | + let mut right = std::mem::take(&mut quantised[angle]); | |
| 624 | + for (l, r) in left.iter_mut().zip(right.iter_mut()) { | |
| 625 | + let (m, a) = couple(*l, *r); | |
| 626 | + *l = m; | |
| 627 | + *r = a; | |
| 628 | + } | |
| 629 | + quantised[magnitude] = left; | |
| 630 | + quantised[angle] = right; | |
| 631 | + } | |
| 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 { | |
| 650 | + for channel in quantised.iter_mut() { | |
| 651 | + for value in channel.iter_mut() { | |
| 652 | + *value = (*value).clamp(-limit, limit); | |
| 653 | + } | |
| 654 | + } | |
| 655 | + break (floor_values, quantised); | |
| 656 | + } | |
| 657 | + }; | |
| 584 | 658 | |
| 585 | 659 | let mut out = BitsOut::new(); |
| 586 | 660 | out.bit(false); |
| @@ -658,7 +732,13 @@ fn peaks_of(floor: &Floor1, half: usize, spectra: &[&Vec<f32>]) -> Vec<f64> { | ||
| 658 | 732 | /// loop's headroom, then hold the absolute threshold of hearing as a lower |
| 659 | 733 | /// bound: below it the residue quantises to zero and costs a class-0 |
| 660 | 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 | 742 | let mut db: Vec<f64> = peaks |
| 663 | 743 | .iter() |
| 664 | 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 | 756 | .iter() |
| 677 | 757 | .zip(ath.iter()) |
| 678 | 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 | 761 | .collect(); |
| 682 | 762 | let y: Vec<i32> = target |
| @@ -900,7 +980,13 @@ fn build_floor(half: usize, points: usize) -> Floor1 { | ||
| 900 | 980 | } |
| 901 | 981 | |
| 902 | 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 | 990 | let floor = build_floor(half, points); |
| 905 | 991 | let ath = floor |
| 906 | 992 | .x_list |
| @@ -910,7 +996,7 @@ fn build_block_config(half: usize, partition: usize, points: usize, config: &Enc | ||
| 910 | 996 | absolute_threshold(hz) |
| 911 | 997 | }) |
| 912 | 998 | .collect(); |
| 913 | - BlockConfig { half, partition, floor, ath } | |
| 999 | + BlockConfig { half, partition, floor, ath, range } | |
| 914 | 1000 | } |
| 915 | 1001 | |
| 916 | 1002 | /// Folded floor amplitudes: mostly small corrections, occasionally an escape |
crates/ec-vorbis/tests/oracle.rsmodified+24 -14
| @@ -561,21 +561,31 @@ fn busy(channels: usize, rate: u32, samples: usize) -> Vec<Vec<f32>> { | ||
| 561 | 561 | /// does. |
| 562 | 562 | #[test] |
| 563 | 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 | |
| 565 | - // one on it. The gap is measured over the WHOLE silence up to the onset, | |
| 566 | - // so a leak in the last few hundred samples before it cannot hide behind a | |
| 567 | - // guard band, and the peak is barred too. | |
| 564 | + // Onsets swept by phase (fractions of a second, so they land anywhere | |
| 565 | + // relative to the long hop) and by offset from the long-hop grid itself, | |
| 566 | + // since the short run's placement is decided per hop. Stereo carries | |
| 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 | 573 | for rate in [44_100u32, 48_000] { |
| 569 | - for channels in [1usize, 2] { | |
| 570 | - for onset_s in [0.9137f64, 1.0] { | |
| 571 | - let onset = (onset_s * f64::from(rate)) as usize; | |
| 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 { | |
| 580 | + for channels in [1usize, 2] { | |
| 572 | 581 | let tone_len = rate as usize; |
| 573 | - let mut source = vec![vec![0.0f32; onset + tone_len]; channels]; | |
| 574 | - for i in 0..tone_len { | |
| 575 | - 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; | |
| 577 | - for plane in &mut source { | |
| 578 | - plane[onset + i] = v; | |
| 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; | |
| 586 | + for i in 0..tone_len { | |
| 587 | + let t = i as f64 / f64::from(rate); | |
| 588 | + plane[start + i] = (amp * (2.0 * std::f64::consts::PI * hz * t).sin()) as f32; | |
| 579 | 589 | } |
| 580 | 590 | } |
| 581 | 591 | let name = format!("onset-{rate}-{channels}ch-{onset}.ogg"); |
| @@ -607,7 +617,7 @@ fn onset_after_silence_has_no_pre_echo() { | ||
| 607 | 617 | gap_rms <= 0.002 * tone_rms, |
| 608 | 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 | 623 | let (ours, _) = our_decode(&path); |