Skip to main content

opus_rs/silk/
init_decoder.rs

1use crate::silk::decoder_structs::SilkDecoderState;
2use crate::silk::define::*;
3use crate::silk::tables_nlsf::*;
4
5pub fn silk_decoder_set_fs(dec: &mut SilkDecoderState, fs_khz: i32, fs_api_hz: i32) -> i32 {
6    if fs_khz != 8 && fs_khz != 12 && fs_khz != 16 {
7        return -1;
8    }
9
10    dec.fs_khz = fs_khz;
11    dec.fs_api_hz = fs_api_hz;
12
13    dec.nb_subfr = MAX_NB_SUBFR as i32;
14    dec.subfr_length = (SUB_FRAME_LENGTH_MS as i32) * fs_khz;
15    dec.frame_length = dec.subfr_length * dec.nb_subfr;
16    dec.ltp_mem_length = (LTP_MEM_LENGTH_MS as i32) * fs_khz;
17    dec.lpc_order = if fs_khz == 8 {
18        MIN_LPC_ORDER as i32
19    } else {
20        MAX_LPC_ORDER as i32
21    };
22
23    if fs_khz == 8 {
24        if dec.nb_subfr == MAX_NB_SUBFR as i32 {
25            dec.pitch_contour_icdf = &crate::silk::tables::SILK_PITCH_CONTOUR_NB_ICDF;
26        } else {
27            dec.pitch_contour_icdf = &crate::silk::tables::SILK_PITCH_CONTOUR_10_MS_NB_ICDF;
28        }
29    } else if dec.nb_subfr == MAX_NB_SUBFR as i32 {
30        dec.pitch_contour_icdf = &crate::silk::tables::SILK_PITCH_CONTOUR_ICDF;
31    } else {
32        dec.pitch_contour_icdf = &crate::silk::tables::SILK_PITCH_CONTOUR_10_MS_ICDF;
33    }
34
35    dec.pitch_lag_low_bits_icdf = match fs_khz {
36        8 => &crate::silk::tables::SILK_UNIFORM4_ICDF,
37        12 => &crate::silk::tables::SILK_UNIFORM6_ICDF,
38        16 => &crate::silk::tables::SILK_UNIFORM8_ICDF,
39        _ => &crate::silk::tables::SILK_UNIFORM8_ICDF,
40    };
41
42    dec.ps_nlsf_cb = match fs_khz {
43        8 => Some(&SILK_NLSF_CB_NB_MB),
44        12 => Some(&SILK_NLSF_CB_NB_MB),
45        _ => Some(&SILK_NLSF_CB_WB),
46    };
47
48    0
49}
50
51pub fn silk_reset_decoder(ps_dec: &mut SilkDecoderState) -> i32 {
52    ps_dec.prev_gain_q16 = 1 << 16;
53    ps_dec.exc_q14.fill(0);
54    ps_dec.s_lpc_q14_buf.fill(0);
55    ps_dec.out_buf.fill(0);
56    ps_dec.lag_prev = 100;
57    ps_dec.last_gain_index = 10;
58    ps_dec.first_frame_after_reset = 1;
59    ps_dec.ec_prev_signal_type = 0;
60    ps_dec.ec_prev_lag_index = 0;
61    ps_dec.vad_flags.fill(0);
62    ps_dec.lbrr_flag = 0;
63    ps_dec.lbrr_flags.fill(0);
64    ps_dec.prev_nlsf_q15.fill(0);
65    ps_dec.loss_cnt = 0;
66    ps_dec.prev_signal_type = TYPE_NO_VOICE_ACTIVITY;
67    ps_dec.indices = Default::default();
68
69    0
70}
71
72pub fn silk_init_decoder(ps_dec: &mut SilkDecoderState) -> i32 {
73    silk_reset_decoder(ps_dec);
74
75    ps_dec.s_cng = Default::default();
76
77    ps_dec.s_plc = Default::default();
78
79    0
80}
81
82pub fn silk_create_decoder() -> SilkDecoderState {
83    let mut dec = SilkDecoderState::default();
84    silk_init_decoder(&mut dec);
85    dec
86}