Interface for bandwidth switching
[opus.git] / src_common / SKP_Silk_enc_API.c
index b2619a8..417253b 100644 (file)
@@ -31,6 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "SKP_Silk_control.h"\r
 #include "SKP_Silk_typedef.h"\r
 #include "SKP_Silk_structs.h"\r
+#include "SKP_Silk_tuning_parameters.h"\r
 #if FIXED_POINT\r
 #include "SKP_Silk_main_FIX.h"\r
 #define SKP_Silk_encoder_state_Fxx      SKP_Silk_encoder_state_FIX\r
@@ -47,6 +48,8 @@ typedef struct {
     stereo_state                        sStereo;\r
     SKP_int32                           nBitsExceeded;\r
     SKP_int                             nChannels;\r
+    SKP_int                             timeSinceSwitchAllowed_ms;\r
+    SKP_int                             allowBandwidthSwitch;\r
 } SKP_Silk_encoder;\r
 \r
 /****************************************/\r
@@ -138,6 +141,7 @@ SKP_int SKP_Silk_SDK_Encode(
 {\r
     SKP_int   n, i, nBits, flags, tmp_payloadSize_ms, tmp_complexity, MS_predictorIx = 0, ret = 0;\r
     SKP_int   nSamplesToBuffer, nBlocksOf10ms, nSamplesFromInput = 0;\r
+    SKP_int   speech_act_thr_for_switch_Q8;\r
     SKP_int32 TargetRate_bps, channelRate_bps, LBRR_symbol;\r
     SKP_Silk_encoder *psEnc = ( SKP_Silk_encoder * )encState;\r
     SKP_int16 buf[ MAX_FRAME_LENGTH_MS * MAX_API_FS_KHZ ];\r
@@ -194,7 +198,7 @@ SKP_int SKP_Silk_SDK_Encode(
 \r
     TargetRate_bps = SKP_RSHIFT32( encControl->bitRate, encControl->nChannels - 1 );\r
     for( n = 0; n < encControl->nChannels; n++ ) {\r
-        if( ( ret = SKP_Silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, TargetRate_bps ) ) != 0 ) {\r
+        if( ( ret = SKP_Silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, TargetRate_bps, psEnc->allowBandwidthSwitch ) ) != 0 ) {\r
             SKP_assert( 0 );\r
             return ret;\r
         }\r
@@ -232,6 +236,9 @@ SKP_int SKP_Silk_SDK_Encode(
         samplesIn  += nSamplesFromInput * encControl->nChannels;\r
         nSamplesIn -= nSamplesFromInput;\r
 \r
+        /* Default */\r
+        psEnc->allowBandwidthSwitch = 0;\r
+\r
         /* Silk encoder */\r
         if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) {\r
             /* Enough data in input buffer, so encode */\r
@@ -340,6 +347,17 @@ SKP_int SKP_Silk_SDK_Encode(
                 psEnc->nBitsExceeded += *nBytesOut * 8;\r
                 psEnc->nBitsExceeded -= SKP_DIV32_16( SKP_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 );\r
                 psEnc->nBitsExceeded  = SKP_LIMIT( psEnc->nBitsExceeded, 0, 10000 );\r
+\r
+                /* Update flag indicating if bandwidth switching is allowed */\r
+                speech_act_thr_for_switch_Q8 = SKP_SMLAWB( SKP_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ), \r
+                    SKP_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms );\r
+                if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) {\r
+                    psEnc->allowBandwidthSwitch = 1;\r
+                    psEnc->timeSinceSwitchAllowed_ms = 0;\r
+                } else {\r
+                    psEnc->allowBandwidthSwitch = 0;\r
+                    psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms;\r
+                }\r
             }\r
 \r
             if( nSamplesIn == 0 ) {\r
@@ -350,6 +368,8 @@ SKP_int SKP_Silk_SDK_Encode(
         }\r
     }\r
 \r
+    encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch;\r
+    encControl->inWBmodeWithoutVariableLP = ( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 ) && ( psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0 );\r
     encControl->internalSampleRate = SKP_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 );\r
     if( prefillFlag ) {\r
         encControl->payloadSize_ms = tmp_payloadSize_ms;\r