From 8daa4d373948aadbd155f03b3dbc2ce658dfac12 Mon Sep 17 00:00:00 2001
From: Koen Vos <koenvos@users.noreply.github.com>
Date: Sun, 21 Feb 2016 14:27:11 +0800
Subject: [PATCH] take advantage of more efficient NLSF quantization by
 reducing number of survivors in search

JMV: edited to be a little more conservative
---
 silk/NLSF_decode.c       | 26 ++------------------------
 silk/NLSF_encode.c       |  4 ++--
 silk/control_codec.c     |  8 ++++----
 silk/decode_parameters.c |  2 +-
 silk/define.h            |  2 +-
 silk/main.h              |  5 ++---
 6 files changed, 12 insertions(+), 35 deletions(-)

diff --git a/silk/NLSF_decode.c b/silk/NLSF_decode.c
index f928cf40c..eeb0ba8c9 100644
--- a/silk/NLSF_decode.c
+++ b/silk/NLSF_decode.c
@@ -60,11 +60,10 @@ static OPUS_INLINE void silk_NLSF_residual_dequant(          /* O    Returns RD
 /***********************/
 /* NLSF vector decoder */
 /***********************/
-opus_int silk_NLSF_decode(                                      /* O    Number of bits (Q5), if signalType >= 0     */
+void silk_NLSF_decode(
           opus_int16            *pNLSF_Q15,                     /* O    Quantized NLSF vector [ LPC_ORDER ]         */
           opus_int8             *NLSFIndices,                   /* I    Codebook path vector [ LPC_ORDER + 1 ]      */
-    const silk_NLSF_CB_struct   *psNLSF_CB,                     /* I    Codebook object                             */
-    const opus_int              signalType                      /* I    SignalType, to determine number of bits     */
+    const silk_NLSF_CB_struct   *psNLSF_CB                      /* I    Codebook object                             */
 )
 {
     opus_int         i;
@@ -91,25 +90,4 @@ opus_int silk_NLSF_decode(                                      /* O    Number o
 
     /* NLSF stabilization */
     silk_NLSF_stabilize( pNLSF_Q15, psNLSF_CB->deltaMin_Q15, psNLSF_CB->order );
-
-    if( signalType >= 0 ) {
-        opus_int         prob_Q8, bits_Q5;
-        const opus_uint8 *iCDF_ptr;
-        bits_Q5 = 0;
-        iCDF_ptr = &psNLSF_CB->CB1_iCDF[ ( signalType >> 1 ) * psNLSF_CB->nVectors ];
-        if( NLSFIndices[ 0 ] == 0 ) {
-            prob_Q8 = 256 - iCDF_ptr[ NLSFIndices[ 0 ] ];
-        } else {
-            prob_Q8 = iCDF_ptr[ NLSFIndices[ 0 ] - 1 ] - iCDF_ptr[ NLSFIndices[ 0 ] ];
-        }
-        bits_Q5 = ( 8 << 5 ) - ( silk_lin2log( prob_Q8 ) >> 2 );
-        for( i = 0; i < psNLSF_CB->order; i++ ) {
-            const opus_uint8 *rates_Q5;
-            rates_Q5 = &psNLSF_CB->ec_Rates_Q5[ ec_ix[ i ] ];
-            bits_Q5 += rates_Q5[ NLSFIndices[ i + 1 ] + NLSF_QUANT_MAX_AMPLITUDE ];
-        }
-        return bits_Q5;
-    }
-
-    return 0;
 }
diff --git a/silk/NLSF_encode.c b/silk/NLSF_encode.c
index dda6a1684..fb3b04b87 100644
--- a/silk/NLSF_encode.c
+++ b/silk/NLSF_encode.c
@@ -37,7 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
 /***********************/
 opus_int32 silk_NLSF_encode(                                    /* O    Returns RD value in Q25                     */
           opus_int8             *NLSFIndices,                   /* I    Codebook path vector [ LPC_ORDER + 1 ]      */
-          opus_int16            *pNLSF_Q15,                     /* I/O  Quantized NLSF vector [ LPC_ORDER ]         */
+          opus_int16            *pNLSF_Q15,                     /* I/O  (Un)quantized NLSF vector [ LPC_ORDER ]     */
     const silk_NLSF_CB_struct   *psNLSF_CB,                     /* I    Codebook object                             */
     const opus_int16            *pW_Q2,                         /* I    NLSF weight vector [ LPC_ORDER ]            */
     const opus_int              NLSF_mu_Q20,                    /* I    Rate weight for the RD optimization         */
@@ -117,7 +117,7 @@ opus_int32 silk_NLSF_encode(                                    /* O    Returns
     silk_memcpy( &NLSFIndices[ 1 ], &tempIndices2[ bestIndex * MAX_LPC_ORDER ], psNLSF_CB->order * sizeof( opus_int8 ) );
 
     /* Decode */
-    silk_NLSF_decode( pNLSF_Q15, NLSFIndices, psNLSF_CB, -1 );
+    silk_NLSF_decode( pNLSF_Q15, NLSFIndices, psNLSF_CB );
 
     ret = RD_Q25[ 0 ];
     RESTORE_STACK;
diff --git a/silk/control_codec.c b/silk/control_codec.c
index 044eea3f2..5426e1e1d 100644
--- a/silk/control_codec.c
+++ b/silk/control_codec.c
@@ -339,7 +339,7 @@ static opus_int silk_setup_complexity(
         psEncC->nStatesDelayedDecision          = 1;
         psEncC->useInterpolatedNLSFs            = 0;
         psEncC->LTPQuantLowComplexity           = 0;
-        psEncC->NLSF_MSVQ_Survivors             = 4;
+        psEncC->NLSF_MSVQ_Survivors             = 3;
         psEncC->warping_Q16                     = 0;
     } else if( Complexity < 6 ) {
         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
@@ -350,7 +350,7 @@ static opus_int silk_setup_complexity(
         psEncC->nStatesDelayedDecision          = 2;
         psEncC->useInterpolatedNLSFs            = 1;
         psEncC->LTPQuantLowComplexity           = 0;
-        psEncC->NLSF_MSVQ_Survivors             = 8;
+        psEncC->NLSF_MSVQ_Survivors             = 6;
         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
     } else if( Complexity < 8 ) {
         psEncC->pitchEstimationComplexity       = SILK_PE_MID_COMPLEX;
@@ -361,7 +361,7 @@ static opus_int silk_setup_complexity(
         psEncC->nStatesDelayedDecision          = 3;
         psEncC->useInterpolatedNLSFs            = 1;
         psEncC->LTPQuantLowComplexity           = 0;
-        psEncC->NLSF_MSVQ_Survivors             = 16;
+        psEncC->NLSF_MSVQ_Survivors             = 8;
         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
     } else {
         psEncC->pitchEstimationComplexity       = SILK_PE_MAX_COMPLEX;
@@ -372,7 +372,7 @@ static opus_int silk_setup_complexity(
         psEncC->nStatesDelayedDecision          = MAX_DEL_DEC_STATES;
         psEncC->useInterpolatedNLSFs            = 1;
         psEncC->LTPQuantLowComplexity           = 0;
-        psEncC->NLSF_MSVQ_Survivors             = 32;
+        psEncC->NLSF_MSVQ_Survivors             = 16;
         psEncC->warping_Q16                     = psEncC->fs_kHz * SILK_FIX_CONST( WARPING_MULTIPLIER, 16 );
     }
 
diff --git a/silk/decode_parameters.c b/silk/decode_parameters.c
index 07d6d6f45..e345b1dce 100644
--- a/silk/decode_parameters.c
+++ b/silk/decode_parameters.c
@@ -49,7 +49,7 @@ void silk_decode_parameters(
     /****************/
     /* Decode NLSFs */
     /****************/
-    silk_NLSF_decode( pNLSF_Q15, psDec->indices.NLSFIndices, psDec->psNLSF_CB, -1 );
+    silk_NLSF_decode( pNLSF_Q15, psDec->indices.NLSFIndices, psDec->psNLSF_CB );
 
     /* Convert NLSF parameters to AR prediction filter coefficients */
     silk_NLSF2A( psDecCtrl->PredCoef_Q12[ 1 ], pNLSF_Q15, psDec->LPC_order );
diff --git a/silk/define.h b/silk/define.h
index 19c9b00e2..5abf08103 100644
--- a/silk/define.h
+++ b/silk/define.h
@@ -205,7 +205,7 @@ extern "C"
 /******************/
 #define NLSF_W_Q                                2
 #define NLSF_VQ_MAX_VECTORS                     32
-#define NLSF_VQ_MAX_SURVIVORS                   32
+#define NLSF_VQ_MAX_SURVIVORS                   16
 #define NLSF_QUANT_MAX_AMPLITUDE                4
 #define NLSF_QUANT_MAX_AMPLITUDE_EXT            10
 #define NLSF_QUANT_LEVEL_ADJ                    0.1
diff --git a/silk/main.h b/silk/main.h
index 910e5ddf0..c6d2b9e7c 100644
--- a/silk/main.h
+++ b/silk/main.h
@@ -376,11 +376,10 @@ void silk_NLSF_unpack(
 /***********************/
 /* NLSF vector decoder */
 /***********************/
-opus_int silk_NLSF_decode(                                      /* O    Number of bits (Q5), if signalType >= 0     */
+void silk_NLSF_decode(
           opus_int16            *pNLSF_Q15,                     /* O    Quantized NLSF vector [ LPC_ORDER ]         */
           opus_int8             *NLSFIndices,                   /* I    Codebook path vector [ LPC_ORDER + 1 ]      */
-    const silk_NLSF_CB_struct   *psNLSF_CB,                     /* I    Codebook object                             */
-    const opus_int              signalType                      /* I    SignalType, to determine number of bits     */
+    const silk_NLSF_CB_struct   *psNLSF_CB                      /* I    Codebook object                             */
 );
 
 /****************************************************/
-- 
GitLab