Fixes CNG overflows from the previous commit
authorKoen Vos <koenvos74@gmail.com>
Fri, 20 Jun 2014 05:42:49 +0000 (01:42 -0400)
committerJean-Marc Valin <jmvalin@jmvalin.ca>
Fri, 20 Jun 2014 05:42:49 +0000 (01:42 -0400)
Signed-off-by: Jean-Marc Valin <jmvalin@jmvalin.ca>
silk/CNG.c

index cd15001..bb30a7c 100644 (file)
@@ -125,15 +125,19 @@ void silk_CNG(
     /* Add CNG when packet is lost or during DTX */
     if( psDec->lossCnt ) {
         VARDECL( opus_int32, CNG_sig_Q10 );
-
         ALLOC( CNG_sig_Q10, length + MAX_LPC_ORDER, opus_int32 );
 
         /* Generate CNG excitation */
                gain_Q16 = silk_SMULWW( psDec->sPLC.randScale_Q14, psDec->sPLC.prevGain_Q16[1] );
-               gain_Q16 = silk_SMULWW( gain_Q16, gain_Q16 );
-               gain_Q16 = silk_SUB_LSHIFT32(silk_SMULWW( psCNG->CNG_smth_Gain_Q16, psCNG->CNG_smth_Gain_Q16 ), gain_Q16, 5 );
-               gain_Q16 = silk_max_32( gain_Q16, 0 );
-               gain_Q16 = silk_LSHIFT32( silk_SQRT_APPROX( gain_Q16 ), 8 );
+               if( gain_Q16 >= (1 << 21) || psCNG->CNG_smth_Gain_Q16 > (1 << 23) ) {
+                       gain_Q16 = silk_SMULTT( gain_Q16, gain_Q16 );
+                       gain_Q16 = silk_SUB_LSHIFT32(silk_SMULTT( psCNG->CNG_smth_Gain_Q16, psCNG->CNG_smth_Gain_Q16 ), gain_Q16, 5 );
+                       gain_Q16 = silk_LSHIFT32( silk_SQRT_APPROX( gain_Q16 ), 16 );
+               } else {
+                       gain_Q16 = silk_SMULWW( gain_Q16, gain_Q16 );
+                       gain_Q16 = silk_SUB_LSHIFT32(silk_SMULWW( psCNG->CNG_smth_Gain_Q16, psCNG->CNG_smth_Gain_Q16 ), gain_Q16, 5 );
+                       gain_Q16 = silk_LSHIFT32( silk_SQRT_APPROX( gain_Q16 ), 8 );
+               }
         silk_CNG_exc( CNG_sig_Q10 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q14, gain_Q16, length, &psCNG->rand_seed );
 
         /* Convert CNG NLSF to filter representation */