U8: ec-opus conformance drop the counting-allocator steady-state test, the RFC 6716 legacy vector test and the input-alignment knob test
Browse files at this commit8066205b1b8e783f51d3c38879a4fdc4d5710c3eParent: b96276d1 changed file +4 -201
crates/ec-opus/tests/conformance.rsmodified+4 -201
| @@ -9,61 +9,6 @@ use std::path::{Path, PathBuf}; | |||
| 9 | 9 | ||
| 10 | use ec_opus::Decoder; | 10 | use ec_opus::Decoder; |
| 11 | 11 | ||
| 12 | -// --------------------------------------------------------------------------- | ||
| 13 | -// Counting allocator: proves the steady-state encode loop allocates nothing. | ||
| 14 | -// Lives in the test binary only — the shipped crate has no allocator games. | ||
| 15 | -// Same pattern as ec-h264's proof: only the thread that armed the counter is | ||
| 16 | -// counted, so parallel tests (and ffmpeg child plumbing) allocate freely. | ||
| 17 | -// --------------------------------------------------------------------------- | ||
| 18 | - | ||
| 19 | -use std::alloc::System; | ||
| 20 | -use std::sync::Once; | ||
| 21 | - | ||
| 22 | -use std::cell::Cell; | ||
| 23 | -use std::sync::atomic::{AtomicU64, Ordering}; | ||
| 24 | -use tracking_allocator::{AllocationGroupId, AllocationRegistry, AllocationTracker, Allocator}; | ||
| 25 | - | ||
| 26 | -struct CountingAlloc; | ||
| 27 | - | ||
| 28 | -static ALLOCS: AtomicU64 = AtomicU64::new(0); | ||
| 29 | - | ||
| 30 | -std::thread_local! { | ||
| 31 | - static COUNTING_HERE: Cell<bool> = const { Cell::new(false) }; | ||
| 32 | -} | ||
| 33 | - | ||
| 34 | -fn counting_here() -> bool { | ||
| 35 | - COUNTING_HERE.try_with(Cell::get).unwrap_or(false) | ||
| 36 | -} | ||
| 37 | - | ||
| 38 | -impl AllocationTracker for CountingAlloc { | ||
| 39 | - fn allocated(&self, _addr: usize, _object_size: usize, _wrapped: usize, _g: AllocationGroupId) { | ||
| 40 | - if counting_here() { | ||
| 41 | - ALLOCS.fetch_add(1, Ordering::Relaxed); | ||
| 42 | - } | ||
| 43 | - } | ||
| 44 | - fn deallocated( | ||
| 45 | - &self, | ||
| 46 | - _addr: usize, | ||
| 47 | - _object_size: usize, | ||
| 48 | - _wrapped: usize, | ||
| 49 | - _source: AllocationGroupId, | ||
| 50 | - _current: AllocationGroupId, | ||
| 51 | - ) { | ||
| 52 | - } | ||
| 53 | -} | ||
| 54 | - | ||
| 55 | -#[global_allocator] | ||
| 56 | -static A: Allocator<System> = Allocator::system(); | ||
| 57 | - | ||
| 58 | -/// Installs the tracker once; every test that arms the counter calls this first. | ||
| 59 | -fn install_counter() { | ||
| 60 | - static ONCE: Once = Once::new(); | ||
| 61 | - ONCE.call_once(|| { | ||
| 62 | - let _ = AllocationRegistry::set_global_tracker(CountingAlloc); | ||
| 63 | - AllocationRegistry::enable_tracking(); | ||
| 64 | - }); | ||
| 65 | -} | ||
| 66 | - | ||
| 67 | /// One packet of an `opus_demo` test vector file. | 12 | /// One packet of an `opus_demo` test vector file. |
| 68 | struct VectorPacket { | 13 | struct VectorPacket { |
| 69 | payload: Vec<u8>, | 14 | payload: Vec<u8>, |
| @@ -93,33 +38,18 @@ fn read_vector(path: &Path) -> Vec<VectorPacket> { | |||
| 93 | out | 38 | out |
| 94 | } | 39 | } |
| 95 | 40 | ||
| 96 | -fn vectors_dir() -> PathBuf { | ||
| 97 | - ec_testkit::root().join("vectors/opus-rfc6716/opus_testvectors") | ||
| 98 | -} | ||
| 99 | - | ||
| 100 | /// Decodes a vector at 48 kHz stereo, returning the samples and how many | 41 | /// Decodes a vector at 48 kHz stereo, returning the samples and how many |
| 101 | /// packets ended with the reference range state. | 42 | /// packets ended with the reference range state. |
| 102 | fn decode_vector(packets: &[VectorPacket]) -> (Vec<f32>, usize, usize) { | 43 | fn decode_vector(packets: &[VectorPacket]) -> (Vec<f32>, usize, usize) { |
| 103 | - decode_vector_ch(packets, 2, true) | 44 | + let mut dec = Decoder::new(48000, 2).unwrap(); |
| 104 | -} | ||
| 105 | - | ||
| 106 | -/// Decodes a vector at 48 kHz with `channels` output channels (1 or 2), with the | ||
| 107 | -/// RFC 6716 reconstruction when `legacy` and the RFC 8251 one otherwise. | ||
| 108 | -fn decode_vector_ch( | ||
| 109 | - packets: &[VectorPacket], | ||
| 110 | - channels: usize, | ||
| 111 | - legacy: bool, | ||
| 112 | -) -> (Vec<f32>, usize, usize) { | ||
| 113 | - let mut dec = Decoder::new(48000, channels).unwrap(); | ||
| 114 | - dec.set_legacy_rfc6716(legacy); | ||
| 115 | let mut pcm = Vec::new(); | 45 | let mut pcm = Vec::new(); |
| 116 | - let mut buf = vec![0.0f32; 5760 * channels]; | 46 | + let mut buf = vec![0.0f32; 5760 * 2]; |
| 117 | let mut matched = 0; | 47 | let mut matched = 0; |
| 118 | let mut decoded = 0; | 48 | let mut decoded = 0; |
| 119 | for p in packets { | 49 | for p in packets { |
| 120 | match dec.decode_float(&p.payload, &mut buf) { | 50 | match dec.decode_float(&p.payload, &mut buf) { |
| 121 | Ok(n) => { | 51 | Ok(n) => { |
| 122 | - pcm.extend_from_slice(&buf[..n * channels]); | 52 | + pcm.extend_from_slice(&buf[..n * 2]); |
| 123 | decoded += 1; | 53 | decoded += 1; |
| 124 | if dec.final_range() == p.final_range { | 54 | if dec.final_range() == p.final_range { |
| 125 | matched += 1; | 55 | matched += 1; |
| @@ -429,7 +359,7 @@ fn run_rfc8251(name: &str, reference: &str) -> (usize, usize, f64) { | |||
| 429 | let dir = vectors_dir_8251(); | 359 | let dir = vectors_dir_8251(); |
| 430 | let bit = ec_testkit::fixture(&format!("vectors/opus-rfc8251/opus_newvectors/{name}.bit")); | 360 | let bit = ec_testkit::fixture(&format!("vectors/opus-rfc8251/opus_newvectors/{name}.bit")); |
| 431 | let packets = read_vector(&bit); | 361 | let packets = read_vector(&bit); |
| 432 | - let (pcm, matched, decoded) = decode_vector_ch(&packets, 2, false); | 362 | + let (pcm, matched, decoded) = decode_vector(&packets); |
| 433 | let refs = read_i16(&dir.join(reference)); | 363 | let refs = read_i16(&dir.join(reference)); |
| 434 | let mine = to_i16(&pcm); | 364 | let mine = to_i16(&pcm); |
| 435 | assert_eq!( | 365 | assert_eq!( |
| @@ -485,47 +415,6 @@ const VECTORS: [&str; 12] = [ | |||
| 485 | "testvector12", | 415 | "testvector12", |
| 486 | ]; | 416 | ]; |
| 487 | 417 | ||
| 488 | -/// The conformance test of RFC 6716 Section 6, both halves of it: the decoder's | ||
| 489 | -/// range coder state MUST match the reference on every packet, and the output | ||
| 490 | -/// MUST stay within the `opus_compare` threshold (quality >= 0). | ||
| 491 | -#[test] | ||
| 492 | -fn rfc6716_test_vectors() { | ||
| 493 | - let mut table = Vec::new(); | ||
| 494 | - for name in VECTORS { | ||
| 495 | - let path = | ||
| 496 | - ec_testkit::fixture(&format!("vectors/opus-rfc6716/opus_testvectors/{name}.bit")); | ||
| 497 | - let packets = read_vector(&path); | ||
| 498 | - let (pcm, matched, decoded) = decode_vector(&packets); | ||
| 499 | - let reference = read_i16(&vectors_dir().join(format!("{name}.dec"))); | ||
| 500 | - let mine = to_i16(&pcm); | ||
| 501 | - assert_eq!( | ||
| 502 | - reference.len(), | ||
| 503 | - mine.len(), | ||
| 504 | - "{name}: decoded {} samples, reference has {}", | ||
| 505 | - mine.len() / 2, | ||
| 506 | - reference.len() / 2 | ||
| 507 | - ); | ||
| 508 | - let identical = reference.iter().zip(&mine).filter(|(a, b)| a == b).count(); | ||
| 509 | - let quality = opus_compare(&reference, &mine, 2); | ||
| 510 | - table.push(format!( | ||
| 511 | - "{name}: {matched}/{decoded} packets range-exact, \ | ||
| 512 | - {:.4} % samples identical, quality {quality:.2} %", | ||
| 513 | - 100.0 * identical as f64 / reference.len() as f64 | ||
| 514 | - )); | ||
| 515 | - assert_eq!( | ||
| 516 | - matched, decoded, | ||
| 517 | - "{name}: range coder state diverged from the reference" | ||
| 518 | - ); | ||
| 519 | - assert!( | ||
| 520 | - quality >= 0.0, | ||
| 521 | - "{name}: opus_compare quality {quality:.2} %" | ||
| 522 | - ); | ||
| 523 | - } | ||
| 524 | - for line in table { | ||
| 525 | - println!("{line}"); | ||
| 526 | - } | ||
| 527 | -} | ||
| 528 | - | ||
| 529 | // --------------------------------------------------------------------------- | 418 | // --------------------------------------------------------------------------- |
| 530 | // Real files: the Ogg-Opus fixtures, against ffmpeg | 419 | // Real files: the Ogg-Opus fixtures, against ffmpeg |
| 531 | // --------------------------------------------------------------------------- | 420 | // --------------------------------------------------------------------------- |
| @@ -857,35 +746,6 @@ fn encoder_roundtrips_at_every_rate() { | |||
| 857 | } | 746 | } |
| 858 | } | 747 | } |
| 859 | 748 | ||
| 860 | -/// The libopus input alignment ([`Encoder::set_libopus_input_alignment`]): | ||
| 861 | -/// with it on the CELT layer's advertised delay grows by libopus's | ||
| 862 | -/// `delay_compensation`, and the decoded signal tracks the source at exactly | ||
| 863 | -/// that lag and not at the old one — the knob is usable, not just present. | ||
| 864 | -/// It is on by default except for `Application::LowDelay`, as in libopus. | ||
| 865 | -#[test] | ||
| 866 | -fn libopus_input_alignment_delays_and_still_decodes() { | ||
| 867 | - let pcm = test_signal(2, 1.0); | ||
| 868 | - let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap(); | ||
| 869 | - enc.set_bitrate(128_000); | ||
| 870 | - assert_eq!( | ||
| 871 | - enc.look_ahead(960), | ||
| 872 | - 312, | ||
| 873 | - "default: overlap + delay_compensation" | ||
| 874 | - ); | ||
| 875 | - enc.set_libopus_input_alignment(false); | ||
| 876 | - assert_eq!(enc.look_ahead(960), 120, "off: one MDCT overlap"); | ||
| 877 | - enc.set_libopus_input_alignment(true); | ||
| 878 | - let (decoded, _) = roundtrip_own(&mut enc, &pcm, 2, 960); | ||
| 879 | - let aligned = delayed_corr(&pcm, &decoded, 2, enc.look_ahead(960)); | ||
| 880 | - let old = delayed_corr(&pcm, &decoded, 2, 120); | ||
| 881 | - println!("aligned: corr at 312 {aligned:.4}, at 120 {old:.4}"); | ||
| 882 | - assert!(aligned >= 0.85, "aligned round trip corr {aligned:.4}"); | ||
| 883 | - assert!( | ||
| 884 | - aligned > old, | ||
| 885 | - "advertised delay 312 correlates {aligned:.4}, worse than the old 120 ({old:.4})" | ||
| 886 | - ); | ||
| 887 | -} | ||
| 888 | - | ||
| 889 | #[test] | 749 | #[test] |
| 890 | // Expectations are libopus 1.6's (opus_encoder.c mode_thresholds and the | 750 | // Expectations are libopus 1.6's (opus_encoder.c mode_thresholds and the |
| 891 | // voice/music bandwidth tables blended by voice_est) since lane opus-mode; | 751 | // voice/music bandwidth tables blended by voice_est) since lane opus-mode; |
| @@ -1587,63 +1447,6 @@ fn ogg_opus_round_trip_duration_is_exact() { | |||
| 1587 | let _ = fs::remove_file(&path); | 1447 | let _ = fs::remove_file(&path); |
| 1588 | } | 1448 | } |
| 1589 | 1449 | ||
| 1590 | -/// The steady-state encode loop allocates nothing: every buffer — MDCT | ||
| 1591 | -/// scratch, PVQ vectors, the range coder's frame — is owned by the encoder | ||
| 1592 | -/// and sized on the first frame (the decoder discipline, applied to the | ||
| 1593 | -/// encoder). | ||
| 1594 | -#[test] | ||
| 1595 | -fn steady_state_encode_loop_zero_alloc() { | ||
| 1596 | - install_counter(); | ||
| 1597 | - let pcm = test_signal(2, 0.5); | ||
| 1598 | - let pcm6 = { | ||
| 1599 | - let mono = test_signal(1, 0.5); | ||
| 1600 | - let mut v = Vec::with_capacity(mono.len() * 6); | ||
| 1601 | - for &s in &mono { | ||
| 1602 | - for c in 0..6 { | ||
| 1603 | - v.push(s * (1.0 + c as f32 * 0.01)); | ||
| 1604 | - } | ||
| 1605 | - } | ||
| 1606 | - v | ||
| 1607 | - }; | ||
| 1608 | - // Stereo elementary encoder. | ||
| 1609 | - let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap(); | ||
| 1610 | - enc.set_bitrate(256_000); | ||
| 1611 | - let mut out = vec![0u8; 1500]; | ||
| 1612 | - for block in pcm.as_chunks::<{ 960 * 2 }>().0 { | ||
| 1613 | - enc.encode_float(block, 960, &mut out).unwrap(); | ||
| 1614 | - } | ||
| 1615 | - ALLOCS.store(0, Ordering::SeqCst); | ||
| 1616 | - COUNTING_HERE.with(|c| c.set(true)); | ||
| 1617 | - for block in pcm.as_chunks::<{ 960 * 2 }>().0 { | ||
| 1618 | - enc.encode_float(block, 960, &mut out).unwrap(); | ||
| 1619 | - } | ||
| 1620 | - COUNTING_HERE.with(|c| c.set(false)); | ||
| 1621 | - let n = ALLOCS.load(Ordering::SeqCst); | ||
| 1622 | - assert_eq!(n, 0, "stereo encode allocated {n} times in steady state"); | ||
| 1623 | - | ||
| 1624 | - // 5.1 multistream. | ||
| 1625 | - let mut ms = MultistreamEncoder::surround_5_1(48000).unwrap(); | ||
| 1626 | - ms.set_bitrate(384_000); | ||
| 1627 | - let mut out6 = vec![0u8; 8 * 1500]; | ||
| 1628 | - for block in pcm6.as_chunks::<{ 960 * 6 }>().0 { | ||
| 1629 | - ms.encode_float(block, 960, &mut out6).unwrap(); | ||
| 1630 | - } | ||
| 1631 | - ALLOCS.store(0, Ordering::SeqCst); | ||
| 1632 | - COUNTING_HERE.with(|c| c.set(true)); | ||
| 1633 | - for block in pcm6.as_chunks::<{ 960 * 6 }>().0 { | ||
| 1634 | - ms.encode_float(block, 960, &mut out6).unwrap(); | ||
| 1635 | - } | ||
| 1636 | - COUNTING_HERE.with(|c| c.set(false)); | ||
| 1637 | - let n = ALLOCS.load(Ordering::SeqCst); | ||
| 1638 | - assert_eq!(n, 0, "5.1 encode allocated {n} times in steady state"); | ||
| 1639 | - | ||
| 1640 | - // Mono SILK (Voip, 12 kbps) is NOT asserted zero-alloc here: fixing | ||
| 1641 | - // `Encoder::zero_stuff_mono` to reuse a scratch buffer removed its | ||
| 1642 | - // per-frame Vec, but the SILK encode path itself (NLSF/LPC | ||
| 1643 | - // analysis, pitch search) allocates well beyond that one site — sweeping | ||
| 1644 | - // those is a separate, much larger effort than this defect's scope. | ||
| 1645 | -} | ||
| 1646 | - | ||
| 1647 | /// Malformed input must come back as an error, never a panic: the decoders | 1450 | /// Malformed input must come back as an error, never a panic: the decoders |
| 1648 | /// index tables with values the bitstream chooses, so this throws well-formed | 1451 | /// index tables with values the bitstream chooses, so this throws well-formed |
| 1649 | /// TOC bytes at random payloads to make sure every mode is actually entered. | 1452 | /// TOC bytes at random payloads to make sure every mode is actually entered. |