Fixes a crash on fixed-point stereo voice and some valgrind uninitialized errors
authorKoen Vos <koen.vos@skype.net>
Wed, 25 May 2011 21:18:02 +0000 (17:18 -0400)
committerJean-Marc Valin <jean-marc.valin@octasic.com>
Wed, 25 May 2011 21:18:02 +0000 (17:18 -0400)
13 files changed:
silk/fixed/silk_noise_shape_analysis_FIX.c
silk/fixed/silk_structs_FIX.h
silk/float/silk_LPC_analysis_filter_FLP.c
silk/float/silk_structs_FLP.h
silk/silk_HP_variable_cutoff.c
silk/silk_LPC_analysis_filter.c
silk/silk_control.h
silk/silk_control_codec.c
silk/silk_enc_API.c
silk/silk_init_encoder.c
silk/silk_lin2log.c
silk/silk_structs.h
silk/silk_tuning_parameters.h

index ad23d3b..7ded610 100644 (file)
@@ -308,7 +308,11 @@ void silk_noise_shape_analysis_FIX(
         if( psEnc->sCmn.warping_Q16 > 0 ) {\r
             /* Adjust gain for warping */\r
             gain_mult_Q16 = warped_gain( AR2_Q24, warping_Q16, psEnc->sCmn.shapingLPCOrder );\r
+            SKP_assert( psEncCtrl->Gains_Q16[ k ] >= 0 );\r
             psEncCtrl->Gains_Q16[ k ] = SKP_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 );\r
+            if( psEncCtrl->Gains_Q16[ k ] < 0 ) {\r
+                psEncCtrl->Gains_Q16[ k ] = SKP_int32_MAX;\r
+            }\r
         }\r
 \r
         /* Bandwidth expansion for synthesis filter shaping */\r
index 390c383..d60eb92 100644 (file)
@@ -65,17 +65,17 @@ typedef struct {
 /* Encoder state FIX            */\r
 /********************************/\r
 typedef struct {\r
-    silk_encoder_state          sCmn;                           /* Common struct, shared with floating-point code */\r
-    silk_shape_state_FIX        sShape;                         /* Shape state                                                          */\r
-    silk_prefilter_state_FIX    sPrefilt;                       /* Prefilter State                                                      */\r
+    silk_encoder_state          sCmn;                       /* Common struct, shared with floating-point code                   */\r
+    silk_shape_state_FIX        sShape;                     /* Shape state                                                      */\r
+    silk_prefilter_state_FIX    sPrefilt;                   /* Prefilter State                                                  */\r
 \r
     /* Buffer for find pitch and noise shape analysis */\r
-    SKP_DWORD_ALIGN SKP_int16 x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];\r
-    SKP_int                         LTPCorr_Q15;                    /* Normalized correlation from pitch lag estimator                      */\r
-\r
+    SKP_DWORD_ALIGN SKP_int16   x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];/* Buffer for find pitch and noise shape analysis  */\r
+    SKP_int                     LTPCorr_Q15;                /* Normalized correlation from pitch lag estimator                  */\r
\r
     /* Parameters For LTP scaling Control */\r
-    SKP_int                         prevLTPredCodGain_Q7;\r
-    SKP_int                         HPLTPredCodGain_Q7;\r
+    SKP_int                     prevLTPredCodGain_Q7;\r
+    SKP_int                     HPLTPredCodGain_Q7;\r
 } silk_encoder_state_FIX;\r
 \r
 /************************/\r
@@ -115,13 +115,13 @@ typedef struct {
 /* Encoder Super Struct */\r
 /************************/\r
 typedef struct {\r
-    silk_encoder_state_FIX          state_Fxx[ ENCODER_NUM_CHANNELS ];\r
-    stereo_enc_state                    sStereo;\r
-    SKP_int32                           nBitsExceeded;\r
-    SKP_int                             nChannelsAPI;\r
-       SKP_int                             nChannelsInternal;\r
-    SKP_int                             timeSinceSwitchAllowed_ms;\r
-    SKP_int                             allowBandwidthSwitch;\r
+    silk_encoder_state_FIX  state_Fxx[ ENCODER_NUM_CHANNELS ];\r
+    stereo_enc_state        sStereo;\r
+    SKP_int32               nBitsExceeded;\r
+    SKP_int                 nChannelsAPI;\r
+    SKP_int                 nChannelsInternal;\r
+    SKP_int                 timeSinceSwitchAllowed_ms;\r
+    SKP_int                 allowBandwidthSwitch;\r
 } silk_encoder;\r
 \r
 \r
index b28196c..7f506c4 100644 (file)
@@ -28,12 +28,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <stdlib.h>\r
 #include "silk_main_FLP.h"\r
 \r
-/*******************************************/\r
-/* LPC analysis filter                     */\r
-/* NB! State is kept internally and the    */\r
-/* filter always starts with zero state    */\r
-/* first Order output samples are not set  */\r
-/*******************************************/\r
+/************************************************/\r
+/* LPC analysis filter                          */\r
+/* NB! State is kept internally and the         */\r
+/* filter always starts with zero state         */\r
+/* first Order output samples are set to zero   */\r
+/************************************************/\r
 \r
 /* 16th order LPC analysis filter, does not write first 16 samples */\r
 void silk_LPC_analysis_filter16_FLP(\r
@@ -233,12 +233,12 @@ void silk_LPC_analysis_filter6_FLP(
     }\r
 }\r
 \r
-/*******************************************/\r
-/* LPC analysis filter                     */\r
-/* NB! State is kept internally and the    */\r
-/* filter always starts with zero state    */\r
-/* first Order output samples are not set  */\r
-/*******************************************/\r
+/************************************************/\r
+/* LPC analysis filter                          */\r
+/* NB! State is kept internally and the         */\r
+/* filter always starts with zero state         */\r
+/* first Order output samples are set to zero   */\r
+/************************************************/\r
 \r
 void silk_LPC_analysis_filter_FLP(\r
           SKP_float                 r_LPC[],            /* O    LPC residual signal                     */\r
@@ -279,5 +279,8 @@ void silk_LPC_analysis_filter_FLP(
             SKP_assert( 0 );\r
         break;\r
     }\r
+\r
+    /* Set first Order output samples to zero */\r
+    SKP_memset( r_LPC, 0, Order * sizeof( SKP_float ) );\r
 }\r
 \r
index 44c1e94..db72bba 100644 (file)
@@ -65,17 +65,17 @@ typedef struct {
 /* Encoder state FLP            */\r
 /********************************/\r
 typedef struct {\r
-    silk_encoder_state              sCmn;                       /* Common struct, shared with fixed-point code */\r
-    silk_shape_state_FLP            sShape;                     /* Noise shaping state */\r
-    silk_prefilter_state_FLP        sPrefilt;                   /* Prefilter State */\r
+    silk_encoder_state          sCmn;                       /* Common struct, shared with fixed-point code */\r
+    silk_shape_state_FLP        sShape;                     /* Noise shaping state */\r
+    silk_prefilter_state_FLP    sPrefilt;                   /* Prefilter State */\r
 \r
     /* Buffer for find pitch and noise shape analysis */\r
-    SKP_float                           x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];/* Buffer for find pitch and noise shape analysis */\r
-    SKP_float                           LTPCorr;                    /* Normalized correlation from pitch lag estimator */\r
+    SKP_float                   x_buf[ 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ];/* Buffer for find pitch and noise shape analysis */\r
+    SKP_float                   LTPCorr;                    /* Normalized correlation from pitch lag estimator */\r
 \r
     /* Parameters for LTP scaling control */\r
-    SKP_float                           prevLTPredCodGain;\r
-    SKP_float                           HPLTPredCodGain;\r
+    SKP_float                   prevLTPredCodGain;\r
+    SKP_float                   HPLTPredCodGain;\r
 } silk_encoder_state_FLP;\r
 \r
 /************************/\r
@@ -113,13 +113,13 @@ typedef struct {
 /* Encoder Super Struct */\r
 /************************/\r
 typedef struct {\r
-    silk_encoder_state_FLP          state_Fxx[ ENCODER_NUM_CHANNELS ];\r
-    stereo_enc_state                    sStereo;\r
-    SKP_int32                           nBitsExceeded;\r
-    SKP_int                             nChannelsAPI;\r
-    SKP_int                             nChannelsInternal;\r
-    SKP_int                             timeSinceSwitchAllowed_ms;\r
-    SKP_int                             allowBandwidthSwitch;\r
+    silk_encoder_state_FLP      state_Fxx[ ENCODER_NUM_CHANNELS ];\r
+    stereo_enc_state            sStereo;\r
+    SKP_int32                   nBitsExceeded;\r
+    SKP_int                     nChannelsAPI;\r
+    SKP_int                     nChannelsInternal;\r
+    SKP_int                     timeSinceSwitchAllowed_ms;\r
+    SKP_int                     allowBandwidthSwitch;\r
 } silk_encoder;\r
 \r
 #ifdef __cplusplus\r
index 10f6868..22aac36 100644 (file)
@@ -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.\r
 ***********************************************************************/\r
 \r
+#ifdef HAVE_CONFIG_H\r
+#include "config.h"\r
+#endif\r
 #ifdef FIXED_POINT\r
 #include "silk_main_FIX.h"\r
 #else\r
@@ -44,32 +47,41 @@ void silk_HP_variable_cutoff(
     SKP_int32 pitch_freq_Hz_Q16, pitch_freq_log_Q7, delta_freq_Q7;\r
     silk_encoder_state *psEncC1 = &state_Fxx[ 0 ].sCmn;\r
 \r
-    /*********************************************/\r
-    /* Estimate Low End of Pitch Frequency Range */\r
-    /*********************************************/\r
-    if( psEncC1->prevSignalType == TYPE_VOICED ) {\r
-        /* difference, in log domain */\r
-        pitch_freq_Hz_Q16 = SKP_DIV32_16( SKP_LSHIFT( SKP_MUL( psEncC1->fs_kHz, 1000 ), 16 ), psEncC1->prevLag );\r
-        pitch_freq_log_Q7 = silk_lin2log( pitch_freq_Hz_Q16 ) - ( 16 << 7 );\r
+    if( psEncC1->HP_cutoff_Hz == 0 ) {\r
+        /* Adaptive cutoff frequency: estimate low end of pitch frequency range */\r
+        if( psEncC1->prevSignalType == TYPE_VOICED ) {\r
+            /* difference, in log domain */\r
+            pitch_freq_Hz_Q16 = SKP_DIV32_16( SKP_LSHIFT( SKP_MUL( psEncC1->fs_kHz, 1000 ), 16 ), psEncC1->prevLag );\r
+            pitch_freq_log_Q7 = silk_lin2log( pitch_freq_Hz_Q16 ) - ( 16 << 7 );\r
 \r
-        /* adjustment based on quality */\r
-        quality_Q15 = psEncC1->input_quality_bands_Q15[ 0 ];\r
-        pitch_freq_log_Q7 = SKP_SMLAWB( pitch_freq_log_Q7, SKP_SMULWB( SKP_LSHIFT( -quality_Q15, 2 ), quality_Q15 ), \r
-            pitch_freq_log_Q7 - ( silk_lin2log( SILK_FIX_CONST( VARIABLE_HP_MIN_CUTOFF_HZ, 16 ) ) - ( 16 << 7 ) ) );\r
+            /* adjustment based on quality */\r
+            quality_Q15 = psEncC1->input_quality_bands_Q15[ 0 ];\r
+            pitch_freq_log_Q7 = SKP_SMLAWB( pitch_freq_log_Q7, SKP_SMULWB( SKP_LSHIFT( -quality_Q15, 2 ), quality_Q15 ), \r
+                pitch_freq_log_Q7 - ( silk_lin2log( SILK_FIX_CONST( VARIABLE_HP_MIN_CUTOFF_HZ, 16 ) ) - ( 16 << 7 ) ) );\r
 \r
-        /* delta_freq = pitch_freq_log - psEnc->variable_HP_smth1; */\r
-        delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEncC1->variable_HP_smth1_Q15, 8 );\r
-        if( delta_freq_Q7 < 0 ) {\r
-            /* less smoothing for decreasing pitch frequency, to track something close to the minimum */\r
-            delta_freq_Q7 = SKP_MUL( delta_freq_Q7, 3 );\r
-        }\r
+            /* delta_freq = pitch_freq_log - psEnc->variable_HP_smth1; */\r
+            delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEncC1->variable_HP_smth1_Q15, 8 );\r
+            if( delta_freq_Q7 < 0 ) {\r
+                /* less smoothing for decreasing pitch frequency, to track something close to the minimum */\r
+                delta_freq_Q7 = SKP_MUL( delta_freq_Q7, 3 );\r
+            }\r
+\r
+            /* limit delta, to reduce impact of outliers in pitch estimation */\r
+            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 ) );\r
 \r
-        /* limit delta, to reduce impact of outliers in pitch estimation */\r
-        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 ) );\r
+            /* update smoother */\r
+            psEncC1->variable_HP_smth1_Q15 = SKP_SMLAWB( psEncC1->variable_HP_smth1_Q15, \r
+                SKP_SMULBB( psEncC1->speech_activity_Q8, delta_freq_Q7 ), SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF1, 16 ) );\r
 \r
-        /* update smoother */\r
-        psEncC1->variable_HP_smth1_Q15 = SKP_SMLAWB( psEncC1->variable_HP_smth1_Q15, \r
-            SKP_SMULBB( psEncC1->speech_activity_Q8, delta_freq_Q7 ), SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF1, 16 ) );\r
+            /* limit frequency range */\r
+            psEncC1->variable_HP_smth1_Q15 = SKP_LIMIT_32( psEncC1->variable_HP_smth1_Q15, \r
+                SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ), \r
+                SKP_LSHIFT( silk_lin2log( VARIABLE_HP_MAX_CUTOFF_HZ ), 8 ) );\r
+        }\r
+    } else {\r
+        /* Externally-controlled cutoff frequency */\r
+        cutoff_Hz = SKP_LIMIT( psEncC1->HP_cutoff_Hz, 10, 500 );\r
+        psEncC1->variable_HP_smth1_Q15 = SKP_LSHIFT( silk_lin2log( cutoff_Hz ), 8 );\r
     }\r
 \r
     /* second smoother */\r
@@ -79,9 +91,6 @@ void silk_HP_variable_cutoff(
     /* convert from log scale to Hertz */\r
     cutoff_Hz = silk_log2lin( SKP_RSHIFT( psEncC1->variable_HP_smth2_Q15, 8 ) );\r
 \r
-    /* limit frequency range */\r
-    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 ) );\r
-\r
     /********************************/\r
     /* Compute Filter Coefficients  */\r
     /********************************/\r
index b3f9ad7..2655b4e 100644 (file)
@@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 /* LPC analysis filter                     */\r
 /* NB! State is kept internally and the    */\r
 /* filter always starts with zero state    */\r
-/* first Order output samples are not set  */\r
+/* first d output samples are set to zero  */\r
 /*******************************************/\r
 \r
 void silk_LPC_analysis_filter(\r
@@ -73,4 +73,7 @@ void silk_LPC_analysis_filter(
         /* Saturate output */\r
         out[ ix ] = ( SKP_int16 )SKP_SAT16( out32 );\r
     }\r
+\r
+    /* Set first d output samples to zero */\r
+    SKP_memset( out, 0, d * sizeof( SKP_int16 ) );\r
 }\r
index a74c542..f85c5de 100644 (file)
@@ -83,6 +83,9 @@ typedef struct {
     /* I:   Flag to use constant bitrate                                                    */\r
     SKP_int useCBR;\r
 \r
+    /* I:   Cutoff frequency of input HP filter (of zero: adaptive)                         */\r
+    SKP_int HP_cutoff_Hz;\r
+\r
     /* O:   Internal sampling rate used, in Hertz; 8000/12000/16000                         */\r
     SKP_int32 internalSampleRate;\r
 \r
index dde942f..cbcfad0 100644 (file)
@@ -28,8 +28,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #ifdef HAVE_CONFIG_H\r
 #include "config.h"\r
 #endif\r
-\r
-#include "silk_tuning_parameters.h"\r
 #ifdef FIXED_POINT\r
 #include "silk_main_FIX.h"\r
 #define silk_encoder_state_Fxx      silk_encoder_state_FIX\r
@@ -37,6 +35,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "silk_main_FLP.h"\r
 #define silk_encoder_state_Fxx      silk_encoder_state_FLP\r
 #endif\r
+#include "silk_tuning_parameters.h"\r
 \r
 SKP_int silk_setup_resamplers(\r
     silk_encoder_state_Fxx          *psEnc,             /* I/O                      */\r
index 9f06ece..b665c5b 100644 (file)
@@ -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.\r
 ***********************************************************************/\r
 \r
-\r
+#ifdef HAVE_CONFIG_H\r
+#include "config.h"\r
+#endif\r
 #include "silk_define.h"\r
 #include "silk_API.h"\r
 #include "silk_control.h"\r
@@ -110,6 +112,7 @@ SKP_int silk_QueryEncoder(
     encStatus->useInBandFEC              = state_Fxx[ 0 ].sCmn.useInBandFEC;\r
     encStatus->useDTX                    = state_Fxx[ 0 ].sCmn.useDTX;\r
     encStatus->useCBR                    = state_Fxx[ 0 ].sCmn.useCBR;\r
+    encStatus->HP_cutoff_Hz              = state_Fxx[ 0 ].sCmn.HP_cutoff_Hz;\r
     encStatus->internalSampleRate        = SKP_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );\r
     encStatus->allowBandwidthSwitch      = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch;\r
     encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0;\r
@@ -295,6 +298,7 @@ SKP_int silk_Encode(
             }\r
 \r
             /* High-pass filter */\r
+            psEnc->state_Fxx[ 0 ].sCmn.HP_cutoff_Hz = encControl->HP_cutoff_Hz;\r
             silk_HP_variable_cutoff( psEnc->state_Fxx, psEnc->nChannelsInternal );\r
 \r
             /* Total target bits for packet */\r
index 7ef5867..eeb32da 100644 (file)
@@ -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.\r
 ***********************************************************************/\r
 \r
+#ifdef HAVE_CONFIG_H\r
+#include "config.h"\r
+#endif\r
 #ifdef FIXED_POINT\r
 #include "silk_main_FIX.h"\r
 #else\r
@@ -54,5 +57,5 @@ SKP_int silk_init_encoder(
     /* Initialize Silk VAD */\r
     ret += silk_VAD_Init( &psEnc->sCmn.sVAD );\r
 \r
-    return( ret );\r
+    return  ret;\r
 }\r
index ddee11c..e55d64e 100644 (file)
@@ -26,7 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "silk_SigProc_FIX.h"\r
-/* Approximation of 128 * log2() (very close inverse of approx 2^() below) */\r
+/* Approximation of 128 * log2() (very close inverse of silk_log2lin()) */\r
 /* Convert input to a log scale    */ \r
 SKP_int32 silk_lin2log( const SKP_int32 inLin )    /* I:    Input in linear scale */\r
 {\r
@@ -35,6 +35,6 @@ SKP_int32 silk_lin2log( const SKP_int32 inLin )    /* I:    Input in linear scal
     silk_CLZ_FRAC( inLin, &lz, &frac_Q7 );\r
 \r
     /* Piece-wise parabolic approximation */\r
-    return( SKP_LSHIFT( 31 - lz, 7 ) + SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), 179 ) );\r
+    return SKP_LSHIFT( 31 - lz, 7 ) + SKP_SMLAWB( frac_Q7, SKP_MUL( frac_Q7, 128 - frac_Q7 ), 179 );\r
 }\r
 \r
index efcda6e..78bb98a 100644 (file)
@@ -135,6 +135,7 @@ typedef struct {
     SKP_int32                       In_HP_State[ 2 ];               /* High pass filter state                                               */\r
     SKP_int32                       variable_HP_smth1_Q15;          /* State of first smoother                                              */\r
     SKP_int32                       variable_HP_smth2_Q15;          /* State of second smoother                                             */\r
+    SKP_int                         HP_cutoff_Hz;                   /* Fixed cutoff frequency (if zero: adaptive)                           */\r
     silk_LP_state                   sLP;                            /* Low pass filter state                                                */\r
     silk_VAD_state                  sVAD;                           /* Voice activity detector state                                        */\r
     silk_nsq_state                  sNSQ;                           /* Noise Shape Quantizer State                                          */\r
index ea761f0..946d691 100644 (file)
@@ -75,8 +75,8 @@ extern "C"
 #define VARIABLE_HP_MAX_DELTA_FREQ                      0.4f\r
 \r
 /* Min and max cut-off frequency values (-3 dB points) */\r
-#define VARIABLE_HP_MIN_CUTOFF_HZ                       60.0f\r
-#define VARIABLE_HP_MAX_CUTOFF_HZ                       100.0f\r
+#define VARIABLE_HP_MIN_CUTOFF_HZ                       60\r
+#define VARIABLE_HP_MAX_CUTOFF_HZ                       100\r
 \r
 /***********/\r
 /* Various */\r