//! Port of the libopus SILK float encoder (`silk/float/*`, `silk/enc_API.c` //! and the fixed-point parts it calls). Field names follow the C structs in //! snake_case. #![allow( clippy::needless_range_loop, clippy::too_many_arguments, clippy::manual_memcpy, clippy::explicit_counter_loop )] pub(crate) mod api; pub(crate) mod encode; pub(crate) mod nlsf; pub(crate) mod nsq; pub(crate) mod opus; pub(crate) mod pitch; pub(crate) mod pred; pub(crate) mod resampler; pub(crate) mod shape; pub(crate) mod sigproc; pub(crate) mod stereo; pub(crate) mod vad; pub(crate) const MAX_FS_KHZ: usize = 16; pub(crate) const MAX_NB_SUBFR: usize = 4; pub(crate) const MAX_LPC_ORDER: usize = 16; pub(crate) const MIN_LPC_ORDER: usize = 10; pub(crate) const MAX_SHAPE_LPC_ORDER: usize = 24; pub(crate) const LTP_ORDER: usize = 5; pub(crate) const NB_LTP_CBKS: usize = 3; pub(crate) const MAX_FRAMES_PER_PACKET: usize = 3; pub(crate) const SUB_FRAME_LENGTH_MS: usize = 5; pub(crate) const MAX_SUB_FRAME_LENGTH: usize = SUB_FRAME_LENGTH_MS * MAX_FS_KHZ; pub(crate) const MAX_FRAME_LENGTH_MS: usize = SUB_FRAME_LENGTH_MS * MAX_NB_SUBFR; pub(crate) const MAX_FRAME_LENGTH: usize = MAX_FRAME_LENGTH_MS * MAX_FS_KHZ; pub(crate) const LTP_MEM_LENGTH_MS: usize = 20; pub(crate) const LA_PITCH_MS: usize = 2; pub(crate) const LA_PITCH_MAX: usize = LA_PITCH_MS * MAX_FS_KHZ; pub(crate) const LA_SHAPE_MS: usize = 5; pub(crate) const LA_SHAPE_MAX: usize = LA_SHAPE_MS * MAX_FS_KHZ; pub(crate) const MAX_FIND_PITCH_LPC_ORDER: usize = 16; pub(crate) const FIND_PITCH_LPC_WIN_MS: usize = 20 + (LA_PITCH_MS << 1); pub(crate) const FIND_PITCH_LPC_WIN_MS_2_SF: usize = 10 + (LA_PITCH_MS << 1); pub(crate) const FIND_PITCH_LPC_WIN_MAX: usize = FIND_PITCH_LPC_WIN_MS * MAX_FS_KHZ; pub(crate) const SHAPE_LPC_WIN_MAX: usize = 15 * MAX_FS_KHZ; pub(crate) const MAX_DEL_DEC_STATES: usize = 4; pub(crate) const DECISION_DELAY: usize = 40; pub(crate) const NSQ_LPC_BUF_LENGTH: usize = MAX_LPC_ORDER; pub(crate) const HARM_SHAPE_FIR_TAPS: usize = 3; pub(crate) const VAD_N_BANDS: usize = 4; pub(crate) const N_LEVELS_QGAIN: i32 = 64; pub(crate) const MIN_QGAIN_DB: i32 = 2; pub(crate) const MAX_QGAIN_DB: i32 = 88; pub(crate) const MIN_DELTA_GAIN_QUANT: i32 = -4; pub(crate) const MAX_DELTA_GAIN_QUANT: i32 = 36; pub(crate) const SHELL_CODEC_FRAME_LENGTH: usize = 16; pub(crate) const MAX_NB_SHELL_BLOCKS: usize = MAX_FRAME_LENGTH / SHELL_CODEC_FRAME_LENGTH; pub(crate) const N_RATE_LEVELS: usize = 10; pub(crate) const SILK_MAX_PULSES: i32 = 16; pub(crate) const NLSF_QUANT_MAX_AMPLITUDE: i32 = 4; pub(crate) const NLSF_QUANT_MAX_AMPLITUDE_EXT: i32 = 10; pub(crate) const NLSF_QUANT_DEL_DEC_STATES: usize = 4; pub(crate) const NLSF_VQ_MAX_VECTORS: usize = 32; pub(crate) const NB_SPEECH_FRAMES_BEFORE_DTX: i32 = 10; pub(crate) const MAX_CONSECUTIVE_DTX: i32 = 20; pub(crate) const X_BUF_LEN: usize = 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX; pub(crate) const TYPE_NO_VOICE_ACTIVITY: i8 = 0; pub(crate) const TYPE_UNVOICED: i8 = 1; pub(crate) const TYPE_VOICED: i8 = 2; /// `condCoding`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum CondCoding { Independently, IndependentlyNoLtpScaling, Conditionally, } /// `SideInfoIndices`. #[derive(Clone, Copy, Debug, Default)] pub(crate) struct SideInfoIndices { pub(crate) gains_indices: [i8; MAX_NB_SUBFR], pub(crate) ltp_index: [i8; MAX_NB_SUBFR], pub(crate) nlsf_indices: [i8; MAX_LPC_ORDER + 1], pub(crate) lag_index: i16, pub(crate) contour_index: i8, pub(crate) signal_type: i8, pub(crate) quant_offset_type: i8, pub(crate) nlsf_interp_coef_q2: i8, pub(crate) per_index: i8, pub(crate) ltp_scale_index: i8, pub(crate) seed: i8, } /// `silk_nsq_state`. #[derive(Clone, Debug)] pub(crate) struct NsqState { pub(crate) xq: [i16; 2 * MAX_FRAME_LENGTH], pub(crate) s_ltp_shp_q14: [i32; 2 * MAX_FRAME_LENGTH], pub(crate) s_lpc_q14: [i32; MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH], pub(crate) s_ar2_q14: [i32; MAX_SHAPE_LPC_ORDER], pub(crate) s_lf_ar_shp_q14: i32, pub(crate) s_diff_shp_q14: i32, pub(crate) lag_prev: i32, pub(crate) s_ltp_buf_idx: usize, pub(crate) s_ltp_shp_buf_idx: usize, pub(crate) rand_seed: i32, pub(crate) prev_gain_q16: i32, pub(crate) rewhite_flag: bool, } impl Default for NsqState { fn default() -> Self { NsqState { xq: [0; 2 * MAX_FRAME_LENGTH], s_ltp_shp_q14: [0; 2 * MAX_FRAME_LENGTH], s_lpc_q14: [0; MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH], s_ar2_q14: [0; MAX_SHAPE_LPC_ORDER], s_lf_ar_shp_q14: 0, s_diff_shp_q14: 0, lag_prev: 0, s_ltp_buf_idx: 0, s_ltp_shp_buf_idx: 0, rand_seed: 0, prev_gain_q16: 0, rewhite_flag: false, } } } /// `silk_VAD_state`. #[derive(Clone, Debug, Default)] pub(crate) struct VadState { pub(crate) ana_state: [i32; 2], pub(crate) ana_state1: [i32; 2], pub(crate) ana_state2: [i32; 2], pub(crate) xnrg_subfr: [i32; VAD_N_BANDS], pub(crate) nrg_ratio_smth_q8: [i32; VAD_N_BANDS], pub(crate) hp_state: i16, pub(crate) nl: [i32; VAD_N_BANDS], pub(crate) inv_nl: [i32; VAD_N_BANDS], pub(crate) noise_level_bias: [i32; VAD_N_BANDS], pub(crate) counter: i32, } /// `silk_shape_state_FLP`. #[derive(Clone, Debug, Default)] pub(crate) struct ShapeState { pub(crate) last_gain_index: i8, pub(crate) harm_shape_gain_smth: f32, pub(crate) tilt_smth: f32, } /// `silk_LP_state`. #[derive(Clone, Copy, Debug, Default)] pub(crate) struct LpState { pub(crate) in_lp_state: [i32; 2], pub(crate) transition_frame_no: i32, pub(crate) mode: i32, pub(crate) saved_fs_khz: i32, } /// `silk_encoder_state` merged with `silk_encoder_state_FLP` (minus the /// API resampler, which `api.rs` owns). #[derive(Clone, Debug)] pub(crate) struct EncState { pub(crate) s_lp: LpState, pub(crate) in_hp_state: [i32; 2], pub(crate) variable_hp_smth1_q15: i32, pub(crate) variable_hp_smth2_q15: i32, pub(crate) s_vad: VadState, pub(crate) s_nsq: NsqState, pub(crate) prev_nlsfq_q15: [i16; MAX_LPC_ORDER], pub(crate) speech_activity_q8: i32, pub(crate) allow_bandwidth_switch: bool, pub(crate) lbrr_prev_last_gain_index: i8, pub(crate) prev_signal_type: i8, pub(crate) prev_lag: i32, pub(crate) pitch_lpc_win_length: usize, pub(crate) max_pitch_lag: i32, pub(crate) api_fs_hz: i32, pub(crate) prev_api_fs_hz: i32, pub(crate) max_internal_fs_hz: i32, pub(crate) min_internal_fs_hz: i32, pub(crate) desired_internal_fs_hz: i32, pub(crate) fs_khz: usize, pub(crate) nb_subfr: usize, pub(crate) frame_length: usize, pub(crate) subfr_length: usize, pub(crate) ltp_mem_length: usize, pub(crate) la_pitch: usize, pub(crate) la_shape: usize, pub(crate) shape_win_length: usize, pub(crate) target_rate_bps: i32, pub(crate) packet_size_ms: usize, pub(crate) packet_loss_perc: i32, pub(crate) frame_counter: i32, pub(crate) complexity: i32, pub(crate) n_states_delayed_decision: usize, pub(crate) use_interpolated_nlsfs: bool, pub(crate) shaping_lpc_order: usize, pub(crate) predict_lpc_order: usize, pub(crate) pitch_estimation_complexity: i32, pub(crate) pitch_estimation_lpc_order: usize, pub(crate) pitch_estimation_threshold_q16: i32, pub(crate) sum_log_gain_q7: i32, pub(crate) nlsf_msvq_survivors: usize, pub(crate) first_frame_after_reset: bool, pub(crate) controlled_since_last_payload: bool, pub(crate) warping_q16: i32, pub(crate) use_cbr: bool, pub(crate) prefill_flag: bool, /// `psNLSF_CB`: wideband (16 kHz) codebook when true, NB/MB otherwise. pub(crate) nlsf_cb_wb: bool, pub(crate) input_quality_bands_q15: [i32; VAD_N_BANDS], pub(crate) input_tilt_q15: i32, pub(crate) snr_db_q7: i32, pub(crate) vad_flags: [bool; MAX_FRAMES_PER_PACKET], pub(crate) lbrr_flag: bool, pub(crate) lbrr_flags: [bool; MAX_FRAMES_PER_PACKET], pub(crate) indices: SideInfoIndices, pub(crate) pulses: [i8; MAX_FRAME_LENGTH], pub(crate) input_buf: [i16; MAX_FRAME_LENGTH + 2], pub(crate) input_buf_ix: usize, pub(crate) n_frames_per_packet: usize, pub(crate) n_frames_encoded: usize, pub(crate) n_channels_api: usize, pub(crate) n_channels_internal: usize, pub(crate) channel_nb: usize, pub(crate) ec_prev_signal_type: i8, pub(crate) ec_prev_lag_index: i16, pub(crate) use_dtx: bool, pub(crate) in_dtx: bool, pub(crate) no_speech_counter: i32, pub(crate) use_in_band_fec: bool, pub(crate) lbrr_enabled: bool, pub(crate) lbrr_gain_increases: i32, pub(crate) indices_lbrr: [SideInfoIndices; MAX_FRAMES_PER_PACKET], pub(crate) pulses_lbrr: [[i8; MAX_FRAME_LENGTH]; MAX_FRAMES_PER_PACKET], pub(crate) s_shape: ShapeState, pub(crate) x_buf: [f32; X_BUF_LEN], pub(crate) ltp_corr: f32, } impl Default for EncState { fn default() -> Self { EncState { s_lp: LpState::default(), in_hp_state: [0; 2], variable_hp_smth1_q15: 0, variable_hp_smth2_q15: 0, s_vad: VadState::default(), s_nsq: NsqState::default(), prev_nlsfq_q15: [0; MAX_LPC_ORDER], speech_activity_q8: 0, allow_bandwidth_switch: false, lbrr_prev_last_gain_index: 0, prev_signal_type: 0, prev_lag: 0, pitch_lpc_win_length: 0, max_pitch_lag: 0, api_fs_hz: 0, prev_api_fs_hz: 0, max_internal_fs_hz: 0, min_internal_fs_hz: 0, desired_internal_fs_hz: 0, fs_khz: 0, nb_subfr: 0, frame_length: 0, subfr_length: 0, ltp_mem_length: 0, la_pitch: 0, la_shape: 0, shape_win_length: 0, target_rate_bps: 0, packet_size_ms: 0, packet_loss_perc: 0, frame_counter: 0, complexity: 0, n_states_delayed_decision: 0, use_interpolated_nlsfs: false, shaping_lpc_order: 0, predict_lpc_order: 0, pitch_estimation_complexity: 0, pitch_estimation_lpc_order: 0, pitch_estimation_threshold_q16: 0, sum_log_gain_q7: 0, nlsf_msvq_survivors: 0, first_frame_after_reset: false, controlled_since_last_payload: false, warping_q16: 0, use_cbr: false, prefill_flag: false, nlsf_cb_wb: false, input_quality_bands_q15: [0; VAD_N_BANDS], input_tilt_q15: 0, snr_db_q7: 0, vad_flags: [false; MAX_FRAMES_PER_PACKET], lbrr_flag: false, lbrr_flags: [false; MAX_FRAMES_PER_PACKET], indices: SideInfoIndices::default(), pulses: [0; MAX_FRAME_LENGTH], input_buf: [0; MAX_FRAME_LENGTH + 2], input_buf_ix: 0, n_frames_per_packet: 0, n_frames_encoded: 0, n_channels_api: 0, n_channels_internal: 0, channel_nb: 0, ec_prev_signal_type: 0, ec_prev_lag_index: 0, use_dtx: false, in_dtx: false, no_speech_counter: 0, use_in_band_fec: false, lbrr_enabled: false, lbrr_gain_increases: 0, indices_lbrr: [SideInfoIndices::default(); MAX_FRAMES_PER_PACKET], pulses_lbrr: [[0; MAX_FRAME_LENGTH]; MAX_FRAMES_PER_PACKET], s_shape: ShapeState::default(), x_buf: [0.0; X_BUF_LEN], ltp_corr: 0.0, } } } /// `silk_encoder_control_FLP`. #[derive(Clone, Debug)] pub(crate) struct EncCtrl { pub(crate) gains: [f32; MAX_NB_SUBFR], pub(crate) pred_coef: [[f32; MAX_LPC_ORDER]; 2], pub(crate) ltp_coef: [f32; LTP_ORDER * MAX_NB_SUBFR], pub(crate) ltp_scale: f32, pub(crate) pitch_l: [i32; MAX_NB_SUBFR], pub(crate) ar: [f32; MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER], pub(crate) lf_ma_shp: [f32; MAX_NB_SUBFR], pub(crate) lf_ar_shp: [f32; MAX_NB_SUBFR], pub(crate) tilt: [f32; MAX_NB_SUBFR], pub(crate) harm_shape_gain: [f32; MAX_NB_SUBFR], pub(crate) lambda: f32, pub(crate) input_quality: f32, pub(crate) coding_quality: f32, pub(crate) pred_gain: f32, pub(crate) lt_pred_cod_gain: f32, pub(crate) res_nrg: [f32; MAX_NB_SUBFR], pub(crate) gains_unq_q16: [i32; MAX_NB_SUBFR], pub(crate) last_gain_index_prev: i8, } impl Default for EncCtrl { fn default() -> Self { EncCtrl { gains: [0.0; MAX_NB_SUBFR], pred_coef: [[0.0; MAX_LPC_ORDER]; 2], ltp_coef: [0.0; LTP_ORDER * MAX_NB_SUBFR], ltp_scale: 0.0, pitch_l: [0; MAX_NB_SUBFR], ar: [0.0; MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER], lf_ma_shp: [0.0; MAX_NB_SUBFR], lf_ar_shp: [0.0; MAX_NB_SUBFR], tilt: [0.0; MAX_NB_SUBFR], harm_shape_gain: [0.0; MAX_NB_SUBFR], lambda: 0.0, input_quality: 0.0, coding_quality: 0.0, pred_gain: 0.0, lt_pred_cod_gain: 0.0, res_nrg: [0.0; MAX_NB_SUBFR], gains_unq_q16: [0; MAX_NB_SUBFR], last_gain_index_prev: 0, } } }