forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-mp3loop' (36c6cf04): ec-mp3 granule exit-reason census behind EC_MP3_LOOP_TRACE (no behaviour change); measured: the encoder already beats libmp3lame 14/14 on both gate metrics and a LAME-shaped outer loop scores worse on this MSE gate (finding recorded at the call site)

Browse files at this commit

7302779b40919214ffd4fb69c160ce445094e4b3Ahmet Kaan GümüşParent: d5529553458620
crates/ec-mp3/src/encode.rsmodified+30 -2
@@ -1131,22 +1131,24 @@ fn quantise_granule(
1131 // Short blocks carry twelve bands, long ones twenty-one.1131 // Short blocks carry twelve bands, long ones twenty-one.
1132 let bands = if block_type == 2 { 12 } else { SFB_LONG };1132 let bands = if block_type == 2 { 12 } else { SFB_LONG };
1133 let threshold = band_thresholds(threshold, block_type, sample_rate);1133 let threshold = band_thresholds(threshold, block_type, sample_rate);
1134- for _round in 0..20 {1134+ let mut exit = ("cap", 19usize);
1135+ for round in 0..20 {
1135 let (granule, noise) = code_with(xr, block_type, target_bits, &scalefac, sample_rate);1136 let (granule, noise) = code_with(xr, block_type, target_bits, &scalefac, sample_rate);
1136 let ratios: Vec<f64> = (0..bands)1137 let ratios: Vec<f64> = (0..bands)
1137 .map(|b| f64::from(noise[b]) / f64::from(threshold[b]).max(1e-30))1138 .map(|b| f64::from(noise[b]) / f64::from(threshold[b]).max(1e-30))
1138 .collect();1139 .collect();
1139 let score: f64 = ratios.iter().sum();1140 let score: f64 = ratios.iter().sum();
1140- let spent = granule.part2_3_length;
1141 let mut order: Vec<usize> = (0..bands)1141 let mut order: Vec<usize> = (0..bands)
1142 .filter(|&b| scalefac[b] < max_scalefac(b, block_type))1142 .filter(|&b| scalefac[b] < max_scalefac(b, block_type))
1143 .collect();1143 .collect();
1144 order.sort_by(|a, b| ratios[*b].total_cmp(&ratios[*a]));1144 order.sort_by(|a, b| ratios[*b].total_cmp(&ratios[*a]));
1145 let over: Vec<usize> = order.iter().copied().filter(|&b| ratios[b] > 1.0).collect();1145 let over: Vec<usize> = order.iter().copied().filter(|&b| ratios[b] > 1.0).collect();
1146+ let spent = granule.part2_3_length;
1146 if best.as_ref().is_none_or(|(_, s, _)| score < *s) {1147 if best.as_ref().is_none_or(|(_, s, _)| score < *s) {
1147 best = Some((granule, score, bands));1148 best = Some((granule, score, bands));
1148 }1149 }
1149 if score <= 0.0 {1150 if score <= 0.0 {
1151+ exit = ("mask", round);
1150 break; // every band is already exactly at the mask; nothing left to buy1152 break; // every band is already exactly at the mask; nothing left to buy
1151 }1153 }
1152 // Where to stop amplifying, as a fraction of the frame's budget. Swept1154 // Where to stop amplifying, as a fraction of the frame's budget. Swept
@@ -1156,7 +1158,20 @@ fn quantise_granule(
1156 // budget is gone only makes the rate loop coarsen every other band to1158 // budget is gone only makes the rate loop coarsen every other band to
1157 // pay for it. Below 70% the test stops binding, since the first round1159 // pay for it. Below 70% the test stops binding, since the first round
1158 // already spends more than that.1160 // already spends more than that.
1161+ //
1162+ // LAME's own shape -- drop the budget break, let the distortion test
1163+ // drive, since `code_with` fits `target_bits` at every round anyway --
1164+ // was built and measured here (2026-09-06). It makes the loop live
1165+ // (10440 granules of 36758 reach the 20-round cap against 3 before) and
1166+ // loses on both gate metrics: reelo at 128 kbit/s goes 0.99917 ->
1167+ // 0.99787 correlation, from +0.00062 ahead of libmp3lame to -0.00068
1168+ // behind it, and its worst 20 ms window from -15.3 to -12.4 dB, at
1169+ // roughly five times the encode time. The gate measures waveform error,
1170+ // and a budget-filling rate loop over flat scalefactors is already near
1171+ // the MSE optimum, so perceptual redistribution can only trade away
1172+ // what this gate scores. Don't re-run it: change the metric first.
1159 if spent * 100 > target_bits * 85 {1173 if spent * 100 > target_bits * 85 {
1174+ exit = ("budget", round);
1160 break; // the budget is spent; amplifying now only coarsens1175 break; // the budget is spent; amplifying now only coarsens
1161 }1176 }
1162 // Bands over their mask get the bits first, worst three per round so1177 // Bands over their mask get the bits first, worst three per round so
@@ -1173,16 +1188,29 @@ fn quantise_granule(
1173 .take(3)1188 .take(3)
1174 .collect();1189 .collect();
1175 if chosen.is_empty() {1190 if chosen.is_empty() {
1191+ exit = ("saturated", round);
1176 break;1192 break;
1177 }1193 }
1178 for band in chosen {1194 for band in chosen {
1179 scalefac[band] += 1;1195 scalefac[band] += 1;
1180 }1196 }
1181 }1197 }
1198+ census(exit);
1182 let (granule, score, bands) = best.expect("at least one round runs");1199 let (granule, score, bands) = best.expect("at least one round runs");
1183 (granule, score / bands as f64)1200 (granule, score / bands as f64)
1184 }1201 }
1185 1202
1203+/// Exit-reason census for the distortion loop: with `EC_MP3_LOOP_TRACE` set,
1204+/// one `mp3loop <reason> <round>` line per granule on stderr, so a sweep can be
1205+/// counted with `sort | uniq -c`. The loop's shape is the encoder's open
1206+/// question; without this the only evidence it ever iterates is indirect.
1207+fn census(exit: (&str, usize)) {
1208+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
1209+ if *ON.get_or_init(|| std::env::var_os("EC_MP3_LOOP_TRACE").is_some()) {
1210+ eprintln!("mp3loop {} {}", exit.0, exit.1);
1211+ }
1212+}
1213+
1186 /// Bitstream order for a short block's spectrum.1214 /// Bitstream order for a short block's spectrum.
1187 ///1215 ///
1188 /// A short granule is coded band by band, window by window, line by line,1216 /// A short granule is coded band by band, window by window, line by line,