Code tf_res=is_transient so transient frames keep short blocks
Browse files at this commit5b61429fcfac70457afe2e7b3272e8ba2dc78d24Parent: fc48a1c- fix
- code tf_res=is_transient so transient frames keep short blocks
6 changed files +317 -12
- crates/ec-opus/src/celt.rs +46 -0
- crates/ec-opus/src/celt_enc.rs +10 -6
- crates/ec-opus/src/lib.rs +5 -0
- crates/ec-opus/tests/conformance.rs +192 -6
- lanes/opus-reell-r2.report.md +47 -0
- lanes/opus-reell-r2.sweep.txt +17 -0
crates/ec-opus/src/celt.rsmodified+46 -0
| @@ -1103,6 +1103,26 @@ fn compute_allocation_shared( | |||
| 1103 | } | 1103 | } |
| 1104 | } | 1104 | } |
| 1105 | 1105 | ||
| 1106 | +/// Facts about the last decoded frame, for diagnostics. | ||
| 1107 | +#[derive(Clone, Debug)] | ||
| 1108 | +pub struct CeltDecDiag { | ||
| 1109 | + /// The frame was coded as CELT digital silence. | ||
| 1110 | + pub silence: bool, | ||
| 1111 | + /// Coarse energies used the intra-frame reset. | ||
| 1112 | + pub intra: bool, | ||
| 1113 | + pub transient: bool, | ||
| 1114 | + /// Packet bits the range decoder saw for this frame. | ||
| 1115 | + pub total_bits: i32, | ||
| 1116 | + /// Decoded log2 band energies after fine bits, per channel (`2*NB_BANDS`). | ||
| 1117 | + pub old_band_e: Vec<f32>, | ||
| 1118 | + /// Time-frequency resolution change per band after `tf_select`. | ||
| 1119 | + pub tf_res: Vec<i32>, | ||
| 1120 | + /// Spreading decision as coded (0..3). | ||
| 1121 | + pub spread: usize, | ||
| 1122 | + /// Anti-collapse flag as coded. | ||
| 1123 | + pub anti_collapse: bool, | ||
| 1124 | +} | ||
| 1125 | + | ||
| 1106 | /// A CELT layer decoder, one per Opus stream. | 1126 | /// A CELT layer decoder, one per Opus stream. |
| 1107 | #[derive(Clone, Debug)] | 1127 | #[derive(Clone, Debug)] |
| 1108 | pub struct CeltDecoder { | 1128 | pub struct CeltDecoder { |
| @@ -1146,6 +1166,7 @@ pub struct CeltDecoder { | |||
| 1146 | iy: Vec<i32>, | 1166 | iy: Vec<i32>, |
| 1147 | urow: Vec<u32>, | 1167 | urow: Vec<u32>, |
| 1148 | mdct_buf: Vec<f32>, | 1168 | mdct_buf: Vec<f32>, |
| 1169 | + diag: CeltDecDiag, | ||
| 1149 | } | 1170 | } |
| 1150 | 1171 | ||
| 1151 | impl CeltDecoder { | 1172 | impl CeltDecoder { |
| @@ -1188,6 +1209,16 @@ impl CeltDecoder { | |||
| 1188 | iy: vec![0; max_n], | 1209 | iy: vec![0; max_n], |
| 1189 | urow: vec![0; 256], | 1210 | urow: vec![0; 256], |
| 1190 | mdct_buf: vec![0.0; max_n + OVERLAP], | 1211 | mdct_buf: vec![0.0; max_n + OVERLAP], |
| 1212 | + diag: CeltDecDiag { | ||
| 1213 | + silence: false, | ||
| 1214 | + intra: false, | ||
| 1215 | + transient: false, | ||
| 1216 | + total_bits: 0, | ||
| 1217 | + old_band_e: vec![0.0; 2 * NB_BANDS], | ||
| 1218 | + tf_res: Vec::new(), | ||
| 1219 | + spread: 0, | ||
| 1220 | + anti_collapse: false, | ||
| 1221 | + }, | ||
| 1191 | } | 1222 | } |
| 1192 | } | 1223 | } |
| 1193 | 1224 | ||
| @@ -1214,6 +1245,11 @@ impl CeltDecoder { | |||
| 1214 | self.rng | 1245 | self.rng |
| 1215 | } | 1246 | } |
| 1216 | 1247 | ||
| 1248 | + /// Facts from the last decoded frame (diagnostic). | ||
| 1249 | + pub fn last_diag(&self) -> &CeltDecDiag { | ||
| 1250 | + &self.diag | ||
| 1251 | + } | ||
| 1252 | + | ||
| 1217 | /// Decodes one CELT frame. | 1253 | /// Decodes one CELT frame. |
| 1218 | /// | 1254 | /// |
| 1219 | /// `frame_size` is in 48 kHz samples; `start`/`end` bound the coded bands | 1255 | /// `frame_size` is in 48 kHz samples; `start`/`end` bound the coded bands |
| @@ -1521,6 +1557,16 @@ impl CeltDecoder { | |||
| 1521 | self.old_log_e2[ch * NB_BANDS + i] = -28.0; | 1557 | self.old_log_e2[ch * NB_BANDS + i] = -28.0; |
| 1522 | } | 1558 | } |
| 1523 | } | 1559 | } |
| 1560 | + self.diag = CeltDecDiag { | ||
| 1561 | + silence, | ||
| 1562 | + intra, | ||
| 1563 | + transient, | ||
| 1564 | + total_bits, | ||
| 1565 | + old_band_e: self.old_band_e.clone(), | ||
| 1566 | + tf_res: self.tf_res.to_vec(), | ||
| 1567 | + spread, | ||
| 1568 | + anti_collapse: anti_collapse_on, | ||
| 1569 | + }; | ||
| 1524 | self.rng = dec.range(); | 1570 | self.rng = dec.range(); |
| 1525 | 1571 | ||
| 1526 | self.deemphasis(out, n, cc); | 1572 | self.deemphasis(out, n, cc); |
crates/ec-opus/src/celt_enc.rsmodified+10 -6
| @@ -11,10 +11,12 @@ | |||
| 11 | //! which removes the most expensive analysis in the reference encoder (an | 11 | //! which removes the most expensive analysis in the reference encoder (an |
| 12 | //! autocorrelation search over 1024 lags). Same choice the reference makes | 12 | //! autocorrelation search over 1024 lags). Same choice the reference makes |
| 13 | //! below complexity 5. | 13 | //! below complexity 5. |
| 14 | -//! - **No time/frequency Viterbi.** `tf_res` is all zeros: long-block frames | 14 | +//! - **No time/frequency Viterbi.** Raw `tf_res` is `is_transient` in every |
| 15 | -//! keep full frequency resolution, transient frames get the tf-table's | 15 | +//! band (the reference's no-analysis default): with `tf_select = 0` the |
| 16 | -//! maximum time resolution — the two defaults the search almost always | 16 | +//! tf table maps it to "no change", so long-block frames keep full |
| 17 | -//! lands on. | 17 | +//! frequency resolution and transient frames keep their short blocks. (Raw |
| 18 | +//! zeros in a transient frame would map to `+LM`, merging the short blocks | ||
| 19 | +//! back into one long block and smearing pre-echo over the whole frame.) | ||
| 18 | //! - **Fixed spreading.** `SPREAD_NORMAL` every frame, coded explicitly. | 20 | //! - **Fixed spreading.** `SPREAD_NORMAL` every frame, coded explicitly. |
| 19 | //! - **Single-pass coarse energy** with the reference's own delayed-intra | 21 | //! - **Single-pass coarse energy** with the reference's own delayed-intra |
| 20 | //! heuristic, not the two-pass encode-both-and-compare. | 22 | //! heuristic, not the two-pass encode-both-and-compare. |
| @@ -608,8 +610,10 @@ impl CeltEncoder { | |||
| 608 | } | 610 | } |
| 609 | } | 611 | } |
| 610 | 612 | ||
| 611 | - // --- tf_res: all zeros (throughput-first) --------------------------- | 613 | + // --- tf_res: the reference's no-analysis default -------------------- |
| 612 | - self.tf_res = [0; NB_BANDS]; | 614 | + // Raw `is_transient` per band + `tf_select = 0` codes "no tf change"; |
| 615 | + // raw zeros on a transient frame would code `+LM` (undo the shorts). | ||
| 616 | + self.tf_res = [i32::from(is_transient); NB_BANDS]; | ||
| 613 | let tf_select = 0usize; | 617 | let tf_select = 0usize; |
| 614 | 618 | ||
| 615 | // --- Coarse energy -------------------------------------------------- | 619 | // --- Coarse energy -------------------------------------------------- |
crates/ec-opus/src/lib.rsmodified+5 -0
| @@ -120,6 +120,11 @@ impl Decoder { | |||
| 120 | self.debug_mute = (celt, silk); | 120 | self.debug_mute = (celt, silk); |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | + /// Facts from the last decoded frame's CELT layer (diagnostic). | ||
| 124 | + pub fn last_celt_diag(&self) -> &crate::celt::CeltDecDiag { | ||
| 125 | + self.celt.last_diag() | ||
| 126 | + } | ||
| 127 | + | ||
| 123 | /// A decoder for `channels` channels at `sample_rate`, which must be one of | 128 | /// A decoder for `channels` channels at `sample_rate`, which must be one of |
| 124 | /// 8000, 12000, 16000, 24000 or 48000 Hz. | 129 | /// 8000, 12000, 16000, 24000 or 48000 Hz. |
| 125 | pub fn new(sample_rate: u32, channels: usize) -> Result<Decoder> { | 130 | pub fn new(sample_rate: u32, channels: usize) -> Result<Decoder> { |
crates/ec-opus/tests/conformance.rsmodified+192 -6
| @@ -2319,8 +2319,11 @@ fn silk_compares_to_celt_on_speech_at_speech_rates() { | |||
| 2319 | "speech {tag}: SILK corr {silk_corr:.4} lag {silk_lag} {silk_kbps:.2} kbps; CELT corr {celt_corr:.4} lag {celt_lag} {celt_kbps:.2} kbps" | 2319 | "speech {tag}: SILK corr {silk_corr:.4} lag {silk_lag} {silk_kbps:.2} kbps; CELT corr {celt_corr:.4} lag {celt_lag} {celt_kbps:.2} kbps" |
| 2320 | ); | 2320 | ); |
| 2321 | assert!(silk_corr >= 0.95, "{tag}: SILK corr {silk_corr:.4}"); | 2321 | assert!(silk_corr >= 0.95, "{tag}: SILK corr {silk_corr:.4}"); |
| 2322 | + // SILK should not lose clearly to CELT at speech rates. CELT with | ||
| 2323 | + // short-block transients now edges SILK by ~0.002 at NB16 (synthetic | ||
| 2324 | + // speech); a larger gap means the SILK encoder regressed. | ||
| 2322 | assert!( | 2325 | assert!( |
| 2323 | - silk_corr >= celt_corr, | 2326 | + silk_corr >= celt_corr - 0.005, |
| 2324 | "{tag}: SILK {silk_corr:.4} trails CELT {celt_corr:.4}" | 2327 | "{tag}: SILK {silk_corr:.4} trails CELT {celt_corr:.4}" |
| 2325 | ); | 2328 | ); |
| 2326 | } | 2329 | } |
| @@ -2499,8 +2502,11 @@ fn hybrid_layers_align() { | |||
| 2499 | e.look_ahead(960) | 2502 | e.look_ahead(960) |
| 2500 | } | 2503 | } |
| 2501 | ); | 2504 | ); |
| 2505 | + // A fullband CELT click lands at exactly +120 (`celt_click_peak_offset`); | ||
| 2506 | + // the band-limited HB layer's main lobe sits up to 2 samples early once | ||
| 2507 | + // transient frames keep their short blocks, and SILK lands at +121. | ||
| 2502 | assert!( | 2508 | assert!( |
| 2503 | - (plb - phb).abs() <= 2, | 2509 | + (plb - phb).abs() <= 3, |
| 2504 | "layers misaligned: LB +{plb} vs HB +{phb}" | 2510 | "layers misaligned: LB +{plb} vs HB +{phb}" |
| 2505 | ); | 2511 | ); |
| 2506 | assert!((phb - 120).abs() <= 2, "HB peak +{phb}, look_ahead 120"); | 2512 | assert!((phb - 120).abs() <= 2, "HB peak +{phb}, look_ahead 120"); |
| @@ -3243,8 +3249,10 @@ fn spectral_divergence_vs_libopus() { | |||
| 3243 | 3249 | ||
| 3244 | let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes"); | 3250 | let lanes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lanes"); |
| 3245 | fs::create_dir_all(&lanes_dir).unwrap(); | 3251 | fs::create_dir_all(&lanes_dir).unwrap(); |
| 3246 | - let out_path = lanes_dir.join("opus-reell-r1.bands.txt"); | 3252 | + let out_path = lanes_dir.join("opus-reell-r2.bands.txt"); |
| 3253 | + let frames_path = lanes_dir.join("opus-reell-r2.frames.txt"); | ||
| 3247 | let scratch = lanes_dir.join("opus-reell-r1.scratch.ogg"); | 3254 | let scratch = lanes_dir.join("opus-reell-r1.scratch.ogg"); |
| 3255 | + let mut frames_all = String::new(); | ||
| 3248 | let mut report = String::new(); | 3256 | let mut report = String::new(); |
| 3249 | 3257 | ||
| 3250 | for (tag, src) in &sources { | 3258 | for (tag, src) in &sources { |
| @@ -3261,7 +3269,11 @@ fn spectral_divergence_vs_libopus() { | |||
| 3261 | ffmpeg_encode_libopus(src, KBPS, &scratch, SECS); | 3269 | ffmpeg_encode_libopus(src, KBPS, &scratch, SECS); |
| 3262 | let ref_bytes = fs::metadata(&scratch).map(|m| m.len() as usize).unwrap_or(0); | 3270 | let ref_bytes = fs::metadata(&scratch).map(|m| m.len() as usize).unwrap_or(0); |
| 3263 | let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0; | 3271 | let ref_kbps = ref_bytes as f64 * 8.0 / seconds / 1000.0; |
| 3264 | - let (ref_dec, ref_ch) = decode_ogg(&scratch); | 3272 | + let ref_pkts: Vec<Vec<u8>> = ogg_packets(&fs::read(&scratch).unwrap()) |
| 3273 | + .into_iter() | ||
| 3274 | + .skip(2) | ||
| 3275 | + .collect(); | ||
| 3276 | + let (ref_dec, ref_ch) = decode_ogg(&scratch); | ||
| 3265 | assert_eq!(ref_ch, CHANNELS, "{tag}: ref not stereo"); | 3277 | assert_eq!(ref_ch, CHANNELS, "{tag}: ref not stereo"); |
| 3266 | let (_, ref_aligned) = align_to_source(&source_pcm, &ref_dec, CHANNELS, MAX_LAG); | 3278 | let (_, ref_aligned) = align_to_source(&source_pcm, &ref_dec, CHANNELS, MAX_LAG); |
| 3267 | 3279 | ||
| @@ -3273,13 +3285,17 @@ fn spectral_divergence_vs_libopus() { | |||
| 3273 | let mut packet = vec![0u8; 1500]; | 3285 | let mut packet = vec![0u8; 1500]; |
| 3274 | let mut dbuf = vec![0.0f32; 5760 * CHANNELS]; | 3286 | let mut dbuf = vec![0.0f32; 5760 * CHANNELS]; |
| 3275 | let mut ours_dec = Vec::new(); | 3287 | let mut ours_dec = Vec::new(); |
| 3276 | - let mut diags = Vec::new(); | 3288 | + let mut first_pkts: Vec<Vec<u8>> = Vec::new(); |
| 3289 | + let mut diags = Vec::new(); | ||
| 3277 | let mut ours_bytes = 0usize; | 3290 | let mut ours_bytes = 0usize; |
| 3278 | for block in source_pcm.chunks(FRAME * CHANNELS) { | 3291 | for block in source_pcm.chunks(FRAME * CHANNELS) { |
| 3279 | let mut padded = block.to_vec(); | 3292 | let mut padded = block.to_vec(); |
| 3280 | padded.resize(FRAME * CHANNELS, 0.0); | 3293 | padded.resize(FRAME * CHANNELS, 0.0); |
| 3281 | let len = enc.encode_float(&padded, FRAME, &mut packet).expect("encode"); | 3294 | let len = enc.encode_float(&padded, FRAME, &mut packet).expect("encode"); |
| 3282 | - ours_bytes += len; | 3295 | + if first_pkts.len() < 10 { |
| 3296 | + first_pkts.push(packet[..len].to_vec()); | ||
| 3297 | + } | ||
| 3298 | + ours_bytes += len; | ||
| 3283 | let n = dec.decode_float(&packet[..len], &mut dbuf).expect("decode"); | 3299 | let n = dec.decode_float(&packet[..len], &mut dbuf).expect("decode"); |
| 3284 | ours_dec.extend_from_slice(&dbuf[..n * CHANNELS]); | 3300 | ours_dec.extend_from_slice(&dbuf[..n * CHANNELS]); |
| 3285 | diags.push(enc.last_celt_diag().clone()); | 3301 | diags.push(enc.last_celt_diag().clone()); |
| @@ -3493,6 +3509,63 @@ fn spectral_divergence_vs_libopus() { | |||
| 3493 | d.nb_compressed | 3509 | d.nb_compressed |
| 3494 | )); | 3510 | )); |
| 3495 | } | 3511 | } |
| 3512 | + | ||
| 3513 | + // Per-window per-band ln-energies for the metric windows 12..=20 | ||
| 3514 | + // (+120..+200 ms): the leak's shape, band by band. | ||
| 3515 | + report.push_str( | ||
| 3516 | + "# windows 12..=20: win +ms band lo-hi | lnE src/ours/ref, ch L then R\n", | ||
| 3517 | + ); | ||
| 3518 | + for xi in 12..=20 { | ||
| 3519 | + if xi >= nframes { | ||
| 3520 | + break; | ||
| 3521 | + } | ||
| 3522 | + for bi in 0..NBANDS { | ||
| 3523 | + let e = |b: &Vec<f64>, ci: usize| b[(xi * NBANDS + bi) * CHANNELS + ci].ln(); | ||
| 3524 | + let (sl, sr) = (e(&bands_s, 0), e(&bands_s, 1)); | ||
| 3525 | + let (ol, or_) = (e(&bands_o, 0), e(&bands_o, 1)); | ||
| 3526 | + let (rl, rr) = (e(&bands_r, 0), e(&bands_r, 1)); | ||
| 3527 | + report.push_str(&format!( | ||
| 3528 | + "w{xi} +{}ms b{bi} {}-{}Hz\t{:+8.2} {:+8.2} {:+8.2} | {:+8.2} {:+8.2} {:+8.2}\n", | ||
| 3529 | + xi * WIN_STEP * 1000 / 48000, | ||
| 3530 | + CELT_EBANDS[bi] * 240, | ||
| 3531 | + CELT_EBANDS[bi + 1] * 240, | ||
| 3532 | + sl, | ||
| 3533 | + ol, | ||
| 3534 | + rl, | ||
| 3535 | + sr, | ||
| 3536 | + or_, | ||
| 3537 | + rr, | ||
| 3538 | + )); | ||
| 3539 | + } | ||
| 3540 | + } | ||
| 3541 | + | ||
| 3542 | + // Decode the first 10 packets of each stream with our decoder: does | ||
| 3543 | + // the silence flag survive, and what energies does it leave behind? | ||
| 3544 | + frames_all.push_str(&format!( | ||
| 3545 | + "# {tag}: first 10 packets through our decoder; energies are log2 old_band_e\n" | ||
| 3546 | + )); | ||
| 3547 | + for (label, pkts) in [("ours", &first_pkts), ("ref", &ref_pkts)] { | ||
| 3548 | + let mut d = Decoder::new(48000, CHANNELS).unwrap(); | ||
| 3549 | + let mut ob = vec![0.0f32; 5760 * CHANNELS]; | ||
| 3550 | + for (i, p) in pkts.iter().take(10).enumerate() { | ||
| 3551 | + let _ = d.decode_float(p, &mut ob).expect("decode"); | ||
| 3552 | + let g = d.last_celt_diag(); | ||
| 3553 | + frames_all.push_str(&format!( | ||
| 3554 | + "{label} {i} bytes={} toc={:#04x} sil{} intra{} trans{} bits{}\n", | ||
| 3555 | + p.len(), | ||
| 3556 | + p.first().copied().unwrap_or(0), | ||
| 3557 | + g.silence as u8, | ||
| 3558 | + g.intra as u8, | ||
| 3559 | + g.transient as u8, | ||
| 3560 | + g.total_bits, | ||
| 3561 | + )); | ||
| 3562 | + for (ch, name) in [(0usize, 'L'), (1usize, 'R')] { | ||
| 3563 | + let band = &g.old_band_e[ch * ec_opus::celt::NB_BANDS..(ch + 1) * ec_opus::celt::NB_BANDS]; | ||
| 3564 | + let e: Vec<String> = band.iter().map(|v| format!("{v:+6.1}")).collect(); | ||
| 3565 | + frames_all.push_str(&format!(" {name} {}\n", e.join(" "))); | ||
| 3566 | + } | ||
| 3567 | + } | ||
| 3568 | + } | ||
| 3496 | println!( | 3569 | println!( |
| 3497 | "{tag}@{KBPS}k: err o={err_o:.3} r={err_r:.3} ratio={:.2}", | 3570 | "{tag}@{KBPS}k: err o={err_o:.3} r={err_r:.3} ratio={:.2}", |
| 3498 | err_o / err_r | 3571 | err_o / err_r |
| @@ -3504,5 +3577,118 @@ fn spectral_divergence_vs_libopus() { | |||
| 3504 | only.join(",") | 3577 | only.join(",") |
| 3505 | ); | 3578 | ); |
| 3506 | fs::write(&out_path, header + &report).unwrap(); | 3579 | fs::write(&out_path, header + &report).unwrap(); |
| 3580 | + fs::write(&frames_path, &frames_all).unwrap(); | ||
| 3507 | println!("wrote {}", out_path.display()); | 3581 | println!("wrote {}", out_path.display()); |
| 3508 | } | 3582 | } |
| 3583 | + | ||
| 3584 | +/// Startup leak repro (lane-opus-reell): the first frames of the reell source | ||
| 3585 | +/// (digital silence then an attack) through the library gate's encoder | ||
| 3586 | +/// settings and our decoder. Prints per-frame CELT facts; fails if the decode | ||
| 3587 | +/// peak exceeds 4x the input peak in any frame. | ||
| 3588 | +#[test] | ||
| 3589 | +fn celt_silence_then_attack_decodes_bounded() { | ||
| 3590 | + let fs = 960usize; | ||
| 3591 | + let ch = 2usize; | ||
| 3592 | + let src = shellexpand("~/Music/reell.mp4"); | ||
| 3593 | + if !src.exists() { | ||
| 3594 | + eprintln!("SKIP: missing {}", src.display()); | ||
| 3595 | + return; | ||
| 3596 | + } | ||
| 3597 | + let pcm = ffmpeg_decode_pcm(&src, 1.0); | ||
| 3598 | + let mut enc = Encoder::new(48000, ch, Application::Audio).unwrap(); | ||
| 3599 | + enc.set_bitrate(96_000); | ||
| 3600 | + enc.set_vbr_constrained(true); | ||
| 3601 | + let mut dec = Decoder::new(48000, ch).unwrap(); | ||
| 3602 | + let mut bad = Vec::new(); | ||
| 3603 | + for frame in 0..12 { | ||
| 3604 | + let slice = &pcm[frame * fs * ch..(frame + 1) * fs * ch]; | ||
| 3605 | + let in_peak = slice.iter().fold(0f32, |a, &x| a.max(x.abs())); | ||
| 3606 | + let pcm_i16 = to_i16(slice); | ||
| 3607 | + let mut buf = [0u8; 1500]; | ||
| 3608 | + let n = enc.encode(&pcm_i16, fs, &mut buf).unwrap(); | ||
| 3609 | + let ed = enc.last_celt_diag().clone(); | ||
| 3610 | + let mut out = vec![0f32; fs * ch]; | ||
| 3611 | + let got = dec.decode_float(&buf[..n], &mut out).unwrap(); | ||
| 3612 | + let d = dec.last_celt_diag().clone(); | ||
| 3613 | + let peak = out[..got * ch].iter().fold(0f32, |a, &x| a.max(x.abs())); | ||
| 3614 | + let emax = d.old_band_e.iter().cloned().fold(f32::MIN, f32::max); | ||
| 3615 | + println!( | ||
| 3616 | + "frame {frame}: in_peak {in_peak:.5} bytes {n} enc(sil {} intra {} trans {}) dec(sil {} intra {} trans {} bits {}) bandE max {emax:.2} peak {peak:.5}", | ||
| 3617 | + ed.silence, ed.intra, ed.is_transient, d.silence, d.intra, d.transient, d.total_bits | ||
| 3618 | + ); | ||
| 3619 | + if peak > 4.0 * in_peak.max(1e-4) { | ||
| 3620 | + bad.push(frame); | ||
| 3621 | + } | ||
| 3622 | + } | ||
| 3623 | + assert!(bad.is_empty(), "decode peak >4x input in frames {bad:?}"); | ||
| 3624 | +} | ||
| 3625 | + | ||
| 3626 | +/// Sample-level view of the reell startup: per-120-sample energy of source, | ||
| 3627 | +/// ours (gate path) and libopus reference around the first attack. | ||
| 3628 | +#[test] | ||
| 3629 | +#[ignore] | ||
| 3630 | +fn reell_startup_hop_energies() { | ||
| 3631 | + const CH: usize = 2; | ||
| 3632 | + let src = shellexpand("~/Music/reell.mp4"); | ||
| 3633 | + if !src.exists() { | ||
| 3634 | + return; | ||
| 3635 | + } | ||
| 3636 | + let pcm = ffmpeg_decode_pcm(&src, 2.0); | ||
| 3637 | + let scratch = std::env::temp_dir().join("ec-opus-reell-startup.opus"); | ||
| 3638 | + ffmpeg_encode_libopus(&src, 96, &scratch, 2.0); | ||
| 3639 | + let (ref_dec, _) = decode_ogg(&scratch); | ||
| 3640 | + let (lag_r, ref_al) = align_to_source(&pcm, &ref_dec, CH, 2000); | ||
| 3641 | + let mut enc = Encoder::new(48000, CH, Application::Audio).unwrap(); | ||
| 3642 | + enc.set_bitrate(96_000); | ||
| 3643 | + enc.set_vbr_constrained(true); | ||
| 3644 | + let (ours_dec, _) = roundtrip_own(&mut enc, &pcm, CH, 960); | ||
| 3645 | + let (lag_o, ours_al) = align_to_source(&pcm, &ours_dec, CH, 2000); | ||
| 3646 | + let first = |v: &[f32]| v.iter().position(|x| x.abs() > 1e-4).map(|i| i / CH); | ||
| 3647 | + println!("lag ref {lag_r} ours {lag_o}; first>1e-4: src {:?} ours {:?} ref {:?}", first(&pcm), first(&ours_al), first(&ref_al)); | ||
| 3648 | + let e = |v: &[f32], h: usize| -> f64 { | ||
| 3649 | + v[h * 120 * CH..((h + 1) * 120 * CH).min(v.len())].iter().map(|&x| (x as f64) * (x as f64)).sum() | ||
| 3650 | + }; | ||
| 3651 | + // Frame-2 flags of both streams through our decoder. | ||
| 3652 | + let data = fs::read(&scratch).unwrap(); | ||
| 3653 | + let packets = ogg_packets(&data); | ||
| 3654 | + let mut rd = Decoder::new(48000, CH).unwrap(); | ||
| 3655 | + let mut od = Decoder::new(48000, CH).unwrap(); | ||
| 3656 | + let mut oe = Encoder::new(48000, CH, Application::Audio).unwrap(); | ||
| 3657 | + oe.set_bitrate(96_000); | ||
| 3658 | + oe.set_vbr_constrained(true); | ||
| 3659 | + let mut buf = vec![0f32; 5760 * CH]; | ||
| 3660 | + let mut out = vec![0u8; 1500]; | ||
| 3661 | + for f in 0..4 { | ||
| 3662 | + rd.decode_float(&packets[2 + f], &mut buf).unwrap(); | ||
| 3663 | + let r = rd.last_celt_diag().clone(); | ||
| 3664 | + let n = oe.encode_float(&pcm[f * 960 * CH..(f + 1) * 960 * CH], 960, &mut out).unwrap(); | ||
| 3665 | + od.decode_float(&out[..n], &mut buf).unwrap(); | ||
| 3666 | + let o = od.last_celt_diag().clone(); | ||
| 3667 | + let e = oe.last_celt_diag().clone(); | ||
| 3668 | + println!("frame {f} REF bytes {} sil {} intra {} trans {} spread {} ac {} tf {:?}", packets[2 + f].len(), r.silence, r.intra, r.transient, r.spread, r.anti_collapse, r.tf_res); | ||
| 3669 | + println!("frame {f} OUR bytes {n} sil {} intra {} trans {} spread {} ac {} tf {:?} shorts {} pulses {:?}", o.silence, o.intra, o.transient, o.spread, o.anti_collapse, o.tf_res, e.short_blocks, e.pulses); | ||
| 3670 | + } | ||
| 3671 | + println!("hop(ms)\tsrc\tours\tref"); | ||
| 3672 | + for h in 10..24 { | ||
| 3673 | + println!("{:.1}\t{:.3e}\t{:.3e}\t{:.3e}", h as f64 * 2.5, e(&pcm, h), e(&ours_al, h), e(&ref_al, h)); | ||
| 3674 | + } | ||
| 3675 | +} | ||
| 3676 | + | ||
| 3677 | +/// Where a lone click lands after a CELT-only fullband roundtrip, relative to | ||
| 3678 | +/// the encoder look-ahead (diagnostic for the hybrid alignment tolerance). | ||
| 3679 | +#[test] | ||
| 3680 | +#[ignore] | ||
| 3681 | +fn celt_click_peak_offset() { | ||
| 3682 | + let mut click = vec![0f32; 48000]; | ||
| 3683 | + let at = 960 * 10 + 300; | ||
| 3684 | + click[at] = 0.9; | ||
| 3685 | + for bps in [32_000u32, 64_000, 128_000] { | ||
| 3686 | + let mut enc = Encoder::new(48000, 1, Application::Audio).unwrap(); | ||
| 3687 | + enc.set_mode(Some(ec_opus::Mode::Celt)); | ||
| 3688 | + enc.set_bitrate(bps); | ||
| 3689 | + let (dec, _) = roundtrip_own(&mut enc, &click, 1, 960); | ||
| 3690 | + let p = dec.iter().enumerate().max_by(|a, b| a.1.abs().partial_cmp(&b.1.abs()).unwrap()).unwrap().0; | ||
| 3691 | + let d = enc.last_celt_diag().clone(); | ||
| 3692 | + println!("celt click {bps}: peak +{} (lm {})", p as i64 - at as i64, d.lm); | ||
| 3693 | + } | ||
| 3694 | +} | ||
lanes/opus-reell-r2.report.mdadded+47 -0
| @@ -0,0 +1,47 @@ | |||
| 1 | +# lane-opus-reell r2 — startup leak = transient tf_res default inversion | ||
| 2 | + | ||
| 3 | +## Cause | ||
| 4 | +Silence frames decode to exact zeros (`celt_silence_then_attack_decodes_bounded`). | ||
| 5 | +The leak was pre-echo in the first attack frame: encoder coded raw `tf_res = 0` | ||
| 6 | +with `tf_select = 0` on a transient LM=3 frame, which `TF_SELECT[3][4+0+0]` maps | ||
| 7 | +to `+3` — all 8 short blocks merged back into one long block in every band. The | ||
| 8 | +source is ≈0 for 12 ms before the onset, so opus_compare's Y/X ratio exploded. | ||
| 9 | +libopus without tf_analysis codes raw `tf_res[i] = isTransient` (change 0). | ||
| 10 | + | ||
| 11 | +Evidence (`reell_startup_hop_energies`, energy per 2.5 ms hop, 37.5–45 ms): | ||
| 12 | +ours before 8e-4…6e-3, ours after 3e-19…2e-18, libopus 0…8e-7. Decoder diag of | ||
| 13 | +frame 2: OUR tf `[3;21]` before, `[0;21]` after; REF `[1,1,1,-1,…]` (tf_analysis). | ||
| 14 | + | ||
| 15 | +## Fix | ||
| 16 | +`celt_enc.rs`: `self.tf_res = [i32::from(is_transient); NB_BANDS]` + doc comment | ||
| 17 | +corrected (it claimed the opposite of the table). `CeltDecDiag` gained | ||
| 18 | +`tf_res/spread/anti_collapse`. | ||
| 19 | + | ||
| 20 | +## Gate (14 rows, vs opus-drop-r2) | ||
| 21 | +nik@64k: ref 69.2 ours 69.2 kbps (+0.0%), corr o=0.9876 r=0.9864 gap=-0.0012, Q o=-399.22 r=-154.78, err_ratio 2.764, drop o=0 r=0 | ||
| 22 | +nik@96k: ref 101.0 ours 100.8 kbps (-0.2%), corr o=0.9944 r=0.9940 gap=-0.0003, Q o=-227.79 r=-31.55, err_ratio 3.239, drop o=0 r=0 | ||
| 23 | +reelo@64k: ref 63.6 ours 63.5 kbps (-0.0%), corr o=0.9883 r=0.9866 gap=-0.0017, Q o=-335.73 r=-123.36, err_ratio 2.618, drop o=0 r=0 | ||
| 24 | +reelo@96k: ref 93.9 ours 93.8 kbps (-0.0%), corr o=0.9946 r=0.9937 gap=-0.0009, Q o=-265.38 r=-125.18, err_ratio 1.966, drop o=0 r=0 | ||
| 25 | +her@64k: ref 67.9 ours 67.3 kbps (-0.9%), corr o=0.9827 r=0.9794 gap=-0.0033, Q o=-839.84 r=-339.00, err_ratio 4.650, drop o=0 r=0 | ||
| 26 | +her@96k: ref 101.5 ours 100.9 kbps (-0.6%), corr o=0.9913 r=0.9902 gap=-0.0011, Q o=-899.99 r=-191.50, err_ratio 10.126, drop o=0 r=0 | ||
| 27 | +reell@64k: ref 71.3 ours 71.3 kbps (-0.0%), corr o=0.9903 r=0.9880 gap=-0.0024, Q o=-222.24 r=-128.39, err_ratio 1.603, drop o=0 r=0 | ||
| 28 | +reell@96k: ref 105.4 ours 105.3 kbps (-0.0%), corr o=0.9954 r=0.9948 gap=-0.0007, Q o=-229.13 r=-29.16, err_ratio 3.328, drop o=0 r=0 | ||
| 29 | +reelm@64k: ref 63.3 ours 62.6 kbps (-1.1%), corr o=0.9816 r=0.9859 gap=+0.0043, Q o=-389.15 r=-392.46, err_ratio 0.988, drop o=0 r=0 | ||
| 30 | +reelm@96k: ref 84.8 ours 84.0 kbps (-1.0%), corr o=0.9920 r=0.9920 gap=-0.0001, Q o=-303.07 r=-231.16, err_ratio 1.346, drop o=0 r=0 | ||
| 31 | +dl8a@64k: ref 65.8 ours 65.4 kbps (-0.6%), corr o=0.9879 r=0.9865 gap=-0.0015, Q o=-386.87 r=-378.84, err_ratio 1.029, drop o=0 r=0 | ||
| 32 | +dl8a@96k: ref 96.8 ours 96.0 kbps (-0.9%), corr o=0.9939 r=0.9934 gap=-0.0006, Q o=-340.97 r=-423.47, err_ratio 0.747, drop o=0 r=0 | ||
| 33 | +hein@64k: ref 64.9 ours 64.5 kbps (-0.6%), corr o=0.9853 r=0.9874 gap=+0.0020, Q o=-426.68 r=-388.31, err_ratio 1.141, drop o=0 r=0 | ||
| 34 | +hein@96k: ref 86.9 ours 86.3 kbps (-0.7%), corr o=0.9937 r=0.9930 gap=-0.0006, Q o=-376.12 r=-246.46, err_ratio 1.653, drop o=0 r=0 | ||
| 35 | +RATE ±5% pass, DROPOUT pass. reell err_ratio 21→1.60 @64, 23.9→3.33 @96; corr gap | ||
| 36 | +−.0024/−.0007. No row regressed corr by >.001; reelm/hein@64 unchanged (lane-opus-64). | ||
| 37 | + | ||
| 38 | +## Suite | ||
| 39 | +ec-opus: 34 + 27 pass, 7 ignored. Two assertions re-sized after the change: | ||
| 40 | +- `hybrid_layers_align`: layer tolerance 2→3. Fullband CELT click peaks at | ||
| 41 | + exactly +120 (`celt_click_peak_offset`, 32/64/128 k); the band-limited HB | ||
| 42 | + layer's main lobe now sits at +118, SILK at +121. | ||
| 43 | +- `silk_compares_to_celt_on_speech_at_speech_rates`: SILK ≥ CELT − .005; CELT | ||
| 44 | + edges SILK by .002 at NB16 (synthetic). Residue: SILK encoder quality. | ||
| 45 | + | ||
| 46 | +## Residue | ||
| 47 | +her@96 err_ratio 10.1 (was 13+) — next largest startup/transient window; not named. | ||
lanes/opus-reell-r2.sweep.txtadded+17 -0
| @@ -0,0 +1,17 @@ | |||
| 1 | +# ec-opus encoder vs ffmpeg libopus — encoders-only gate (r1) | ||
| 2 | +# both bitstreams decoded by ec-opus; 120s cap; VBR; 48kHz stereo | ||
| 3 | +# source kbps ours_kbps ref_kbps rate% corr_ours corr_ref gap Q_ours Q_ref err_ratio minsec_ours minsec_ref drop_ours drop_ref | ||
| 4 | +nik 64 69.2 69.2 +0.0 0.9876 0.9864 -0.0012 -399.22 -154.78 2.764 0.9759 0.9745 0 0 | ||
| 5 | +nik 96 100.8 101.0 -0.2 0.9944 0.9940 -0.0003 -227.79 -31.55 3.239 0.9881 0.9888 0 0 | ||
| 6 | +reelo 64 63.5 63.6 -0.0 0.9883 0.9866 -0.0017 -335.73 -123.36 2.618 0.9644 0.9686 0 0 | ||
| 7 | +reelo 96 93.8 93.9 -0.0 0.9946 0.9937 -0.0009 -265.38 -125.18 1.966 0.9862 0.9854 0 0 | ||
| 8 | +her 64 67.3 67.9 -0.9 0.9827 0.9794 -0.0033 -839.84 -339.00 4.650 0.9472 0.9406 0 0 | ||
| 9 | +her 96 100.9 101.5 -0.6 0.9913 0.9902 -0.0011 -899.99 -191.50 10.126 0.9730 0.9728 0 0 | ||
| 10 | +reell 64 71.3 71.3 -0.0 0.9903 0.9880 -0.0024 -222.24 -128.39 1.603 0.9700 0.9644 0 0 | ||
| 11 | +reell 96 105.3 105.4 -0.0 0.9954 0.9948 -0.0007 -229.13 -29.16 3.328 0.9852 0.9804 0 0 | ||
| 12 | +reelm 64 62.6 63.3 -1.1 0.9816 0.9859 +0.0043 -389.15 -392.46 0.988 0.9151 0.9100 0 0 | ||
| 13 | +reelm 96 84.0 84.8 -1.0 0.9920 0.9920 -0.0001 -303.07 -231.16 1.346 0.9661 0.9511 0 0 | ||
| 14 | +dl8a 64 65.4 65.8 -0.6 0.9879 0.9865 -0.0015 -386.87 -378.84 1.029 0.9744 0.9723 0 0 | ||
| 15 | +dl8a 96 96.0 96.8 -0.9 0.9939 0.9934 -0.0006 -340.97 -423.47 0.747 0.9858 0.9864 0 0 | ||
| 16 | +hein 64 64.5 64.9 -0.6 0.9853 0.9874 +0.0020 -426.68 -388.31 1.141 0.9209 0.9148 0 0 | ||
| 17 | +hein 96 86.3 86.9 -0.7 0.9937 0.9930 -0.0006 -376.12 -246.46 1.653 0.9675 0.9499 0 0 | ||