SILK update
authorKoen Vos <koen.vos@skype.net>
Wed, 10 Nov 2010 05:36:21 +0000 (13:36 +0800)
committerJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Wed, 10 Nov 2010 05:36:21 +0000 (13:36 +0800)
114 files changed:
Makefile.am
interface/SKP_Silk_SDK_API.h
interface/SKP_Silk_control.h
interface/SKP_Silk_errors.h
src_FIX/SKP_Silk_HP_variable_cutoff_FIX.c
src_FIX/SKP_Silk_LTP_scale_ctrl_FIX.c
src_FIX/SKP_Silk_NLSF_MSVQ_encode_FIX.c
src_FIX/SKP_Silk_NLSF_VQ_rate_distortion_FIX.c
src_FIX/SKP_Silk_NLSF_VQ_sum_error_FIX.c
src_FIX/SKP_Silk_control_codec_FIX.c
src_FIX/SKP_Silk_corrMatrix_FIX.c
src_FIX/SKP_Silk_encode_frame_FIX.c
src_FIX/SKP_Silk_find_LPC_FIX.c
src_FIX/SKP_Silk_find_LTP_FIX.c
src_FIX/SKP_Silk_find_pitch_lags_FIX.c
src_FIX/SKP_Silk_find_pred_coefs_FIX.c
src_FIX/SKP_Silk_init_encoder_FIX.c
src_FIX/SKP_Silk_main_FIX.h
src_FIX/SKP_Silk_noise_shape_analysis_FIX.c
src_FIX/SKP_Silk_prefilter_FIX.c
src_FIX/SKP_Silk_process_gains_FIX.c
src_FIX/SKP_Silk_solve_LS_FIX.c
src_FIX/SKP_Silk_structs_FIX.h
src_FIX/SKP_Silk_warped_autocorrelation_FIX.c
src_FLP/SKP_Silk_HP_variable_cutoff_FLP.c
src_FLP/SKP_Silk_LPC_analysis_filter_FLP.c
src_FLP/SKP_Silk_LTP_analysis_filter_FLP.c
src_FLP/SKP_Silk_LTP_scale_ctrl_FLP.c
src_FLP/SKP_Silk_NLSF_MSVQ_decode_FLP.c
src_FLP/SKP_Silk_NLSF_MSVQ_encode_FLP.c
src_FLP/SKP_Silk_VQ_nearest_neighbor_FLP.c
src_FLP/SKP_Silk_apply_sine_window_FLP.c
src_FLP/SKP_Silk_control_codec_FLP.c
src_FLP/SKP_Silk_encode_frame_FLP.c
src_FLP/SKP_Silk_find_LPC_FLP.c
src_FLP/SKP_Silk_find_LTP_FLP.c
src_FLP/SKP_Silk_find_pitch_lags_FLP.c
src_FLP/SKP_Silk_init_encoder_FLP.c
src_FLP/SKP_Silk_main_FLP.h
src_FLP/SKP_Silk_noise_shape_analysis_FLP.c
src_FLP/SKP_Silk_prefilter_FLP.c
src_FLP/SKP_Silk_process_gains_FLP.c
src_FLP/SKP_Silk_quant_LTP_gains_FLP.c
src_FLP/SKP_Silk_solve_LS_FLP.c
src_FLP/SKP_Silk_structs_FLP.h
src_FLP/SKP_Silk_tables_FLP.h
src_FLP/SKP_Silk_tables_NLSF_CB0_10_FLP.c
src_FLP/SKP_Silk_tables_NLSF_CB0_16_FLP.c
src_FLP/SKP_Silk_tables_NLSF_CB1_10_FLP.c
src_FLP/SKP_Silk_tables_NLSF_CB1_16_FLP.c
src_FLP/SKP_Silk_tables_other_FLP.c
src_FLP/SKP_Silk_wrappers_FLP.c
src_SigProc_FIX/SKP_Silk_A2NLSF.c
src_SigProc_FIX/SKP_Silk_LPC_inv_pred_gain.c
src_SigProc_FIX/SKP_Silk_LPC_stabilize.c
src_SigProc_FIX/SKP_Silk_NLSF_VQ_weights_laroia.c
src_SigProc_FIX/SKP_Silk_SigProc_FIX.h
src_SigProc_FIX/SKP_Silk_apply_sine_window.c
src_SigProc_FIX/SKP_Silk_array_maxabs.c
src_SigProc_FIX/SKP_Silk_burg_modified.c
src_SigProc_FIX/SKP_Silk_debug.c [new file with mode: 0644]
src_SigProc_FIX/SKP_Silk_pitch_analysis_core.c
src_SigProc_FIX/SKP_Silk_pitch_est_tables.c
src_SigProc_FIX/SKP_Silk_resampler_down2.c
src_SigProc_FIX/SKP_Silk_resampler_private.h
src_SigProc_FIX/SKP_Silk_resampler_private_AR2.c
src_SigProc_FIX/SKP_Silk_resampler_private_ARMA4.c
src_SigProc_FIX/SKP_Silk_resampler_private_down_FIR.c
src_SigProc_FIX/SKP_Silk_resampler_private_up2_HQ.c
src_SigProc_FIX/SKP_Silk_resampler_private_up4.c
src_SigProc_FIX/SKP_Silk_resampler_rom.c
src_SigProc_FIX/SKP_Silk_resampler_rom.h
src_SigProc_FIX/SKP_Silk_resampler_structs.h
src_SigProc_FIX/SKP_Silk_schur.c
src_SigProc_FLP/SKP_Silk_NLSF_VQ_weights_laroia_FLP.c
src_SigProc_FLP/SKP_Silk_SigProc_FLP.h
src_SigProc_FLP/SKP_Silk_k2a_FLP.c
src_SigProc_FLP/SKP_Silk_pitch_analysis_core_FLP.c
src_SigProc_FLP/SKP_Silk_schur_FLP.c
src_SigProc_FLP/SKP_Silk_sort_FLP.c
src_common/SKP_Silk_NLSF2A_stable.c
src_common/SKP_Silk_NLSF_MSVQ_decode.c
src_common/SKP_Silk_NSQ.c
src_common/SKP_Silk_NSQ_del_dec.c
src_common/SKP_Silk_PLC.c
src_common/SKP_Silk_VAD.c
src_common/SKP_Silk_code_signs.c
src_common/SKP_Silk_control_audio_bandwidth.c [new file with mode: 0644]
src_common/SKP_Silk_create_init_destroy.c
src_common/SKP_Silk_dec_API.c [moved from src_common/SKP_Silk_dec_SDK_API.c with 71% similarity]
src_common/SKP_Silk_decode_core.c
src_common/SKP_Silk_decode_frame.c
src_common/SKP_Silk_decode_indices.c
src_common/SKP_Silk_decode_parameters.c
src_common/SKP_Silk_decoder_set_fs.c
src_common/SKP_Silk_define.h
src_common/SKP_Silk_enc_API.c
src_common/SKP_Silk_encode_parameters.c
src_common/SKP_Silk_encode_pulses.c
src_common/SKP_Silk_main.h
src_common/SKP_Silk_setup_complexity.h [new file with mode: 0644]
src_common/SKP_Silk_structs.h
src_common/SKP_Silk_tables.h
src_common/SKP_Silk_tables_NLSF_CB0_10.c
src_common/SKP_Silk_tables_NLSF_CB0_10.h
src_common/SKP_Silk_tables_NLSF_CB0_16.c
src_common/SKP_Silk_tables_NLSF_CB0_16.h
src_common/SKP_Silk_tables_NLSF_CB1_10.c
src_common/SKP_Silk_tables_NLSF_CB1_10.h
src_common/SKP_Silk_tables_NLSF_CB1_16.c
src_common/SKP_Silk_tables_other.c
src_common/SKP_Silk_tuning_parameters.h [new file with mode: 0644]
test/Decoder.c
test/Encoder.c

index 45da4f5..f3276db 100644 (file)
@@ -29,7 +29,7 @@ src_common/SKP_Silk_decode_indices.c \
 src_common/SKP_Silk_decode_parameters.c \
 src_common/SKP_Silk_decode_pulses.c \
 src_common/SKP_Silk_decoder_set_fs.c \
 src_common/SKP_Silk_decode_parameters.c \
 src_common/SKP_Silk_decode_pulses.c \
 src_common/SKP_Silk_decoder_set_fs.c \
-src_common/SKP_Silk_dec_SDK_API.c \
+src_common/SKP_Silk_dec_API.c \
 src_common/SKP_Silk_detect_SWB_input.c \
 src_common/SKP_Silk_enc_API.c \
 src_common/SKP_Silk_encode_parameters.c \
 src_common/SKP_Silk_detect_SWB_input.c \
 src_common/SKP_Silk_enc_API.c \
 src_common/SKP_Silk_encode_parameters.c \
@@ -58,6 +58,7 @@ src_common/SKP_Silk_tables_pulses_per_block.c \
 src_common/SKP_Silk_tables_sign.c \
 src_common/SKP_Silk_tables_type_offset.c \
 src_common/SKP_Silk_VAD.c \
 src_common/SKP_Silk_tables_sign.c \
 src_common/SKP_Silk_tables_type_offset.c \
 src_common/SKP_Silk_VAD.c \
+src_common/SKP_Silk_control_audio_bandwidth.c \
 src_FLP/SKP_Silk_apply_sine_window_FLP.c \
 src_FLP/SKP_Silk_control_codec_FLP.c \
 src_FLP/SKP_Silk_corrMatrix_FLP.c \
 src_FLP/SKP_Silk_apply_sine_window_FLP.c \
 src_FLP/SKP_Silk_control_codec_FLP.c \
 src_FLP/SKP_Silk_corrMatrix_FLP.c \
index e112526..8a83520 100644 (file)
@@ -83,11 +83,11 @@ SKP_int SKP_Silk_SDK_QueryEncoder(                      /* O:   Returns error co
 /**************************/\r
 SKP_int SKP_Silk_SDK_Encode(                            /* O:   Returns error code                              */\r
     void                                *encState,      /* I/O: State                                           */\r
 /**************************/\r
 SKP_int SKP_Silk_SDK_Encode(                            /* O:   Returns error code                              */\r
     void                                *encState,      /* I/O: State                                           */\r
-    const SKP_SILK_SDK_EncControlStruct *encControl,    /* I:   Control status                                  */\r
+    SKP_SILK_SDK_EncControlStruct       *encControl,    /* I:   Control status                                  */\r
     const SKP_int16                     *samplesIn,     /* I:   Speech sample input vector                      */\r
     SKP_int                             nSamplesIn,     /* I:   Number of samples in input vector               */\r
     ec_enc                              *psRangeEnc,    /* I/O  Compressor data structure                       */\r
     const SKP_int16                     *samplesIn,     /* I:   Speech sample input vector                      */\r
     SKP_int                             nSamplesIn,     /* I:   Number of samples in input vector               */\r
     ec_enc                              *psRangeEnc,    /* I/O  Compressor data structure                       */\r
-    SKP_int16                           *nBytesOut      /* I/O: Number of bytes in payload (input: Max bytes)   */\r
+    SKP_int32                           *nBytesOut      /* I/O: Number of bytes in payload (input: Max bytes)   */\r
 );\r
 \r
 /****************************************/\r
 );\r
 \r
 /****************************************/\r
@@ -118,7 +118,7 @@ SKP_int SKP_Silk_SDK_Decode(                            /* O:   Returns error co
     ec_dec                              *psRangeDec,    /* I/O  Compressor data structure                       */\r
     const SKP_int                       nBytesIn,       /* I:   Number of input bytes                           */\r
     SKP_int16                           *samplesOut,    /* O:   Decoded output speech vector                    */\r
     ec_dec                              *psRangeDec,    /* I/O  Compressor data structure                       */\r
     const SKP_int                       nBytesIn,       /* I:   Number of input bytes                           */\r
     SKP_int16                           *samplesOut,    /* O:   Decoded output speech vector                    */\r
-    SKP_int16                           *nSamplesOut    /* I/O: Number of samples (vector/decoded)              */\r
+    SKP_int32                           *nSamplesOut    /* I/O: Number of samples (vector/decoded)              */\r
 );\r
 \r
 /***************************************************************/\r
 );\r
 \r
 /***************************************************************/\r
@@ -129,7 +129,7 @@ void SKP_Silk_SDK_search_for_LBRR(
     const SKP_int16                     nBytesIn,       /* I:   Number of input bytes                           */\r
     SKP_int                             lost_offset,    /* I:   Offset from lost packet                         */\r
     SKP_uint8                           *LBRRData,      /* O:   LBRR payload                                    */\r
     const SKP_int16                     nBytesIn,       /* I:   Number of input bytes                           */\r
     SKP_int                             lost_offset,    /* I:   Offset from lost packet                         */\r
     SKP_uint8                           *LBRRData,      /* O:   LBRR payload                                    */\r
-    SKP_int16                           *nLBRRBytes     /* O:   Number of LBRR Bytes                            */\r
+    SKP_int32                           *nLBRRBytes     /* O:   Number of LBRR Bytes                            */\r
 );\r
 \r
 /**************************************/\r
 );\r
 \r
 /**************************************/\r
index b0fdc15..d10e3f9 100644 (file)
@@ -39,14 +39,17 @@ extern "C"
 /* Structure for controlling encoder operation */\r
 /***********************************************/\r
 typedef struct {\r
 /* Structure for controlling encoder operation */\r
 /***********************************************/\r
 typedef struct {\r
-    /* I:   Input signal sampling rate in Hertz; 8000/12000/16000/24000                     */\r
+    /* I:   Input signal sampling rate in Hertz; 8000/12000/16000/24000/32000/44100/48000   */\r
     SKP_int32 API_sampleRate;\r
 \r
     /* I:   Maximum internal sampling rate in Hertz; 8000/12000/16000/24000                 */\r
     SKP_int32 maxInternalSampleRate;\r
 \r
     SKP_int32 API_sampleRate;\r
 \r
     /* I:   Maximum internal sampling rate in Hertz; 8000/12000/16000/24000                 */\r
     SKP_int32 maxInternalSampleRate;\r
 \r
-    /* I:   Number of samples per packet; must be equivalent of 20, 40, 60, 80 or 100 ms    */\r
-    SKP_int packetSize;\r
+    /* I:   Minimum internal sampling rate in Hertz; 8000/12000/16000/24000                 */\r
+    SKP_int32 minInternalSampleRate;\r
+\r
+    /* I:   Number of samples per packet in milliseconds; 10/20/40/60                       */\r
+    SKP_int payloadSize_ms;\r
 \r
     /* I:   Bitrate during active speech in bits/second; internally limited                 */\r
     SKP_int32 bitRate;                        \r
 \r
     /* I:   Bitrate during active speech in bits/second; internally limited                 */\r
     SKP_int32 bitRate;                        \r
@@ -62,20 +65,32 @@ typedef struct {
 \r
     /* I:   Flag to enable discontinuous transmission (DTX); 0/1                            */\r
     SKP_int useDTX;\r
 \r
     /* I:   Flag to enable discontinuous transmission (DTX); 0/1                            */\r
     SKP_int useDTX;\r
+\r
+    /* I:   Flag to use constant bitrate                                                    */\r
+    SKP_int useCBR;\r
+\r
+    /* O:   Internal sampling rate used, in Hertz; 8000/12000/16000/24000                   */\r
+    SKP_int32 internalSampleRate;\r
 } SKP_SILK_SDK_EncControlStruct;\r
 \r
 /**************************************************************************/\r
 /* Structure for controlling decoder operation and reading decoder status */\r
 /**************************************************************************/\r
 typedef struct {\r
 } SKP_SILK_SDK_EncControlStruct;\r
 \r
 /**************************************************************************/\r
 /* Structure for controlling decoder operation and reading decoder status */\r
 /**************************************************************************/\r
 typedef struct {\r
-    /* I:   Output signal sampling rate in Hertz; 8000/12000/16000/24000                    */\r
+    /* I:   Output signal sampling rate in Hertz; 8000/12000/16000/24000/32000/44100/48000  */\r
     SKP_int32 API_sampleRate;\r
 \r
     SKP_int32 API_sampleRate;\r
 \r
+    /* I:   Number of samples per packet in milliseconds; 10/20/40/60                       */\r
+    SKP_int payloadSize_ms;\r
+\r
+    /* I:   Internal sampling rate used, in Hertz; 8000/12000/16000/24000                   */\r
+    SKP_int32 internalSampleRate;\r
+\r
     /* O:   Number of samples per frame                                                     */\r
     SKP_int frameSize;\r
 \r
     /* O:   Number of samples per frame                                                     */\r
     SKP_int frameSize;\r
 \r
-    /* O:   Frames per packet 1, 2, 3, 4, 5                                                 */\r
-    SKP_int framesPerPacket;\r
+    /* O:   Frames per payload 1, 2, 3                                                      */\r
+    SKP_int framesPerPayload;\r
 \r
     /* O:   Flag to indicate that the decoder has remaining payloads internally             */\r
     SKP_int moreInternalDecoderFrames;\r
 \r
     /* O:   Flag to indicate that the decoder has remaining payloads internally             */\r
     SKP_int moreInternalDecoderFrames;\r
index 475f493..a1032f8 100644 (file)
@@ -43,44 +43,50 @@ extern "C"
 /**************************/\r
 \r
 /* Input length is not a multiplum of 10 ms, or length is longer than the packet length */\r
 /**************************/\r
 \r
 /* Input length is not a multiplum of 10 ms, or length is longer than the packet length */\r
-#define SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES        -1\r
+#define SKP_SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES        -101\r
 \r
 /* Sampling frequency not 8000, 12000, 16000 or 24000 Hertz */\r
 \r
 /* Sampling frequency not 8000, 12000, 16000 or 24000 Hertz */\r
-#define SKP_SILK_ENC_FS_NOT_SUPPORTED                   -2\r
+#define SKP_SILK_ENC_FS_NOT_SUPPORTED                   -102\r
 \r
 /* Packet size not 20, 40, 60, 80 or 100 ms */\r
 \r
 /* Packet size not 20, 40, 60, 80 or 100 ms */\r
-#define SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED          -3\r
+#define SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED          -103\r
 \r
 /* Allocated payload buffer too short */\r
 \r
 /* Allocated payload buffer too short */\r
-#define SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT              -4\r
+#define SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT              -104\r
 \r
 /* Loss rate not between 0 and 100 percent */\r
 \r
 /* Loss rate not between 0 and 100 percent */\r
-#define SKP_SILK_ENC_INVALID_LOSS_RATE                  -5\r
+#define SKP_SILK_ENC_INVALID_LOSS_RATE                  -105\r
 \r
 /* Complexity setting not valid, use 0, 1 or 2 */\r
 \r
 /* Complexity setting not valid, use 0, 1 or 2 */\r
-#define SKP_SILK_ENC_INVALID_COMPLEXITY_SETTING         -6\r
+#define SKP_SILK_ENC_INVALID_COMPLEXITY_SETTING         -106\r
 \r
 /* Inband FEC setting not valid, use 0 or 1 */\r
 \r
 /* Inband FEC setting not valid, use 0 or 1 */\r
-#define SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING         -7\r
+#define SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING         -107\r
 \r
 /* DTX setting not valid, use 0 or 1 */\r
 \r
 /* DTX setting not valid, use 0 or 1 */\r
-#define SKP_SILK_ENC_INVALID_DTX_SETTING                -8\r
+#define SKP_SILK_ENC_INVALID_DTX_SETTING                -108\r
+\r
+/* CBR setting not valid, use 0 or 1 */\r
+#define SKP_SILK_ENC_INVALID_CBR_SETTING                -109\r
 \r
 /* Internal encoder error */\r
 \r
 /* Internal encoder error */\r
-#define SKP_SILK_ENC_INTERNAL_ERROR                     -9\r
+#define SKP_SILK_ENC_INTERNAL_ERROR                     -110\r
 \r
 /**************************/\r
 /* Decoder error messages */\r
 /**************************/\r
 \r
 /* Output sampling frequency lower than internal decoded sampling frequency */\r
 \r
 /**************************/\r
 /* Decoder error messages */\r
 /**************************/\r
 \r
 /* Output sampling frequency lower than internal decoded sampling frequency */\r
-#define SKP_SILK_DEC_INVALID_SAMPLING_FREQUENCY         -10\r
+#define SKP_SILK_DEC_INVALID_SAMPLING_FREQUENCY         -200\r
 \r
 /* Payload size exceeded the maximum allowed 1024 bytes */\r
 \r
 /* Payload size exceeded the maximum allowed 1024 bytes */\r
-#define SKP_SILK_DEC_PAYLOAD_TOO_LARGE                  -11\r
+#define SKP_SILK_DEC_PAYLOAD_TOO_LARGE                  -201\r
+\r
+/* Payload has bit errors */\r
+#define SKP_SILK_DEC_PAYLOAD_ERROR                      -202\r
 \r
 /* Payload has bit errors */\r
 \r
 /* Payload has bit errors */\r
-#define SKP_SILK_DEC_PAYLOAD_ERROR                      -12\r
+#define SKP_SILK_DEC_INVALID_FRAME_SIZE                 -203\r
 \r
 #ifdef __cplusplus\r
 }\r
 \r
 #ifdef __cplusplus\r
 }\r
index b695a5a..10bd439 100644 (file)
@@ -26,6 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 #if HIGH_PASS_INPUT\r
 \r
 \r
 #if HIGH_PASS_INPUT\r
 \r
@@ -57,7 +58,7 @@ void SKP_Silk_HP_variable_cutoff_FIX(
         quality_Q15 = psEncCtrl->input_quality_bands_Q15[ 0 ];\r
         pitch_freq_log_Q7 = SKP_SUB32( pitch_freq_log_Q7, SKP_SMULWB( SKP_SMULWB( SKP_LSHIFT( quality_Q15, 2 ), quality_Q15 ), \r
             pitch_freq_log_Q7 - SKP_LOG2_VARIABLE_HP_MIN_FREQ_Q7 ) );\r
         quality_Q15 = psEncCtrl->input_quality_bands_Q15[ 0 ];\r
         pitch_freq_log_Q7 = SKP_SUB32( pitch_freq_log_Q7, SKP_SMULWB( SKP_SMULWB( SKP_LSHIFT( quality_Q15, 2 ), quality_Q15 ), \r
             pitch_freq_log_Q7 - SKP_LOG2_VARIABLE_HP_MIN_FREQ_Q7 ) );\r
-        pitch_freq_log_Q7 = SKP_ADD32( pitch_freq_log_Q7, SKP_RSHIFT( 19661 - quality_Q15, 9 ) ); // 19661_Q15 = 0.6_Q0\r
+        pitch_freq_log_Q7 = SKP_ADD32( pitch_freq_log_Q7, SKP_RSHIFT( SKP_FIX_CONST( 0.6, 15 ) - quality_Q15, 9 ) );\r
 \r
         //delta_freq = pitch_freq_log - psEnc->variable_HP_smth1;\r
         delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEnc->variable_HP_smth1_Q15, 8 );\r
 \r
         //delta_freq = pitch_freq_log - psEnc->variable_HP_smth1;\r
         delta_freq_Q7 = pitch_freq_log_Q7 - SKP_RSHIFT( psEnc->variable_HP_smth1_Q15, 8 );\r
@@ -67,21 +68,22 @@ void SKP_Silk_HP_variable_cutoff_FIX(
         }\r
 \r
         /* limit delta, to reduce impact of outliers */\r
         }\r
 \r
         /* limit delta, to reduce impact of outliers */\r
-        delta_freq_Q7 = SKP_LIMIT_32( delta_freq_Q7, -VARIABLE_HP_MAX_DELTA_FREQ_Q7, VARIABLE_HP_MAX_DELTA_FREQ_Q7 );\r
+        delta_freq_Q7 = SKP_LIMIT_32( delta_freq_Q7, -SKP_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ), SKP_FIX_CONST( VARIABLE_HP_MAX_DELTA_FREQ, 7 ) );\r
 \r
         /* update smoother */\r
         psEnc->variable_HP_smth1_Q15 = SKP_SMLAWB( psEnc->variable_HP_smth1_Q15, \r
 \r
         /* update smoother */\r
         psEnc->variable_HP_smth1_Q15 = SKP_SMLAWB( psEnc->variable_HP_smth1_Q15, \r
-            SKP_MUL( SKP_LSHIFT( psEnc->speech_activity_Q8, 1 ), delta_freq_Q7 ), VARIABLE_HP_SMTH_COEF1_Q16 );\r
+            SKP_MUL( SKP_LSHIFT( psEnc->speech_activity_Q8, 1 ), delta_freq_Q7 ), SKP_FIX_CONST( VARIABLE_HP_SMTH_COEF1, 16 ) );\r
     }\r
     /* second smoother */\r
     psEnc->variable_HP_smth2_Q15 = SKP_SMLAWB( psEnc->variable_HP_smth2_Q15, \r
     }\r
     /* second smoother */\r
     psEnc->variable_HP_smth2_Q15 = SKP_SMLAWB( psEnc->variable_HP_smth2_Q15, \r
-        psEnc->variable_HP_smth1_Q15 - psEnc->variable_HP_smth2_Q15, VARIABLE_HP_SMTH_COEF2_Q16 );\r
+        psEnc->variable_HP_smth1_Q15 - psEnc->variable_HP_smth2_Q15, SKP_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );\r
 \r
     /* convert from log scale to Hertz */\r
 \r
     /* convert from log scale to Hertz */\r
-    psEncCtrl->pitch_freq_low_Hz = SKP_Silk_log2lin( SKP_RSHIFT( psEnc->variable_HP_smth2_Q15, 8 ) ); //pow( 2.0, psEnc->variable_HP_smth2 );\r
+    psEncCtrl->pitch_freq_low_Hz = SKP_Silk_log2lin( SKP_RSHIFT( psEnc->variable_HP_smth2_Q15, 8 ) );\r
 \r
     /* limit frequency range */\r
 \r
     /* limit frequency range */\r
-    psEncCtrl->pitch_freq_low_Hz = SKP_LIMIT_32( psEncCtrl->pitch_freq_low_Hz, VARIABLE_HP_MIN_FREQ_Q0, VARIABLE_HP_MAX_FREQ_Q0 );\r
+    psEncCtrl->pitch_freq_low_Hz = SKP_LIMIT_32( psEncCtrl->pitch_freq_low_Hz, \r
+        SKP_FIX_CONST( VARIABLE_HP_MIN_FREQ, 0 ), SKP_FIX_CONST( VARIABLE_HP_MAX_FREQ, 0 ) );\r
 \r
     /********************************/\r
     /* Compute Filter Coefficients  */\r
 \r
     /********************************/\r
     /* Compute Filter Coefficients  */\r
@@ -94,7 +96,7 @@ void SKP_Silk_HP_variable_cutoff_FIX(
     SKP_assert( Fc_Q19 >=  3704 );\r
     SKP_assert( Fc_Q19 <= 27787 );\r
 \r
     SKP_assert( Fc_Q19 >=  3704 );\r
     SKP_assert( Fc_Q19 <= 27787 );\r
 \r
-    r_Q28 = ( 1 << 28 ) - SKP_MUL( 471, Fc_Q19 ); // 471_Q9 = 0.92_Q0, range: 255347779 to 266690872, 27-28 bits\r
+    r_Q28 = SKP_FIX_CONST( 1.0, 28 ) - SKP_MUL( SKP_FIX_CONST( 0.92, 9 ), Fc_Q19 );\r
     SKP_assert( r_Q28 >= 255347779 );\r
     SKP_assert( r_Q28 <= 266690872 );\r
 \r
     SKP_assert( r_Q28 >= 255347779 );\r
     SKP_assert( r_Q28 <= 266690872 );\r
 \r
@@ -106,7 +108,7 @@ void SKP_Silk_HP_variable_cutoff_FIX(
     \r
     // -r * ( 2 - Fc * Fc );\r
     r_Q22  = SKP_RSHIFT( r_Q28, 6 );\r
     \r
     // -r * ( 2 - Fc * Fc );\r
     r_Q22  = SKP_RSHIFT( r_Q28, 6 );\r
-    A_Q28[ 0 ] = SKP_SMULWW( r_Q22, SKP_SMULWW( Fc_Q19, Fc_Q19 ) - ( 2 << 22 ) );\r
+    A_Q28[ 0 ] = SKP_SMULWW( r_Q22, SKP_SMULWW( Fc_Q19, Fc_Q19 ) - SKP_FIX_CONST( 2.0,  22 ) );\r
     A_Q28[ 1 ] = SKP_SMULWW( r_Q22, r_Q22 );\r
 \r
     /********************************/\r
     A_Q28[ 1 ] = SKP_SMULWW( r_Q22, r_Q22 );\r
 \r
     /********************************/\r
index 9470e37..c7baece 100644 (file)
@@ -52,7 +52,7 @@ void SKP_Silk_LTP_scale_ctrl_FIX(
 \r
     /* combine input and filtered input */\r
     g_out_Q5    = SKP_RSHIFT_ROUND( SKP_RSHIFT( psEncCtrl->LTPredCodGain_Q7, 1 ) + SKP_RSHIFT( psEnc->HPLTPredCodGain_Q7, 1 ), 3 );\r
 \r
     /* combine input and filtered input */\r
     g_out_Q5    = SKP_RSHIFT_ROUND( SKP_RSHIFT( psEncCtrl->LTPredCodGain_Q7, 1 ) + SKP_RSHIFT( psEnc->HPLTPredCodGain_Q7, 1 ), 3 );\r
-    g_limit_Q15 = SKP_Silk_sigm_Q15( g_out_Q5 - ( 3 << 5 ) ); /* mulitplid with 0.5 */\r
+    g_limit_Q15 = SKP_Silk_sigm_Q15( g_out_Q5 - ( 3 << 5 ) );\r
             \r
     /* Default is minimum scaling */\r
     psEncCtrl->sCmn.LTP_scaleIndex = 0;\r
             \r
     /* Default is minimum scaling */\r
     psEncCtrl->sCmn.LTP_scaleIndex = 0;\r
index 73e40c0..67b14a2 100644 (file)
@@ -43,9 +43,8 @@ void SKP_Silk_NLSF_MSVQ_encode_FIX(
     const SKP_int                   deactivate_fluc_red     /* I    Deactivate fluctuation reduction        */\r
 )\r
 {\r
     const SKP_int                   deactivate_fluc_red     /* I    Deactivate fluctuation reduction        */\r
 )\r
 {\r
-    SKP_int     i, s, k, cur_survivors = 0, prev_survivors, input_index, cb_index, bestIndex;\r
+    SKP_int     i, s, k, cur_survivors = 0, prev_survivors, min_survivors, input_index, cb_index, bestIndex;\r
     SKP_int32   rateDistThreshold_Q18;\r
     SKP_int32   rateDistThreshold_Q18;\r
-    SKP_int     pNLSF_in_Q15[ MAX_LPC_ORDER ];\r
 #if( NLSF_MSVQ_FLUCTUATION_REDUCTION == 1 )\r
     SKP_int32   se_Q15, wsse_Q20, bestRateDist_Q20;\r
 #endif\r
 #if( NLSF_MSVQ_FLUCTUATION_REDUCTION == 1 )\r
     SKP_int32   se_Q15, wsse_Q20, bestRateDist_Q20;\r
 #endif\r
@@ -72,7 +71,7 @@ void SKP_Silk_NLSF_MSVQ_encode_FIX(
 \r
     const SKP_int   *pConstInt;\r
           SKP_int   *pInt;\r
 \r
     const SKP_int   *pConstInt;\r
           SKP_int   *pInt;\r
-    const SKP_int16 *pCB_element;\r
+    const SKP_int *pCB_element;\r
     const SKP_Silk_NLSF_CBS *pCurrentCBStage;\r
 \r
     SKP_assert( NLSF_MSVQ_Survivors <= MAX_NLSF_MSVQ_SURVIVORS );\r
     const SKP_Silk_NLSF_CBS *pCurrentCBStage;\r
 \r
     SKP_assert( NLSF_MSVQ_Survivors <= MAX_NLSF_MSVQ_SURVIVORS );\r
@@ -85,25 +84,24 @@ void SKP_Silk_NLSF_MSVQ_encode_FIX(
     DEBUG_STORE_DATA( NLSF_mu.dat, &NLSF_mu_Q15,             sizeof( SKP_int32 ) );\r
 #endif\r
 \r
     DEBUG_STORE_DATA( NLSF_mu.dat, &NLSF_mu_Q15,             sizeof( SKP_int32 ) );\r
 #endif\r
 \r
-\r
-    /* Copy the input vector */\r
-    SKP_memcpy( pNLSF_in_Q15, pNLSF_Q15, LPC_order * sizeof( SKP_int ) );\r
-\r
     /****************************************************/\r
     /* Tree search for the multi-stage vector quantizer */\r
     /****************************************************/\r
 \r
     /* Clear accumulated rates */\r
     SKP_memset( pRate_Q5, 0, NLSF_MSVQ_Survivors * sizeof( SKP_int32 ) );\r
     /****************************************************/\r
     /* Tree search for the multi-stage vector quantizer */\r
     /****************************************************/\r
 \r
     /* Clear accumulated rates */\r
     SKP_memset( pRate_Q5, 0, NLSF_MSVQ_Survivors * sizeof( SKP_int32 ) );\r
-    \r
-    /* Copy NLSFs into residual signal vector */\r
+\r
+    /* Subtract 1/2 from NLSF input vector to create initial residual */\r
     for( i = 0; i < LPC_order; i++ ) {\r
     for( i = 0; i < LPC_order; i++ ) {\r
-        pRes_Q15[ i ] = pNLSF_Q15[ i ];\r
+        pRes_Q15[ i ] = pNLSF_Q15[ i ] - SKP_FIX_CONST( 0.5f, 15 );\r
     }\r
 \r
     /* Set first stage values */\r
     prev_survivors = 1;\r
 \r
     }\r
 \r
     /* Set first stage values */\r
     prev_survivors = 1;\r
 \r
+    /* Minimum number of survivors */\r
+    min_survivors = NLSF_MSVQ_Survivors / 2;\r
+\r
     /* Loop over all stages */\r
     for( s = 0; s < psNLSF_CB->nStages; s++ ) {\r
 \r
     /* Loop over all stages */\r
     for( s = 0; s < psNLSF_CB->nStages; s++ ) {\r
 \r
@@ -130,9 +128,10 @@ void SKP_Silk_NLSF_MSVQ_encode_FIX(
             prev_survivors * pCurrentCBStage->nVectors, cur_survivors );\r
 \r
         /* Discard survivors with rate-distortion values too far above the best one */\r
             prev_survivors * pCurrentCBStage->nVectors, cur_survivors );\r
 \r
         /* Discard survivors with rate-distortion values too far above the best one */\r
-        if( pRateDist_Q18[ 0 ] < SKP_int32_MAX / NLSF_MSVQ_SURV_MAX_REL_RD ) {\r
-            rateDistThreshold_Q18 = SKP_MUL( NLSF_MSVQ_SURV_MAX_REL_RD, pRateDist_Q18[ 0 ] );\r
-            while( pRateDist_Q18[ cur_survivors - 1 ] > rateDistThreshold_Q18 && cur_survivors > 1 ) {\r
+        if( pRateDist_Q18[ 0 ] < SKP_int32_MAX / MAX_NLSF_MSVQ_SURVIVORS ) {\r
+            rateDistThreshold_Q18 = SKP_SMLAWB( pRateDist_Q18[ 0 ], \r
+                SKP_MUL( NLSF_MSVQ_Survivors, pRateDist_Q18[ 0 ] ), SKP_FIX_CONST( NLSF_MSVQ_SURV_MAX_REL_RD, 16 ) );\r
+            while( pRateDist_Q18[ cur_survivors - 1 ] > rateDistThreshold_Q18 && cur_survivors > min_survivors ) {\r
                 cur_survivors--;\r
             }\r
         }\r
                 cur_survivors--;\r
             }\r
         }\r
@@ -155,14 +154,14 @@ void SKP_Silk_NLSF_MSVQ_encode_FIX(
 \r
             /* Subtract new contribution from the previous residual vector for each of 'cur_survivors' */\r
             pConstInt   = &pRes_Q15[ SKP_SMULBB( input_index, LPC_order ) ];\r
 \r
             /* Subtract new contribution from the previous residual vector for each of 'cur_survivors' */\r
             pConstInt   = &pRes_Q15[ SKP_SMULBB( input_index, LPC_order ) ];\r
-            pCB_element = &pCurrentCBStage->CB_NLSF_Q15[ SKP_SMULBB( cb_index, LPC_order ) ];\r
+            pCB_element = &pCurrentCBStage->CB_NLSF_Q8[ SKP_SMULBB( cb_index, LPC_order ) ];\r
             pInt        = &pRes_new_Q15[ SKP_SMULBB( k, LPC_order ) ];\r
             for( i = 0; i < LPC_order; i++ ) {\r
             pInt        = &pRes_new_Q15[ SKP_SMULBB( k, LPC_order ) ];\r
             for( i = 0; i < LPC_order; i++ ) {\r
-                pInt[ i ] = pConstInt[ i ] - ( SKP_int )pCB_element[ i ];\r
+                pInt[ i ] = pConstInt[ i ] - SKP_LSHIFT16( ( SKP_int )pCB_element[ i ], 7 );\r
             }\r
 \r
             /* Update accumulated rate for stage 1 to the current */\r
             }\r
 \r
             /* Update accumulated rate for stage 1 to the current */\r
-            pRate_new_Q5[ k ] = pRate_Q5[ input_index ] + pCurrentCBStage->Rates_Q5[ cb_index ];\r
+            pRate_new_Q5[ k ] = pRate_Q5[ input_index ] + SKP_LSHIFT32( ( SKP_int32 )pCurrentCBStage->Rates_Q4[ cb_index ], 1 );\r
 \r
             /* Copy paths from previous matrix, starting with the best path */\r
             pConstInt = &pPath[ SKP_SMULBB( input_index, psNLSF_CB->nStages ) ];\r
 \r
             /* Copy paths from previous matrix, starting with the best path */\r
             pConstInt = &pPath[ SKP_SMULBB( input_index, psNLSF_CB->nStages ) ];\r
index 09e2bc7..e5bb81d 100644 (file)
@@ -43,7 +43,7 @@ void SKP_Silk_NLSF_VQ_rate_distortion_FIX(
     SKP_int32 *pRD_vec_Q20;\r
 \r
     /* Compute weighted quantization errors for all input vectors over one codebook stage */\r
     SKP_int32 *pRD_vec_Q20;\r
 \r
     /* Compute weighted quantization errors for all input vectors over one codebook stage */\r
-    SKP_Silk_NLSF_VQ_sum_error_FIX( pRD_Q20, in_Q15, w_Q6, psNLSF_CBS->CB_NLSF_Q15\r
+    SKP_Silk_NLSF_VQ_sum_error_FIX( pRD_Q20, in_Q15, w_Q6, psNLSF_CBS->CB_NLSF_Q8\r
         N, psNLSF_CBS->nVectors, LPC_order );\r
 \r
     /* Loop over input vectors */\r
         N, psNLSF_CBS->nVectors, LPC_order );\r
 \r
     /* Loop over input vectors */\r
@@ -51,9 +51,9 @@ void SKP_Silk_NLSF_VQ_rate_distortion_FIX(
     for( n = 0; n < N; n++ ) {\r
         /* Add rate cost to error for each codebook vector */\r
         for( i = 0; i < psNLSF_CBS->nVectors; i++ ) {\r
     for( n = 0; n < N; n++ ) {\r
         /* Add rate cost to error for each codebook vector */\r
         for( i = 0; i < psNLSF_CBS->nVectors; i++ ) {\r
-            SKP_assert( rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ] >= 0 );\r
-            SKP_assert( rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ] <= SKP_int16_MAX );\r
-            pRD_vec_Q20[ i ] = SKP_SMLABB( pRD_vec_Q20[ i ], rate_acc_Q5[ n ] + psNLSF_CBS->Rates_Q5[ i ], mu_Q15 );\r
+            SKP_assert( rate_acc_Q5[ n ] + SKP_LSHIFT32( ( SKP_int32 )psNLSF_CBS->Rates_Q4[ i ], 1 ) >= 0 );\r
+            SKP_assert( rate_acc_Q5[ n ] + SKP_LSHIFT32( ( SKP_int32 )psNLSF_CBS->Rates_Q4[ i ], 1 ) <= SKP_int16_MAX );\r
+            pRD_vec_Q20[ i ] = SKP_SMLABB( pRD_vec_Q20[ i ], rate_acc_Q5[ n ] + SKP_LSHIFT32( ( SKP_int32 )psNLSF_CBS->Rates_Q4[ i ], 1 ), mu_Q15 );\r
             SKP_assert( pRD_vec_Q20[ i ] >= 0 );\r
         }\r
         pRD_vec_Q20 += psNLSF_CBS->nVectors;\r
             SKP_assert( pRD_vec_Q20[ i ] >= 0 );\r
         }\r
         pRD_vec_Q20 += psNLSF_CBS->nVectors;\r
index 7a2171c..e1f5283 100644 (file)
@@ -32,16 +32,16 @@ void SKP_Silk_NLSF_VQ_sum_error_FIX(
     SKP_int32                       *err_Q20,           /* O    Weighted quantization errors  [N*K]         */\r
     const SKP_int                   *in_Q15,            /* I    Input vectors to be quantized [N*LPC_order] */\r
     const SKP_int                   *w_Q6,              /* I    Weighting vectors             [N*LPC_order] */\r
     SKP_int32                       *err_Q20,           /* O    Weighted quantization errors  [N*K]         */\r
     const SKP_int                   *in_Q15,            /* I    Input vectors to be quantized [N*LPC_order] */\r
     const SKP_int                   *w_Q6,              /* I    Weighting vectors             [N*LPC_order] */\r
-    const SKP_int16                 *pCB_Q15,           /* I    Codebook vectors              [K*LPC_order] */\r
+    const SKP_int8                  *pCB_Q8,            /* I    Codebook vectors              [K*LPC_order] */\r
     const SKP_int                   N,                  /* I    Number of input vectors                     */\r
     const SKP_int                   K,                  /* I    Number of codebook vectors                  */\r
     const SKP_int                   LPC_order           /* I    Number of LPCs                              */\r
 )\r
 {\r
     const SKP_int                   N,                  /* I    Number of input vectors                     */\r
     const SKP_int                   K,                  /* I    Number of codebook vectors                  */\r
     const SKP_int                   LPC_order           /* I    Number of LPCs                              */\r
 )\r
 {\r
-    SKP_int         i, n, m;\r
-    SKP_int32       diff_Q15, sum_error, Wtmp_Q6;\r
-    SKP_int32       Wcpy_Q6[ MAX_LPC_ORDER / 2 ];\r
-    const SKP_int16 *cb_vec_Q15;\r
+    SKP_int        i, n, m;\r
+    SKP_int32      diff_Q15, sum_error, Wtmp_Q6;\r
+    SKP_int32      Wcpy_Q6[ MAX_LPC_ORDER / 2 ];\r
+    const SKP_int8 *cb_vec_Q8;\r
 \r
     SKP_assert( LPC_order <= 16 );\r
     SKP_assert( ( LPC_order & 1 ) == 0 );\r
 \r
     SKP_assert( LPC_order <= 16 );\r
     SKP_assert( ( LPC_order & 1 ) == 0 );\r
@@ -54,7 +54,7 @@ void SKP_Silk_NLSF_VQ_sum_error_FIX(
     /* Loop over input vectors */\r
     for( n = 0; n < N; n++ ) {\r
         /* Loop over codebook */\r
     /* Loop over input vectors */\r
     for( n = 0; n < N; n++ ) {\r
         /* Loop over codebook */\r
-        cb_vec_Q15 = pCB_Q15;\r
+        cb_vec_Q8 = pCB_Q8;\r
         for( i = 0; i < K; i++ ) {\r
             sum_error = 0;\r
             for( m = 0; m < LPC_order; m += 2 ) {\r
         for( i = 0; i < K; i++ ) {\r
             sum_error = 0;\r
             for( m = 0; m < LPC_order; m += 2 ) {\r
@@ -62,11 +62,11 @@ void SKP_Silk_NLSF_VQ_sum_error_FIX(
                 Wtmp_Q6 = Wcpy_Q6[ SKP_RSHIFT( m, 1 ) ];\r
 \r
                 /* Compute weighted squared quantization error for index m */\r
                 Wtmp_Q6 = Wcpy_Q6[ SKP_RSHIFT( m, 1 ) ];\r
 \r
                 /* Compute weighted squared quantization error for index m */\r
-                diff_Q15 = in_Q15[ m ] - *cb_vec_Q15++; // range: [ -32767 : 32767 ]\r
+                diff_Q15 = in_Q15[ m ] - SKP_LSHIFT16( ( SKP_int )( *cb_vec_Q8++ ), 7 ); // range: [ -32767 : 32767 ]\r
                 sum_error = SKP_SMLAWB( sum_error, SKP_SMULBB( diff_Q15, diff_Q15 ), Wtmp_Q6 );\r
 \r
                 /* Compute weighted squared quantization error for index m + 1 */\r
                 sum_error = SKP_SMLAWB( sum_error, SKP_SMULBB( diff_Q15, diff_Q15 ), Wtmp_Q6 );\r
 \r
                 /* Compute weighted squared quantization error for index m + 1 */\r
-                diff_Q15 = in_Q15[m + 1] - *cb_vec_Q15++; // range: [ -32767 : 32767 ]\r
+                diff_Q15 = in_Q15[m + 1] - SKP_LSHIFT16( ( SKP_int )( *cb_vec_Q8++ ), 7 ); // range: [ -32767 : 32767 ]\r
                 sum_error = SKP_SMLAWT( sum_error, SKP_SMULBB( diff_Q15, diff_Q15 ), Wtmp_Q6 );\r
             }\r
             SKP_assert( sum_error >= 0 );\r
                 sum_error = SKP_SMLAWT( sum_error, SKP_SMULBB( diff_Q15, diff_Q15 ), Wtmp_Q6 );\r
             }\r
             SKP_assert( sum_error >= 0 );\r
index b9bcca4..073a66b 100644 (file)
@@ -26,28 +26,23 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
+#include "SKP_Silk_setup_complexity.h"\r
 \r
 /* ToDo: Move the functions belowto common to be able to use them in FLP control codec also */\r
 SKP_INLINE SKP_int SKP_Silk_setup_resamplers(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
 \r
 /* ToDo: Move the functions belowto common to be able to use them in FLP control codec also */\r
 SKP_INLINE SKP_int SKP_Silk_setup_resamplers(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         API_fs_Hz,          /* I                        */\r
     SKP_int                         fs_kHz              /* I                        */\r
 );\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_packetsize(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
     SKP_int                         fs_kHz              /* I                        */\r
 );\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_packetsize(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         PacketSize_ms,      /* I                        */\r
-    SKP_int                         fs_kHz              /* I                        */\r
+    SKP_int                         PacketSize_ms       /* I                        */\r
 );\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_fs(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
 );\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_fs(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         fs_kHz              /* I                        */\r
-);\r
-\r
-SKP_INLINE SKP_int SKP_Silk_setup_complexity(\r
-    SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         Complexity          /* I                        */\r
+    SKP_int                         fs_kHz,             /* I                        */\r
+    SKP_int                         PacketSize_ms       /* I                        */\r
 );\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_rate(\r
 );\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_rate(\r
@@ -56,142 +51,49 @@ SKP_INLINE SKP_int SKP_Silk_setup_rate(
 );\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_LBRR(\r
 );\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_LBRR(\r
-    SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         INBandFEC_enabled   /* I                        */\r
+    SKP_Silk_encoder_state_FIX      *psEnc              /* I/O                      */\r
 );\r
 \r
 /* Control encoder SNR */\r
 SKP_int SKP_Silk_control_encoder_FIX( \r
 );\r
 \r
 /* Control encoder SNR */\r
 SKP_int SKP_Silk_control_encoder_FIX( \r
-    SKP_Silk_encoder_state_FIX  *psEnc,             /* I/O  Pointer to Silk encoder state                   */\r
-    const SKP_int32             API_fs_Hz,          /* I    External (API) sampling rate (Hz)               */\r
-    const SKP_int               max_internal_fs_kHz,/* I    Maximum internal sampling rate (kHz)            */\r
-    const SKP_int               PacketSize_ms,      /* I    Packet length (ms)                              */\r
-    SKP_int32                   TargetRate_bps,     /* I    Target max bitrate (bps) (used if SNR_dB == 0)  */\r
-    const SKP_int               PacketLoss_perc,    /* I    Packet loss rate (in percent)                   */\r
-    const SKP_int               INBandFEC_enabled,  /* I    Enable (1) / disable (0) inband FEC             */\r
-    const SKP_int               DTX_enabled,        /* I    Enable / disable DTX                            */\r
-    const SKP_int               InputFramesize_ms,  /* I    Inputframe in ms                                */\r
-    const SKP_int               Complexity          /* I    Complexity (0->low; 1->medium; 2->high)         */\r
+    SKP_Silk_encoder_state_FIX  *psEnc,                 /* I/O  Pointer to Silk encoder state           */\r
+    const SKP_int               PacketSize_ms,          /* I    Packet length (ms)                      */\r
+    const SKP_int32             TargetRate_bps,         /* I    Target max bitrate (bps)                */\r
+    const SKP_int               PacketLoss_perc,        /* I    Packet loss rate (in percent)           */\r
+    const SKP_int               Complexity              /* I    Complexity (0->low; 1->medium; 2->high) */\r
 )\r
 {\r
     SKP_int   fs_kHz, ret = 0;\r
 \r
 )\r
 {\r
     SKP_int   fs_kHz, ret = 0;\r
 \r
-    /* State machine for the SWB/WB switching */\r
-    fs_kHz = psEnc->sCmn.fs_kHz;\r
-    \r
-    /* Only switch during low speech activity, when no frames are sitting in the payload buffer */\r
-    if( API_fs_Hz == 8000 || fs_kHz == 0 || API_fs_Hz < SKP_SMULBB( fs_kHz, 1000 ) || fs_kHz > max_internal_fs_kHz ) {\r
-        /* Switching is not possible, encoder just initialized, internal mode higher than external, */\r
-        /* or internal mode higher than maximum allowed internal mode                               */\r
-        fs_kHz = SKP_min( SKP_DIV32_16( API_fs_Hz, 1000 ), max_internal_fs_kHz );\r
-    } else {\r
-        /* Accumulate the difference between the target rate and limit for switching down */\r
-        psEnc->sCmn.bitrateDiff += SKP_MUL( InputFramesize_ms, TargetRate_bps - psEnc->sCmn.bitrate_threshold_down );\r
-        psEnc->sCmn.bitrateDiff  = SKP_min( psEnc->sCmn.bitrateDiff, 0 );\r
-\r
-        if( psEnc->speech_activity_Q8 < 128 && psEnc->sCmn.nFramesInPayloadBuf == 0 ) { /* Low speech activity and payload buffer empty */\r
-            /* Check if we should switch down */\r
-#if SWITCH_TRANSITION_FILTERING \r
-            if( ( psEnc->sCmn.sLP.transition_frame_no == 0 ) &&                         /* Transition phase not active */\r
-                ( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ||              /* Bitrate threshold is met */\r
-                ( psEnc->sCmn.sSWBdetect.WB_detected * psEnc->sCmn.fs_kHz == 24 ) ) ) { /* Forced down-switching due to WB input */\r
-                psEnc->sCmn.sLP.transition_frame_no = 1;                                /* Begin transition phase */\r
-                psEnc->sCmn.sLP.mode                = 0;                                /* Switch down */\r
-            } else if( \r
-                ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_DOWN ) &&    /* Transition phase complete */\r
-                ( psEnc->sCmn.sLP.mode == 0 ) ) {                                       /* Ready to switch down */\r
-                psEnc->sCmn.sLP.transition_frame_no = 0;                                /* Ready for new transition phase */\r
-#else\r
-            if( psEnc->sCmn.bitrateDiff <= -ACCUM_BITS_DIFF_THRESHOLD ) {               /* Bitrate threshold is met */ \r
-#endif            \r
-                psEnc->sCmn.bitrateDiff = 0;\r
-\r
-                /* Switch to a lower sample frequency */\r
-                if( psEnc->sCmn.fs_kHz == 24 ) {\r
-                    fs_kHz = 16;\r
-                } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
-                    fs_kHz = 12;\r
-                } else {\r
-                    SKP_assert( psEnc->sCmn.fs_kHz == 12 );\r
-                    fs_kHz = 8;\r
-                }\r
-            }\r
-\r
-            /* Check if we should switch up */\r
-            if( ( ( SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) < API_fs_Hz ) &&\r
-                ( TargetRate_bps >= psEnc->sCmn.bitrate_threshold_up ) && \r
-                ( psEnc->sCmn.sSWBdetect.WB_detected * psEnc->sCmn.fs_kHz != 16 ) ) && \r
-                ( ( psEnc->sCmn.fs_kHz == 16 ) && ( max_internal_fs_kHz >= 24 ) || \r
-                  ( psEnc->sCmn.fs_kHz == 12 ) && ( max_internal_fs_kHz >= 16 ) ||\r
-                  ( psEnc->sCmn.fs_kHz ==  8 ) && ( max_internal_fs_kHz >= 12 ) ) \r
-#if SWITCH_TRANSITION_FILTERING\r
-                  && ( psEnc->sCmn.sLP.transition_frame_no == 0 ) ) { /* No transition phase running, ready to switch */\r
-                    psEnc->sCmn.sLP.mode = 1; /* Switch up */\r
-#else\r
-                ) {\r
-#endif\r
-                psEnc->sCmn.bitrateDiff = 0;\r
-\r
-                /* Switch to a higher sample frequency */\r
-                if( psEnc->sCmn.fs_kHz == 8 ) {\r
-                    fs_kHz = 12;\r
-                } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
-                    fs_kHz = 16;\r
-                } else {\r
-                    SKP_assert( psEnc->sCmn.fs_kHz == 16 );\r
-                    fs_kHz = 24;\r
-                } \r
-            }\r
+    if( psEnc->sCmn.controlled_since_last_payload != 0 ) {\r
+        if( psEnc->sCmn.API_fs_Hz != psEnc->sCmn.prev_API_fs_Hz && psEnc->sCmn.fs_kHz > 0 ) {\r
+            /* Change in API sampling rate in the middle of encoding a packet */\r
+            ret += SKP_Silk_setup_resamplers( psEnc, psEnc->sCmn.fs_kHz );\r
         }\r
         }\r
+        return ret;\r
     }\r
 \r
     }\r
 \r
-#if SWITCH_TRANSITION_FILTERING\r
-    /* After switching up, stop transition filter during speech inactivity */\r
-    if( ( psEnc->sCmn.sLP.mode == 1 ) &&\r
-        ( psEnc->sCmn.sLP.transition_frame_no >= TRANSITION_FRAMES_UP ) && \r
-        ( psEnc->speech_activity_Q8 < 128 ) && \r
-        ( psEnc->sCmn.nFramesInPayloadBuf == 0 ) ) {\r
-        \r
-        psEnc->sCmn.sLP.transition_frame_no = 0;\r
-\r
-        /* Reset transition filter state */\r
-        SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, 2 * sizeof( SKP_int32 ) );\r
-    }\r
-#endif\r
-\r
-#ifdef FORCE_FS_KHZ\r
-    SKP_assert( FORCE_FS_KHZ == 8 || FORCE_FS_KHZ == 12 || FORCE_FS_KHZ == 16 || FORCE_FS_KHZ == 24 );\r
-    if( psEnc->sCmn.fs_kHz != 0 ) {\r
-        /* Force except during init */\r
-        fs_kHz = FORCE_FS_KHZ;\r
-    }\r
-#endif\r
+    /* Beyond this point we know that there are no previously coded frames in the payload buffer */\r
 \r
 \r
-#ifdef SAVE_ALL_INTERNAL_DATA\r
-    DEBUG_STORE_DATA( bitrate.dat,  &TargetRate_bps,          1 * sizeof( SKP_int32 ) ); \r
-    DEBUG_STORE_DATA( fs.dat,       &fs_kHz,                  1 * sizeof( SKP_int32 ) ); \r
-    DEBUG_STORE_DATA( diff.dat,     &psEnc->sCmn.bitrateDiff, 1 * sizeof( SKP_int32 ) ); \r
-    DEBUG_STORE_DATA( lashape.dat,  &psEnc->sCmn.la_shape,    1 * sizeof( SKP_int32 ) ); \r
-#endif\r
     /********************************************/\r
     /********************************************/\r
-    /* Prepare resampler and buffered data      */    \r
+    /* Determine internal sampling rate         */\r
     /********************************************/\r
     /********************************************/\r
-    SKP_Silk_setup_resamplers( psEnc, API_fs_Hz, fs_kHz );\r
+    fs_kHz = SKP_Silk_control_audio_bandwidth( &psEnc->sCmn, TargetRate_bps );\r
 \r
     /********************************************/\r
 \r
     /********************************************/\r
-    /* Set packet size                          */\r
+    /* Prepare resampler and buffered data      */\r
     /********************************************/\r
     /********************************************/\r
-    ret += SKP_Silk_setup_packetsize( psEnc, PacketSize_ms, fs_kHz );\r
+    ret += SKP_Silk_setup_resamplers( psEnc, fs_kHz );\r
 \r
     /********************************************/\r
     /* Set internal sampling frequency          */\r
     /********************************************/\r
 \r
     /********************************************/\r
     /* Set internal sampling frequency          */\r
     /********************************************/\r
-    ret += SKP_Silk_setup_fs( psEnc, fs_kHz );\r
+    ret += SKP_Silk_setup_fs( psEnc, fs_kHz, PacketSize_ms );\r
 \r
     /********************************************/\r
     /* Set encoding complexity                  */\r
     /********************************************/\r
 \r
     /********************************************/\r
     /* Set encoding complexity                  */\r
     /********************************************/\r
-    ret += SKP_Silk_setup_complexity( psEnc, Complexity );\r
+    ret += SKP_Silk_setup_complexity( &psEnc->sCmn, Complexity );\r
 \r
     /********************************************/\r
     /* Set bitrate/coding quality               */\r
 \r
     /********************************************/\r
     /* Set bitrate/coding quality               */\r
@@ -209,15 +111,9 @@ SKP_int SKP_Silk_control_encoder_FIX(
     /********************************************/\r
     /* Set LBRR usage                           */\r
     /********************************************/\r
     /********************************************/\r
     /* Set LBRR usage                           */\r
     /********************************************/\r
-    ret += SKP_Silk_setup_LBRR( psEnc, INBandFEC_enabled );\r
+    ret += SKP_Silk_setup_LBRR( psEnc );\r
 \r
 \r
-    /********************************************/\r
-    /* Set DTX mode                             */\r
-    /********************************************/\r
-    if( DTX_enabled < 0 || DTX_enabled > 1 ) {\r
-        ret = SKP_SILK_ENC_INVALID_DTX_SETTING;\r
-    }\r
-    psEnc->sCmn.useDTX = DTX_enabled;\r
+    psEnc->sCmn.controlled_since_last_payload = 1;\r
 \r
     return ret;\r
 }\r
 \r
     return ret;\r
 }\r
@@ -225,7 +121,7 @@ SKP_int SKP_Silk_control_encoder_FIX(
 /* Control low bitrate redundancy usage */\r
 void SKP_Silk_LBRR_ctrl_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,     /* I/O  encoder state                               */\r
 /* Control low bitrate redundancy usage */\r
 void SKP_Silk_LBRR_ctrl_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,     /* I/O  encoder state                               */\r
-    SKP_Silk_encoder_control_FIX    *psEncCtrl  /* I/O  encoder control                             */\r
+    SKP_Silk_encoder_control        *psEncCtrlC /* I/O  encoder control                             */\r
 )\r
 {\r
     SKP_int LBRR_usage;\r
 )\r
 {\r
     SKP_int LBRR_usage;\r
@@ -236,108 +132,110 @@ void SKP_Silk_LBRR_ctrl_FIX(
         /* Usage Control based on sensitivity and packet loss caracteristics */\r
         /* For now only enable adding to next for active frames. Make more complex later */\r
         LBRR_usage = SKP_SILK_NO_LBRR;\r
         /* Usage Control based on sensitivity and packet loss caracteristics */\r
         /* For now only enable adding to next for active frames. Make more complex later */\r
         LBRR_usage = SKP_SILK_NO_LBRR;\r
-        if( psEnc->speech_activity_Q8 > LBRR_SPEECH_ACTIVITY_THRES_Q8 && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) { // nb! maybe multiply loss prob and speech activity \r
-            LBRR_usage = SKP_SILK_ADD_LBRR_TO_PLUS1;\r
+        if( psEnc->speech_activity_Q8 > SKP_FIX_CONST( LBRR_SPEECH_ACTIVITY_THRES, 8 ) && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) {\r
+            LBRR_usage = SKP_SILK_LBRR;\r
         }\r
         }\r
-        psEncCtrl->sCmn.LBRR_usage = LBRR_usage;\r
+        psEncCtrlC->LBRR_usage = LBRR_usage;\r
     } else {\r
     } else {\r
-        psEncCtrl->sCmn.LBRR_usage = SKP_SILK_NO_LBRR;\r
+        psEncCtrlC->LBRR_usage = SKP_SILK_NO_LBRR;\r
     }\r
 }\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_packetsize(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
     }\r
 }\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_packetsize(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         PacketSize_ms,      /* I                        */\r
-    SKP_int                         fs_kHz              /* I                        */\r
+    SKP_int                         PacketSize_ms       /* I                        */\r
 )\r
 {\r
     SKP_int ret = SKP_SILK_NO_ERROR;\r
     if( ( PacketSize_ms !=  10 ) &&\r
         ( PacketSize_ms !=  20 ) &&\r
         ( PacketSize_ms !=  40 ) && \r
 )\r
 {\r
     SKP_int ret = SKP_SILK_NO_ERROR;\r
     if( ( PacketSize_ms !=  10 ) &&\r
         ( PacketSize_ms !=  20 ) &&\r
         ( PacketSize_ms !=  40 ) && \r
-        ( PacketSize_ms !=  60 ) && \r
-        ( PacketSize_ms !=  80 ) && \r
-        ( PacketSize_ms != 100 ) ) {\r
+        ( PacketSize_ms !=  60 ) ) {\r
         ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;\r
     } else {\r
         if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {\r
             if( PacketSize_ms == 10 ) {\r
         ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;\r
     } else {\r
         if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {\r
             if( PacketSize_ms == 10 ) {\r
-                if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) {\r
-                    /* Only allowed when the payload buffer is empty */\r
-                    psEnc->sCmn.nb_subfr      = MAX_NB_SUBFR >> 1;\r
-                    psEnc->sCmn.frame_length  = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );\r
-                    psEnc->sCmn.PacketSize_ms = PacketSize_ms;\r
-                    psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
-                    /* Packet length changes. Reset LBRR buffer */\r
-                    SKP_Silk_LBRR_reset( &psEnc->sCmn );\r
-                }\r
+                /* Only allowed when the payload buffer is empty */\r
+                psEnc->sCmn.nb_subfr = MAX_NB_SUBFR >> 1;\r
+                psEnc->sCmn.PacketSize_ms = PacketSize_ms;\r
+                psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, psEnc->sCmn.fs_kHz );\r
+                /* Packet length changes. Reset LBRR buffer */\r
+                SKP_Silk_LBRR_reset( &psEnc->sCmn );\r
             } else{\r
             } else{\r
-                psEnc->sCmn.nb_subfr      = MAX_NB_SUBFR;\r
-                psEnc->sCmn.frame_length  = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );\r
+                psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;\r
                 psEnc->sCmn.PacketSize_ms = PacketSize_ms;\r
                 psEnc->sCmn.PacketSize_ms = PacketSize_ms;\r
-                psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
+                psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, psEnc->sCmn.fs_kHz );\r
                 /* Packet length changes. Reset LBRR buffer */\r
                 SKP_Silk_LBRR_reset( &psEnc->sCmn );\r
             }\r
         }\r
                 /* Packet length changes. Reset LBRR buffer */\r
                 SKP_Silk_LBRR_reset( &psEnc->sCmn );\r
             }\r
         }\r
+        psEnc->sCmn.frame_length = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );\r
     }\r
     }\r
+\r
     return(ret);\r
 }\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_resamplers(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
     return(ret);\r
 }\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_resamplers(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         API_fs_Hz,          /* I                        */\r
     SKP_int                         fs_kHz              /* I                        */\r
 )\r
 {\r
     SKP_int ret = SKP_SILK_NO_ERROR;\r
     \r
     SKP_int                         fs_kHz              /* I                        */\r
 )\r
 {\r
     SKP_int ret = SKP_SILK_NO_ERROR;\r
     \r
-    if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != API_fs_Hz ) {\r
+    if( psEnc->sCmn.fs_kHz != fs_kHz || psEnc->sCmn.prev_API_fs_Hz != psEnc->sCmn.API_fs_Hz ) {\r
 \r
 \r
-        /* Allocate space for worst case temporary upsampling, 8 to 48 kHz, so a factor 6 */\r
-        SKP_int16 x_buf_API_fs_Hz[ ( MAX_API_FS_KHZ / 8 ) * ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) ];\r
+        if( psEnc->sCmn.fs_kHz == 0 ) {\r
+            /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
+            ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, fs_kHz * 1000 );\r
+        } else {\r
+            /* Allocate space for worst case temporary upsampling, 8 to 48 kHz, so a factor 6 */\r
+            SKP_int16 x_buf_API_fs_Hz[ ( 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX ) * ( MAX_API_FS_KHZ / 8 ) ];\r
 \r
 \r
-        SKP_int32 nSamples_temp = SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + psEnc->sCmn.la_shape;\r
+            SKP_int32 nSamples_temp = SKP_LSHIFT( psEnc->sCmn.frame_length, 1 ) + LA_SHAPE_MS * psEnc->sCmn.fs_kHz;\r
 \r
 \r
-        if( SKP_SMULBB( fs_kHz, 1000 ) < API_fs_Hz && psEnc->sCmn.fs_kHz != 0 ) {\r
-            /* Resample buffered data in x_buf to API_fs_Hz */\r
+            if( SKP_SMULBB( fs_kHz, 1000 ) < psEnc->sCmn.API_fs_Hz && psEnc->sCmn.fs_kHz != 0 ) {\r
+                /* Resample buffered data in x_buf to API_fs_Hz */\r
 \r
 \r
-            SKP_Silk_resampler_state_struct  temp_resampler_state;\r
+                SKP_Silk_resampler_state_struct  temp_resampler_state;\r
 \r
 \r
-            /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */\r
-            ret += SKP_Silk_resampler_init( &temp_resampler_state, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), API_fs_Hz );\r
+                /* Initialize resampler for temporary resampling of x_buf data to API_fs_Hz */\r
+                ret += SKP_Silk_resampler_init( &temp_resampler_state, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ), psEnc->sCmn.API_fs_Hz );\r
 \r
 \r
-            /* Temporary resampling of x_buf data to API_fs_Hz */\r
-            ret += SKP_Silk_resampler( &temp_resampler_state, x_buf_API_fs_Hz, psEnc->x_buf, nSamples_temp );\r
+                /* Temporary resampling of x_buf data to API_fs_Hz */\r
+                ret += SKP_Silk_resampler( &temp_resampler_state, x_buf_API_fs_Hz, psEnc->x_buf, nSamples_temp );\r
 \r
 \r
-            /* Calculate number of samples that has been temporarily upsampled */\r
-            nSamples_temp = SKP_DIV32_16( nSamples_temp * API_fs_Hz, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) );\r
+                /* Calculate number of samples that has been temporarily upsampled */\r
+                nSamples_temp = SKP_DIV32_16( nSamples_temp * psEnc->sCmn.API_fs_Hz, SKP_SMULBB( psEnc->sCmn.fs_kHz, 1000 ) );\r
 \r
 \r
-            /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
-            ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, API_fs_Hz, SKP_SMULBB( fs_kHz, 1000 ) );\r
+                /* Initialize the resampler for enc_API.c preparing resampling from API_fs_Hz to fs_kHz */\r
+                ret += SKP_Silk_resampler_init( &psEnc->sCmn.resampler_state, psEnc->sCmn.API_fs_Hz, SKP_SMULBB( fs_kHz, 1000 ) );\r
 \r
 \r
-        } else {\r
-            /* Copy data */\r
-            SKP_memcpy( x_buf_API_fs_Hz, psEnc->x_buf, nSamples_temp * sizeof( SKP_int16 ) );\r
-        }\r
+            } else {\r
+                /* Copy data */\r
+                SKP_memcpy( x_buf_API_fs_Hz, psEnc->x_buf, nSamples_temp * sizeof( SKP_int16 ) );\r
+            }\r
 \r
 \r
-        if( 1000 * fs_kHz != API_fs_Hz ) {\r
-            /* Correct resampler state (unless resampling by a factor 1) by resampling buffered data from API_fs_Hz to fs_kHz */\r
-            ret += SKP_Silk_resampler( &psEnc->sCmn.resampler_state, psEnc->x_buf, x_buf_API_fs_Hz, nSamples_temp );\r
+            if( 1000 * fs_kHz != psEnc->sCmn.API_fs_Hz ) {\r
+                /* Correct resampler state (unless resampling by a factor 1) by resampling buffered data from API_fs_Hz to fs_kHz */\r
+                ret += SKP_Silk_resampler( &psEnc->sCmn.resampler_state, psEnc->x_buf, x_buf_API_fs_Hz, nSamples_temp );\r
+            }\r
         }\r
     }\r
         }\r
     }\r
-    psEnc->sCmn.prev_API_fs_Hz = API_fs_Hz;\r
\r
+\r
+    psEnc->sCmn.prev_API_fs_Hz = psEnc->sCmn.API_fs_Hz;\r
+\r
     return(ret);\r
 }\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_fs(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
     return(ret);\r
 }\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_fs(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         fs_kHz              /* I                        */\r
+    SKP_int                         fs_kHz,             /* I                        */\r
+    SKP_int                         PacketSize_ms       /* I                        */\r
 )\r
 {\r
 )\r
 {\r
-    SKP_int ret = 0;\r
+    SKP_int ret = SKP_SILK_NO_ERROR;\r
 \r
 \r
+    /* Set internal sampling frequency */\r
     if( psEnc->sCmn.fs_kHz != fs_kHz ) {\r
         /* reset part of the state */\r
         SKP_memset( &psEnc->sShape,          0,                            sizeof( SKP_Silk_shape_state_FIX ) );\r
     if( psEnc->sCmn.fs_kHz != fs_kHz ) {\r
         /* reset part of the state */\r
         SKP_memset( &psEnc->sShape,          0,                            sizeof( SKP_Silk_shape_state_FIX ) );\r
@@ -374,6 +272,7 @@ SKP_INLINE SKP_int SKP_Silk_setup_fs(
         psEnc->sNSQ.lagPrev                 = 100;\r
         psEnc->sNSQ.prev_inv_gain_Q16       = 65536;\r
         psEnc->sNSQ_LBRR.prev_inv_gain_Q16  = 65536;\r
         psEnc->sNSQ.lagPrev                 = 100;\r
         psEnc->sNSQ.prev_inv_gain_Q16       = 65536;\r
         psEnc->sNSQ_LBRR.prev_inv_gain_Q16  = 65536;\r
+\r
         psEnc->sCmn.fs_kHz = fs_kHz;\r
         if( psEnc->sCmn.fs_kHz == 8 ) {\r
             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;\r
         psEnc->sCmn.fs_kHz = fs_kHz;\r
         if( psEnc->sCmn.fs_kHz == 8 ) {\r
             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;\r
@@ -385,10 +284,8 @@ SKP_INLINE SKP_int SKP_Silk_setup_fs(
             psEnc->sCmn.psNLSF_CB[ 1 ]  = &SKP_Silk_NLSF_CB1_16;\r
         }\r
         psEnc->sCmn.subfr_length   = SKP_SMULBB( SUB_FRAME_LENGTH_MS, fs_kHz );\r
             psEnc->sCmn.psNLSF_CB[ 1 ]  = &SKP_Silk_NLSF_CB1_16;\r
         }\r
         psEnc->sCmn.subfr_length   = SKP_SMULBB( SUB_FRAME_LENGTH_MS, fs_kHz );\r
-        psEnc->sCmn.frame_length   = SKP_SMULBB( psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr );\r
         psEnc->sCmn.ltp_mem_length = SKP_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz ); \r
         psEnc->sCmn.la_pitch       = SKP_SMULBB( LA_PITCH_MS, fs_kHz );\r
         psEnc->sCmn.ltp_mem_length = SKP_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz ); \r
         psEnc->sCmn.la_pitch       = SKP_SMULBB( LA_PITCH_MS, fs_kHz );\r
-        psEnc->sCmn.la_shape       = SKP_SMULBB( LA_SHAPE_MS, fs_kHz );\r
         psEnc->sPred.min_pitch_lag = SKP_SMULBB(  3, fs_kHz );\r
         psEnc->sPred.max_pitch_lag = SKP_SMULBB( 18, fs_kHz );\r
         if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
         psEnc->sPred.min_pitch_lag = SKP_SMULBB(  3, fs_kHz );\r
         psEnc->sPred.max_pitch_lag = SKP_SMULBB( 18, fs_kHz );\r
         if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
@@ -397,87 +294,33 @@ SKP_INLINE SKP_int SKP_Silk_setup_fs(
             psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
         }\r
         if( psEnc->sCmn.fs_kHz == 24 ) {\r
             psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
         }\r
         if( psEnc->sCmn.fs_kHz == 24 ) {\r
-            psEnc->mu_LTP_Q8 = MU_LTP_QUANT_SWB_Q8;\r
+            psEnc->mu_LTP_Q8 = SKP_FIX_CONST( MU_LTP_QUANT_SWB, 8 );\r
             psEnc->sCmn.bitrate_threshold_up   = SKP_int32_MAX;\r
             psEnc->sCmn.bitrate_threshold_down = SWB2WB_BITRATE_BPS; \r
         } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
             psEnc->sCmn.bitrate_threshold_up   = SKP_int32_MAX;\r
             psEnc->sCmn.bitrate_threshold_down = SWB2WB_BITRATE_BPS; \r
         } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
-            psEnc->mu_LTP_Q8 = MU_LTP_QUANT_WB_Q8;\r
+            psEnc->mu_LTP_Q8 = SKP_FIX_CONST( MU_LTP_QUANT_WB, 8 );\r
             psEnc->sCmn.bitrate_threshold_up   = WB2SWB_BITRATE_BPS;\r
             psEnc->sCmn.bitrate_threshold_down = WB2MB_BITRATE_BPS; \r
         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
             psEnc->sCmn.bitrate_threshold_up   = WB2SWB_BITRATE_BPS;\r
             psEnc->sCmn.bitrate_threshold_down = WB2MB_BITRATE_BPS; \r
         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
-            psEnc->mu_LTP_Q8 = MU_LTP_QUANT_MB_Q8;\r
+            psEnc->mu_LTP_Q8 = SKP_FIX_CONST( MU_LTP_QUANT_MB, 8 );\r
             psEnc->sCmn.bitrate_threshold_up   = MB2WB_BITRATE_BPS;\r
             psEnc->sCmn.bitrate_threshold_down = MB2NB_BITRATE_BPS;\r
         } else {\r
             psEnc->sCmn.bitrate_threshold_up   = MB2WB_BITRATE_BPS;\r
             psEnc->sCmn.bitrate_threshold_down = MB2NB_BITRATE_BPS;\r
         } else {\r
-            psEnc->mu_LTP_Q8 = MU_LTP_QUANT_NB_Q8;\r
+            psEnc->mu_LTP_Q8 = SKP_FIX_CONST( MU_LTP_QUANT_NB, 8 );\r
             psEnc->sCmn.bitrate_threshold_up   = NB2MB_BITRATE_BPS;\r
             psEnc->sCmn.bitrate_threshold_down = 0;\r
         }\r
         psEnc->sCmn.fs_kHz_changed = 1;\r
             psEnc->sCmn.bitrate_threshold_up   = NB2MB_BITRATE_BPS;\r
             psEnc->sCmn.bitrate_threshold_down = 0;\r
         }\r
         psEnc->sCmn.fs_kHz_changed = 1;\r
-\r
-        /* Check that settings are valid */\r
-        SKP_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );\r
     }\r
     }\r
-    return( ret );\r
-}\r
 \r
 \r
-SKP_INLINE SKP_int SKP_Silk_setup_complexity(\r
-    SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         Complexity          /* I                        */\r
-)\r
-{\r
-    SKP_int ret = 0;\r
+    /********************************************/\r
+    /* Set packet size                          */\r
+    /********************************************/\r
+    ret += SKP_Silk_setup_packetsize( psEnc, PacketSize_ms );\r
 \r
     /* Check that settings are valid */\r
 \r
     /* Check that settings are valid */\r
-    if( LOW_COMPLEXITY_ONLY && Complexity != 0 ) { \r
-        ret = SKP_SILK_ENC_INVALID_COMPLEXITY_SETTING;\r
-    }\r
-\r
-    /* Set encoding complexity */\r
-    if( Complexity == 0 || LOW_COMPLEXITY_ONLY ) {\r
-        /* Low complexity */\r
-        psEnc->sCmn.Complexity                  = 0;\r
-        psEnc->sCmn.pitchEstimationComplexity   = PITCH_EST_COMPLEXITY_LC_MODE;\r
-        psEnc->pitchEstimationThreshold_Q16     = FIND_PITCH_CORRELATION_THRESHOLD_Q16_LC_MODE;\r
-        psEnc->sCmn.pitchEstimationLPCOrder     = 8;\r
-        psEnc->sCmn.shapingLPCOrder             = 8;\r
-        psEnc->sCmn.nStatesDelayedDecision      = 1;\r
-        psEnc->sCmn.useInterpolatedNLSFs        = 0;\r
-        psEnc->sCmn.LTPQuantLowComplexity       = 1;\r
-        psEnc->sCmn.NLSF_MSVQ_Survivors         = MAX_NLSF_MSVQ_SURVIVORS_LC_MODE;\r
-    } else if( Complexity == 1 ) {\r
-        /* Medium complexity */\r
-        psEnc->sCmn.Complexity                  = 1;\r
-        psEnc->sCmn.pitchEstimationComplexity   = PITCH_EST_COMPLEXITY_MC_MODE;\r
-        psEnc->pitchEstimationThreshold_Q16     = FIND_PITCH_CORRELATION_THRESHOLD_Q16_MC_MODE;\r
-        psEnc->sCmn.pitchEstimationLPCOrder     = 12;\r
-        psEnc->sCmn.shapingLPCOrder             = 12;\r
-        psEnc->sCmn.nStatesDelayedDecision      = 2;\r
-        psEnc->sCmn.useInterpolatedNLSFs        = 0;\r
-        psEnc->sCmn.LTPQuantLowComplexity       = 0;\r
-        psEnc->sCmn.NLSF_MSVQ_Survivors         = MAX_NLSF_MSVQ_SURVIVORS_MC_MODE;\r
-    } else if( Complexity == 2 ) {\r
-        /* High complexity */\r
-        psEnc->sCmn.Complexity                  = 2;\r
-        psEnc->sCmn.pitchEstimationComplexity   = PITCH_EST_COMPLEXITY_HC_MODE;\r
-        psEnc->pitchEstimationThreshold_Q16     = FIND_PITCH_CORRELATION_THRESHOLD_Q16_HC_MODE;\r
-        psEnc->sCmn.pitchEstimationLPCOrder     = 16;\r
-        psEnc->sCmn.shapingLPCOrder             = 16;\r
-        psEnc->sCmn.nStatesDelayedDecision      = 4;\r
-        psEnc->sCmn.useInterpolatedNLSFs        = 1;\r
-        psEnc->sCmn.LTPQuantLowComplexity       = 0;\r
-        psEnc->sCmn.NLSF_MSVQ_Survivors         = MAX_NLSF_MSVQ_SURVIVORS;\r
-    } else {\r
-        ret = SKP_SILK_ENC_INVALID_COMPLEXITY_SETTING;\r
-    }\r
-\r
-    /* Do not allow higher pitch estimation LPC order than predict LPC order */\r
-    psEnc->sCmn.pitchEstimationLPCOrder = SKP_min_int( psEnc->sCmn.pitchEstimationLPCOrder, psEnc->sCmn.predictLPCOrder );\r
-\r
-    SKP_assert( psEnc->sCmn.pitchEstimationLPCOrder <= MAX_FIND_PITCH_LPC_ORDER );\r
-    SKP_assert( psEnc->sCmn.shapingLPCOrder         <= MAX_SHAPE_LPC_ORDER      );\r
-    SKP_assert( psEnc->sCmn.nStatesDelayedDecision  <= MAX_DEL_DEC_STATES       );\r
-\r
+    SKP_assert( ( psEnc->sCmn.subfr_length * psEnc->sCmn.nb_subfr ) == psEnc->sCmn.frame_length );\r
\r
     return( ret );\r
 }\r
 \r
     return( ret );\r
 }\r
 \r
@@ -490,16 +333,7 @@ SKP_INLINE SKP_int SKP_Silk_setup_rate(
     SKP_int32 frac_Q6;\r
     const SKP_int32 *rateTable;\r
 \r
     SKP_int32 frac_Q6;\r
     const SKP_int32 *rateTable;\r
 \r
-    TargetRate_bps = SKP_min( TargetRate_bps, 100000 );\r
-    if( psEnc->sCmn.fs_kHz == 8 ) {\r
-        TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_NB_BPS );\r
-    } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
-        TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_MB_BPS );\r
-    } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
-        TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_WB_BPS );\r
-    } else {\r
-        TargetRate_bps = SKP_max( TargetRate_bps, MIN_TARGET_RATE_SWB_BPS );\r
-    }\r
+    /* Set bitrate/coding quality */\r
     if( TargetRate_bps != psEnc->sCmn.TargetRate_bps ) {\r
         psEnc->sCmn.TargetRate_bps = TargetRate_bps;\r
 \r
     if( TargetRate_bps != psEnc->sCmn.TargetRate_bps ) {\r
         psEnc->sCmn.TargetRate_bps = TargetRate_bps;\r
 \r
@@ -527,57 +361,53 @@ SKP_INLINE SKP_int SKP_Silk_setup_rate(
 }\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_LBRR(\r
 }\r
 \r
 SKP_INLINE SKP_int SKP_Silk_setup_LBRR(\r
-    SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         INBandFEC_enabled   /* I                        */\r
+    SKP_Silk_encoder_state_FIX      *psEnc             /* I/O                      */\r
 )\r
 {\r
 )\r
 {\r
-    SKP_int   ret = 0;\r
-    SKP_int32 LBRRRate_thres_bps;\r
+    SKP_int   ret = SKP_SILK_NO_ERROR;\r
 \r
 #if USE_LBRR\r
 \r
 #if USE_LBRR\r
-    if( INBandFEC_enabled < 0 || INBandFEC_enabled > 1 ) {\r
+    SKP_int32 LBRRRate_thres_bps;\r
+\r
+    if( psEnc->sCmn.useInBandFEC < 0 || psEnc->sCmn.useInBandFEC > 1 ) {\r
         ret = SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING;\r
     }\r
     \r
         ret = SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING;\r
     }\r
     \r
-    /* Only change settings if first frame in packet */\r
-    if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) {\r
-        \r
-        psEnc->sCmn.LBRR_enabled = INBandFEC_enabled;\r
-        if( psEnc->sCmn.fs_kHz == 8 ) {\r
-            LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 9000;\r
-        } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
-            LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 6000;;\r
-        } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
-            LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 3000;\r
-        } else {\r
-            LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS;\r
-        }\r
+    psEnc->sCmn.LBRR_enabled = psEnc->sCmn.useInBandFEC;\r
+    if( psEnc->sCmn.fs_kHz == 8 ) {\r
+        LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 9000;\r
+    } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
+        LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 6000;;\r
+    } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
+        LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS - 3000;\r
+    } else {\r
+        LBRRRate_thres_bps = INBAND_FEC_MIN_RATE_BPS;\r
+    }\r
 \r
 \r
-        if( psEnc->sCmn.TargetRate_bps >= LBRRRate_thres_bps ) {\r
-            /* Set gain increase / rate reduction for LBRR usage */\r
-            /* Coarsely tuned with PESQ for now. */\r
-            /* Linear regression coefs G = 8 - 0.5 * loss */\r
-            /* Meaning that at 16% loss main rate and redundant rate is the same, -> G = 0 */\r
-            psEnc->sCmn.LBRR_GainIncreases = SKP_max_int( 8 - SKP_RSHIFT( psEnc->sCmn.PacketLoss_perc, 1 ), 0 );\r
-\r
-            /* Set main stream rate compensation */\r
-            if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) {\r
-                /* Tuned to give aprox same mean / weighted bitrate as no inband FEC */\r
-                psEnc->inBandFEC_SNR_comp_Q8 = ( 6 << 8 ) - SKP_LSHIFT( psEnc->sCmn.LBRR_GainIncreases, 7 );\r
-            } else {\r
-                psEnc->inBandFEC_SNR_comp_Q8 = 0;\r
-                psEnc->sCmn.LBRR_enabled     = 0;\r
-            }\r
+    if( psEnc->sCmn.TargetRate_bps >= LBRRRate_thres_bps ) {\r
+        /* Set gain increase / rate reduction for LBRR usage */\r
+        /* Coarsely tuned with PESQ for now. */\r
+        /* Linear regression coefs G = 8 - 0.5 * loss */\r
+        /* Meaning that at 16% loss main rate and redundant rate is the same, -> G = 0 */\r
+        psEnc->sCmn.LBRR_GainIncreases = SKP_max_int( 8 - SKP_RSHIFT( psEnc->sCmn.PacketLoss_perc, 1 ), 0 );\r
+\r
+        /* Set main stream rate compensation */\r
+        if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.PacketLoss_perc > LBRR_LOSS_THRES ) {\r
+            /* Tuned to give approx same mean / weighted bitrate as no inband FEC */\r
+            psEnc->inBandFEC_SNR_comp_Q8 = SKP_FIX_CONST( 6.0f, 8 ) - SKP_LSHIFT( psEnc->sCmn.LBRR_GainIncreases, 7 );\r
         } else {\r
         } else {\r
-            psEnc->inBandFEC_SNR_comp_Q8     = 0;\r
-            psEnc->sCmn.LBRR_enabled         = 0;\r
+            psEnc->inBandFEC_SNR_comp_Q8 = 0;\r
+            psEnc->sCmn.LBRR_enabled     = 0;\r
         }\r
         }\r
+    } else {\r
+        psEnc->inBandFEC_SNR_comp_Q8     = 0;\r
+        psEnc->sCmn.LBRR_enabled         = 0;\r
     }\r
 #else\r
     }\r
 #else\r
-    if( INBandFEC_enabled != 0 ) {\r
+    if( psEnc->sCmn.LBRR_enabled != 0 ) {\r
         ret = SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING;\r
         ret = SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING;\r
+        psEnc->sCmn.LBRR_enabled = 0;\r
     }\r
     }\r
-    psEnc->sCmn.LBRR_enabled = 0;\r
 #endif\r
 #endif\r
-    return( ret );\r
-}
\ No newline at end of file
+    return ret;\r
+}\r
index 8d41874..2370a55 100644 (file)
@@ -72,6 +72,7 @@ void SKP_Silk_corrMatrix_FIX(
     const SKP_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */\r
     const SKP_int                   L,          /* I    Length of vectors                                   */\r
     const SKP_int                   order,      /* I    Max lag for correlation                             */\r
     const SKP_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */\r
     const SKP_int                   L,          /* I    Length of vectors                                   */\r
     const SKP_int                   order,      /* I    Max lag for correlation                             */\r
+    const SKP_int                   head_room,  /* I    Desired headroom                                    */\r
     SKP_int32                       *XX,        /* O    Pointer to X'*X correlation matrix [ order x order ]*/\r
     SKP_int                         *rshifts    /* I/O  Right shifts of correlations                        */\r
 )\r
     SKP_int32                       *XX,        /* O    Pointer to X'*X correlation matrix [ order x order ]*/\r
     SKP_int                         *rshifts    /* I/O  Right shifts of correlations                        */\r
 )\r
@@ -82,9 +83,8 @@ void SKP_Silk_corrMatrix_FIX(
 \r
     /* Calculate energy to find shift used to fit in 32 bits */\r
     SKP_Silk_sum_sqr_shift( &energy, &rshifts_local, x, L + order - 1 );\r
 \r
     /* Calculate energy to find shift used to fit in 32 bits */\r
     SKP_Silk_sum_sqr_shift( &energy, &rshifts_local, x, L + order - 1 );\r
-    /* Add shifts to get the wanted head room */\r
-\r
-    head_room_rshifts = SKP_max( LTP_CORRS_HEAD_ROOM - SKP_Silk_CLZ32( energy ), 0 );\r
+    /* Add shifts to get the desired head room */\r
+    head_room_rshifts = SKP_max( head_room - SKP_Silk_CLZ32( energy ), 0 );\r
     \r
     energy = SKP_RSHIFT32( energy, head_room_rshifts );\r
     rshifts_local += head_room_rshifts;\r
     \r
     energy = SKP_RSHIFT32( energy, head_room_rshifts );\r
     rshifts_local += head_room_rshifts;\r
index 7d42a4d..e4b726d 100644 (file)
@@ -26,16 +26,17 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 /****************/\r
 /* Encode frame */\r
 /****************/\r
 SKP_int SKP_Silk_encode_frame_FIX( \r
 \r
 /****************/\r
 /* Encode frame */\r
 /****************/\r
 SKP_int SKP_Silk_encode_frame_FIX( \r
-    SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O  Pointer to Silk FIX encoder state           */\r
-    SKP_uint8                       *pCode,             /* O    Pointer to payload                          */\r
-    SKP_int16                       *pnBytesOut,        /* I/O  Pointer to number of payload bytes          */\r
-                                                        /*      input: max length; output: used             */\r
-    const SKP_int16                 *pIn                /* I    Pointer to input speech frame               */\r
+    SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O  Encoder state FIX                       */\r
+    SKP_int32                       *pnBytesOut,        /* I/O  Number of payload bytes                 */\r
+                                                        /*      input: max length; output: used         */\r
+    ec_enc                          *psRangeEnc,        /* I/O  compressor data structure               */\r
+    const SKP_int16                 *pIn                /* I    Input speech frame                      */\r
 )\r
 {\r
     SKP_Silk_encoder_control_FIX sEncCtrl;\r
 )\r
 {\r
     SKP_Silk_encoder_control_FIX sEncCtrl;\r
@@ -44,29 +45,28 @@ SKP_int SKP_Silk_encode_frame_FIX(
     SKP_int16   xfw[ MAX_FRAME_LENGTH ];\r
     SKP_int16   pIn_HP[ MAX_FRAME_LENGTH ];\r
     SKP_int16   res_pitch[ 2 * MAX_FRAME_LENGTH + LA_PITCH_MAX ];\r
     SKP_int16   xfw[ MAX_FRAME_LENGTH ];\r
     SKP_int16   pIn_HP[ MAX_FRAME_LENGTH ];\r
     SKP_int16   res_pitch[ 2 * MAX_FRAME_LENGTH + LA_PITCH_MAX ];\r
-    SKP_int     LBRR_idx, frame_terminator;\r
+    SKP_int     LBRR_idx, frame_terminator, SNR_dB_Q7;\r
     const SKP_uint16 *FrameTermination_CDF;\r
 \r
     /* Low bitrate redundancy parameters */\r
     SKP_uint8   LBRRpayload[ MAX_ARITHM_BYTES ];\r
     const SKP_uint16 *FrameTermination_CDF;\r
 \r
     /* Low bitrate redundancy parameters */\r
     SKP_uint8   LBRRpayload[ MAX_ARITHM_BYTES ];\r
-    SKP_int16   nBytesLBRR;\r
-    ec_byte_buffer range_enc_celt_buf;\r
-\r
-    sEncCtrl.sCmn.Seed = psEnc->sCmn.frameCounter++ & 3;\r
+    SKP_int32   nBytesLBRR;\r
 \r
 TIC(ENCODE_FRAME)\r
 \r
 \r
 TIC(ENCODE_FRAME)\r
 \r
+    sEncCtrl.sCmn.Seed = psEnc->sCmn.frameCounter++ & 3;\r
     /**************************************************************/\r
     /* Setup Input Pointers, and insert frame in input buffer    */\r
     /*************************************************************/\r
     /**************************************************************/\r
     /* Setup Input Pointers, and insert frame in input buffer    */\r
     /*************************************************************/\r
-    x_frame         = psEnc->x_buf + psEnc->sCmn.ltp_mem_length; /* start of frame to encode */\r
-    res_pitch_frame = res_pitch    + psEnc->sCmn.ltp_mem_length; /* start of pitch LPC residual frame */\r
+    /* pointers aligned with start of frame to encode */\r
+    x_frame         = psEnc->x_buf + psEnc->sCmn.ltp_mem_length;    /* start of frame to encode */\r
+    res_pitch_frame = res_pitch    + psEnc->sCmn.ltp_mem_length;    /* start of pitch LPC residual frame */\r
 \r
     /****************************/\r
     /* Voice Activity Detection */\r
     /****************************/\r
 TIC(VAD)\r
 \r
     /****************************/\r
     /* Voice Activity Detection */\r
     /****************************/\r
 TIC(VAD)\r
-    ret = SKP_Silk_VAD_GetSA_Q8( &psEnc->sCmn.sVAD, &psEnc->speech_activity_Q8, \r
+    ret = SKP_Silk_VAD_GetSA_Q8( &psEnc->sCmn.sVAD, &psEnc->speech_activity_Q8, &SNR_dB_Q7, \r
                                  sEncCtrl.input_quality_bands_Q15, &sEncCtrl.input_tilt_Q15,\r
                                  pIn, psEnc->sCmn.frame_length, psEnc->sCmn.fs_kHz );\r
 TOC(VAD)\r
                                  sEncCtrl.input_quality_bands_Q15, &sEncCtrl.input_tilt_Q15,\r
                                  pIn, psEnc->sCmn.frame_length, psEnc->sCmn.fs_kHz );\r
 TOC(VAD)\r
@@ -79,15 +79,15 @@ TIC(HP_IN)
     /* Variable high-pass filter */\r
     SKP_Silk_HP_variable_cutoff_FIX( psEnc, &sEncCtrl, pIn_HP, pIn );\r
 #else\r
     /* Variable high-pass filter */\r
     SKP_Silk_HP_variable_cutoff_FIX( psEnc, &sEncCtrl, pIn_HP, pIn );\r
 #else\r
-    SKP_memcpy( pIn_HP, pIn,psEnc->sCmn.frame_length * sizeof( SKP_int16 ) );\r
+    SKP_memcpy( pIn_HP, pIn, psEnc->sCmn.frame_length * sizeof( SKP_int16 ) );\r
 #endif\r
 TOC(HP_IN)\r
 \r
 #if SWITCH_TRANSITION_FILTERING\r
     /* Ensure smooth bandwidth transitions */\r
 #endif\r
 TOC(HP_IN)\r
 \r
 #if SWITCH_TRANSITION_FILTERING\r
     /* Ensure smooth bandwidth transitions */\r
-    SKP_Silk_LP_variable_cutoff( &psEnc->sCmn.sLP, x_frame + psEnc->sCmn.la_shape, pIn_HP, psEnc->sCmn.frame_length );\r
+    SKP_Silk_LP_variable_cutoff( &psEnc->sCmn.sLP, x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, pIn_HP, psEnc->sCmn.frame_length );\r
 #else\r
 #else\r
-    SKP_memcpy( x_frame + psEnc->sCmn.la_shape, pIn_HP,psEnc->sCmn.frame_length * sizeof( SKP_int16 ) );\r
+    SKP_memcpy( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, pIn_HP,psEnc->sCmn.frame_length * sizeof( SKP_int16 ) );\r
 #endif\r
     \r
     /*****************************************/\r
 #endif\r
     \r
     /*****************************************/\r
@@ -124,7 +124,7 @@ TOC(FIND_PRED_COEF)
 TIC(PROCESS_GAINS)\r
     SKP_Silk_process_gains_FIX( psEnc, &sEncCtrl );\r
 TOC(PROCESS_GAINS)\r
 TIC(PROCESS_GAINS)\r
     SKP_Silk_process_gains_FIX( psEnc, &sEncCtrl );\r
 TOC(PROCESS_GAINS)\r
\r
+    \r
     psEnc->sCmn.sigtype[         psEnc->sCmn.nFramesInPayloadBuf ] = sEncCtrl.sCmn.sigtype;\r
     psEnc->sCmn.QuantOffsetType[ psEnc->sCmn.nFramesInPayloadBuf ] = sEncCtrl.sCmn.QuantOffsetType;\r
 \r
     psEnc->sCmn.sigtype[         psEnc->sCmn.nFramesInPayloadBuf ] = sEncCtrl.sCmn.sigtype;\r
     psEnc->sCmn.QuantOffsetType[ psEnc->sCmn.nFramesInPayloadBuf ] = sEncCtrl.sCmn.QuantOffsetType;\r
 \r
@@ -140,17 +140,17 @@ TOC(LBRR)
     /* Noise shaping quantization            */\r
     /*****************************************/\r
 TIC(NSQ)\r
     /* Noise shaping quantization            */\r
     /*****************************************/\r
 TIC(NSQ)\r
-    if( psEnc->sCmn.nStatesDelayedDecision > 1 ) {\r
+    if( psEnc->sCmn.nStatesDelayedDecision > 1 || psEnc->sCmn.warping_Q16 > 0 ) {\r
         SKP_Silk_NSQ_del_dec( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sNSQ, xfw,\r
         SKP_Silk_NSQ_del_dec( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sNSQ, xfw,\r
-            &psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ], sEncCtrl.sCmn.NLSFInterpCoef_Q2, \r
+            psEnc->sCmn.q, sEncCtrl.sCmn.NLSFInterpCoef_Q2, \r
             sEncCtrl.PredCoef_Q12[ 0 ], sEncCtrl.LTPCoef_Q14, sEncCtrl.AR2_Q13, sEncCtrl.HarmShapeGain_Q14, \r
             sEncCtrl.Tilt_Q14, sEncCtrl.LF_shp_Q14, sEncCtrl.Gains_Q16, sEncCtrl.Lambda_Q10, \r
             sEncCtrl.LTP_scale_Q14 );\r
     } else {\r
         SKP_Silk_NSQ( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sNSQ, xfw, \r
             sEncCtrl.PredCoef_Q12[ 0 ], sEncCtrl.LTPCoef_Q14, sEncCtrl.AR2_Q13, sEncCtrl.HarmShapeGain_Q14, \r
             sEncCtrl.Tilt_Q14, sEncCtrl.LF_shp_Q14, sEncCtrl.Gains_Q16, sEncCtrl.Lambda_Q10, \r
             sEncCtrl.LTP_scale_Q14 );\r
     } else {\r
         SKP_Silk_NSQ( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sNSQ, xfw, \r
-               &psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ], sEncCtrl.sCmn.NLSFInterpCoef_Q2, \r
-               sEncCtrl.PredCoef_Q12[ 0 ], sEncCtrl.LTPCoef_Q14, sEncCtrl.AR2_Q13, sEncCtrl.HarmShapeGain_Q14, \r
-               sEncCtrl.Tilt_Q14, sEncCtrl.LF_shp_Q14, sEncCtrl.Gains_Q16, sEncCtrl.Lambda_Q10, \r
+            psEnc->sCmn.q, sEncCtrl.sCmn.NLSFInterpCoef_Q2, \r
+            sEncCtrl.PredCoef_Q12[ 0 ], sEncCtrl.LTPCoef_Q14, sEncCtrl.AR2_Q13, sEncCtrl.HarmShapeGain_Q14, \r
+            sEncCtrl.Tilt_Q14, sEncCtrl.LF_shp_Q14, sEncCtrl.Gains_Q16, sEncCtrl.Lambda_Q10, \r
             sEncCtrl.LTP_scale_Q14 );\r
     }\r
 TOC(NSQ)\r
             sEncCtrl.LTP_scale_Q14 );\r
     }\r
 TOC(NSQ)\r
@@ -158,7 +158,7 @@ TOC(NSQ)
     /**************************************************/\r
     /* Convert speech activity into VAD and DTX flags */\r
     /**************************************************/\r
     /**************************************************/\r
     /* Convert speech activity into VAD and DTX flags */\r
     /**************************************************/\r
-    if( psEnc->speech_activity_Q8 < SPEECH_ACTIVITY_DTX_THRES_Q8 ) {\r
+    if( psEnc->speech_activity_Q8 < SKP_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ) ) {\r
         psEnc->sCmn.vadFlag = NO_VOICE_ACTIVITY;\r
         psEnc->sCmn.noSpeechCounter++;\r
         if( psEnc->sCmn.noSpeechCounter > NO_SPEECH_FRAMES_BEFORE_DTX ) {\r
         psEnc->sCmn.vadFlag = NO_VOICE_ACTIVITY;\r
         psEnc->sCmn.noSpeechCounter++;\r
         if( psEnc->sCmn.noSpeechCounter > NO_SPEECH_FRAMES_BEFORE_DTX ) {\r
@@ -175,13 +175,9 @@ TOC(NSQ)
     }\r
 \r
     /****************************************/\r
     }\r
 \r
     /****************************************/\r
-    /* Initialize arithmetic coder          */\r
+    /* Initialize range coder               */\r
     /****************************************/\r
     if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) {\r
     /****************************************/\r
     if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) {\r
-        ec_byte_writeinit_buffer( &range_enc_celt_buf, psEnc->sCmn.sRC.buffer, MAX_ARITHM_BYTES );\r
-        ec_enc_init( &psEnc->sCmn.sRC.range_enc_celt_state, &range_enc_celt_buf );\r
-\r
-        SKP_Silk_range_enc_init( &psEnc->sCmn.sRC );\r
         psEnc->sCmn.nBytesInPayloadBuf = 0;\r
     }\r
 \r
         psEnc->sCmn.nBytesInPayloadBuf = 0;\r
     }\r
 \r
@@ -189,62 +185,60 @@ TOC(NSQ)
     /* Encode Parameters                    */\r
     /****************************************/\r
 TIC(ENCODE_PARAMS)\r
     /* Encode Parameters                    */\r
     /****************************************/\r
 TIC(ENCODE_PARAMS)\r
-    SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sCmn.sRC );\r
-    FrameTermination_CDF = SKP_Silk_FrameTermination_v4_CDF;\r
+    SKP_Silk_encode_parameters( &psEnc->sCmn, &sEncCtrl.sCmn, psRangeEnc );\r
+    FrameTermination_CDF = SKP_Silk_FrameTermination_CDF;\r
 TOC(ENCODE_PARAMS)\r
 \r
     /****************************************/\r
     /* Update Buffers and State             */\r
     /****************************************/\r
 TOC(ENCODE_PARAMS)\r
 \r
     /****************************************/\r
     /* Update Buffers and State             */\r
     /****************************************/\r
-    /* Update Input buffer */\r
-    SKP_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ], ( psEnc->sCmn.ltp_mem_length + psEnc->sCmn.la_shape ) * sizeof( SKP_int16 ) );\r
+    /* Update input buffer */\r
+    SKP_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ], \r
+        ( psEnc->sCmn.ltp_mem_length + LA_SHAPE_MS * psEnc->sCmn.fs_kHz ) * sizeof( SKP_int16 ) );\r
     \r
     \r
-    /* parameters needed for next frame */\r
+    /* Parameters needed for next frame */\r
     psEnc->sCmn.prev_sigtype            = sEncCtrl.sCmn.sigtype;\r
     psEnc->sCmn.prev_sigtype            = sEncCtrl.sCmn.sigtype;\r
-    psEnc->sCmn.prevLag                 = sEncCtrl.sCmn.pitchL[  psEnc->sCmn.nb_subfr - 1 ];\r
+    psEnc->sCmn.prevLag                 = sEncCtrl.sCmn.pitchL[ psEnc->sCmn.nb_subfr - 1 ];\r
     psEnc->sCmn.first_frame_after_reset = 0;\r
 \r
     psEnc->sCmn.first_frame_after_reset = 0;\r
 \r
-    if( psEnc->sCmn.sRC.error ) {\r
-        /* encoder returned error: clear payload buffer */\r
+    if( 0 ) { //psEnc->sCmn.sRC.error ) {\r
+        /* Encoder returned error: clear payload buffer */\r
         psEnc->sCmn.nFramesInPayloadBuf = 0;\r
     } else {\r
         psEnc->sCmn.nFramesInPayloadBuf++;\r
     }\r
 \r
     /****************************************/\r
         psEnc->sCmn.nFramesInPayloadBuf = 0;\r
     } else {\r
         psEnc->sCmn.nFramesInPayloadBuf++;\r
     }\r
 \r
     /****************************************/\r
-    /* finalize payload and copy to output  */\r
+    /* Finalize payload and copy to output  */\r
     /****************************************/\r
     if( psEnc->sCmn.nFramesInPayloadBuf * SUB_FRAME_LENGTH_MS * psEnc->sCmn.nb_subfr >= psEnc->sCmn.PacketSize_ms ) {\r
 \r
         LBRR_idx = ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK;\r
 \r
         /* Check if FEC information should be added */\r
     /****************************************/\r
     if( psEnc->sCmn.nFramesInPayloadBuf * SUB_FRAME_LENGTH_MS * psEnc->sCmn.nb_subfr >= psEnc->sCmn.PacketSize_ms ) {\r
 \r
         LBRR_idx = ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK;\r
 \r
         /* Check if FEC information should be added */\r
-        frame_terminator = SKP_SILK_LAST_FRAME;\r
-        if( psEnc->sCmn.LBRR_buffer[ LBRR_idx ].usage == SKP_SILK_ADD_LBRR_TO_PLUS1 ) {\r
-            frame_terminator = SKP_SILK_LBRR_VER1;\r
-        }\r
-        if( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage == SKP_SILK_ADD_LBRR_TO_PLUS2 ) {\r
-            frame_terminator = SKP_SILK_LBRR_VER2;\r
-            LBRR_idx = psEnc->sCmn.oldest_LBRR_idx;\r
-        }\r
+        //frame_terminator = psEnc->sCmn.LBRR_buffer[ LBRR_idx ].usage;\r
+        frame_terminator = SKP_SILK_NO_LBRR;\r
 \r
         /* Add the frame termination info to stream */\r
 \r
         /* Add the frame termination info to stream */\r
-        SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF );\r
+        ec_encode_bin( psRangeEnc, FrameTermination_CDF[ frame_terminator ], \r
+            FrameTermination_CDF[ frame_terminator + 1 ], 16 );\r
 \r
         /* Code excitation signal */\r
 \r
         /* Code excitation signal */\r
-        for( i = 0; i <psEnc->sCmn.nFramesInPayloadBuf; i++ ) {\r
-            SKP_Silk_encode_pulses( &psEnc->sCmn.sRC, psEnc->sCmn.sigtype[ i ],psEnc->sCmn.QuantOffsetType[ i ], \r
-                &psEnc->sCmn.q[ i * psEnc->sCmn.frame_length],psEnc->sCmn.frame_length );\r
+        for( i = 0; i < psEnc->sCmn.nFramesInPayloadBuf; i++ ) {\r
+            SKP_Silk_encode_pulses( psRangeEnc, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ], \r
+                &psEnc->sCmn.q[ i * psEnc->sCmn.frame_length ], psEnc->sCmn.frame_length );\r
         }\r
         }\r
-        /* payload length so far */\r
-        SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC, &nBytes );\r
 \r
 \r
-        /* check that there is enough space in external output buffer, and move data */\r
+        /* Payload length so far */\r
+        nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );\r
+\r
+        /* Check that there is enough space in external output buffer, and move data */\r
         if( *pnBytesOut >= nBytes ) {\r
         if( *pnBytesOut >= nBytes ) {\r
-            SKP_int bits_in_stream, mask;\r
-            bits_in_stream = ec_enc_tell( &psEnc->sCmn.sRC.range_enc_celt_state, 0 );\r
-            ec_enc_done( &psEnc->sCmn.sRC.range_enc_celt_state );\r
+            //SKP_int bits_in_stream, mask;\r
+            //bits_in_stream = ec_enc_tell( psRangeEnc, 0 );\r
+            //ec_enc_done( psRangeEnc );\r
             \r
             \r
+#if 0\r
             /* Fill up any remaining bits in the last byte with 1s */\r
             if( bits_in_stream & 7 ) {\r
                 mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 );\r
             /* Fill up any remaining bits in the last byte with 1s */\r
             if( bits_in_stream & 7 ) {\r
                 mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 );\r
@@ -253,7 +247,9 @@ TOC(ENCODE_PARAMS)
                 }\r
             }\r
             SKP_memcpy( pCode, psEnc->sCmn.sRC.range_enc_celt_state.buf->buf, nBytes * sizeof( SKP_uint8 ) );\r
                 }\r
             }\r
             SKP_memcpy( pCode, psEnc->sCmn.sRC.range_enc_celt_state.buf->buf, nBytes * sizeof( SKP_uint8 ) );\r
+#endif\r
 \r
 \r
+#if 0\r
             if( frame_terminator > SKP_SILK_MORE_FRAMES && \r
                     *pnBytesOut >= nBytes + psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes ) {\r
                 /* Get old packet and add to payload. */\r
             if( frame_terminator > SKP_SILK_MORE_FRAMES && \r
                     *pnBytesOut >= nBytes + psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes ) {\r
                 /* Get old packet and add to payload. */\r
@@ -262,57 +258,53 @@ TOC(ENCODE_PARAMS)
                     psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes * sizeof( SKP_uint8 ) );\r
                 nBytes += psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes;\r
             }\r
                     psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes * sizeof( SKP_uint8 ) );\r
                 nBytes += psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes;\r
             }\r
-                        \r
+#endif\r
             *pnBytesOut = nBytes;\r
             *pnBytesOut = nBytes;\r
-        \r
+\r
             /* Update FEC buffer */\r
             SKP_memcpy( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].payload, LBRRpayload, \r
                 nBytesLBRR * sizeof( SKP_uint8 ) );\r
             psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].nBytes = nBytesLBRR;\r
             /* Update FEC buffer */\r
             SKP_memcpy( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].payload, LBRRpayload, \r
                 nBytesLBRR * sizeof( SKP_uint8 ) );\r
             psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].nBytes = nBytesLBRR;\r
-            /* This line tells describes how FEC should be used */\r
+            /* The line below describes how FEC should be used */\r
             psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage = sEncCtrl.sCmn.LBRR_usage;\r
             psEnc->sCmn.oldest_LBRR_idx = ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK;\r
 \r
             psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage = sEncCtrl.sCmn.LBRR_usage;\r
             psEnc->sCmn.oldest_LBRR_idx = ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK;\r
 \r
-            /* Reset number of frames in payload buffer */\r
-            psEnc->sCmn.nFramesInPayloadBuf = 0;\r
         } else {\r
             /* Not enough space: Payload will be discarded */\r
             *pnBytesOut = 0;\r
             nBytes      = 0;\r
         } else {\r
             /* Not enough space: Payload will be discarded */\r
             *pnBytesOut = 0;\r
             nBytes      = 0;\r
-            psEnc->sCmn.nFramesInPayloadBuf = 0;\r
             ret = SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT;\r
         }\r
             ret = SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT;\r
         }\r
+\r
+        /* Reset the number of frames in payload buffer */\r
+        psEnc->sCmn.nFramesInPayloadBuf = 0;\r
     } else {\r
     } else {\r
-        /* no payload for you this time */\r
+        /* No payload this time */\r
         *pnBytesOut = 0;\r
 \r
         *pnBytesOut = 0;\r
 \r
-        /* Encode that more frames follows */\r
-        frame_terminator = SKP_SILK_MORE_FRAMES;\r
-        SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF );\r
+        /* Payload length so far */\r
+        nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );\r
 \r
 \r
-        /* payload length so far */\r
-        SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC, &nBytes );\r
-\r
-        /* Take into account the q signal that isnt in the bitstream yet */\r
-        nBytes +=  SKP_Silk_pulses_to_bytes( &psEnc->sCmn, \r
-            &psEnc->sCmn.q[ (psEnc->sCmn.nFramesInPayloadBuf - 1) * psEnc->sCmn.frame_length ] );\r
+        /* Take into account the q signal that isn't in the bitstream yet */\r
+        nBytes += SKP_Silk_pulses_to_bytes( &psEnc->sCmn, \r
+            &psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length ] );\r
     }\r
 \r
     /* Check for arithmetic coder errors */\r
     }\r
 \r
     /* Check for arithmetic coder errors */\r
-    if( psEnc->sCmn.sRC.error ) {\r
+    if( 0 ) { //psEnc->sCmn.sRC.error ) {\r
         ret = SKP_SILK_ENC_INTERNAL_ERROR;\r
     }\r
 \r
         ret = SKP_SILK_ENC_INTERNAL_ERROR;\r
     }\r
 \r
-    /* simulate number of ms buffered in channel because of exceeding TargetRate */\r
+    /* Simulate number of ms buffered in channel because of exceeding TargetRate */\r
     SKP_assert(  ( 8 * 1000 * ( (SKP_int64)nBytes - (SKP_int64)psEnc->sCmn.nBytesInPayloadBuf ) ) == \r
         SKP_SAT32( 8 * 1000 * ( (SKP_int64)nBytes - (SKP_int64)psEnc->sCmn.nBytesInPayloadBuf ) ) );\r
     SKP_assert( psEnc->sCmn.TargetRate_bps > 0 );\r
     SKP_assert(  ( 8 * 1000 * ( (SKP_int64)nBytes - (SKP_int64)psEnc->sCmn.nBytesInPayloadBuf ) ) == \r
         SKP_SAT32( 8 * 1000 * ( (SKP_int64)nBytes - (SKP_int64)psEnc->sCmn.nBytesInPayloadBuf ) ) );\r
     SKP_assert( psEnc->sCmn.TargetRate_bps > 0 );\r
-    psEnc->BufferedInChannel_ms   += SKP_DIV32( 8 * 1000 * ( nBytes -psEnc->sCmn.nBytesInPayloadBuf ),psEnc->sCmn.TargetRate_bps );\r
+    psEnc->BufferedInChannel_ms   += SKP_DIV32( 8 * 1000 * ( nBytes - psEnc->sCmn.nBytesInPayloadBuf ), psEnc->sCmn.TargetRate_bps );\r
     psEnc->BufferedInChannel_ms   -= SKP_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr );\r
     psEnc->BufferedInChannel_ms    = SKP_LIMIT_int( psEnc->BufferedInChannel_ms, 0, 100 );\r
     psEnc->sCmn.nBytesInPayloadBuf = nBytes;\r
 \r
     psEnc->BufferedInChannel_ms   -= SKP_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr );\r
     psEnc->BufferedInChannel_ms    = SKP_LIMIT_int( psEnc->BufferedInChannel_ms, 0, 100 );\r
     psEnc->sCmn.nBytesInPayloadBuf = nBytes;\r
 \r
-    if( psEnc->speech_activity_Q8 > WB_DETECT_ACTIVE_SPEECH_LEVEL_THRES_Q8 ) {\r
+    if( psEnc->speech_activity_Q8 > SKP_FIX_CONST( WB_DETECT_ACTIVE_SPEECH_LEVEL_THRES, 8 ) ) {\r
         psEnc->sCmn.sSWBdetect.ActiveSpeech_ms = SKP_ADD_POS_SAT32( psEnc->sCmn.sSWBdetect.ActiveSpeech_ms, SKP_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr ) ); \r
     }\r
 \r
         psEnc->sCmn.sSWBdetect.ActiveSpeech_ms = SKP_ADD_POS_SAT32( psEnc->sCmn.sSWBdetect.ActiveSpeech_ms, SKP_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr ) ); \r
     }\r
 \r
@@ -322,7 +314,7 @@ TOC(ENCODE_FRAME)
     {\r
         SKP_float tmp[ MAX_NB_SUBFR * LTP_ORDER ];\r
         int i;\r
     {\r
         SKP_float tmp[ MAX_NB_SUBFR * LTP_ORDER ];\r
         int i;\r
-        DEBUG_STORE_DATA( xf.dat,                   x_frame +psEnc->sCmn.la_shape,  psEnc->sCmn.frame_length    * sizeof( SKP_int16 ) );\r
+        DEBUG_STORE_DATA( xf.dat,                   x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.frame_length    * sizeof( SKP_int16 ) );\r
         DEBUG_STORE_DATA( xfw.dat,                  xfw,                            psEnc->sCmn.frame_length    * sizeof( SKP_int16 ) );\r
         //     DEBUG_STORE_DATA( q.dat,                    &psEnc->sCmn.q[ ( psEnc->sCmn.nFramesInPayloadBuf - 1)*psEnc->sCmn.frame_length ],  psEnc->sCmn.frame_length    * sizeof( SKP_int8 ) );\r
         DEBUG_STORE_DATA( pitchL.dat,               sEncCtrl.sCmn.pitchL,           psEnc->sCmn.nb_subfr            * sizeof( SKP_int ) );\r
         DEBUG_STORE_DATA( xfw.dat,                  xfw,                            psEnc->sCmn.frame_length    * sizeof( SKP_int16 ) );\r
         //     DEBUG_STORE_DATA( q.dat,                    &psEnc->sCmn.q[ ( psEnc->sCmn.nFramesInPayloadBuf - 1)*psEnc->sCmn.frame_length ],  psEnc->sCmn.frame_length    * sizeof( SKP_int8 ) );\r
         DEBUG_STORE_DATA( pitchL.dat,               sEncCtrl.sCmn.pitchL,           psEnc->sCmn.nb_subfr            * sizeof( SKP_int ) );\r
@@ -368,6 +360,7 @@ TOC(ENCODE_FRAME)
     return( ret );\r
 }\r
 \r
     return( ret );\r
 }\r
 \r
+#if 0  //tmp\r
 /* Low BitRate Redundancy encoding functionality. Reuse all parameters but encode residual with lower bitrate */\r
 void SKP_Silk_LBRR_encode_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Pointer to Silk encoder state           */\r
 /* Low BitRate Redundancy encoding functionality. Reuse all parameters but encode residual with lower bitrate */\r
 void SKP_Silk_LBRR_encode_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Pointer to Silk encoder state           */\r
@@ -386,10 +379,10 @@ void SKP_Silk_LBRR_encode_FIX(
     /*******************************************/\r
     /* Control use of inband LBRR              */\r
     /*******************************************/\r
     /*******************************************/\r
     /* Control use of inband LBRR              */\r
     /*******************************************/\r
-    SKP_Silk_LBRR_ctrl_FIX( psEnc, psEncCtrl );\r
+    SKP_Silk_LBRR_ctrl_FIX( psEnc, &psEncCtrl->sCmn );\r
 \r
     if( psEnc->sCmn.LBRR_enabled ) {\r
 \r
     if( psEnc->sCmn.LBRR_enabled ) {\r
-        /* Save original Gains */\r
+        /* Save original gains */\r
         SKP_memcpy( TempGainsIndices, psEncCtrl->sCmn.GainsIndices, MAX_NB_SUBFR * sizeof( SKP_int   ) );\r
         SKP_memcpy( TempGains_Q16,    psEncCtrl->Gains_Q16,         MAX_NB_SUBFR * sizeof( SKP_int32 ) );\r
 \r
         SKP_memcpy( TempGainsIndices, psEncCtrl->sCmn.GainsIndices, MAX_NB_SUBFR * sizeof( SKP_int   ) );\r
         SKP_memcpy( TempGains_Q16,    psEncCtrl->Gains_Q16,         MAX_NB_SUBFR * sizeof( SKP_int32 ) );\r
 \r
@@ -411,37 +404,35 @@ void SKP_Silk_LBRR_encode_FIX(
 \r
         if( psEnc->sCmn.Complexity > 0 && psEnc->sCmn.TargetRate_bps > Rate_only_parameters ) {\r
             if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) {\r
 \r
         if( psEnc->sCmn.Complexity > 0 && psEnc->sCmn.TargetRate_bps > Rate_only_parameters ) {\r
             if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) {\r
-                /* First frame in packet copy Everything */\r
+                /* First frame in packet; copy everything */\r
                 SKP_memcpy( &psEnc->sNSQ_LBRR, &psEnc->sNSQ, sizeof( SKP_Silk_nsq_state ) );\r
                 SKP_memcpy( &psEnc->sNSQ_LBRR, &psEnc->sNSQ, sizeof( SKP_Silk_nsq_state ) );\r
-            \r
+\r
                 psEnc->sCmn.LBRRprevLastGainIndex = psEnc->sShape.LastGainIndex;\r
                 /* Increase Gains to get target LBRR rate */\r
                 psEncCtrl->sCmn.GainsIndices[ 0 ] = psEncCtrl->sCmn.GainsIndices[ 0 ] + psEnc->sCmn.LBRR_GainIncreases;\r
                 psEncCtrl->sCmn.GainsIndices[ 0 ] = SKP_LIMIT_int( psEncCtrl->sCmn.GainsIndices[ 0 ], 0, N_LEVELS_QGAIN - 1 );\r
             }\r
                 psEnc->sCmn.LBRRprevLastGainIndex = psEnc->sShape.LastGainIndex;\r
                 /* Increase Gains to get target LBRR rate */\r
                 psEncCtrl->sCmn.GainsIndices[ 0 ] = psEncCtrl->sCmn.GainsIndices[ 0 ] + psEnc->sCmn.LBRR_GainIncreases;\r
                 psEncCtrl->sCmn.GainsIndices[ 0 ] = SKP_LIMIT_int( psEncCtrl->sCmn.GainsIndices[ 0 ], 0, N_LEVELS_QGAIN - 1 );\r
             }\r
-            /* Decode to get Gains in sync with decoder         */\r
+            /* Decode to get gains in sync with decoder         */\r
             /* Overwrite unquantized gains with quantized gains */\r
             SKP_Silk_gains_dequant( psEncCtrl->Gains_Q16, psEncCtrl->sCmn.GainsIndices, \r
                 &psEnc->sCmn.LBRRprevLastGainIndex, psEnc->sCmn.nFramesInPayloadBuf, psEnc->sCmn.nb_subfr );\r
             /* Overwrite unquantized gains with quantized gains */\r
             SKP_Silk_gains_dequant( psEncCtrl->Gains_Q16, psEncCtrl->sCmn.GainsIndices, \r
                 &psEnc->sCmn.LBRRprevLastGainIndex, psEnc->sCmn.nFramesInPayloadBuf, psEnc->sCmn.nb_subfr );\r
+\r
             /*****************************************/\r
             /* Noise shaping quantization            */\r
             /*****************************************/\r
             /*****************************************/\r
             /* Noise shaping quantization            */\r
             /*****************************************/\r
-            if( psEnc->sCmn.nStatesDelayedDecision > 1 ) {\r
-                SKP_Silk_NSQ_del_dec( &psEnc->sCmn, &psEncCtrl->sCmn,\r
-                    &psEnc->sNSQ_LBRR, xfw, &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length ], \r
+            if( psEnc->sCmn.nStatesDelayedDecision > 1 || psEnc->sCmn.warping_Q16 > 0 ) {\r
+                SKP_Silk_NSQ_del_dec( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sNSQ_LBRR, xfw, psEnc->sCmn.q_LBRR, \r
                     psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->LTPCoef_Q14, \r
                     psEncCtrl->AR2_Q13, psEncCtrl->HarmShapeGain_Q14, psEncCtrl->Tilt_Q14, psEncCtrl->LF_shp_Q14, \r
                     psEncCtrl->Gains_Q16, psEncCtrl->Lambda_Q10, psEncCtrl->LTP_scale_Q14 );\r
             } else {\r
                     psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->LTPCoef_Q14, \r
                     psEncCtrl->AR2_Q13, psEncCtrl->HarmShapeGain_Q14, psEncCtrl->Tilt_Q14, psEncCtrl->LF_shp_Q14, \r
                     psEncCtrl->Gains_Q16, psEncCtrl->Lambda_Q10, psEncCtrl->LTP_scale_Q14 );\r
             } else {\r
-                SKP_Silk_NSQ( &psEnc->sCmn, &psEncCtrl->sCmn,\r
-                    &psEnc->sNSQ_LBRR, xfw, &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length ], \r
+                SKP_Silk_NSQ( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sNSQ_LBRR, xfw, psEnc->sCmn.q_LBRR, \r
                     psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->LTPCoef_Q14, \r
                     psEncCtrl->AR2_Q13, psEncCtrl->HarmShapeGain_Q14, psEncCtrl->Tilt_Q14, psEncCtrl->LF_shp_Q14, \r
                     psEncCtrl->Gains_Q16, psEncCtrl->Lambda_Q10, psEncCtrl->LTP_scale_Q14 );\r
             }\r
         } else {\r
                     psEncCtrl->sCmn.NLSFInterpCoef_Q2, psEncCtrl->PredCoef_Q12[ 0 ], psEncCtrl->LTPCoef_Q14, \r
                     psEncCtrl->AR2_Q13, psEncCtrl->HarmShapeGain_Q14, psEncCtrl->Tilt_Q14, psEncCtrl->LF_shp_Q14, \r
                     psEncCtrl->Gains_Q16, psEncCtrl->Lambda_Q10, psEncCtrl->LTP_scale_Q14 );\r
             }\r
         } else {\r
-            SKP_memset( &psEnc->sCmn.q_LBRR[ psEnc->sCmn.nFramesInPayloadBuf *psEnc->sCmn.frame_length ], 0,\r
-                psEnc->sCmn.frame_length * sizeof( SKP_int ) );\r
+            SKP_memset( psEnc->sCmn.q_LBRR, 0, psEnc->sCmn.frame_length * sizeof( SKP_int8 ) );\r
             psEncCtrl->sCmn.LTP_scaleIndex = 0;\r
         }\r
         /****************************************/\r
             psEncCtrl->sCmn.LTP_scaleIndex = 0;\r
         }\r
         /****************************************/\r
@@ -458,17 +449,21 @@ void SKP_Silk_LBRR_encode_FIX(
         /****************************************/\r
         /* Encode Parameters                    */\r
         /****************************************/\r
         /****************************************/\r
         /* Encode Parameters                    */\r
         /****************************************/\r
-        SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR );\r
+        SKP_Silk_encode_parameters( &psEnc->sCmn, &psEncCtrl->sCmn, \r
+            &psEnc->sCmn.sRC_LBRR );\r
 \r
 \r
+        /****************************************/\r
+        /* Encode Parameters                    */\r
+        /****************************************/\r
         if( psEnc->sCmn.sRC_LBRR.error ) {\r
         if( psEnc->sCmn.sRC_LBRR.error ) {\r
-            /* encoder returned error: clear payload buffer */\r
+            /* Encoder returned error: clear payload buffer */\r
             nFramesInPayloadBuf = 0;\r
         } else {\r
             nFramesInPayloadBuf = psEnc->sCmn.nFramesInPayloadBuf + 1;\r
         }\r
 \r
         /****************************************/\r
             nFramesInPayloadBuf = 0;\r
         } else {\r
             nFramesInPayloadBuf = psEnc->sCmn.nFramesInPayloadBuf + 1;\r
         }\r
 \r
         /****************************************/\r
-        /* finalize payload and copy to output  */\r
+        /* Finalize payload and copy to output  */\r
         /****************************************/\r
         if( SKP_SMULBB( nFramesInPayloadBuf, SKP_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr ) ) >= psEnc->sCmn.PacketSize_ms ) {\r
 \r
         /****************************************/\r
         if( SKP_SMULBB( nFramesInPayloadBuf, SKP_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr ) ) >= psEnc->sCmn.PacketSize_ms ) {\r
 \r
@@ -476,8 +471,9 @@ void SKP_Silk_LBRR_encode_FIX(
             frame_terminator = SKP_SILK_LAST_FRAME;\r
 \r
             /* Add the frame termination info to stream */\r
             frame_terminator = SKP_SILK_LAST_FRAME;\r
 \r
             /* Add the frame termination info to stream */\r
-            SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_v4_CDF );\r
-            \r
+            ec_encode_bin( psRangeEnc_LBRR, FrameTermination_CDF[ frame_terminator ], \r
+                FrameTermination_CDF[ frame_terminator + 1 ], 16 );\r
+\r
             /*********************************************/\r
             /* Encode quantization indices of excitation */\r
             /*********************************************/\r
             /*********************************************/\r
             /* Encode quantization indices of excitation */\r
             /*********************************************/\r
@@ -485,10 +481,11 @@ void SKP_Silk_LBRR_encode_FIX(
                 SKP_Silk_encode_pulses( &psEnc->sCmn.sRC_LBRR, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ],\r
                     &psEnc->sCmn.q_LBRR[ i * psEnc->sCmn.frame_length ], psEnc->sCmn.frame_length );\r
             }\r
                 SKP_Silk_encode_pulses( &psEnc->sCmn.sRC_LBRR, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ],\r
                     &psEnc->sCmn.q_LBRR[ i * psEnc->sCmn.frame_length ], psEnc->sCmn.frame_length );\r
             }\r
-            /* payload length so far */\r
-            SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC_LBRR, &nBytes );\r
 \r
 \r
-            /* check that there is enough space in external output buffer, and move data */\r
+            /* Payload length so far */\r
+            nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc_LBRR, 0 ) + 7, 3 );\r
+\r
+            /* Check that there is enough space in external output buffer and move data */\r
             if( *pnBytesOut >= nBytes ) {\r
                 SKP_int bits_in_stream, mask;\r
                 bits_in_stream = ec_enc_tell( &psEnc->sCmn.sRC_LBRR.range_enc_celt_state, 0 );\r
             if( *pnBytesOut >= nBytes ) {\r
                 SKP_int bits_in_stream, mask;\r
                 bits_in_stream = ec_enc_tell( &psEnc->sCmn.sRC_LBRR.range_enc_celt_state, 0 );\r
@@ -502,28 +499,30 @@ void SKP_Silk_LBRR_encode_FIX(
                     }\r
                 }\r
                 SKP_memcpy( pCode, psEnc->sCmn.sRC_LBRR.range_enc_celt_state.buf->buf, nBytes * sizeof( SKP_uint8 ) );\r
                     }\r
                 }\r
                 SKP_memcpy( pCode, psEnc->sCmn.sRC_LBRR.range_enc_celt_state.buf->buf, nBytes * sizeof( SKP_uint8 ) );\r
-//#endif\r
+\r
                 *pnBytesOut = nBytes;\r
             } else {\r
                 *pnBytesOut = nBytes;\r
             } else {\r
-                /* not enough space: payload will be discarded */\r
+                /* Not enough space: payload will be discarded */\r
                 *pnBytesOut = 0;\r
                 SKP_assert( 0 );\r
             }\r
         } else {\r
                 *pnBytesOut = 0;\r
                 SKP_assert( 0 );\r
             }\r
         } else {\r
-            /* no payload for you this time */\r
+            /* No payload this time */\r
             *pnBytesOut = 0;\r
 \r
             /* Encode that more frames follows */\r
             frame_terminator = SKP_SILK_MORE_FRAMES;\r
             *pnBytesOut = 0;\r
 \r
             /* Encode that more frames follows */\r
             frame_terminator = SKP_SILK_MORE_FRAMES;\r
-            SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_v4_CDF );\r
+            ec_encode_bin( psRangeEnc_LBRR, FrameTermination_CDF[ frame_terminator ], \r
+                FrameTermination_CDF[ frame_terminator + 1 ], 16 );\r
         }\r
 \r
         /* Restore original Gains */\r
         }\r
 \r
         /* Restore original Gains */\r
-        SKP_memcpy( psEncCtrl->sCmn.GainsIndices, TempGainsIndices, MAX_NB_SUBFR * sizeof( SKP_int   ) );\r
-        SKP_memcpy( psEncCtrl->Gains_Q16,         TempGains_Q16,    MAX_NB_SUBFR * sizeof( SKP_int32 ) );\r
+        SKP_memcpy( psEncCtrl->sCmn.GainsIndices, TempGainsIndices, psEnc->sCmn.nb_subfr * sizeof( SKP_int   ) );\r
+        SKP_memcpy( psEncCtrl->Gains_Q16,         TempGains_Q16,    psEnc->sCmn.nb_subfr * sizeof( SKP_int32 ) );\r
     \r
         /* Restore LTP scale index and typeoffset */\r
         psEncCtrl->sCmn.LTP_scaleIndex = LTP_scaleIndex;\r
     \r
         /* Restore LTP scale index and typeoffset */\r
         psEncCtrl->sCmn.LTP_scaleIndex = LTP_scaleIndex;\r
-        psEnc->sCmn.typeOffsetPrev = typeOffset;\r
+        psEnc->sCmn.typeOffsetPrev     = typeOffset;\r
     }\r
 }\r
     }\r
 }\r
+#endif\r
index fbf2608..450ec1e 100644 (file)
@@ -26,6 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 /* Finds LPC vector from correlations, and converts to NLSF */\r
 void SKP_Silk_find_LPC_FIX(\r
 \r
 /* Finds LPC vector from correlations, and converts to NLSF */\r
 void SKP_Silk_find_LPC_FIX(\r
@@ -41,7 +42,6 @@ void SKP_Silk_find_LPC_FIX(
 {\r
     SKP_int     k;\r
     SKP_int32   a_Q16[ MAX_LPC_ORDER ];\r
 {\r
     SKP_int     k;\r
     SKP_int32   a_Q16[ MAX_LPC_ORDER ];\r
-\r
     SKP_int     isInterpLower, shift;\r
     SKP_int16   S[ MAX_LPC_ORDER ];\r
     SKP_int32   res_nrg0, res_nrg1;\r
     SKP_int     isInterpLower, shift;\r
     SKP_int16   S[ MAX_LPC_ORDER ];\r
     SKP_int32   res_nrg0, res_nrg1;\r
@@ -58,13 +58,17 @@ void SKP_Silk_find_LPC_FIX(
     *interpIndex = 4;\r
 \r
     /* Burg AR analysis for the full frame */\r
     *interpIndex = 4;\r
 \r
     /* Burg AR analysis for the full frame */\r
-    SKP_Silk_burg_modified( &res_nrg, &res_nrg_Q, a_Q16, x, subfr_length, nb_subfr, FIND_LPC_COND_FAC_Q32, LPC_order );\r
+    SKP_Silk_burg_modified( &res_nrg, &res_nrg_Q, a_Q16, x, subfr_length, nb_subfr, SKP_FIX_CONST( FIND_LPC_COND_FAC, 32 ), LPC_order );\r
+\r
+    SKP_Silk_bwexpander_32( a_Q16, LPC_order, SKP_FIX_CONST( FIND_LPC_CHIRP, 16 ) );\r
 \r
     if( useInterpolatedNLSFs == 1 && nb_subfr == MAX_NB_SUBFR ) {\r
 \r
         /* Optimal solution for last 10 ms */\r
         SKP_Silk_burg_modified( &res_tmp_nrg, &res_tmp_nrg_Q, a_tmp_Q16, x + ( MAX_NB_SUBFR >> 1 ) * subfr_length, \r
 \r
     if( useInterpolatedNLSFs == 1 && nb_subfr == MAX_NB_SUBFR ) {\r
 \r
         /* Optimal solution for last 10 ms */\r
         SKP_Silk_burg_modified( &res_tmp_nrg, &res_tmp_nrg_Q, a_tmp_Q16, x + ( MAX_NB_SUBFR >> 1 ) * subfr_length, \r
-            subfr_length, ( MAX_NB_SUBFR >> 1 ), FIND_LPC_COND_FAC_Q32, LPC_order );\r
+            subfr_length, ( MAX_NB_SUBFR >> 1 ), SKP_FIX_CONST( FIND_LPC_COND_FAC, 32 ), LPC_order );\r
+\r
+        SKP_Silk_bwexpander_32( a_tmp_Q16, LPC_order, SKP_FIX_CONST( FIND_LPC_CHIRP, 16 ) );\r
 \r
         /* subtract residual energy here, as that's easier than adding it to the    */\r
         /* residual energy of the first 10 ms in each iteration of the search below */\r
 \r
         /* subtract residual energy here, as that's easier than adding it to the    */\r
         /* residual energy of the first 10 ms in each iteration of the search below */\r
index 2f0881e..cddc29a 100644 (file)
@@ -26,6 +26,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
+\r
+/* Head room for correlations                           */\r
+#define LTP_CORRS_HEAD_ROOM                             2\r
 \r
 void SKP_Silk_fit_LTP(\r
     SKP_int32 LTP_coefs_Q16[ LTP_ORDER ],\r
 \r
 void SKP_Silk_fit_LTP(\r
     SKP_int32 LTP_coefs_Q16[ LTP_ORDER ],\r
@@ -68,22 +72,24 @@ void SKP_Silk_find_LTP_FIX(
         lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 );\r
 \r
         SKP_Silk_sum_sqr_shift( &rr[ k ], &rr_shifts, r_ptr, subfr_length ); /* rr[ k ] in Q( -rr_shifts ) */\r
         lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 );\r
 \r
         SKP_Silk_sum_sqr_shift( &rr[ k ], &rr_shifts, r_ptr, subfr_length ); /* rr[ k ] in Q( -rr_shifts ) */\r
+\r
         /* Assure headroom */\r
         LZs = SKP_Silk_CLZ32( rr[k] );\r
         if( LZs < LTP_CORRS_HEAD_ROOM ) {\r
             rr[ k ] = SKP_RSHIFT_ROUND( rr[ k ], LTP_CORRS_HEAD_ROOM - LZs );\r
         /* Assure headroom */\r
         LZs = SKP_Silk_CLZ32( rr[k] );\r
         if( LZs < LTP_CORRS_HEAD_ROOM ) {\r
             rr[ k ] = SKP_RSHIFT_ROUND( rr[ k ], LTP_CORRS_HEAD_ROOM - LZs );\r
-            rr_shifts += (LTP_CORRS_HEAD_ROOM - LZs);\r
+            rr_shifts += ( LTP_CORRS_HEAD_ROOM - LZs );\r
         }\r
         corr_rshifts[ k ] = rr_shifts;\r
         }\r
         corr_rshifts[ k ] = rr_shifts;\r
-        SKP_Silk_corrMatrix_FIX( lag_ptr, subfr_length, LTP_ORDER, WLTP_ptr, &corr_rshifts[ k ] );     /* WLTP_fix_ptr in Q( -corr_rshifts[ k ] ) */\r
-        /* The correlation vector always have lower max abs value than rr and/or RR so head room is assured */\r
-        SKP_Silk_corrVector_FIX( lag_ptr, r_ptr, subfr_length, LTP_ORDER, Rr, corr_rshifts[ k ] ); /* Rr_fix_ptr   in Q( -corr_rshifts[ k ] ) */\r
+        SKP_Silk_corrMatrix_FIX( lag_ptr, subfr_length, LTP_ORDER, LTP_CORRS_HEAD_ROOM, WLTP_ptr, &corr_rshifts[ k ] );  /* WLTP_fix_ptr in Q( -corr_rshifts[ k ] ) */\r
+\r
+        /* The correlation vector always has lower max abs value than rr and/or RR so head room is assured */\r
+        SKP_Silk_corrVector_FIX( lag_ptr, r_ptr, subfr_length, LTP_ORDER, Rr, corr_rshifts[ k ] );  /* Rr_fix_ptr   in Q( -corr_rshifts[ k ] ) */\r
         if( corr_rshifts[ k ] > rr_shifts ) {\r
             rr[ k ] = SKP_RSHIFT( rr[ k ], corr_rshifts[ k ] - rr_shifts ); /* rr[ k ] in Q( -corr_rshifts[ k ] ) */\r
         }\r
         SKP_assert( rr[ k ] >= 0 );\r
 \r
         if( corr_rshifts[ k ] > rr_shifts ) {\r
             rr[ k ] = SKP_RSHIFT( rr[ k ], corr_rshifts[ k ] - rr_shifts ); /* rr[ k ] in Q( -corr_rshifts[ k ] ) */\r
         }\r
         SKP_assert( rr[ k ] >= 0 );\r
 \r
-        regu = SKP_SMULWB( rr[ k ] + 1, LTP_DAMPING_Q16 );\r
+        regu = SKP_SMULWB( rr[ k ] + 1, SKP_FIX_CONST( LTP_DAMPING, 16 ) );\r
         SKP_Silk_regularize_correlations_FIX( WLTP_ptr, &rr[k], regu, LTP_ORDER );\r
 \r
         SKP_Silk_solve_LDL_FIX( WLTP_ptr, LTP_ORDER, Rr, b_Q16 ); /* WLTP_fix_ptr and Rr_fix_ptr both in Q(-corr_rshifts[k]) */\r
         SKP_Silk_regularize_correlations_FIX( WLTP_ptr, &rr[k], regu, LTP_ORDER );\r
 \r
         SKP_Silk_solve_LDL_FIX( WLTP_ptr, LTP_ORDER, Rr, b_Q16 ); /* WLTP_fix_ptr and Rr_fix_ptr both in Q(-corr_rshifts[k]) */\r
@@ -96,12 +102,12 @@ void SKP_Silk_find_LTP_FIX(
 \r
         /* temp = Wght[ k ] / ( nrg[ k ] * Wght[ k ] + 0.01f * subfr_length ); */\r
         extra_shifts = SKP_min_int( corr_rshifts[ k ], LTP_CORRS_HEAD_ROOM );\r
 \r
         /* temp = Wght[ k ] / ( nrg[ k ] * Wght[ k ] + 0.01f * subfr_length ); */\r
         extra_shifts = SKP_min_int( corr_rshifts[ k ], LTP_CORRS_HEAD_ROOM );\r
-        denom32 = SKP_LSHIFT_SAT32( SKP_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 + extra_shifts ) +  /* Q( -corr_rshifts[ k ] + extra_shifts ) */\r
-            SKP_RSHIFT( SKP_SMULWB( subfr_length, 655 ), corr_rshifts[ k ] - extra_shifts );     /* Q( -corr_rshifts[ k ] + extra_shifts ) */\r
+        denom32 = SKP_LSHIFT_SAT32( SKP_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 + extra_shifts ) + /* Q( -corr_rshifts[ k ] + extra_shifts ) */\r
+            SKP_RSHIFT( SKP_SMULWB( subfr_length, 655 ), corr_rshifts[ k ] - extra_shifts );    /* Q( -corr_rshifts[ k ] + extra_shifts ) */\r
         denom32 = SKP_max( denom32, 1 );\r
         denom32 = SKP_max( denom32, 1 );\r
-        SKP_assert( ((SKP_int64)Wght_Q15[ k ] << 16 ) < SKP_int32_MAX ); /* Wght always < 0.5 in Q0 */\r
-        temp32 = SKP_DIV32( SKP_LSHIFT( ( SKP_int32 )Wght_Q15[ k ], 16 ), denom32 );  /* Q( 15 + 16 + corr_rshifts[k] - extra_shifts ) */\r
-        temp32 = SKP_RSHIFT( temp32, 31 + corr_rshifts[ k ] - extra_shifts - 26 );  /* Q26 */\r
+        SKP_assert( ((SKP_int64)Wght_Q15[ k ] << 16 ) < SKP_int32_MAX );                        /* Wght always < 0.5 in Q0 */\r
+        temp32 = SKP_DIV32( SKP_LSHIFT( ( SKP_int32 )Wght_Q15[ k ], 16 ), denom32 );            /* Q( 15 + 16 + corr_rshifts[k] - extra_shifts ) */\r
+        temp32 = SKP_RSHIFT( temp32, 31 + corr_rshifts[ k ] - extra_shifts - 26 );              /* Q26 */\r
         \r
         /* Limit temp such that the below scaling never wraps around */\r
         WLTP_max = 0;\r
         \r
         /* Limit temp such that the below scaling never wraps around */\r
         WLTP_max = 0;\r
@@ -129,7 +135,7 @@ void SKP_Silk_find_LTP_FIX(
         maxRshifts = SKP_max_int( corr_rshifts[ k ], maxRshifts );\r
     }\r
 \r
         maxRshifts = SKP_max_int( corr_rshifts[ k ], maxRshifts );\r
     }\r
 \r
-    /* compute LTP coding gain */\r
+    /* Compute LTP coding gain */\r
     if( LTPredCodGain_Q7 != NULL ) {\r
         LPC_LTP_res_nrg = 0;\r
         LPC_res_nrg     = 0;\r
     if( LTPredCodGain_Q7 != NULL ) {\r
         LPC_LTP_res_nrg = 0;\r
         LPC_res_nrg     = 0;\r
@@ -201,8 +207,8 @@ void SKP_Silk_find_LTP_FIX(
 \r
         g_Q26 = SKP_MUL( \r
             SKP_DIV32( \r
 \r
         g_Q26 = SKP_MUL( \r
             SKP_DIV32( \r
-                LTP_SMOOTHING_Q26\r
-                SKP_RSHIFT( LTP_SMOOTHING_Q26, 10 ) + temp32 ),                                       /* Q10 */ \r
+                SKP_FIX_CONST( LTP_SMOOTHING, 26 )\r
+                SKP_RSHIFT( SKP_FIX_CONST( LTP_SMOOTHING, 26 ), 10 ) + temp32 ),                                       /* Q10 */ \r
             SKP_LSHIFT_SAT32( SKP_SUB_SAT32( ( SKP_int32 )m_Q12, SKP_RSHIFT( d_Q14[ k ], 2 ) ), 4 ) );  /* Q16 */\r
 \r
         temp32 = 0;\r
             SKP_LSHIFT_SAT32( SKP_SUB_SAT32( ( SKP_int32 )m_Q12, SKP_RSHIFT( d_Q14[ k ], 2 ) ), 4 ) );  /* Q16 */\r
 \r
         temp32 = 0;\r
index 3601b25..ab13656 100644 (file)
@@ -26,7 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
-#include "SKP_Silk_common_pitch_est_defines.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 /* Find pitch lags */\r
 void SKP_Silk_find_pitch_lags_FIX(\r
 \r
 /* Find pitch lags */\r
 void SKP_Silk_find_pitch_lags_FIX(\r
@@ -38,7 +38,7 @@ void SKP_Silk_find_pitch_lags_FIX(
 {\r
     SKP_Silk_predict_state_FIX *psPredSt = &psEnc->sPred;\r
     SKP_int   buf_len, i, scale;\r
 {\r
     SKP_Silk_predict_state_FIX *psPredSt = &psEnc->sPred;\r
     SKP_int   buf_len, i, scale;\r
-    SKP_int32 thrhld_Q15;\r
+    SKP_int32 thrhld_Q15, res_nrg;\r
     const SKP_int16 *x_buf, *x_buf_ptr;\r
     SKP_int16 Wsig[      FIND_PITCH_LPC_WIN_MAX ], *Wsig_ptr;\r
     SKP_int32 auto_corr[ MAX_FIND_PITCH_LPC_ORDER + 1 ];\r
     const SKP_int16 *x_buf, *x_buf_ptr;\r
     SKP_int16 Wsig[      FIND_PITCH_LPC_WIN_MAX ], *Wsig_ptr;\r
     SKP_int32 auto_corr[ MAX_FIND_PITCH_LPC_ORDER + 1 ];\r
@@ -81,13 +81,16 @@ void SKP_Silk_find_pitch_lags_FIX(
     /* Calculate autocorrelation sequence */\r
     SKP_Silk_autocorr( auto_corr, &scale, Wsig, psPredSt->pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1 ); \r
         \r
     /* Calculate autocorrelation sequence */\r
     SKP_Silk_autocorr( auto_corr, &scale, Wsig, psPredSt->pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1 ); \r
         \r
-    /* add white noise, as fraction of energy */\r
-    auto_corr[ 0 ] = SKP_SMLAWB( auto_corr[ 0 ], auto_corr[ 0 ], FIND_PITCH_WHITE_NOISE_FRACTION_Q16 );\r
+    /* Add white noise, as fraction of energy */\r
+    auto_corr[ 0 ] = SKP_SMLAWB( auto_corr[ 0 ], auto_corr[ 0 ], SKP_FIX_CONST( FIND_PITCH_WHITE_NOISE_FRACTION, 16 ) );\r
 \r
 \r
-    /* calculate the reflection coefficients using schur */\r
-    SKP_Silk_schur( rc_Q15, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder );\r
+    /* Calculate the reflection coefficients using schur */\r
+    res_nrg = SKP_Silk_schur( rc_Q15, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder );\r
 \r
 \r
-    /* convert reflection coefficients to prediction coefficients */\r
+    /* Prediction gain */\r
+    psEncCtrl->predGain_Q16 = SKP_DIV32_varQ( auto_corr[ 0 ], SKP_max_int( res_nrg, 1 ), 16 );\r
+\r
+    /* Convert reflection coefficients to prediction coefficients */\r
     SKP_Silk_k2a( A_Q24, rc_Q15, psEnc->sCmn.pitchEstimationLPCOrder );\r
     \r
     /* Convert From 32 bit Q24 to 16 bit Q12 coefs */\r
     SKP_Silk_k2a( A_Q24, rc_Q15, psEnc->sCmn.pitchEstimationLPCOrder );\r
     \r
     /* Convert From 32 bit Q24 to 16 bit Q12 coefs */\r
@@ -96,7 +99,7 @@ void SKP_Silk_find_pitch_lags_FIX(
     }\r
 \r
     /* Do BWE */\r
     }\r
 \r
     /* Do BWE */\r
-    SKP_Silk_bwexpander( A_Q12, psEnc->sCmn.pitchEstimationLPCOrder, FIND_PITCH_BANDWITH_EXPANSION_Q16 );\r
+    SKP_Silk_bwexpander( A_Q12, psEnc->sCmn.pitchEstimationLPCOrder, SKP_FIX_CONST( FIND_PITCH_BANDWITH_EXPANSION, 16 ) );\r
     \r
     /*****************************************/\r
     /* LPC analysis filtering                */\r
     \r
     /*****************************************/\r
     /* LPC analysis filtering                */\r
@@ -106,11 +109,11 @@ void SKP_Silk_find_pitch_lags_FIX(
     SKP_memset( res, 0, psEnc->sCmn.pitchEstimationLPCOrder * sizeof( SKP_int16 ) );\r
 \r
     /* Threshold for pitch estimator */\r
     SKP_memset( res, 0, psEnc->sCmn.pitchEstimationLPCOrder * sizeof( SKP_int16 ) );\r
 \r
     /* Threshold for pitch estimator */\r
-    thrhld_Q15 = ( 1 << 14 ); // 0.5f in Q15\r
-    thrhld_Q15 = SKP_SMLABB( thrhld_Q15, -131, psEnc->sCmn.pitchEstimationLPCOrder );\r
-    thrhld_Q15 = SKP_SMLABB( thrhld_Q15,  -13, ( SKP_int16 )SKP_Silk_SQRT_APPROX( SKP_LSHIFT( ( SKP_int32 )psEnc->speech_activity_Q8, 8 ) ) );\r
-    thrhld_Q15 = SKP_SMLABB( thrhld_Q15, 4587, psEnc->sCmn.prev_sigtype );\r
-    thrhld_Q15 = SKP_MLA(    thrhld_Q15,  -31, SKP_RSHIFT( psEncCtrl->input_tilt_Q15, 8 ) );\r
+    thrhld_Q15 = SKP_FIX_CONST( 0.45, 15 );\r
+    thrhld_Q15 = SKP_SMLABB( thrhld_Q15, SKP_FIX_CONST( -0.004, 15 ), psEnc->sCmn.pitchEstimationLPCOrder );\r
+    thrhld_Q15 = SKP_SMLABB( thrhld_Q15, SKP_FIX_CONST( -0.1,   7  ), psEnc->speech_activity_Q8 );\r
+    thrhld_Q15 = SKP_SMLABB( thrhld_Q15, SKP_FIX_CONST(  0.15,  15 ), psEnc->sCmn.prev_sigtype );\r
+    thrhld_Q15 = SKP_SMLAWB( thrhld_Q15, SKP_FIX_CONST( -0.1,   16 ), psEncCtrl->input_tilt_Q15 );\r
     thrhld_Q15 = SKP_SAT16(  thrhld_Q15 );\r
 \r
     /*****************************************/\r
     thrhld_Q15 = SKP_SAT16(  thrhld_Q15 );\r
 \r
     /*****************************************/\r
@@ -118,7 +121,7 @@ void SKP_Silk_find_pitch_lags_FIX(
     /*****************************************/\r
 TIC(pitch_analysis_core_FIX)\r
     psEncCtrl->sCmn.sigtype = SKP_Silk_pitch_analysis_core( res, psEncCtrl->sCmn.pitchL, &psEncCtrl->sCmn.lagIndex, \r
     /*****************************************/\r
 TIC(pitch_analysis_core_FIX)\r
     psEncCtrl->sCmn.sigtype = SKP_Silk_pitch_analysis_core( res, psEncCtrl->sCmn.pitchL, &psEncCtrl->sCmn.lagIndex, \r
-        &psEncCtrl->sCmn.contourIndex, &psEnc->LTPCorr_Q15, psEnc->sCmn.prevLag, psEnc->pitchEstimationThreshold_Q16, \r
+        &psEncCtrl->sCmn.contourIndex, &psEnc->LTPCorr_Q15, psEnc->sCmn.prevLag, psEnc->sCmn.pitchEstimationThreshold_Q16, \r
         ( SKP_int16 )thrhld_Q15, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity, psEnc->sCmn.nb_subfr );\r
 TOC(pitch_analysis_core_FIX)\r
 }\r
         ( SKP_int16 )thrhld_Q15, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity, psEnc->sCmn.nb_subfr );\r
 TOC(pitch_analysis_core_FIX)\r
 }\r
index efb8ead..a82775c 100644 (file)
@@ -228,8 +228,8 @@ void SKP_Silk_find_pred_coefs_FIX(
 \r
         /* Weighted input energy */\r
         in_ptr = &x_flp[ psEnc->sCmn.ltp_mem_length ];\r
 \r
         /* Weighted input energy */\r
         in_ptr = &x_flp[ psEnc->sCmn.ltp_mem_length ];\r
-        DEBUG_STORE_DATA( x_flp.dat, x_flp, psEnc->sCmn.frame_length * sizeof(SKP_float));\r
-        DEBUG_STORE_DATA( in_ptr.dat, in_ptr, psEnc->sCmn.frame_length * sizeof(SKP_float));\r
+        DEBUG_STORE_DATA( x_flp.dat,  x_flp,  psEnc->sCmn.frame_length * sizeof( SKP_float ) );\r
+        DEBUG_STORE_DATA( in_ptr.dat, in_ptr, psEnc->sCmn.frame_length * sizeof( SKP_float ) );\r
         in_nrg = 0.0f;\r
         for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
             in_nrg += (SKP_float)SKP_Silk_energy_FLP( in_ptr, psEnc->sCmn.subfr_length ) * Wght[ k ];\r
         in_nrg = 0.0f;\r
         for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
             in_nrg += (SKP_float)SKP_Silk_energy_FLP( in_ptr, psEnc->sCmn.subfr_length ) * Wght[ k ];\r
@@ -241,15 +241,15 @@ void SKP_Silk_find_pred_coefs_FIX(
         }\r
 \r
         DEBUG_STORE_DATA( uq_PredCoef.dat, uq_PredCoef[0], psEnc->sCmn.predictLPCOrder * sizeof( SKP_float ) );\r
         }\r
 \r
         DEBUG_STORE_DATA( uq_PredCoef.dat, uq_PredCoef[0], psEnc->sCmn.predictLPCOrder * sizeof( SKP_float ) );\r
-        DEBUG_STORE_DATA( PredCoef.dat, PredCoef[0], psEnc->sCmn.predictLPCOrder * sizeof( SKP_float ) );\r
+        DEBUG_STORE_DATA( PredCoef.dat,    PredCoef[0],    psEnc->sCmn.predictLPCOrder * sizeof( SKP_float ) );\r
 \r
         LPC_res_nrg  = 0.0f;\r
         LTP_res_nrg  = 0.0f;\r
         qLPC_res_nrg = 0.0f;\r
         qLTP_res_nrg = 0.0f;\r
 \r
         LPC_res_nrg  = 0.0f;\r
         LTP_res_nrg  = 0.0f;\r
         qLPC_res_nrg = 0.0f;\r
         qLTP_res_nrg = 0.0f;\r
-        for( j = 0; j < psEnc->sCmn.nb_subfr; j+=2 ) {\r
+        for( j = 0; j < psEnc->sCmn.nb_subfr; j += 2 ) {\r
             /* Calculate LPC residual with unquantized LPC */\r
             /* Calculate LPC residual with unquantized LPC */\r
-            SKP_Silk_LPC_analysis_filter_FLP( LPC_res, uq_PredCoef[ j >> 1 ], &x_flp[ j * psEnc->sCmn.subfr_length ],\r
+            SKP_Silk_LPC_analysis_filter_FLP( LPC_res, uq_PredCoef[ j >> 1 ], x_flp + j * psEnc->sCmn.subfr_length,\r
                 ( psEnc->sCmn.ltp_mem_length + ( psEnc->sCmn.subfr_length << 1 ) ), psEnc->sCmn.predictLPCOrder );\r
 \r
             /* Weighted energy */\r
                 ( psEnc->sCmn.ltp_mem_length + ( psEnc->sCmn.subfr_length << 1 ) ), psEnc->sCmn.predictLPCOrder );\r
 \r
             /* Weighted energy */\r
@@ -273,7 +273,7 @@ void SKP_Silk_find_pred_coefs_FIX(
             }\r
 \r
             /* Calculate LPC residual with quantized LPC */\r
             }\r
 \r
             /* Calculate LPC residual with quantized LPC */\r
-            SKP_Silk_LPC_analysis_filter_FLP( LPC_res, PredCoef[ j >> 1 ], &x_flp[ j * psEnc->sCmn.subfr_length ],\r
+            SKP_Silk_LPC_analysis_filter_FLP( LPC_res, PredCoef[ j >> 1 ], x_flp + j * psEnc->sCmn.subfr_length,\r
                 ( psEnc->sCmn.ltp_mem_length + ( psEnc->sCmn.subfr_length << 1 ) ), psEnc->sCmn.predictLPCOrder );\r
 \r
             /* Weighted energy */\r
                 ( psEnc->sCmn.ltp_mem_length + ( psEnc->sCmn.subfr_length << 1 ) ), psEnc->sCmn.predictLPCOrder );\r
 \r
             /* Weighted energy */\r
@@ -300,8 +300,8 @@ void SKP_Silk_find_pred_coefs_FIX(
                 SKP_memcpy( LTP_res, &LPC_res[ psEnc->sCmn.ltp_mem_length ], ( psEnc->sCmn.subfr_length << 1 ) * sizeof( SKP_float ) );\r
             }\r
             /* Save residual */\r
                 SKP_memcpy( LTP_res, &LPC_res[ psEnc->sCmn.ltp_mem_length ], ( psEnc->sCmn.subfr_length << 1 ) * sizeof( SKP_float ) );\r
             }\r
             /* Save residual */\r
-            DEBUG_STORE_DATA( LPC_res.dat,          &LPC_res[ psEnc->sCmn.ltp_mem_length ], (psEnc->sCmn.subfr_length << 1) * sizeof( SKP_float ) );\r
-            DEBUG_STORE_DATA( res.dat,              LTP_res,                              ( psEnc->sCmn.subfr_length << 1 ) * sizeof( SKP_float ) );\r
+            DEBUG_STORE_DATA( LPC_res.dat, &LPC_res[ psEnc->sCmn.ltp_mem_length ], ( psEnc->sCmn.subfr_length << 1 ) * sizeof( SKP_float ) );\r
+            DEBUG_STORE_DATA( res.dat,     LTP_res,                                ( psEnc->sCmn.subfr_length << 1 ) * sizeof( SKP_float ) );\r
         }\r
         if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
             LPC_predCodGain  = 3.0f * SKP_Silk_log2( in_nrg       / LPC_res_nrg  );\r
         }\r
         if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
             LPC_predCodGain  = 3.0f * SKP_Silk_log2( in_nrg       / LPC_res_nrg  );\r
@@ -321,7 +321,7 @@ void SKP_Silk_find_pred_coefs_FIX(
         DEBUG_STORE_DATA( LPC_predCodGain.dat,  &LPC_predCodGain,                                      sizeof( SKP_float ) );\r
         DEBUG_STORE_DATA( QLPC_predCodGain.dat, &QLPC_predCodGain,                                     sizeof( SKP_float ) );\r
         DEBUG_STORE_DATA( predCodGain.dat,      &predCodGain,                                          sizeof( SKP_float ) ); \r
         DEBUG_STORE_DATA( LPC_predCodGain.dat,  &LPC_predCodGain,                                      sizeof( SKP_float ) );\r
         DEBUG_STORE_DATA( QLPC_predCodGain.dat, &QLPC_predCodGain,                                     sizeof( SKP_float ) );\r
         DEBUG_STORE_DATA( predCodGain.dat,      &predCodGain,                                          sizeof( SKP_float ) ); \r
-        DEBUG_STORE_DATA( ResNrg.dat,           SF_resNrg,                                  psEnc->sCmn.nb_subfr * sizeof( SKP_float ) );\r
+        DEBUG_STORE_DATA( ResNrg.dat,           SF_resNrg,                      psEnc->sCmn.nb_subfr * sizeof( SKP_float ) );\r
     }\r
 #endif\r
 }\r
     }\r
 #endif\r
 }\r
@@ -425,18 +425,4 @@ double SKP_Silk_energy_FLP(
     SKP_assert( result >= 0.0 );\r
     return result;\r
 }\r
     SKP_assert( result >= 0.0 );\r
     return result;\r
 }\r
-\r
-/* integer to floating-point conversion */\r
-SKP_INLINE void SKP_short2float_array(\r
-       SKP_float               *out, \r
-       const SKP_int16 *in, \r
-       SKP_int32               length\r
-) \r
-{\r
-       SKP_int32 k;\r
-       for (k = length-1; k >= 0; k--) {\r
-               out[k] = (SKP_float)in[k];\r
-       }\r
-}\r
-\r
 #endif\r
 #endif\r
index 63c378f..01bfc5e 100644 (file)
@@ -37,9 +37,6 @@ SKP_int SKP_Silk_init_encoder_FIX(
     /* Clear the entire encoder state */\r
     SKP_memset( psEnc, 0, sizeof( SKP_Silk_encoder_state_FIX ) );\r
 \r
     /* Clear the entire encoder state */\r
     SKP_memset( psEnc, 0, sizeof( SKP_Silk_encoder_state_FIX ) );\r
 \r
-    /* Initialize to 24 kHz API sampling, 24 kHz max internal sampling, 20 ms packets, 25 kbps, 0% packet loss, and init non-zero values */\r
-    ret = SKP_Silk_control_encoder_FIX( psEnc, 24000, 24, 20, 25, 0, 0, 0, 10, 0 );\r
-\r
 #if HIGH_PASS_INPUT\r
     psEnc->variable_HP_smth1_Q15 = 200844; /* = SKP_Silk_log2(70)_Q0; */\r
     psEnc->variable_HP_smth2_Q15 = 200844; /* = SKP_Silk_log2(70)_Q0; */\r
 #if HIGH_PASS_INPUT\r
     psEnc->variable_HP_smth1_Q15 = 200844; /* = SKP_Silk_log2(70)_Q0; */\r
     psEnc->variable_HP_smth2_Q15 = 200844; /* = SKP_Silk_log2(70)_Q0; */\r
@@ -47,8 +44,6 @@ SKP_int SKP_Silk_init_encoder_FIX(
 \r
     /* Used to deactivate e.g. LSF interpolation and fluctuation reduction */\r
     psEnc->sCmn.first_frame_after_reset = 1;\r
 \r
     /* Used to deactivate e.g. LSF interpolation and fluctuation reduction */\r
     psEnc->sCmn.first_frame_after_reset = 1;\r
-    psEnc->sCmn.fs_kHz_changed          = 0;\r
-    psEnc->sCmn.LBRR_enabled            = 0;\r
 \r
     /* Initialize Silk VAD */\r
     ret += SKP_Silk_VAD_Init( &psEnc->sCmn.sVAD );\r
 \r
     /* Initialize Silk VAD */\r
     ret += SKP_Silk_VAD_Init( &psEnc->sCmn.sVAD );\r
index 6d7d0dd..c4573e1 100644 (file)
@@ -28,11 +28,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #ifndef SKP_SILK_MAIN_FIX_H\r
 #define SKP_SILK_MAIN_FIX_H\r
 \r
 #ifndef SKP_SILK_MAIN_FIX_H\r
 #define SKP_SILK_MAIN_FIX_H\r
 \r
-#include <stdlib.h>\r
 #include "SKP_Silk_SigProc_FIX.h"\r
 #include "SKP_Silk_structs_FIX.h"\r
 #include "SKP_Silk_main.h"\r
 #include "SKP_Silk_SigProc_FIX.h"\r
 #include "SKP_Silk_structs_FIX.h"\r
 #include "SKP_Silk_main.h"\r
-#include "SKP_Silk_define_FIX.h"\r
 #include "SKP_Silk_PLC.h"\r
 #include "SKP_debug.h"\r
 #include "entenc.h"\r
 #include "SKP_Silk_PLC.h"\r
 #include "SKP_debug.h"\r
 #include "entenc.h"\r
@@ -48,49 +46,44 @@ extern "C"
 /* Encoder Functions */\r
 /*********************/\r
 \r
 /* Encoder Functions */\r
 /*********************/\r
 \r
-/* Initializes the Silk encoder state */\r
-SKP_int SKP_Silk_init_encoder_FIX(\r
-    SKP_Silk_encoder_state_FIX  *psEnc              /* I/O  Pointer to Silk FIX encoder state           */\r
-);\r
-\r
-/* Control the Silk encoder */\r
-SKP_int SKP_Silk_control_encoder_FIX( \r
-    SKP_Silk_encoder_state_FIX  *psEnc,             /* I/O  Pointer to Silk encoder state                   */\r
-    const SKP_int32             API_fs_Hz,          /* I    External (API) sampling rate (Hz)               */\r
-    const SKP_int               max_internal_fs_kHz,/* I    Maximum internal sampling rate (kHz)            */\r
-    const SKP_int               PacketSize_ms,      /* I    Packet length (ms)                              */\r
-    SKP_int32                   TargetRate_bps,     /* I    Target max bitrate (bps) (used if SNR_dB == 0)  */\r
-    const SKP_int               PacketLoss_perc,    /* I    Packet loss rate (in percent)                   */\r
-    const SKP_int               INBandFEC_enabled,  /* I    Enable (1) / disable (0) inband FEC             */\r
-    const SKP_int               DTX_enabled,        /* I    Enable / disable DTX                            */\r
-    const SKP_int               InputFramesize_ms,  /* I    Inputframe in ms                                */\r
-    const SKP_int               Complexity          /* I    Complexity (0->low; 1->medium; 2->high)         */\r
+/* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */\r
+void SKP_Silk_HP_variable_cutoff_FIX(\r
+    SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
+    SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Encoder control                             */\r
+    SKP_int16                       *out,           /* O    high-pass filtered output signal            */\r
+    const SKP_int16                 *in             /* I    input signal                                */\r
 );\r
 \r
 /* Encoder main function */\r
 SKP_int SKP_Silk_encode_frame_FIX( \r
 );\r
 \r
 /* Encoder main function */\r
 SKP_int SKP_Silk_encode_frame_FIX( \r
-    SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O  Pointer to Silk FIX encoder state           */\r
-    SKP_uint8                       *pCode,             /* O    Pointer to payload                          */\r
-    SKP_int16                       *pnBytesOut,        /* I/O  Pointer to number of payload bytes;         */\r
-                                                        /*      input: max length; output: used             */\r
-    const SKP_int16                 *pIn                /* I    Pointer to input speech frame               */\r
+    SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O  Pointer to Silk FIX encoder state       */\r
+    SKP_int32                       *pnBytesOut,        /* I/O  Pointer to number of payload bytes;     */\r
+                                                        /*      input: max length; output: used         */\r
+    ec_enc                          *psRangeEnc,        /* I/O  compressor data structure               */\r
+    const SKP_int16                 *pIn                /* I    Pointer to input speech frame           */\r
 );\r
 \r
 );\r
 \r
-/* Low BitRate Redundancy encoding functionality. Reuse all parameters but encode with lower bitrate */\r
+/* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate           */\r
 void SKP_Silk_LBRR_encode_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Pointer to Silk FIX encoder state           */\r
     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Pointer to Silk FIX encoder control struct  */\r
     SKP_uint8                       *pCode,         /* O    Pointer to payload                          */\r
 void SKP_Silk_LBRR_encode_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Pointer to Silk FIX encoder state           */\r
     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Pointer to Silk FIX encoder control struct  */\r
     SKP_uint8                       *pCode,         /* O    Pointer to payload                          */\r
-    SKP_int16                       *pnBytesOut,    /* I/O  Pointer to number of payload bytes          */\r
+    SKP_int32                       *pnBytesOut,    /* I/O  Pointer to number of payload bytes          */\r
     SKP_int16                       xfw[]           /* I    Input signal                                */\r
 );\r
 \r
     SKP_int16                       xfw[]           /* I    Input signal                                */\r
 );\r
 \r
-/* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */\r
-void SKP_Silk_HP_variable_cutoff_FIX(\r
-    SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
-    SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Encoder control                             */\r
-    SKP_int16                       *out,           /* O    high-pass filtered output signal            */\r
-    const SKP_int16                 *in             /* I    input signal                                */\r
+/* Initializes the Silk encoder state */\r
+SKP_int SKP_Silk_init_encoder_FIX(\r
+    SKP_Silk_encoder_state_FIX  *psEnc              /* I/O  Pointer to Silk FIX encoder state           */\r
+);\r
+\r
+/* Control the Silk encoder */\r
+SKP_int SKP_Silk_control_encoder_FIX( \r
+    SKP_Silk_encoder_state_FIX  *psEnc,                 /* I/O  Pointer to Silk encoder state           */\r
+    const SKP_int               PacketSize_ms,          /* I    Packet length (ms)                      */\r
+    const SKP_int32             TargetRate_bps,         /* I    Target max bitrate (bps)                */\r
+    const SKP_int               PacketLoss_perc,        /* I    Packet loss rate (in percent)           */\r
+    const SKP_int               Complexity              /* I    Complexity (0->low; 1->medium; 2->high) */\r
 );\r
 \r
 /****************/\r
 );\r
 \r
 /****************/\r
@@ -103,9 +96,10 @@ void SKP_Silk_prefilter_FIX(
     const SKP_int16                     x[]             /* I    Speech signal                               */\r
 );\r
 \r
     const SKP_int16                     x[]             /* I    Speech signal                               */\r
 );\r
 \r
-/**************************************************************/\r
+/**************************/\r
+/* Noise shaping analysis */\r
+/**************************/\r
 /* Compute noise shaping coefficients and initial gain values */\r
 /* Compute noise shaping coefficients and initial gain values */\r
-/**************************************************************/\r
 void SKP_Silk_noise_shape_analysis_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state FIX                           */\r
     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Encoder control FIX                         */\r
 void SKP_Silk_noise_shape_analysis_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state FIX                           */\r
     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Encoder control FIX                         */\r
@@ -118,21 +112,15 @@ void SKP_Silk_warped_autocorrelation_FIX(
           SKP_int32                 *corr,              /* O    Result [order + 1]                      */\r
           SKP_int                   *scale,             /* O    Scaling of the correlation vector       */\r
     const SKP_int16                 *input,             /* I    Input data to correlate                 */\r
           SKP_int32                 *corr,              /* O    Result [order + 1]                      */\r
           SKP_int                   *scale,             /* O    Scaling of the correlation vector       */\r
     const SKP_int16                 *input,             /* I    Input data to correlate                 */\r
-    const SKP_int16                 warping_Q16,        /* I    Warping coefficient                     */\r
+    const SKP_int                   warping_Q16,        /* I    Warping coefficient                     */\r
     const SKP_int                   length,             /* I    Length of input                         */\r
     const SKP_int                   order               /* I    Correlation order (even)                */\r
 );\r
 \r
     const SKP_int                   length,             /* I    Length of input                         */\r
     const SKP_int                   order               /* I    Correlation order (even)                */\r
 );\r
 \r
-/* Processing of gains */\r
-void SKP_Silk_process_gains_FIX(\r
-    SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
-    SKP_Silk_encoder_control_FIX    *psEncCtrl      /* I/O  Encoder control                             */\r
-);\r
-\r
 /* Control low bitrate redundancy usage */\r
 void SKP_Silk_LBRR_ctrl_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
 /* Control low bitrate redundancy usage */\r
 void SKP_Silk_LBRR_ctrl_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
-    SKP_Silk_encoder_control_FIX    *psEncCtrl      /* I/O  encoder control                             */\r
+    SKP_Silk_encoder_control        *psEncCtrlC     /* I/O  encoder control                             */\r
 );\r
 \r
 /* Calculation of LTP state scaling */\r
 );\r
 \r
 /* Calculation of LTP state scaling */\r
@@ -144,7 +132,6 @@ void SKP_Silk_LTP_scale_ctrl_FIX(
 /**********************************************/\r
 /* Prediction Analysis                        */\r
 /**********************************************/\r
 /**********************************************/\r
 /* Prediction Analysis                        */\r
 /**********************************************/\r
-\r
 /* Find pitch lags */\r
 void SKP_Silk_find_pitch_lags_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
 /* Find pitch lags */\r
 void SKP_Silk_find_pitch_lags_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
@@ -153,12 +140,14 @@ void SKP_Silk_find_pitch_lags_FIX(
     const SKP_int16                 x[]             /* I    Speech signal                               */\r
 );\r
 \r
     const SKP_int16                 x[]             /* I    Speech signal                               */\r
 );\r
 \r
+/* Find LPC and LTP coefficients */\r
 void SKP_Silk_find_pred_coefs_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  encoder control                             */\r
     const SKP_int16                 res_pitch[]     /* I    Residual from pitch analysis                */\r
 );\r
 \r
 void SKP_Silk_find_pred_coefs_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  encoder control                             */\r
     const SKP_int16                 res_pitch[]     /* I    Residual from pitch analysis                */\r
 );\r
 \r
+/* LPC analysis */\r
 void SKP_Silk_find_LPC_FIX(\r
     SKP_int             NLSF_Q15[],                 /* O    NLSFs                                                                       */\r
     SKP_int             *interpIndex,               /* O    NLSF interpolation index, only used for NLSF interpolation                  */\r
 void SKP_Silk_find_LPC_FIX(\r
     SKP_int             NLSF_Q15[],                 /* O    NLSFs                                                                       */\r
     SKP_int             *interpIndex,               /* O    NLSF interpolation index, only used for NLSF interpolation                  */\r
@@ -170,18 +159,7 @@ void SKP_Silk_find_LPC_FIX(
     const SKP_int       nb_subfr                    /* I:   Number of subframes                                                         */\r
 );\r
 \r
     const SKP_int       nb_subfr                    /* I:   Number of subframes                                                         */\r
 );\r
 \r
-void SKP_Silk_LTP_analysis_filter_FIX(\r
-    SKP_int16           *LTP_res,                               /* O:   LTP residual signal of length MAX_NB_SUBFR * ( pre_length + subfr_length )  */\r
-    const SKP_int16     *x,                                     /* I:   Pointer to input signal with at least max( pitchL ) preceeding samples      */\r
-    const SKP_int16     LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ],/* I:   LTP_ORDER LTP coefficients for each MAX_NB_SUBFR subframe                   */\r
-    const SKP_int       pitchL[ MAX_NB_SUBFR ],                 /* I:   Pitch lag, one for each subframe                                            */\r
-    const SKP_int32     invGains_Q16[ MAX_NB_SUBFR ],           /* I:   Inverse quantization gains, one for each subframe                           */\r
-    const SKP_int       subfr_length,                           /* I:   Length of each subframe                                                     */\r
-    const SKP_int       nb_subfr,                               /* I:   Number of subframes                                                         */\r
-    const SKP_int       pre_length                              /* I:   Length of the preceeding samples starting at &x[0] for each subframe        */\r
-);\r
-\r
-/* Finds LTP vector from correlations */\r
+/* LTP analysis */\r
 void SKP_Silk_find_LTP_FIX(\r
     SKP_int16           b_Q14[ MAX_NB_SUBFR * LTP_ORDER ],              /* O    LTP coefs                                                   */\r
     SKP_int32           WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ],   /* O    Weight for LTP quantization                                 */\r
 void SKP_Silk_find_LTP_FIX(\r
     SKP_int16           b_Q14[ MAX_NB_SUBFR * LTP_ORDER ],              /* O    LTP coefs                                                   */\r
     SKP_int32           WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ],   /* O    Weight for LTP quantization                                 */\r
@@ -195,6 +173,30 @@ void SKP_Silk_find_LTP_FIX(
     SKP_int             corr_rshifts[ MAX_NB_SUBFR ]                    /* O    right shifts applied to correlations                        */\r
 );\r
 \r
     SKP_int             corr_rshifts[ MAX_NB_SUBFR ]                    /* O    right shifts applied to correlations                        */\r
 );\r
 \r
+void SKP_Silk_LTP_analysis_filter_FIX(\r
+    SKP_int16           *LTP_res,                               /* O:   LTP residual signal of length MAX_NB_SUBFR * ( pre_length + subfr_length )  */\r
+    const SKP_int16     *x,                                     /* I:   Pointer to input signal with at least max( pitchL ) preceeding samples      */\r
+    const SKP_int16     LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ],/* I:   LTP_ORDER LTP coefficients for each MAX_NB_SUBFR subframe                   */\r
+    const SKP_int       pitchL[ MAX_NB_SUBFR ],                 /* I:   Pitch lag, one for each subframe                                            */\r
+    const SKP_int32     invGains_Q16[ MAX_NB_SUBFR ],           /* I:   Inverse quantization gains, one for each subframe                           */\r
+    const SKP_int       subfr_length,                           /* I:   Length of each subframe                                                     */\r
+    const SKP_int       nb_subfr,                               /* I:   Number of subframes                                                         */\r
+    const SKP_int       pre_length                              /* I:   Length of the preceeding samples starting at &x[0] for each subframe        */\r
+);\r
+\r
+/* Calculates residual energies of input subframes where all subframes have LPC_order   */\r
+/* of preceeding samples                                                                */\r
+void SKP_Silk_residual_energy_FIX(\r
+          SKP_int32 nrgs[ MAX_NB_SUBFR ],           /* O    Residual energy per subframe    */\r
+          SKP_int   nrgsQ[ MAX_NB_SUBFR ],          /* O    Q value per subframe            */\r
+    const SKP_int16 x[],                            /* I    Input signal                    */\r
+    const SKP_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],    /* I    AR coefs for each frame half    */\r
+    const SKP_int32 gains[ MAX_NB_SUBFR ],          /* I    Quantization gains              */\r
+    const SKP_int   subfr_length,                   /* I    Subframe length                 */\r
+    const SKP_int   nb_subfr,                       /* I    Number of subframes             */\r
+    const SKP_int   LPC_order                       /* I    LPC order                       */\r
+);\r
+\r
 /* LTP tap quantizer */\r
 void SKP_Silk_quant_LTP_gains_FIX(\r
     SKP_int16           B_Q14[],                /* I/O  (un)quantized LTP gains     */\r
 /* LTP tap quantizer */\r
 void SKP_Silk_quant_LTP_gains_FIX(\r
     SKP_int16           B_Q14[],                /* I/O  (un)quantized LTP gains     */\r
@@ -209,7 +211,6 @@ void SKP_Silk_quant_LTP_gains_FIX(
 /******************/\r
 /* NLSF Quantizer */\r
 /******************/\r
 /******************/\r
 /* NLSF Quantizer */\r
 /******************/\r
-\r
 /* Limit, stabilize, convert and quantize NLSFs.    */ \r
 void SKP_Silk_process_NLSFs_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,     /* I/O  encoder state                               */\r
 /* Limit, stabilize, convert and quantize NLSFs.    */ \r
 void SKP_Silk_process_NLSFs_FIX(\r
     SKP_Silk_encoder_state_FIX      *psEnc,     /* I/O  encoder state                               */\r
@@ -217,7 +218,7 @@ void SKP_Silk_process_NLSFs_FIX(
     SKP_int                         *pNLSF_Q15  /* I/O  Normalized LSFs (quant out) (0 - (2^15-1))  */\r
 );\r
 \r
     SKP_int                         *pNLSF_Q15  /* I/O  Normalized LSFs (quant out) (0 - (2^15-1))  */\r
 );\r
 \r
-/* LSF vector encoder */\r
+/* NLSF vector encoder */\r
 void SKP_Silk_NLSF_MSVQ_encode_FIX(\r
           SKP_int                   *NLSFIndices,           /* O    Codebook path vector [ CB_STAGES ]      */\r
           SKP_int                   *pNLSF_Q15,             /* I/O  Quantized NLSF vector [ LPC_ORDER ]     */\r
 void SKP_Silk_NLSF_MSVQ_encode_FIX(\r
           SKP_int                   *NLSFIndices,           /* O    Codebook path vector [ CB_STAGES ]      */\r
           SKP_int                   *pNLSF_Q15,             /* I/O  Quantized NLSF vector [ LPC_ORDER ]     */\r
@@ -248,12 +249,22 @@ void SKP_Silk_NLSF_VQ_sum_error_FIX(
     SKP_int32                       *err_Q20,           /* O    Weighted quantization errors  [N*K]         */\r
     const SKP_int                   *in_Q15,            /* I    Input vectors to be quantized [N*LPC_order] */\r
     const SKP_int                   *w_Q6,              /* I    Weighting vectors             [N*LPC_order] */\r
     SKP_int32                       *err_Q20,           /* O    Weighted quantization errors  [N*K]         */\r
     const SKP_int                   *in_Q15,            /* I    Input vectors to be quantized [N*LPC_order] */\r
     const SKP_int                   *w_Q6,              /* I    Weighting vectors             [N*LPC_order] */\r
-    const SKP_int16                 *pCB_Q15,           /* I    Codebook vectors              [K*LPC_order] */\r
+    const SKP_int8                  *pCB_Q9,            /* I    Codebook vectors              [K*LPC_order] */\r
     const SKP_int                   N,                  /* I    Number of input vectors                     */\r
     const SKP_int                   K,                  /* I    Number of codebook vectors                  */\r
     const SKP_int                   LPC_order           /* I    Number of LPCs                              */\r
 );\r
 \r
     const SKP_int                   N,                  /* I    Number of input vectors                     */\r
     const SKP_int                   K,                  /* I    Number of codebook vectors                  */\r
     const SKP_int                   LPC_order           /* I    Number of LPCs                              */\r
 );\r
 \r
+/* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */\r
+SKP_int32 SKP_Silk_residual_energy16_covar_FIX(\r
+    const SKP_int16                 *c,                 /* I    Prediction vector                           */\r
+    const SKP_int32                 *wXX,               /* I    Correlation matrix                          */\r
+    const SKP_int32                 *wXx,               /* I    Correlation vector                          */\r
+    SKP_int32                       wxx,                /* I    Signal energy                               */\r
+    SKP_int                         D,                  /* I    Dimension                                   */\r
+    SKP_int                         cQ                  /* I    Q value for c vector 0 - 15                 */\r
+);\r
+\r
 /* Entropy constrained MATRIX-weighted VQ, for a single input data vector */\r
 void SKP_Silk_VQ_WMat_EC_FIX(\r
     SKP_int                         *ind,               /* O    index of best codebook vector               */\r
 /* Entropy constrained MATRIX-weighted VQ, for a single input data vector */\r
 void SKP_Silk_VQ_WMat_EC_FIX(\r
     SKP_int                         *ind,               /* O    index of best codebook vector               */\r
@@ -266,15 +277,21 @@ void SKP_Silk_VQ_WMat_EC_FIX(
     SKP_int                         L                   /* I    number of vectors in codebook               */\r
 );\r
 \r
     SKP_int                         L                   /* I    number of vectors in codebook               */\r
 );\r
 \r
+/* Processing of gains */\r
+void SKP_Silk_process_gains_FIX(\r
+    SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
+    SKP_Silk_encoder_control_FIX    *psEncCtrl      /* I/O  Encoder control                             */\r
+);\r
+\r
 /******************/\r
 /* Linear Algebra */\r
 /******************/\r
 /******************/\r
 /* Linear Algebra */\r
 /******************/\r
-\r
 /* Calculates correlation matrix X'*X */\r
 void SKP_Silk_corrMatrix_FIX(\r
     const SKP_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */\r
     const SKP_int                   L,          /* I    Length of vectors                                   */\r
     const SKP_int                   order,      /* I    Max lag for correlation                             */\r
 /* Calculates correlation matrix X'*X */\r
 void SKP_Silk_corrMatrix_FIX(\r
     const SKP_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */\r
     const SKP_int                   L,          /* I    Length of vectors                                   */\r
     const SKP_int                   order,      /* I    Max lag for correlation                             */\r
+    const SKP_int                   head_room,  /* I    Desired headroom                                    */\r
     SKP_int32                       *XX,        /* O    Pointer to X'*X correlation matrix [ order x order ]*/\r
     SKP_int                         *rshifts    /* I/O  Right shifts of correlations                        */\r
 );\r
     SKP_int32                       *XX,        /* O    Pointer to X'*X correlation matrix [ order x order ]*/\r
     SKP_int                         *rshifts    /* I/O  Right shifts of correlations                        */\r
 );\r
@@ -305,29 +322,6 @@ void SKP_Silk_solve_LDL_FIX(
     SKP_int32                       *x_Q16              /* O    Pointer to x solution vector                */\r
 );\r
 \r
     SKP_int32                       *x_Q16              /* O    Pointer to x solution vector                */\r
 );\r
 \r
-/* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */\r
-SKP_int32 SKP_Silk_residual_energy16_covar_FIX(\r
-    const SKP_int16                 *c,                 /* I    Prediction vector                           */\r
-    const SKP_int32                 *wXX,               /* I    Correlation matrix                          */\r
-    const SKP_int32                 *wXx,               /* I    Correlation vector                          */\r
-    SKP_int32                       wxx,                /* I    Signal energy                               */\r
-    SKP_int                         D,                  /* I    Dimension                                   */\r
-    SKP_int                         cQ                  /* I    Q value for c vector 0 - 15                 */\r
-);\r
-\r
-/* Calculates residual energies of input subframes where all subframes have LPC_order   */\r
-/* of preceeding samples                                                                */\r
-void SKP_Silk_residual_energy_FIX(\r
-          SKP_int32 nrgs[ MAX_NB_SUBFR ],           /* O    Residual energy per subframe    */\r
-          SKP_int   nrgsQ[ MAX_NB_SUBFR ],          /* O    Q value per subframe            */\r
-    const SKP_int16 x[],                            /* I    Input signal                    */\r
-    const SKP_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],    /* I    AR coefs for each frame half    */\r
-    const SKP_int32 gains[ MAX_NB_SUBFR ],          /* I    Quantization gains              */\r
-    const SKP_int   subfr_length,                   /* I    Subframe length                 */\r
-    const SKP_int   nb_subfr,                       /* I    Number of subframes             */\r
-    const SKP_int   LPC_order                       /* I    LPC order                       */\r
-);\r
-\r
 #ifndef FORCE_CPP_BUILD\r
 #ifdef __cplusplus\r
 }\r
 #ifndef FORCE_CPP_BUILD\r
 #ifdef __cplusplus\r
 }\r
index a695d1d..05cbd2b 100644 (file)
@@ -26,7 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
-#include "SKP_Silk_perceptual_parameters_FIX.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 /* Compute gain to make warped filter coefficients have a zero mean log frequency response on a     */\r
 /* non-warped frequency scale. (So that it can be implemented with a minimum-phase monic filter.)   */\r
 \r
 /* Compute gain to make warped filter coefficients have a zero mean log frequency response on a     */\r
 /* non-warped frequency scale. (So that it can be implemented with a minimum-phase monic filter.)   */\r
@@ -43,62 +43,66 @@ SKP_INLINE SKP_int32 warped_gain( // gain in Q16
     for( i = order - 2; i >= 0; i-- ) {\r
         gain_Q24 = SKP_SMLAWB( coefs_Q24[ i ], gain_Q24, lambda_Q16 );\r
     }\r
     for( i = order - 2; i >= 0; i-- ) {\r
         gain_Q24 = SKP_SMLAWB( coefs_Q24[ i ], gain_Q24, lambda_Q16 );\r
     }\r
-    gain_Q24  = SKP_SMLAWB( 1 << 24, gain_Q24, -lambda_Q16 );\r
+    gain_Q24  = SKP_SMLAWB( SKP_FIX_CONST( 1.0, 24 ), gain_Q24, -lambda_Q16 );\r
     return SKP_INVERSE32_varQ( gain_Q24, 40 );\r
 }\r
 \r
     return SKP_INVERSE32_varQ( gain_Q24, 40 );\r
 }\r
 \r
-/* Convert warped filter coefficients to monic pseudo-warped coefficients */\r
-SKP_INLINE void warped_true2monic_coefs( \r
-    SKP_int32           *coefs_Q24,\r
+/* Convert warped filter coefficients to monic pseudo-warped coefficients and limit maximum     */\r
+/* amplitude of monic warped coefficients by using bandwidth expansion on the true coefficients */\r
+SKP_INLINE void limit_warped_coefs( \r
+    SKP_int32           *coefs_syn_Q24,\r
+    SKP_int32           *coefs_ana_Q24,\r
     SKP_int             lambda_Q16,\r
     SKP_int             lambda_Q16,\r
-    SKP_int             order \r
+    SKP_int32           limit_Q24,\r
+    SKP_int             order\r
 ) {\r
 ) {\r
-    SKP_int   i;\r
-    SKP_int32 gain_Q16, nom_Q16, den_Q24;\r
+    SKP_int   i, iter, ind = 0;\r
+    SKP_int32 tmp, maxabs_Q24, chirp_Q16, gain_syn_Q16, gain_ana_Q16;\r
+    SKP_int32 nom_Q16, den_Q24;\r
 \r
 \r
+    /* Convert to monic coefficients */\r
     lambda_Q16 = -lambda_Q16;\r
     for( i = order - 1; i > 0; i-- ) {\r
     lambda_Q16 = -lambda_Q16;\r
     for( i = order - 1; i > 0; i-- ) {\r
-        coefs_Q24[ i - 1 ] = SKP_SMLAWB( coefs_Q24[ i - 1 ], coefs_Q24[ i ], lambda_Q16 );\r
+        coefs_syn_Q24[ i - 1 ] = SKP_SMLAWB( coefs_syn_Q24[ i - 1 ], coefs_syn_Q24[ i ], lambda_Q16 );\r
+        coefs_ana_Q24[ i - 1 ] = SKP_SMLAWB( coefs_ana_Q24[ i - 1 ], coefs_ana_Q24[ i ], lambda_Q16 );\r
     }\r
     }\r
-    nom_Q16  = SKP_SMLAWB( 1 << 16, lambda_Q16,     -lambda_Q16 );\r
-    den_Q24  = SKP_SMLAWB( 1 << 24, coefs_Q24[ 0 ], -lambda_Q16 );\r
-    gain_Q16 = SKP_DIV32_varQ( nom_Q16, den_Q24, 24 );\r
+    lambda_Q16 = -lambda_Q16;\r
+    nom_Q16  = SKP_SMLAWB( SKP_FIX_CONST( 1.0, 16 ), -lambda_Q16,        lambda_Q16 );\r
+    den_Q24  = SKP_SMLAWB( SKP_FIX_CONST( 1.0, 24 ), coefs_syn_Q24[ 0 ], lambda_Q16 );\r
+    gain_syn_Q16 = SKP_DIV32_varQ( nom_Q16, den_Q24, 24 );\r
+    den_Q24  = SKP_SMLAWB( SKP_FIX_CONST( 1.0, 24 ), coefs_ana_Q24[ 0 ], lambda_Q16 );\r
+    gain_ana_Q16 = SKP_DIV32_varQ( nom_Q16, den_Q24, 24 );\r
     for( i = 0; i < order; i++ ) {\r
     for( i = 0; i < order; i++ ) {\r
-        coefs_Q24[ i ] = SKP_SMULWW( gain_Q16, coefs_Q24[ i ] );\r
+        coefs_syn_Q24[ i ] = SKP_SMULWW( gain_syn_Q16, coefs_syn_Q24[ i ] );\r
+        coefs_ana_Q24[ i ] = SKP_SMULWW( gain_ana_Q16, coefs_ana_Q24[ i ] );\r
     }\r
     }\r
-}\r
-\r
-/* Limit max amplitude of monic warped coefficients by using bandwidth expansion on the true coefficients */\r
-SKP_INLINE void limit_warped_coefs( \r
-    SKP_int32           *coefs_syn_Q24,\r
-    SKP_int32           *coefs_ana_Q24,\r
-    SKP_int             lambda_Q16,\r
-    SKP_int32           limit_Q24,\r
-    SKP_int             order\r
-) {\r
-    SKP_int   i, iter, ind;\r
-    SKP_int32 tmp, maxabs_Q24, chirp_Q16;\r
 \r
     for( iter = 0; iter < 10; iter++ ) {\r
         /* Find maximum absolute value */\r
 \r
     for( iter = 0; iter < 10; iter++ ) {\r
         /* Find maximum absolute value */\r
-        ind = 1;\r
-        maxabs_Q24 = SKP_abs( coefs_syn_Q24[ ind ] );\r
-        for( i = 2; i < order - 1; i++ ) {\r
-            tmp = SKP_abs( coefs_syn_Q24[ i ] );\r
+        maxabs_Q24 = -1;\r
+        for( i = 0; i < order; i++ ) {\r
+            tmp = SKP_max( SKP_abs_int32( coefs_syn_Q24[ i ] ), SKP_abs_int32( coefs_ana_Q24[ i ] ) );\r
             if( tmp > maxabs_Q24 ) {\r
                 maxabs_Q24 = tmp;\r
                 ind = i;\r
             }\r
         }\r
         if( maxabs_Q24 <= limit_Q24 ) {\r
             if( tmp > maxabs_Q24 ) {\r
                 maxabs_Q24 = tmp;\r
                 ind = i;\r
             }\r
         }\r
         if( maxabs_Q24 <= limit_Q24 ) {\r
+            /* Coefficients are within range - done */\r
             return;\r
         }\r
 \r
             return;\r
         }\r
 \r
-        /* Convert to true warped coefficients */\r
+        /* Convert back to true warped coefficients */\r
         for( i = 1; i < order; i++ ) {\r
             coefs_syn_Q24[ i - 1 ] = SKP_SMLAWB( coefs_syn_Q24[ i - 1 ], coefs_syn_Q24[ i ], lambda_Q16 );\r
             coefs_ana_Q24[ i - 1 ] = SKP_SMLAWB( coefs_ana_Q24[ i - 1 ], coefs_ana_Q24[ i ], lambda_Q16 );\r
         }\r
         for( i = 1; i < order; i++ ) {\r
             coefs_syn_Q24[ i - 1 ] = SKP_SMLAWB( coefs_syn_Q24[ i - 1 ], coefs_syn_Q24[ i ], lambda_Q16 );\r
             coefs_ana_Q24[ i - 1 ] = SKP_SMLAWB( coefs_ana_Q24[ i - 1 ], coefs_ana_Q24[ i ], lambda_Q16 );\r
         }\r
+        gain_syn_Q16 = SKP_INVERSE32_varQ( gain_syn_Q16, 32 );\r
+        gain_ana_Q16 = SKP_INVERSE32_varQ( gain_ana_Q16, 32 );\r
+        for( i = 0; i < order; i++ ) {\r
+            coefs_syn_Q24[ i ] = SKP_SMULWW( gain_syn_Q16, coefs_syn_Q24[ i ] );\r
+            coefs_ana_Q24[ i ] = SKP_SMULWW( gain_ana_Q16, coefs_ana_Q24[ i ] );\r
+        }\r
 \r
         /* Apply bandwidth expansion */\r
         chirp_Q16 = SKP_FIX_CONST( 0.99, 16 ) - SKP_DIV32_varQ(\r
 \r
         /* Apply bandwidth expansion */\r
         chirp_Q16 = SKP_FIX_CONST( 0.99, 16 ) - SKP_DIV32_varQ(\r
@@ -107,14 +111,24 @@ SKP_INLINE void limit_warped_coefs(
         SKP_Silk_bwexpander_32( coefs_syn_Q24, order, chirp_Q16 );\r
         SKP_Silk_bwexpander_32( coefs_ana_Q24, order, chirp_Q16 );\r
 \r
         SKP_Silk_bwexpander_32( coefs_syn_Q24, order, chirp_Q16 );\r
         SKP_Silk_bwexpander_32( coefs_ana_Q24, order, chirp_Q16 );\r
 \r
-        /* Convert back to monic warped coefficients */\r
+        /* Convert to monic warped coefficients */\r
         lambda_Q16 = -lambda_Q16;\r
         for( i = order - 1; i > 0; i-- ) {\r
             coefs_syn_Q24[ i - 1 ] = SKP_SMLAWB( coefs_syn_Q24[ i - 1 ], coefs_syn_Q24[ i ], lambda_Q16 );\r
             coefs_ana_Q24[ i - 1 ] = SKP_SMLAWB( coefs_ana_Q24[ i - 1 ], coefs_ana_Q24[ i ], lambda_Q16 );\r
         }\r
         lambda_Q16 = -lambda_Q16;\r
         lambda_Q16 = -lambda_Q16;\r
         for( i = order - 1; i > 0; i-- ) {\r
             coefs_syn_Q24[ i - 1 ] = SKP_SMLAWB( coefs_syn_Q24[ i - 1 ], coefs_syn_Q24[ i ], lambda_Q16 );\r
             coefs_ana_Q24[ i - 1 ] = SKP_SMLAWB( coefs_ana_Q24[ i - 1 ], coefs_ana_Q24[ i ], lambda_Q16 );\r
         }\r
         lambda_Q16 = -lambda_Q16;\r
+        nom_Q16  = SKP_SMLAWB( SKP_FIX_CONST( 1.0, 16 ), -lambda_Q16,        lambda_Q16 );\r
+        den_Q24  = SKP_SMLAWB( SKP_FIX_CONST( 1.0, 24 ), coefs_syn_Q24[ 0 ], lambda_Q16 );\r
+        gain_syn_Q16 = SKP_DIV32_varQ( nom_Q16, den_Q24, 24 );\r
+        den_Q24  = SKP_SMLAWB( SKP_FIX_CONST( 1.0, 24 ), coefs_ana_Q24[ 0 ], lambda_Q16 );\r
+        gain_ana_Q16 = SKP_DIV32_varQ( nom_Q16, den_Q24, 24 );\r
+        for( i = 0; i < order; i++ ) {\r
+            coefs_syn_Q24[ i ] = SKP_SMULWW( gain_syn_Q16, coefs_syn_Q24[ i ] );\r
+            coefs_ana_Q24[ i ] = SKP_SMULWW( gain_ana_Q16, coefs_ana_Q24[ i ] );\r
+        }\r
     }\r
     }\r
+       SKP_assert( 0 );\r
 }\r
 \r
 /**************************************************************/\r
 }\r
 \r
 /**************************************************************/\r
@@ -128,7 +142,7 @@ void SKP_Silk_noise_shape_analysis_FIX(
 )\r
 {\r
     SKP_Silk_shape_state_FIX *psShapeSt = &psEnc->sShape;\r
 )\r
 {\r
     SKP_Silk_shape_state_FIX *psShapeSt = &psEnc->sShape;\r
-    SKP_int     k, i, nSamples, Qnrg, b_Q14, scale = 0, sz;\r
+    SKP_int     k, i, nSamples, Qnrg, b_Q14, warping_Q16, scale = 0;\r
     SKP_int32   SNR_adj_dB_Q7, HarmBoost_Q16, HarmShapeGain_Q16, Tilt_Q16, tmp32;\r
     SKP_int32   nrg, pre_nrg_Q30, log_energy_Q7, log_energy_prev_Q7, energy_variation_Q7;\r
     SKP_int32   delta_Q16, BWExp1_Q16, BWExp2_Q16, gain_mult_Q16, gain_add_Q16, strength_Q16, b_Q8;\r
     SKP_int32   SNR_adj_dB_Q7, HarmBoost_Q16, HarmShapeGain_Q16, Tilt_Q16, tmp32;\r
     SKP_int32   nrg, pre_nrg_Q30, log_energy_Q7, log_energy_prev_Q7, energy_variation_Q7;\r
     SKP_int32   delta_Q16, BWExp1_Q16, BWExp2_Q16, gain_mult_Q16, gain_add_Q16, strength_Q16, b_Q8;\r
@@ -142,7 +156,7 @@ void SKP_Silk_noise_shape_analysis_FIX(
     SKP_int32   sqrt_nrg[ MAX_NB_SUBFR ], Qnrg_vec[ MAX_NB_SUBFR ];\r
 \r
     /* Point to start of first LPC analysis block */\r
     SKP_int32   sqrt_nrg[ MAX_NB_SUBFR ], Qnrg_vec[ MAX_NB_SUBFR ];\r
 \r
     /* Point to start of first LPC analysis block */\r
-    x_ptr = x + psEnc->sCmn.la_shape - SKP_SMULBB( SHAPE_LPC_WIN_MS, psEnc->sCmn.fs_kHz ) + psEnc->sCmn.subfr_length;\r
+    x_ptr = x - psEnc->sCmn.la_shape;\r
 \r
     /****************/\r
     /* CONTROL SNR  */\r
 \r
     /****************/\r
     /* CONTROL SNR  */\r
@@ -151,11 +165,12 @@ void SKP_Silk_noise_shape_analysis_FIX(
     psEncCtrl->current_SNR_dB_Q7 = psEnc->SNR_dB_Q7;\r
 #else\r
     /* Reduce SNR_dB values if recent bitstream has exceeded TargetRate */\r
     psEncCtrl->current_SNR_dB_Q7 = psEnc->SNR_dB_Q7;\r
 #else\r
     /* Reduce SNR_dB values if recent bitstream has exceeded TargetRate */\r
-    psEncCtrl->current_SNR_dB_Q7 = psEnc->SNR_dB_Q7 - SKP_SMULWB( SKP_LSHIFT( ( SKP_int32 )psEnc->BufferedInChannel_ms, 7 ), 3277 );\r
+    psEncCtrl->current_SNR_dB_Q7 = psEnc->SNR_dB_Q7 - SKP_SMULWB( SKP_LSHIFT( ( SKP_int32 )psEnc->BufferedInChannel_ms, 7 ), \r
+        SKP_FIX_CONST( 0.05, 16 ) );\r
 #endif\r
 \r
     /* Reduce SNR_dB if inband FEC used */\r
 #endif\r
 \r
     /* Reduce SNR_dB if inband FEC used */\r
-    if( psEnc->speech_activity_Q8 > LBRR_SPEECH_ACTIVITY_THRES_Q8 ) {\r
+    if( psEnc->speech_activity_Q8 > SKP_FIX_CONST( LBRR_SPEECH_ACTIVITY_THRES, 8 ) ) {\r
         psEncCtrl->current_SNR_dB_Q7 -= SKP_RSHIFT( psEnc->inBandFEC_SNR_comp_Q8, 1 );\r
     }\r
 \r
         psEncCtrl->current_SNR_dB_Q7 -= SKP_RSHIFT( psEnc->inBandFEC_SNR_comp_Q8, 1 );\r
     }\r
 \r
@@ -167,23 +182,24 @@ void SKP_Silk_noise_shape_analysis_FIX(
         + psEncCtrl->input_quality_bands_Q15[ 1 ], 2 );\r
 \r
     /* Coding quality level, between 0.0_Q0 and 1.0_Q0, but in Q14 */\r
         + psEncCtrl->input_quality_bands_Q15[ 1 ], 2 );\r
 \r
     /* Coding quality level, between 0.0_Q0 and 1.0_Q0, but in Q14 */\r
-    psEncCtrl->coding_quality_Q14 = SKP_RSHIFT( SKP_Silk_sigm_Q15( SKP_RSHIFT_ROUND( psEncCtrl->current_SNR_dB_Q7 - ( 17 << 7 ), 4 ) ), 1 );\r
+    psEncCtrl->coding_quality_Q14 = SKP_RSHIFT( SKP_Silk_sigm_Q15( SKP_RSHIFT_ROUND( psEncCtrl->current_SNR_dB_Q7 - \r
+        SKP_FIX_CONST( 18.0, 7 ), 4 ) ), 1 );\r
 \r
     /* Reduce coding SNR during low speech activity */\r
 \r
     /* Reduce coding SNR during low speech activity */\r
-    b_Q8 = ( 1 << 8 ) - psEnc->speech_activity_Q8;\r
+    b_Q8 = SKP_FIX_CONST( 1.0, 8 ) - psEnc->speech_activity_Q8;\r
     b_Q8 = SKP_SMULWB( SKP_LSHIFT( b_Q8, 8 ), b_Q8 );\r
     SNR_adj_dB_Q7 = SKP_SMLAWB( psEncCtrl->current_SNR_dB_Q7,\r
     b_Q8 = SKP_SMULWB( SKP_LSHIFT( b_Q8, 8 ), b_Q8 );\r
     SNR_adj_dB_Q7 = SKP_SMLAWB( psEncCtrl->current_SNR_dB_Q7,\r
-        SKP_SMULBB( -BG_SNR_DECR_dB_Q7 >> ( 4 + 1 ), b_Q8 ),                                            // Q11\r
-        SKP_SMULWB( ( 1 << 14 ) + psEncCtrl->input_quality_Q14, psEncCtrl->coding_quality_Q14 ) );      // Q12\r
+        SKP_SMULBB( SKP_FIX_CONST( -BG_SNR_DECR_dB, 7 ) >> ( 4 + 1 ), b_Q8 ),                                       // Q11\r
+        SKP_SMULWB( SKP_FIX_CONST( 1.0, 14 ) + psEncCtrl->input_quality_Q14, psEncCtrl->coding_quality_Q14 ) );     // Q12\r
 \r
     if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         /* Reduce gains for periodic signals */\r
 \r
     if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         /* Reduce gains for periodic signals */\r
-        SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, HARM_SNR_INCR_dB_Q7 << 1, psEnc->LTPCorr_Q15 );\r
+        SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, SKP_FIX_CONST( HARM_SNR_INCR_dB, 8 ), psEnc->LTPCorr_Q15 );\r
     } else { \r
         /* For unvoiced signals and low-quality input, adjust the quality slower than SNR_dB setting */\r
         SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, \r
     } else { \r
         /* For unvoiced signals and low-quality input, adjust the quality slower than SNR_dB setting */\r
         SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, \r
-            SKP_SMLAWB( 6 << ( 7 + 2 ), -104856, psEncCtrl->current_SNR_dB_Q7 ),    //-104856_Q18 = -0.4_Q0, Q9\r
-            ( 1 << 14 ) - psEncCtrl->input_quality_Q14 );                           // Q14\r
+            SKP_SMLAWB( SKP_FIX_CONST( 6.0, 9 ), -SKP_FIX_CONST( 0.4, 18 ), psEncCtrl->current_SNR_dB_Q7 ),\r
+            SKP_FIX_CONST( 1.0, 14 ) - psEncCtrl->input_quality_Q14 );\r
     }\r
 \r
     /*************************/\r
     }\r
 \r
     /*************************/\r
@@ -212,53 +228,70 @@ void SKP_Silk_noise_shape_analysis_FIX(
             pitch_res_ptr += nSamples;\r
         }\r
 \r
             pitch_res_ptr += nSamples;\r
         }\r
 \r
-        psEncCtrl->sparseness_Q8 = SKP_RSHIFT( SKP_Silk_sigm_Q15( SKP_SMULWB( energy_variation_Q7 - ( 5 << 7 ), 6554 ) ), 7 ); // 6554_Q16 = 0.1_Q0\r
+        psEncCtrl->sparseness_Q8 = SKP_RSHIFT( SKP_Silk_sigm_Q15( SKP_SMULWB( energy_variation_Q7 - \r
+            SKP_FIX_CONST( 5.0, 7 ), SKP_FIX_CONST( 0.1, 16 ) ) ), 7 );\r
 \r
         /* Set quantization offset depending on sparseness measure */\r
 \r
         /* Set quantization offset depending on sparseness measure */\r
-        if( psEncCtrl->sparseness_Q8 > SPARSENESS_THRESHOLD_QNT_OFFSET_Q8 ) {\r
+        if( psEncCtrl->sparseness_Q8 > SKP_FIX_CONST( SPARSENESS_THRESHOLD_QNT_OFFSET, 8 ) ) {\r
             psEncCtrl->sCmn.QuantOffsetType = 0;\r
         } else {\r
             psEncCtrl->sCmn.QuantOffsetType = 1;\r
         }\r
         \r
         /* Increase coding SNR for sparse signals */\r
             psEncCtrl->sCmn.QuantOffsetType = 0;\r
         } else {\r
             psEncCtrl->sCmn.QuantOffsetType = 1;\r
         }\r
         \r
         /* Increase coding SNR for sparse signals */\r
-        SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, SPARSE_SNR_INCR_dB_Q7 << 8, psEncCtrl->sparseness_Q8 - ( 1 << 7 ) );\r
+        SNR_adj_dB_Q7 = SKP_SMLAWB( SNR_adj_dB_Q7, SKP_FIX_CONST( SPARSE_SNR_INCR_dB, 15 ), psEncCtrl->sparseness_Q8 - SKP_FIX_CONST( 0.5, 8 ) );\r
     }\r
 \r
     /*******************************/\r
     /* Control bandwidth expansion */\r
     /*******************************/\r
     }\r
 \r
     /*******************************/\r
     /* Control bandwidth expansion */\r
     /*******************************/\r
-    delta_Q16  = SKP_SMULWB( ( 1 << 16 ) - SKP_SMULBB( 3, psEncCtrl->coding_quality_Q14 ), LOW_RATE_BANDWIDTH_EXPANSION_DELTA_Q16 );\r
-    BWExp1_Q16 = BANDWIDTH_EXPANSION_Q16 - delta_Q16;\r
-    BWExp2_Q16 = BANDWIDTH_EXPANSION_Q16 + delta_Q16;\r
-    if( psEnc->sCmn.fs_kHz == 24 ) {\r
-        /* Less bandwidth expansion for super wideband */\r
-        BWExp1_Q16 = ( 1 << 16 ) - SKP_SMULWB( SWB_BANDWIDTH_EXPANSION_REDUCTION_Q16, ( 1 << 16 ) - BWExp1_Q16 );\r
-        BWExp2_Q16 = ( 1 << 16 ) - SKP_SMULWB( SWB_BANDWIDTH_EXPANSION_REDUCTION_Q16, ( 1 << 16 ) - BWExp2_Q16 );\r
-    }\r
+    /* More BWE for signals with high prediction gain */\r
+    strength_Q16 = SKP_SMULWB( psEncCtrl->predGain_Q16, SKP_FIX_CONST( FIND_PITCH_WHITE_NOISE_FRACTION, 16 ) );\r
+    BWExp1_Q16 = BWExp2_Q16 = SKP_DIV32_varQ( SKP_FIX_CONST( BANDWIDTH_EXPANSION, 16 ), \r
+        SKP_SMLAWW( SKP_FIX_CONST( 1.0, 16 ), strength_Q16, strength_Q16 ), 16 );\r
+    delta_Q16  = SKP_SMULWB( SKP_FIX_CONST( 1.0, 16 ) - SKP_SMULBB( 3, psEncCtrl->coding_quality_Q14 ), \r
+        SKP_FIX_CONST( LOW_RATE_BANDWIDTH_EXPANSION_DELTA, 16 ) );\r
+    BWExp1_Q16 = SKP_SUB32( BWExp1_Q16, delta_Q16 );\r
+    BWExp2_Q16 = SKP_ADD32( BWExp2_Q16, delta_Q16 );\r
     /* BWExp1 will be applied after BWExp2, so make it relative */\r
     BWExp1_Q16 = SKP_DIV32_16( SKP_LSHIFT( BWExp1_Q16, 14 ), SKP_RSHIFT( BWExp2_Q16, 2 ) );\r
 \r
     /* BWExp1 will be applied after BWExp2, so make it relative */\r
     BWExp1_Q16 = SKP_DIV32_16( SKP_LSHIFT( BWExp1_Q16, 14 ), SKP_RSHIFT( BWExp2_Q16, 2 ) );\r
 \r
-    /* Warping coefficient */\r
-    psEncCtrl->sCmn.warping_Q16 = psEnc->sCmn.fs_kHz * WARPING_MULTIPLIER_Q16;\r
-    psEncCtrl->sCmn.warping_Q16 = SKP_min( psEncCtrl->sCmn.warping_Q16, 32767 );\r
+    if( psEnc->sCmn.warping_Q16 > 0 ) {\r
+        /* Slightly more warping in analysis will move quantization noise up in frequency, where it's better masked */\r
+        warping_Q16 = SKP_SMLAWB( psEnc->sCmn.warping_Q16, psEncCtrl->coding_quality_Q14, SKP_FIX_CONST( 0.01, 18 ) );\r
+    } else {\r
+        warping_Q16 = 0;\r
+    }\r
 \r
     /********************************************/\r
     /* Compute noise shaping AR coefs and gains */\r
     /********************************************/\r
 \r
     /********************************************/\r
     /* Compute noise shaping AR coefs and gains */\r
     /********************************************/\r
-    sz = ( SKP_int )SKP_SMULBB( SHAPE_LPC_WIN_MS, psEnc->sCmn.fs_kHz );\r
     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
-        /* Apply window */\r
-        SKP_Silk_apply_sine_window( x_windowed, x_ptr, 0, SHAPE_LPC_WIN_MS * psEnc->sCmn.fs_kHz );\r
+        /* Apply window: sine slope followed by flat part followed by cosine slope */\r
+        SKP_int shift, slope_part, flat_part;\r
+        flat_part = psEnc->sCmn.fs_kHz * 3;\r
+        slope_part = SKP_RSHIFT( psEnc->sCmn.shapeWinLength - flat_part, 1 );\r
+\r
+        SKP_Silk_apply_sine_window( x_windowed, x_ptr, 1, slope_part );\r
+        shift = slope_part;\r
+        SKP_memcpy( x_windowed + shift, x_ptr + shift, flat_part * sizeof(SKP_int16) );\r
+        shift += flat_part;\r
+        SKP_Silk_apply_sine_window( x_windowed + shift, x_ptr + shift, 2, slope_part );\r
 \r
         /* Update pointer: next LPC analysis block */\r
         x_ptr += psEnc->sCmn.subfr_length;\r
 \r
 \r
         /* Update pointer: next LPC analysis block */\r
         x_ptr += psEnc->sCmn.subfr_length;\r
 \r
-        /* Calculate warped auto correlation */\r
-        SKP_Silk_warped_autocorrelation_FIX( auto_corr, &scale, x_windowed, psEncCtrl->sCmn.warping_Q16, sz, psEnc->sCmn.shapingLPCOrder ); \r
+        if( psEnc->sCmn.warping_Q16 > 0 ) {\r
+            /* Calculate warped auto correlation */\r
+            SKP_Silk_warped_autocorrelation_FIX( auto_corr, &scale, x_windowed, warping_Q16, psEnc->sCmn.shapeWinLength, psEnc->sCmn.shapingLPCOrder ); \r
+        } else {\r
+            /* Calculate regular auto correlation */\r
+            SKP_Silk_autocorr( auto_corr, &scale, x_windowed, psEnc->sCmn.shapeWinLength, psEnc->sCmn.shapingLPCOrder + 1 );\r
+        }\r
 \r
         /* Add white noise, as a fraction of energy */\r
 \r
         /* Add white noise, as a fraction of energy */\r
-        auto_corr[0] = SKP_ADD32( auto_corr[0], SKP_max_32( SKP_SMULWB( SKP_RSHIFT( auto_corr[ 0 ], 4 ), SHAPE_WHITE_NOISE_FRACTION_Q20 ), 1 ) ); \r
+        auto_corr[0] = SKP_ADD32( auto_corr[0], SKP_max_32( SKP_SMULWB( SKP_RSHIFT( auto_corr[ 0 ], 4 ), \r
+            SKP_FIX_CONST( SHAPE_WHITE_NOISE_FRACTION, 20 ) ), 1 ) ); \r
 \r
         /* Calculate the reflection coefficients using schur */\r
         nrg = SKP_Silk_schur64( refl_coef_Q16, auto_corr, psEnc->sCmn.shapingLPCOrder );\r
 \r
         /* Calculate the reflection coefficients using schur */\r
         nrg = SKP_Silk_schur64( refl_coef_Q16, auto_corr, psEnc->sCmn.shapingLPCOrder );\r
@@ -267,26 +300,6 @@ void SKP_Silk_noise_shape_analysis_FIX(
         /* Convert reflection coefficients to prediction coefficients */\r
         SKP_Silk_k2a_Q16( AR2_Q24, refl_coef_Q16, psEnc->sCmn.shapingLPCOrder );\r
 \r
         /* Convert reflection coefficients to prediction coefficients */\r
         SKP_Silk_k2a_Q16( AR2_Q24, refl_coef_Q16, psEnc->sCmn.shapingLPCOrder );\r
 \r
-        /* Convert residual energy to non-warped scale */\r
-        gain_mult_Q16 = warped_gain( AR2_Q24, psEncCtrl->sCmn.warping_Q16, psEnc->sCmn.shapingLPCOrder );\r
-        gain_mult_Q16 = SKP_SMULWW( gain_mult_Q16, gain_mult_Q16 );\r
-        SKP_assert( gain_mult_Q16 >= 0 ); // If breaking, add dynamic scaling\r
-        nrg = SKP_SMULWW( nrg, gain_mult_Q16 );\r
-        SKP_assert( nrg >= 0 ); // If breaking, add dynamic scaling\r
-\r
-        /* Bandwidth expansion for synthesis filter shaping */\r
-        SKP_Silk_bwexpander_32( AR2_Q24, psEnc->sCmn.shapingLPCOrder, BWExp2_Q16 );\r
-\r
-        /* Compute noise shaping filter coefficients */\r
-        SKP_memcpy( AR1_Q24, AR2_Q24, psEnc->sCmn.shapingLPCOrder * sizeof( SKP_int32 ) );\r
-\r
-        /* Bandwidth expansion for analysis filter shaping */\r
-        SKP_assert( BWExp1_Q16 <= ( 1 << 16 ) ); // If ever breaking, use LPC_stabilize() in these cases to stay within range\r
-        SKP_Silk_bwexpander_32( AR1_Q24, psEnc->sCmn.shapingLPCOrder, BWExp1_Q16 );\r
-\r
-        /* Increase residual energy */\r
-        nrg = SKP_SMLAWB( nrg, SKP_RSHIFT( auto_corr[ 0 ], 8 ), SHAPE_MIN_ENERGY_RATIO_Q24 );\r
-\r
         Qnrg = -scale;          // range: -12...30\r
         SKP_assert( Qnrg >= -12 );\r
         SKP_assert( Qnrg <=  30 );\r
         Qnrg = -scale;          // range: -12...30\r
         SKP_assert( Qnrg >= -12 );\r
         SKP_assert( Qnrg <=  30 );\r
@@ -305,6 +318,22 @@ void SKP_Silk_noise_shape_analysis_FIX(
 \r
         psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( tmp32, 16 - Qnrg );\r
 \r
 \r
         psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( tmp32, 16 - Qnrg );\r
 \r
+        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
+            psEncCtrl->Gains_Q16[ k ] = SKP_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 );\r
+        }\r
+\r
+        /* Bandwidth expansion for synthesis filter shaping */\r
+        SKP_Silk_bwexpander_32( AR2_Q24, psEnc->sCmn.shapingLPCOrder, BWExp2_Q16 );\r
+\r
+        /* Compute noise shaping filter coefficients */\r
+        SKP_memcpy( AR1_Q24, AR2_Q24, psEnc->sCmn.shapingLPCOrder * sizeof( SKP_int32 ) );\r
+\r
+        /* Bandwidth expansion for analysis filter shaping */\r
+        SKP_assert( BWExp1_Q16 <= SKP_FIX_CONST( 1.0, 16 ) );\r
+        SKP_Silk_bwexpander_32( AR1_Q24, psEnc->sCmn.shapingLPCOrder, BWExp1_Q16 );\r
+\r
         /* Ratio of prediction gains, in energy domain */\r
         SKP_Silk_LPC_inverse_pred_gain_Q24( &pre_nrg_Q30, AR2_Q24, psEnc->sCmn.shapingLPCOrder );\r
         SKP_Silk_LPC_inverse_pred_gain_Q24( &nrg,         AR1_Q24, psEnc->sCmn.shapingLPCOrder );\r
         /* Ratio of prediction gains, in energy domain */\r
         SKP_Silk_LPC_inverse_pred_gain_Q24( &pre_nrg_Q30, AR2_Q24, psEnc->sCmn.shapingLPCOrder );\r
         SKP_Silk_LPC_inverse_pred_gain_Q24( &nrg,         AR1_Q24, psEnc->sCmn.shapingLPCOrder );\r
@@ -313,12 +342,8 @@ void SKP_Silk_noise_shape_analysis_FIX(
         pre_nrg_Q30 = SKP_LSHIFT32( SKP_SMULWB( pre_nrg_Q30, SKP_FIX_CONST( 0.7, 15 ) ), 1 );\r
         psEncCtrl->GainsPre_Q14[ k ] = ( SKP_int ) SKP_FIX_CONST( 0.3, 14 ) + SKP_DIV32_varQ( pre_nrg_Q30, nrg, 14 );\r
 \r
         pre_nrg_Q30 = SKP_LSHIFT32( SKP_SMULWB( pre_nrg_Q30, SKP_FIX_CONST( 0.7, 15 ) ), 1 );\r
         psEncCtrl->GainsPre_Q14[ k ] = ( SKP_int ) SKP_FIX_CONST( 0.3, 14 ) + SKP_DIV32_varQ( pre_nrg_Q30, nrg, 14 );\r
 \r
-        /* Convert to monic warped prediction coefficients */\r
-        warped_true2monic_coefs( AR1_Q24, psEncCtrl->sCmn.warping_Q16, psEnc->sCmn.shapingLPCOrder );\r
-        warped_true2monic_coefs( AR2_Q24, psEncCtrl->sCmn.warping_Q16, psEnc->sCmn.shapingLPCOrder );\r
-\r
-        /* Limit absolute values */\r
-        limit_warped_coefs( AR2_Q24, AR1_Q24, psEncCtrl->sCmn.warping_Q16, SKP_FIX_CONST( 3.999, 24 ), psEnc->sCmn.shapingLPCOrder );\r
+        /* Convert to monic warped prediction coefficients and limit absolute values */\r
+        limit_warped_coefs( AR2_Q24, AR1_Q24, warping_Q16, SKP_FIX_CONST( 3.999, 24 ), psEnc->sCmn.shapingLPCOrder );\r
 \r
         /* Convert from Q24 to Q13 and store in int16 */\r
         for( i = 0; i < psEnc->sCmn.shapingLPCOrder; i++ ) {\r
 \r
         /* Convert from Q24 to Q13 and store in int16 */\r
         for( i = 0; i < psEnc->sCmn.shapingLPCOrder; i++ ) {\r
@@ -331,9 +356,9 @@ void SKP_Silk_noise_shape_analysis_FIX(
     /* Gain tweaking */\r
     /*****************/\r
     /* Increase gains during low speech activity and put lower limit on gains */\r
     /* Gain tweaking */\r
     /*****************/\r
     /* Increase gains during low speech activity and put lower limit on gains */\r
-    gain_mult_Q16 = SKP_Silk_log2lin( -SKP_SMLAWB( -16 << 7, SNR_adj_dB_Q7,           10486 ) ); // 10486_Q16 = 0.16_Q0\r
-    gain_add_Q16  = SKP_Silk_log2lin(  SKP_SMLAWB(  16 << 7, NOISE_FLOOR_dB_Q7,       10486 ) ); // 10486_Q16 = 0.16_Q0\r
-    tmp32         = SKP_Silk_log2lin(  SKP_SMLAWB(  16 << 7, RELATIVE_MIN_GAIN_dB_Q7, 10486 ) ); // 10486_Q16 = 0.16_Q0\r
+    gain_mult_Q16 = SKP_Silk_log2lin( -SKP_SMLAWB( -SKP_FIX_CONST( 16.0, 7 ), SNR_adj_dB_Q7,                            SKP_FIX_CONST( 0.16, 16 ) ) );\r
+    gain_add_Q16  = SKP_Silk_log2lin(  SKP_SMLAWB(  SKP_FIX_CONST( 16.0, 7 ), SKP_FIX_CONST( NOISE_FLOOR_dB, 7 ),       SKP_FIX_CONST( 0.16, 16 ) ) );\r
+    tmp32         = SKP_Silk_log2lin(  SKP_SMLAWB(  SKP_FIX_CONST( 16.0, 7 ), SKP_FIX_CONST( RELATIVE_MIN_GAIN_dB, 7 ), SKP_FIX_CONST( 0.16, 16 ) ) );\r
     tmp32 = SKP_SMULWW( psEnc->avgGain_Q16, tmp32 );\r
     gain_add_Q16 = SKP_ADD_SAT32( gain_add_Q16, tmp32 );\r
     SKP_assert( gain_mult_Q16 >= 0 );\r
     tmp32 = SKP_SMULWW( psEnc->avgGain_Q16, tmp32 );\r
     gain_add_Q16 = SKP_ADD_SAT32( gain_add_Q16, tmp32 );\r
     SKP_assert( gain_mult_Q16 >= 0 );\r
@@ -349,32 +374,12 @@ void SKP_Silk_noise_shape_analysis_FIX(
             psEnc->avgGain_Q16, \r
             SKP_SMULWB(\r
                 psEncCtrl->Gains_Q16[ k ] - psEnc->avgGain_Q16, \r
             psEnc->avgGain_Q16, \r
             SKP_SMULWB(\r
                 psEncCtrl->Gains_Q16[ k ] - psEnc->avgGain_Q16, \r
-                SKP_RSHIFT_ROUND( SKP_SMULBB( psEnc->speech_activity_Q8, GAIN_SMOOTHING_COEF_Q10 ), 2 ) \r
+                SKP_RSHIFT_ROUND( SKP_SMULBB( psEnc->speech_activity_Q8, SKP_FIX_CONST( GAIN_SMOOTHING_COEF, 10 ) ), 2 ) \r
             ) );\r
     }\r
 \r
             ) );\r
     }\r
 \r
-    /************************************************/\r
-    /* Decrease level during fricatives (de-essing) */\r
-    /************************************************/\r
-    gain_mult_Q16 = ( 1 << 16 ) + SKP_RSHIFT_ROUND( SKP_MLA( INPUT_TILT_Q26, psEncCtrl->coding_quality_Q14, HIGH_RATE_INPUT_TILT_Q12 ), 10 );\r
-\r
-    if( psEncCtrl->input_tilt_Q15 <= 0 && psEncCtrl->sCmn.sigtype == SIG_TYPE_UNVOICED ) {\r
-        if( psEnc->sCmn.fs_kHz == 24 ) {\r
-            SKP_int32 essStrength_Q15 = SKP_SMULWW( -psEncCtrl->input_tilt_Q15, \r
-                SKP_SMULBB( psEnc->speech_activity_Q8, ( 1 << 8 ) - psEncCtrl->sparseness_Q8 ) );\r
-            tmp32 = SKP_Silk_log2lin( ( 16 << 7 ) - SKP_SMULWB( essStrength_Q15, \r
-                SKP_SMULWB( DE_ESSER_COEF_SWB_dB_Q7, 20972 ) ) ); // 20972_Q17 = 0.16_Q0\r
-            gain_mult_Q16 = SKP_SMULWW( gain_mult_Q16, tmp32 );\r
-        } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
-            SKP_int32 essStrength_Q15 = SKP_SMULWW(-psEncCtrl->input_tilt_Q15, \r
-                SKP_SMULBB( psEnc->speech_activity_Q8, ( 1 << 8 ) - psEncCtrl->sparseness_Q8 ));\r
-            tmp32 = SKP_Silk_log2lin( ( 16 << 7 ) - SKP_SMULWB( essStrength_Q15, \r
-                SKP_SMULWB( DE_ESSER_COEF_WB_dB_Q7, 20972 ) ) ); // 20972_Q17 = 0.16_Q0\r
-            gain_mult_Q16 = SKP_SMULWW( gain_mult_Q16, tmp32 );\r
-        } else {\r
-            SKP_assert( psEnc->sCmn.fs_kHz == 12 || psEnc->sCmn.fs_kHz == 8 );\r
-        }\r
-    }\r
+    gain_mult_Q16 = SKP_FIX_CONST( 1.0, 16 ) + SKP_RSHIFT_ROUND( SKP_MLA( SKP_FIX_CONST( INPUT_TILT, 26 ), \r
+        psEncCtrl->coding_quality_Q14, SKP_FIX_CONST( HIGH_RATE_INPUT_TILT, 12 ) ), 10 );\r
 \r
     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
         psEncCtrl->GainsPre_Q14[ k ] = SKP_SMULWB( gain_mult_Q16, psEncCtrl->GainsPre_Q14[ k ] );\r
 \r
     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
         psEncCtrl->GainsPre_Q14[ k ] = SKP_SMULWB( gain_mult_Q16, psEncCtrl->GainsPre_Q14[ k ] );\r
@@ -384,47 +389,50 @@ void SKP_Silk_noise_shape_analysis_FIX(
     /* Control low-frequency shaping and noise tilt */\r
     /************************************************/\r
     /* Less low frequency shaping for noisy inputs */\r
     /* Control low-frequency shaping and noise tilt */\r
     /************************************************/\r
     /* Less low frequency shaping for noisy inputs */\r
-    strength_Q16 = SKP_MUL( LOW_FREQ_SHAPING_Q0, ( 1 << 16 ) + SKP_SMULBB( LOW_QUALITY_LOW_FREQ_SHAPING_DECR_Q1, psEncCtrl->input_quality_bands_Q15[ 0 ] - ( 1 << 15 ) ) );\r
+    strength_Q16 = SKP_MUL( SKP_FIX_CONST( LOW_FREQ_SHAPING, 0 ), SKP_FIX_CONST( 1.0, 16 ) + \r
+        SKP_SMULBB( SKP_FIX_CONST( LOW_QUALITY_LOW_FREQ_SHAPING_DECR, 1 ), psEncCtrl->input_quality_bands_Q15[ 0 ] - SKP_FIX_CONST( 1.0, 15 ) ) );\r
     if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         /* Reduce low frequencies quantization noise for periodic signals, depending on pitch lag */\r
         /*f = 400; freqz([1, -0.98 + 2e-4 * f], [1, -0.97 + 7e-4 * f], 2^12, Fs); axis([0, 1000, -10, 1])*/\r
     if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         /* Reduce low frequencies quantization noise for periodic signals, depending on pitch lag */\r
         /*f = 400; freqz([1, -0.98 + 2e-4 * f], [1, -0.97 + 7e-4 * f], 2^12, Fs); axis([0, 1000, -10, 1])*/\r
-        SKP_int fs_kHz_inv = SKP_DIV32_16( 3277, psEnc->sCmn.fs_kHz );      // 0.2_Q0 = 3277_Q14\r
+        SKP_int fs_kHz_inv = SKP_DIV32_16( SKP_FIX_CONST( 0.2, 14 ), psEnc->sCmn.fs_kHz );\r
         for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
         for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
-            b_Q14 = fs_kHz_inv + SKP_DIV32_16( ( 3 << 14 ), psEncCtrl->sCmn.pitchL[ k ] ); \r
+            b_Q14 = fs_kHz_inv + SKP_DIV32_16( SKP_FIX_CONST( 3.0, 14 ), psEncCtrl->sCmn.pitchL[ k ] ); \r
             /* Pack two coefficients in one int32 */\r
             /* Pack two coefficients in one int32 */\r
-            psEncCtrl->LF_shp_Q14[ k ]  = SKP_LSHIFT( ( 1 << 14 ) - b_Q14 - SKP_SMULWB( strength_Q16, b_Q14 ), 16 );\r
-            psEncCtrl->LF_shp_Q14[ k ] |= (SKP_uint16)( b_Q14 - ( 1 << 14 ) );\r
+            psEncCtrl->LF_shp_Q14[ k ]  = SKP_LSHIFT( SKP_FIX_CONST( 1.0, 14 ) - b_Q14 - SKP_SMULWB( strength_Q16, b_Q14 ), 16 );\r
+            psEncCtrl->LF_shp_Q14[ k ] |= (SKP_uint16)( b_Q14 - SKP_FIX_CONST( 1.0, 14 ) );\r
         }\r
         }\r
-        SKP_assert( HARM_HP_NOISE_COEF_Q24 < ( 1 << 23 ) ); // Guarantees that second argument to SMULWB() is within range of an SKP_int16\r
-        Tilt_Q16 = - HP_NOISE_COEF_Q16 - \r
-            SKP_SMULWB( ( 1 << 16 ) - HP_NOISE_COEF_Q16, SKP_SMULWB( HARM_HP_NOISE_COEF_Q24, psEnc->speech_activity_Q8 ) );\r
+        SKP_assert( SKP_FIX_CONST( HARM_HP_NOISE_COEF, 24 ) < SKP_FIX_CONST( 0.5, 24 ) ); // Guarantees that second argument to SMULWB() is within range of an SKP_int16\r
+        Tilt_Q16 = - SKP_FIX_CONST( HP_NOISE_COEF, 16 ) - \r
+            SKP_SMULWB( SKP_FIX_CONST( 1.0, 16 ) - SKP_FIX_CONST( HP_NOISE_COEF, 16 ), \r
+                SKP_SMULWB( SKP_FIX_CONST( HARM_HP_NOISE_COEF, 24 ), psEnc->speech_activity_Q8 ) );\r
     } else {\r
         b_Q14 = SKP_DIV32_16( 21299, psEnc->sCmn.fs_kHz ); // 1.3_Q0 = 21299_Q14\r
         /* Pack two coefficients in one int32 */\r
     } else {\r
         b_Q14 = SKP_DIV32_16( 21299, psEnc->sCmn.fs_kHz ); // 1.3_Q0 = 21299_Q14\r
         /* Pack two coefficients in one int32 */\r
-        psEncCtrl->LF_shp_Q14[ 0 ]  = SKP_LSHIFT( ( 1 << 14 ) - b_Q14 - SKP_SMULWB( strength_Q16, SKP_SMULWB( 39322, b_Q14 ) ), 16 ); // 0.6_Q0 = 39322_Q16\r
-        psEncCtrl->LF_shp_Q14[ 0 ] |= (SKP_uint16)( b_Q14 - ( 1 << 14 ) );\r
+        psEncCtrl->LF_shp_Q14[ 0 ]  = SKP_LSHIFT( SKP_FIX_CONST( 1.0, 14 ) - b_Q14 - \r
+            SKP_SMULWB( strength_Q16, SKP_SMULWB( SKP_FIX_CONST( 0.6, 16 ), b_Q14 ) ), 16 );\r
+        psEncCtrl->LF_shp_Q14[ 0 ] |= (SKP_uint16)( b_Q14 - SKP_FIX_CONST( 1.0, 14 ) );\r
         for( k = 1; k < psEnc->sCmn.nb_subfr; k++ ) {\r
         for( k = 1; k < psEnc->sCmn.nb_subfr; k++ ) {\r
-            psEncCtrl->LF_shp_Q14[ k ] = psEncCtrl->LF_shp_Q14[ k - 1 ];\r
+            psEncCtrl->LF_shp_Q14[ k ] = psEncCtrl->LF_shp_Q14[ 0 ];\r
         }\r
         }\r
-        Tilt_Q16 = -HP_NOISE_COEF_Q16;\r
+        Tilt_Q16 = -SKP_FIX_CONST( HP_NOISE_COEF, 16 );\r
     }\r
 \r
     /****************************/\r
     /* HARMONIC SHAPING CONTROL */\r
     /****************************/\r
     /* Control boosting of harmonic frequencies */\r
     }\r
 \r
     /****************************/\r
     /* HARMONIC SHAPING CONTROL */\r
     /****************************/\r
     /* Control boosting of harmonic frequencies */\r
-    HarmBoost_Q16 = SKP_SMULWB( SKP_SMULWB( ( 1 << 17 ) - SKP_LSHIFT( psEncCtrl->coding_quality_Q14, 3 ), \r
-        psEnc->LTPCorr_Q15 ), LOW_RATE_HARMONIC_BOOST_Q16 );\r
+    HarmBoost_Q16 = SKP_SMULWB( SKP_SMULWB( SKP_FIX_CONST( 1.0, 17 ) - SKP_LSHIFT( psEncCtrl->coding_quality_Q14, 3 ), \r
+        psEnc->LTPCorr_Q15 ), SKP_FIX_CONST( LOW_RATE_HARMONIC_BOOST, 16 ) );\r
 \r
     /* More harmonic boost for noisy input signals */\r
     HarmBoost_Q16 = SKP_SMLAWB( HarmBoost_Q16, \r
 \r
     /* More harmonic boost for noisy input signals */\r
     HarmBoost_Q16 = SKP_SMLAWB( HarmBoost_Q16, \r
-        ( 1 << 16 ) - SKP_LSHIFT( psEncCtrl->input_quality_Q14, 2 ), LOW_INPUT_QUALITY_HARMONIC_BOOST_Q16 );\r
+        SKP_FIX_CONST( 1.0, 16 ) - SKP_LSHIFT( psEncCtrl->input_quality_Q14, 2 ), SKP_FIX_CONST( LOW_INPUT_QUALITY_HARMONIC_BOOST, 16 ) );\r
 \r
     if( USE_HARM_SHAPING && psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         /* More harmonic noise shaping for high bitrates or noisy input */\r
 \r
     if( USE_HARM_SHAPING && psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         /* More harmonic noise shaping for high bitrates or noisy input */\r
-        HarmShapeGain_Q16 = SKP_SMLAWB( HARMONIC_SHAPING_Q16\r
-                ( 1 << 16 ) - SKP_SMULWB( ( 1 << 18 ) - SKP_LSHIFT( psEncCtrl->coding_quality_Q14, 4 ),\r
-                psEncCtrl->input_quality_Q14 ), HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING_Q16 );\r
+        HarmShapeGain_Q16 = SKP_SMLAWB( SKP_FIX_CONST( HARMONIC_SHAPING, 16 )\r
+                SKP_FIX_CONST( 1.0, 16 ) - SKP_SMULWB( SKP_FIX_CONST( 1.0, 18 ) - SKP_LSHIFT( psEncCtrl->coding_quality_Q14, 4 ),\r
+                psEncCtrl->input_quality_Q14 ), SKP_FIX_CONST( HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING, 16 ) );\r
 \r
         /* Less harmonic noise shaping for less periodic signals */\r
         HarmShapeGain_Q16 = SKP_SMULWB( SKP_LSHIFT( HarmShapeGain_Q16, 1 ), \r
 \r
         /* Less harmonic noise shaping for less periodic signals */\r
         HarmShapeGain_Q16 = SKP_SMULWB( SKP_LSHIFT( HarmShapeGain_Q16, 1 ), \r
@@ -438,11 +446,11 @@ void SKP_Silk_noise_shape_analysis_FIX(
     /*************************/\r
     for( k = 0; k < MAX_NB_SUBFR; k++ ) {\r
         psShapeSt->HarmBoost_smth_Q16 =\r
     /*************************/\r
     for( k = 0; k < MAX_NB_SUBFR; k++ ) {\r
         psShapeSt->HarmBoost_smth_Q16 =\r
-            SKP_SMLAWB( psShapeSt->HarmBoost_smth_Q16,     HarmBoost_Q16     - psShapeSt->HarmBoost_smth_Q16,     SUBFR_SMTH_COEF_Q16 );\r
+            SKP_SMLAWB( psShapeSt->HarmBoost_smth_Q16,     HarmBoost_Q16     - psShapeSt->HarmBoost_smth_Q16,     SKP_FIX_CONST( SUBFR_SMTH_COEF, 16 ) );\r
         psShapeSt->HarmShapeGain_smth_Q16 =\r
         psShapeSt->HarmShapeGain_smth_Q16 =\r
-            SKP_SMLAWB( psShapeSt->HarmShapeGain_smth_Q16, HarmShapeGain_Q16 - psShapeSt->HarmShapeGain_smth_Q16, SUBFR_SMTH_COEF_Q16 );\r
+            SKP_SMLAWB( psShapeSt->HarmShapeGain_smth_Q16, HarmShapeGain_Q16 - psShapeSt->HarmShapeGain_smth_Q16, SKP_FIX_CONST( SUBFR_SMTH_COEF, 16 ) );\r
         psShapeSt->Tilt_smth_Q16 =\r
         psShapeSt->Tilt_smth_Q16 =\r
-            SKP_SMLAWB( psShapeSt->Tilt_smth_Q16,          Tilt_Q16          - psShapeSt->Tilt_smth_Q16,          SUBFR_SMTH_COEF_Q16 );\r
+            SKP_SMLAWB( psShapeSt->Tilt_smth_Q16,          Tilt_Q16          - psShapeSt->Tilt_smth_Q16,          SKP_FIX_CONST( SUBFR_SMTH_COEF, 16 ) );\r
 \r
         psEncCtrl->HarmBoost_Q14[ k ]     = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->HarmBoost_smth_Q16,     2 );\r
         psEncCtrl->HarmShapeGain_Q14[ k ] = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->HarmShapeGain_smth_Q16, 2 );\r
 \r
         psEncCtrl->HarmBoost_Q14[ k ]     = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->HarmBoost_smth_Q16,     2 );\r
         psEncCtrl->HarmShapeGain_Q14[ k ] = ( SKP_int )SKP_RSHIFT_ROUND( psShapeSt->HarmShapeGain_smth_Q16, 2 );\r
index 1b7dcd2..c0a2272 100644 (file)
@@ -26,7 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
-#include "SKP_Silk_perceptual_parameters_FIX.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 /* SKP_Silk_prefilter. Prefilter for finding Quantizer input signal */\r
 SKP_INLINE void SKP_Silk_prefilt_FIX(\r
 \r
 /* SKP_Silk_prefilter. Prefilter for finding Quantizer input signal */\r
 SKP_INLINE void SKP_Silk_prefilt_FIX(\r
@@ -120,15 +120,15 @@ void SKP_Silk_prefilter_FIX(
         AR1_shp_Q13 = &psEncCtrl->AR1_Q13[   k * MAX_SHAPE_LPC_ORDER ];\r
 \r
         /* Short term FIR filtering*/\r
         AR1_shp_Q13 = &psEncCtrl->AR1_Q13[   k * MAX_SHAPE_LPC_ORDER ];\r
 \r
         /* Short term FIR filtering*/\r
-        SKP_Silk_warped_LPC_analysis_filter_FIX( P->sAR_shp1, st_res, AR1_shp_Q13, px, \r
-            psEncCtrl->sCmn.warping_Q16, psEnc->sCmn.subfr_length, psEnc->sCmn.shapingLPCOrder );\r
+        SKP_Silk_warped_LPC_analysis_filter_FIX( P->sAR_shp, st_res, AR1_shp_Q13, px, \r
+            psEnc->sCmn.warping_Q16, psEnc->sCmn.subfr_length, psEnc->sCmn.shapingLPCOrder );\r
 \r
         /* reduce (mainly) low frequencies during harmonic emphasis */\r
         B_Q12[ 0 ] = SKP_RSHIFT_ROUND( psEncCtrl->GainsPre_Q14[ k ], 2 );\r
 \r
         /* reduce (mainly) low frequencies during harmonic emphasis */\r
         B_Q12[ 0 ] = SKP_RSHIFT_ROUND( psEncCtrl->GainsPre_Q14[ k ], 2 );\r
-        tmp_32 = SKP_SMLABB( INPUT_TILT_Q26, psEncCtrl->HarmBoost_Q14[ k ], HarmShapeGain_Q12 ); /* Q26 */\r
-        tmp_32 = SKP_SMLABB( tmp_32, psEncCtrl->coding_quality_Q14, HIGH_RATE_INPUT_TILT_Q12 );  /* Q26 */\r
-        tmp_32 = SKP_SMULWB( tmp_32, -psEncCtrl->GainsPre_Q14[ k ] );                            /* Q24 */\r
-        tmp_32 = SKP_RSHIFT_ROUND( tmp_32, 12 );                                                 /* Q12 */\r
+        tmp_32 = SKP_SMLABB( SKP_FIX_CONST( INPUT_TILT, 26 ), psEncCtrl->HarmBoost_Q14[ k ], HarmShapeGain_Q12 );   /* Q26 */\r
+        tmp_32 = SKP_SMLABB( tmp_32, psEncCtrl->coding_quality_Q14, SKP_FIX_CONST( HIGH_RATE_INPUT_TILT, 12 ) );    /* Q26 */\r
+        tmp_32 = SKP_SMULWB( tmp_32, -psEncCtrl->GainsPre_Q14[ k ] );                                               /* Q24 */\r
+        tmp_32 = SKP_RSHIFT_ROUND( tmp_32, 12 );                                                                    /* Q12 */\r
         B_Q12[ 1 ]= SKP_SAT16( tmp_32 );\r
 \r
         x_filt_Q12[ 0 ] = SKP_SMLABB( SKP_SMULBB( st_res[ 0 ], B_Q12[ 0 ] ), P->sHarmHP, B_Q12[ 1 ] );\r
         B_Q12[ 1 ]= SKP_SAT16( tmp_32 );\r
 \r
         x_filt_Q12[ 0 ] = SKP_SMLABB( SKP_SMULBB( st_res[ 0 ], B_Q12[ 0 ] ), P->sHarmHP, B_Q12[ 1 ] );\r
@@ -165,10 +165,10 @@ SKP_INLINE void SKP_Silk_prefilt_FIX(
     SKP_int16 *LTP_shp_buf;\r
 \r
     /* To speed up use temp variables instead of using the struct */\r
     SKP_int16 *LTP_shp_buf;\r
 \r
     /* To speed up use temp variables instead of using the struct */\r
-    LTP_shp_buf     = P->sLTP_shp1;\r
-    LTP_shp_buf_idx = P->sLTP_shp_buf_idx1;\r
-    sLF_AR_shp_Q12  = P->sLF_AR_shp1_Q12;\r
-    sLF_MA_shp_Q12  = P->sLF_MA_shp1_Q12;\r
+    LTP_shp_buf     = P->sLTP_shp;\r
+    LTP_shp_buf_idx = P->sLTP_shp_buf_idx;\r
+    sLF_AR_shp_Q12  = P->sLF_AR_shp_Q12;\r
+    sLF_MA_shp_Q12  = P->sLF_MA_shp_Q12;\r
 \r
     for( i = 0; i < length; i++ ) {\r
         if( lag > 0 ) {\r
 \r
     for( i = 0; i < length; i++ ) {\r
         if( lag > 0 ) {\r
@@ -195,7 +195,7 @@ SKP_INLINE void SKP_Silk_prefilt_FIX(
     }\r
 \r
     /* Copy temp variable back to state */\r
     }\r
 \r
     /* Copy temp variable back to state */\r
-    P->sLF_AR_shp1_Q12   = sLF_AR_shp_Q12;\r
-    P->sLF_MA_shp1_Q12   = sLF_MA_shp_Q12;\r
-    P->sLTP_shp_buf_idx1 = LTP_shp_buf_idx;\r
+    P->sLF_AR_shp_Q12   = sLF_AR_shp_Q12;\r
+    P->sLF_MA_shp_Q12   = sLF_MA_shp_Q12;\r
+    P->sLTP_shp_buf_idx = LTP_shp_buf_idx;\r
 }\r
 }\r
index 272fd29..301687a 100644 (file)
@@ -26,6 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 /* Processing of gains */\r
 void SKP_Silk_process_gains_FIX(\r
 \r
 /* Processing of gains */\r
 void SKP_Silk_process_gains_FIX(\r
@@ -35,21 +36,20 @@ void SKP_Silk_process_gains_FIX(
 {\r
     SKP_Silk_shape_state_FIX    *psShapeSt = &psEnc->sShape;\r
     SKP_int     k;\r
 {\r
     SKP_Silk_shape_state_FIX    *psShapeSt = &psEnc->sShape;\r
     SKP_int     k;\r
-    SKP_int32   s_Q16, InvMaxSqrVal_Q16, gain, gain_squared, ResNrg, ResNrgPart;\r
+    SKP_int32   s_Q16, InvMaxSqrVal_Q16, gain, gain_squared, ResNrg, ResNrgPart, quant_offset_Q10;\r
 \r
     /* Gain reduction when LTP coding gain is high */\r
     if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         /*s = -0.5f * SKP_sigmoid( 0.25f * ( psEncCtrl->LTPredCodGain - 12.0f ) ); */\r
 \r
     /* Gain reduction when LTP coding gain is high */\r
     if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         /*s = -0.5f * SKP_sigmoid( 0.25f * ( psEncCtrl->LTPredCodGain - 12.0f ) ); */\r
-        s_Q16 = -SKP_Silk_sigm_Q15( SKP_RSHIFT_ROUND( psEncCtrl->LTPredCodGain_Q7 - (12 << 7), 4 ) );\r
+        s_Q16 = -SKP_Silk_sigm_Q15( SKP_RSHIFT_ROUND( psEncCtrl->LTPredCodGain_Q7 - SKP_FIX_CONST( 12.0, 7 ), 4 ) );\r
         for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
             psEncCtrl->Gains_Q16[ k ] = SKP_SMLAWB( psEncCtrl->Gains_Q16[ k ], psEncCtrl->Gains_Q16[ k ], s_Q16 );\r
         }\r
     }\r
 \r
     /* Limit the quantized signal */\r
         for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
             psEncCtrl->Gains_Q16[ k ] = SKP_SMLAWB( psEncCtrl->Gains_Q16[ k ], psEncCtrl->Gains_Q16[ k ], s_Q16 );\r
         }\r
     }\r
 \r
     /* Limit the quantized signal */\r
-    /*  69 = 21.0f + 16/0.33    */\r
     InvMaxSqrVal_Q16 = SKP_DIV32_16( SKP_Silk_log2lin( \r
     InvMaxSqrVal_Q16 = SKP_DIV32_16( SKP_Silk_log2lin( \r
-        SKP_SMULWB( (69 << 7) - psEncCtrl->current_SNR_dB_Q7, SKP_FIX_CONST( 0.33, 16 )) ), psEnc->sCmn.subfr_length );\r
+        SKP_SMULWB( SKP_FIX_CONST( 70.0, 7 ) - psEncCtrl->current_SNR_dB_Q7, SKP_FIX_CONST( 0.33, 16 ) ) ), psEnc->sCmn.subfr_length );\r
 \r
     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
         /* Soft limit on ratio residual energy and squared gains */\r
 \r
     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {\r
         /* Soft limit on ratio residual energy and squared gains */\r
@@ -74,10 +74,10 @@ void SKP_Silk_process_gains_FIX(
             /* recalculate with higher precision */\r
             gain_squared = SKP_SMLAWW( SKP_LSHIFT( ResNrgPart, 16 ), gain, gain );\r
             SKP_assert( gain_squared > 0 );\r
             /* recalculate with higher precision */\r
             gain_squared = SKP_SMLAWW( SKP_LSHIFT( ResNrgPart, 16 ), gain, gain );\r
             SKP_assert( gain_squared > 0 );\r
-            gain = SKP_Silk_SQRT_APPROX( gain_squared );                  /* Q8   */\r
+            gain = SKP_Silk_SQRT_APPROX( gain_squared );                    /* Q8   */\r
             psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( gain, 8 );        /* Q16  */\r
         } else {\r
             psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( gain, 8 );        /* Q16  */\r
         } else {\r
-            gain = SKP_Silk_SQRT_APPROX( gain_squared );                  /* Q0   */\r
+            gain = SKP_Silk_SQRT_APPROX( gain_squared );                    /* Q0   */\r
             psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( gain, 16 );       /* Q16  */\r
         }\r
     }\r
             psEncCtrl->Gains_Q16[ k ] = SKP_LSHIFT_SAT32( gain, 16 );       /* Q16  */\r
         }\r
     }\r
@@ -87,7 +87,7 @@ void SKP_Silk_process_gains_FIX(
         &psShapeSt->LastGainIndex, psEnc->sCmn.nFramesInPayloadBuf, psEnc->sCmn.nb_subfr );\r
     /* Set quantizer offset for voiced signals. Larger offset when LTP coding gain is low or tilt is high (ie low-pass) */\r
     if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
         &psShapeSt->LastGainIndex, psEnc->sCmn.nFramesInPayloadBuf, psEnc->sCmn.nb_subfr );\r
     /* Set quantizer offset for voiced signals. Larger offset when LTP coding gain is low or tilt is high (ie low-pass) */\r
     if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
-        if( psEncCtrl->LTPredCodGain_Q7 + SKP_RSHIFT( psEncCtrl->input_tilt_Q15, 8 ) > ( 1 << 7 ) ) {\r
+        if( psEncCtrl->LTPredCodGain_Q7 + SKP_RSHIFT( psEncCtrl->input_tilt_Q15, 8 ) > SKP_FIX_CONST( 1.0, 7 ) ) {\r
             psEncCtrl->sCmn.QuantOffsetType = 0;\r
         } else {\r
             psEncCtrl->sCmn.QuantOffsetType = 1;\r
             psEncCtrl->sCmn.QuantOffsetType = 0;\r
         } else {\r
             psEncCtrl->sCmn.QuantOffsetType = 1;\r
@@ -95,19 +95,14 @@ void SKP_Silk_process_gains_FIX(
     }\r
 \r
     /* Quantizer boundary adjustment */\r
     }\r
 \r
     /* Quantizer boundary adjustment */\r
-    if( psEncCtrl->sCmn.sigtype == SIG_TYPE_VOICED ) {\r
-        psEncCtrl->Lambda_Q10 = SKP_FIX_CONST( 1.2, 10 )\r
-                  - SKP_SMULWB( SKP_FIX_CONST( 0.4, 18 ), psEnc->speech_activity_Q8       )\r
-                  - SKP_SMULWB( SKP_FIX_CONST( 0.3, 12 ), psEncCtrl->input_quality_Q14    )\r
-                  + SKP_SMULBB( SKP_FIX_CONST( 0.2, 10 ), psEncCtrl->sCmn.QuantOffsetType )\r
-                  - SKP_SMULWB( SKP_FIX_CONST( 0.1, 12 ), psEncCtrl->coding_quality_Q14   );\r
-    } else {\r
-        psEncCtrl->Lambda_Q10 = SKP_FIX_CONST( 1.2, 10 )\r
-                  - SKP_SMULWB( SKP_FIX_CONST( 0.4, 18 ), psEnc->speech_activity_Q8       )\r
-                  - SKP_SMULWB( SKP_FIX_CONST( 0.4, 12 ), psEncCtrl->input_quality_Q14    )\r
-                  + SKP_SMULBB( SKP_FIX_CONST( 0.4, 10 ), psEncCtrl->sCmn.QuantOffsetType )\r
-                  - SKP_SMULWB( SKP_FIX_CONST( 0.1, 12 ), psEncCtrl->coding_quality_Q14   );\r
-    }\r
-    SKP_assert( psEncCtrl->Lambda_Q10 >= 0 );\r
+    quant_offset_Q10 = SKP_Silk_Quantization_Offsets_Q10[ psEncCtrl->sCmn.sigtype ][ psEncCtrl->sCmn.QuantOffsetType ];\r
+    psEncCtrl->Lambda_Q10 = SKP_FIX_CONST( LAMBDA_OFFSET, 10 )\r
+                          + SKP_SMULBB( SKP_FIX_CONST( LAMBDA_DELAYED_DECISIONS, 10 ), psEnc->sCmn.nStatesDelayedDecision )\r
+                          + SKP_SMULWB( SKP_FIX_CONST( LAMBDA_SPEECH_ACT,        18 ), psEnc->speech_activity_Q8          )\r
+                          + SKP_SMULWB( SKP_FIX_CONST( LAMBDA_INPUT_QUALITY,     12 ), psEncCtrl->input_quality_Q14       )\r
+                          + SKP_SMULWB( SKP_FIX_CONST( LAMBDA_CODING_QUALITY,    12 ), psEncCtrl->coding_quality_Q14      )\r
+                          + SKP_SMULWB( SKP_FIX_CONST( LAMBDA_QUANT_OFFSET,      16 ), quant_offset_Q10                   );\r
+\r
+    SKP_assert( psEncCtrl->Lambda_Q10 > 0 );\r
     SKP_assert( psEncCtrl->Lambda_Q10 < SKP_FIX_CONST( 2, 10 ) );\r
 }\r
     SKP_assert( psEncCtrl->Lambda_Q10 < SKP_FIX_CONST( 2, 10 ) );\r
 }\r
index 23c0c9c..f61e9fb 100644 (file)
@@ -26,6 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 /*****************************/\r
 /* Internal function headers */\r
 \r
 /*****************************/\r
 /* Internal function headers */\r
@@ -120,7 +121,7 @@ SKP_INLINE void SKP_Silk_LDL_factorize_FIX(
     SKP_assert( M <= MAX_MATRIX_SIZE );\r
 \r
     status = 1;\r
     SKP_assert( M <= MAX_MATRIX_SIZE );\r
 \r
     status = 1;\r
-    diag_min_value = SKP_max_32( SKP_SMMUL( SKP_ADD_SAT32( A[ 0 ], A[ SKP_SMULBB( M, M ) - 1 ] ), FIND_LTP_COND_FAC_Q31 ), 1 << 9 );\r
+    diag_min_value = SKP_max_32( SKP_SMMUL( SKP_ADD_SAT32( A[ 0 ], A[ SKP_SMULBB( M, M ) - 1 ] ), SKP_FIX_CONST( FIND_LTP_COND_FAC, 31 ) ), 1 << 9 );\r
     for( loop_count = 0; loop_count < M && status == 1; loop_count++ ) {\r
         status = 0;\r
         for( j = 0; j < M; j++ ) {\r
     for( loop_count = 0; loop_count < M && status == 1; loop_count++ ) {\r
         status = 0;\r
         for( j = 0; j < M; j++ ) {\r
index 8e2eb2c..261610c 100644 (file)
@@ -29,7 +29,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #define SKP_SILK_STRUCTS_FIX_H\r
 \r
 #include "SKP_Silk_typedef.h"\r
 #define SKP_SILK_STRUCTS_FIX_H\r
 \r
 #include "SKP_Silk_typedef.h"\r
-#include "SKP_Silk_define_FIX.h"\r
 #include "SKP_Silk_main.h"\r
 #include "SKP_Silk_structs.h"\r
 \r
 #include "SKP_Silk_main.h"\r
 #include "SKP_Silk_structs.h"\r
 \r
@@ -52,17 +51,11 @@ typedef struct {
 /* Prefilter state              */\r
 /********************************/\r
 typedef struct {\r
 /* Prefilter state              */\r
 /********************************/\r
 typedef struct {\r
-    SKP_int16   sLTP_shp1[ LTP_BUF_LENGTH ];\r
-    SKP_int32   sAR_shp1[ MAX_SHAPE_LPC_ORDER + 1 ];\r
-    SKP_int32   sAR_shp2_Q14[ MAX_SHAPE_LPC_ORDER ];\r
-    SKP_int16   sLTP_shp2_FIX[ LTP_BUF_LENGTH ];\r
-    SKP_int     sLTP_shp_buf_idx1;\r
-    SKP_int     sAR_shp_buf_idx2;\r
-    SKP_int     sLTP_shp_buf_idx2;\r
-    SKP_int32   sLF_AR_shp1_Q12;\r
-    SKP_int32   sLF_MA_shp1_Q12;\r
-    SKP_int32   sLF_AR_shp2_Q12;\r
-    SKP_int32   sLF_MA_shp2_Q12;\r
+    SKP_int16   sLTP_shp[ LTP_BUF_LENGTH ];\r
+    SKP_int32   sAR_shp[ MAX_SHAPE_LPC_ORDER + 1 ];\r
+    SKP_int     sLTP_shp_buf_idx;\r
+    SKP_int32   sLF_AR_shp_Q12;\r
+    SKP_int32   sLF_MA_shp_Q12;\r
     SKP_int     sHarmHP;\r
     SKP_int32   rand_seed;\r
     SKP_int     lagPrev;\r
     SKP_int     sHarmHP;\r
     SKP_int32   rand_seed;\r
     SKP_int     lagPrev;\r
@@ -96,7 +89,7 @@ typedef struct {
     SKP_Silk_nsq_state              sNSQ_LBRR;                      /* Noise Shape Quantizer State ( for low bitrate redundancy )           */\r
 \r
     /* Buffer for find pitch and noise shape analysis */\r
     SKP_Silk_nsq_state              sNSQ_LBRR;                      /* Noise Shape Quantizer State ( for low bitrate redundancy )           */\r
 \r
     /* Buffer for find pitch and noise shape analysis */\r
-    SKP_array_of_int16_4_byte_aligned( x_buf, 2 * MAX_FRAME_LENGTH + LA_SHAPE_MAX );    \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
     SKP_int                         mu_LTP_Q8;                      /* Rate-distortion tradeoff in LTP quantization                         */\r
     SKP_int32                       SNR_dB_Q7;                      /* Quality setting                                                      */\r
     SKP_int                         LTPCorr_Q15;                    /* Normalized correlation from pitch lag estimator                      */\r
     SKP_int                         mu_LTP_Q8;                      /* Rate-distortion tradeoff in LTP quantization                         */\r
     SKP_int32                       SNR_dB_Q7;                      /* Quality setting                                                      */\r
@@ -104,7 +97,6 @@ typedef struct {
     SKP_int32                       avgGain_Q16_one_bit_per_sample; /* average gain during active speech                                    */\r
     SKP_int                         BufferedInChannel_ms;           /* Simulated number of ms buffer because of exceeded TargetRate_bps     */\r
     SKP_int                         speech_activity_Q8;             /* Speech activity in Q8                                                */\r
     SKP_int32                       avgGain_Q16_one_bit_per_sample; /* average gain during active speech                                    */\r
     SKP_int                         BufferedInChannel_ms;           /* Simulated number of ms buffer because of exceeded TargetRate_bps     */\r
     SKP_int                         speech_activity_Q8;             /* Speech activity in Q8                                                */\r
-    SKP_int32                       pitchEstimationThreshold_Q16;   /* Threshold for pitch estimator                                        */\r
 \r
     /* Parameters For LTP scaling Control */\r
     SKP_int                         prevLTPredCodGain_Q7;\r
 \r
     /* Parameters For LTP scaling Control */\r
     SKP_int                         prevLTPredCodGain_Q7;\r
@@ -122,14 +114,14 @@ typedef struct {
 \r
     /* Prediction and coding parameters */\r
     SKP_int32                   Gains_Q16[ MAX_NB_SUBFR ];\r
 \r
     /* Prediction and coding parameters */\r
     SKP_int32                   Gains_Q16[ MAX_NB_SUBFR ];\r
-    SKP_array_of_int16_4_byte_aligned( PredCoef_Q12[ 2 ], MAX_LPC_ORDER );\r
+    SKP_DWORD_ALIGN SKP_int16   PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ];\r
     SKP_int16                   LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];\r
     SKP_int                     LTP_scale_Q14;\r
 \r
     /* Noise shaping parameters */\r
     /* Testing */\r
     SKP_int16                   LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];\r
     SKP_int                     LTP_scale_Q14;\r
 \r
     /* Noise shaping parameters */\r
     /* Testing */\r
-    SKP_array_of_int16_4_byte_aligned( AR1_Q13, MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER );\r
-    SKP_array_of_int16_4_byte_aligned( AR2_Q13, MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER );\r
+    SKP_DWORD_ALIGN SKP_int16 AR1_Q13[ MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER ];\r
+    SKP_DWORD_ALIGN SKP_int16 AR2_Q13[ MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER ];\r
     SKP_int32   LF_shp_Q14[        MAX_NB_SUBFR ];          /* Packs two int16 coefficients per int32 value             */\r
     SKP_int     GainsPre_Q14[      MAX_NB_SUBFR ];\r
     SKP_int     HarmBoost_Q14[     MAX_NB_SUBFR ];\r
     SKP_int32   LF_shp_Q14[        MAX_NB_SUBFR ];          /* Packs two int16 coefficients per int32 value             */\r
     SKP_int     GainsPre_Q14[      MAX_NB_SUBFR ];\r
     SKP_int     HarmBoost_Q14[     MAX_NB_SUBFR ];\r
@@ -143,6 +135,7 @@ typedef struct {
 \r
     /* measures */\r
     SKP_int     sparseness_Q8;\r
 \r
     /* measures */\r
     SKP_int     sparseness_Q8;\r
+    SKP_int32   predGain_Q16;\r
     SKP_int     LTPredCodGain_Q7;\r
     SKP_int     input_quality_bands_Q15[ VAD_N_BANDS ];\r
     SKP_int     input_tilt_Q15;\r
     SKP_int     LTPredCodGain_Q7;\r
     SKP_int     input_quality_bands_Q15[ VAD_N_BANDS ];\r
     SKP_int     input_tilt_Q15;\r
index 7bb4ae0..b9dabed 100644 (file)
@@ -28,41 +28,43 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "SKP_Silk_main_FIX.h"\r
 \r
 #define QC  10\r
 #include "SKP_Silk_main_FIX.h"\r
 \r
 #define QC  10\r
+#define QS  14\r
 \r
 /* Autocorrelations for a warped frequency axis */\r
 void SKP_Silk_warped_autocorrelation_FIX(\r
           SKP_int32                 *corr,              /* O    Result [order + 1]                      */\r
           SKP_int                   *scale,             /* O    Scaling of the correlation vector       */\r
     const SKP_int16                 *input,             /* I    Input data to correlate                 */\r
 \r
 /* Autocorrelations for a warped frequency axis */\r
 void SKP_Silk_warped_autocorrelation_FIX(\r
           SKP_int32                 *corr,              /* O    Result [order + 1]                      */\r
           SKP_int                   *scale,             /* O    Scaling of the correlation vector       */\r
     const SKP_int16                 *input,             /* I    Input data to correlate                 */\r
-    const SKP_int16                 warping_Q16,        /* I    Warping coefficient                     */\r
+    const SKP_int                   warping_Q16,        /* I    Warping coefficient                     */\r
     const SKP_int                   length,             /* I    Length of input                         */\r
     const SKP_int                   order               /* I    Correlation order (even)                */\r
 )\r
 {\r
     SKP_int   n, i, lsh;\r
     const SKP_int                   length,             /* I    Length of input                         */\r
     const SKP_int                   order               /* I    Correlation order (even)                */\r
 )\r
 {\r
     SKP_int   n, i, lsh;\r
-    SKP_int32 tmp1_Q16, tmp2_Q16;\r
-    SKP_int32 state_Q16[ MAX_SHAPE_LPC_ORDER + 1 ] = { 0 };\r
-    SKP_int64 corr_QC[   MAX_SHAPE_LPC_ORDER + 1 ] = { 0 };\r
+    SKP_int32 tmp1_QS, tmp2_QS;\r
+    SKP_int32 state_QS[ MAX_SHAPE_LPC_ORDER + 1 ] = { 0 };\r
+    SKP_int64 corr_QC[  MAX_SHAPE_LPC_ORDER + 1 ] = { 0 };\r
 \r
     /* Order must be even */\r
     SKP_assert( ( order & 1 ) == 0 );\r
 \r
     /* Order must be even */\r
     SKP_assert( ( order & 1 ) == 0 );\r
+    SKP_assert( 2 * QS - QC >= 0 );\r
 \r
     /* Loop over samples */\r
     for( n = 0; n < length; n++ ) {\r
 \r
     /* Loop over samples */\r
     for( n = 0; n < length; n++ ) {\r
-        tmp1_Q16 = SKP_LSHIFT32( ( SKP_int32 )input[ n ], 16 );\r
+        tmp1_QS = SKP_LSHIFT32( ( SKP_int32 )input[ n ], QS );\r
         /* Loop over allpass sections */\r
         for( i = 0; i < order; i += 2 ) {\r
             /* Output of allpass section */\r
         /* Loop over allpass sections */\r
         for( i = 0; i < order; i += 2 ) {\r
             /* Output of allpass section */\r
-            tmp2_Q16 = SKP_SMLAWB( state_Q16[ i ], state_Q16[ i + 1 ] - tmp1_Q16, warping_Q16 );\r
-            state_Q16[ i ] = tmp1_Q16;\r
-            corr_QC[ i ] += SKP_RSHIFT64( SKP_SMULL( tmp1_Q16, state_Q16[ 0 ] ), 32 - QC );\r
+            tmp2_QS = SKP_SMLAWB( state_QS[ i ], state_QS[ i + 1 ] - tmp1_QS, warping_Q16 );\r
+            state_QS[ i ]  = tmp1_QS;\r
+            corr_QC[  i ] += SKP_RSHIFT64( SKP_SMULL( tmp1_QS, state_QS[ 0 ] ), 2 * QS - QC );\r
             /* Output of allpass section */\r
             /* Output of allpass section */\r
-            tmp1_Q16 = SKP_SMLAWB( state_Q16[ i + 1 ], state_Q16[ i + 2 ] - tmp2_Q16, warping_Q16 );\r
-            state_Q16[ i + 1 ] = tmp2_Q16;\r
-            corr_QC[ i + 1 ] += SKP_RSHIFT64( SKP_SMULL( tmp2_Q16, state_Q16[ 0 ] ), 32 - QC );\r
+            tmp1_QS = SKP_SMLAWB( state_QS[ i + 1 ], state_QS[ i + 2 ] - tmp2_QS, warping_Q16 );\r
+            state_QS[ i + 1 ]  = tmp2_QS;\r
+            corr_QC[  i + 1 ] += SKP_RSHIFT64( SKP_SMULL( tmp2_QS, state_QS[ 0 ] ), 2 * QS - QC );\r
         }\r
         }\r
-        state_Q16[ order ] = tmp1_Q16;\r
-        corr_QC[ order ] += SKP_RSHIFT64( SKP_SMULL( tmp1_Q16, state_Q16[ 0 ] ), 32 - QC );\r
+        state_QS[ order ] = tmp1_QS;\r
+        corr_QC[  order ] += SKP_RSHIFT64( SKP_SMULL( tmp1_QS, state_QS[ 0 ] ), 2 * QS - QC );\r
     }\r
 \r
     lsh = SKP_Silk_CLZ64( corr_QC[ 0 ] ) - 35;\r
     }\r
 \r
     lsh = SKP_Silk_CLZ64( corr_QC[ 0 ] ) - 35;\r
index 66ee3d9..9700f7b 100644 (file)
@@ -26,6 +26,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FLP.h"\r
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FLP.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 \r
 #if HIGH_PASS_INPUT\r
 \r
 \r
 #if HIGH_PASS_INPUT\r
 \r
@@ -72,7 +73,7 @@ void SKP_Silk_HP_variable_cutoff_FLP(
     psEnc->variable_HP_smth2 += VARIABLE_HP_SMTH_COEF2 * ( psEnc->variable_HP_smth1 - psEnc->variable_HP_smth2 );\r
 \r
     /* Convert from log scale to Hertz */\r
     psEnc->variable_HP_smth2 += VARIABLE_HP_SMTH_COEF2 * ( psEnc->variable_HP_smth1 - psEnc->variable_HP_smth2 );\r
 \r
     /* Convert from log scale to Hertz */\r
-    psEncCtrl->pitch_freq_low_Hz = ( SKP_float )pow( 2.0, psEnc->variable_HP_smth2 );\r
+    psEncCtrl->pitch_freq_low_Hz = ( SKP_float )pow( 2.0f, psEnc->variable_HP_smth2 );\r
 \r
     /* Limit frequency range */\r
     psEncCtrl->pitch_freq_low_Hz = SKP_LIMIT_float( psEncCtrl->pitch_freq_low_Hz, VARIABLE_HP_MIN_FREQ, VARIABLE_HP_MAX_FREQ );\r
 \r
     /* Limit frequency range */\r
     psEncCtrl->pitch_freq_low_Hz = SKP_LIMIT_float( psEncCtrl->pitch_freq_low_Hz, VARIABLE_HP_MIN_FREQ, VARIABLE_HP_MAX_FREQ );\r
index 3bbe586..1f71667 100644 (file)
@@ -46,6 +46,10 @@ void SKP_Silk_LPC_analysis_filter_FLP(
     SKP_assert( Order <= length );\r
 \r
     switch( Order ) {\r
     SKP_assert( Order <= length );\r
 \r
     switch( Order ) {\r
+        case 6:\r
+            SKP_Silk_LPC_analysis_filter6_FLP(  r_LPC, PredCoef, s, length );\r
+        break;\r
+\r
         case 8:\r
             SKP_Silk_LPC_analysis_filter8_FLP(  r_LPC, PredCoef, s, length );\r
         break;\r
         case 8:\r
             SKP_Silk_LPC_analysis_filter8_FLP(  r_LPC, PredCoef, s, length );\r
         break;\r
@@ -204,3 +208,31 @@ void SKP_Silk_LPC_analysis_filter8_FLP(
         r_LPC[ix] = s_ptr[ 1 ] - LPC_pred;\r
     }\r
 }\r
         r_LPC[ix] = s_ptr[ 1 ] - LPC_pred;\r
     }\r
 }\r
+\r
+/* 6th order LPC analysis filter, does not write first 6 samples */\r
+void SKP_Silk_LPC_analysis_filter6_FLP(\r
+          SKP_float                 r_LPC[],            /* O    LPC residual signal                     */\r
+    const SKP_float                 PredCoef[],         /* I    LPC coefficients                        */\r
+    const SKP_float                 s[],                /* I    Input signal                            */\r
+    const SKP_int                   length              /* I    Length of input signal                  */\r
+)\r
+{\r
+    SKP_int   ix = 8;\r
+    SKP_float LPC_pred;\r
+    const SKP_float *s_ptr;\r
+\r
+    for ( ; ix < length; ix++) {\r
+        s_ptr = &s[ix - 1];\r
+\r
+        /* short-term prediction */\r
+        LPC_pred = s_ptr[  0 ] * PredCoef[ 0 ]  + \r
+                   s_ptr[ -1 ] * PredCoef[ 1 ]  +\r
+                   s_ptr[ -2 ] * PredCoef[ 2 ]  +\r
+                   s_ptr[ -3 ] * PredCoef[ 3 ]  +\r
+                   s_ptr[ -4 ] * PredCoef[ 4 ]  +\r
+                   s_ptr[ -5 ] * PredCoef[ 5 ];\r
+\r
+        /* prediction error */\r
+        r_LPC[ix] = s_ptr[ 1 ] - LPC_pred;\r
+    }\r
+}\r
index 1089a27..1b565c7 100644 (file)
@@ -28,14 +28,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "SKP_Silk_main_FLP.h"\r
 \r
 void SKP_Silk_LTP_analysis_filter_FLP(\r
 #include "SKP_Silk_main_FLP.h"\r
 \r
 void SKP_Silk_LTP_analysis_filter_FLP(\r
-          SKP_float         *LTP_res,                   /* O    LTP res MAX_NB_SUBFR*(pre_lgth+subfr_lngth) */\r
-    const SKP_float         *x,                         /* I    Input signal, with preceeding samples       */\r
-    const SKP_float         B[ LTP_ORDER * MAX_NB_SUBFR ],  /* I    LTP coefficients for each subframe      */\r
-    const SKP_int           pitchL[   MAX_NB_SUBFR ],   /* I    Pitch lags                                  */\r
-    const SKP_float         invGains[ MAX_NB_SUBFR ],   /* I    Inverse quantization gains                  */\r
-    const SKP_int           subfr_length,               /* I    Length of each subframe                     */\r
-    const SKP_int           nb_subfr,                   /* I    number of subframes                         */\r
-    const SKP_int           pre_length                  /* I    Preceeding samples for each subframe        */\r
+          SKP_float         *LTP_res,                       /* O    LTP res MAX_NB_SUBFR*(pre_lgth+subfr_lngth) */\r
+    const SKP_float         *x,                             /* I    Input signal, with preceeding samples       */\r
+    const SKP_float         B[ LTP_ORDER * MAX_NB_SUBFR ],  /* I    LTP coefficients for each subframe          */\r
+    const SKP_int           pitchL[   MAX_NB_SUBFR ],       /* I    Pitch lags                                  */\r
+    const SKP_float         invGains[ MAX_NB_SUBFR ],       /* I    Inverse quantization gains                  */\r
+    const SKP_int           subfr_length,                   /* I    Length of each subframe                     */\r
+    const SKP_int           nb_subfr,                       /* I    number of subframes                         */\r
+    const SKP_int           pre_length                      /* I    Preceeding samples for each subframe        */\r
 )\r
 {\r
     const SKP_float *x_ptr, *x_lag_ptr;\r
 )\r
 {\r
     const SKP_float *x_ptr, *x_lag_ptr;\r
index 0380d12..5039d54 100644 (file)
@@ -51,7 +51,7 @@ void SKP_Silk_LTP_scale_ctrl_FLP(
     \r
     psEnc->prevLTPredCodGain = psEncCtrl->LTPredCodGain;\r
 \r
     \r
     psEnc->prevLTPredCodGain = psEncCtrl->LTPredCodGain;\r
 \r
-    /* combine input and filtered input */\r
+    /* Combine input and filtered input */\r
     g_out = 0.5f * psEncCtrl->LTPredCodGain + ( 1.0f - 0.5f ) * psEnc->HPLTPredCodGain;\r
     g_limit = SKP_sigmoid( 0.5f * ( g_out - 6 ) );\r
     \r
     g_out = 0.5f * psEncCtrl->LTPredCodGain + ( 1.0f - 0.5f ) * psEnc->HPLTPredCodGain;\r
     g_limit = SKP_sigmoid( 0.5f * ( g_out - 6 ) );\r
     \r
index 589ac36..1b7cdc6 100644 (file)
@@ -39,7 +39,6 @@ void SKP_Silk_NLSF_MSVQ_decode_FLP(
           SKP_int    s;\r
           SKP_int    i;\r
 \r
           SKP_int    s;\r
           SKP_int    i;\r
 \r
-\r
     /* Check that each index is within valid range */\r
     SKP_assert( 0 <= NLSFIndices[ 0 ] && NLSFIndices[ 0 ] < psNLSF_CB_FLP->CBStages[ 0 ].nVectors );\r
 \r
     /* Check that each index is within valid range */\r
     SKP_assert( 0 <= NLSFIndices[ 0 ] && NLSFIndices[ 0 ] < psNLSF_CB_FLP->CBStages[ 0 ].nVectors );\r
 \r
@@ -85,6 +84,11 @@ void SKP_Silk_NLSF_MSVQ_decode_FLP(
         }\r
     }\r
 \r
         }\r
     }\r
 \r
+    /* Add 1/2 */\r
+    for( i = 0; i < LPC_order; i++ ) {\r
+        pNLSF[ i ] += 0.5f;\r
+    }\r
+\r
     /* NLSF stabilization */\r
     SKP_Silk_NLSF_stabilize_FLP( pNLSF, psNLSF_CB_FLP->NDeltaMin, LPC_order );\r
 }\r
     /* NLSF stabilization */\r
     SKP_Silk_NLSF_stabilize_FLP( pNLSF, psNLSF_CB_FLP->NDeltaMin, LPC_order );\r
 }\r
index bf0878e..e74e03d 100644 (file)
@@ -43,9 +43,8 @@ void SKP_Silk_NLSF_MSVQ_encode_FLP(
     const SKP_int                   deactivate_fluc_red /* I    Deactivate fluctuation reduction        */\r
 )\r
 {\r
     const SKP_int                   deactivate_fluc_red /* I    Deactivate fluctuation reduction        */\r
 )\r
 {\r
-    SKP_int     i, s, k, cur_survivors, prev_survivors, input_index, cb_index, bestIndex;\r
+    SKP_int     i, s, k, cur_survivors, prev_survivors, min_survivors, input_index, cb_index, bestIndex;\r
     SKP_float   se, wsse, rateDistThreshold, bestRateDist;\r
     SKP_float   se, wsse, rateDistThreshold, bestRateDist;\r
-    SKP_float   pNLSF_in[ MAX_LPC_ORDER ];\r
 \r
 #if( LOW_COMPLEXITY_ONLY == 1 )\r
     SKP_float   pRateDist[      NLSF_MSVQ_TREE_SEARCH_MAX_VECTORS_EVALUATED_LC_MODE ];\r
 \r
 #if( LOW_COMPLEXITY_ONLY == 1 )\r
     SKP_float   pRateDist[