Libopus dc_reject on the API input, and lag-dependent tests read look_ahead
Browse files at this commit0151578356e0061225a91c7df907b4e6754318e4Parent: b1e155b- feat
- libopus dc_reject on the API input, and lag-dependent tests read look_ahead
4 changed files +163 -60
- crates/ec-opus/src/celt_enc.rs +30 -3
- crates/ec-opus/src/encoder.rs +85 -34
- crates/ec-opus/src/multistream_enc.rs +7 -0
- crates/ec-opus/tests/conformance.rs +41 -23
crates/ec-opus/src/celt_enc.rsmodified+30 -3
| @@ -794,8 +794,10 @@ impl CeltEncoder { | ||
| 794 | 794 | // .708->.703, reelo@96 .940->.872, dl8a .551/.374->.503/.346, hein |
| 795 | 795 | // .855/.528->.771/.419), corr within +.0001 everywhere. The KEEP rule |
| 796 | 796 | // is "err_ratio not worse on every row", so the search ships off. |
| 797 | + // `EC_OPUS_TWO_PASS=1` re-arms it for a sweep (lane opusx2 r2). | |
| 797 | 798 | const TWO_PASS_COARSE_ENERGY: bool = false; |
| 798 | - let mut two_pass = TWO_PASS_COARSE_ENERGY; | |
| 799 | + let mut two_pass = | |
| 800 | + TWO_PASS_COARSE_ENERGY || std::env::var_os("EC_OPUS_TWO_PASS").is_some(); | |
| 799 | 801 | let mut intra = self.force_intra |
| 800 | 802 | || (!two_pass |
| 801 | 803 | && self.delayed_intra > (2 * c * (end - start)) as f32 |
| @@ -896,7 +898,7 @@ impl CeltEncoder { | ||
| 896 | 898 | // --- Allocation trim ------------------------------------------------ |
| 897 | 899 | let mut alloc_trim = 5i32; |
| 898 | 900 | if tell_frac + (6 << BITRES) <= total_bits_frac - total_boost { |
| 899 | - alloc_trim = self.alloc_trim_analysis(end, lm, c, n); | |
| 901 | + alloc_trim = self.alloc_trim_analysis(end, lm, c, n, tf_estimate); | |
| 900 | 902 | enc.enc_icdf(alloc_trim as usize, &TRIM_ICDF, 7); |
| 901 | 903 | tell_frac = enc.tell_frac() as i32; |
| 902 | 904 | } |
| @@ -1574,7 +1576,20 @@ impl CeltEncoder { | ||
| 1574 | 1576 | /// dl8a@64 .9889->.9879). The KEEP rule rejects it; this is the third |
| 1575 | 1577 | /// rejection of the same port (see `lanes/opus-trim-r2.{A,B}.sweep.txt`). |
| 1576 | 1578 | /// Do not re-port it without a fix for the transient rows first. |
| 1577 | - fn alloc_trim_analysis(&mut self, end: usize, lm: usize, c: usize, n0: usize) -> i32 { | |
| 1579 | + /// | |
| 1580 | + /// `EC_OPUS_TRIM13=1` selects the continuous 1.3 form instead, for the | |
| 1581 | + /// re-sweep on the aligned (delayed) spectrum the discrete ladder was | |
| 1582 | + /// never measured against. | |
| 1583 | + fn alloc_trim_analysis( | |
| 1584 | + &mut self, | |
| 1585 | + end: usize, | |
| 1586 | + lm: usize, | |
| 1587 | + c: usize, | |
| 1588 | + n0: usize, | |
| 1589 | + tf_estimate: f32, | |
| 1590 | + ) -> i32 { | |
| 1591 | + let cont = std::env::var_os("EC_OPUS_TRIM13").is_some(); | |
| 1592 | + let mut trim_f = 5.0f32; | |
| 1578 | 1593 | let mut trim = 5i32; |
| 1579 | 1594 | if c == 2 { |
| 1580 | 1595 | let mut sum = 0.0f32; |
| @@ -1586,6 +1601,10 @@ impl CeltEncoder { | ||
| 1586 | 1601 | sum += partial; |
| 1587 | 1602 | } |
| 1588 | 1603 | sum *= 1.0 / 8.0; |
| 1604 | + if cont { | |
| 1605 | + let sum_c = sum.abs().min(1.0); | |
| 1606 | + trim_f += (0.75 * (1.001 - sum_c * sum_c).log2()).max(-4.0); | |
| 1607 | + } | |
| 1589 | 1608 | if sum > 0.995 { |
| 1590 | 1609 | trim -= 4; |
| 1591 | 1610 | } else if sum > 0.92 { |
| @@ -1619,6 +1638,14 @@ impl CeltEncoder { | ||
| 1619 | 1638 | * (2 + 2 * i as i32 - NB_BANDS as i32) as f32; |
| 1620 | 1639 | } |
| 1621 | 1640 | } |
| 1641 | + if cont { | |
| 1642 | + // libopus divides the same sum by `C*(end-1)`; the ladder above | |
| 1643 | + // carries an extra factor of two in its thresholds. | |
| 1644 | + let diff = diff / (c * (end - 1)) as f32; | |
| 1645 | + trim_f -= ((diff + 1.0) / 6.0).clamp(-2.0, 2.0); | |
| 1646 | + trim_f -= 2.0 * tf_estimate; | |
| 1647 | + return (trim_f + 0.5).floor().clamp(0.0, 10.0) as i32; | |
| 1648 | + } | |
| 1622 | 1649 | diff /= (2 * c * (end - 1)) as f32; |
| 1623 | 1650 | if diff > 2.0 { |
| 1624 | 1651 | trim -= 1; |
crates/ec-opus/src/encoder.rsmodified+85 -34
| @@ -116,6 +116,12 @@ pub struct Encoder { | ||
| 116 | 116 | /// Scratch space for the zero-stuffed SILK/hybrid input, sized once so |
| 117 | 117 | /// the steady-state loop doesn't allocate a `Vec` per frame. |
| 118 | 118 | silk_stuff: Vec<f32>, |
| 119 | + /// libopus `opus_encoder.c`'s `hp_mem`: the one-pole state of the | |
| 120 | + /// [`Encoder::dc_reject`] filter every frame of API input goes through, | |
| 121 | + /// one per channel. | |
| 122 | + hp_mem: [f32; 2], | |
| 123 | + /// Scratch space for that filter's output, sized once. | |
| 124 | + hp_buf: Vec<f32>, | |
| 119 | 125 | } |
| 120 | 126 | |
| 121 | 127 | /// SILK's internal sample rate (kHz) for a target bandwidth: Narrow → 8, |
| @@ -175,6 +181,8 @@ impl Encoder { | ||
| 175 | 181 | silk_buf: [0u8; MAX_SILK_PACKET_BYTES], |
| 176 | 182 | silk_delay: Vec::new(), |
| 177 | 183 | silk_stuff: Vec::new(), |
| 184 | + hp_mem: [0.0; 2], | |
| 185 | + hp_buf: Vec::new(), | |
| 178 | 186 | }) |
| 179 | 187 | } |
| 180 | 188 | |
| @@ -283,6 +291,7 @@ impl Encoder { | ||
| 283 | 291 | self.silk_stereo_mb = None; |
| 284 | 292 | self.silk_stereo_wb = None; |
| 285 | 293 | self.silk_delay.clear(); |
| 294 | + self.hp_mem = [0.0; 2]; | |
| 286 | 295 | self.final_range = 0; |
| 287 | 296 | } |
| 288 | 297 | |
| @@ -692,6 +701,32 @@ impl Encoder { | ||
| 692 | 701 | BWS[idx].min(ceiling) |
| 693 | 702 | } |
| 694 | 703 | |
| 704 | + /// libopus `opus_encoder.c`'s `dc_reject(pcm, 3, ...)`: a 3 Hz one-pole | |
| 705 | + /// high-pass on the API input, applied once before *both* layers (libopus | |
| 706 | + /// filters into `pcm_buf`, which is what `silk_Encode` and | |
| 707 | + /// `celt_encode_with_ec` both read). `coef = 6.3 * cutoff / Fs` at the | |
| 708 | + /// API rate; the output is the input minus a leaky integrator of it, and | |
| 709 | + /// the integrator state carries across frames. | |
| 710 | + /// | |
| 711 | + /// libopus takes the `hp_cutoff` branch instead for | |
| 712 | + /// `OPUS_APPLICATION_VOIP` (a bandwidth-dependent second-order section); | |
| 713 | + /// not ported — every VoIP frame still gets this DC reject. | |
| 714 | + fn dc_reject(&mut self, pcm: &[f32], frame_size: usize, out: &mut Vec<f32>) { | |
| 715 | + let c = self.channels; | |
| 716 | + let coef = 6.3 * 3.0 / self.sample_rate as f32; | |
| 717 | + out.clear(); | |
| 718 | + out.resize(frame_size * c, 0.0); | |
| 719 | + for ch in 0..c { | |
| 720 | + let mut m = self.hp_mem[ch]; | |
| 721 | + for i in 0..frame_size { | |
| 722 | + let x = pcm[i * c + ch]; | |
| 723 | + out[i * c + ch] = x - m; | |
| 724 | + m += coef * (x - m); | |
| 725 | + } | |
| 726 | + self.hp_mem[ch] = m; | |
| 727 | + } | |
| 728 | + } | |
| 729 | + | |
| 695 | 730 | /// Encodes one frame — `frame_size` samples per channel of interleaved |
| 696 | 731 | /// `f32` at the input rate, 2.5, 5, 10 or 20 ms of it — as a code-0 Opus |
| 697 | 732 | /// packet in `out`, returning the packet length. |
| @@ -701,19 +736,27 @@ impl Encoder { | ||
| 701 | 736 | frame_size: usize, |
| 702 | 737 | out: &mut [u8], |
| 703 | 738 | ) -> Result<usize> { |
| 704 | - let (toc, payload) = | |
| 705 | - self.encode_toc_and_payload(pcm, frame_size, out.len().saturating_sub(1))?; | |
| 706 | - let n = payload.len(); | |
| 707 | - if out.len() < 1 + n { | |
| 708 | - return Err(Error::corrupt(format!( | |
| 709 | - "opus encode: packet needs {} bytes, buffer holds {}", | |
| 710 | - 1 + n, | |
| 711 | - out.len() | |
| 712 | - ))); | |
| 713 | - } | |
| 714 | - out[0] = toc; | |
| 715 | - out[1..1 + n].copy_from_slice(payload); | |
| 716 | - Ok(1 + n) | |
| 739 | + let mut hp = std::mem::take(&mut self.hp_buf); | |
| 740 | + self.dc_reject(pcm, frame_size, &mut hp); | |
| 741 | + let r = match self.encode_toc_and_payload(&hp, frame_size, out.len().saturating_sub(1)) { | |
| 742 | + Err(e) => Err(e), | |
| 743 | + Ok((toc, payload)) => { | |
| 744 | + let n = payload.len(); | |
| 745 | + if out.len() < 1 + n { | |
| 746 | + Err(Error::corrupt(format!( | |
| 747 | + "opus encode: packet needs {} bytes, buffer holds {}", | |
| 748 | + 1 + n, | |
| 749 | + out.len() | |
| 750 | + ))) | |
| 751 | + } else { | |
| 752 | + out[0] = toc; | |
| 753 | + out[1..1 + n].copy_from_slice(payload); | |
| 754 | + Ok(1 + n) | |
| 755 | + } | |
| 756 | + } | |
| 757 | + }; | |
| 758 | + self.hp_buf = hp; | |
| 759 | + r | |
| 717 | 760 | } |
| 718 | 761 | |
| 719 | 762 | /// [`Encoder::encode_float`] from 16-bit samples. |
| @@ -731,27 +774,35 @@ impl Encoder { | ||
| 731 | 774 | frame_size: usize, |
| 732 | 775 | out: &mut [u8], |
| 733 | 776 | ) -> Result<usize> { |
| 734 | - let (toc, payload) = | |
| 735 | - self.encode_toc_and_payload(pcm, frame_size, out.len().saturating_sub(3))?; | |
| 736 | - let n = payload.len(); | |
| 737 | - let len_bytes = if n < 252 { 1 } else { 2 }; | |
| 738 | - if out.len() < 1 + len_bytes + n { | |
| 739 | - return Err(Error::corrupt(format!( | |
| 740 | - "opus encode: self-delimited packet needs {} bytes, buffer holds {}", | |
| 741 | - 1 + len_bytes + n, | |
| 742 | - out.len() | |
| 743 | - ))); | |
| 744 | - } | |
| 745 | - out[0] = toc; | |
| 746 | - if n < 252 { | |
| 747 | - out[1] = n as u8; | |
| 748 | - } else { | |
| 749 | - let b0 = 252 + ((n - 252) & 3); | |
| 750 | - out[1] = b0 as u8; | |
| 751 | - out[2] = ((n - b0) / 4) as u8; | |
| 752 | - } | |
| 753 | - out[1 + len_bytes..1 + len_bytes + n].copy_from_slice(payload); | |
| 754 | - Ok(1 + len_bytes + n) | |
| 777 | + let mut hp = std::mem::take(&mut self.hp_buf); | |
| 778 | + self.dc_reject(pcm, frame_size, &mut hp); | |
| 779 | + let r = match self.encode_toc_and_payload(&hp, frame_size, out.len().saturating_sub(3)) { | |
| 780 | + Err(e) => Err(e), | |
| 781 | + Ok((toc, payload)) => { | |
| 782 | + let n = payload.len(); | |
| 783 | + let len_bytes = if n < 252 { 1 } else { 2 }; | |
| 784 | + if out.len() < 1 + len_bytes + n { | |
| 785 | + Err(Error::corrupt(format!( | |
| 786 | + "opus encode: self-delimited packet needs {} bytes, buffer holds {}", | |
| 787 | + 1 + len_bytes + n, | |
| 788 | + out.len() | |
| 789 | + ))) | |
| 790 | + } else { | |
| 791 | + out[0] = toc; | |
| 792 | + if n < 252 { | |
| 793 | + out[1] = n as u8; | |
| 794 | + } else { | |
| 795 | + let b0 = 252 + ((n - 252) & 3); | |
| 796 | + out[1] = b0 as u8; | |
| 797 | + out[2] = ((n - b0) / 4) as u8; | |
| 798 | + } | |
| 799 | + out[1 + len_bytes..1 + len_bytes + n].copy_from_slice(payload); | |
| 800 | + Ok(1 + len_bytes + n) | |
| 801 | + } | |
| 802 | + } | |
| 803 | + }; | |
| 804 | + self.hp_buf = hp; | |
| 805 | + r | |
| 755 | 806 | } |
| 756 | 807 | |
| 757 | 808 | /// One hybrid frame (10 or 20 ms, mono or stereo, `bandwidth` SWB or FB) |
crates/ec-opus/src/multistream_enc.rsmodified+7 -0
| @@ -27,6 +27,13 @@ pub struct MultistreamEncoder { | ||
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | impl MultistreamEncoder { |
| 30 | + /// The encoder delay each stream carries for a `frame_size` frame — the | |
| 31 | + /// per-stream [`Encoder::look_ahead`], which every stream shares, and so | |
| 32 | + /// the Ogg-Opus pre-skip a multistream file must carry. | |
| 33 | + pub fn look_ahead(&self, frame_size: usize) -> usize { | |
| 34 | + self.streams[0].look_ahead(frame_size) | |
| 35 | + } | |
| 36 | + | |
| 30 | 37 | /// An encoder for `streams` elementary streams of which the first |
| 31 | 38 | /// `coupled` are stereo, with `mapping` naming the coded channel each |
| 32 | 39 | /// input channel feeds. Layout rules are the decoder's, checked the same |
crates/ec-opus/tests/conformance.rsmodified+41 -23
| @@ -864,17 +864,18 @@ fn roundtrip_own( | ||
| 864 | 864 | (decoded, bytes) |
| 865 | 865 | } |
| 866 | 866 | |
| 867 | -/// Worst-channel correlation against the source with the encoder's | |
| 868 | -/// 120-sample delay removed. | |
| 869 | -fn delayed_corr(source: &[f32], decoded: &[f32], channels: usize) -> f64 { | |
| 870 | - const DELAY: usize = 120; | |
| 871 | - let n = (decoded.len() / channels).saturating_sub(DELAY); | |
| 867 | +/// Worst-channel correlation against the source with the encoder's own | |
| 868 | +/// delay removed — `delay` is [`Encoder::look_ahead`] for the frame size the | |
| 869 | +/// round trip used, never a constant (it moved 120 -> 312 when the CELT layer | |
| 870 | +/// took libopus's Fs/250 input delay). | |
| 871 | +fn delayed_corr(source: &[f32], decoded: &[f32], channels: usize, delay: usize) -> f64 { | |
| 872 | + let n = (decoded.len() / channels).saturating_sub(delay); | |
| 872 | 873 | let n = n.min(source.len() / channels); |
| 873 | 874 | let mut worst = 1.0f64; |
| 874 | 875 | for c in 0..channels { |
| 875 | 876 | let a: Vec<f32> = (0..n).map(|i| source[i * channels + c]).collect(); |
| 876 | 877 | let b: Vec<f32> = (0..n) |
| 877 | - .map(|i| decoded[(i + DELAY) * channels + c]) | |
| 878 | + .map(|i| decoded[(i + delay) * channels + c]) | |
| 878 | 879 | .collect(); |
| 879 | 880 | worst = worst.min(correlation(&a, &b)); |
| 880 | 881 | } |
| @@ -894,7 +895,7 @@ fn encoder_roundtrips_at_every_rate() { | ||
| 894 | 895 | let mut enc = Encoder::new(48000, channels, Application::Audio).unwrap(); |
| 895 | 896 | enc.set_bitrate(kbps * 1000); |
| 896 | 897 | let (decoded, bytes) = roundtrip_own(&mut enc, &pcm, channels, 960); |
| 897 | - let corr = delayed_corr(&pcm, &decoded, channels); | |
| 898 | + let corr = delayed_corr(&pcm, &decoded, channels, enc.look_ahead(960)); | |
| 898 | 899 | let rate = bytes as f64 * 8.0 / 2.0 / 1000.0; |
| 899 | 900 | println!("{channels} ch {kbps:>3} kbps: corr {corr:.4}, actual {rate:.1} kbps"); |
| 900 | 901 | let floor = if kbps < 64 { 0.8 } else { 0.9 }; |
| @@ -1271,7 +1272,7 @@ fn encoder_frame_sizes_and_vbr() { | ||
| 1271 | 1272 | let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap(); |
| 1272 | 1273 | enc.set_bitrate(128_000); |
| 1273 | 1274 | let (decoded, _) = roundtrip_own(&mut enc, &pcm, 2, frame); |
| 1274 | - let corr = delayed_corr(&pcm, &decoded, 2); | |
| 1275 | + let corr = delayed_corr(&pcm, &decoded, 2, enc.look_ahead(frame)); | |
| 1275 | 1276 | println!("frame {frame}: corr {corr:.4}"); |
| 1276 | 1277 | assert!(corr >= 0.85, "frame {frame}: correlation {corr:.4}"); |
| 1277 | 1278 | } |
| @@ -1279,7 +1280,7 @@ fn encoder_frame_sizes_and_vbr() { | ||
| 1279 | 1280 | enc.set_bitrate(128_000); |
| 1280 | 1281 | enc.set_vbr_constrained(true); |
| 1281 | 1282 | let (decoded, bytes) = roundtrip_own(&mut enc, &pcm, 2, 960); |
| 1282 | - let corr = delayed_corr(&pcm, &decoded, 2); | |
| 1283 | + let corr = delayed_corr(&pcm, &decoded, 2, enc.look_ahead(960)); | |
| 1283 | 1284 | let rate = bytes as f64 * 8.0 / 1.0 / 1000.0; |
| 1284 | 1285 | println!("cvbr 128k: corr {corr:.4}, actual {rate:.1} kbps"); |
| 1285 | 1286 | assert!(corr >= 0.9, "cvbr: correlation {corr:.4}"); |
| @@ -1405,10 +1406,10 @@ fn oracle_decodes_our_packets_across_the_rate_table() { | ||
| 1405 | 1406 | write_ogg_opus( |
| 1406 | 1407 | &path, |
| 1407 | 1408 | &packets, |
| 1408 | - opus_head(channels, 120, None), | |
| 1409 | + opus_head(channels, enc.look_ahead(960) as u16, None), | |
| 1409 | 1410 | channels, |
| 1410 | 1411 | total, |
| 1411 | - 120, | |
| 1412 | + enc.look_ahead(960) as i64, | |
| 1412 | 1413 | ); |
| 1413 | 1414 | let Some(reference) = ffmpeg_decode(&path, channels) else { |
| 1414 | 1415 | eprintln!("ffmpeg unavailable, skipped"); |
| @@ -1534,9 +1535,10 @@ fn five_one_encode_end_to_end() { | ||
| 1534 | 1535 | let len = enc.encode_float(&silence, 960, &mut out).expect("encode"); |
| 1535 | 1536 | packets.push(out[..len].to_vec()); |
| 1536 | 1537 | let (streams, coupled, mapping) = enc.layout(); |
| 1537 | - let head = opus_head(6, 120, Some((streams, coupled, mapping))); | |
| 1538 | + let pre_skip = enc.look_ahead(960); | |
| 1539 | + let head = opus_head(6, pre_skip as u16, Some((streams, coupled, mapping))); | |
| 1538 | 1540 | let path = temp_path("5.1.opus"); |
| 1539 | - write_ogg_opus(&path, &packets, head, 6, total, 120); | |
| 1541 | + write_ogg_opus(&path, &packets, head, 6, total, pre_skip as i64); | |
| 1540 | 1542 | let Some(reference) = ffmpeg_decode(&path, 6) else { |
| 1541 | 1543 | eprintln!("ffmpeg unavailable, skipped"); |
| 1542 | 1544 | let _ = fs::remove_file(&path); |
| @@ -1568,7 +1570,8 @@ fn ogg_opus_round_trip_duration_is_exact() { | ||
| 1568 | 1570 | enc.set_bitrate(128_000); |
| 1569 | 1571 | let packets = encode_packets(&mut enc, &pcm, 2); |
| 1570 | 1572 | let path = temp_path("duration.opus"); |
| 1571 | - write_ogg_opus(&path, &packets, opus_head(2, 120, None), 2, total, 120); | |
| 1573 | + let pre_skip = enc.look_ahead(960); | |
| 1574 | + write_ogg_opus(&path, &packets, opus_head(2, pre_skip as u16, None), 2, total, pre_skip as i64); | |
| 1572 | 1575 | let probe = Command::new("ffprobe") |
| 1573 | 1576 | .args([ |
| 1574 | 1577 | "-v", |
| @@ -1592,9 +1595,9 @@ fn ogg_opus_round_trip_duration_is_exact() { | ||
| 1592 | 1595 | .unwrap_or(0.0); |
| 1593 | 1596 | // ffprobe reports Ogg-Opus durations *including* the pre-skip — its |
| 1594 | 1597 | // own 3.000 s encode probes as 3.0065 (312/48000 more). Ours carries |
| 1595 | - // a 120-sample pre-skip, so exact is 2.0025; the decoded sample | |
| 1596 | - // count below is the pre-skip-free half of the claim. | |
| 1597 | - let expected = (total as f64 + 120.0) / 48000.0; | |
| 1598 | + // the same pre-skip our encoder advertises, so exact is 2.000 s plus | |
| 1599 | + // it; the decoded sample count below is the pre-skip-free half. | |
| 1600 | + let expected = (total as f64 + pre_skip as f64) / 48000.0; | |
| 1598 | 1601 | assert!( |
| 1599 | 1602 | (dur - expected).abs() < 1e-6, |
| 1600 | 1603 | "ffprobe reports {dur} s, expected {expected} s (2.000 s + pre-skip)" |
| @@ -2525,6 +2528,9 @@ fn hybrid_layers_align() { | ||
| 2525 | 2528 | /// 20 ms FB hybrid at 32 kbps: decodes in our decoder range-exactly, tracks |
| 2526 | 2529 | /// the input full-band and in the SILK layer's band alone, and the reference |
| 2527 | 2530 | /// oracle decodes the packets to the same signal. |
| 2531 | +/// | |
| 2532 | +/// The delay [`hybrid_roundtrip`]'s encoder advertises for a 20 ms frame is | |
| 2533 | +/// [`Encoder::look_ahead`] of that same configuration, never a constant. | |
| 2528 | 2534 | #[test] |
| 2529 | 2535 | fn hybrid_fb_roundtrip() { |
| 2530 | 2536 | for (name, pcm) in silk_sources() { |
| @@ -2542,9 +2548,14 @@ fn hybrid_fb_roundtrip() { | ||
| 2542 | 2548 | ); |
| 2543 | 2549 | assert!(corr >= 0.95, "{name}: full-band corr {corr:.4}"); |
| 2544 | 2550 | // +-3: the wav16 fixture is a pure tone, whose phase moves the peak. |
| 2551 | + let delay = { | |
| 2552 | + let mut e = Encoder::new(48000, 1, Application::Voip).unwrap(); | |
| 2553 | + e.set_bitrate(32_000); | |
| 2554 | + e.look_ahead(960) | |
| 2555 | + }; | |
| 2545 | 2556 | assert!( |
| 2546 | - (117..=123).contains(&lag), | |
| 2547 | - "{name}: aligned at lag {lag}, not the documented 120" | |
| 2557 | + (delay - 3..=delay + 3).contains(&lag), | |
| 2558 | + "{name}: aligned at lag {lag}, not the advertised {delay}" | |
| 2548 | 2559 | ); |
| 2549 | 2560 | assert!(lb_corr >= 0.9, "{name}: low-band corr {lb_corr:.4}"); |
| 2550 | 2561 | assert!( |
| @@ -2552,8 +2563,8 @@ fn hybrid_fb_roundtrip() { | ||
| 2552 | 2563 | "{name}: {kbps:.1} kbps for a 32 kbps CBR target" |
| 2553 | 2564 | ); |
| 2554 | 2565 | if let Some(oracle) = oracle_decode(&format!("hybrid-{name}"), &packets, pcm.len()) { |
| 2555 | - let n = oracle.len().min(decoded.len() - 120); | |
| 2556 | - let c = correlation(&oracle[..n], &decoded[120..120 + n]); | |
| 2566 | + let n = oracle.len().min(decoded.len() - delay); | |
| 2567 | + let c = correlation(&oracle[..n], &decoded[delay..delay + n]); | |
| 2557 | 2568 | eprintln!("hybrid FB {name}: oracle vs ours corr {c:.4}"); |
| 2558 | 2569 | assert!(c >= 0.95, "{name}: oracle corr {c:.4}"); |
| 2559 | 2570 | } |
| @@ -2661,13 +2672,20 @@ fn hybrid_shapes_roundtrip() { | ||
| 2661 | 2672 | assert!(worst >= floor, "{tag}: worst corr {worst:.4}"); |
| 2662 | 2673 | let total = pcm.len() / channels; |
| 2663 | 2674 | let path = temp_path(&format!("hybrid-{tag}.opus")); |
| 2675 | + // The pre-skip is what `hybrid_roundtrip_shape`'s own encoder | |
| 2676 | + // advertises for this frame size, not a constant. | |
| 2677 | + let pre_skip = { | |
| 2678 | + let mut e = Encoder::new(48000, channels, Application::Voip).unwrap(); | |
| 2679 | + e.set_bitrate(bps); | |
| 2680 | + e.look_ahead(frame) | |
| 2681 | + }; | |
| 2664 | 2682 | write_ogg_opus( |
| 2665 | 2683 | &path, |
| 2666 | 2684 | &packets, |
| 2667 | - opus_head(channels, 120, None), | |
| 2685 | + opus_head(channels, pre_skip as u16, None), | |
| 2668 | 2686 | channels, |
| 2669 | 2687 | total, |
| 2670 | - 120, | |
| 2688 | + pre_skip as i64, | |
| 2671 | 2689 | ); |
| 2672 | 2690 | if Command::new("ffmpeg").arg("-version").output().is_ok() { |
| 2673 | 2691 | let out = Command::new("ffmpeg") |