Skip to content

Commit

Permalink
refactor: make silk_stereo_decode_pred and silk_stereo_decode_mid_onl…
Browse files Browse the repository at this point in the history
…y safe
  • Loading branch information
DCNick3 committed Jul 5, 2024
1 parent 5b255f9 commit 570fb0b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
22 changes: 11 additions & 11 deletions src/silk/dec_API.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ pub struct silk_decoder {
pub sStereo: stereo_dec_state,
pub nChannelsAPI: i32,
pub nChannelsInternal: i32,
pub prev_decode_only_middle: i32,
pub prev_decode_only_middle: bool,
}
pub fn silk_InitDecoder() -> silk_decoder {
silk_decoder {
channel_state: [silk_init_decoder(), silk_init_decoder()],
sStereo: stereo_dec_state::default(),
nChannelsAPI: 0,
nChannelsInternal: 0,
prev_decode_only_middle: 0,
prev_decode_only_middle: false,
}
}
pub unsafe fn silk_Decode(
Expand All @@ -66,7 +66,7 @@ pub unsafe fn silk_Decode(
) -> i32 {
let mut i: i32 = 0;
let mut n: i32 = 0;
let mut decode_only_middle: i32 = 0;
let mut decode_only_middle: bool = false;
let mut ret: i32 = SILK_NO_ERROR;
let mut nSamplesOutDec: i32 = 0;
let mut LBRR_symbol: i32 = 0;
Expand Down Expand Up @@ -203,7 +203,7 @@ pub unsafe fn silk_Decode(
let mut pulses: [i16; 320] = [0; 320];
let mut condCoding: i32 = 0;
if decControl.nChannelsInternal == 2 && n == 0 {
silk_stereo_decode_pred(psRangeDec, MS_pred_Q13.as_mut_ptr());
silk_stereo_decode_pred(psRangeDec, &mut MS_pred_Q13);
if channel_state[1].LBRR_flags[i as usize] == 0 {
silk_stereo_decode_mid_only(psRangeDec, &mut decode_only_middle);
}
Expand Down Expand Up @@ -247,15 +247,15 @@ pub unsafe fn silk_Decode(
|| lostFlag == FLAG_DECODE_LBRR
&& channel_state[0].LBRR_flags[channel_state[0].nFramesDecoded as usize] == 1
{
silk_stereo_decode_pred(psRangeDec, MS_pred_Q13.as_mut_ptr());
silk_stereo_decode_pred(psRangeDec, &mut MS_pred_Q13);
if lostFlag == FLAG_DECODE_NORMAL
&& channel_state[1].VAD_flags[channel_state[0].nFramesDecoded as usize] == 0
|| lostFlag == FLAG_DECODE_LBRR
&& channel_state[1].LBRR_flags[channel_state[0].nFramesDecoded as usize] == 0
{
silk_stereo_decode_mid_only(psRangeDec, &mut decode_only_middle);
} else {
decode_only_middle = 0;
decode_only_middle = false;
}
} else {
n = 0;
Expand All @@ -266,8 +266,8 @@ pub unsafe fn silk_Decode(
}
}
if decControl.nChannelsInternal == 2
&& decode_only_middle == 0
&& psDec.prev_decode_only_middle == 1
&& decode_only_middle == false
&& psDec.prev_decode_only_middle == true
{
memset(
(channel_state[1].outBuf).as_mut_ptr() as *mut core::ffi::c_void,
Expand Down Expand Up @@ -305,9 +305,9 @@ pub unsafe fn silk_Decode(
.offset(2 as isize);
}
if lostFlag == FLAG_DECODE_NORMAL {
has_side = (decode_only_middle == 0) as i32;
has_side = (decode_only_middle == false) as i32;
} else {
has_side = (psDec.prev_decode_only_middle == 0
has_side = (psDec.prev_decode_only_middle == false
|| decControl.nChannelsInternal == 2
&& lostFlag == FLAG_DECODE_LBRR
&& channel_state[1].LBRR_flags[channel_state[1].nFramesDecoded as usize] == 1)
Expand All @@ -328,7 +328,7 @@ pub unsafe fn silk_Decode(
} else {
CODE_INDEPENDENTLY
};
} else if n > 0 && psDec.prev_decode_only_middle != 0 {
} else if n > 0 && psDec.prev_decode_only_middle != false {
condCoding_0 = CODE_INDEPENDENTLY_NO_LTP_SCALING;
} else {
condCoding_0 = CODE_CONDITIONALLY;
Expand Down
68 changes: 44 additions & 24 deletions src/silk/stereo_decode_pred.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
use crate::celt::entdec::{ec_dec, ec_dec_icdf};
use crate::silk::define::STEREO_QUANT_SUB_STEPS;
use crate::silk::macros::silk_SMULWB;
use crate::silk::tables_other::{
silk_stereo_only_code_mid_iCDF, silk_stereo_pred_joint_iCDF, silk_stereo_pred_quant_Q13,
silk_uniform3_iCDF, silk_uniform5_iCDF,
};
use crate::silk::SigProc_FIX::SILK_FIX_CONST;

pub unsafe fn silk_stereo_decode_pred(psRangeDec: &mut ec_dec, pred_Q13: *mut i32) {
let mut n: i32 = 0;
let mut ix: [[i32; 3]; 2] = [[0; 3]; 2];
let mut low_Q13: i32 = 0;
let mut step_Q13: i32 = 0;
n = ec_dec_icdf(psRangeDec, &silk_stereo_pred_joint_iCDF, 8);
ix[0 as usize][2 as usize] = n / 5;
ix[1 as usize][2 as usize] = n - 5 * ix[0 as usize][2 as usize];
n = 0;
/// Decode mid/side predictors
///
/// ```text
/// psRangeDec I/O Compressor data structure
/// pred_Q13[] O Predictors
/// ```
pub fn silk_stereo_decode_pred(psRangeDec: &mut ec_dec, pred_Q13: &mut [i32; 2]) {
let n = ec_dec_icdf(psRangeDec, &silk_stereo_pred_joint_iCDF, 8) as usize;

let mut ix: [[usize; 3]; 2] = [[0; 3]; 2];
ix[0][2] = n / 5;
ix[1][2] = n - 5 * ix[0][2];

/* Entropy decoding */
let mut n = 0;
while n < 2 {
ix[n as usize][0 as usize] = ec_dec_icdf(psRangeDec, &silk_uniform3_iCDF, 8);
ix[n as usize][1 as usize] = ec_dec_icdf(psRangeDec, &silk_uniform5_iCDF, 8);
ix[n][0] = ec_dec_icdf(psRangeDec, &silk_uniform3_iCDF, 8) as usize;
ix[n][1] = ec_dec_icdf(psRangeDec, &silk_uniform5_iCDF, 8) as usize;
n += 1;
}
n = 0;

/* Dequantize */
let mut n = 0;
while n < 2 {
ix[n as usize][0 as usize] += 3 * ix[n as usize][2 as usize];
low_Q13 = silk_stereo_pred_quant_Q13[ix[n as usize][0 as usize] as usize] as i32;
step_Q13 = ((silk_stereo_pred_quant_Q13[(ix[n as usize][0 as usize] + 1) as usize] as i32
- low_Q13) as i64
* (0.5f64 / 5 as f64 * ((1) << 16) as f64 + 0.5f64) as i32 as i16 as i64
>> 16) as i32;
*pred_Q13.offset(n as isize) =
low_Q13 + step_Q13 as i16 as i32 * (2 * ix[n as usize][1 as usize] + 1) as i16 as i32;
ix[n][0] += 3 * ix[n][2];
let low_Q13 = silk_stereo_pred_quant_Q13[ix[n][0]] as i32;
let step_Q13 = silk_SMULWB(
(silk_stereo_pred_quant_Q13[ix[n][0] + 1] as i32 - low_Q13),

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (macos-14, release)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (macos-14, release)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (ubuntu-latest, release)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (ubuntu-latest, release)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-vectors (macos-13)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-vectors (macos-13)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (macos-13, release)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (macos-13, release)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-vectors (ubuntu-latest)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-vectors (ubuntu-latest)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-vectors (macos-14)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-vectors (macos-14)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (windows-latest, release)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (windows-latest, release)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-vectors (windows-latest)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-vectors (windows-latest)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (macos-14, dev)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (macos-14, dev)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (ubuntu-latest, dev)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (ubuntu-latest, dev)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (windows-latest, dev)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (windows-latest, dev)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (macos-13, dev)

unnecessary parentheses around function argument

Check warning on line 37 in src/silk/stereo_decode_pred.rs

View workflow job for this annotation

GitHub Actions / test-basic (macos-13, dev)

unnecessary parentheses around function argument
SILK_FIX_CONST!(0.5 / STEREO_QUANT_SUB_STEPS as f64, 16),
);

pred_Q13[n] = low_Q13 + step_Q13 as i16 as i32 * (2 * ix[n][1] + 1) as i16 as i32;
n += 1;
}
let ref mut fresh0 = *pred_Q13.offset(0 as isize);
*fresh0 -= *pred_Q13.offset(1 as isize);

/* Subtract second from first predictor (helps when actually applying these) */
pred_Q13[0] -= pred_Q13[1];
}
pub unsafe fn silk_stereo_decode_mid_only(psRangeDec: &mut ec_dec, decode_only_mid: *mut i32) {
*decode_only_mid = ec_dec_icdf(psRangeDec, &silk_stereo_only_code_mid_iCDF, 8);

/// Decode mid-only flag
///
/// ```text
/// psRangeDec I/O Compressor data structure
/// decode_only_mid O Flag that only mid channel has been coded
/// ```
pub fn silk_stereo_decode_mid_only(psRangeDec: &mut ec_dec, decode_only_mid: &mut bool) {
/* Decode flag that only mid channel is coded */
*decode_only_mid = ec_dec_icdf(psRangeDec, &silk_stereo_only_code_mid_iCDF, 8) != 0;
}

0 comments on commit 570fb0b

Please sign in to comment.