Skip to content

Commit

Permalink
refactor: make silk_decode_parameters safe
Browse files Browse the repository at this point in the history
  • Loading branch information
DCNick3 committed Jul 4, 2024
1 parent 78fc252 commit c595d26
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 90 deletions.
6 changes: 3 additions & 3 deletions src/silk/PLC.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ unsafe fn silk_PLC_update(psDec: &mut silk_decoder_state, psDecCtrl: *mut silk_d
break;
}
temp_LTP_Gain_Q14 = 0;
i = 0;
while i < LTP_ORDER {
let mut i = 0;
while i < LTP_ORDER as usize {
temp_LTP_Gain_Q14 += (*psDecCtrl).LTPCoef_Q14
[((psDec.nb_subfr - 1 - j) * LTP_ORDER + i) as usize]
[(psDec.nb_subfr - 1 - j) as usize * LTP_ORDER as usize + i]
as i32;
i += 1;
}
Expand Down
21 changes: 10 additions & 11 deletions src/silk/decode_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ pub unsafe fn silk_decode_frame(
let mut mv_len: i32 = 0;
let ret: i32 = 0;
L = psDec.frame_length;
let mut psDecCtrl: [silk_decoder_control; 1] = [silk_decoder_control {
let mut psDecCtrl = silk_decoder_control {
pitchL: [0; 4],
Gains_Q16: [0; 4],
PredCoef_Q12: [[0; 16]; 2],
LTPCoef_Q14: [0; 20],
LTP_scale_Q14: 0,
}; 1];
(*psDecCtrl.as_mut_ptr()).LTP_scale_Q14 = 0;
};
psDecCtrl.LTP_scale_Q14 = 0;
assert!(L > 0 && L <= 5 * 4 * 16);
if lostFlag == FLAG_DECODE_NORMAL
|| lostFlag == FLAG_DECODE_LBRR
&& psDec.LBRR_flags[psDec.nFramesDecoded as usize] == 1
|| lostFlag == FLAG_DECODE_LBRR && psDec.LBRR_flags[psDec.nFramesDecoded as usize] == 1
{
let vla = (L + 16 - 1 & !(16 - 1)) as usize;
let mut pulses: Vec<i16> = ::std::vec::from_elem(0, vla);
Expand All @@ -51,22 +50,22 @@ pub unsafe fn silk_decode_frame(
psDec.indices.quantOffsetType as i32,
psDec.frame_length,
);
silk_decode_parameters(psDec, psDecCtrl.as_mut_ptr(), condCoding);
silk_decode_parameters(psDec, &mut psDecCtrl, condCoding);
silk_decode_core(
psDec,
psDecCtrl.as_mut_ptr(),
&mut psDecCtrl,
pOut,
pulses.as_mut_ptr() as *const i16,
arch,
);
silk_PLC(psDec, psDecCtrl.as_mut_ptr(), pOut, 0, arch);
silk_PLC(psDec, &mut psDecCtrl, pOut, 0, arch);
psDec.lossCnt = 0;
psDec.prevSignalType = psDec.indices.signalType as i32;
assert!(psDec.prevSignalType >= 0 && psDec.prevSignalType <= 2);
psDec.first_frame_after_reset = 0;
} else {
psDec.indices.signalType = psDec.prevSignalType as i8;
silk_PLC(psDec, psDecCtrl.as_mut_ptr(), pOut, 1, arch);
silk_PLC(psDec, &mut psDecCtrl, pOut, 1, arch);
}
assert!(psDec.ltp_mem_length >= psDec.frame_length);
mv_len = psDec.ltp_mem_length - psDec.frame_length;
Expand All @@ -83,9 +82,9 @@ pub unsafe fn silk_decode_frame(
pOut as *const core::ffi::c_void,
(psDec.frame_length as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
);
silk_CNG(psDec, psDecCtrl.as_mut_ptr(), pOut, L);
silk_CNG(psDec, &mut psDecCtrl, pOut, L);
silk_PLC_glue_frames(psDec, pOut, L);
psDec.lagPrev = (*psDecCtrl.as_mut_ptr()).pitchL[(psDec.nb_subfr - 1) as usize];
psDec.lagPrev = psDecCtrl.pitchL[(psDec.nb_subfr - 1) as usize];
*pN = L;
return ret;
}
161 changes: 89 additions & 72 deletions src/silk/decode_parameters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::externs::{memcpy, memset};
use crate::silk::bwexpander::silk_bwexpander;
use crate::silk::decode_pitch::silk_decode_pitch;
use crate::silk::define::{BWE_AFTER_LOSS_Q16, CODE_CONDITIONALLY, LTP_ORDER, TYPE_VOICED};
Expand All @@ -9,103 +8,121 @@ use crate::silk::tables_other::silk_LTPScales_table_Q14;
use crate::silk::NLSF_decode::silk_NLSF_decode;
use crate::silk::NLSF2A::silk_NLSF2A;

pub unsafe fn silk_decode_parameters(
/// Decode parameters from payload
///
/// ```text
/// psDec I/O State
/// psDecCtrl I/O Decoder control
/// condCoding I The type of conditional coding to use
/// ```
pub fn silk_decode_parameters(
psDec: &mut silk_decoder_state,
psDecCtrl: *mut silk_decoder_control,
psDecCtrl: &mut silk_decoder_control,
condCoding: i32,
) {
let mut i: i32 = 0;
let mut k: i32 = 0;
let mut Ix: i32 = 0;
let mut pNLSF_Q15: [i16; 16] = [0; 16];
let mut pNLSF0_Q15: [i16; 16] = [0; 16];
let mut cbk_ptr_Q7: *const i8 = 0 as *const i8;
let [PredCoef_Q12_0, PredCoef_Q12_1] = &mut psDecCtrl.PredCoef_Q12;
let PredCoef_Q12_0 = &mut PredCoef_Q12_0[..psDec.LPC_order as usize];
let PredCoef_Q12_1 = &mut PredCoef_Q12_1[..psDec.LPC_order as usize];

let Gains_Q16 = &mut psDecCtrl.Gains_Q16[..psDec.nb_subfr as usize];
let GainsIndices = &psDec.indices.GainsIndices[..psDec.nb_subfr as usize];

let NLSFIndices = &psDec.indices.NLSFIndices[..psDec.psNLSF_CB.order as usize + 1];

let prevNLSF_Q15 = &mut psDec.prevNLSF_Q15[..psDec.LPC_order as usize];

let pitchL = &mut psDecCtrl.pitchL[..psDec.nb_subfr as usize];

let LTPCoef_Q14 = &mut psDecCtrl.LTPCoef_Q14[..psDec.nb_subfr as usize * LTP_ORDER as usize];

/* Dequant Gains */
silk_gains_dequant(
&mut ((*psDecCtrl).Gains_Q16)[..psDec.nb_subfr as usize],
&psDec.indices.GainsIndices[..psDec.nb_subfr as usize],
Gains_Q16,
GainsIndices,
&mut psDec.LastGainIndex,
condCoding == CODE_CONDITIONALLY,
);
silk_NLSF_decode(
&mut pNLSF_Q15[..psDec.psNLSF_CB.order as usize],
&psDec.indices.NLSFIndices[..psDec.psNLSF_CB.order as usize + 1],
psDec.psNLSF_CB,
);
silk_NLSF2A(
&mut (*psDecCtrl).PredCoef_Q12[1][..psDec.LPC_order as usize],
&pNLSF_Q15[..psDec.LPC_order as usize],
);

/****************/
/* Decode NLSFs */
/****************/
let mut pNLSF_Q15: [i16; 16] = [0; 16];
let pNLSF_Q15 = &mut pNLSF_Q15[..psDec.LPC_order as usize];
silk_NLSF_decode(pNLSF_Q15, NLSFIndices, psDec.psNLSF_CB);

/* Convert NLSF parameters to AR prediction filter coefficients */
silk_NLSF2A(PredCoef_Q12_1, pNLSF_Q15);

/* If just reset, e.g., because internal Fs changed, do not allow interpolation */
/* improves the case of packet loss in the first frame after a switch */
if psDec.first_frame_after_reset == 1 {
psDec.indices.NLSFInterpCoef_Q2 = 4;
}
if (psDec.indices.NLSFInterpCoef_Q2 as i32) < 4 {
i = 0;
while i < psDec.LPC_order {
pNLSF0_Q15[i as usize] = (psDec.prevNLSF_Q15[i as usize] as i32
+ (psDec.indices.NLSFInterpCoef_Q2 as i32
* (pNLSF_Q15[i as usize] as i32 - psDec.prevNLSF_Q15[i as usize] as i32)
/* Calculation of the interpolated NLSF0 vector from the interpolation factor, */
/* the previous NLSF1, and the current NLSF1 */
let mut pNLSF0_Q15: [i16; 16] = [0; 16];
let pNLSF0_Q15 = &mut pNLSF0_Q15[..psDec.LPC_order as usize];

for i in 0..psDec.LPC_order as usize {
pNLSF0_Q15[i] = (prevNLSF_Q15[i] as i32
+ ((psDec.indices.NLSFInterpCoef_Q2 as i32
* (pNLSF_Q15[i] as i32 - prevNLSF_Q15[i] as i32))
>> 2)) as i16;
i += 1;
}
silk_NLSF2A(
&mut (*psDecCtrl).PredCoef_Q12[0][..psDec.LPC_order as usize],
&pNLSF0_Q15[..psDec.LPC_order as usize],
);

/* Convert NLSF parameters to AR prediction filter coefficients */
silk_NLSF2A(PredCoef_Q12_0, pNLSF0_Q15);
} else {
memcpy(
((*psDecCtrl).PredCoef_Q12[0 as usize]).as_mut_ptr() as *mut core::ffi::c_void,
((*psDecCtrl).PredCoef_Q12[1 as usize]).as_mut_ptr() as *const core::ffi::c_void,
(psDec.LPC_order as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
);
/* Copy LPC coefficients for first half from second half */
PredCoef_Q12_0.copy_from_slice(PredCoef_Q12_1);
}
memcpy(
(psDec.prevNLSF_Q15).as_mut_ptr() as *mut core::ffi::c_void,
pNLSF_Q15.as_mut_ptr() as *const core::ffi::c_void,
(psDec.LPC_order as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
);

prevNLSF_Q15[..psDec.LPC_order as usize]
.copy_from_slice(&pNLSF_Q15[..psDec.LPC_order as usize]);

/* After a packet loss do BWE of LPC coefs */
if psDec.lossCnt != 0 {
silk_bwexpander(
&mut (*psDecCtrl).PredCoef_Q12[0][..psDec.LPC_order as usize],
BWE_AFTER_LOSS_Q16,
);
silk_bwexpander(
&mut (*psDecCtrl).PredCoef_Q12[1][..psDec.LPC_order as usize],
BWE_AFTER_LOSS_Q16,
);
silk_bwexpander(PredCoef_Q12_0, BWE_AFTER_LOSS_Q16);
silk_bwexpander(PredCoef_Q12_1, BWE_AFTER_LOSS_Q16);
}

if psDec.indices.signalType as i32 == TYPE_VOICED {
/*********************/
/* Decode pitch lags */
/*********************/

/* Decode pitch values */
silk_decode_pitch(
psDec.indices.lagIndex,
psDec.indices.contourIndex,
&mut (*psDecCtrl).pitchL[..psDec.nb_subfr as usize],
pitchL,
psDec.fs_kHz,
);
cbk_ptr_Q7 = &*(*silk_LTP_vq_ptrs_Q7[psDec.indices.PERIndex as usize].as_ptr()).as_ptr();
k = 0;
while k < psDec.nb_subfr {
Ix = psDec.indices.LTPIndex[k as usize] as i32;
i = 0;
while i < LTP_ORDER {
(*psDecCtrl).LTPCoef_Q14[(k * LTP_ORDER + i) as usize] =
((*cbk_ptr_Q7.offset((Ix * 5 + i) as isize) as u32) << 7) as i32 as i16;
i += 1;

/* Decode Codebook Index */
let cbk_ptr_Q7 = silk_LTP_vq_ptrs_Q7[psDec.indices.PERIndex as usize];

for k in 0..psDec.nb_subfr as usize {
let Ix = psDec.indices.LTPIndex[k] as usize;
for i in 0..LTP_ORDER as usize {
// ugh, I tried making it into a 2D array, but stuff broke
// no idea why
// LTPCoef_Q14[k * LTP_ORDER as usize + i] = (cbk_ptr_Q7[Ix][i] as i16) << 7;
LTPCoef_Q14[k * LTP_ORDER as usize + i] = (cbk_ptr_Q7[Ix][i] as i16) << 7;
}
k += 1;
}
Ix = psDec.indices.LTP_scaleIndex as i32;
(*psDecCtrl).LTP_scale_Q14 = silk_LTPScales_table_Q14[Ix as usize] as i32;

/**********************/
/* Decode LTP scaling */
/**********************/
let Ix = psDec.indices.LTP_scaleIndex as usize;
psDecCtrl.LTP_scale_Q14 = silk_LTPScales_table_Q14[Ix] as i32;
} else {
memset(
((*psDecCtrl).pitchL).as_mut_ptr() as *mut core::ffi::c_void,
0,
(psDec.nb_subfr as u64).wrapping_mul(::core::mem::size_of::<i32>() as u64),
);
memset(
((*psDecCtrl).LTPCoef_Q14).as_mut_ptr() as *mut core::ffi::c_void,
0,
((5 * psDec.nb_subfr) as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
);
pitchL.fill(0);
LTPCoef_Q14.fill(0);

psDec.indices.PERIndex = 0;
(*psDecCtrl).LTP_scale_Q14 = 0;
psDecCtrl.LTP_scale_Q14 = 0;
};
}
9 changes: 5 additions & 4 deletions src/silk/structs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::silk::define::{LTP_ORDER, MAX_LPC_ORDER, MAX_NB_SUBFR};
use crate::silk::resampler::ResamplerState;

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -111,10 +112,10 @@ pub struct silk_decoder_state {
#[derive(Copy, Clone)]
#[repr(C)]
pub struct silk_decoder_control {
pub pitchL: [i32; 4],
pub Gains_Q16: [i32; 4],
pub PredCoef_Q12: [[i16; 16]; 2],
pub LTPCoef_Q14: [i16; 20],
pub pitchL: [i32; MAX_NB_SUBFR as usize],
pub Gains_Q16: [i32; MAX_NB_SUBFR as usize],
pub PredCoef_Q12: [[i16; MAX_LPC_ORDER as usize]; 2],
pub LTPCoef_Q14: [i16; LTP_ORDER as usize * MAX_NB_SUBFR as usize],
pub LTP_scale_Q14: i32,
}

Expand Down

0 comments on commit c595d26

Please sign in to comment.