Fixing DTX for fixed-point too, updating project files
[opus.git] / src_FIX / SKP_Silk_control_codec_FIX.c
index b9bcca4..b092460 100644 (file)
@@ -1,5 +1,5 @@
 /***********************************************************************\r
-Copyright (c) 2006-2010, Skype Limited. All rights reserved. \r
+Copyright (c) 2006-2011, Skype Limited. All rights reserved. \r
 Redistribution and use in source and binary forms, with or without \r
 modification, (subject to the limitations in the disclaimer below) \r
 are permitted provided that the following conditions are met:\r
@@ -26,172 +26,64 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/\r
 \r
 #include "SKP_Silk_main_FIX.h"\r
+#include "SKP_Silk_setup.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
-    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                         PacketSize_ms,      /* I                        */\r
     SKP_int                         fs_kHz              /* I                        */\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
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         TargetRate_bps      /* I                        */\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_int32                       TargetRate_bps      /* I                        */\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-10)                       */\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 && psEnc->sCmn.prefillFlag == 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
+        return ret;\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
+    /* Beyond this point we know that there are no previously coded frames in the payload buffer */\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
-\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
-    /* Prepare resampler and buffered data      */    \r
+    /* Determine internal sampling rate         */\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
-    /* Set packet size                          */\r
+    /* Prepare resampler and buffered data      */\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
-    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
-    ret += SKP_Silk_setup_complexity( psEnc, Complexity );\r
+    ret += SKP_Silk_setup_complexity( &psEnc->sCmn, Complexity );\r
 \r
     /********************************************/\r
     /* Set bitrate/coding quality               */\r
@@ -209,297 +101,187 @@ SKP_int SKP_Silk_control_encoder_FIX(
     /********************************************/\r
     /* Set LBRR usage                           */\r
     /********************************************/\r
-    ret += SKP_Silk_setup_LBRR( psEnc, INBandFEC_enabled );\r
+    ret += SKP_Silk_setup_LBRR( &psEnc->sCmn );\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
-/* 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
-)\r
-{\r
-    SKP_int LBRR_usage;\r
-\r
-    if( psEnc->sCmn.LBRR_enabled ) {\r
-        /* Control LBRR */\r
-\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
-        }\r
-        psEncCtrl->sCmn.LBRR_usage = LBRR_usage;\r
-    } else {\r
-        psEncCtrl->sCmn.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
-    SKP_int                         PacketSize_ms,      /* I                        */\r
-    SKP_int                         fs_kHz              /* 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
-        ( PacketSize_ms !=  60 ) && \r
-        ( PacketSize_ms !=  80 ) && \r
-        ( PacketSize_ms != 100 ) ) {\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
-            } 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.PacketSize_ms = PacketSize_ms;\r
-                psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
-                /* Packet length changes. Reset LBRR buffer */\r
-                SKP_Silk_LBRR_reset( &psEnc->sCmn );\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
-    SKP_int                         API_fs_Hz,          /* I                        */\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
-        /* 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
-        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
-        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
-            SKP_Silk_resampler_state_struct  temp_resampler_state;\r
+                SKP_Silk_resampler_state_struct  temp_resampler_state;\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
-            /* 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
-            /* 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
-            /* 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
-        } 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
-        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
-    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
-    SKP_int                         fs_kHz              /* I                        */\r
+    SKP_int                         fs_kHz,             /* I                        */\r
+    SKP_int                         PacketSize_ms       /* I                        */\r
 )\r
 {\r
-    SKP_int ret = 0;\r
+    SKP_int ret = SKP_SILK_NO_ERROR;\r
 \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
-        SKP_memset( &psEnc->sPrefilt,        0,                            sizeof( SKP_Silk_prefilter_state_FIX ) );\r
-        SKP_memset( &psEnc->sNSQ,            0,                            sizeof( SKP_Silk_nsq_state ) );\r
-        SKP_memset( &psEnc->sPred,           0,                            sizeof( SKP_Silk_predict_state_FIX ) );\r
-        SKP_memset( psEnc->sNSQ.xq,          0, ( 2 * MAX_FRAME_LENGTH ) * sizeof( SKP_int16 ) );\r
-        SKP_memset( psEnc->sNSQ_LBRR.xq,     0, ( 2 * MAX_FRAME_LENGTH ) * sizeof( SKP_int16 ) );\r
-        SKP_memset( psEnc->sCmn.LBRR_buffer, 0,           MAX_LBRR_DELAY * sizeof( SKP_SILK_LBRR_struct ) );\r
-#if SWITCH_TRANSITION_FILTERING\r
-        SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, 2 * sizeof( SKP_int32 ) );\r
-        if( psEnc->sCmn.sLP.mode == 1 ) {\r
-            /* Begin transition phase */\r
-            psEnc->sCmn.sLP.transition_frame_no = 1;\r
+    /* Set packet size */\r
+    if( PacketSize_ms != psEnc->sCmn.PacketSize_ms ) {\r
+        if( ( PacketSize_ms !=  10 ) &&\r
+            ( PacketSize_ms !=  20 ) &&\r
+            ( PacketSize_ms !=  40 ) && \r
+            ( PacketSize_ms !=  60 ) ) {\r
+            ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;\r
+        }\r
+        if( PacketSize_ms <= 10 ) {\r
+            psEnc->sCmn.nFramesPerPacket = 1;\r
+            psEnc->sCmn.nb_subfr = PacketSize_ms == 10 ? 2 : 1;\r
+            psEnc->sCmn.frame_length = SKP_SMULBB( PacketSize_ms, fs_kHz );\r
+            psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
+            if( psEnc->sCmn.fs_kHz == 8 ) {\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
+            } else {\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
+            }\r
         } else {\r
-            /* End transition phase */\r
-            psEnc->sCmn.sLP.transition_frame_no = 0;\r
+            psEnc->sCmn.nFramesPerPacket = SKP_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );\r
+            psEnc->sCmn.nb_subfr = MAX_NB_SUBFR;\r
+            psEnc->sCmn.frame_length = SKP_SMULBB( 20, fs_kHz );\r
+            psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
+            if( psEnc->sCmn.fs_kHz == 8 ) {\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF;\r
+            } else {\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
+            }\r
         }\r
-#endif\r
-        psEnc->sCmn.inputBufIx          = 0;\r
-        psEnc->sCmn.nFramesInPayloadBuf = 0;\r
-        psEnc->sCmn.nBytesInPayloadBuf  = 0;\r
-        psEnc->sCmn.oldest_LBRR_idx     = 0;\r
-        psEnc->sCmn.TargetRate_bps      = 0; /* Ensures that psEnc->SNR_dB is recomputed */\r
+        psEnc->sCmn.PacketSize_ms  = PacketSize_ms;\r
+        psEnc->sCmn.TargetRate_bps = 0;         /* trigger new SNR computation */\r
+    }\r
 \r
-        SKP_memset( psEnc->sPred.prev_NLSFq_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) );\r
+    /* Set internal sampling frequency */\r
+    SKP_assert( fs_kHz == 8 || fs_kHz == 12 || fs_kHz == 16 );\r
+    SKP_assert( psEnc->sCmn.nb_subfr == 2 || psEnc->sCmn.nb_subfr == 4 );\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
+        SKP_memset( &psEnc->sPrefilt,             0, sizeof( SKP_Silk_prefilter_state_FIX ) );\r
+        SKP_memset( &psEnc->sCmn.sNSQ,            0, sizeof( SKP_Silk_nsq_state ) );\r
+        SKP_memset( psEnc->sCmn.prev_NLSFq_Q15,   0, sizeof( psEnc->sCmn.prev_NLSFq_Q15 ) );\r
+        SKP_memset( &psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );\r
+        psEnc->sCmn.inputBufIx                  = 0;\r
+        psEnc->sCmn.nFramesAnalyzed             = 0;\r
+        psEnc->sCmn.TargetRate_bps              = 0; /* Ensures that psEnc->SNR_dB is recomputed */\r
 \r
         /* Initialize non-zero parameters */\r
-        psEnc->sCmn.prevLag                 = 100;\r
-        psEnc->sCmn.prev_sigtype            = SIG_TYPE_UNVOICED;\r
-        psEnc->sCmn.first_frame_after_reset = 1;\r
-        psEnc->sPrefilt.lagPrev             = 100;\r
-        psEnc->sShape.LastGainIndex         = 1;\r
-        psEnc->sNSQ.lagPrev                 = 100;\r
-        psEnc->sNSQ.prev_inv_gain_Q16       = 65536;\r
-        psEnc->sNSQ_LBRR.prev_inv_gain_Q16  = 65536;\r
+        psEnc->sCmn.prevLag                     = 100;\r
+        psEnc->sCmn.first_frame_after_reset     = 1;\r
+        psEnc->sPrefilt.lagPrev                 = 100;\r
+        psEnc->sShape.LastGainIndex             = 10;\r
+        psEnc->sCmn.sNSQ.lagPrev                = 100;\r
+        psEnc->sCmn.sNSQ.prev_inv_gain_Q16      = 65536;\r
+\r
         psEnc->sCmn.fs_kHz = fs_kHz;\r
         if( psEnc->sCmn.fs_kHz == 8 ) {\r
+            if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF; \r
+            } else {\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
+            }\r
+        } else {\r
+            if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
+            } else {\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
+            }\r
+        }\r
+        if( psEnc->sCmn.fs_kHz == 8 || psEnc->sCmn.fs_kHz == 12 ) {\r
             psEnc->sCmn.predictLPCOrder = MIN_LPC_ORDER;\r
-            psEnc->sCmn.psNLSF_CB[ 0 ]  = &SKP_Silk_NLSF_CB0_10;\r
-            psEnc->sCmn.psNLSF_CB[ 1 ]  = &SKP_Silk_NLSF_CB1_10;\r
+            psEnc->sCmn.psNLSF_CB  = &SKP_Silk_NLSF_CB_NB_MB;\r
         } else {\r
             psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;\r
-            psEnc->sCmn.psNLSF_CB[ 0 ]  = &SKP_Silk_NLSF_CB0_16;\r
-            psEnc->sCmn.psNLSF_CB[ 1 ]  = &SKP_Silk_NLSF_CB1_16;\r
+            psEnc->sCmn.psNLSF_CB  = &SKP_Silk_NLSF_CB_WB;\r
         }\r
-        psEnc->sCmn.subfr_length   = SKP_SMULBB( SUB_FRAME_LENGTH_MS, fs_kHz );\r
+        psEnc->sCmn.subfr_length   = 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.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.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
+        psEnc->sCmn.max_pitch_lag  = SKP_SMULBB( 18, fs_kHz );\r
+        if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
+            psEnc->sCmn.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS, fs_kHz );\r
         } else {\r
-            psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\r
+            psEnc->sCmn.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->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->sCmn.bitrate_threshold_up   = WB2SWB_BITRATE_BPS;\r
-            psEnc->sCmn.bitrate_threshold_down = WB2MB_BITRATE_BPS; \r
+        if( psEnc->sCmn.fs_kHz == 16 ) {\r
+            psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_WB, 9 );\r
+            psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform8_iCDF;\r
         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
-            psEnc->mu_LTP_Q8 = MU_LTP_QUANT_MB_Q8;\r
-            psEnc->sCmn.bitrate_threshold_up   = MB2WB_BITRATE_BPS;\r
-            psEnc->sCmn.bitrate_threshold_down = MB2NB_BITRATE_BPS;\r
+            psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_MB, 9 );\r
+            psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform6_iCDF;\r
         } else {\r
-            psEnc->mu_LTP_Q8 = MU_LTP_QUANT_NB_Q8;\r
-            psEnc->sCmn.bitrate_threshold_up   = NB2MB_BITRATE_BPS;\r
-            psEnc->sCmn.bitrate_threshold_down = 0;\r
+            psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_NB, 9 );\r
+            psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform4_iCDF;\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
-    return( ret );\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
     /* 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
 SKP_INLINE SKP_int SKP_Silk_setup_rate(\r
     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O                      */\r
-    SKP_int                         TargetRate_bps      /* I                        */\r
+    SKP_int32                       TargetRate_bps      /* I                        */\r
 )\r
 {\r
-    SKP_int k, ret = 0;\r
+    SKP_int k, ret = SKP_SILK_NO_ERROR;\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
@@ -511,11 +293,15 @@ SKP_INLINE SKP_int SKP_Silk_setup_rate(
         } else if( psEnc->sCmn.fs_kHz == 16 ) {\r
             rateTable = TargetRate_table_WB;\r
         } else {\r
-            rateTable = TargetRate_table_SWB;\r
+            SKP_assert( 0 );\r
+        }\r
+        /* Reduce bitrate for 10 ms modes in these calculations */\r
+        if( psEnc->sCmn.nb_subfr == 2 ) {\r
+            TargetRate_bps -= REDUCE_BITRATE_10_MS_BPS;\r
         }\r
         for( k = 1; k < TARGET_RATE_TAB_SZ; k++ ) {\r
             /* Find bitrate interval in table and interpolate */\r
-            if( TargetRate_bps < rateTable[ k ] ) {\r
+            if( TargetRate_bps <= rateTable[ k ] ) {\r
                 frac_Q6 = SKP_DIV32( SKP_LSHIFT( TargetRate_bps - rateTable[ k - 1 ], 6 ), \r
                                                  rateTable[ k ] - rateTable[ k - 1 ] );\r
                 psEnc->SNR_dB_Q7 = SKP_LSHIFT( SNR_table_Q1[ k - 1 ], 6 ) + SKP_MUL( frac_Q6, SNR_table_Q1[ k ] - SNR_table_Q1[ k - 1 ] );\r
@@ -525,59 +311,3 @@ SKP_INLINE SKP_int SKP_Silk_setup_rate(
     }\r
     return( ret );\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
-)\r
-{\r
-    SKP_int   ret = 0;\r
-    SKP_int32 LBRRRate_thres_bps;\r
-\r
-#if USE_LBRR\r
-    if( INBandFEC_enabled < 0 || INBandFEC_enabled > 1 ) {\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
-\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
-        } else {\r
-            psEnc->inBandFEC_SNR_comp_Q8     = 0;\r
-            psEnc->sCmn.LBRR_enabled         = 0;\r
-        }\r
-    }\r
-#else\r
-    if( INBandFEC_enabled != 0 ) {\r
-        ret = SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING;\r
-    }\r
-    psEnc->sCmn.LBRR_enabled = 0;\r
-#endif\r
-    return( ret );\r
-}
\ No newline at end of file