media/libopus/silk/VAD.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial