Fixing DTX for fixed-point too, updating project files
[opus.git] / src_FIX / SKP_Silk_control_codec_FIX.c
index 016c88e..b092460 100644 (file)
@@ -28,7 +28,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #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                         fs_kHz              /* I                        */\r
@@ -56,7 +55,7 @@ SKP_int SKP_Silk_control_encoder_FIX(
 {\r
     SKP_int   fs_kHz, ret = 0;\r
 \r
-    if( psEnc->sCmn.controlled_since_last_payload != 0 ) {\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
@@ -177,10 +176,11 @@ SKP_INLINE SKP_int SKP_Silk_setup_fs(
             ( PacketSize_ms !=  60 ) ) {\r
             ret = SKP_SILK_ENC_PACKET_SIZE_NOT_SUPPORTED;\r
         }\r
-        if( PacketSize_ms == 10 ) {\r
+        if( PacketSize_ms <= 10 ) {\r
             psEnc->sCmn.nFramesPerPacket = 1;\r
-            psEnc->sCmn.nb_subfr = MAX_NB_SUBFR >> 1;\r
-            psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, fs_kHz );\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
@@ -189,37 +189,31 @@ SKP_INLINE SKP_int SKP_Silk_setup_fs(
         } else {\r
             psEnc->sCmn.nFramesPerPacket = SKP_DIV32_16( PacketSize_ms, MAX_FRAME_LENGTH_MS );\r
             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.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
-        psEnc->sCmn.PacketSize_ms = PacketSize_ms;\r
+        psEnc->sCmn.PacketSize_ms  = PacketSize_ms;\r
+        psEnc->sCmn.TargetRate_bps = 0;         /* trigger new SNR computation */\r
     }\r
 \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->sPred,               0, sizeof( SKP_Silk_predict_state_FIX ) );\r
-        SKP_memset( psEnc->sPred.prev_NLSFq_Q15, 0, sizeof( psEnc->sPred.prev_NLSFq_Q15 ) );\r
-#if SWITCH_TRANSITION_FILTERING\r
-        SKP_memset( psEnc->sCmn.sLP.In_LP_State, 0, sizeof( psEnc->sCmn.sLP.In_LP_State ) );\r
-        if( psEnc->sCmn.sLP.mode == 1 ) {\r
-            /* Begin transition phase */\r
-            psEnc->sCmn.sLP.transition_frame_no = 1;\r
-        } else {\r
-            /* End transition phase */\r
-            psEnc->sCmn.sLP.transition_frame_no = 0;\r
-        }\r
-#endif\r
-        psEnc->sCmn.inputBufIx          = 0;\r
-        psEnc->sCmn.nFramesAnalyzed     = 0;\r
-        psEnc->sCmn.TargetRate_bps      = 0; /* Ensures that psEnc->SNR_dB is recomputed */\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
@@ -231,62 +225,44 @@ SKP_INLINE SKP_int SKP_Silk_setup_fs(
 \r
         psEnc->sCmn.fs_kHz = fs_kHz;\r
         if( psEnc->sCmn.fs_kHz == 8 ) {\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
-            if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
+            if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_NB_iCDF; \r
-            } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
-                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
             } else {\r
-                /* Unsupported number of frames */\r
-                SKP_assert( 0 );\r
+                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_NB_iCDF;\r
             }\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
-            if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ){\r
+            if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR ) {\r
                 psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_iCDF; \r
-            } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
-                psEnc->sCmn.pitch_contour_iCDF = SKP_Silk_pitch_contour_10_ms_iCDF;\r
             } else {\r
-                /* Unsupported number of frames */\r
-                SKP_assert( 0 );\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  = &SKP_Silk_NLSF_CB_NB_MB;\r
+        } else {\r
+            psEnc->sCmn.predictLPCOrder = MAX_LPC_ORDER;\r
+            psEnc->sCmn.psNLSF_CB  = &SKP_Silk_NLSF_CB_WB;\r
+        }\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->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
-        } else if( psEnc->sCmn.nb_subfr == MAX_NB_SUBFR / 2 ){\r
-            psEnc->sPred.pitch_LPC_win_length = SKP_SMULBB( FIND_PITCH_LPC_WIN_MS_2_SF, 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
-            /* Unsupported number of frames */\r
-            SKP_assert( 0 );\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 == 16 ) {\r
             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_WB, 9 );\r
-            psEnc->sCmn.bitrate_threshold_up    = SKP_int32_MAX;\r
-            psEnc->sCmn.bitrate_threshold_down  = WB2MB_BITRATE_BPS; \r
             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform8_iCDF;\r
         } else if( psEnc->sCmn.fs_kHz == 12 ) {\r
             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_MB, 9 );\r
-            psEnc->sCmn.bitrate_threshold_up    = MB2WB_BITRATE_BPS;\r
-            psEnc->sCmn.bitrate_threshold_down  = MB2NB_BITRATE_BPS;\r
             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform6_iCDF;\r
-        } else if( psEnc->sCmn.fs_kHz == 8 ) {\r
+        } else {\r
             psEnc->sCmn.mu_LTP_Q9 = SKP_FIX_CONST( MU_LTP_QUANT_NB, 9 );\r
-            psEnc->sCmn.bitrate_threshold_up    = NB2MB_BITRATE_BPS;\r
-            psEnc->sCmn.bitrate_threshold_down  = 0;\r
             psEnc->sCmn.pitch_lag_low_bits_iCDF = SKP_Silk_uniform4_iCDF;\r
-        } else {\r
-            /* unsupported sampling rate */\r
-            SKP_assert( 0 );\r
         }\r
     }\r
 \r