forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

mod.rs375 lines, 13.3 KiBHistoryRawDownload
1//! Port of the libopus SILK float encoder (`silk/float/*`, `silk/enc_API.c`
2//! and the fixed-point parts it calls). Field names follow the C structs in
3//! snake_case.
4
5#![allow(
6 clippy::needless_range_loop,
7 clippy::too_many_arguments,
8 clippy::manual_memcpy,
9 clippy::explicit_counter_loop
10)]
11
12pub(crate) mod api;
13pub(crate) mod encode;
14pub(crate) mod nlsf;
15pub(crate) mod nsq;
16pub(crate) mod opus;
17pub(crate) mod pitch;
18pub(crate) mod pred;
19pub(crate) mod resampler;
20pub(crate) mod shape;
21pub(crate) mod sigproc;
22pub(crate) mod stereo;
23pub(crate) mod vad;
24
25pub(crate) const MAX_FS_KHZ: usize = 16;
26pub(crate) const MAX_NB_SUBFR: usize = 4;
27pub(crate) const MAX_LPC_ORDER: usize = 16;
28pub(crate) const MIN_LPC_ORDER: usize = 10;
29pub(crate) const MAX_SHAPE_LPC_ORDER: usize = 24;
30pub(crate) const LTP_ORDER: usize = 5;
31pub(crate) const NB_LTP_CBKS: usize = 3;
32pub(crate) const MAX_FRAMES_PER_PACKET: usize = 3;
33pub(crate) const SUB_FRAME_LENGTH_MS: usize = 5;
34pub(crate) const MAX_SUB_FRAME_LENGTH: usize = SUB_FRAME_LENGTH_MS * MAX_FS_KHZ;
35pub(crate) const MAX_FRAME_LENGTH_MS: usize = SUB_FRAME_LENGTH_MS * MAX_NB_SUBFR;
36pub(crate) const MAX_FRAME_LENGTH: usize = MAX_FRAME_LENGTH_MS * MAX_FS_KHZ;
37pub(crate) const LTP_MEM_LENGTH_MS: usize = 20;
38pub(crate) const LA_PITCH_MS: usize = 2;
39pub(crate) const LA_PITCH_MAX: usize = LA_PITCH_MS * MAX_FS_KHZ;
40pub(crate) const LA_SHAPE_MS: usize = 5;
41pub(crate) const LA_SHAPE_MAX: usize = LA_SHAPE_MS * MAX_FS_KHZ;
42pub(crate) const MAX_FIND_PITCH_LPC_ORDER: usize = 16;
43pub(crate) const FIND_PITCH_LPC_WIN_MS: usize = 20 + (LA_PITCH_MS << 1);
44pub(crate) const FIND_PITCH_LPC_WIN_MS_2_SF: usize = 10 + (LA_PITCH_MS << 1);
45pub(crate) const FIND_PITCH_LPC_WIN_MAX: usize = FIND_PITCH_LPC_WIN_MS * MAX_FS_KHZ;
46pub(crate) const SHAPE_LPC_WIN_MAX: usize = 15 * MAX_FS_KHZ;
47pub(crate) const MAX_DEL_DEC_STATES: usize = 4;
48pub(crate) const DECISION_DELAY: usize = 40;
49pub(crate) const NSQ_LPC_BUF_LENGTH: usize = MAX_LPC_ORDER;
50pub(crate) const HARM_SHAPE_FIR_TAPS: usize = 3;
51pub(crate) const VAD_N_BANDS: usize = 4;
52pub(crate) const N_LEVELS_QGAIN: i32 = 64;
53pub(crate) const MIN_QGAIN_DB: i32 = 2;
54pub(crate) const MAX_QGAIN_DB: i32 = 88;
55pub(crate) const MIN_DELTA_GAIN_QUANT: i32 = -4;
56pub(crate) const MAX_DELTA_GAIN_QUANT: i32 = 36;
57pub(crate) const SHELL_CODEC_FRAME_LENGTH: usize = 16;
58pub(crate) const MAX_NB_SHELL_BLOCKS: usize = MAX_FRAME_LENGTH / SHELL_CODEC_FRAME_LENGTH;
59pub(crate) const N_RATE_LEVELS: usize = 10;
60pub(crate) const SILK_MAX_PULSES: i32 = 16;
61pub(crate) const NLSF_QUANT_MAX_AMPLITUDE: i32 = 4;
62pub(crate) const NLSF_QUANT_MAX_AMPLITUDE_EXT: i32 = 10;
63pub(crate) const NLSF_QUANT_DEL_DEC_STATES: usize = 4;
64pub(crate) const NLSF_VQ_MAX_VECTORS: usize = 32;
65pub(crate) const NB_SPEECH_FRAMES_BEFORE_DTX: i32 = 10;
66pub(crate) const MAX_CONSECUTIVE_DTX: i32 = 20;
67pub(crate) const X_BUF_LEN: usize = 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX;
68
69pub(crate) const TYPE_NO_VOICE_ACTIVITY: i8 = 0;
70pub(crate) const TYPE_UNVOICED: i8 = 1;
71pub(crate) const TYPE_VOICED: i8 = 2;
72
73/// `condCoding`.
74#[derive(Clone, Copy, Debug, PartialEq, Eq)]
75pub(crate) enum CondCoding {
76 Independently,
77 IndependentlyNoLtpScaling,
78 Conditionally,
79}
80
81/// `SideInfoIndices`.
82#[derive(Clone, Copy, Debug, Default)]
83pub(crate) struct SideInfoIndices {
84 pub(crate) gains_indices: [i8; MAX_NB_SUBFR],
85 pub(crate) ltp_index: [i8; MAX_NB_SUBFR],
86 pub(crate) nlsf_indices: [i8; MAX_LPC_ORDER + 1],
87 pub(crate) lag_index: i16,
88 pub(crate) contour_index: i8,
89 pub(crate) signal_type: i8,
90 pub(crate) quant_offset_type: i8,
91 pub(crate) nlsf_interp_coef_q2: i8,
92 pub(crate) per_index: i8,
93 pub(crate) ltp_scale_index: i8,
94 pub(crate) seed: i8,
95}
96
97/// `silk_nsq_state`.
98#[derive(Clone, Debug)]
99pub(crate) struct NsqState {
100 pub(crate) xq: [i16; 2 * MAX_FRAME_LENGTH],
101 pub(crate) s_ltp_shp_q14: [i32; 2 * MAX_FRAME_LENGTH],
102 pub(crate) s_lpc_q14: [i32; MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH],
103 pub(crate) s_ar2_q14: [i32; MAX_SHAPE_LPC_ORDER],
104 pub(crate) s_lf_ar_shp_q14: i32,
105 pub(crate) s_diff_shp_q14: i32,
106 pub(crate) lag_prev: i32,
107 pub(crate) s_ltp_buf_idx: usize,
108 pub(crate) s_ltp_shp_buf_idx: usize,
109 pub(crate) rand_seed: i32,
110 pub(crate) prev_gain_q16: i32,
111 pub(crate) rewhite_flag: bool,
112}
113
114impl Default for NsqState {
115 fn default() -> Self {
116 NsqState {
117 xq: [0; 2 * MAX_FRAME_LENGTH],
118 s_ltp_shp_q14: [0; 2 * MAX_FRAME_LENGTH],
119 s_lpc_q14: [0; MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH],
120 s_ar2_q14: [0; MAX_SHAPE_LPC_ORDER],
121 s_lf_ar_shp_q14: 0,
122 s_diff_shp_q14: 0,
123 lag_prev: 0,
124 s_ltp_buf_idx: 0,
125 s_ltp_shp_buf_idx: 0,
126 rand_seed: 0,
127 prev_gain_q16: 0,
128 rewhite_flag: false,
129 }
130 }
131}
132
133/// `silk_VAD_state`.
134#[derive(Clone, Debug, Default)]
135pub(crate) struct VadState {
136 pub(crate) ana_state: [i32; 2],
137 pub(crate) ana_state1: [i32; 2],
138 pub(crate) ana_state2: [i32; 2],
139 pub(crate) xnrg_subfr: [i32; VAD_N_BANDS],
140 pub(crate) nrg_ratio_smth_q8: [i32; VAD_N_BANDS],
141 pub(crate) hp_state: i16,
142 pub(crate) nl: [i32; VAD_N_BANDS],
143 pub(crate) inv_nl: [i32; VAD_N_BANDS],
144 pub(crate) noise_level_bias: [i32; VAD_N_BANDS],
145 pub(crate) counter: i32,
146}
147
148/// `silk_shape_state_FLP`.
149#[derive(Clone, Debug, Default)]
150pub(crate) struct ShapeState {
151 pub(crate) last_gain_index: i8,
152 pub(crate) harm_shape_gain_smth: f32,
153 pub(crate) tilt_smth: f32,
154}
155
156/// `silk_LP_state`.
157#[derive(Clone, Copy, Debug, Default)]
158pub(crate) struct LpState {
159 pub(crate) in_lp_state: [i32; 2],
160 pub(crate) transition_frame_no: i32,
161 pub(crate) mode: i32,
162 pub(crate) saved_fs_khz: i32,
163}
164
165/// `silk_encoder_state` merged with `silk_encoder_state_FLP` (minus the
166/// API resampler, which `api.rs` owns).
167#[derive(Clone, Debug)]
168pub(crate) struct EncState {
169 pub(crate) s_lp: LpState,
170 pub(crate) in_hp_state: [i32; 2],
171 pub(crate) variable_hp_smth1_q15: i32,
172 pub(crate) variable_hp_smth2_q15: i32,
173 pub(crate) s_vad: VadState,
174 pub(crate) s_nsq: NsqState,
175 pub(crate) prev_nlsfq_q15: [i16; MAX_LPC_ORDER],
176 pub(crate) speech_activity_q8: i32,
177 pub(crate) allow_bandwidth_switch: bool,
178 pub(crate) lbrr_prev_last_gain_index: i8,
179 pub(crate) prev_signal_type: i8,
180 pub(crate) prev_lag: i32,
181 pub(crate) pitch_lpc_win_length: usize,
182 pub(crate) max_pitch_lag: i32,
183 pub(crate) api_fs_hz: i32,
184 pub(crate) prev_api_fs_hz: i32,
185 pub(crate) max_internal_fs_hz: i32,
186 pub(crate) min_internal_fs_hz: i32,
187 pub(crate) desired_internal_fs_hz: i32,
188 pub(crate) fs_khz: usize,
189 pub(crate) nb_subfr: usize,
190 pub(crate) frame_length: usize,
191 pub(crate) subfr_length: usize,
192 pub(crate) ltp_mem_length: usize,
193 pub(crate) la_pitch: usize,
194 pub(crate) la_shape: usize,
195 pub(crate) shape_win_length: usize,
196 pub(crate) target_rate_bps: i32,
197 pub(crate) packet_size_ms: usize,
198 pub(crate) packet_loss_perc: i32,
199 pub(crate) frame_counter: i32,
200 pub(crate) complexity: i32,
201 pub(crate) n_states_delayed_decision: usize,
202 pub(crate) use_interpolated_nlsfs: bool,
203 pub(crate) shaping_lpc_order: usize,
204 pub(crate) predict_lpc_order: usize,
205 pub(crate) pitch_estimation_complexity: i32,
206 pub(crate) pitch_estimation_lpc_order: usize,
207 pub(crate) pitch_estimation_threshold_q16: i32,
208 pub(crate) sum_log_gain_q7: i32,
209 pub(crate) nlsf_msvq_survivors: usize,
210 pub(crate) first_frame_after_reset: bool,
211 pub(crate) controlled_since_last_payload: bool,
212 pub(crate) warping_q16: i32,
213 pub(crate) use_cbr: bool,
214 pub(crate) prefill_flag: bool,
215 /// `psNLSF_CB`: wideband (16 kHz) codebook when true, NB/MB otherwise.
216 pub(crate) nlsf_cb_wb: bool,
217 pub(crate) input_quality_bands_q15: [i32; VAD_N_BANDS],
218 pub(crate) input_tilt_q15: i32,
219 pub(crate) snr_db_q7: i32,
220 pub(crate) vad_flags: [bool; MAX_FRAMES_PER_PACKET],
221 pub(crate) lbrr_flag: bool,
222 pub(crate) lbrr_flags: [bool; MAX_FRAMES_PER_PACKET],
223 pub(crate) indices: SideInfoIndices,
224 pub(crate) pulses: [i8; MAX_FRAME_LENGTH],
225 pub(crate) input_buf: [i16; MAX_FRAME_LENGTH + 2],
226 pub(crate) input_buf_ix: usize,
227 pub(crate) n_frames_per_packet: usize,
228 pub(crate) n_frames_encoded: usize,
229 pub(crate) n_channels_api: usize,
230 pub(crate) n_channels_internal: usize,
231 pub(crate) channel_nb: usize,
232 pub(crate) ec_prev_signal_type: i8,
233 pub(crate) ec_prev_lag_index: i16,
234 pub(crate) use_dtx: bool,
235 pub(crate) in_dtx: bool,
236 pub(crate) no_speech_counter: i32,
237 pub(crate) use_in_band_fec: bool,
238 pub(crate) lbrr_enabled: bool,
239 pub(crate) lbrr_gain_increases: i32,
240 pub(crate) indices_lbrr: [SideInfoIndices; MAX_FRAMES_PER_PACKET],
241 pub(crate) pulses_lbrr: [[i8; MAX_FRAME_LENGTH]; MAX_FRAMES_PER_PACKET],
242 pub(crate) s_shape: ShapeState,
243 pub(crate) x_buf: [f32; X_BUF_LEN],
244 pub(crate) ltp_corr: f32,
245}
246
247impl Default for EncState {
248 fn default() -> Self {
249 EncState {
250 s_lp: LpState::default(),
251 in_hp_state: [0; 2],
252 variable_hp_smth1_q15: 0,
253 variable_hp_smth2_q15: 0,
254 s_vad: VadState::default(),
255 s_nsq: NsqState::default(),
256 prev_nlsfq_q15: [0; MAX_LPC_ORDER],
257 speech_activity_q8: 0,
258 allow_bandwidth_switch: false,
259 lbrr_prev_last_gain_index: 0,
260 prev_signal_type: 0,
261 prev_lag: 0,
262 pitch_lpc_win_length: 0,
263 max_pitch_lag: 0,
264 api_fs_hz: 0,
265 prev_api_fs_hz: 0,
266 max_internal_fs_hz: 0,
267 min_internal_fs_hz: 0,
268 desired_internal_fs_hz: 0,
269 fs_khz: 0,
270 nb_subfr: 0,
271 frame_length: 0,
272 subfr_length: 0,
273 ltp_mem_length: 0,
274 la_pitch: 0,
275 la_shape: 0,
276 shape_win_length: 0,
277 target_rate_bps: 0,
278 packet_size_ms: 0,
279 packet_loss_perc: 0,
280 frame_counter: 0,
281 complexity: 0,
282 n_states_delayed_decision: 0,
283 use_interpolated_nlsfs: false,
284 shaping_lpc_order: 0,
285 predict_lpc_order: 0,
286 pitch_estimation_complexity: 0,
287 pitch_estimation_lpc_order: 0,
288 pitch_estimation_threshold_q16: 0,
289 sum_log_gain_q7: 0,
290 nlsf_msvq_survivors: 0,
291 first_frame_after_reset: false,
292 controlled_since_last_payload: false,
293 warping_q16: 0,
294 use_cbr: false,
295 prefill_flag: false,
296 nlsf_cb_wb: false,
297 input_quality_bands_q15: [0; VAD_N_BANDS],
298 input_tilt_q15: 0,
299 snr_db_q7: 0,
300 vad_flags: [false; MAX_FRAMES_PER_PACKET],
301 lbrr_flag: false,
302 lbrr_flags: [false; MAX_FRAMES_PER_PACKET],
303 indices: SideInfoIndices::default(),
304 pulses: [0; MAX_FRAME_LENGTH],
305 input_buf: [0; MAX_FRAME_LENGTH + 2],
306 input_buf_ix: 0,
307 n_frames_per_packet: 0,
308 n_frames_encoded: 0,
309 n_channels_api: 0,
310 n_channels_internal: 0,
311 channel_nb: 0,
312 ec_prev_signal_type: 0,
313 ec_prev_lag_index: 0,
314 use_dtx: false,
315 in_dtx: false,
316 no_speech_counter: 0,
317 use_in_band_fec: false,
318 lbrr_enabled: false,
319 lbrr_gain_increases: 0,
320 indices_lbrr: [SideInfoIndices::default(); MAX_FRAMES_PER_PACKET],
321 pulses_lbrr: [[0; MAX_FRAME_LENGTH]; MAX_FRAMES_PER_PACKET],
322 s_shape: ShapeState::default(),
323 x_buf: [0.0; X_BUF_LEN],
324 ltp_corr: 0.0,
325 }
326 }
327}
328
329/// `silk_encoder_control_FLP`.
330#[derive(Clone, Debug)]
331pub(crate) struct EncCtrl {
332 pub(crate) gains: [f32; MAX_NB_SUBFR],
333 pub(crate) pred_coef: [[f32; MAX_LPC_ORDER]; 2],
334 pub(crate) ltp_coef: [f32; LTP_ORDER * MAX_NB_SUBFR],
335 pub(crate) ltp_scale: f32,
336 pub(crate) pitch_l: [i32; MAX_NB_SUBFR],
337 pub(crate) ar: [f32; MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER],
338 pub(crate) lf_ma_shp: [f32; MAX_NB_SUBFR],
339 pub(crate) lf_ar_shp: [f32; MAX_NB_SUBFR],
340 pub(crate) tilt: [f32; MAX_NB_SUBFR],
341 pub(crate) harm_shape_gain: [f32; MAX_NB_SUBFR],
342 pub(crate) lambda: f32,
343 pub(crate) input_quality: f32,
344 pub(crate) coding_quality: f32,
345 pub(crate) pred_gain: f32,
346 pub(crate) lt_pred_cod_gain: f32,
347 pub(crate) res_nrg: [f32; MAX_NB_SUBFR],
348 pub(crate) gains_unq_q16: [i32; MAX_NB_SUBFR],
349 pub(crate) last_gain_index_prev: i8,
350}
351
352impl Default for EncCtrl {
353 fn default() -> Self {
354 EncCtrl {
355 gains: [0.0; MAX_NB_SUBFR],
356 pred_coef: [[0.0; MAX_LPC_ORDER]; 2],
357 ltp_coef: [0.0; LTP_ORDER * MAX_NB_SUBFR],
358 ltp_scale: 0.0,
359 pitch_l: [0; MAX_NB_SUBFR],
360 ar: [0.0; MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER],
361 lf_ma_shp: [0.0; MAX_NB_SUBFR],
362 lf_ar_shp: [0.0; MAX_NB_SUBFR],
363 tilt: [0.0; MAX_NB_SUBFR],
364 harm_shape_gain: [0.0; MAX_NB_SUBFR],
365 lambda: 0.0,
366 input_quality: 0.0,
367 coding_quality: 0.0,
368 pred_gain: 0.0,
369 lt_pred_cod_gain: 0.0,
370 res_nrg: [0.0; MAX_NB_SUBFR],
371 gains_unq_q16: [0; MAX_NB_SUBFR],
372 last_gain_index_prev: 0,
373 }
374 }
375}