Skip to content

Commit c595d26

Browse files
committed
refactor: make silk_decode_parameters safe
1 parent 78fc252 commit c595d26

File tree

4 files changed

+107
-90
lines changed

4 files changed

+107
-90
lines changed

src/silk/PLC.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ unsafe fn silk_PLC_update(psDec: &mut silk_decoder_state, psDecCtrl: *mut silk_d
6868
break;
6969
}
7070
temp_LTP_Gain_Q14 = 0;
71-
i = 0;
72-
while i < LTP_ORDER {
71+
let mut i = 0;
72+
while i < LTP_ORDER as usize {
7373
temp_LTP_Gain_Q14 += (*psDecCtrl).LTPCoef_Q14
74-
[((psDec.nb_subfr - 1 - j) * LTP_ORDER + i) as usize]
74+
[(psDec.nb_subfr - 1 - j) as usize * LTP_ORDER as usize + i]
7575
as i32;
7676
i += 1;
7777
}

src/silk/decode_frame.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ pub unsafe fn silk_decode_frame(
2222
let mut mv_len: i32 = 0;
2323
let ret: i32 = 0;
2424
L = psDec.frame_length;
25-
let mut psDecCtrl: [silk_decoder_control; 1] = [silk_decoder_control {
25+
let mut psDecCtrl = silk_decoder_control {
2626
pitchL: [0; 4],
2727
Gains_Q16: [0; 4],
2828
PredCoef_Q12: [[0; 16]; 2],
2929
LTPCoef_Q14: [0; 20],
3030
LTP_scale_Q14: 0,
31-
}; 1];
32-
(*psDecCtrl.as_mut_ptr()).LTP_scale_Q14 = 0;
31+
};
32+
psDecCtrl.LTP_scale_Q14 = 0;
3333
assert!(L > 0 && L <= 5 * 4 * 16);
3434
if lostFlag == FLAG_DECODE_NORMAL
35-
|| lostFlag == FLAG_DECODE_LBRR
36-
&& psDec.LBRR_flags[psDec.nFramesDecoded as usize] == 1
35+
|| lostFlag == FLAG_DECODE_LBRR && psDec.LBRR_flags[psDec.nFramesDecoded as usize] == 1
3736
{
3837
let vla = (L + 16 - 1 & !(16 - 1)) as usize;
3938
let mut pulses: Vec<i16> = ::std::vec::from_elem(0, vla);
@@ -51,22 +50,22 @@ pub unsafe fn silk_decode_frame(
5150
psDec.indices.quantOffsetType as i32,
5251
psDec.frame_length,
5352
);
54-
silk_decode_parameters(psDec, psDecCtrl.as_mut_ptr(), condCoding);
53+
silk_decode_parameters(psDec, &mut psDecCtrl, condCoding);
5554
silk_decode_core(
5655
psDec,
57-
psDecCtrl.as_mut_ptr(),
56+
&mut psDecCtrl,
5857
pOut,
5958
pulses.as_mut_ptr() as *const i16,
6059
arch,
6160
);
62-
silk_PLC(psDec, psDecCtrl.as_mut_ptr(), pOut, 0, arch);
61+
silk_PLC(psDec, &mut psDecCtrl, pOut, 0, arch);
6362
psDec.lossCnt = 0;
6463
psDec.prevSignalType = psDec.indices.signalType as i32;
6564
assert!(psDec.prevSignalType >= 0 && psDec.prevSignalType <= 2);
6665
psDec.first_frame_after_reset = 0;
6766
} else {
6867
psDec.indices.signalType = psDec.prevSignalType as i8;
69-
silk_PLC(psDec, psDecCtrl.as_mut_ptr(), pOut, 1, arch);
68+
silk_PLC(psDec, &mut psDecCtrl, pOut, 1, arch);
7069
}
7170
assert!(psDec.ltp_mem_length >= psDec.frame_length);
7271
mv_len = psDec.ltp_mem_length - psDec.frame_length;
@@ -83,9 +82,9 @@ pub unsafe fn silk_decode_frame(
8382
pOut as *const core::ffi::c_void,
8483
(psDec.frame_length as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
8584
);
86-
silk_CNG(psDec, psDecCtrl.as_mut_ptr(), pOut, L);
85+
silk_CNG(psDec, &mut psDecCtrl, pOut, L);
8786
silk_PLC_glue_frames(psDec, pOut, L);
88-
psDec.lagPrev = (*psDecCtrl.as_mut_ptr()).pitchL[(psDec.nb_subfr - 1) as usize];
87+
psDec.lagPrev = psDecCtrl.pitchL[(psDec.nb_subfr - 1) as usize];
8988
*pN = L;
9089
return ret;
9190
}

src/silk/decode_parameters.rs

Lines changed: 89 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::externs::{memcpy, memset};
21
use crate::silk::bwexpander::silk_bwexpander;
32
use crate::silk::decode_pitch::silk_decode_pitch;
43
use crate::silk::define::{BWE_AFTER_LOSS_Q16, CODE_CONDITIONALLY, LTP_ORDER, TYPE_VOICED};
@@ -9,103 +8,121 @@ use crate::silk::tables_other::silk_LTPScales_table_Q14;
98
use crate::silk::NLSF_decode::silk_NLSF_decode;
109
use crate::silk::NLSF2A::silk_NLSF2A;
1110

12-
pub unsafe fn silk_decode_parameters(
11+
/// Decode parameters from payload
12+
///
13+
/// ```text
14+
/// psDec I/O State
15+
/// psDecCtrl I/O Decoder control
16+
/// condCoding I The type of conditional coding to use
17+
/// ```
18+
pub fn silk_decode_parameters(
1319
psDec: &mut silk_decoder_state,
14-
psDecCtrl: *mut silk_decoder_control,
20+
psDecCtrl: &mut silk_decoder_control,
1521
condCoding: i32,
1622
) {
17-
let mut i: i32 = 0;
18-
let mut k: i32 = 0;
19-
let mut Ix: i32 = 0;
20-
let mut pNLSF_Q15: [i16; 16] = [0; 16];
21-
let mut pNLSF0_Q15: [i16; 16] = [0; 16];
22-
let mut cbk_ptr_Q7: *const i8 = 0 as *const i8;
23+
let [PredCoef_Q12_0, PredCoef_Q12_1] = &mut psDecCtrl.PredCoef_Q12;
24+
let PredCoef_Q12_0 = &mut PredCoef_Q12_0[..psDec.LPC_order as usize];
25+
let PredCoef_Q12_1 = &mut PredCoef_Q12_1[..psDec.LPC_order as usize];
26+
27+
let Gains_Q16 = &mut psDecCtrl.Gains_Q16[..psDec.nb_subfr as usize];
28+
let GainsIndices = &psDec.indices.GainsIndices[..psDec.nb_subfr as usize];
29+
30+
let NLSFIndices = &psDec.indices.NLSFIndices[..psDec.psNLSF_CB.order as usize + 1];
31+
32+
let prevNLSF_Q15 = &mut psDec.prevNLSF_Q15[..psDec.LPC_order as usize];
33+
34+
let pitchL = &mut psDecCtrl.pitchL[..psDec.nb_subfr as usize];
35+
36+
let LTPCoef_Q14 = &mut psDecCtrl.LTPCoef_Q14[..psDec.nb_subfr as usize * LTP_ORDER as usize];
37+
38+
/* Dequant Gains */
2339
silk_gains_dequant(
24-
&mut ((*psDecCtrl).Gains_Q16)[..psDec.nb_subfr as usize],
25-
&psDec.indices.GainsIndices[..psDec.nb_subfr as usize],
40+
Gains_Q16,
41+
GainsIndices,
2642
&mut psDec.LastGainIndex,
2743
condCoding == CODE_CONDITIONALLY,
2844
);
29-
silk_NLSF_decode(
30-
&mut pNLSF_Q15[..psDec.psNLSF_CB.order as usize],
31-
&psDec.indices.NLSFIndices[..psDec.psNLSF_CB.order as usize + 1],
32-
psDec.psNLSF_CB,
33-
);
34-
silk_NLSF2A(
35-
&mut (*psDecCtrl).PredCoef_Q12[1][..psDec.LPC_order as usize],
36-
&pNLSF_Q15[..psDec.LPC_order as usize],
37-
);
45+
46+
/****************/
47+
/* Decode NLSFs */
48+
/****************/
49+
let mut pNLSF_Q15: [i16; 16] = [0; 16];
50+
let pNLSF_Q15 = &mut pNLSF_Q15[..psDec.LPC_order as usize];
51+
silk_NLSF_decode(pNLSF_Q15, NLSFIndices, psDec.psNLSF_CB);
52+
53+
/* Convert NLSF parameters to AR prediction filter coefficients */
54+
silk_NLSF2A(PredCoef_Q12_1, pNLSF_Q15);
55+
56+
/* If just reset, e.g., because internal Fs changed, do not allow interpolation */
57+
/* improves the case of packet loss in the first frame after a switch */
3858
if psDec.first_frame_after_reset == 1 {
3959
psDec.indices.NLSFInterpCoef_Q2 = 4;
4060
}
4161
if (psDec.indices.NLSFInterpCoef_Q2 as i32) < 4 {
42-
i = 0;
43-
while i < psDec.LPC_order {
44-
pNLSF0_Q15[i as usize] = (psDec.prevNLSF_Q15[i as usize] as i32
45-
+ (psDec.indices.NLSFInterpCoef_Q2 as i32
46-
* (pNLSF_Q15[i as usize] as i32 - psDec.prevNLSF_Q15[i as usize] as i32)
62+
/* Calculation of the interpolated NLSF0 vector from the interpolation factor, */
63+
/* the previous NLSF1, and the current NLSF1 */
64+
let mut pNLSF0_Q15: [i16; 16] = [0; 16];
65+
let pNLSF0_Q15 = &mut pNLSF0_Q15[..psDec.LPC_order as usize];
66+
67+
for i in 0..psDec.LPC_order as usize {
68+
pNLSF0_Q15[i] = (prevNLSF_Q15[i] as i32
69+
+ ((psDec.indices.NLSFInterpCoef_Q2 as i32
70+
* (pNLSF_Q15[i] as i32 - prevNLSF_Q15[i] as i32))
4771
>> 2)) as i16;
48-
i += 1;
4972
}
50-
silk_NLSF2A(
51-
&mut (*psDecCtrl).PredCoef_Q12[0][..psDec.LPC_order as usize],
52-
&pNLSF0_Q15[..psDec.LPC_order as usize],
53-
);
73+
74+
/* Convert NLSF parameters to AR prediction filter coefficients */
75+
silk_NLSF2A(PredCoef_Q12_0, pNLSF0_Q15);
5476
} else {
55-
memcpy(
56-
((*psDecCtrl).PredCoef_Q12[0 as usize]).as_mut_ptr() as *mut core::ffi::c_void,
57-
((*psDecCtrl).PredCoef_Q12[1 as usize]).as_mut_ptr() as *const core::ffi::c_void,
58-
(psDec.LPC_order as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
59-
);
77+
/* Copy LPC coefficients for first half from second half */
78+
PredCoef_Q12_0.copy_from_slice(PredCoef_Q12_1);
6079
}
61-
memcpy(
62-
(psDec.prevNLSF_Q15).as_mut_ptr() as *mut core::ffi::c_void,
63-
pNLSF_Q15.as_mut_ptr() as *const core::ffi::c_void,
64-
(psDec.LPC_order as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
65-
);
80+
81+
prevNLSF_Q15[..psDec.LPC_order as usize]
82+
.copy_from_slice(&pNLSF_Q15[..psDec.LPC_order as usize]);
83+
84+
/* After a packet loss do BWE of LPC coefs */
6685
if psDec.lossCnt != 0 {
67-
silk_bwexpander(
68-
&mut (*psDecCtrl).PredCoef_Q12[0][..psDec.LPC_order as usize],
69-
BWE_AFTER_LOSS_Q16,
70-
);
71-
silk_bwexpander(
72-
&mut (*psDecCtrl).PredCoef_Q12[1][..psDec.LPC_order as usize],
73-
BWE_AFTER_LOSS_Q16,
74-
);
86+
silk_bwexpander(PredCoef_Q12_0, BWE_AFTER_LOSS_Q16);
87+
silk_bwexpander(PredCoef_Q12_1, BWE_AFTER_LOSS_Q16);
7588
}
89+
7690
if psDec.indices.signalType as i32 == TYPE_VOICED {
91+
/*********************/
92+
/* Decode pitch lags */
93+
/*********************/
94+
95+
/* Decode pitch values */
7796
silk_decode_pitch(
7897
psDec.indices.lagIndex,
7998
psDec.indices.contourIndex,
80-
&mut (*psDecCtrl).pitchL[..psDec.nb_subfr as usize],
99+
pitchL,
81100
psDec.fs_kHz,
82101
);
83-
cbk_ptr_Q7 = &*(*silk_LTP_vq_ptrs_Q7[psDec.indices.PERIndex as usize].as_ptr()).as_ptr();
84-
k = 0;
85-
while k < psDec.nb_subfr {
86-
Ix = psDec.indices.LTPIndex[k as usize] as i32;
87-
i = 0;
88-
while i < LTP_ORDER {
89-
(*psDecCtrl).LTPCoef_Q14[(k * LTP_ORDER + i) as usize] =
90-
((*cbk_ptr_Q7.offset((Ix * 5 + i) as isize) as u32) << 7) as i32 as i16;
91-
i += 1;
102+
103+
/* Decode Codebook Index */
104+
let cbk_ptr_Q7 = silk_LTP_vq_ptrs_Q7[psDec.indices.PERIndex as usize];
105+
106+
for k in 0..psDec.nb_subfr as usize {
107+
let Ix = psDec.indices.LTPIndex[k] as usize;
108+
for i in 0..LTP_ORDER as usize {
109+
// ugh, I tried making it into a 2D array, but stuff broke
110+
// no idea why
111+
// LTPCoef_Q14[k * LTP_ORDER as usize + i] = (cbk_ptr_Q7[Ix][i] as i16) << 7;
112+
LTPCoef_Q14[k * LTP_ORDER as usize + i] = (cbk_ptr_Q7[Ix][i] as i16) << 7;
92113
}
93-
k += 1;
94114
}
95-
Ix = psDec.indices.LTP_scaleIndex as i32;
96-
(*psDecCtrl).LTP_scale_Q14 = silk_LTPScales_table_Q14[Ix as usize] as i32;
115+
116+
/**********************/
117+
/* Decode LTP scaling */
118+
/**********************/
119+
let Ix = psDec.indices.LTP_scaleIndex as usize;
120+
psDecCtrl.LTP_scale_Q14 = silk_LTPScales_table_Q14[Ix] as i32;
97121
} else {
98-
memset(
99-
((*psDecCtrl).pitchL).as_mut_ptr() as *mut core::ffi::c_void,
100-
0,
101-
(psDec.nb_subfr as u64).wrapping_mul(::core::mem::size_of::<i32>() as u64),
102-
);
103-
memset(
104-
((*psDecCtrl).LTPCoef_Q14).as_mut_ptr() as *mut core::ffi::c_void,
105-
0,
106-
((5 * psDec.nb_subfr) as u64).wrapping_mul(::core::mem::size_of::<i16>() as u64),
107-
);
122+
pitchL.fill(0);
123+
LTPCoef_Q14.fill(0);
124+
108125
psDec.indices.PERIndex = 0;
109-
(*psDecCtrl).LTP_scale_Q14 = 0;
126+
psDecCtrl.LTP_scale_Q14 = 0;
110127
};
111128
}

src/silk/structs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::silk::define::{LTP_ORDER, MAX_LPC_ORDER, MAX_NB_SUBFR};
12
use crate::silk::resampler::ResamplerState;
23

34
#[derive(Copy, Clone)]
@@ -111,10 +112,10 @@ pub struct silk_decoder_state {
111112
#[derive(Copy, Clone)]
112113
#[repr(C)]
113114
pub struct silk_decoder_control {
114-
pub pitchL: [i32; 4],
115-
pub Gains_Q16: [i32; 4],
116-
pub PredCoef_Q12: [[i16; 16]; 2],
117-
pub LTPCoef_Q14: [i16; 20],
115+
pub pitchL: [i32; MAX_NB_SUBFR as usize],
116+
pub Gains_Q16: [i32; MAX_NB_SUBFR as usize],
117+
pub PredCoef_Q12: [[i16; MAX_LPC_ORDER as usize]; 2],
118+
pub LTPCoef_Q14: [i16; LTP_ORDER as usize * MAX_NB_SUBFR as usize],
118119
pub LTP_scale_Q14: i32,
119120
}
120121

0 commit comments

Comments
 (0)