From d8fab2069fb0c288d69ab2b2e48b7c5f9e98370f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 21 Aug 2026 13:29:09 +0300 Subject: [PATCH] feat(ec-opus): encode multiframe silk --- crates/ec-opus/src/silk_enc_write.rs | 335 ++++++++++++++++++--------- crates/ec-opus/tests/conformance.rs | 70 +++++- 2 files changed, 286 insertions(+), 119 deletions(-) diff --git a/crates/ec-opus/src/silk_enc_write.rs b/crates/ec-opus/src/silk_enc_write.rs index 4f03aac6..4b820008 100644 --- a/crates/ec-opus/src/silk_enc_write.rs +++ b/crates/ec-opus/src/silk_enc_write.rs @@ -12,8 +12,7 @@ //! output error is fed back through short-term (bandwidth-expanded LPC), //! harmonic and tilt shaping filters so the noise hides under the signal's //! own spectrum, and a gain-offset rate loop sizes each packet against -//! [`SilkEncoder::set_bitrate`]. 40/60 ms packets and stereo are not -//! written yet. +//! [`SilkEncoder::set_bitrate`]. Stereo is not written yet. // Same idiom as silk.rs: index loops mirror the decoder's position arithmetic. #![allow(clippy::needless_range_loop)] @@ -27,7 +26,7 @@ use crate::silk::{ nlsf2a, silk_rand, }; use crate::silk_enc::{ - Resampler48, Vad, estimate_pitch, lpc_analyze, quantize_gains, quantize_nlsf, + NlsfQuant, Resampler48, Vad, estimate_pitch, lpc_analyze, quantize_gains, quantize_nlsf, }; const SHELL_LEN: usize = 16; @@ -140,27 +139,66 @@ impl SilkEncoder { self.encode_frame_ms(pcm48, out, 20) } - /// Encodes 10 or 20 ms of mono 48 kHz samples as one code-0 packet in - /// `out`, returning its length. + /// Encodes 10, 20, 40 or 60 ms of mono 48 kHz samples as one code-0 + /// packet in `out`, returning its length. pub fn encode_frame_ms( &mut self, pcm48: &[f32], out: &mut [u8], frame_ms: usize, ) -> Result { - let Trial { bytes, range, .. } = self.encode_inner(pcm48, frame_ms, None)?; - let len = bytes.len(); - self.final_range = range; - let duration = match frame_ms { - 10 => 0u8, - 20 => 1, + let frames_per_packet = match frame_ms { + 10 | 20 => 1usize, + 40 => 2, + 60 => 3, _ => { return Err(ec_core::Error::unsupported( format!("silk encode frame of {frame_ms} ms"), - "SILK encoder writes 10 or 20 ms packets", + "SILK encoder writes 10, 20, 40 or 60 ms packets", )); } }; + assert_eq!( + pcm48.len(), + 48 * frame_ms, + "SilkEncoder input is 48 kHz mono" + ); + let mut frames = Vec::with_capacity(frames_per_packet); + let sub_ms = if frame_ms == 10 { 10 } else { 20 }; + let mut prev_signal_type = 0; + for i in 0..frames_per_packet { + let from = i * sub_ms * 48; + let to = from + sub_ms * 48; + let conditional = i > 0; + let trial = self.encode_inner( + &pcm48[from..to], + sub_ms, + None, + conditional, + prev_signal_type, + )?; + prev_signal_type = trial.signal_type; + frames.push(trial); + } + self.range.reset(1275); + let refs: Vec<&Trial> = frames.iter().collect(); + write_packet_header(&mut self.range, &refs); + prev_signal_type = 0; + for (i, frame) in frames.iter().enumerate() { + write_frame_data(&mut self.range, frame, i > 0, prev_signal_type); + prev_signal_type = frame.signal_type; + } + self.range.done(); + debug_assert!(!self.range.error()); + let len = self.range.range_bytes(); + self.final_range = self.range.range(); + let duration = match frame_ms { + 10 => 0u8, + 20 => 1, + 40 => 2, + 60 => 3, + _ => unreachable!(), + }; let base = match self.fs_khz { 8 => 0u8, 12 => 4, @@ -175,7 +213,7 @@ impl SilkEncoder { ))); } out[0] = toc; - out[1..1 + len].copy_from_slice(&bytes); + out[1..1 + len].copy_from_slice(&self.range.data()[..len]); self.replay_ms(&out[1..1 + len], frame_ms)?; debug_assert_eq!(self.mirror_range, self.final_range); Ok(1 + len) @@ -187,7 +225,8 @@ impl SilkEncoder { /// must [`SilkEncoder::replay`] the finished payload so this encoder's /// decoder mirror advances. pub fn encode_hybrid(&mut self, pcm48: &[f32], enc: &mut RangeEncoder) -> Result<()> { - self.encode_inner(pcm48, 20, Some(enc)).map(|_| ()) + self.encode_inner(pcm48, 20, Some(enc), false, 0) + .map(|_| ()) } /// Decodes `payload` (TOC stripped) through the mirror decoder so the next @@ -201,14 +240,27 @@ impl SilkEncoder { fn replay_ms(&mut self, payload: &[u8], frame_ms: usize) -> Result<()> { let fs = self.fs_khz; let mut dec = RangeDecoder::new(payload); - self.mirror.decode( - &mut dec, - &mut self.mirror_out, - frame_ms, - fs as u32 * 1000, - 1, - true, - )?; + let total = frame_ms * fs; + if self.mirror_out.len() < total { + self.mirror_out.resize(total, 0); + } + let mut done = 0usize; + let mut first = true; + while done < total { + let n = self.mirror.decode( + &mut dec, + &mut self.mirror_out[done..], + frame_ms, + fs as u32 * 1000, + 1, + first, + )?; + if n == 0 { + break; + } + done += n; + first = false; + } self.mirror_range = dec.range(); Ok(()) } @@ -222,6 +274,8 @@ impl SilkEncoder { pcm48: &[f32], frame_ms: usize, shared: Option<&mut RangeEncoder>, + conditional: bool, + prev_signal_type: i32, ) -> Result { assert!( matches!(frame_ms, 10 | 20), @@ -516,85 +570,7 @@ impl SilkEncoder { } }; - // Bitstream writer, in decode()'s order; shared by the sizing trials - // and the hybrid path's shared coder. - let write = |enc: &mut RangeEncoder, gain_idx: &[i8], per_index: usize, run: &Run| { - enc.enc_bit_logp(vad_flag, 1); // VAD flag - enc.enc_bit_logp(false, 1); // LBRR flag - // Frame type. - if vad_flag { - enc.enc_icdf( - ((signal_type << 1) | quant_offset_type as i32) as usize - 2, - &TYPE_OFFSET_VAD_ICDF, - 8, - ); - } else { - enc.enc_icdf(quant_offset_type, &TYPE_OFFSET_NO_VAD_ICDF, 8); - } - // Gains: one frame per packet, so always independently coded. - enc.enc_icdf( - (gain_idx[0] as usize) >> 3, - &GAIN_ICDF[signal_type as usize], - 8, - ); - enc.enc_icdf((gain_idx[0] as usize) & 7, &UNIFORM8_ICDF, 8); - for k in 1..nb_subfr { - enc.enc_icdf(gain_idx[k] as usize, &DELTA_GAIN_ICDF, 8); - } - // NLSFs. - let cb = nlsf_cb(wb); - let stage1_icdf = &cb.cb1_icdf[(signal_type as usize >> 1) * cb.n_vectors..]; - enc.enc_icdf(nq.indices[0] as usize, stage1_icdf, 8); - let (ec_ix, _) = crate::silk::nlsf_unpack(cb, nq.indices[0] as usize); - for i in 0..order { - let q = nq.indices[i + 1] as i32; - let icdf = &cb.ec_icdf[ec_ix[i] as usize..]; - if q <= -4 { - enc.enc_icdf(0, icdf, 8); - enc.enc_icdf((-4 - q) as usize, &NLSF_EXT_ICDF, 8); - } else if q >= 4 { - enc.enc_icdf(8, icdf, 8); - enc.enc_icdf((q - 4) as usize, &NLSF_EXT_ICDF, 8); - } else { - enc.enc_icdf((q + 4) as usize, icdf, 8); - } - } - if nb_subfr == MAX_NB_SUBFR { - enc.enc_icdf(nq.interp_index as usize, &NLSF_INTERPOLATION_FACTOR_ICDF, 8); - } - // Pitch + LTP. (One frame per packet: the decoder only takes the - // lag-delta code path for the 2nd+ frame of a packet, so the - // absolute lag is always coded here.) - if voiced { - let half = fs as i32 >> 1; - enc.enc_icdf((lag_index / half) as usize, &PITCH_LAG_ICDF, 8); - let low_icdf: &[u8] = match fs { - 16 => &UNIFORM8_ICDF, - 12 => &UNIFORM6_ICDF, - _ => &UNIFORM4_ICDF, - }; - enc.enc_icdf((lag_index % half) as usize, low_icdf, 8); - let contour_icdf: &[u8] = match (fs, nb_subfr) { - (8, 4) => &PITCH_CONTOUR_NB_ICDF, - (8, _) => &PITCH_CONTOUR_10_MS_NB_ICDF, - (_, 4) => &PITCH_CONTOUR_ICDF, - _ => &PITCH_CONTOUR_10_MS_ICDF, - }; - enc.enc_icdf(contour_index as usize, contour_icdf, 8); - enc.enc_icdf(per_index, <P_PER_INDEX_ICDF, 8); - let gain_icdf: &[u8] = match per_index { - 0 => <P_GAIN_ICDF_0, - 1 => <P_GAIN_ICDF_1, - _ => <P_GAIN_ICDF_2, - }; - for k in 0..nb_subfr { - enc.enc_icdf(run.ltp_index[k], gain_icdf, 8); - } - enc.enc_icdf(ltp_scale_index, <PSCALE_ICDF, 8); - } - enc.enc_icdf(seed_index as usize, &UNIFORM4_ICDF, 8); - encode_pulses(enc, &run.pulses, signal_type, quant_offset_type as i32); - }; + let fs = self.fs_khz; // One full trial at a common gain offset (in quantiser steps): returns // the coded packet body so the rate loop can read its exact size. let candidates: Vec = if voiced { vec![0, 1, 2] } else { vec![0] }; @@ -605,28 +581,45 @@ impl SilkEncoder { *v = ((*v as f64 * scale as f64).round() as i64).clamp(1, i32::MAX as i64) as i32; } let mut prev_gain_index = self.prev_gain_index; - let (gain_idx, gains_q16) = quantize_gains(&t, &mut prev_gain_index, false, nb_subfr); + let (gain_idx, gains_q16) = + quantize_gains(&t, &mut prev_gain_index, conditional, nb_subfr); let (per_index, run) = candidates .iter() .map(|&per| (per, run(per, &gains_q16))) .min_by(|a, b| a.1.err.partial_cmp(&b.1.err).unwrap()) .unwrap(); - // --- Bitstream, in decode()'s order. --- + let mut trial = Trial { + bytes: Vec::new(), + range: 0, + prev_gain_index, + gain_idx, + per_index, + run, + vad_flag, + signal_type, + quant_offset_type, + wb, + order, + nb_subfr, + fs, + lag_index, + contour_index, + voiced, + ltp_scale_index, + nq: nq.clone(), + seed_index, + }; let enc = &mut self.range; enc.reset(1275); - write(enc, &gain_idx, per_index, &run); + write_packet_header(enc, &[&trial]); + write_frame_data(enc, &trial, conditional, prev_signal_type); enc.done(); debug_assert!(!enc.error()); let len = enc.range_bytes(); - Trial { - bytes: enc.data()[..len].to_vec(), - range: enc.range(), - prev_gain_index, - gain_idx, - per_index, - run, - } + trial.bytes = enc.data()[..len].to_vec(); + trial.range = enc.range(); + trial }; // Rate control: a common gain offset moves every subframe's pulse @@ -664,7 +657,9 @@ impl SilkEncoder { ); } if let Some(enc) = shared { - write(enc, &best.gain_idx, best.per_index, &best.run); + let trial_ref = &best; + write_packet_header(enc, &[trial_ref]); + write_frame_data(enc, trial_ref, conditional, prev_signal_type); } self.prev_gain_index = best.prev_gain_index; self.voiced_frames += usize::from(voiced); @@ -685,7 +680,6 @@ struct Run { gq: Vec, } -/// One coded candidate of a frame at a given gain offset. struct Trial { bytes: Vec, range: u32, @@ -693,6 +687,119 @@ struct Trial { gain_idx: [i8; MAX_NB_SUBFR], per_index: usize, run: Run, + vad_flag: bool, + signal_type: i32, + quant_offset_type: usize, + wb: bool, + order: usize, + nb_subfr: usize, + fs: usize, + lag_index: i32, + contour_index: i32, + voiced: bool, + ltp_scale_index: usize, + nq: NlsfQuant, + seed_index: i32, +} + +fn write_packet_header(enc: &mut RangeEncoder, frames: &[&Trial]) { + for frame in frames { + enc.enc_bit_logp(frame.vad_flag, 1); + } + enc.enc_bit_logp(false, 1); +} + +fn write_frame_data( + enc: &mut RangeEncoder, + frame: &Trial, + conditional: bool, + prev_signal_type: i32, +) { + if frame.vad_flag { + enc.enc_icdf( + ((frame.signal_type << 1) | frame.quant_offset_type as i32) as usize - 2, + &TYPE_OFFSET_VAD_ICDF, + 8, + ); + } else { + enc.enc_icdf(frame.quant_offset_type, &TYPE_OFFSET_NO_VAD_ICDF, 8); + } + if conditional { + enc.enc_icdf(frame.gain_idx[0] as usize, &DELTA_GAIN_ICDF, 8); + } else { + enc.enc_icdf( + (frame.gain_idx[0] as usize) >> 3, + &GAIN_ICDF[frame.signal_type as usize], + 8, + ); + enc.enc_icdf((frame.gain_idx[0] as usize) & 7, &UNIFORM8_ICDF, 8); + } + for k in 1..frame.nb_subfr { + enc.enc_icdf(frame.gain_idx[k] as usize, &DELTA_GAIN_ICDF, 8); + } + let cb = nlsf_cb(frame.wb); + let stage1_icdf = &cb.cb1_icdf[(frame.signal_type as usize >> 1) * cb.n_vectors..]; + enc.enc_icdf(frame.nq.indices[0] as usize, stage1_icdf, 8); + let (ec_ix, _) = crate::silk::nlsf_unpack(cb, frame.nq.indices[0] as usize); + for i in 0..frame.order { + let q = frame.nq.indices[i + 1] as i32; + let icdf = &cb.ec_icdf[ec_ix[i] as usize..]; + if q <= -4 { + enc.enc_icdf(0, icdf, 8); + enc.enc_icdf((-4 - q) as usize, &NLSF_EXT_ICDF, 8); + } else if q >= 4 { + enc.enc_icdf(8, icdf, 8); + enc.enc_icdf((q - 4) as usize, &NLSF_EXT_ICDF, 8); + } else { + enc.enc_icdf((q + 4) as usize, icdf, 8); + } + } + if frame.nb_subfr == MAX_NB_SUBFR { + enc.enc_icdf( + frame.nq.interp_index as usize, + &NLSF_INTERPOLATION_FACTOR_ICDF, + 8, + ); + } + if frame.voiced { + if conditional && prev_signal_type == 2 { + enc.enc_icdf(0, &PITCH_DELTA_ICDF, 8); + } + let half = frame.fs as i32 >> 1; + enc.enc_icdf((frame.lag_index / half) as usize, &PITCH_LAG_ICDF, 8); + let low_icdf: &[u8] = match frame.fs { + 16 => &UNIFORM8_ICDF, + 12 => &UNIFORM6_ICDF, + _ => &UNIFORM4_ICDF, + }; + enc.enc_icdf((frame.lag_index % half) as usize, low_icdf, 8); + let contour_icdf: &[u8] = match (frame.fs, frame.nb_subfr) { + (8, 4) => &PITCH_CONTOUR_NB_ICDF, + (8, _) => &PITCH_CONTOUR_10_MS_NB_ICDF, + (_, 4) => &PITCH_CONTOUR_ICDF, + _ => &PITCH_CONTOUR_10_MS_ICDF, + }; + enc.enc_icdf(frame.contour_index as usize, contour_icdf, 8); + enc.enc_icdf(frame.per_index, <P_PER_INDEX_ICDF, 8); + let gain_icdf: &[u8] = match frame.per_index { + 0 => <P_GAIN_ICDF_0, + 1 => <P_GAIN_ICDF_1, + _ => <P_GAIN_ICDF_2, + }; + for k in 0..frame.nb_subfr { + enc.enc_icdf(frame.run.ltp_index[k], gain_icdf, 8); + } + if !conditional { + enc.enc_icdf(frame.ltp_scale_index, <PSCALE_ICDF, 8); + } + } + enc.enc_icdf(frame.seed_index as usize, &UNIFORM4_ICDF, 8); + encode_pulses( + enc, + &frame.run.pulses, + frame.signal_type, + frame.quant_offset_type as i32, + ); } /// Bits an ICDF symbol costs, for choosing among equivalent codings. diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs index 5cd3efdd..55e22b95 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -972,9 +972,9 @@ fn opus_head(channels: usize, pre_skip: u16, layout: Option<(usize, usize, &[u8] ec_opus::ogg::opus_head(channels as u8, pre_skip, 48000, 0, mapping) } -/// Muxes 20 ms packets into an Ogg-Opus file whose final granule trims the -/// stream to exactly `total_samples` — RFC 7845 end-trimming plus the -/// pre-skip accounting. +/// Muxes packets into an Ogg-Opus file whose final granule trims the stream +/// to exactly `total_samples` — RFC 7845 end-trimming plus the pre-skip +/// accounting. fn write_ogg_opus( path: &Path, packets: &[Vec], @@ -996,16 +996,17 @@ fn write_ogg_opus( let mut pts = 0i64; for (idx, p) in packets.iter().enumerate() { let last = idx == packets.len() - 1; + let packet_dur = ec_opus::Packet::parse(p, false).unwrap().samples_48k() as i64; let dur = if last { total_samples as i64 + pre_skip - pts } else { - 960 + packet_dur }; let pkt = CorePacket::new(0, tb, p.clone()) .with_pts(pts) .with_duration(dur); mux.write_packet(&pkt).unwrap(); - pts += 960; + pts += packet_dur; } mux.finish().unwrap(); } @@ -1739,6 +1740,65 @@ fn silk_mediumband_and_10ms_roundtrip() { } } +#[test] +fn silk_multiframe_packets_roundtrip() { + let full = speech_like(); + let pcm = &full[..46_080]; + for (tag, enc, frame_ms, config, cutoff, bps) in [ + ( + "NB40", + SilkEncoder::new(false), + 40usize, + 2u8, + 3500.0, + 16_000u32, + ), + ("MB40", SilkEncoder::new_mediumband(), 40, 6, 5500.0, 20_000), + ("WB40", SilkEncoder::new(true), 40, 10, 7000.0, 24_000), + ("WB60", SilkEncoder::new(true), 60, 11, 7000.0, 24_000), + ] { + let (decoded, packets, voiced) = silk_roundtrip_ms(enc, pcm, frame_ms, Some(bps)); + let toc = ec_opus::Toc::new(packets[0][0]); + assert_eq!(toc.config, config, "{tag}: wrong TOC config"); + assert_eq!(toc.code, 0, "{tag}: multiframe SILK uses one Opus frame"); + let reference = lowpass(pcm, cutoff); + let (corr, lag) = aligned_corr(&reference, &decoded, 2500); + let bytes: usize = packets.iter().map(Vec::len).sum(); + let kbps = bytes as f64 * 8.0 / (packets.len() as f64 * frame_ms as f64); + eprintln!( + "silk {tag}: corr {corr:.4} at lag {lag}, voiced {voiced}/{} frames, {kbps:.2} kbps", + packets.len() + ); + assert!(corr >= 0.70, "{tag}: corr {corr:.4}"); + assert!(voiced > 0, "{tag}: no frame coded with LTP"); + if let Some(oracle) = oracle_decode(&format!("silk-{tag}"), &packets, pcm.len()) { + let (c, _) = aligned_corr(&oracle, &decoded, 2500); + assert!( + c >= 0.99, + "{tag}: internal decode vs external decode corr {c:.4}" + ); + } + if Command::new("gst-launch-1.0") + .arg("--version") + .output() + .is_ok() + { + let path = temp_path(&format!("silk-{tag}-gst.opus")); + write_ogg_opus(&path, &packets, opus_head(1, 120, None), 1, pcm.len(), 120); + let location = format!("location={}", path.display()); + let out = Command::new("gst-launch-1.0") + .args([ + "-q", "filesrc", &location, "!", "oggdemux", "!", "opusdec", "!", "fakesink", + ]) + .output() + .unwrap(); + let stderr = String::from_utf8_lossy(&out.stderr); + assert!(out.status.success(), "{tag}: gst decode: {stderr}"); + let _ = fs::remove_file(&path); + } + } +} + #[test] fn silk_compares_to_celt_on_speech_at_speech_rates() { let pcm = speech_like();