Update SILK code using the CELT range coder
[opus.git] / src_SigProc_FIX / SKP_Silk_pitch_analysis_core.c
similarity index 66%
rename from src/SKP_Silk_pitch_analysis_core.c
rename to src_SigProc_FIX/SKP_Silk_pitch_analysis_core.c
index 6d68c1a..9e76ab3 100644 (file)
@@ -30,7 +30,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ********************************************************** */\r
 #include "SKP_Silk_SigProc_FIX.h"\r
 #include "SKP_Silk_pitch_est_defines.h"\r
-#include "SKP_Silk_resample_rom.h"\r
+#include "SKP_debug.h"\r
 \r
 #define SCRATCH_SIZE    22\r
 \r
@@ -38,18 +38,20 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 /* Internally used functions                                */\r
 /************************************************************/\r
 void SKP_FIX_P_Ana_calc_corr_st3(\r
-    SKP_int32        cross_corr_st3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX][PITCH_EST_NB_STAGE3_LAGS],/* (O) 3 DIM correlation array */\r
+    SKP_int32        cross_corr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ],/* (O) 3 DIM correlation array */\r
     const SKP_int16  signal[],                        /* I vector to correlate         */\r
     SKP_int          start_lag,                       /* I lag offset to search around */\r
     SKP_int          sf_length,                       /* I length of a 5 ms subframe   */\r
+    SKP_int          nb_subfr,                        /* I number of subframes         */\r
     SKP_int          complexity                       /* I Complexity setting          */\r
 );\r
 \r
 void SKP_FIX_P_Ana_calc_energy_st3(\r
-    SKP_int32        energies_st3[PITCH_EST_NB_SUBFR][PITCH_EST_NB_CBKS_STAGE3_MAX][PITCH_EST_NB_STAGE3_LAGS],/* (O) 3 DIM energy array */\r
+    SKP_int32        energies_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ],/* (O) 3 DIM energy array */\r
     const SKP_int16  signal[],                        /* I vector to calc energy in    */\r
     SKP_int          start_lag,                       /* I lag offset to search around */\r
     SKP_int          sf_length,                       /* I length of one 5 ms subframe */\r
+    SKP_int          nb_subfr,                        /* I number of subframes         */\r
     SKP_int          complexity                       /* I Complexity setting          */\r
 );\r
 \r
@@ -59,36 +61,11 @@ SKP_int32 SKP_FIX_P_Ana_find_scaling(
     const SKP_int    sum_sqr_len\r
 );\r
 \r
-void SKP_Silk_decode_pitch(\r
-    SKP_int          lagIndex,                        /* I                             */\r
-    SKP_int          contourIndex,                    /* O                             */\r
-    SKP_int          pitch_lags[],                    /* O 4 pitch values              */\r
-    SKP_int          Fs_kHz                           /* I sampling frequency (kHz)    */\r
-)\r
-{\r
-    SKP_int lag, i, min_lag;\r
-\r
-    min_lag = SKP_SMULBB( PITCH_EST_MIN_LAG_MS, Fs_kHz );\r
-\r
-    /* Only for 24 / 16 kHz version for now */\r
-    lag = min_lag + lagIndex;\r
-    if( Fs_kHz == 8 ) {\r
-        /* Only a small codebook for 8 khz */\r
-        for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) {\r
-            pitch_lags[ i ] = lag + SKP_Silk_CB_lags_stage2[ i ][ contourIndex ];\r
-        }\r
-    } else {\r
-        for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) {\r
-            pitch_lags[ i ] = lag + SKP_Silk_CB_lags_stage3[ i ][ contourIndex ];\r
-        }\r
-    }\r
-}\r
-\r
 /*************************************************************/\r
 /*      FIXED POINT CORE PITCH ANALYSIS FUNCTION             */\r
 /*************************************************************/\r
-SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unvoiced                      */\r
-    const SKP_int16  *signal,            /* I    Signal of length PITCH_EST_FRAME_LENGTH_MS*Fs_kHz           */\r
+SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unvoiced                        */\r
+    const SKP_int16  *signal,            /* I    Signal of length PE_FRAME_LENGTH_MS*Fs_kHz           */\r
     SKP_int          *pitch_out,         /* O    4 pitch lag values                                          */\r
     SKP_int          *lagIndex,          /* O    Lag Index                                                   */\r
     SKP_int          *contourIndex,      /* O    Pitch contour Index                                         */\r
@@ -97,107 +74,79 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
     const SKP_int32  search_thres1_Q16,  /* I    First stage threshold for lag candidates 0 - 1              */\r
     const SKP_int    search_thres2_Q15,  /* I    Final threshold for lag candidates 0 - 1                    */\r
     const SKP_int    Fs_kHz,             /* I    Sample frequency (kHz)                                      */\r
-    const SKP_int    complexity          /* I    Complexity setting, 0-2, where 2 is highest                 */\r
+    const SKP_int    complexity,         /* I    Complexity setting, 0-2, where 2 is highest                 */\r
+    const SKP_int    nb_subfr            /* I    number of 5 ms subframes                                    */\r
 )\r
 {\r
-    SKP_int16 signal_8kHz[ PITCH_EST_MAX_FRAME_LENGTH_ST_2 ];\r
-    SKP_int16 signal_4kHz[ PITCH_EST_MAX_FRAME_LENGTH_ST_1 ];\r
-    SKP_int32 scratch_mem[ 3 * PITCH_EST_MAX_FRAME_LENGTH ];\r
+    SKP_int16 signal_8kHz[ PE_MAX_FRAME_LENGTH_ST_2 ];\r
+    SKP_int16 signal_4kHz[ PE_MAX_FRAME_LENGTH_ST_1 ];\r
+    SKP_int32 scratch_mem[ 3 * PE_MAX_FRAME_LENGTH ];\r
     SKP_int16 *input_signal_ptr;\r
-    SKP_int32 filt_state[ PITCH_EST_MAX_DECIMATE_STATE_LENGTH ];\r
+    SKP_int32 filt_state[ PE_MAX_DECIMATE_STATE_LENGTH ];\r
     SKP_int   i, k, d, j;\r
-    SKP_int16 C[ PITCH_EST_NB_SUBFR ][ ( PITCH_EST_MAX_LAG >> 1 ) + 5 ];\r
+    SKP_int16 C[ PE_MAX_NB_SUBFR ][ ( PE_MAX_LAG >> 1 ) + 5 ];\r
     const SKP_int16 *target_ptr, *basis_ptr;\r
     SKP_int32 cross_corr, normalizer, energy, shift, energy_basis, energy_target;\r
-    SKP_int   d_srch[ PITCH_EST_D_SRCH_LENGTH ];\r
-    SKP_int16 d_comp[ ( PITCH_EST_MAX_LAG >> 1 ) + 5 ];\r
-    SKP_int   Cmax, length_d_srch, length_d_comp;\r
-    SKP_int32 sum, threshold, temp32;\r
+    SKP_int   d_srch[ PE_D_SRCH_LENGTH ], Cmax, length_d_srch, length_d_comp;\r
+    SKP_int16 d_comp[ ( PE_MAX_LAG >> 1 ) + 5 ];\r
+    SKP_int32 sum, threshold, temp32, lag_counter;\r
     SKP_int   CBimax, CBimax_new, CBimax_old, lag, start_lag, end_lag, lag_new;\r
-    SKP_int32 CC[ PITCH_EST_NB_CBKS_STAGE2_EXT ], CCmax, CCmax_b, CCmax_new_b, CCmax_new;\r
-    SKP_int32 energies_st3[  PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ];\r
-    SKP_int32 crosscorr_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ];\r
-    SKP_int32 lag_counter;\r
+    SKP_int32 CC[ PE_NB_CBKS_STAGE2_EXT ], CCmax, CCmax_b, CCmax_new_b, CCmax_new;\r
+    SKP_int32 energies_st3[  PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ];\r
+    SKP_int32 crosscorr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ];\r
     SKP_int   frame_length, frame_length_8kHz, frame_length_4kHz, max_sum_sq_length;\r
     SKP_int   sf_length, sf_length_8kHz, sf_length_4kHz;\r
     SKP_int   min_lag, min_lag_8kHz, min_lag_4kHz;\r
     SKP_int   max_lag, max_lag_8kHz, max_lag_4kHz;\r
-    SKP_int32 contour_bias, diff;\r
-    SKP_int32 lz, lshift;\r
-    SKP_int   cbk_offset, cbk_size, nb_cbks_stage2;\r
+    SKP_int32 contour_bias, diff, lz, lshift;\r
+    SKP_int   cbk_offset, nb_cbk_search, cbk_size;\r
     SKP_int32 delta_lag_log2_sqr_Q7, lag_log2_Q7, prevLag_log2_Q7, prev_lag_bias_Q15, corr_thres_Q15;\r
-\r
+    const SKP_int8 *Lag_CB_ptr;\r
     /* Check for valid sampling frequency */\r
     SKP_assert( Fs_kHz == 8 || Fs_kHz == 12 || Fs_kHz == 16 || Fs_kHz == 24 );\r
 \r
     /* Check for valid complexity setting */\r
-    SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX );\r
-    SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX );\r
+    SKP_assert( complexity >= SKP_Silk_PE_MIN_COMPLEX );\r
+    SKP_assert( complexity <= SKP_Silk_PE_MAX_COMPLEX );\r
 \r
     SKP_assert( search_thres1_Q16 >= 0 && search_thres1_Q16 <= (1<<16) );\r
     SKP_assert( search_thres2_Q15 >= 0 && search_thres2_Q15 <= (1<<15) );\r
 \r
     /* Setup frame lengths max / min lag for the sampling frequency */\r
-    frame_length      = PITCH_EST_FRAME_LENGTH_MS * Fs_kHz;\r
-    frame_length_4kHz = PITCH_EST_FRAME_LENGTH_MS * 4;\r
-    frame_length_8kHz = PITCH_EST_FRAME_LENGTH_MS * 8;\r
-    sf_length         = SKP_RSHIFT( frame_length,      3 );\r
-    sf_length_4kHz    = SKP_RSHIFT( frame_length_4kHz, 3 );\r
-    sf_length_8kHz    = SKP_RSHIFT( frame_length_8kHz, 3 );\r
-    min_lag           = PITCH_EST_MIN_LAG_MS * Fs_kHz;\r
-    min_lag_4kHz      = PITCH_EST_MIN_LAG_MS * 4;\r
-    min_lag_8kHz      = PITCH_EST_MIN_LAG_MS * 8;\r
-    max_lag           = PITCH_EST_MAX_LAG_MS * Fs_kHz;\r
-    max_lag_4kHz      = PITCH_EST_MAX_LAG_MS * 4;\r
-    max_lag_8kHz      = PITCH_EST_MAX_LAG_MS * 8;\r
-\r
-    SKP_memset( C, 0, sizeof( SKP_int16 ) * PITCH_EST_NB_SUBFR * ( ( PITCH_EST_MAX_LAG >> 1 ) + 5) );\r
+    frame_length      = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * Fs_kHz;\r
+    frame_length_4kHz = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * 4;\r
+    frame_length_8kHz = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * 8;\r
+    sf_length         = PE_SUBFR_LENGTH_MS * Fs_kHz;\r
+    sf_length_4kHz    = PE_SUBFR_LENGTH_MS * 4;\r
+    sf_length_8kHz    = PE_SUBFR_LENGTH_MS * 8;\r
+    min_lag           = PE_MIN_LAG_MS * Fs_kHz;\r
+    min_lag_4kHz      = PE_MIN_LAG_MS * 4;\r
+    min_lag_8kHz      = PE_MIN_LAG_MS * 8;\r
+    max_lag           = PE_MAX_LAG_MS * Fs_kHz;\r
+    max_lag_4kHz      = PE_MAX_LAG_MS * 4;\r
+    max_lag_8kHz      = PE_MAX_LAG_MS * 8;\r
+\r
+    SKP_memset( C, 0, sizeof( SKP_int16 ) * nb_subfr * ( ( PE_MAX_LAG >> 1 ) + 5) );\r
     \r
     /* Resample from input sampled at Fs_kHz to 8 kHz */\r
-    if( Fs_kHz == 12 ) {\r
-        SKP_int16 R23[ SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ];\r
-        SKP_memset( R23, 0, ( SigProc_Resample_2_3_coarsest_NUM_FIR_COEFS - 1 ) * sizeof( SKP_int16 ) );\r
-        \r
-        SKP_Silk_resample_2_3_coarsest( signal_8kHz, R23, signal, \r
-            PITCH_EST_FRAME_LENGTH_MS * 12, (SKP_int16*)scratch_mem );\r
-    } else if( Fs_kHz == 16 ) {\r
-        if( complexity == SigProc_PITCH_EST_MAX_COMPLEX ) {\r
-            SKP_assert( 4 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH );\r
-            SKP_memset( filt_state, 0, 4 * sizeof( SKP_int32 ) );\r
-\r
-            SKP_Silk_resample_1_2_coarse( signal, filt_state, signal_8kHz,\r
-                scratch_mem, frame_length_8kHz );\r
-        } else {\r
-            SKP_assert( 2 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH );\r
-            SKP_memset( filt_state, 0, 2 * sizeof( SKP_int32 ) );\r
-            \r
-            SKP_Silk_resample_1_2_coarsest( signal, filt_state, signal_8kHz,\r
-                scratch_mem, frame_length_8kHz );\r
-        }\r
+    if( Fs_kHz == 16 ) {\r
+        SKP_memset( filt_state, 0, 2 * sizeof( SKP_int32 ) );\r
+        SKP_Silk_resampler_down2( filt_state, signal_8kHz, signal, frame_length );\r
+    } else if ( Fs_kHz == 12 ) {\r
+        SKP_int32 R23[ 6 ];\r
+        SKP_memset( R23, 0, 6 * sizeof( SKP_int32 ) );\r
+        SKP_Silk_resampler_down2_3( R23, signal_8kHz, signal, frame_length );\r
     } else if( Fs_kHz == 24 ) {\r
-        /* Resample to 24 -> 8 khz */\r
-        SKP_assert( 7 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH );\r
-        SKP_memset( filt_state, 0, 7 * sizeof( SKP_int32 ) );\r
-\r
-        SKP_Silk_resample_1_3( signal_8kHz, filt_state, signal, 24 * PITCH_EST_FRAME_LENGTH_MS );\r
-    \r
+        SKP_int32 filt_state_fix[ 8 ];\r
+        SKP_memset( filt_state_fix, 0, 8 * sizeof(SKP_int32) );\r
+        SKP_Silk_resampler_down3( filt_state_fix, signal_8kHz, signal, frame_length );\r
     } else {\r
         SKP_assert( Fs_kHz == 8 );\r
-        SKP_memcpy( signal_8kHz, signal, frame_length_8kHz * sizeof( SKP_int16 ) );\r
-    }\r
-\r
-    /* Decimate again to 4 kHz. Set mem to zero */\r
-    if( complexity == SigProc_PITCH_EST_MAX_COMPLEX ) {\r
-        SKP_assert( 4 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH );\r
-        SKP_memset( filt_state, 0, 4 * sizeof( SKP_int32 ) );    \r
-        SKP_Silk_resample_1_2_coarse( signal_8kHz, filt_state,\r
-            signal_4kHz, scratch_mem, frame_length_4kHz );\r
-    } else {\r
-        SKP_assert( 2 <= PITCH_EST_MAX_DECIMATE_STATE_LENGTH );\r
-        SKP_memset( filt_state, 0, 2 * sizeof( SKP_int32 ) );    \r
-        SKP_Silk_resample_1_2_coarsest( signal_8kHz, filt_state,\r
-            signal_4kHz, scratch_mem, frame_length_4kHz );\r
+        SKP_memcpy( signal_8kHz, signal, frame_length_8kHz * sizeof(SKP_int16) );\r
     }\r
+    /* Decimate again to 4 kHz */\r
+    SKP_memset( filt_state, 0, 2 * sizeof( SKP_int32 ) );/* Set state to zero */\r
+    SKP_Silk_resampler_down2( filt_state, signal_4kHz, signal_8kHz, frame_length_8kHz );\r
 \r
     /* Low-pass filter */\r
     for( i = frame_length_4kHz - 1; i > 0; i-- ) {\r
@@ -210,7 +159,7 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
     *******************************************************************************/\r
     \r
     /* Inner product is calculated with different lengths, so scale for the worst case */\r
-    max_sum_sq_length = SKP_max_32( sf_length_8kHz, SKP_RSHIFT( frame_length_4kHz, 1 ) );\r
+    max_sum_sq_length = SKP_max_32( sf_length_8kHz, SKP_LSHIFT( sf_length_4kHz, 2 ) );\r
     shift = SKP_FIX_P_Ana_find_scaling( signal_4kHz, frame_length_4kHz, max_sum_sq_length );\r
     if( shift > 0 ) {\r
         for( i = 0; i < frame_length_4kHz; i++ ) {\r
@@ -221,8 +170,8 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
     /******************************************************************************\r
     * FIRST STAGE, operating in 4 khz\r
     ******************************************************************************/\r
-    target_ptr = &signal_4kHz[ SKP_RSHIFT( frame_length_4kHz, 1 ) ];\r
-    for( k = 0; k < 2; k++ ) {\r
+    target_ptr = &signal_4kHz[ SKP_LSHIFT( sf_length_4kHz, 2 ) ];\r
+    for( k = 0; k < nb_subfr >> 1; k++ ) {\r
         /* Check that we are within range of the array */\r
         SKP_assert( target_ptr >= signal_4kHz );\r
         SKP_assert( target_ptr + sf_length_8kHz <= signal_4kHz + frame_length_4kHz );\r
@@ -266,30 +215,39 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
     }\r
 \r
     /* Combine two subframes into single correlation measure and apply short-lag bias */\r
-    for( i = max_lag_4kHz; i >= min_lag_4kHz; i-- ) {\r
-        sum = (SKP_int32)C[ 0 ][ i ] + (SKP_int32)C[ 1 ][ i ];                /* Q0 */\r
-        SKP_assert( SKP_RSHIFT( sum, 1 ) == SKP_SAT16( SKP_RSHIFT( sum, 1 ) ) );\r
-        sum = SKP_RSHIFT( sum, 1 );                                           /* Q-1 */\r
-        SKP_assert( SKP_LSHIFT( (SKP_int32)-i, 4 ) == SKP_SAT16( SKP_LSHIFT( (SKP_int32)-i, 4 ) ) );\r
-        sum = SKP_SMLAWB( sum, sum, SKP_LSHIFT( -i, 4 ) );                    /* Q-1 */\r
-        SKP_assert( sum == SKP_SAT16( sum ) );\r
-        C[ 0 ][ i ] = (SKP_int16)sum;                                         /* Q-1 */\r
+    if( nb_subfr == PE_MAX_NB_SUBFR ) {\r
+        for( i = max_lag_4kHz; i >= min_lag_4kHz; i-- ) {\r
+            sum = (SKP_int32)C[ 0 ][ i ] + (SKP_int32)C[ 1 ][ i ];                /* Q0 */\r
+            SKP_assert( SKP_RSHIFT( sum, 1 ) == SKP_SAT16( SKP_RSHIFT( sum, 1 ) ) );\r
+            sum = SKP_RSHIFT( sum, 1 );                                           /* Q-1 */\r
+            SKP_assert( SKP_LSHIFT( (SKP_int32)-i, 4 ) == SKP_SAT16( SKP_LSHIFT( (SKP_int32)-i, 4 ) ) );\r
+            sum = SKP_SMLAWB( sum, sum, SKP_LSHIFT( -i, 4 ) );                    /* Q-1 */\r
+            SKP_assert( sum == SKP_SAT16( sum ) );\r
+            C[ 0 ][ i ] = (SKP_int16)sum;                                         /* Q-1 */\r
+        }\r
+    } else {\r
+        /* Only short lag bias */\r
+        for( i = max_lag_4kHz; i >= min_lag_4kHz; i-- ) {\r
+            sum = (SKP_int32)C[ 0 ][ i ];\r
+            sum = SKP_SMLAWB( sum, sum, SKP_LSHIFT( -i, 4 ) );                    /* Q-1 */\r
+            C[ 0 ][ i ] = (SKP_int16)sum;                                         /* Q-1 */\r
+        }\r
     }\r
-\r
     /* Sort */\r
     length_d_srch = 5 + complexity;\r
-    SKP_assert( length_d_srch <= PITCH_EST_D_SRCH_LENGTH );\r
-    SKP_Silk_insertion_sort_decreasing_int16( &C[ 0 ][ min_lag_4kHz ], d_srch, max_lag_4kHz - min_lag_4kHz + 1, length_d_srch );\r
+    SKP_assert( length_d_srch <= PE_D_SRCH_LENGTH );\r
+      SKP_Silk_insertion_sort_decreasing_int16( &C[ 0 ][ min_lag_4kHz ], d_srch, max_lag_4kHz - min_lag_4kHz + 1, length_d_srch );\r
 \r
     /* Escape if correlation is very low already here */\r
-    target_ptr = &signal_4kHz[ SKP_RSHIFT( frame_length_4kHz, 1 ) ];\r
-    energy = SKP_Silk_inner_prod_aligned( target_ptr, target_ptr, SKP_RSHIFT( frame_length_4kHz, 1 ) );\r
+    target_ptr = &signal_4kHz[ SKP_SMULBB( sf_length_4kHz, nb_subfr ) ];\r
+    energy = SKP_Silk_inner_prod_aligned( target_ptr, target_ptr, SKP_LSHIFT( sf_length_4kHz, 2 ) );\r
     energy = SKP_ADD_SAT32( energy, 1000 );                                  /* Q0 */\r
     Cmax = (SKP_int)C[ 0 ][ min_lag_4kHz ];                                  /* Q-1 */\r
     threshold = SKP_SMULBB( Cmax, Cmax );                                    /* Q-2 */\r
     /* Compare in Q-2 domain */\r
+\r
     if( SKP_RSHIFT( energy, 4 + 2 ) > threshold ) {                            \r
-        SKP_memset( pitch_out, 0, PITCH_EST_NB_SUBFR * sizeof( SKP_int ) );\r
+        SKP_memset( pitch_out, 0, nb_subfr * sizeof( SKP_int ) );\r
         *LTPCorr_Q15  = 0;\r
         *lagIndex     = 0;\r
         *contourIndex = 0;\r
@@ -359,10 +317,10 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
     /********************************************************************************* \r
     * Find energy of each subframe projected onto its history, for a range of delays\r
     *********************************************************************************/\r
-    SKP_memset( C, 0, PITCH_EST_NB_SUBFR * ( ( PITCH_EST_MAX_LAG >> 1 ) + 5 ) * sizeof( SKP_int16 ) );\r
+    SKP_memset( C, 0, PE_MAX_NB_SUBFR * ( ( PE_MAX_LAG >> 1 ) + 5 ) * sizeof( SKP_int16 ) );\r
     \r
-    target_ptr = &signal_8kHz[ frame_length_4kHz ]; /* point to middle of frame */\r
-    for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) {\r
+    target_ptr = &signal_8kHz[ PE_LTP_MEM_LENGTH_MS * 8 ];\r
+    for( k = 0; k < nb_subfr; k++ ) {\r
 \r
         /* Check that we are within range of the array */\r
         SKP_assert( target_ptr >= signal_8kHz );\r
@@ -375,7 +333,7 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
             basis_ptr = target_ptr - d;\r
 \r
             /* Check that we are within range of the array */\r
-            SKP_assert( basis_ptr >= signal_8kHz );\r
+             SKP_assert( basis_ptr >= signal_8kHz );\r
             SKP_assert( basis_ptr + sf_length_8kHz <= signal_8kHz + frame_length_8kHz );\r
         \r
             cross_corr   = SKP_Silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz );\r
@@ -383,13 +341,13 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
             if( cross_corr > 0 ) {\r
                 energy = SKP_max( energy_target, energy_basis ); /* Find max to make sure first division < 1.0 */\r
                 lz = SKP_Silk_CLZ32( cross_corr );\r
-                lshift = SKP_LIMIT( lz - 1, 0, 15 );\r
+                lshift = SKP_LIMIT_32( lz - 1, 0, 15 );\r
                 temp32 = SKP_DIV32( SKP_LSHIFT( cross_corr, lshift ), SKP_RSHIFT( energy, 15 - lshift ) + 1 ); /* Q15 */\r
                 SKP_assert( temp32 == SKP_SAT16( temp32 ) );\r
                 temp32 = SKP_SMULWB( cross_corr, temp32 ); /* Q(-1), cc * ( cc / max(b, t) ) */\r
                 temp32 = SKP_ADD_SAT32( temp32, temp32 );  /* Q(0) */\r
                 lz = SKP_Silk_CLZ32( temp32 );\r
-                lshift = SKP_LIMIT( lz - 1, 0, 15 );\r
+                lshift = SKP_LIMIT_32( lz - 1, 0, 15 );\r
                 energy = SKP_min( energy_target, energy_basis );\r
                 C[ k ][ d ] = SKP_DIV32( SKP_LSHIFT( temp32, lshift ), SKP_RSHIFT( energy, 15 - lshift ) + 1 ); // Q15\r
             } else {\r
@@ -421,28 +379,37 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
         prevLag_log2_Q7 = 0;\r
     }\r
     SKP_assert( search_thres2_Q15 == SKP_SAT16( search_thres2_Q15 ) );\r
-    corr_thres_Q15 = SKP_RSHIFT( SKP_SMULBB( search_thres2_Q15, search_thres2_Q15 ), 13 );\r
-\r
-    /* If input is 8 khz use a larger codebook here because it is last stage */\r
-    if( Fs_kHz == 8 && complexity > SigProc_PITCH_EST_MIN_COMPLEX ) {\r
-        nb_cbks_stage2 = PITCH_EST_NB_CBKS_STAGE2_EXT;    \r
-    } else {\r
-        nb_cbks_stage2 = PITCH_EST_NB_CBKS_STAGE2;\r
+    /* Setup stage 2 codebook based on number of subframes */\r
+    if( nb_subfr == PE_MAX_NB_SUBFR ) {\r
+        cbk_size   = PE_NB_CBKS_STAGE2_EXT;\r
+        Lag_CB_ptr = &SKP_Silk_CB_lags_stage2[ 0 ][ 0 ];\r
+        if( Fs_kHz == 8 && complexity > SKP_Silk_PE_MIN_COMPLEX ) {\r
+            /* If input is 8 khz use a larger codebook here because it is last stage */\r
+            nb_cbk_search = PE_NB_CBKS_STAGE2_EXT;\r
+        } else {\r
+            nb_cbk_search = PE_NB_CBKS_STAGE2;\r
+        }\r
+        corr_thres_Q15 = SKP_RSHIFT( SKP_SMULBB( search_thres2_Q15, search_thres2_Q15 ), 13 );\r
+    }else{\r
+        cbk_size       = PE_NB_CBKS_STAGE2_10MS;\r
+        Lag_CB_ptr     = &SKP_Silk_CB_lags_stage2_10_ms[ 0 ][ 0 ];\r
+        nb_cbk_search  = PE_NB_CBKS_STAGE2_10MS;\r
+        corr_thres_Q15 = SKP_RSHIFT( SKP_SMULBB( search_thres2_Q15, search_thres2_Q15 ), 14 );\r
     }\r
 \r
     for( k = 0; k < length_d_srch; k++ ) {\r
         d = d_srch[ k ];\r
-        for( j = 0; j < nb_cbks_stage2; j++ ) {\r
+        for( j = 0; j < nb_cbk_search; j++ ) {\r
             CC[ j ] = 0;\r
-            for( i = 0; i < PITCH_EST_NB_SUBFR; i++ ) {\r
+            for( i = 0; i < nb_subfr; i++ ) {\r
                 /* Try all codebooks */\r
-                CC[ j ] = CC[ j ] + (SKP_int32)C[ i ][ d + SKP_Silk_CB_lags_stage2[ i ][ j ] ];\r
+                CC[ j ] = CC[ j ] + (SKP_int32)C[ i ][ d + matrix_ptr( Lag_CB_ptr, i, j, cbk_size )];\r
             }\r
         }\r
         /* Find best codebook */\r
         CCmax_new = SKP_int32_MIN;\r
         CBimax_new = 0;\r
-        for( i = 0; i < nb_cbks_stage2; i++ ) {\r
+        for( i = 0; i < nb_cbk_search; i++ ) {\r
             if( CC[ i ] > CCmax_new ) {\r
                 CCmax_new = CC[ i ];\r
                 CBimax_new = i;\r
@@ -452,16 +419,16 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
         /* Bias towards shorter lags */\r
         lag_log2_Q7 = SKP_Silk_lin2log( (SKP_int32)d ); /* Q7 */\r
         SKP_assert( lag_log2_Q7 == SKP_SAT16( lag_log2_Q7 ) );\r
-        SKP_assert( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15 == SKP_SAT16( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15 ) );\r
-        CCmax_new_b = CCmax_new - SKP_RSHIFT( SKP_SMULBB( PITCH_EST_NB_SUBFR * PITCH_EST_SHORTLAG_BIAS_Q15, lag_log2_Q7 ), 7 ); /* Q15 */\r
+        SKP_assert( nb_subfr * PE_SHORTLAG_BIAS_Q15 == SKP_SAT16( nb_subfr * PE_SHORTLAG_BIAS_Q15 ) );\r
+        CCmax_new_b = CCmax_new - SKP_RSHIFT( SKP_SMULBB( nb_subfr * PE_SHORTLAG_BIAS_Q15, lag_log2_Q7 ), 7 ); /* Q15 */\r
 \r
         /* Bias towards previous lag */\r
-        SKP_assert( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15 == SKP_SAT16( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15 ) );\r
+        SKP_assert( nb_subfr * PE_PREVLAG_BIAS_Q15 == SKP_SAT16( nb_subfr * PE_PREVLAG_BIAS_Q15 ) );\r
         if( prevLag > 0 ) {\r
             delta_lag_log2_sqr_Q7 = lag_log2_Q7 - prevLag_log2_Q7;\r
             SKP_assert( delta_lag_log2_sqr_Q7 == SKP_SAT16( delta_lag_log2_sqr_Q7 ) );\r
             delta_lag_log2_sqr_Q7 = SKP_RSHIFT( SKP_SMULBB( delta_lag_log2_sqr_Q7, delta_lag_log2_sqr_Q7 ), 7 );\r
-            prev_lag_bias_Q15 = SKP_RSHIFT( SKP_SMULBB( PITCH_EST_NB_SUBFR * PITCH_EST_PREVLAG_BIAS_Q15, ( *LTPCorr_Q15 ) ), 15 ); /* Q15 */\r
+            prev_lag_bias_Q15 = SKP_RSHIFT( SKP_SMULBB( nb_subfr * PE_PREVLAG_BIAS_Q15, ( *LTPCorr_Q15 ) ), 15 ); /* Q15 */\r
             prev_lag_bias_Q15 = SKP_DIV32( SKP_MUL( prev_lag_bias_Q15, delta_lag_log2_sqr_Q7 ), delta_lag_log2_sqr_Q7 + ( 1 << 6 ) );\r
             CCmax_new_b -= prev_lag_bias_Q15; /* Q15 */\r
         }\r
@@ -476,7 +443,7 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
 \r
     if( lag == -1 ) {\r
         /* No suitable candidate found */\r
-        SKP_memset( pitch_out, 0, PITCH_EST_NB_SUBFR * sizeof( SKP_int ) );\r
+        SKP_memset( pitch_out, 0, nb_subfr * sizeof( SKP_int ) );\r
         *LTPCorr_Q15  = 0;\r
         *lagIndex     = 0;\r
         *contourIndex = 0;\r
@@ -515,7 +482,7 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
             lag = SKP_SMULBB( lag, 3 );\r
         }\r
 \r
-        lag = SKP_LIMIT( lag, min_lag, max_lag );\r
+        lag = SKP_LIMIT_int( lag, min_lag, max_lag );\r
         start_lag = SKP_max_int( lag - 2, min_lag );\r
         end_lag   = SKP_min_int( lag + 2, max_lag );\r
         lag_new   = lag;                                    /* to avoid undefined lag */\r
@@ -525,36 +492,44 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
 \r
         CCmax = SKP_int32_MIN;\r
         /* pitch lags according to second stage */\r
-        for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) {\r
+        for( k = 0; k < nb_subfr; k++ ) {\r
             pitch_out[ k ] = lag + 2 * SKP_Silk_CB_lags_stage2[ k ][ CBimax_old ];\r
         }\r
         /* Calculate the correlations and energies needed in stage 3 */\r
-        SKP_FIX_P_Ana_calc_corr_st3(  crosscorr_st3, input_signal_ptr, start_lag, sf_length, complexity );\r
-        SKP_FIX_P_Ana_calc_energy_st3( energies_st3, input_signal_ptr, start_lag, sf_length, complexity );\r
+        SKP_FIX_P_Ana_calc_corr_st3(  crosscorr_st3, input_signal_ptr, start_lag, sf_length, nb_subfr, complexity );\r
+        SKP_FIX_P_Ana_calc_energy_st3( energies_st3, input_signal_ptr, start_lag, sf_length, nb_subfr, complexity );\r
 \r
         lag_counter = 0;\r
         SKP_assert( lag == SKP_SAT16( lag ) );\r
-        contour_bias = SKP_DIV32_16( PITCH_EST_FLATCONTOUR_BIAS_Q20, lag );\r
-\r
-        /* Setup cbk parameters acording to complexity setting */\r
-        cbk_size   = (SKP_int)SKP_Silk_cbk_sizes_stage3[   complexity ];\r
-        cbk_offset = (SKP_int)SKP_Silk_cbk_offsets_stage3[ complexity ];\r
-\r
+        contour_bias = SKP_DIV32_16( PE_FLATCONTOUR_BIAS_Q20, lag );\r
+\r
+        /* Setup cbk parameters acording to complexity setting and frame length */\r
+        if( nb_subfr == PE_MAX_NB_SUBFR ) {\r
+            nb_cbk_search   = (SKP_int)SKP_Silk_nb_cbk_searchs_stage3[   complexity ];\r
+            cbk_size        = PE_NB_CBKS_STAGE3_MAX;\r
+            cbk_offset      = (SKP_int)SKP_Silk_cbk_offsets_stage3[ complexity ];\r
+            Lag_CB_ptr      = &SKP_Silk_CB_lags_stage3[ 0 ][ 0 ];\r
+        } else {\r
+            nb_cbk_search   = PE_NB_CBKS_STAGE3_10MS;\r
+            cbk_size        = PE_NB_CBKS_STAGE3_10MS;\r
+            cbk_offset      = 0;\r
+            Lag_CB_ptr      = &SKP_Silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];\r
+        }\r
         for( d = start_lag; d <= end_lag; d++ ) {\r
-            for( j = cbk_offset; j < ( cbk_offset + cbk_size ); j++ ) {\r
+            for( j = cbk_offset; j < ( cbk_offset + nb_cbk_search ); j++ ) {\r
                 cross_corr = 0;\r
                 energy     = 0;\r
-                for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) {\r
-                    SKP_assert( PITCH_EST_NB_SUBFR == 4 );\r
+                for( k = 0; k < nb_subfr; k++ ) {\r
+                    SKP_assert( PE_MAX_NB_SUBFR == 4 );\r
                     energy     += SKP_RSHIFT( energies_st3[  k ][ j ][ lag_counter ], 2 ); /* use mean, to avoid overflow */\r
-                    SKP_assert( energy >= 0 );\r
+                     SKP_assert( energy >= 0 );\r
                     cross_corr += SKP_RSHIFT( crosscorr_st3[ k ][ j ][ lag_counter ], 2 ); /* use mean, to avoid overflow */\r
                 }\r
                 if( cross_corr > 0 ) {\r
                     /* Divide cross_corr / energy and get result in Q15 */\r
                     lz = SKP_Silk_CLZ32( cross_corr );\r
                     /* Divide with result in Q13, cross_corr could be larger than energy */\r
-                    lshift = SKP_LIMIT( lz - 1, 0, 13 );\r
+                    lshift = SKP_LIMIT_32( lz - 1, 0, 13 );\r
                     CCmax_new = SKP_DIV32( SKP_LSHIFT( cross_corr, lshift ), SKP_RSHIFT( energy, 13 - lshift ) + 1 );\r
                     CCmax_new = SKP_SAT16( CCmax_new );\r
                     CCmax_new = SKP_SMULWB( cross_corr, CCmax_new );\r
@@ -565,7 +540,7 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
                         CCmax_new = SKP_LSHIFT( CCmax_new, 3 );\r
                     }\r
                     /* Reduce depending on flatness of contour */\r
-                    diff = j - SKP_RSHIFT( PITCH_EST_NB_CBKS_STAGE3_MAX, 1 );\r
+                    diff = j - SKP_RSHIFT( cbk_size, 1 );\r
                     diff = SKP_MUL( diff, diff );\r
                     diff = SKP_int16_MAX - SKP_RSHIFT( SKP_MUL( contour_bias, diff ), 5 ); /* Q20 -> Q15 */\r
                     SKP_assert( diff == SKP_SAT16( diff ) );\r
@@ -583,8 +558,8 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
             lag_counter++;\r
         }\r
 \r
-        for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) {\r
-            pitch_out[ k ] = lag_new + SKP_Silk_CB_lags_stage3[ k ][ CBimax ];\r
+        for( k = 0; k < nb_subfr; k++ ) {\r
+            pitch_out[ k ] = lag_new + matrix_ptr( Lag_CB_ptr, k, CBimax, cbk_size );\r
         }\r
         *lagIndex = lag_new - min_lag;\r
         *contourIndex = CBimax;\r
@@ -592,7 +567,7 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
         /* Save Lags and correlation */\r
         CCmax = SKP_max( CCmax, 0 );\r
         *LTPCorr_Q15 = (SKP_int)SKP_Silk_SQRT_APPROX( SKP_LSHIFT( CCmax, 13 ) ); /* Output normalized correlation */\r
-        for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) {\r
+        for( k = 0; k < nb_subfr; k++ ) {\r
             pitch_out[ k ] = lag + SKP_Silk_CB_lags_stage2[ k ][ CBimax ];\r
         }\r
         *lagIndex = lag - min_lag_8kHz;\r
@@ -608,31 +583,47 @@ SKP_int SKP_Silk_pitch_analysis_core(  /* O    Voicing estimate: 0 voiced, 1 unv
 /* the whole lag codebook for all the searched offset lags (lag +- 2),   */\r
 /*************************************************************************/\r
 void SKP_FIX_P_Ana_calc_corr_st3(\r
-    SKP_int32        cross_corr_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ],/* (O) 3 DIM correlation array */\r
+    SKP_int32        cross_corr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ],/* (O) 3 DIM correlation array */\r
     const SKP_int16  signal[],                        /* I vector to correlate         */\r
     SKP_int          start_lag,                       /* I lag offset to search around */\r
     SKP_int          sf_length,                       /* I length of a 5 ms subframe   */\r
+    SKP_int          nb_subfr,                        /* I number of subframes         */\r
     SKP_int          complexity                       /* I Complexity setting          */\r
 )\r
 {\r
     const SKP_int16 *target_ptr, *basis_ptr;\r
-    SKP_int32    cross_corr;\r
-    SKP_int        i, j, k, lag_counter;\r
-    SKP_int        cbk_offset, cbk_size, delta, idx;\r
-    SKP_int32    scratch_mem[ SCRATCH_SIZE ];\r
-\r
-    SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX );\r
-    SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX );\r
-\r
-    cbk_offset = SKP_Silk_cbk_offsets_stage3[ complexity ];\r
-    cbk_size   = SKP_Silk_cbk_sizes_stage3[   complexity ];\r
+    SKP_int32 cross_corr;\r
+    SKP_int   i, j, k, lag_counter, lag_low, lag_high;\r
+    SKP_int   cbk_offset, nb_cbk_search, delta, idx, cbk_size;\r
+    SKP_int32 scratch_mem[ SCRATCH_SIZE ];\r
+    const SKP_int8 *Lag_range_ptr, *Lag_CB_ptr;\r
+\r
+    SKP_assert( complexity >= SKP_Silk_PE_MIN_COMPLEX );\r
+    SKP_assert( complexity <= SKP_Silk_PE_MAX_COMPLEX );\r
+\r
+    if( nb_subfr == PE_MAX_NB_SUBFR ){\r
+        Lag_range_ptr = &SKP_Silk_Lag_range_stage3[ complexity ][ 0 ][ 0 ];\r
+        Lag_CB_ptr    = &SKP_Silk_CB_lags_stage3[ 0 ][ 0 ];\r
+        cbk_offset    = SKP_Silk_cbk_offsets_stage3[ complexity ];\r
+        nb_cbk_search = SKP_Silk_nb_cbk_searchs_stage3[   complexity ];\r
+        cbk_size      = PE_NB_CBKS_STAGE3_MAX;\r
+    } else {\r
+        SKP_assert( nb_subfr == PE_MAX_NB_SUBFR >> 1);\r
+        Lag_range_ptr = &SKP_Silk_Lag_range_stage3_10_ms[ 0 ][ 0 ];\r
+        Lag_CB_ptr    = &SKP_Silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];\r
+        cbk_offset    = 0;\r
+        nb_cbk_search = PE_NB_CBKS_STAGE3_10MS;\r
+        cbk_size      = PE_NB_CBKS_STAGE3_10MS;\r
+    }\r
 \r
     target_ptr = &signal[ SKP_LSHIFT( sf_length, 2 ) ]; /* Pointer to middle of frame */\r
-    for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) {\r
+    for( k = 0; k < nb_subfr; k++ ) {\r
         lag_counter = 0;\r
 \r
         /* Calculate the correlations for each subframe */\r
-        for( j = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ]; j <= SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 1 ]; j++ ) {\r
+        lag_low  = matrix_ptr( Lag_range_ptr, k, 0, 2 );\r
+        lag_high = matrix_ptr( Lag_range_ptr, k, 1, 2 );\r
+        for( j = lag_low; j <= lag_high; j++ ) {\r
             basis_ptr = target_ptr - ( start_lag + j );\r
             cross_corr = SKP_Silk_inner_prod_aligned( (SKP_int16*)target_ptr, (SKP_int16*)basis_ptr, sf_length );\r
             SKP_assert( lag_counter < SCRATCH_SIZE );\r
@@ -640,12 +631,12 @@ void SKP_FIX_P_Ana_calc_corr_st3(
             lag_counter++;\r
         }\r
 \r
-        delta = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ];\r
-        for( i = cbk_offset; i < ( cbk_offset + cbk_size ); i++ ) { \r
+        delta = matrix_ptr( Lag_range_ptr, k, 0, 2 );\r
+        for( i = cbk_offset; i < ( cbk_offset + nb_cbk_search ); i++ ) { \r
             /* Fill out the 3 dim array that stores the correlations for */\r
             /* each code_book vector for each start lag */\r
-            idx = SKP_Silk_CB_lags_stage3[ k ][ i ] - delta;\r
-            for( j = 0; j < PITCH_EST_NB_STAGE3_LAGS; j++ ) {\r
+            idx = matrix_ptr( Lag_CB_ptr, k, i, cbk_size ) - delta;\r
+            for( j = 0; j < PE_NB_STAGE3_LAGS; j++ ) {\r
                 SKP_assert( idx + j < SCRATCH_SIZE );\r
                 SKP_assert( idx + j < lag_counter );\r
                 cross_corr_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ];\r
@@ -660,37 +651,51 @@ void SKP_FIX_P_Ana_calc_corr_st3(
 /* calculated recursively.                                          */\r
 /********************************************************************/\r
 void SKP_FIX_P_Ana_calc_energy_st3(\r
-    SKP_int32        energies_st3[ PITCH_EST_NB_SUBFR ][ PITCH_EST_NB_CBKS_STAGE3_MAX ][ PITCH_EST_NB_STAGE3_LAGS ],/* (O) 3 DIM energy array */\r
+    SKP_int32        energies_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ],/* (O) 3 DIM energy array */\r
     const SKP_int16  signal[],                        /* I vector to calc energy in    */\r
     SKP_int          start_lag,                       /* I lag offset to search around */\r
     SKP_int          sf_length,                       /* I length of one 5 ms subframe */\r
+    SKP_int          nb_subfr,                     /* I number of subframes         */\r
     SKP_int          complexity                       /* I Complexity setting          */\r
 )\r
 {\r
     const SKP_int16 *target_ptr, *basis_ptr;\r
-    SKP_int32    energy;\r
-    SKP_int        k, i, j, lag_counter;\r
-    SKP_int        cbk_offset, cbk_size, delta, idx;\r
-    SKP_int32    scratch_mem[ SCRATCH_SIZE ];\r
-\r
-    SKP_assert( complexity >= SigProc_PITCH_EST_MIN_COMPLEX );\r
-    SKP_assert( complexity <= SigProc_PITCH_EST_MAX_COMPLEX );\r
-\r
-    cbk_offset = SKP_Silk_cbk_offsets_stage3[ complexity ];\r
-    cbk_size   = SKP_Silk_cbk_sizes_stage3[   complexity ];\r
-\r
+    SKP_int32 energy;\r
+    SKP_int   k, i, j, lag_counter;\r
+    SKP_int   cbk_offset, nb_cbk_search, delta, idx, cbk_size, lag_diff;\r
+    SKP_int32 scratch_mem[ SCRATCH_SIZE ];\r
+    const SKP_int8 *Lag_range_ptr, *Lag_CB_ptr;\r
+\r
+    SKP_assert( complexity >= SKP_Silk_PE_MIN_COMPLEX );\r
+    SKP_assert( complexity <= SKP_Silk_PE_MAX_COMPLEX );\r
+\r
+    if( nb_subfr == PE_MAX_NB_SUBFR ){\r
+        Lag_range_ptr = &SKP_Silk_Lag_range_stage3[ complexity ][ 0 ][ 0 ];\r
+        Lag_CB_ptr    = &SKP_Silk_CB_lags_stage3[ 0 ][ 0 ];\r
+        cbk_offset    = SKP_Silk_cbk_offsets_stage3[ complexity ];\r
+        nb_cbk_search = SKP_Silk_nb_cbk_searchs_stage3[   complexity ];\r
+        cbk_size      = PE_NB_CBKS_STAGE3_MAX;\r
+    } else {\r
+        SKP_assert( nb_subfr == PE_MAX_NB_SUBFR >> 1);\r
+        Lag_range_ptr = &SKP_Silk_Lag_range_stage3_10_ms[ 0 ][ 0 ];\r
+        Lag_CB_ptr    = &SKP_Silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];\r
+        cbk_offset    = 0;\r
+        nb_cbk_search = PE_NB_CBKS_STAGE3_10MS;\r
+        cbk_size      = PE_NB_CBKS_STAGE3_10MS;\r
+    }\r
     target_ptr = &signal[ SKP_LSHIFT( sf_length, 2 ) ];\r
-    for( k = 0; k < PITCH_EST_NB_SUBFR; k++ ) {\r
+    for( k = 0; k < nb_subfr; k++ ) {\r
         lag_counter = 0;\r
 \r
         /* Calculate the energy for first lag */\r
-        basis_ptr = target_ptr - ( start_lag + SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ] );\r
+        basis_ptr = target_ptr - ( start_lag + matrix_ptr( Lag_range_ptr, k, 0, 2 ) );\r
         energy = SKP_Silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length );\r
         SKP_assert( energy >= 0 );\r
         scratch_mem[ lag_counter ] = energy;\r
         lag_counter++;\r
 \r
-        for( i = 1; i < ( SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 1 ] - SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ] + 1 ); i++ ) {\r
+        lag_diff = ( matrix_ptr( Lag_range_ptr, k, 1, 2 ) -  matrix_ptr( Lag_range_ptr, k, 0, 2 ) + 1 );\r
+        for( i = 1; i < lag_diff; i++ ) {\r
             /* remove part outside new window */\r
             energy -= SKP_SMULBB( basis_ptr[ sf_length - i ], basis_ptr[ sf_length - i ] );\r
             SKP_assert( energy >= 0 );\r
@@ -703,16 +708,16 @@ void SKP_FIX_P_Ana_calc_energy_st3(
             lag_counter++;\r
         }\r
 \r
-        delta = SKP_Silk_Lag_range_stage3[ complexity ][ k ][ 0 ];\r
-        for( i = cbk_offset; i < ( cbk_offset + cbk_size ); i++ ) { \r
+        delta = matrix_ptr( Lag_range_ptr, k, 0, 2 );\r
+        for( i = cbk_offset; i < ( cbk_offset + nb_cbk_search ); i++ ) { \r
             /* Fill out the 3 dim array that stores the correlations for    */\r
-            /* each code_book vector for each start lag                        */\r
-            idx = SKP_Silk_CB_lags_stage3[ k ][ i ] - delta;\r
-            for( j = 0; j < PITCH_EST_NB_STAGE3_LAGS; j++ ) {\r
+            /* each code_book vector for each start lag                     */\r
+            idx = matrix_ptr( Lag_CB_ptr, k, i, cbk_size ) - delta;\r
+            for( j = 0; j < PE_NB_STAGE3_LAGS; j++ ) {\r
                 SKP_assert( idx + j < SCRATCH_SIZE );\r
                 SKP_assert( idx + j < lag_counter );\r
                 energies_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ];\r
-                SKP_assert( energies_st3[ k ][ i ][ j ] >= 0.0f );\r
+                SKP_assert( energies_st3[ k ][ i ][ j ] >= 0 );\r
             }\r
         }\r
         target_ptr += sf_length;\r