shithub: opus

Download patch

ref: f06774c64072e09936e262a1bfbad54b9a15e42f
parent: 07691f15d428ce56b4f7ddb4fcf6df2d703cb83f
author: Koen Vos <koenvos@users.noreply.github.com>
date: Wed Jun 1 09:14:32 EDT 2016

more conservative scaling of LTP corrs; remove assert that checks for negative residual energy; discard CB entries leading to such negative energies

--- a/silk/VQ_WMat_EC.c
+++ b/silk/VQ_WMat_EC.c
@@ -34,13 +34,13 @@
 /* Entropy constrained matrix-weighted VQ, hard-coded to 5-element vectors, for a single input data vector */
 void silk_VQ_WMat_EC_c(
     opus_int8                   *ind,                           /* O    index of best codebook vector               */
-    opus_int32                  *res_nrg_Q15,                    /* O    best residual energy                        */
-    opus_int32                  *rate_dist_Q8,                  /* O    best total bitrate                            */
+    opus_int32                  *res_nrg_Q15,                   /* O    best residual energy                        */
+    opus_int32                  *rate_dist_Q8,                  /* O    best total bitrate                          */
     const opus_int32            *XX_Q17,                        /* I    correlation matrix                          */
-    const opus_int32            *xX_Q17,                        /* I    correlation vector                            */
+    const opus_int32            *xX_Q17,                        /* I    correlation vector                          */
     const opus_int8             *cb_Q7,                         /* I    codebook                                    */
     const opus_uint8            *cl_Q5,                         /* I    code length for each codebook vector        */
-    const opus_int              subfr_len,                        /* I    number of samples per subframe                */
+    const opus_int              subfr_len,                      /* I    number of samples per subframe              */
     const opus_int              L                               /* I    number of vectors in codebook               */
 )
 {
@@ -62,10 +62,12 @@
     *res_nrg_Q15 = silk_int32_MAX;
     sum1_best_Q15 = silk_int32_MAX;
     cb_row_Q7 = cb_Q7;
+    /* In things go really bad, at least *ind is set to something safe. */
+    *ind = 0;
     for( k = 0; k < L; k++ ) {
         /* Weighted rate */
         /* Quantization error: 1 - 2* xX * cb + cb' * XX * cb */
-        sum1_Q15 = SILK_FIX_CONST( 1.0001, 15 );
+        sum1_Q15 = SILK_FIX_CONST( 1.001, 15 );
 
         /* first row of XX_Q17 */
         sum2_Q24 = silk_MLA( neg_xX_Q24[ 0 ], XX_Q17[  1 ], cb_row_Q7[ 1 ] );
@@ -102,11 +104,8 @@
         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[ 24 ], cb_row_Q7[ 4 ] );
         sum1_Q15 = silk_SMLAWB( sum1_Q15,        sum2_Q24,  cb_row_Q7[ 4 ] );
 
-        /* If ever the following assert triggers, increase LTP_CORR_INV_MAX */
-        silk_assert( sum1_Q15 >= 0 );
-
         /* find best */
-        if( sum1_Q15 <= sum1_best_Q15 ) {
+        if( sum1_Q15 <= sum1_best_Q15 && sum1_Q15 >= 0 ) {
             sum1_best_Q15 = sum1_Q15;
             /* Translate residual energy to bits using high-rate assumption (6 dB ==> 1 bit/sample) */
             bits_res_Q8 = silk_SMULBB( subfr_len, silk_lin2log( sum1_Q15 ) - (15 << 7) );
--- a/silk/fixed/find_LTP_FIX.c
+++ b/silk/fixed/find_LTP_FIX.c
@@ -33,9 +33,9 @@
 #include "tuning_parameters.h"
 
 void silk_find_LTP_FIX(
-    opus_int32                      XXLTP_Q17[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O    Correlation matrix                                                */
-    opus_int32                      xXLTP_Q17[ MAX_NB_SUBFR * LTP_ORDER ],    /* O    Correlation vector                                                            */
-    const opus_int16                r_ptr[],                                /* I    Residual signal after LPC                                                    */
+    opus_int32                      XXLTP_Q17[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O    Correlation matrix                                               */
+    opus_int32                      xXLTP_Q17[ MAX_NB_SUBFR * LTP_ORDER ],  /* O    Correlation vector                                                          */
+    const opus_int16                r_ptr[],                                /* I    Residual signal after LPC                                                   */
     const opus_int                  lag[ MAX_NB_SUBFR ],                    /* I    LTP lags                                                                    */
     const opus_int                  subfr_length,                           /* I    Subframe length                                                             */
     const opus_int                  nb_subfr,                               /* I    Number of subframes                                                         */
@@ -53,7 +53,7 @@
     for( k = 0; k < nb_subfr; k++ ) {
         lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 );
 
-        silk_sum_sqr_shift( &xx, &xx_shifts, r_ptr, subfr_length );                                    /* xx in Q( -xx_shifts ) */
+        silk_sum_sqr_shift( &xx, &xx_shifts, r_ptr, subfr_length + LTP_ORDER );                            /* xx in Q( -xx_shifts ) */
         silk_corrMatrix_FIX( lag_ptr, subfr_length, LTP_ORDER, XXLTP_Q17_ptr, &nrg, &XX_shifts, arch );    /* XXLTP_Q17_ptr and nrg in Q( -XX_shifts ) */
         extra_shifts = xx_shifts - XX_shifts;
         if( extra_shifts > 0 ) {
@@ -60,13 +60,13 @@
             /* Shift XX */
             xX_shifts = xx_shifts;
             for( i = 0; i < LTP_ORDER * LTP_ORDER; i++ ) {
-                XXLTP_Q17_ptr[ i ] = silk_RSHIFT32( XXLTP_Q17_ptr[ i ], extra_shifts );                /* Q( -xX_shifts ) */
+                XXLTP_Q17_ptr[ i ] = silk_RSHIFT32( XXLTP_Q17_ptr[ i ], extra_shifts );              /* Q( -xX_shifts ) */
             }
             nrg = silk_RSHIFT32( nrg, extra_shifts );                                                /* Q( -xX_shifts ) */
-        } else if( extra_shifts < 0 ) { 
+        } else if( extra_shifts < 0 ) {
             /* Shift xx */
             xX_shifts = XX_shifts;
-            xx = silk_RSHIFT32( xx, -extra_shifts );                                                /* Q( -xX_shifts ) */
+            xx = silk_RSHIFT32( xx, -extra_shifts );                                                 /* Q( -xX_shifts ) */
         } else {
             xX_shifts = xx_shifts;
         }
--- a/silk/fixed/find_pred_coefs_FIX.c
+++ b/silk/fixed/find_pred_coefs_FIX.c
@@ -86,7 +86,7 @@
         ALLOC( XXLTP_Q17, psEnc->sCmn.nb_subfr * LTP_ORDER * LTP_ORDER, opus_int32 );
 
         /* LTP analysis */
-        silk_find_LTP_FIX( XXLTP_Q17, xXLTP_Q17, res_pitch, 
+        silk_find_LTP_FIX( XXLTP_Q17, xXLTP_Q17, res_pitch,
             psEncCtrl->pitchL, psEnc->sCmn.subfr_length, psEnc->sCmn.nb_subfr, psEnc->sCmn.arch );
 
         /* Quantize LTP gain parameters */
--- a/silk/float/find_LTP_FLP.c
+++ b/silk/float/find_LTP_FLP.c
@@ -33,10 +33,10 @@
 #include "tuning_parameters.h"
 
 void silk_find_LTP_FLP(
-    silk_float                      XX[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O    Weight for LTP quantization       */
-    silk_float                      xX[ MAX_NB_SUBFR * LTP_ORDER ],        /* O    Weight for LTP quantization       */
+    silk_float                      XX[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O    Weight for LTP quantization         */
+    silk_float                      xX[ MAX_NB_SUBFR * LTP_ORDER ],     /* O    Weight for LTP quantization                 */
     const silk_float                r_ptr[],                            /* I    LPC residual                                */
-    const opus_int                  lag[  MAX_NB_SUBFR ],               /* I    LTP lags                                    */
+    const opus_int                  lag[ MAX_NB_SUBFR ],                /* I    LTP lags                                    */
     const opus_int                  subfr_length,                       /* I    Subframe length                             */
     const opus_int                  nb_subfr                            /* I    number of subframes                         */
 )
@@ -52,7 +52,7 @@
         lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 );
         silk_corrMatrix_FLP( lag_ptr, subfr_length, LTP_ORDER, XX_ptr );
         silk_corrVector_FLP( lag_ptr, r_ptr, subfr_length, LTP_ORDER, xX_ptr );
-        xx = ( silk_float )silk_energy_FLP( r_ptr, subfr_length );
+        xx = ( silk_float )silk_energy_FLP( r_ptr, subfr_length + LTP_ORDER );
         temp = 1.0f / silk_max( xx, LTP_CORR_INV_MAX * 0.5f * ( XX_ptr[ 0 ] + XX_ptr[ 24 ] ) + 1.0f );
         silk_scale_vector_FLP( XX_ptr, temp, LTP_ORDER * LTP_ORDER );
         silk_scale_vector_FLP( xX_ptr, temp, LTP_ORDER );
--- a/silk/main.h
+++ b/silk/main.h
@@ -210,7 +210,7 @@
     opus_int8                   *periodicity_index,                         /* O    Periodicity Index               */
     opus_int                    *pred_gain_dB_Q7,                           /* O    LTP prediction gain             */
     const opus_int32            XX_Q17[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ], /* I    Correlation matrix in Q18       */
-    const opus_int32            xX_Q17[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ], /* I    Correlation vector in Q18       */
+    const opus_int32            xX_Q17[ MAX_NB_SUBFR*LTP_ORDER ],           /* I    Correlation vector in Q18       */
     const opus_int              subfr_len,                                  /* I    Number of samples per subframe  */
     const opus_int              nb_subfr,                                   /* I    Number of subframes             */
     int                         arch                                        /* I    Run-time architecture           */
--- a/silk/quant_LTP_gains.c
+++ b/silk/quant_LTP_gains.c
@@ -32,13 +32,13 @@
 #include "main.h"
 
 void silk_quant_LTP_gains(
-    opus_int16                  B_Q14[ MAX_NB_SUBFR * LTP_ORDER ],          /* O    Quantized LTP gains                */
+    opus_int16                  B_Q14[ MAX_NB_SUBFR * LTP_ORDER ],          /* O    Quantized LTP gains             */
     opus_int8                   cbk_index[ MAX_NB_SUBFR ],                  /* O    Codebook Index                  */
     opus_int8                   *periodicity_index,                         /* O    Periodicity Index               */
-    opus_int                    *pred_gain_dB_Q7,                            /* O    LTP prediction gain                */
+    opus_int                    *pred_gain_dB_Q7,                           /* O    LTP prediction gain             */
     const opus_int32            XX_Q17[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ], /* I    Correlation matrix in Q18       */
-    const opus_int32            xX_Q17[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ], /* I    Correlation vector in Q18       */
-    const opus_int              subfr_len,                                    /* I    Number of samples per subframe  */
+    const opus_int32            xX_Q17[ MAX_NB_SUBFR*LTP_ORDER ],           /* I    Correlation vector in Q18       */
+    const opus_int              subfr_len,                                  /* I    Number of samples per subframe  */
     const opus_int              nb_subfr,                                   /* I    Number of subframes             */
     int                         arch                                        /* I    Run-time architecture           */
 )
@@ -47,7 +47,6 @@
     opus_int8            temp_idx[ MAX_NB_SUBFR ];
     const opus_uint8     *cl_ptr_Q5;
     const opus_int8      *cbk_ptr_Q7;
-    const opus_int16     *b_Q14_ptr;
     const opus_int32     *XX_Q17_ptr, *xX_Q17_ptr;
     opus_int32           res_nrg_Q15_subfr, res_nrg_Q15, rate_dist_Q7_subfr, rate_dist_Q7, min_rate_dist_Q7;
 
@@ -61,10 +60,9 @@
         cbk_ptr_Q7 = silk_LTP_vq_ptrs_Q7[        k ];
         cbk_size   = silk_LTP_vq_sizes[          k ];
 
-        /* Set up pointer to first subframe */
+        /* Set up pointers to first subframe */
         XX_Q17_ptr = XX_Q17;
         xX_Q17_ptr = xX_Q17;
-        b_Q14_ptr  = B_Q14;
 
         res_nrg_Q15 = 0;
         rate_dist_Q7 = 0;
@@ -71,21 +69,20 @@
         for( j = 0; j < nb_subfr; j++ ) {
             silk_VQ_WMat_EC(
                 &temp_idx[ j ],         /* O    index of best codebook vector                           */
-                &res_nrg_Q15_subfr,        /* O    residual energy                                            */
+                &res_nrg_Q15_subfr,     /* O    residual energy                                         */
                 &rate_dist_Q7_subfr,    /* O    best weighted quantization error + mu * rate            */
                 XX_Q17_ptr,             /* I    correlation matrix                                      */
-                xX_Q17_ptr,             /* I    correlation vector                                        */
+                xX_Q17_ptr,             /* I    correlation vector                                      */
                 cbk_ptr_Q7,             /* I    codebook                                                */
                 cl_ptr_Q5,              /* I    code length for each codebook vector                    */
-                subfr_len,              /* I    number of samples per subframe                            */
+                subfr_len,              /* I    number of samples per subframe                          */
                 cbk_size,               /* I    number of vectors in codebook                           */
                 arch                    /* I    Run-time architecture                                   */
             );
 
-            res_nrg_Q15   = silk_ADD_POS_SAT32( res_nrg_Q15, res_nrg_Q15_subfr );
+            res_nrg_Q15  = silk_ADD_POS_SAT32( res_nrg_Q15, res_nrg_Q15_subfr );
             rate_dist_Q7 = silk_ADD_POS_SAT32( rate_dist_Q7, rate_dist_Q7_subfr );
 
-            b_Q14_ptr  += LTP_ORDER;
             XX_Q17_ptr += LTP_ORDER * LTP_ORDER;
             xX_Q17_ptr += LTP_ORDER;
         }
@@ -112,4 +109,3 @@
 
     *pred_gain_dB_Q7 = (opus_int)silk_SMULBB( -3, silk_lin2log( res_nrg_Q15 ) - ( 15 << 7 ) );
 }
-
--- a/silk/tuning_parameters.h
+++ b/silk/tuning_parameters.h
@@ -54,7 +54,7 @@
 #define FIND_LPC_COND_FAC                               1e-5f
 
 /* LTP analysis defines */
-#define LTP_CORR_INV_MAX                                0.02f
+#define LTP_CORR_INV_MAX                                0.015f
 
 /***********************/
 /* High pass filtering */