VAD.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "main.h"
  31. #include "stack_alloc.h"
  32. /* Silk VAD noise level estimation */
  33. # if !defined(OPUS_X86_MAY_HAVE_SSE4_1)
  34. static OPUS_INLINE void silk_VAD_GetNoiseLevels(
  35. const opus_int32 pX[ VAD_N_BANDS ], /* I subband energies */
  36. silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */
  37. );
  38. #endif
  39. /**********************************/
  40. /* Initialization of the Silk VAD */
  41. /**********************************/
  42. opus_int silk_VAD_Init( /* O Return value, 0 if success */
  43. silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */
  44. )
  45. {
  46. opus_int b, ret = 0;
  47. /* reset state memory */
  48. silk_memset( psSilk_VAD, 0, sizeof( silk_VAD_state ) );
  49. /* init noise levels */
  50. /* Initialize array with approx pink noise levels (psd proportional to inverse of frequency) */
  51. for( b = 0; b < VAD_N_BANDS; b++ ) {
  52. psSilk_VAD->NoiseLevelBias[ b ] = silk_max_32( silk_DIV32_16( VAD_NOISE_LEVELS_BIAS, b + 1 ), 1 );
  53. }
  54. /* Initialize state */
  55. for( b = 0; b < VAD_N_BANDS; b++ ) {
  56. psSilk_VAD->NL[ b ] = silk_MUL( 100, psSilk_VAD->NoiseLevelBias[ b ] );
  57. psSilk_VAD->inv_NL[ b ] = silk_DIV32( silk_int32_MAX, psSilk_VAD->NL[ b ] );
  58. }
  59. psSilk_VAD->counter = 15;
  60. /* init smoothed energy-to-noise ratio*/
  61. for( b = 0; b < VAD_N_BANDS; b++ ) {
  62. psSilk_VAD->NrgRatioSmth_Q8[ b ] = 100 * 256; /* 100 * 256 --> 20 dB SNR */
  63. }
  64. return( ret );
  65. }
  66. /* Weighting factors for tilt measure */
  67. static const opus_int32 tiltWeights[ VAD_N_BANDS ] = { 30000, 6000, -12000, -12000 };
  68. /***************************************/
  69. /* Get the speech activity level in Q8 */
  70. /***************************************/
  71. opus_int silk_VAD_GetSA_Q8_c( /* O Return value, 0 if success */
  72. silk_encoder_state *psEncC, /* I/O Encoder state */
  73. const opus_int16 pIn[] /* I PCM input */
  74. )
  75. {
  76. opus_int SA_Q15, pSNR_dB_Q7, input_tilt;
  77. opus_int decimated_framelength1, decimated_framelength2;
  78. opus_int decimated_framelength;
  79. opus_int dec_subframe_length, dec_subframe_offset, SNR_Q7, i, b, s;
  80. opus_int32 sumSquared, smooth_coef_Q16;
  81. opus_int16 HPstateTmp;
  82. VARDECL( opus_int16, X );
  83. opus_int32 Xnrg[ VAD_N_BANDS ];
  84. opus_int32 NrgToNoiseRatio_Q8[ VAD_N_BANDS ];
  85. opus_int32 speech_nrg, x_tmp;
  86. opus_int X_offset[ VAD_N_BANDS ];
  87. opus_int ret = 0;
  88. silk_VAD_state *psSilk_VAD = &psEncC->sVAD;
  89. SAVE_STACK;
  90. /* Safety checks */
  91. silk_assert( VAD_N_BANDS == 4 );
  92. celt_assert( MAX_FRAME_LENGTH >= psEncC->frame_length );
  93. celt_assert( psEncC->frame_length <= 512 );
  94. celt_assert( psEncC->frame_length == 8 * silk_RSHIFT( psEncC->frame_length, 3 ) );
  95. /***********************/
  96. /* Filter and Decimate */
  97. /***********************/
  98. decimated_framelength1 = silk_RSHIFT( psEncC->frame_length, 1 );
  99. decimated_framelength2 = silk_RSHIFT( psEncC->frame_length, 2 );
  100. decimated_framelength = silk_RSHIFT( psEncC->frame_length, 3 );
  101. /* Decimate into 4 bands:
  102. 0 L 3L L 3L 5L
  103. - -- - -- --
  104. 8 8 2 4 4
  105. [0-1 kHz| temp. |1-2 kHz| 2-4 kHz | 4-8 kHz |
  106. They're arranged to allow the minimal ( frame_length / 4 ) extra
  107. scratch space during the downsampling process */
  108. X_offset[ 0 ] = 0;
  109. X_offset[ 1 ] = decimated_framelength + decimated_framelength2;
  110. X_offset[ 2 ] = X_offset[ 1 ] + decimated_framelength;
  111. X_offset[ 3 ] = X_offset[ 2 ] + decimated_framelength2;
  112. ALLOC( X, X_offset[ 3 ] + decimated_framelength1, opus_int16 );
  113. /* 0-8 kHz to 0-4 kHz and 4-8 kHz */
  114. silk_ana_filt_bank_1( pIn, &psSilk_VAD->AnaState[ 0 ],
  115. X, &X[ X_offset[ 3 ] ], psEncC->frame_length );
  116. /* 0-4 kHz to 0-2 kHz and 2-4 kHz */
  117. silk_ana_filt_bank_1( X, &psSilk_VAD->AnaState1[ 0 ],
  118. X, &X[ X_offset[ 2 ] ], decimated_framelength1 );
  119. /* 0-2 kHz to 0-1 kHz and 1-2 kHz */
  120. silk_ana_filt_bank_1( X, &psSilk_VAD->AnaState2[ 0 ],
  121. X, &X[ X_offset[ 1 ] ], decimated_framelength2 );
  122. /*********************************************/
  123. /* HP filter on lowest band (differentiator) */
  124. /*********************************************/
  125. X[ decimated_framelength - 1 ] = silk_RSHIFT( X[ decimated_framelength - 1 ], 1 );
  126. HPstateTmp = X[ decimated_framelength - 1 ];
  127. for( i = decimated_framelength - 1; i > 0; i-- ) {
  128. X[ i - 1 ] = silk_RSHIFT( X[ i - 1 ], 1 );
  129. X[ i ] -= X[ i - 1 ];
  130. }
  131. X[ 0 ] -= psSilk_VAD->HPstate;
  132. psSilk_VAD->HPstate = HPstateTmp;
  133. /*************************************/
  134. /* Calculate the energy in each band */
  135. /*************************************/
  136. for( b = 0; b < VAD_N_BANDS; b++ ) {
  137. /* Find the decimated framelength in the non-uniformly divided bands */
  138. decimated_framelength = silk_RSHIFT( psEncC->frame_length, silk_min_int( VAD_N_BANDS - b, VAD_N_BANDS - 1 ) );
  139. /* Split length into subframe lengths */
  140. dec_subframe_length = silk_RSHIFT( decimated_framelength, VAD_INTERNAL_SUBFRAMES_LOG2 );
  141. dec_subframe_offset = 0;
  142. /* Compute energy per sub-frame */
  143. /* initialize with summed energy of last subframe */
  144. Xnrg[ b ] = psSilk_VAD->XnrgSubfr[ b ];
  145. for( s = 0; s < VAD_INTERNAL_SUBFRAMES; s++ ) {
  146. sumSquared = 0;
  147. for( i = 0; i < dec_subframe_length; i++ ) {
  148. /* The energy will be less than dec_subframe_length * ( silk_int16_MIN / 8 ) ^ 2. */
  149. /* Therefore we can accumulate with no risk of overflow (unless dec_subframe_length > 128) */
  150. x_tmp = silk_RSHIFT(
  151. X[ X_offset[ b ] + i + dec_subframe_offset ], 3 );
  152. sumSquared = silk_SMLABB( sumSquared, x_tmp, x_tmp );
  153. /* Safety check */
  154. silk_assert( sumSquared >= 0 );
  155. }
  156. /* Add/saturate summed energy of current subframe */
  157. if( s < VAD_INTERNAL_SUBFRAMES - 1 ) {
  158. Xnrg[ b ] = silk_ADD_POS_SAT32( Xnrg[ b ], sumSquared );
  159. } else {
  160. /* Look-ahead subframe */
  161. Xnrg[ b ] = silk_ADD_POS_SAT32( Xnrg[ b ], silk_RSHIFT( sumSquared, 1 ) );
  162. }
  163. dec_subframe_offset += dec_subframe_length;
  164. }
  165. psSilk_VAD->XnrgSubfr[ b ] = sumSquared;
  166. }
  167. /********************/
  168. /* Noise estimation */
  169. /********************/
  170. silk_VAD_GetNoiseLevels( &Xnrg[ 0 ], psSilk_VAD );
  171. /***********************************************/
  172. /* Signal-plus-noise to noise ratio estimation */
  173. /***********************************************/
  174. sumSquared = 0;
  175. input_tilt = 0;
  176. for( b = 0; b < VAD_N_BANDS; b++ ) {
  177. speech_nrg = Xnrg[ b ] - psSilk_VAD->NL[ b ];
  178. if( speech_nrg > 0 ) {
  179. /* Divide, with sufficient resolution */
  180. if( ( Xnrg[ b ] & 0xFF800000 ) == 0 ) {
  181. NrgToNoiseRatio_Q8[ b ] = silk_DIV32( silk_LSHIFT( Xnrg[ b ], 8 ), psSilk_VAD->NL[ b ] + 1 );
  182. } else {
  183. NrgToNoiseRatio_Q8[ b ] = silk_DIV32( Xnrg[ b ], silk_RSHIFT( psSilk_VAD->NL[ b ], 8 ) + 1 );
  184. }
  185. /* Convert to log domain */
  186. SNR_Q7 = silk_lin2log( NrgToNoiseRatio_Q8[ b ] ) - 8 * 128;
  187. /* Sum-of-squares */
  188. sumSquared = silk_SMLABB( sumSquared, SNR_Q7, SNR_Q7 ); /* Q14 */
  189. /* Tilt measure */
  190. if( speech_nrg < ( (opus_int32)1 << 20 ) ) {
  191. /* Scale down SNR value for small subband speech energies */
  192. SNR_Q7 = silk_SMULWB( silk_LSHIFT( silk_SQRT_APPROX( speech_nrg ), 6 ), SNR_Q7 );
  193. }
  194. input_tilt = silk_SMLAWB( input_tilt, tiltWeights[ b ], SNR_Q7 );
  195. } else {
  196. NrgToNoiseRatio_Q8[ b ] = 256;
  197. }
  198. }
  199. /* Mean-of-squares */
  200. sumSquared = silk_DIV32_16( sumSquared, VAD_N_BANDS ); /* Q14 */
  201. /* Root-mean-square approximation, scale to dBs, and write to output pointer */
  202. pSNR_dB_Q7 = (opus_int16)( 3 * silk_SQRT_APPROX( sumSquared ) ); /* Q7 */
  203. /*********************************/
  204. /* Speech Probability Estimation */
  205. /*********************************/
  206. SA_Q15 = silk_sigm_Q15( silk_SMULWB( VAD_SNR_FACTOR_Q16, pSNR_dB_Q7 ) - VAD_NEGATIVE_OFFSET_Q5 );
  207. /**************************/
  208. /* Frequency Tilt Measure */
  209. /**************************/
  210. psEncC->input_tilt_Q15 = silk_LSHIFT( silk_sigm_Q15( input_tilt ) - 16384, 1 );
  211. /**************************************************/
  212. /* Scale the sigmoid output based on power levels */
  213. /**************************************************/
  214. speech_nrg = 0;
  215. for( b = 0; b < VAD_N_BANDS; b++ ) {
  216. /* Accumulate signal-without-noise energies, higher frequency bands have more weight */
  217. speech_nrg += ( b + 1 ) * silk_RSHIFT( Xnrg[ b ] - psSilk_VAD->NL[ b ], 4 );
  218. }
  219. if( psEncC->frame_length == 20 * psEncC->fs_kHz ) {
  220. speech_nrg = silk_RSHIFT32( speech_nrg, 1 );
  221. }
  222. /* Power scaling */
  223. if( speech_nrg <= 0 ) {
  224. SA_Q15 = silk_RSHIFT( SA_Q15, 1 );
  225. } else if( speech_nrg < 16384 ) {
  226. speech_nrg = silk_LSHIFT32( speech_nrg, 16 );
  227. /* square-root */
  228. speech_nrg = silk_SQRT_APPROX( speech_nrg );
  229. SA_Q15 = silk_SMULWB( 32768 + speech_nrg, SA_Q15 );
  230. }
  231. /* Copy the resulting speech activity in Q8 */
  232. psEncC->speech_activity_Q8 = silk_min_int( silk_RSHIFT( SA_Q15, 7 ), silk_uint8_MAX );
  233. /***********************************/
  234. /* Energy Level and SNR estimation */
  235. /***********************************/
  236. /* Smoothing coefficient */
  237. smooth_coef_Q16 = silk_SMULWB( VAD_SNR_SMOOTH_COEF_Q18, silk_SMULWB( (opus_int32)SA_Q15, SA_Q15 ) );
  238. if( psEncC->frame_length == 10 * psEncC->fs_kHz ) {
  239. smooth_coef_Q16 >>= 1;
  240. }
  241. for( b = 0; b < VAD_N_BANDS; b++ ) {
  242. /* compute smoothed energy-to-noise ratio per band */
  243. psSilk_VAD->NrgRatioSmth_Q8[ b ] = silk_SMLAWB( psSilk_VAD->NrgRatioSmth_Q8[ b ],
  244. NrgToNoiseRatio_Q8[ b ] - psSilk_VAD->NrgRatioSmth_Q8[ b ], smooth_coef_Q16 );
  245. /* signal to noise ratio in dB per band */
  246. SNR_Q7 = 3 * ( silk_lin2log( psSilk_VAD->NrgRatioSmth_Q8[b] ) - 8 * 128 );
  247. /* quality = sigmoid( 0.25 * ( SNR_dB - 16 ) ); */
  248. psEncC->input_quality_bands_Q15[ b ] = silk_sigm_Q15( silk_RSHIFT( SNR_Q7 - 16 * 128, 4 ) );
  249. }
  250. RESTORE_STACK;
  251. return( ret );
  252. }
  253. /**************************/
  254. /* Noise level estimation */
  255. /**************************/
  256. # if !defined(OPUS_X86_MAY_HAVE_SSE4_1)
  257. static OPUS_INLINE
  258. #endif
  259. void silk_VAD_GetNoiseLevels(
  260. const opus_int32 pX[ VAD_N_BANDS ], /* I subband energies */
  261. silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */
  262. )
  263. {
  264. opus_int k;
  265. opus_int32 nl, nrg, inv_nrg;
  266. opus_int coef, min_coef;
  267. /* Initially faster smoothing */
  268. if( psSilk_VAD->counter < 1000 ) { /* 1000 = 20 sec */
  269. min_coef = silk_DIV32_16( silk_int16_MAX, silk_RSHIFT( psSilk_VAD->counter, 4 ) + 1 );
  270. /* Increment frame counter */
  271. psSilk_VAD->counter++;
  272. } else {
  273. min_coef = 0;
  274. }
  275. for( k = 0; k < VAD_N_BANDS; k++ ) {
  276. /* Get old noise level estimate for current band */
  277. nl = psSilk_VAD->NL[ k ];
  278. silk_assert( nl >= 0 );
  279. /* Add bias */
  280. nrg = silk_ADD_POS_SAT32( pX[ k ], psSilk_VAD->NoiseLevelBias[ k ] );
  281. silk_assert( nrg > 0 );
  282. /* Invert energies */
  283. inv_nrg = silk_DIV32( silk_int32_MAX, nrg );
  284. silk_assert( inv_nrg >= 0 );
  285. /* Less update when subband energy is high */
  286. if( nrg > silk_LSHIFT( nl, 3 ) ) {
  287. coef = VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 >> 3;
  288. } else if( nrg < nl ) {
  289. coef = VAD_NOISE_LEVEL_SMOOTH_COEF_Q16;
  290. } else {
  291. coef = silk_SMULWB( silk_SMULWW( inv_nrg, nl ), VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 << 1 );
  292. }
  293. /* Initially faster smoothing */
  294. coef = silk_max_int( coef, min_coef );
  295. /* Smooth inverse energies */
  296. psSilk_VAD->inv_NL[ k ] = silk_SMLAWB( psSilk_VAD->inv_NL[ k ], inv_nrg - psSilk_VAD->inv_NL[ k ], coef );
  297. silk_assert( psSilk_VAD->inv_NL[ k ] >= 0 );
  298. /* Compute noise level by inverting again */
  299. nl = silk_DIV32( silk_int32_MAX, psSilk_VAD->inv_NL[ k ] );
  300. silk_assert( nl >= 0 );
  301. /* Limit noise levels (guarantee 7 bits of head room) */
  302. nl = silk_min( nl, 0x00FFFFFF );
  303. /* Store as part of state */
  304. psSilk_VAD->NL[ k ] = nl;
  305. }
  306. }