forge
Explore
Log in

Don't you dare go Hollow

wip(opus-silkq/r1): per-frame SILK diag hook (last_silk_diag)

Browse files at this commit

9aa3c42c53a403260431d174b92b85305055c0d9Ahmet Kaan GümüşParent: c2b907e
crates/ec-opus/src/encoder.rsmodified+10 -0
@@ -254,6 +254,16 @@ impl Encoder {
254 self.celt.last_diag()254 self.celt.last_diag()
255 }255 }
256 256
257+ /// Per-frame SILK diagnostics from the last SILK-encoded frame (mono).
258+ /// Returns `None` if no mono SILK encoder has been initialised.
259+ pub fn last_silk_diag(&self) -> Option<&crate::silk_enc_write::SilkFrameDiag> {
260+ self.silk_mb
261+ .as_ref()
262+ .or(self.silk_wb.as_ref())
263+ .or(self.silk_nb.as_ref())
264+ .map(|s| s.last_diag())
265+ }
266+
257 /// Drops all inter-frame state; the next packet starts like the first.267 /// Drops all inter-frame state; the next packet starts like the first.
258 pub fn reset(&mut self) {268 pub fn reset(&mut self) {
259 self.celt.reset();269 self.celt.reset();
crates/ec-opus/src/lib.rsmodified+1 -0
@@ -76,6 +76,7 @@ pub mod silk_enc_write;
76 pub use celt::CeltDecoder;76 pub use celt::CeltDecoder;
77 pub use celt_enc::CeltFrameDiag;77 pub use celt_enc::CeltFrameDiag;
78 pub use encoder::{Application, Encoder};78 pub use encoder::{Application, Encoder};
79+pub use silk_enc_write::SilkFrameDiag;
79 pub use multistream::MultistreamDecoder;80 pub use multistream::MultistreamDecoder;
80 pub use multistream_enc::MultistreamEncoder;81 pub use multistream_enc::MultistreamEncoder;
81 pub use packet::{Bandwidth, Mode, Packet, Toc};82 pub use packet::{Bandwidth, Mode, Packet, Toc};
crates/ec-opus/src/silk_enc_write.rsmodified+42 -0
@@ -38,6 +38,21 @@ const LEVEL_ADJUST: f32 = 80.0 / 1024.0;
38 /// with 16 such pulses in one block.38 /// with 16 such pulses in one block.
39 const MAX_PULSE: i32 = 1023;39 const MAX_PULSE: i32 = 1023;
40 40
41+/// Per-frame SILK diagnostics captured at the end of the last `encode_inner`
42+/// call, for the speech-quality diagnostic lane.
43+#[derive(Clone, Debug, Default)]
44+pub struct SilkFrameDiag {
45+ pub voiced: bool,
46+ pub signal_type: i32,
47+ pub gain_idx: [i8; MAX_NB_SUBFR],
48+ pub nb_subfr: usize,
49+ pub lag_index: i32,
50+ pub pitch_l: [i32; MAX_NB_SUBFR],
51+ pub nlsf_interp: i32,
52+ pub bytes: usize,
53+ pub ltp_gain: f32,
54+}
55+
41 /// A mono, 10/20/40/60 ms, NB/MB/WB SILK encoder.56 /// A mono, 10/20/40/60 ms, NB/MB/WB SILK encoder.
42 #[derive(Clone, Debug)]57 #[derive(Clone, Debug)]
43 pub struct SilkEncoder {58 pub struct SilkEncoder {
@@ -61,6 +76,7 @@ pub struct SilkEncoder {
61 shape_d: Vec<f32>,76 shape_d: Vec<f32>,
62 shape_u: Vec<f32>,77 shape_u: Vec<f32>,
63 shape_gq: Vec<f32>,78 shape_gq: Vec<f32>,
79+ last_diag: SilkFrameDiag,
64 }80 }
65 81
66 /// Noise-shaping constants: AR (denominator) and MA (numerator) bandwidth82 /// Noise-shaping constants: AR (denominator) and MA (numerator) bandwidth
@@ -108,6 +124,7 @@ impl SilkEncoder {
108 shape_d: vec![0.0; 20 * fs_khz],124 shape_d: vec![0.0; 20 * fs_khz],
109 shape_u: vec![0.0; 20 * fs_khz],125 shape_u: vec![0.0; 20 * fs_khz],
110 shape_gq: vec![0.0; 20 * fs_khz],126 shape_gq: vec![0.0; 20 * fs_khz],
127+ last_diag: SilkFrameDiag::default(),
111 }128 }
112 }129 }
113 130
@@ -122,6 +139,12 @@ impl SilkEncoder {
122 self.voiced_frames139 self.voiced_frames
123 }140 }
124 141
142+ /// Per-frame diagnostics captured at the end of the last `encode_inner`
143+ /// call.
144+ pub fn last_diag(&self) -> &SilkFrameDiag {
145+ &self.last_diag
146+ }
147+
125 /// Target bit rate in bits per second; `None` (the default) codes pulses148 /// Target bit rate in bits per second; `None` (the default) codes pulses
126 /// at unit excitation RMS with no rate loop.149 /// at unit excitation RMS with no rate loop.
127 pub fn set_bitrate(&mut self, bps: u32) {150 pub fn set_bitrate(&mut self, bps: u32) {
@@ -627,6 +650,7 @@ impl SilkEncoder {
627 voiced,650 voiced,
628 ltp_scale_index,651 ltp_scale_index,
629 nq: nq.clone(),652 nq: nq.clone(),
653+ ltp_gain,
630 seed_index,654 seed_index,
631 };655 };
632 let enc = &mut self.range;656 let enc = &mut self.range;
@@ -684,6 +708,23 @@ impl SilkEncoder {
684 self.shape_d.copy_from_slice(&best.run.d[n..]);708 self.shape_d.copy_from_slice(&best.run.d[n..]);
685 self.shape_u.copy_from_slice(&best.run.u[n..]);709 self.shape_u.copy_from_slice(&best.run.u[n..]);
686 self.shape_gq.copy_from_slice(&best.run.gq[n..]);710 self.shape_gq.copy_from_slice(&best.run.gq[n..]);
711+ self.last_diag = SilkFrameDiag {
712+ voiced: best.voiced,
713+ signal_type: best.signal_type,
714+ gain_idx: best.gain_idx,
715+ nb_subfr: best.nb_subfr,
716+ lag_index: best.lag_index,
717+ pitch_l: {
718+ let mut pl = [0i32; MAX_NB_SUBFR];
719+ for (i, &v) in pitch_l.iter().enumerate().take(best.nb_subfr) {
720+ pl[i] = v;
721+ }
722+ pl
723+ },
724+ nlsf_interp: best.nq.interp_index,
725+ bytes: best.bytes.len(),
726+ ltp_gain: best.ltp_gain,
727+ };
687 Ok(best)728 Ok(best)
688 }729 }
689 }730 }
@@ -969,6 +1010,7 @@ struct Trial {
969 voiced: bool,1010 voiced: bool,
970 ltp_scale_index: usize,1011 ltp_scale_index: usize,
971 nq: NlsfQuant,1012 nq: NlsfQuant,
1013+ ltp_gain: f32,
972 seed_index: i32,1014 seed_index: i32,
973 }1015 }
974 1016