From be75f1d0fe8611e73c80e487410f7355adfdd122 Mon Sep 17 00:00:00 2001 From: Koen Vos <koen.vos@skype.net> Date: Wed, 25 May 2011 17:18:02 -0400 Subject: [PATCH] Fixes a crash on fixed-point stereo voice and some valgrind uninitialized errors --- silk/fixed/silk_noise_shape_analysis_FIX.c | 4 ++ silk/fixed/silk_structs_FIX.h | 30 +++++------ silk/float/silk_LPC_analysis_filter_FLP.c | 27 +++++----- silk/float/silk_structs_FLP.h | 28 +++++----- silk/silk_HP_variable_cutoff.c | 59 +++++++++++++--------- silk/silk_LPC_analysis_filter.c | 5 +- silk/silk_control.h | 3 ++ silk/silk_control_codec.c | 3 +- silk/silk_enc_API.c | 6 ++- silk/silk_init_encoder.c | 5 +- silk/silk_lin2log.c | 4 +- silk/silk_structs.h | 1 + silk/silk_tuning_parameters.h | 4 +- 13 files changed, 104 insertions(+), 75 deletions(-) diff --git a/silk/fixed/silk_noise_shape_analysis_FIX.c b/silk/fixed/silk_noise_shape_analysis_FIX.c index ad23d3bde..7ded610cd 100644 --- a/silk/fixed/silk_noise_shape_analysis_FIX.c +++ b/silk/fixed/silk_noise_shape_analysis_FIX.c @@ -308,7 +308,11 @@ void silk_noise_shape_analysis_FIX( if( psEnc->sCmn.warping_Q16 > 0 ) { /* Adjust gain for warping */ gain_mult_Q16 = warped_gain( AR2_Q24, warping_Q16, psEnc->sCmn.shapingLPCOrder ); + SKP_assert( psEncCtrl->Gains_Q16[ k ] >= 0 ); psEncCtrl->Gains_Q16[ k ] = SKP_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 ); + if( psEncCtrl->Gains_Q16[ k ] < 0 ) { + psEncCtrl->Gains_Q16[ k ] = SKP_int32_MAX; + } } /* Bandwidth expansion for synthesis filter shaping */ diff --git a/silk/fixed/silk_structs_FIX.h b/silk/fixed/silk_structs_FIX.h index 390c38378..d60eb920a 100644 --- a/silk/fixed/silk_structs_FIX.h +++ b/silk/fixed/silk_structs_FIX.h @@ -65,17 +65,17 @@ typedef struct { /* Encoder state FIX */ /********************************/ typedef struct { - silk_encoder_state sCmn; /* Common struct, shared with floating-point code */ - silk_shape_state_FIX sShape; /* Shape state */ - silk_prefilter_state_FIX sPrefilt; /* Prefilter State */ + silk_encoder_state sCmn; /* Common struct, shared with floating-point code */ + silk_shape_state_FIX sShape; /* Shape state */ + silk_prefilter_state_FIX sPrefilt; /* Prefilter State */ /* Buffer for find pitch and noise shape analysis */ - SKP_DWORD_ALIGN SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ]; - SKP_int LTPCorr_Q15; /* Normalized correlation from pitch lag estimator */ - + SKP_DWORD_ALIGN SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];/* Buffer for find pitch and noise shape analysis */ + SKP_int LTPCorr_Q15; /* Normalized correlation from pitch lag estimator */ + /* Parameters For LTP scaling Control */ - SKP_int prevLTPredCodGain_Q7; - SKP_int HPLTPredCodGain_Q7; + SKP_int prevLTPredCodGain_Q7; + SKP_int HPLTPredCodGain_Q7; } silk_encoder_state_FIX; /************************/ @@ -115,13 +115,13 @@ typedef struct { /* Encoder Super Struct */ /************************/ typedef struct { - silk_encoder_state_FIX state_Fxx[ ENCODER_NUM_CHANNELS ]; - stereo_enc_state sStereo; - SKP_int32 nBitsExceeded; - SKP_int nChannelsAPI; - SKP_int nChannelsInternal; - SKP_int timeSinceSwitchAllowed_ms; - SKP_int allowBandwidthSwitch; + silk_encoder_state_FIX state_Fxx[ ENCODER_NUM_CHANNELS ]; + stereo_enc_state sStereo; + SKP_int32 nBitsExceeded; + SKP_int nChannelsAPI; + SKP_int nChannelsInternal; + SKP_int timeSinceSwitchAllowed_ms; + SKP_int allowBandwidthSwitch; } silk_encoder; diff --git a/silk/float/silk_LPC_analysis_filter_FLP.c b/silk/float/silk_LPC_analysis_filter_FLP.c index b28196cd8..7f506c4b0 100644 --- a/silk/float/silk_LPC_analysis_filter_FLP.c +++ b/silk/float/silk_LPC_analysis_filter_FLP.c @@ -28,12 +28,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <stdlib.h> #include "silk_main_FLP.h" -/*******************************************/ -/* LPC analysis filter */ -/* NB! State is kept internally and the */ -/* filter always starts with zero state */ -/* first Order output samples are not set */ -/*******************************************/ +/************************************************/ +/* LPC analysis filter */ +/* NB! State is kept internally and the */ +/* filter always starts with zero state */ +/* first Order output samples are set to zero */ +/************************************************/ /* 16th order LPC analysis filter, does not write first 16 samples */ void silk_LPC_analysis_filter16_FLP( @@ -233,12 +233,12 @@ void silk_LPC_analysis_filter6_FLP( } } -/*******************************************/ -/* LPC analysis filter */ -/* NB! State is kept internally and the */ -/* filter always starts with zero state */ -/* first Order output samples are not set */ -/*******************************************/ +/************************************************/ +/* LPC analysis filter */ +/* NB! State is kept internally and the */ +/* filter always starts with zero state */ +/* first Order output samples are set to zero */ +/************************************************/ void silk_LPC_analysis_filter_FLP( SKP_float r_LPC[], /* O LPC residual signal */ @@ -279,5 +279,8 @@ void silk_LPC_analysis_filter_FLP( SKP_assert( 0 ); break; } + + /* Set first Order output samples to zero */ + SKP_memset( r_LPC, 0, Order * sizeof( SKP_float ) ); } diff --git a/silk/float/silk_structs_FLP.h b/silk/float/silk_structs_FLP.h index 44c1e94f0..db72bba41 100644 --- a/silk/float/silk_structs_FLP.h +++ b/silk/float/silk_structs_FLP.h @@ -65,17 +65,17 @@ typedef struct { /* Encoder state FLP */ /********************************/ typedef struct { - silk_encoder_state sCmn; /* Common struct, shared with fixed-point code */ - silk_shape_state_FLP sShape; /* Noise shaping state */ - silk_prefilter_state_FLP sPrefilt; /* Prefilter State */ + silk_encoder_state sCmn; /* Common struct, shared with fixed-point code */ + silk_shape_state_FLP sShape; /* Noise shaping state */ + silk_prefilter_state_FLP sPrefilt; /* Prefilter State */ /* Buffer for find pitch and noise shape analysis */ - SKP_float x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];/* Buffer for find pitch and noise shape analysis */ - SKP_float LTPCorr; /* Normalized correlation from pitch lag estimator */ + SKP_float x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];/* Buffer for find pitch and noise shape analysis */ + SKP_float LTPCorr; /* Normalized correlation from pitch lag estimator */ /* Parameters for LTP scaling control */ - SKP_float prevLTPredCodGain; - SKP_float HPLTPredCodGain; + SKP_float prevLTPredCodGain; + SKP_float HPLTPredCodGain; } silk_encoder_state_FLP; /************************/ @@ -113,13 +113,13 @@ typedef struct { /* Encoder Super Struct */ /************************/ typedef struct { - silk_encoder_state_FLP state_Fxx[ ENCODER_NUM_CHANNELS ]; - stereo_enc_state sStereo; - SKP_int32 nBitsExceeded; - SKP_int nChannelsAPI; - SKP_int nChannelsInternal; - SKP_int timeSinceSwitchAllowed_ms; - SKP_int allowBandwidthSwitch; + silk_encoder_state_FLP state_Fxx[ ENCODER_NUM_CHANNELS ]; + stereo_enc_state sStereo; + SKP_int32 nBitsExceeded; + SKP_int nChannelsAPI; + SKP_int nChannelsInternal; + SKP_int timeSinceSwitchAllowed_ms; + SKP_int allowBandwidthSwitch; } silk_encoder; #ifdef __cplusplus diff --git a/silk/silk_HP_variable_cutoff.c b/silk/silk_HP_variable_cutoff.c index 10f6868cc..22aac3671 100644 --- a/silk/silk_HP_variable_cutoff.c +++ b/silk/silk_HP_variable_cutoff.c @@ -25,6 +25,9 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #ifdef FIXED_POINT #include "silk_main_FIX.h" #else @@ -44,32 +47,41 @@ void silk_HP_variable_cutoff( SKP_int32 pitch_freq_Hz_Q16, pitch_freq_log_Q7, delta_freq_Q7; silk_encoder_state *psEncC1 = &state_Fxx[ 0 ].sCmn; - /*********************************************/ - /* Estimate Low End of Pitch Frequency Range */ - /*********************************************/ - if( psEncC1->prevSignalType == TYPE_VOICED ) { - /* difference, in log domain */ - pitch_freq_Hz_Q16 = SKP_DIV32_16( SKP_LSHIFT( SKP_MUL( psEncC1->fs_kHz, 1000 ), 16 ), psEncC1->prevLag ); - pitch_freq_log_Q7 = silk_lin2log( pitch_freq_Hz_Q16 ) - ( 16 << 7 ); + if( psEncC1->HP_cutoff_Hz == 0 ) { + /* Adaptive cutoff frequency: estimate low end of pitch frequency range */ + if( psEncC1->prevSignalType == TYPE_VOICED ) { + /* difference, in log domain */ + pitch_freq_Hz_Q16 = SKP_DIV32_16( SKP_LSHIFT( SKP_MUL( psEncC1->fs_kHz, 1000 ), 16 ), psEncC1->prevLag ); + pitch_freq_log_Q7 = silk_lin2log( pitch_freq_Hz_Q16 ) - ( 16 << 7 ); - /* adjustment based on quality */ - quality_Q15 = psEncC1->input_quality_bands_Q15[ 0 ]; - pitch_freq_log_Q7 = SKP_SMLAWB( pitch_freq_log_Q7, SKP_SMULWB( SKP_LSHIFT( -quality_Q15, 2 ), quality_Q15 ), - pitch_freq_log_Q7 - ( silk_lin2log( SILK_FIX_CONST( VARIABLE_HP_MIN_CUTOFF_HZ, 16 ) ) - ( 16 << 7 ) ) ); + /* adjustment based on quality */ + quality_Q15 = psEncC1->input_quality_bands_Q15[ 0 ]; + pitch_freq_log_Q7 = SKP_SMLAWB( pitch_freq_log_Q7, SKP_SMULWB( SKP_LSHIFT( -quality_Q15, 2 ), quality_Q15 ), + pitch_freq_log_Q7 - ( silk_lin2log( SILK_FIX_CONST( VARIABLE_HP_MIN_CUTOFF_HZ, 16 ) ) - ( 16 << 7 ) ) ); - /* delta_freq = pitch_freq_log - psEnc->variable_HP_smth1; */ - delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEncC1->variable_HP_smth1_Q15, 8 ); - if( delta_freq_Q7 < 0 ) { - /* less smoothing for decreasing pitch frequency, to track something close to the minimum */ - delta_freq_Q7 = SKP_MUL( delta_freq_Q7, 3 ); - } + /* delta_freq = pitch_freq_log - psEnc->variable_HP_smth1; */ + delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEncC1->variable_HP_smth1_Q15, 8 ); + if( delta_freq_Q7 < 0 ) { + /* less smoothing for decreasing pitch frequency, to track something close to the minimum */ + delta_freq_Q7 = SKP_MUL( delta_freq_Q7, 3 ); + } + + /* limit delta, to reduce impact of outliers in pitch estimation */ + delta_freq_Q7 = SKP_LIMIT_32( delta_freq_Q7, -SILK_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ), SILK_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ) ); - /* limit delta, to reduce impact of outliers in pitch estimation */ - delta_freq_Q7 = SKP_LIMIT_32( delta_freq_Q7, -SILK_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ), SILK_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ) ); + /* update smoother */ + psEncC1->variable_HP_smth1_Q15 = SKP_SMLAWB( psEncC1->variable_HP_smth1_Q15, + SKP_SMULBB( psEncC1->speech_activity_Q8, delta_freq_Q7 ), SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF1, 16 ) ); - /* update smoother */ - psEncC1->variable_HP_smth1_Q15 = SKP_SMLAWB( psEncC1->variable_HP_smth1_Q15, - SKP_SMULBB( psEncC1->speech_activity_Q8, delta_freq_Q7 ), SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF1, 16 ) ); + /* limit frequency range */ + psEncC1->variable_HP_smth1_Q15 = SKP_LIMIT_32( psEncC1->variable_HP_smth1_Q15, + SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ), + SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MAX_CUTOFF_HZ ), 8 ) ); + } + } else { + /* Externally-controlled cutoff frequency */ + cutoff_Hz = SKP_LIMIT( psEncC1->HP_cutoff_Hz, 10, 500 ); + psEncC1->variable_HP_smth1_Q15 = SKP_LSHIFT( silk_lin2log( cutoff_Hz ), 8 ); } /* second smoother */ @@ -79,9 +91,6 @@ void silk_HP_variable_cutoff( /* convert from log scale to Hertz */ cutoff_Hz = silk_log2lin( SKP_RSHIFT( psEncC1->variable_HP_smth2_Q15, 8 ) ); - /* limit frequency range */ - cutoff_Hz = SKP_LIMIT_32( cutoff_Hz, SILK_FIX_CONST( VARIABLE_HP_MIN_CUTOFF_HZ, 0 ), SILK_FIX_CONST( VARIABLE_HP_MAX_CUTOFF_HZ, 0 ) ); - /********************************/ /* Compute Filter Coefficients */ /********************************/ diff --git a/silk/silk_LPC_analysis_filter.c b/silk/silk_LPC_analysis_filter.c index b3f9ad75e..2655b4e1d 100644 --- a/silk/silk_LPC_analysis_filter.c +++ b/silk/silk_LPC_analysis_filter.c @@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /* LPC analysis filter */ /* NB! State is kept internally and the */ /* filter always starts with zero state */ -/* first Order output samples are not set */ +/* first d output samples are set to zero */ /*******************************************/ void silk_LPC_analysis_filter( @@ -73,4 +73,7 @@ void silk_LPC_analysis_filter( /* Saturate output */ out[ ix ] = ( SKP_int16 )SKP_SAT16( out32 ); } + + /* Set first d output samples to zero */ + SKP_memset( out, 0, d * sizeof( SKP_int16 ) ); } diff --git a/silk/silk_control.h b/silk/silk_control.h index a74c54209..f85c5dea5 100644 --- a/silk/silk_control.h +++ b/silk/silk_control.h @@ -83,6 +83,9 @@ typedef struct { /* I: Flag to use constant bitrate */ SKP_int useCBR; + /* I: Cutoff frequency of input HP filter (of zero: adaptive) */ + SKP_int HP_cutoff_Hz; + /* O: Internal sampling rate used, in Hertz; 8000/12000/16000 */ SKP_int32 internalSampleRate; diff --git a/silk/silk_control_codec.c b/silk/silk_control_codec.c index dde942f7e..cbcfad004 100644 --- a/silk/silk_control_codec.c +++ b/silk/silk_control_codec.c @@ -28,8 +28,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef HAVE_CONFIG_H #include "config.h" #endif - -#include "silk_tuning_parameters.h" #ifdef FIXED_POINT #include "silk_main_FIX.h" #define silk_encoder_state_Fxx silk_encoder_state_FIX @@ -37,6 +35,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "silk_main_FLP.h" #define silk_encoder_state_Fxx silk_encoder_state_FLP #endif +#include "silk_tuning_parameters.h" SKP_int silk_setup_resamplers( silk_encoder_state_Fxx *psEnc, /* I/O */ diff --git a/silk/silk_enc_API.c b/silk/silk_enc_API.c index 9f06ecede..b665c5bb6 100644 --- a/silk/silk_enc_API.c +++ b/silk/silk_enc_API.c @@ -25,7 +25,9 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ - +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "silk_define.h" #include "silk_API.h" #include "silk_control.h" @@ -110,6 +112,7 @@ SKP_int silk_QueryEncoder( encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC; encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX; encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR; + encStatus->HP_cutoff_Hz = state_Fxx[ 0 ].sCmn.HP_cutoff_Hz; encStatus->internalSampleRate = SKP_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch; encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0; @@ -295,6 +298,7 @@ SKP_int silk_Encode( } /* High-pass filter */ + psEnc->state_Fxx[ 0 ].sCmn.HP_cutoff_Hz = encControl->HP_cutoff_Hz; silk_HP_variable_cutoff( psEnc->state_Fxx, psEnc->nChannelsInternal ); /* Total target bits for packet */ diff --git a/silk/silk_init_encoder.c b/silk/silk_init_encoder.c index 7ef5867d6..eeb32da93 100644 --- a/silk/silk_init_encoder.c +++ b/silk/silk_init_encoder.c @@ -25,6 +25,9 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #ifdef FIXED_POINT #include "silk_main_FIX.h" #else @@ -54,5 +57,5 @@ SKP_int silk_init_encoder( /* Initialize Silk VAD */ ret += silk_VAD_Init( &psEnc->sCmn.sVAD ); - return( ret ); + return ret; } diff --git a/silk/silk_lin2log.c b/silk/silk_lin2log.c index ddee11c00..e55d64e56 100644 --- a/silk/silk_lin2log.c +++ b/silk/silk_lin2log.c @@ -26,7 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***********************************************************************/ #include "silk_SigProc_FIX.h" -/* Approximation of 128 * log2() (very close inverse of approx 2^() below) */ +/* Approximation of 128 * log2() (very close inverse of silk_log2lin()) */ /* Convert input to a log scale */ SKP_int32 silk_lin2log( const SKP_int32 inLin ) /* I: Input in linear scale */ { @@ -35,6 +35,6 @@ SKP_int32 silk_lin2log( const SKP_int32 inLin ) /* I: Input in linear scal silk_CLZ_FRAC( inLin, &lz, &frac_Q7 ); /* Piece-wise parabolic approximation */ - return( SKP_LSHIFT( 31 - lz, 7 ) + SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), 179 ) ); + return SKP_LSHIFT( 31 - lz, 7 ) + SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), 179 ); } diff --git a/silk/silk_structs.h b/silk/silk_structs.h index efcda6e58..78bb98a99 100644 --- a/silk/silk_structs.h +++ b/silk/silk_structs.h @@ -135,6 +135,7 @@ typedef struct { SKP_int32 In_HP_State[ 2 ]; /* High pass filter state */ SKP_int32 variable_HP_smth1_Q15; /* State of first smoother */ SKP_int32 variable_HP_smth2_Q15; /* State of second smoother */ + SKP_int HP_cutoff_Hz; /* Fixed cutoff frequency (if zero: adaptive) */ silk_LP_state sLP; /* Low pass filter state */ silk_VAD_state sVAD; /* Voice activity detector state */ silk_nsq_state sNSQ; /* Noise Shape Quantizer State */ diff --git a/silk/silk_tuning_parameters.h b/silk/silk_tuning_parameters.h index ea761f06c..946d691a2 100644 --- a/silk/silk_tuning_parameters.h +++ b/silk/silk_tuning_parameters.h @@ -75,8 +75,8 @@ extern "C" #define VARIABLE_HP_MAX_DELTA_FREQ 0.4f /* Min and max cut-off frequency values (-3 dB points) */ -#define VARIABLE_HP_MIN_CUTOFF_HZ 60.0f -#define VARIABLE_HP_MAX_CUTOFF_HZ 100.0f +#define VARIABLE_HP_MIN_CUTOFF_HZ 60 +#define VARIABLE_HP_MAX_CUTOFF_HZ 100 /***********/ /* Various */ -- GitLab