Skip to content

Commit

Permalink
refactor: make silk_decode_core safe
Browse files Browse the repository at this point in the history
  • Loading branch information
DCNick3 committed Jul 7, 2024
1 parent b499210 commit c94435f
Show file tree
Hide file tree
Showing 30 changed files with 559 additions and 709 deletions.
23 changes: 12 additions & 11 deletions src/silk/CNG.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::externs::{memcpy, memmove, memset};
use crate::silk::define::{CNG_BUF_MASK_MAX, MAX_LPC_ORDER, TYPE_NO_VOICE_ACTIVITY};
use crate::silk::structs::{silk_CNG_struct, silk_decoder_control, silk_decoder_state};
use crate::silk::Inlines::silk_SQRT_APPROX;
use crate::silk::SigProc_FIX::silk_RAND;
use crate::silk::NLSF2A::silk_NLSF2A;

#[inline]
unsafe fn silk_CNG_exc(exc_Q14: *mut i32, exc_buf_Q14: *mut i32, length: i32, rand_seed: *mut i32) {
let mut seed: i32 = 0;
Expand All @@ -23,23 +25,22 @@ unsafe fn silk_CNG_exc(exc_Q14: *mut i32, exc_buf_Q14: *mut i32, length: i32, ra
seed = *rand_seed;
i = 0;
while i < length {
seed = 907633515_u32.wrapping_add((seed as u32).wrapping_mul(196314165)) as i32;
seed = silk_RAND(seed);
idx = seed >> 24 & exc_mask;
*exc_Q14.offset(i as isize) = *exc_buf_Q14.offset(idx as isize);
i += 1;
}
*rand_seed = seed;
}
pub fn silk_CNG_Reset(psDec: &mut silk_decoder_state) {
let mut i: i32 = 0;
let mut NLSF_step_Q15: i32 = 0;
let mut NLSF_acc_Q15: i32 = 0;
NLSF_step_Q15 = 0x7fff / (psDec.LPC_order + 1);
NLSF_step_Q15 = 0x7fff / (psDec.LPC_order as i32 + 1);
NLSF_acc_Q15 = 0;
i = 0;
let mut i = 0;
while i < psDec.LPC_order {
NLSF_acc_Q15 += NLSF_step_Q15;
psDec.sCNG.CNG_smth_NLSF_Q15[i as usize] = NLSF_acc_Q15 as i16;
psDec.sCNG.CNG_smth_NLSF_Q15[i] = NLSF_acc_Q15 as i16;
i += 1;
}
psDec.sCNG.CNG_smth_Gain_Q16 = 0;
Expand All @@ -65,7 +66,7 @@ pub unsafe fn silk_CNG(
}
if psDec.lossCnt == 0 && psDec.prevSignalType == TYPE_NO_VOICE_ACTIVITY {
i = 0;
while i < psDec.LPC_order {
while i < psDec.LPC_order as i32 {
(*psCNG).CNG_smth_NLSF_Q15[i as usize] = ((*psCNG).CNG_smth_NLSF_Q15[i as usize] as i32
+ ((psDec.prevNLSF_Q15[i as usize] as i32
- (*psCNG).CNG_smth_NLSF_Q15[i as usize] as i32) as i64
Expand All @@ -76,7 +77,7 @@ pub unsafe fn silk_CNG(
max_Gain_Q16 = 0;
subfr = 0;
i = 0;
while i < psDec.nb_subfr {
while i < psDec.nb_subfr as i32 {
if (*psDecCtrl).Gains_Q16[i as usize] > max_Gain_Q16 {
max_Gain_Q16 = (*psDecCtrl).Gains_Q16[i as usize];
subfr = i;
Expand All @@ -96,12 +97,12 @@ pub unsafe fn silk_CNG(
((*psCNG).CNG_exc_buf_Q14).as_mut_ptr() as *mut core::ffi::c_void,
&mut *(psDec.exc_Q14)
.as_mut_ptr()
.offset((subfr * psDec.subfr_length) as isize) as *mut i32
.offset((subfr * psDec.subfr_length as i32) as isize) as *mut i32
as *const core::ffi::c_void,
(psDec.subfr_length as u64).wrapping_mul(::core::mem::size_of::<i32>() as u64),
);
i = 0;
while i < psDec.nb_subfr {
while i < psDec.nb_subfr as i32 {
(*psCNG).CNG_smth_Gain_Q16 += (((*psDecCtrl).Gains_Q16[i as usize]
- (*psCNG).CNG_smth_Gain_Q16) as i64
* 4634 as i64
Expand Down Expand Up @@ -145,7 +146,7 @@ pub unsafe fn silk_CNG(
assert!(psDec.LPC_order == 10 || psDec.LPC_order == 16);
i = 0;
while i < length {
LPC_pred_Q10 = psDec.LPC_order >> 1;
LPC_pred_Q10 = psDec.LPC_order as i32 >> 1;
LPC_pred_Q10 = (LPC_pred_Q10 as i64
+ (*CNG_sig_Q14.as_mut_ptr().offset((16 + i - 1) as isize) as i64
* A_Q12[0 as usize] as i64
Expand Down Expand Up @@ -214,7 +215,7 @@ pub unsafe fn silk_CNG(
}
*CNG_sig_Q14
.as_mut_ptr()
.offset((MAX_LPC_ORDER + i) as isize) =
.offset((MAX_LPC_ORDER as i32 + i) as isize) =
if (*CNG_sig_Q14.as_mut_ptr().offset((16 + i) as isize) as u32).wrapping_add(
(((if 0x80000000 as u32 as i32 >> 4 > 0x7fffffff >> 4 {
if LPC_pred_Q10 > 0x80000000 as u32 as i32 >> 4 {
Expand Down
12 changes: 7 additions & 5 deletions src/silk/LPC_analysis_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ use crate::silk::SigProc_FIX::{silk_RSHIFT_ROUND, silk_SAT16};
/// filter always starts with zero state
/// first d output samples are set to zero
///
/// out O Output signal
/// in I Input signal
/// B I MA prediction coefficients, Q12 [order]
/// len I Signal length
/// d I Filter order
/// ```text
/// out O Output signal
/// in I Input signal
/// B I MA prediction coefficients, Q12 [order]
/// len I Signal length
/// d I Filter order
/// ```
pub fn silk_LPC_analysis_filter(out: &mut [i16], input: &[i16], B: &[i16]) {
let len = input.len();
let d = B.len();
Expand Down
4 changes: 2 additions & 2 deletions src/silk/LP_variable_cutoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ fn silk_LP_interpolate_filter_taps(
/// frame_length I Frame length
/// ```
pub fn silk_LP_variable_cutoff(psLP: &mut silk_LP_state, frame: &mut [i16]) {
assert!(psLP.transition_frame_no >= 0 && psLP.transition_frame_no <= TRANSITION_FRAMES);
assert!(psLP.transition_frame_no >= 0 && psLP.transition_frame_no <= TRANSITION_FRAMES as i32);

/* Run filter if needed */
if psLP.mode != 0 {
/* Calculate index and interpolation factor for interpolation */
let fac_Q16 = (TRANSITION_FRAMES - psLP.transition_frame_no) << (16 - 6);
let fac_Q16 = (TRANSITION_FRAMES as i32 - psLP.transition_frame_no) << (16 - 6);
let ind = fac_Q16 >> 16;
let fac_Q16 = fac_Q16 - (ind << 16);

Expand Down
2 changes: 1 addition & 1 deletion src/silk/NLSF_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub unsafe fn silk_NLSF_encode(
*RD_Q25.as_mut_ptr().offset(s as isize) = silk_NLSF_del_dec_quant(
&mut *tempIndices2
.as_mut_ptr()
.offset((s * MAX_LPC_ORDER) as isize),
.offset((s * MAX_LPC_ORDER as i32) as isize),
res_Q10.as_mut_ptr() as *const i16,
W_adj_Q5.as_mut_ptr() as *const i16,
pred_Q8.as_mut_ptr() as *const u8,
Expand Down
49 changes: 25 additions & 24 deletions src/silk/NSQ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ use crate::silk::structs::{silk_encoder_state, silk_nsq_state, SideInfoIndices};
use crate::silk::tables_other::silk_Quantization_Offsets_Q10;
use crate::silk::Inlines::{silk_DIV32_varQ, silk_INVERSE32_varQ};
use crate::silk::LPC_analysis_filter::silk_LPC_analysis_filter;
use crate::silk::SigProc_FIX::silk_RAND;

pub unsafe fn silk_NSQ_c(
psEncC: &silk_encoder_state,
Expand Down Expand Up @@ -152,17 +153,17 @@ pub unsafe fn silk_NSQ_c(
let mut sLTP: Vec<i16> = ::std::vec::from_elem(0, vla_0);
let vla_1 = psEncC.subfr_length as usize;
let mut x_sc_Q10: Vec<i32> = ::std::vec::from_elem(0, vla_1);
(*NSQ).sLTP_shp_buf_idx = psEncC.ltp_mem_length;
(*NSQ).sLTP_buf_idx = psEncC.ltp_mem_length;
(*NSQ).sLTP_shp_buf_idx = psEncC.ltp_mem_length as i32;
(*NSQ).sLTP_buf_idx = psEncC.ltp_mem_length as i32;
pxq = &mut *((*NSQ).xq)
.as_mut_ptr()
.offset(psEncC.ltp_mem_length as isize) as *mut i16;
k = 0;
while k < psEncC.nb_subfr {
while k < psEncC.nb_subfr as i32 {
A_Q12 = &*PredCoef_Q12
.offset(((k >> 1 | 1 - LSF_interpolation_flag) * MAX_LPC_ORDER) as isize)
.offset(((k >> 1 | 1 - LSF_interpolation_flag) * MAX_LPC_ORDER as i32) as isize)
as *const i16;
B_Q14 = &*LTPCoef_Q14.offset((k * LTP_ORDER) as isize) as *const i16;
B_Q14 = &*LTPCoef_Q14.offset((k * LTP_ORDER as i32) as isize) as *const i16;
AR_shp_Q13 = &*AR_Q13.offset((k * MAX_SHAPE_LPC_ORDER) as isize) as *const i16;
HarmShapeFIRPacked_Q14 = *HarmShapeGain_Q14.offset(k as isize) >> 2;
HarmShapeFIRPacked_Q14 |=
Expand All @@ -171,17 +172,19 @@ pub unsafe fn silk_NSQ_c(
if (*psIndices).signalType as i32 == TYPE_VOICED {
lag = *pitchL.offset(k as isize);
if k & 3 - ((LSF_interpolation_flag as u32) << 1) as i32 == 0 {
start_idx =
psEncC.ltp_mem_length - lag - psEncC.predictLPCOrder - LTP_ORDER / 2;
start_idx = psEncC.ltp_mem_length as i32
- lag
- psEncC.predictLPCOrder
- LTP_ORDER as i32 / 2;
assert!(start_idx > 0);
silk_LPC_analysis_filter(
&mut sLTP[start_idx as usize..psEncC.ltp_mem_length as usize],
&(*NSQ).xq[(start_idx + k * psEncC.subfr_length) as usize..]
[..(psEncC.ltp_mem_length - start_idx) as usize],
&(*NSQ).xq[(start_idx + k * psEncC.subfr_length as i32) as usize..]
[..(psEncC.ltp_mem_length - start_idx as usize) as usize],
std::slice::from_raw_parts(A_Q12, psEncC.predictLPCOrder as usize),
);
(*NSQ).rewhite_flag = 1;
(*NSQ).sLTP_buf_idx = psEncC.ltp_mem_length;
(*NSQ).sLTP_buf_idx = psEncC.ltp_mem_length as i32;
}
}
silk_nsq_scale_states(
Expand Down Expand Up @@ -214,7 +217,7 @@ pub unsafe fn silk_NSQ_c(
*Gains_Q16.offset(k as isize),
Lambda_Q10,
offset_Q10,
psEncC.subfr_length,
psEncC.subfr_length as i32,
psEncC.shapingLPCOrder,
psEncC.predictLPCOrder,
psEncC.arch,
Expand All @@ -229,16 +232,14 @@ pub unsafe fn silk_NSQ_c(
((*NSQ).xq).as_mut_ptr() as *mut core::ffi::c_void,
&mut *((*NSQ).xq)
.as_mut_ptr()
.offset(psEncC.frame_length as isize) as *mut i16
as *const core::ffi::c_void,
.offset(psEncC.frame_length as isize) as *mut i16 as *const core::ffi::c_void,
(psEncC.ltp_mem_length as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
);
memmove(
((*NSQ).sLTP_shp_Q14).as_mut_ptr() as *mut core::ffi::c_void,
&mut *((*NSQ).sLTP_shp_Q14)
.as_mut_ptr()
.offset(psEncC.frame_length as isize) as *mut i32
as *const core::ffi::c_void,
.offset(psEncC.frame_length as isize) as *mut i32 as *const core::ffi::c_void,
(psEncC.ltp_mem_length as u64).wrapping_mul(::core::mem::size_of::<i32>() as u64),
);
}
Expand Down Expand Up @@ -292,16 +293,16 @@ unsafe fn silk_noise_shape_quantizer(
.as_mut_ptr()
.offset(((*NSQ).sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2) as isize)
as *mut i32;
pred_lag_ptr =
&mut *sLTP_Q15.offset(((*NSQ).sLTP_buf_idx - lag + LTP_ORDER / 2) as isize) as *mut i32;
pred_lag_ptr = &mut *sLTP_Q15
.offset(((*NSQ).sLTP_buf_idx - lag + LTP_ORDER as i32 / 2) as isize)
as *mut i32;
Gain_Q10 = Gain_Q16 >> 6;
psLPC_Q14 = &mut *((*NSQ).sLPC_Q14)
.as_mut_ptr()
.offset((NSQ_LPC_BUF_LENGTH - 1) as isize) as *mut i32;
i = 0;
while i < length {
(*NSQ).rand_seed =
907633515_u32.wrapping_add(((*NSQ).rand_seed as u32).wrapping_mul(196314165)) as i32;
(*NSQ).rand_seed = silk_RAND((*NSQ).rand_seed);
LPC_pred_Q10 =
silk_noise_shape_quantizer_short_prediction_c(psLPC_Q14, a_Q12, predictLPCOrder);
if signalType == TYPE_VOICED {
Expand Down Expand Up @@ -518,7 +519,7 @@ unsafe fn silk_nsq_scale_states(
(inv_gain_Q31 >> 5 - 1) + 1 >> 1
};
i = 0;
while i < psEncC.subfr_length {
while i < psEncC.subfr_length as i32 {
*x_sc_Q10.offset(i as isize) =
(*x16.offset(i as isize) as i64 * inv_gain_Q26 as i64 >> 16) as i32;
i += 1;
Expand All @@ -529,7 +530,7 @@ unsafe fn silk_nsq_scale_states(
as u32)
<< 2) as i32;
}
i = (*NSQ).sLTP_buf_idx - lag - LTP_ORDER / 2;
i = (*NSQ).sLTP_buf_idx - lag - LTP_ORDER as i32 / 2;
while i < (*NSQ).sLTP_buf_idx {
*sLTP_Q15.offset(i as isize) =
(inv_gain_Q31 as i64 * *sLTP.offset(i as isize) as i64 >> 16) as i32;
Expand All @@ -538,14 +539,14 @@ unsafe fn silk_nsq_scale_states(
}
if *Gains_Q16.offset(subfr as isize) != (*NSQ).prev_gain_Q16 {
gain_adj_Q16 = silk_DIV32_varQ((*NSQ).prev_gain_Q16, *Gains_Q16.offset(subfr as isize), 16);
i = (*NSQ).sLTP_shp_buf_idx - psEncC.ltp_mem_length;
i = (*NSQ).sLTP_shp_buf_idx - psEncC.ltp_mem_length as i32;
while i < (*NSQ).sLTP_shp_buf_idx {
(*NSQ).sLTP_shp_Q14[i as usize] =
(gain_adj_Q16 as i64 * (*NSQ).sLTP_shp_Q14[i as usize] as i64 >> 16) as i32;
i += 1;
}
if signal_type == TYPE_VOICED && (*NSQ).rewhite_flag == 0 {
i = (*NSQ).sLTP_buf_idx - lag - LTP_ORDER / 2;
i = (*NSQ).sLTP_buf_idx - lag - LTP_ORDER as i32 / 2;
while i < (*NSQ).sLTP_buf_idx {
*sLTP_Q15.offset(i as isize) =
(gain_adj_Q16 as i64 * *sLTP_Q15.offset(i as isize) as i64 >> 16) as i32;
Expand All @@ -555,7 +556,7 @@ unsafe fn silk_nsq_scale_states(
(*NSQ).sLF_AR_shp_Q14 = (gain_adj_Q16 as i64 * (*NSQ).sLF_AR_shp_Q14 as i64 >> 16) as i32;
(*NSQ).sDiff_shp_Q14 = (gain_adj_Q16 as i64 * (*NSQ).sDiff_shp_Q14 as i64 >> 16) as i32;
i = 0;
while i < NSQ_LPC_BUF_LENGTH {
while i < NSQ_LPC_BUF_LENGTH as i32 {
(*NSQ).sLPC_Q14[i as usize] =
(gain_adj_Q16 as i64 * (*NSQ).sLPC_Q14[i as usize] as i64 >> 16) as i32;
i += 1;
Expand Down
Loading

0 comments on commit c94435f

Please sign in to comment.