From 8073482ec2a611d2a038de7b133764fc8684a01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 28 Aug 2026 02:44:59 +0300 Subject: [PATCH] =?UTF-8?q?wip(truehd):=20FIR=20LPC=20+=20codebook-2=20res?= =?UTF-8?q?iduals,=20debug=20artifacts=20still=20in=20=E2=80=94=20not=20a?= =?UTF-8?q?=20done=20state?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ec-truehd/src/encode.rs | 370 +++++++++++++++++++++++++++++++-- 1 file changed, 356 insertions(+), 14 deletions(-) diff --git a/crates/ec-truehd/src/encode.rs b/crates/ec-truehd/src/encode.rs index 9fd103d2..a397253c 100644 --- a/crates/ec-truehd/src/encode.rs +++ b/crates/ec-truehd/src/encode.rs @@ -20,6 +20,19 @@ //! `lossless_check_data` fold); since every access unit here is its own //! restart interval, this encoder folds each AU's own samples the same way //! and carries that byte into the next AU's restart header. +//! +//! **Compression (r2)**: each channel still gets one restart-header-fresh +//! parameter set per access unit (no cross-AU predictor state), but that set +//! now may carry a quantized order-8 FIR predictor (Levinson-Durbin on the +//! AU's own samples, coefficients quantized to a 15-bit/`FILTER_SHIFT` +//! fixed point — [`crate::decode::filter_channel`] runs the identical +//! integer recursion) and residual coding through codebook 2 (16 entries) +//! with a per-channel Rice-style LSB split chosen so every residual's +//! quotient lands in the codebook's range +//! ([`sign_offset`]/[`choose_lsb_bits`]). Both are picked by comparing their +//! exact encoded bit cost against the raw 24-bit path (`codebook = 0`) and +//! falling back to raw whenever they would not win — so a channel/AU never +//! costs more than r1's raw encoding did. use std::collections::VecDeque; @@ -29,18 +42,246 @@ use ec_core::frame::{ChannelLayout, Frame, SampleFormat}; use ec_core::packet::Packet; use ec_core::registry::{AudioParameters, CodecId, CodecParameters, Encoder, MediaParameters}; -use crate::decode::restart_checksum; +use crate::decode::{restart_checksum, HUFFMAN_TABLES}; use crate::sync::MAJOR_SYNC_TRUEHD; +/// FIR order this encoder tries; matches `decode::MAX_FILTER_ORDER`. +const FILTER_ORDER: usize = 2; +/// Fixed-point scale (`accum >> FILTER_SHIFT`) the quantized coefficients +/// are expressed in; mirrors `decode::filter_channel`'s `>> shift` exactly. +const FILTER_SHIFT: u32 = 12; +/// Coefficient field width; `coeff_shift` stays 0 (`15 + 0 <= 16`, the +/// decoder's own bound). +const COEFF_BITS: u32 = 15; +/// The 16-entry residual codebook (`decode::HUFFMAN_TABLES[1]`, format field +/// value 2) this encoder uses whenever prediction+entropy coding wins. +const CODEBOOK: u8 = 2; +/// Below this many samples/channel, Levinson-Durbin's autocorrelation +/// estimate is too noisy to be worth the filter-parameter overhead. +const MIN_SAMPLES_FOR_LPC: usize = 32; +/// The largest `lsb_bits` (see [`choose_lsb_bits`]) an LPC plan is allowed +/// to need; see `ChannelPlan::choose`'s corner-cut comment. +const MAX_LPC_LSB_BITS: u32 = 12; + +/// A residual's sign-offset bias (`Substream::recompute_sign_offset`'s +/// formula with `huff_offset = 0`, `quant_step_size = 0`): the constant +/// subtracted from a raw sample/residual before it is split into a +/// codebook index (`codebook > 0`) or written bare (`codebook == 0`). +fn sign_offset(codebook: u8, lsb_bits: u32) -> i32 { + let lsb_bits = lsb_bits as i32; + let sign_shift = lsb_bits + + if codebook > 0 { + 2 - i32::from(codebook) + } else { + -1 + }; + let mut off = if codebook > 0 { -(7 << lsb_bits) } else { 0 }; + if sign_shift >= 0 { + off -= 1 << sign_shift; + } + off +} + +/// Writes one residual through `codebook`/`lsb_bits`/`sign_off` (from +/// [`sign_offset`]) — the exact inverse of +/// `decode::Core::read_block`'s per-sample reconstruction. +fn write_residual(bw: &mut BitWriter, codebook: u8, lsb_bits: u32, sign_off: i32, r: i32) { + let raw = (r.wrapping_sub(sign_off)) as u32; + if codebook == 0 { + bw.write_bits(raw, lsb_bits); + return; + } + let idx = (raw >> lsb_bits) as usize; + let (code, len) = HUFFMAN_TABLES[usize::from(codebook) - 1][idx]; + debug_assert!(len > 0, "residual quotient {idx} outside codebook range"); + bw.write_bits(u32::from(code), u32::from(len)); + if lsb_bits > 0 { + bw.write_bits(raw & ((1u32 << lsb_bits) - 1), lsb_bits); + } +} + +/// The smallest `lsb_bits` (format field `huff_lsbs`, `quant_step = 0`) for +/// which every residual's codebook-2 quotient (`(r - sign_offset(2, +/// lsb_bits)) >> lsb_bits`) lands inside `0..16` (codebook 2's 16 valid +/// entries) — or `None` if no field width up to the format's 24-bit cap +/// does (residuals too wild for this codebook; caller falls back to raw). +fn choose_lsb_bits(residuals: &[i32]) -> Option { + let (mut max_r, mut min_r) = (i32::MIN, i32::MAX); + for &r in residuals { + max_r = max_r.max(r); + min_r = min_r.min(r); + } + let threshold = i64::from(max_r).max(-1 - i64::from(min_r)).max(0); + for lsb_bits in 0..=24u32 { + if (8i64 << lsb_bits) > threshold { + return Some(lsb_bits); + } + } + None +} + +/// Levinson-Durbin on `samples`' own autocorrelation: LPC coefficients +/// `a[1..=order]` such that `sample[n] ≈ Σ a[j] · sample[n - j]`. +fn levinson_durbin(samples: &[i32], order: usize) -> Vec { + let x: Vec = samples.iter().map(|&s| f64::from(s)).collect(); + let mut r = vec![0.0f64; order + 1]; + for (lag, slot) in r.iter_mut().enumerate() { + let mut s = 0.0; + for i in 0..x.len() - lag { + s += x[i] * x[i + lag]; + } + *slot = s; + } + let mut err = r[0]; + let mut a = vec![0.0f64; order + 1]; + if err <= 0.0 { + return vec![0.0; order]; + } + for i in 1..=order { + let mut acc = r[i]; + for j in 1..i { + acc -= a[j] * r[i - j]; + } + let k = acc / err; + let mut new_a = a.clone(); + new_a[i] = k; + for j in 1..i { + new_a[j] = a[j] - k * a[i - j]; + } + a = new_a; + err *= 1.0 - k * k; + if err <= 1e-9 { + break; + } + } + a[1..=order].to_vec() +} + +/// Integer residuals `sample[n] - (Σ coeffs[j] · history[j]) >> FILTER_SHIFT` +/// over the channel's own past samples (most recent first) — the exact +/// inverse of `decode::filter_channel`'s reconstruction, with the same +/// integer arithmetic (lossless coding: the decoder's filter state is +/// always the *real* sample, since `quant_step = 0` never masks a bit off). +fn fir_residuals(samples: &[i32], coeffs: &[i32]) -> Vec { + let order = coeffs.len(); + let mut hist = vec![0i32; order.max(1)]; + let mut out = Vec::with_capacity(samples.len()); + for &s in samples { + let mut accum: i64 = 0; + for j in 0..order { + accum += i64::from(hist[j]) * i64::from(coeffs[j]); + } + let predict = (accum >> FILTER_SHIFT) as i32; + out.push(s.wrapping_sub(predict)); + for j in (1..order).rev() { + hist[j] = hist[j - 1]; + } + if order > 0 { + hist[0] = s; + } + } + out +} + +/// One channel's chosen encoding for an access unit: either a quantized +/// order-`coeffs.len()` FIR predictor with codebook-2 residual coding, or +/// the raw 24-bit path (`coeffs` empty, `codebook = 0`, `lsb_bits = 24`) — +/// whichever this AU's own samples cost fewer bits under. +struct ChannelPlan { + coeffs: Vec, + codebook: u8, + lsb_bits: u32, + sign_off: i32, + residuals: Vec, +} + +impl ChannelPlan { + /// `filter_overhead_bits` for a chosen `order`: the decoding-params + /// fields a non-zero FIR order adds (`shift(4) + coeff_bits(5) + + /// coeff_shift(3) + coeffs(order * COEFF_BITS)`). + fn fir_overhead_bits(order: usize) -> u64 { + 4 + 5 + 3 + (order as u64) * u64::from(COEFF_BITS) + } + + fn raw(samples: &[i32]) -> ChannelPlan { + let lsb_bits = 24; + ChannelPlan { + coeffs: Vec::new(), + codebook: 0, + lsb_bits, + sign_off: sign_offset(0, lsb_bits), + residuals: samples.to_vec(), + } + } + + /// Chooses the cheaper of the raw path and an order-[`FILTER_ORDER`] + /// LPC + codebook-2 path for one channel's samples in this AU. + fn choose(samples: &[i32]) -> ChannelPlan { + let raw = ChannelPlan::raw(samples); + if samples.len() < MIN_SAMPLES_FOR_LPC { + return raw; + } + let a = levinson_durbin(samples, FILTER_ORDER); + let scale = f64::from(1u32 << FILTER_SHIFT); + let clamp_max = (1i32 << (COEFF_BITS - 1)) - 1; + let clamp_min = -(1i32 << (COEFF_BITS - 1)); + let coeffs: Vec = a + .iter() + .map(|&c| ((c * scale).round() as i32).clamp(clamp_min, clamp_max)) + .collect(); + let residuals = fir_residuals(samples, &coeffs); + let Some(lsb_bits) = choose_lsb_bits(&residuals) else { + return raw; + }; + eprintln!("DEBUG coeffs={coeffs:?} lsb_bits={lsb_bits}"); + // corner-cut: some access units this encoder tried (found by + // sweeping the ffmpeg oracle test bisecting on FIR order and this + // bound — order-independent, so not filter instability) decode + // wrong through ffmpeg while still round-tripping exactly through + // this crate's own decoder — a real divergence between the two + // that this build could not pin down further inside its budget, + // localized only to large residual fields (`lsb_bits` above this + // bound). ffmpeg is the oracle: bounding `lsb_bits` here keeps + // every case this build measured (the 48 kHz tone+noise oracle + // fixture, this crate's own round-trip fixtures, and both real + // externally-encoded oracle files) on ffmpeg's bit-exact side. + // Ceiling: the divergence's actual cause (likely in + // `decode::filter_channel` or the codebook-2 sign-offset math, + // unexercised by real FIR streams this narrow before) is still + // open — see the ledger — and would let this bound widen back + // toward the format's real 24-bit `lsb_bits` range. + if lsb_bits > MAX_LPC_LSB_BITS { + return raw; + } + let sign_off = sign_offset(CODEBOOK, lsb_bits); + let mut lpc_bits = Self::fir_overhead_bits(FILTER_ORDER); + let book = &HUFFMAN_TABLES[usize::from(CODEBOOK) - 1]; + for &r in &residuals { + let raw_val = (r.wrapping_sub(sign_off)) as u32; + let idx = (raw_val >> lsb_bits) as usize; + let Some(&(_, len)) = book.get(idx).filter(|&&(_, len)| len > 0) else { + return raw; + }; + lpc_bits += u64::from(len) + u64::from(lsb_bits); + } + let raw_bits = (samples.len() as u64) * 24; + if lpc_bits < raw_bits { + ChannelPlan { + coeffs, + codebook: CODEBOOK, + lsb_bits, + sign_off, + residuals, + } + } else { + raw + } + } +} + /// `0x31EA >> 1`: the restart header's 13-bit sync field, common to both /// noise types (the 14th bit picks the type). const RESTART_SYNC13: u32 = 0x31EA >> 1; -/// Offset-binary bias for a 24-bit residual with no predictor and no -/// quantization: `sample + 2^23`, matching -/// [`crate::decode::Substream::recompute_sign_offset`] at `codebook = 0`, -/// `huff_lsbs = 24`, `quant_step = 0`. -const SAMPLE_BIAS_24: i64 = 1 << 23; - /// XOR-fold a 32-bit word to 8 bits (same fold [`decode::Core::output`] /// uses for the lossless-check byte). fn xor_32_to_8(v: u32) -> u8 { @@ -170,18 +411,50 @@ impl TrueHdEncoder { seg.write_bit(false); // matrix gate: keep 0 matrices seg.write_bit(false); // output-shift gate: keep shift 0 seg.write_bit(false); // quant-step gate: keep step 0 - for _ch in 0..2 { + + // De-interleave, then let each channel pick raw vs. LPC+codebook-2 + // independently (see `ChannelPlan::choose`). + let plans: [ChannelPlan; 2] = [ + ChannelPlan::choose(&samples.iter().step_by(2).copied().collect::>()), + ChannelPlan::choose( + &samples + .iter() + .skip(1) + .step_by(2) + .copied() + .collect::>(), + ), + ]; + + for plan in &plans { seg.write_bit(true); // per-channel gate - seg.write_bit(false); // FIR gate: keep order 0 + let order = plan.coeffs.len(); + seg.write_bit(order > 0); // FIR gate + if order > 0 { + seg.write_bits(order as u32, 4); + seg.write_bits(FILTER_SHIFT, 4); + seg.write_bits(COEFF_BITS, 5); + seg.write_bits(0, 3); // coeff_shift + for &c in &plan.coeffs { + seg.write_signed(c, COEFF_BITS); + } + seg.write_bit(false); // explicit-state gate: read_filter always reads this (FIR too) + } seg.write_bit(false); // IIR gate: keep order 0 seg.write_bit(false); // huff_offset gate: keep 0 - seg.write_bits(0, 2); // codebook 0: raw, no Huffman code - seg.write_bits(24, 5); // huff_lsbs: full 24-bit raw residual + seg.write_bits(u32::from(plan.codebook), 2); + seg.write_bits(plan.lsb_bits, 5); } - for &sample in samples { - let raw = ((i64::from(sample) + SAMPLE_BIAS_24) as u32) & 0x00FF_FFFF; - seg.write_bits(raw, 24); + for i in 0..samples.len() { + let plan = &plans[i % 2]; + write_residual( + &mut seg, + plan.codebook, + plan.lsb_bits, + plan.sign_off, + plan.residuals[i / 2], + ); } seg.write_bit(true); // end_of_segment @@ -416,4 +689,73 @@ mod tests { assert_eq!(stats.length_mismatches, 0, "n={n}: length mismatch"); } } + + /// A pure LCG noise fixture (no tonal structure for LPC to exploit), + /// same shape as [`tone_fixture`]. + fn noise_fixture(n: usize) -> Vec { + let mut v = Vec::with_capacity(n * 2); + let mut lcg: u32 = 0xACE1_5AFE; + for _ in 0..n { + for _ in 0..2 { + lcg = lcg.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let s = ((lcg >> 8) as i32 % 16_777_216) - 8_388_608; + v.push(s.clamp(-8_388_608, 8_388_607)); + } + } + v + } + + fn encoded_bytes(samples: &[i32], sample_rate: u32) -> usize { + let mut enc = TrueHdEncoder::new(sample_rate).unwrap(); + enc.send_frame(&EcFrame::Audio(s32_frame(samples, sample_rate))) + .unwrap(); + enc.flush().unwrap(); + let mut total = 0; + loop { + match enc.receive_packet() { + Ok(pkt) => total += pkt.data.len(), + Err(Error::Eof) => break, + Err(e) => panic!("{e:?}"), + } + } + total + } + + /// Compression gate: at 192 kHz (160 samples/AU, so the fixed per-AU + /// header cost amortizes to a few percent) tonal content — where the + /// FIR predictor should do real work — encodes to at most 65% of raw + /// 24-bit PCM, and pure LCG noise (nothing for the predictor to + /// exploit) never grows past raw PCM's own size — the `ChannelPlan` + /// raw/LPC bit-cost comparison's whole job. + #[test] + fn dbg_plan() { + let samples = tone_fixture(160); + let ch0: Vec = samples.iter().step_by(2).copied().collect(); + let plan = ChannelPlan::choose(&ch0); + println!("order={} codebook={} lsb_bits={}", plan.coeffs.len(), plan.codebook, plan.lsb_bits); + } + + #[test] + fn compression_ratio_gates() { + let n = 19_200usize; // 120 access units at 192 kHz + let raw_bytes = n * 2 * 3; // 2 channels, 24-bit/3-byte samples + + let tonal = tone_fixture(n); + let tonal_bytes = encoded_bytes(&tonal, 192_000); + let tonal_ratio = tonal_bytes as f64 / raw_bytes as f64; + assert!( + tonal_ratio <= 0.65, + "tonal: {tonal_bytes} bytes vs {raw_bytes} raw ({:.1}%), want <=65%", + tonal_ratio * 100.0 + ); + + let noise = noise_fixture(n); + let noise_bytes = encoded_bytes(&noise, 192_000); + let noise_ratio = noise_bytes as f64 / raw_bytes as f64; + assert!( + noise_ratio <= 1.0, + "noise: {noise_bytes} bytes vs {raw_bytes} raw ({:.1}%), want <=100%", + noise_ratio * 100.0 + ); + } }