mp3: gate VBR against the reference ladder, and split its frames too
Browse files at this commit806ee38e8b3065a8b880a2159d57b682f6e1088cParent: e90e5933 changed files +217 -8
- crates/ec-mp3/src/encode.rs +9 -8
- crates/ec-mp3/tests/encode_matrix.rs +150 -0
- lanes/mp3lame-r1.report.md +58 -0
crates/ec-mp3/src/encode.rsmodified+9 -8
| @@ -803,13 +803,7 @@ impl Mp3Encode { | |||
| 803 | let (granule, r) = quantise_granule( | 803 | let (granule, r) = quantise_granule( |
| 804 | &xrs[index], | 804 | &xrs[index], |
| 805 | types[index], | 805 | types[index], |
| 806 | - // The even split, not the demand split CBR | 806 | + demand_share(capacity * 8, index), |
| 807 | - // uses: VBR picks its bitrate by the noise-to-mask | ||
| 808 | - // ratio a candidate reaches, and that mapping is | ||
| 809 | - // calibrated to what an even split produces. Both | ||
| 810 | - // want to change together, against a VBR reference | ||
| 811 | - // gate that does not exist yet. | ||
| 812 | - ((capacity * 8) / count).min(PART2_3_MAX as usize) as u32, | ||
| 813 | &thresholds[index], | 807 | &thresholds[index], |
| 814 | self.sample_rate, | 808 | self.sample_rate, |
| 815 | ); | 809 | ); |
| @@ -1087,7 +1081,14 @@ fn quality_threshold(threshold: &[f32], quality: f32) -> Vec<f32> { | |||
| 1087 | let t = q - 0.5; | 1081 | let t = q - 0.5; |
| 1088 | 4.5 - 11.0 * t - 12.0 * t * t | 1082 | 4.5 - 11.0 * t - 12.0 * t * t |
| 1089 | }; | 1083 | }; |
| 1090 | - let scale = 10f32.powf(db / 10.0); | 1084 | + // Recalibration, measured against `real_library_vbr_vs_lame`: splitting a |
| 1085 | + // frame by bit demand reaches the same masking bar for fewer bits, and the | ||
| 1086 | + // quality knob promises a mean rate rather than a bar, so the bar tightens | ||
| 1087 | + // to spend the saving instead of pocketing it. The slope is fitted to two | ||
| 1088 | + // points on the library -- 2 dB at quality 0.3 holds its promised 163 | ||
| 1089 | + // kbit/s, and the rate moves about 9.5 kbit/s per decibel at quality 0.6. | ||
| 1090 | + let trim = 1.6 - 12.0 * q; | ||
| 1091 | + let scale = 10f32.powf((db + trim) / 10.0); | ||
| 1091 | threshold.iter().map(|&t| t * scale).collect() | 1092 | threshold.iter().map(|&t| t * scale).collect() |
| 1092 | } | 1093 | } |
| 1093 | 1094 | ||
crates/ec-mp3/tests/encode_matrix.rsmodified+150 -0
| @@ -701,3 +701,153 @@ fn mid_side_follows_the_channel_correlation() { | |||
| 701 | } | 701 | } |
| 702 | } | 702 | } |
| 703 | } | 703 | } |
| 704 | + | ||
| 705 | +/// libmp3lame's VBR levels, encoded once per source so the level that | ||
| 706 | +/// actually spent what we spent can be picked by measurement. Nominal rates | ||
| 707 | +/// are no guide: on speech, V6 lands near 60 kbit/s rather than its nominal | ||
| 708 | +/// 115, because libmp3lame drops the rate hard on easy material. | ||
| 709 | +const LAME_V_LEVELS: [u32; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
| 710 | + | ||
| 711 | +/// How far under libmp3lame a VBR row may sit in correlation when it spent at | ||
| 712 | +/// least as many bits. Thirteen of the fourteen rows are ahead; the floor is | ||
| 713 | +/// set by her at quality 0.3, which is 0.00042 behind while spending 1.5% | ||
| 714 | +/// less. | ||
| 715 | +const VBR_CORR_FLOOR: f64 = -0.0005; | ||
| 716 | + | ||
| 717 | +/// How far behind libmp3lame's worst 20 ms window a VBR row may sit. Set by | ||
| 718 | +/// nik and reelo at quality 0.3, both 2.8 dB behind: the frames our VBR calls | ||
| 719 | +/// easy are where it still under-spends against a transient. | ||
| 720 | +const VBR_WORST_EXCESS_DB: f64 = 3.5; | ||
| 721 | + | ||
| 722 | +/// How much of libmp3lame's rate ours may spend at a matched quality. Rows | ||
| 723 | +/// where libmp3lame is already on its top rung (V0) are exempt: it cannot | ||
| 724 | +/// spend more there, so a premium measures its ceiling and not our appetite. | ||
| 725 | +const VBR_RATE_CEILING: f64 = 8.0; | ||
| 726 | + | ||
| 727 | +/// Our VBR against libmp3lame's, on the user's own library, at a matched rate. | ||
| 728 | +/// | ||
| 729 | +/// CBR has had a reference gate since `real_library_sweep_vs_lame`; VBR has | ||
| 730 | +/// had none, which is why the demand-weighted frame split landed in the CBR | ||
| 731 | +/// arm only -- VBR's bitrate choice is calibrated to the noise-to-mask ratio | ||
| 732 | +/// the even split produces, and nothing could tell whether moving both was a | ||
| 733 | +/// gain or a loss. | ||
| 734 | +#[test] | ||
| 735 | +#[ignore = "needs the user's library and ffmpeg's libmp3lame"] | ||
| 736 | +fn real_library_vbr_vs_lame() { | ||
| 737 | + let seconds = 60u32; | ||
| 738 | + let work = workdir("lame-vbr"); | ||
| 739 | + println!( | ||
| 740 | + "{:<7} {:>4} {:>9} {:>9} {:>8} {:>8} {:>8} {:>8} {:>8} {:>4}", | ||
| 741 | + "source", | ||
| 742 | + "q", | ||
| 743 | + "corr_ours", | ||
| 744 | + "corr_lame", | ||
| 745 | + "gap", | ||
| 746 | + "wworst_o", | ||
| 747 | + "wworst_r", | ||
| 748 | + "ours_kb", | ||
| 749 | + "lame_kb", | ||
| 750 | + "V" | ||
| 751 | + ); | ||
| 752 | + let only = std::env::var("EC_MP3_SWEEP_ONLY").unwrap_or_default(); | ||
| 753 | + let mut rows = 0; | ||
| 754 | + let mut failures = Vec::new(); | ||
| 755 | + for (name, src) in LIBRARY { | ||
| 756 | + if !only.is_empty() && !only.split(',').any(|w| w.trim() == name) { | ||
| 757 | + continue; | ||
| 758 | + } | ||
| 759 | + let src = expand(src); | ||
| 760 | + if !src.exists() { | ||
| 761 | + println!("{name:<7} SKIP (missing)"); | ||
| 762 | + continue; | ||
| 763 | + } | ||
| 764 | + let Some(pcm) = decode_source(&src, seconds) else { | ||
| 765 | + println!("{name:<7} SKIP (decode)"); | ||
| 766 | + continue; | ||
| 767 | + }; | ||
| 768 | + let secs = pcm.len() as f64 / (44100.0 * 2.0); | ||
| 769 | + let kb = |p: &Path| std::fs::metadata(p).expect("size").len() as f64 * 8.0 / secs / 1000.0; | ||
| 770 | + // Every rung of libmp3lame's VBR ladder, measured on this source. | ||
| 771 | + let lame_ladder: Vec<(u32, PathBuf, f64)> = LAME_V_LEVELS | ||
| 772 | + .iter() | ||
| 773 | + .map(|&level| { | ||
| 774 | + let file = work.join(format!("lame-{name}-v{level}.mp3")); | ||
| 775 | + let status = Command::new("ffmpeg") | ||
| 776 | + .args(["-y", "-v", "error", "-i"]) | ||
| 777 | + .arg(&src) | ||
| 778 | + .args([ | ||
| 779 | + "-vn", | ||
| 780 | + "-t", | ||
| 781 | + &seconds.to_string(), | ||
| 782 | + "-ac", | ||
| 783 | + "2", | ||
| 784 | + "-ar", | ||
| 785 | + "44100", | ||
| 786 | + "-c:a", | ||
| 787 | + "libmp3lame", | ||
| 788 | + "-q:a", | ||
| 789 | + &level.to_string(), | ||
| 790 | + ]) | ||
| 791 | + .arg(&file) | ||
| 792 | + .status() | ||
| 793 | + .expect("ffmpeg runs"); | ||
| 794 | + assert!(status.success(), "libmp3lame V{level} of {name}"); | ||
| 795 | + let rate = kb(&file); | ||
| 796 | + (level, file, rate) | ||
| 797 | + }) | ||
| 798 | + .collect(); | ||
| 799 | + for quality in [0.3f32, 0.6] { | ||
| 800 | + let ours_bytes = encode_vbr(&pcm, 44100, 2, quality); | ||
| 801 | + let ours_file = work.join(format!("ours-{name}-{quality}.mp3")); | ||
| 802 | + std::fs::write(&ours_file, &ours_bytes).expect("write ours"); | ||
| 803 | + let ours_kb = kb(&ours_file); | ||
| 804 | + // The level that actually spent what we spent. | ||
| 805 | + let (level, lame_file) = lame_ladder | ||
| 806 | + .iter() | ||
| 807 | + .min_by(|a, b| { | ||
| 808 | + (a.2 - ours_kb) | ||
| 809 | + .abs() | ||
| 810 | + .partial_cmp(&(b.2 - ours_kb).abs()) | ||
| 811 | + .expect("finite") | ||
| 812 | + }) | ||
| 813 | + .map(|(v, f, _)| (*v, f.clone())) | ||
| 814 | + .expect("a ladder rung"); | ||
| 815 | + let measure = |file: &Path| -> (f64, f64) { | ||
| 816 | + let decoded = decode(file); | ||
| 817 | + let (corr, offset) = aligned(&decoded, &pcm, 2); | ||
| 818 | + (corr, worst_window_db(&decoded, &pcm, offset, 2)) | ||
| 819 | + }; | ||
| 820 | + let ((ours_corr, ours_worst), (lame_corr, lame_worst)) = | ||
| 821 | + (measure(&ours_file), measure(&lame_file)); | ||
| 822 | + let lame_kb = kb(&lame_file); | ||
| 823 | + let gap = ours_corr - lame_corr; | ||
| 824 | + let premium = (ours_kb / lame_kb - 1.0) * 100.0; | ||
| 825 | + println!( | ||
| 826 | + "{name:<7} {quality:>4} {ours_corr:>9.5} {lame_corr:>9.5} {gap:>+8.5} \ | ||
| 827 | + {ours_worst:>+8.1} {lame_worst:>+8.1} {ours_kb:>8.1} {lame_kb:>8.1} {level:>4}" | ||
| 828 | + ); | ||
| 829 | + rows += 1; | ||
| 830 | + // Being behind on quality is only a failure if we did not also | ||
| 831 | + // spend less to get there; spending more is a failure on its own. | ||
| 832 | + if gap < VBR_CORR_FLOOR && ours_kb >= lame_kb * 0.98 { | ||
| 833 | + failures.push(format!( | ||
| 834 | + "{name} at q{quality}: {ours_corr:.5} against LAME's {lame_corr:.5} \ | ||
| 835 | + at {ours_kb:.1} against {lame_kb:.1} kbit/s" | ||
| 836 | + )); | ||
| 837 | + } | ||
| 838 | + if ours_worst - lame_worst > VBR_WORST_EXCESS_DB { | ||
| 839 | + failures.push(format!( | ||
| 840 | + "{name} at q{quality}: worst window {ours_worst:.1} dB \ | ||
| 841 | + against LAME's {lame_worst:.1} dB" | ||
| 842 | + )); | ||
| 843 | + } | ||
| 844 | + if level > 0 && premium > VBR_RATE_CEILING { | ||
| 845 | + failures.push(format!( | ||
| 846 | + "{name} at q{quality}: {premium:+.1}% of LAME's rate (V{level})" | ||
| 847 | + )); | ||
| 848 | + } | ||
| 849 | + } | ||
| 850 | + } | ||
| 851 | + assert!(rows > 0, "no library sources were readable"); | ||
| 852 | + assert!(failures.is_empty(), "{failures:#?}"); | ||
| 853 | +} | ||
lanes/mp3lame-r1.report.mdmodified+58 -0
| @@ -331,3 +331,61 @@ kbit/s instead of 208 for the same noise-to-mask bar. That is plausibly the | |||
| 331 | encoder getting more quality per bit, but nothing here measures VBR against a | 331 | encoder getting more quality per bit, but nothing here measures VBR against a |
| 332 | reference, so it is **deferred — unblocked by a VBR reference gate against | 332 | reference, so it is **deferred — unblocked by a VBR reference gate against |
| 333 | `lame -V`**, which is the next round of this lane. | 333 | `lame -V`**, which is the next round of this lane. |
| 334 | + | ||
| 335 | +## Round 6: VBR gets a reference gate, and loses to it | ||
| 336 | + | ||
| 337 | +Round 5 left the demand split out of the VBR arm because nothing could judge | ||
| 338 | +it. This round builds the judge: `real_library_vbr_vs_lame` encodes each source | ||
| 339 | +at quality 0.3 and 0.6, encodes **every rung of libmp3lame's VBR ladder** | ||
| 340 | +(V0..V9) on the same audio, and compares against the rung that actually spent | ||
| 341 | +what we spent. | ||
| 342 | + | ||
| 343 | +Picking the rung by its nominal rate does not work, and the first attempt did: | ||
| 344 | +libmp3lame's V6 is nominally 115 kbit/s but lands near 60 on speech, because it | ||
| 345 | +drops the rate hard on easy material. Matching on the nominal rate compared us | ||
| 346 | +against a rung spending half what we did and made a losing table look winning. | ||
| 347 | + | ||
| 348 | +Matched on the measured rate, the shipped VBR lost almost everywhere -- 11 of | ||
| 349 | +14 rows behind on correlation, and 5 to 8 dB behind on the worst 20 ms window | ||
| 350 | +while spending the same bits. Two changes fixed it: | ||
| 351 | + | ||
| 352 | +1. **The demand split, in the VBR arm too.** Same change round 5 made to CBR; | ||
| 353 | + it took the table from 3 rows ahead to 10. | ||
| 354 | +2. **The quality bar recalibrated.** The knob's documented contract is a mean | ||
| 355 | + *rate*, not a masking bar, so reaching the same bar for fewer bits has to be | ||
| 356 | + spent rather than pocketed. `quality_threshold` now tightens by | ||
| 357 | + `1.6 - 12 q` dB, fitted to two points on the library: quality 0.3 lands at | ||
| 358 | + 162 kbit/s against its promised 163, and quality 0.6 at 232 against 230. | ||
| 359 | + | ||
| 360 | +| source | q | corr gap | worst window ours / LAME | ours / LAME kbit/s | rung | | ||
| 361 | +|---|---|---|---|---|---| | ||
| 362 | +| nik | 0.3 | +0.00046 | -11.2 / -14.0 | 170.6 / 169.6 | V2 | | ||
| 363 | +| nik | 0.6 | +0.00019 | -18.5 / -19.7 | 238.1 / 224.7 | V0 | | ||
| 364 | +| reelo | 0.3 | +0.00006 | -12.2 / -15.0 | 164.6 / 162.6 | V2 | | ||
| 365 | +| reelo | 0.6 | +0.00012 | -21.3 / -20.2 | 232.7 / 210.0 | V0 | | ||
| 366 | +| her | 0.3 | **-0.00042** | -12.5 / -14.7 | 221.6 / 224.9 | V0 | | ||
| 367 | +| her | 0.6 | +0.00032 | -19.6 / -14.7 | 303.2 / 224.9 | V0 | | ||
| 368 | +| reell | 0.3 | +0.00017 | -11.3 / -12.6 | 197.5 / 194.2 | V1 | | ||
| 369 | +| reell | 0.6 | +0.00022 | -18.5 / -14.4 | 276.2 / 219.6 | V0 | | ||
| 370 | +| reelm | 0.3 | +0.00021 | -10.6 / -8.8 | 138.3 / 133.9 | V1 | | ||
| 371 | +| reelm | 0.6 | +0.00016 | -16.0 / -10.8 | 207.9 / 154.0 | V0 | | ||
| 372 | +| dl8a | 0.3 | +0.00062 | -8.3 / -9.3 | 150.9 / 158.0 | V3 | | ||
| 373 | +| dl8a | 0.6 | +0.00012 | -17.0 / -13.7 | 226.8 / 227.1 | V0 | | ||
| 374 | +| hein | 0.3 | +0.00019 | -7.5 / -9.3 | 129.8 / 135.3 | V1 | | ||
| 375 | +| hein | 0.6 | +0.00019 | -16.5 / -12.0 | 189.2 / 153.7 | V0 | | ||
| 376 | + | ||
| 377 | +Thirteen of fourteen rows are ahead. The floors: correlation -0.0005 (her at | ||
| 378 | +0.3, which is behind while spending 1.5% less), worst-window excess 3.5 dB (nik | ||
| 379 | +and reelo at 0.3, both 2.8 dB behind — the frames our VBR calls easy are where | ||
| 380 | +it still under-spends against a transient), and a rate ceiling of 8% of the | ||
| 381 | +matched rung, with V0 rows exempt because libmp3lame cannot spend more there | ||
| 382 | +and the premium would measure its ceiling rather than our appetite. | ||
| 383 | + | ||
| 384 | +The V0 rows are the standing weakness this gate cannot yet price: at quality | ||
| 385 | +0.6 on four sources we spend 20-35% more than libmp3lame's top rung and buy | ||
| 386 | +quality with it, and nothing here says whether that trade is the one a listener | ||
| 387 | +would pick. Sitting off the top of the reference ladder is the next round's | ||
| 388 | +problem. | ||
| 389 | + | ||
| 390 | +Closes the round 5 deferral: VBR now has a reference gate, and the split it was | ||
| 391 | +waiting on is in. | ||