Chris@69: /* Copyright (c) 2014, Cisco Systems, INC Chris@69: Written by XiangMingZhu WeiZhou MinPeng YanWang Chris@69: Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: Chris@69: - Redistributions of source code must retain the above copyright Chris@69: notice, this list of conditions and the following disclaimer. Chris@69: Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@69: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT Chris@69: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR Chris@69: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER Chris@69: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@69: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@69: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Chris@69: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF Chris@69: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING Chris@69: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@69: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@69: */ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include Chris@69: #include Chris@69: #include Chris@69: Chris@69: #include "main.h" Chris@69: #include "stack_alloc.h" Chris@69: Chris@69: /* Weighting factors for tilt measure */ Chris@69: static const opus_int32 tiltWeights[ VAD_N_BANDS ] = { 30000, 6000, -12000, -12000 }; Chris@69: Chris@69: /***************************************/ Chris@69: /* Get the speech activity level in Q8 */ Chris@69: /***************************************/ Chris@69: opus_int silk_VAD_GetSA_Q8_sse4_1( /* O Return value, 0 if success */ Chris@69: silk_encoder_state *psEncC, /* I/O Encoder state */ Chris@69: const opus_int16 pIn[] /* I PCM input */ Chris@69: ) Chris@69: { Chris@69: opus_int SA_Q15, pSNR_dB_Q7, input_tilt; Chris@69: opus_int decimated_framelength1, decimated_framelength2; Chris@69: opus_int decimated_framelength; Chris@69: opus_int dec_subframe_length, dec_subframe_offset, SNR_Q7, i, b, s; Chris@69: opus_int32 sumSquared, smooth_coef_Q16; Chris@69: opus_int16 HPstateTmp; Chris@69: VARDECL( opus_int16, X ); Chris@69: opus_int32 Xnrg[ VAD_N_BANDS ]; Chris@69: opus_int32 NrgToNoiseRatio_Q8[ VAD_N_BANDS ]; Chris@69: opus_int32 speech_nrg, x_tmp; Chris@69: opus_int X_offset[ VAD_N_BANDS ]; Chris@69: opus_int ret = 0; Chris@69: silk_VAD_state *psSilk_VAD = &psEncC->sVAD; Chris@69: Chris@69: SAVE_STACK; Chris@69: Chris@69: /* Safety checks */ Chris@69: silk_assert( VAD_N_BANDS == 4 ); Chris@69: celt_assert( MAX_FRAME_LENGTH >= psEncC->frame_length ); Chris@69: celt_assert( psEncC->frame_length <= 512 ); Chris@69: celt_assert( psEncC->frame_length == 8 * silk_RSHIFT( psEncC->frame_length, 3 ) ); Chris@69: Chris@69: /***********************/ Chris@69: /* Filter and Decimate */ Chris@69: /***********************/ Chris@69: decimated_framelength1 = silk_RSHIFT( psEncC->frame_length, 1 ); Chris@69: decimated_framelength2 = silk_RSHIFT( psEncC->frame_length, 2 ); Chris@69: decimated_framelength = silk_RSHIFT( psEncC->frame_length, 3 ); Chris@69: /* Decimate into 4 bands: Chris@69: 0 L 3L L 3L 5L Chris@69: - -- - -- -- Chris@69: 8 8 2 4 4 Chris@69: Chris@69: [0-1 kHz| temp. |1-2 kHz| 2-4 kHz | 4-8 kHz | Chris@69: Chris@69: They're arranged to allow the minimal ( frame_length / 4 ) extra Chris@69: scratch space during the downsampling process */ Chris@69: X_offset[ 0 ] = 0; Chris@69: X_offset[ 1 ] = decimated_framelength + decimated_framelength2; Chris@69: X_offset[ 2 ] = X_offset[ 1 ] + decimated_framelength; Chris@69: X_offset[ 3 ] = X_offset[ 2 ] + decimated_framelength2; Chris@69: ALLOC( X, X_offset[ 3 ] + decimated_framelength1, opus_int16 ); Chris@69: Chris@69: /* 0-8 kHz to 0-4 kHz and 4-8 kHz */ Chris@69: silk_ana_filt_bank_1( pIn, &psSilk_VAD->AnaState[ 0 ], Chris@69: X, &X[ X_offset[ 3 ] ], psEncC->frame_length ); Chris@69: Chris@69: /* 0-4 kHz to 0-2 kHz and 2-4 kHz */ Chris@69: silk_ana_filt_bank_1( X, &psSilk_VAD->AnaState1[ 0 ], Chris@69: X, &X[ X_offset[ 2 ] ], decimated_framelength1 ); Chris@69: Chris@69: /* 0-2 kHz to 0-1 kHz and 1-2 kHz */ Chris@69: silk_ana_filt_bank_1( X, &psSilk_VAD->AnaState2[ 0 ], Chris@69: X, &X[ X_offset[ 1 ] ], decimated_framelength2 ); Chris@69: Chris@69: /*********************************************/ Chris@69: /* HP filter on lowest band (differentiator) */ Chris@69: /*********************************************/ Chris@69: X[ decimated_framelength - 1 ] = silk_RSHIFT( X[ decimated_framelength - 1 ], 1 ); Chris@69: HPstateTmp = X[ decimated_framelength - 1 ]; Chris@69: for( i = decimated_framelength - 1; i > 0; i-- ) { Chris@69: X[ i - 1 ] = silk_RSHIFT( X[ i - 1 ], 1 ); Chris@69: X[ i ] -= X[ i - 1 ]; Chris@69: } Chris@69: X[ 0 ] -= psSilk_VAD->HPstate; Chris@69: psSilk_VAD->HPstate = HPstateTmp; Chris@69: Chris@69: /*************************************/ Chris@69: /* Calculate the energy in each band */ Chris@69: /*************************************/ Chris@69: for( b = 0; b < VAD_N_BANDS; b++ ) { Chris@69: /* Find the decimated framelength in the non-uniformly divided bands */ Chris@69: decimated_framelength = silk_RSHIFT( psEncC->frame_length, silk_min_int( VAD_N_BANDS - b, VAD_N_BANDS - 1 ) ); Chris@69: Chris@69: /* Split length into subframe lengths */ Chris@69: dec_subframe_length = silk_RSHIFT( decimated_framelength, VAD_INTERNAL_SUBFRAMES_LOG2 ); Chris@69: dec_subframe_offset = 0; Chris@69: Chris@69: /* Compute energy per sub-frame */ Chris@69: /* initialize with summed energy of last subframe */ Chris@69: Xnrg[ b ] = psSilk_VAD->XnrgSubfr[ b ]; Chris@69: for( s = 0; s < VAD_INTERNAL_SUBFRAMES; s++ ) { Chris@69: __m128i xmm_X, xmm_acc; Chris@69: sumSquared = 0; Chris@69: Chris@69: xmm_acc = _mm_setzero_si128(); Chris@69: Chris@69: for( i = 0; i < dec_subframe_length - 7; i += 8 ) Chris@69: { Chris@69: xmm_X = _mm_loadu_si128( (__m128i *)&(X[ X_offset[ b ] + i + dec_subframe_offset ] ) ); Chris@69: xmm_X = _mm_srai_epi16( xmm_X, 3 ); Chris@69: xmm_X = _mm_madd_epi16( xmm_X, xmm_X ); Chris@69: xmm_acc = _mm_add_epi32( xmm_acc, xmm_X ); Chris@69: } Chris@69: Chris@69: xmm_acc = _mm_add_epi32( xmm_acc, _mm_unpackhi_epi64( xmm_acc, xmm_acc ) ); Chris@69: xmm_acc = _mm_add_epi32( xmm_acc, _mm_shufflelo_epi16( xmm_acc, 0x0E ) ); Chris@69: Chris@69: sumSquared += _mm_cvtsi128_si32( xmm_acc ); Chris@69: Chris@69: for( ; i < dec_subframe_length; i++ ) { Chris@69: /* The energy will be less than dec_subframe_length * ( silk_int16_MIN / 8 ) ^ 2. */ Chris@69: /* Therefore we can accumulate with no risk of overflow (unless dec_subframe_length > 128) */ Chris@69: x_tmp = silk_RSHIFT( Chris@69: X[ X_offset[ b ] + i + dec_subframe_offset ], 3 ); Chris@69: sumSquared = silk_SMLABB( sumSquared, x_tmp, x_tmp ); Chris@69: Chris@69: /* Safety check */ Chris@69: silk_assert( sumSquared >= 0 ); Chris@69: } Chris@69: Chris@69: /* Add/saturate summed energy of current subframe */ Chris@69: if( s < VAD_INTERNAL_SUBFRAMES - 1 ) { Chris@69: Xnrg[ b ] = silk_ADD_POS_SAT32( Xnrg[ b ], sumSquared ); Chris@69: } else { Chris@69: /* Look-ahead subframe */ Chris@69: Xnrg[ b ] = silk_ADD_POS_SAT32( Xnrg[ b ], silk_RSHIFT( sumSquared, 1 ) ); Chris@69: } Chris@69: Chris@69: dec_subframe_offset += dec_subframe_length; Chris@69: } Chris@69: psSilk_VAD->XnrgSubfr[ b ] = sumSquared; Chris@69: } Chris@69: Chris@69: /********************/ Chris@69: /* Noise estimation */ Chris@69: /********************/ Chris@69: silk_VAD_GetNoiseLevels( &Xnrg[ 0 ], psSilk_VAD ); Chris@69: Chris@69: /***********************************************/ Chris@69: /* Signal-plus-noise to noise ratio estimation */ Chris@69: /***********************************************/ Chris@69: sumSquared = 0; Chris@69: input_tilt = 0; Chris@69: for( b = 0; b < VAD_N_BANDS; b++ ) { Chris@69: speech_nrg = Xnrg[ b ] - psSilk_VAD->NL[ b ]; Chris@69: if( speech_nrg > 0 ) { Chris@69: /* Divide, with sufficient resolution */ Chris@69: if( ( Xnrg[ b ] & 0xFF800000 ) == 0 ) { Chris@69: NrgToNoiseRatio_Q8[ b ] = silk_DIV32( silk_LSHIFT( Xnrg[ b ], 8 ), psSilk_VAD->NL[ b ] + 1 ); Chris@69: } else { Chris@69: NrgToNoiseRatio_Q8[ b ] = silk_DIV32( Xnrg[ b ], silk_RSHIFT( psSilk_VAD->NL[ b ], 8 ) + 1 ); Chris@69: } Chris@69: Chris@69: /* Convert to log domain */ Chris@69: SNR_Q7 = silk_lin2log( NrgToNoiseRatio_Q8[ b ] ) - 8 * 128; Chris@69: Chris@69: /* Sum-of-squares */ Chris@69: sumSquared = silk_SMLABB( sumSquared, SNR_Q7, SNR_Q7 ); /* Q14 */ Chris@69: Chris@69: /* Tilt measure */ Chris@69: if( speech_nrg < ( (opus_int32)1 << 20 ) ) { Chris@69: /* Scale down SNR value for small subband speech energies */ Chris@69: SNR_Q7 = silk_SMULWB( silk_LSHIFT( silk_SQRT_APPROX( speech_nrg ), 6 ), SNR_Q7 ); Chris@69: } Chris@69: input_tilt = silk_SMLAWB( input_tilt, tiltWeights[ b ], SNR_Q7 ); Chris@69: } else { Chris@69: NrgToNoiseRatio_Q8[ b ] = 256; Chris@69: } Chris@69: } Chris@69: Chris@69: /* Mean-of-squares */ Chris@69: sumSquared = silk_DIV32_16( sumSquared, VAD_N_BANDS ); /* Q14 */ Chris@69: Chris@69: /* Root-mean-square approximation, scale to dBs, and write to output pointer */ Chris@69: pSNR_dB_Q7 = (opus_int16)( 3 * silk_SQRT_APPROX( sumSquared ) ); /* Q7 */ Chris@69: Chris@69: /*********************************/ Chris@69: /* Speech Probability Estimation */ Chris@69: /*********************************/ Chris@69: SA_Q15 = silk_sigm_Q15( silk_SMULWB( VAD_SNR_FACTOR_Q16, pSNR_dB_Q7 ) - VAD_NEGATIVE_OFFSET_Q5 ); Chris@69: Chris@69: /**************************/ Chris@69: /* Frequency Tilt Measure */ Chris@69: /**************************/ Chris@69: psEncC->input_tilt_Q15 = silk_LSHIFT( silk_sigm_Q15( input_tilt ) - 16384, 1 ); Chris@69: Chris@69: /**************************************************/ Chris@69: /* Scale the sigmoid output based on power levels */ Chris@69: /**************************************************/ Chris@69: speech_nrg = 0; Chris@69: for( b = 0; b < VAD_N_BANDS; b++ ) { Chris@69: /* Accumulate signal-without-noise energies, higher frequency bands have more weight */ Chris@69: speech_nrg += ( b + 1 ) * silk_RSHIFT( Xnrg[ b ] - psSilk_VAD->NL[ b ], 4 ); Chris@69: } Chris@69: Chris@69: /* Power scaling */ Chris@69: if( speech_nrg <= 0 ) { Chris@69: SA_Q15 = silk_RSHIFT( SA_Q15, 1 ); Chris@69: } else if( speech_nrg < 32768 ) { Chris@69: if( psEncC->frame_length == 10 * psEncC->fs_kHz ) { Chris@69: speech_nrg = silk_LSHIFT_SAT32( speech_nrg, 16 ); Chris@69: } else { Chris@69: speech_nrg = silk_LSHIFT_SAT32( speech_nrg, 15 ); Chris@69: } Chris@69: Chris@69: /* square-root */ Chris@69: speech_nrg = silk_SQRT_APPROX( speech_nrg ); Chris@69: SA_Q15 = silk_SMULWB( 32768 + speech_nrg, SA_Q15 ); Chris@69: } Chris@69: Chris@69: /* Copy the resulting speech activity in Q8 */ Chris@69: psEncC->speech_activity_Q8 = silk_min_int( silk_RSHIFT( SA_Q15, 7 ), silk_uint8_MAX ); Chris@69: Chris@69: /***********************************/ Chris@69: /* Energy Level and SNR estimation */ Chris@69: /***********************************/ Chris@69: /* Smoothing coefficient */ Chris@69: smooth_coef_Q16 = silk_SMULWB( VAD_SNR_SMOOTH_COEF_Q18, silk_SMULWB( (opus_int32)SA_Q15, SA_Q15 ) ); Chris@69: Chris@69: if( psEncC->frame_length == 10 * psEncC->fs_kHz ) { Chris@69: smooth_coef_Q16 >>= 1; Chris@69: } Chris@69: Chris@69: for( b = 0; b < VAD_N_BANDS; b++ ) { Chris@69: /* compute smoothed energy-to-noise ratio per band */ Chris@69: psSilk_VAD->NrgRatioSmth_Q8[ b ] = silk_SMLAWB( psSilk_VAD->NrgRatioSmth_Q8[ b ], Chris@69: NrgToNoiseRatio_Q8[ b ] - psSilk_VAD->NrgRatioSmth_Q8[ b ], smooth_coef_Q16 ); Chris@69: Chris@69: /* signal to noise ratio in dB per band */ Chris@69: SNR_Q7 = 3 * ( silk_lin2log( psSilk_VAD->NrgRatioSmth_Q8[b] ) - 8 * 128 ); Chris@69: /* quality = sigmoid( 0.25 * ( SNR_dB - 16 ) ); */ Chris@69: psEncC->input_quality_bands_Q15[ b ] = silk_sigm_Q15( silk_RSHIFT( SNR_Q7 - 16 * 128, 4 ) ); Chris@69: } Chris@69: Chris@69: RESTORE_STACK; Chris@69: return( ret ); Chris@69: }