media/libopus/silk/CNG.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 /* Generates excitation for CNG LPC synthesis */
michael@0 36 static OPUS_INLINE void silk_CNG_exc(
michael@0 37 opus_int32 residual_Q10[], /* O CNG residual signal Q10 */
michael@0 38 opus_int32 exc_buf_Q14[], /* I Random samples buffer Q10 */
michael@0 39 opus_int32 Gain_Q16, /* I Gain to apply */
michael@0 40 opus_int length, /* I Length */
michael@0 41 opus_int32 *rand_seed /* I/O Seed to random index generator */
michael@0 42 )
michael@0 43 {
michael@0 44 opus_int32 seed;
michael@0 45 opus_int i, idx, exc_mask;
michael@0 46
michael@0 47 exc_mask = CNG_BUF_MASK_MAX;
michael@0 48 while( exc_mask > length ) {
michael@0 49 exc_mask = silk_RSHIFT( exc_mask, 1 );
michael@0 50 }
michael@0 51
michael@0 52 seed = *rand_seed;
michael@0 53 for( i = 0; i < length; i++ ) {
michael@0 54 seed = silk_RAND( seed );
michael@0 55 idx = (opus_int)( silk_RSHIFT( seed, 24 ) & exc_mask );
michael@0 56 silk_assert( idx >= 0 );
michael@0 57 silk_assert( idx <= CNG_BUF_MASK_MAX );
michael@0 58 residual_Q10[ i ] = (opus_int16)silk_SAT16( silk_SMULWW( exc_buf_Q14[ idx ], Gain_Q16 >> 4 ) );
michael@0 59 }
michael@0 60 *rand_seed = seed;
michael@0 61 }
michael@0 62
michael@0 63 void silk_CNG_Reset(
michael@0 64 silk_decoder_state *psDec /* I/O Decoder state */
michael@0 65 )
michael@0 66 {
michael@0 67 opus_int i, NLSF_step_Q15, NLSF_acc_Q15;
michael@0 68
michael@0 69 NLSF_step_Q15 = silk_DIV32_16( silk_int16_MAX, psDec->LPC_order + 1 );
michael@0 70 NLSF_acc_Q15 = 0;
michael@0 71 for( i = 0; i < psDec->LPC_order; i++ ) {
michael@0 72 NLSF_acc_Q15 += NLSF_step_Q15;
michael@0 73 psDec->sCNG.CNG_smth_NLSF_Q15[ i ] = NLSF_acc_Q15;
michael@0 74 }
michael@0 75 psDec->sCNG.CNG_smth_Gain_Q16 = 0;
michael@0 76 psDec->sCNG.rand_seed = 3176576;
michael@0 77 }
michael@0 78
michael@0 79 /* Updates CNG estimate, and applies the CNG when packet was lost */
michael@0 80 void silk_CNG(
michael@0 81 silk_decoder_state *psDec, /* I/O Decoder state */
michael@0 82 silk_decoder_control *psDecCtrl, /* I/O Decoder control */
michael@0 83 opus_int16 frame[], /* I/O Signal */
michael@0 84 opus_int length /* I Length of residual */
michael@0 85 )
michael@0 86 {
michael@0 87 opus_int i, subfr;
michael@0 88 opus_int32 sum_Q6, max_Gain_Q16;
michael@0 89 opus_int16 A_Q12[ MAX_LPC_ORDER ];
michael@0 90 silk_CNG_struct *psCNG = &psDec->sCNG;
michael@0 91 SAVE_STACK;
michael@0 92
michael@0 93 if( psDec->fs_kHz != psCNG->fs_kHz ) {
michael@0 94 /* Reset state */
michael@0 95 silk_CNG_Reset( psDec );
michael@0 96
michael@0 97 psCNG->fs_kHz = psDec->fs_kHz;
michael@0 98 }
michael@0 99 if( psDec->lossCnt == 0 && psDec->prevSignalType == TYPE_NO_VOICE_ACTIVITY ) {
michael@0 100 /* Update CNG parameters */
michael@0 101
michael@0 102 /* Smoothing of LSF's */
michael@0 103 for( i = 0; i < psDec->LPC_order; i++ ) {
michael@0 104 psCNG->CNG_smth_NLSF_Q15[ i ] += silk_SMULWB( (opus_int32)psDec->prevNLSF_Q15[ i ] - (opus_int32)psCNG->CNG_smth_NLSF_Q15[ i ], CNG_NLSF_SMTH_Q16 );
michael@0 105 }
michael@0 106 /* Find the subframe with the highest gain */
michael@0 107 max_Gain_Q16 = 0;
michael@0 108 subfr = 0;
michael@0 109 for( i = 0; i < psDec->nb_subfr; i++ ) {
michael@0 110 if( psDecCtrl->Gains_Q16[ i ] > max_Gain_Q16 ) {
michael@0 111 max_Gain_Q16 = psDecCtrl->Gains_Q16[ i ];
michael@0 112 subfr = i;
michael@0 113 }
michael@0 114 }
michael@0 115 /* Update CNG excitation buffer with excitation from this subframe */
michael@0 116 silk_memmove( &psCNG->CNG_exc_buf_Q14[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q14, ( psDec->nb_subfr - 1 ) * psDec->subfr_length * sizeof( opus_int32 ) );
michael@0 117 silk_memcpy( psCNG->CNG_exc_buf_Q14, &psDec->exc_Q14[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( opus_int32 ) );
michael@0 118
michael@0 119 /* Smooth gains */
michael@0 120 for( i = 0; i < psDec->nb_subfr; i++ ) {
michael@0 121 psCNG->CNG_smth_Gain_Q16 += silk_SMULWB( psDecCtrl->Gains_Q16[ i ] - psCNG->CNG_smth_Gain_Q16, CNG_GAIN_SMTH_Q16 );
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 /* Add CNG when packet is lost or during DTX */
michael@0 126 if( psDec->lossCnt ) {
michael@0 127 VARDECL( opus_int32, CNG_sig_Q10 );
michael@0 128
michael@0 129 ALLOC( CNG_sig_Q10, length + MAX_LPC_ORDER, opus_int32 );
michael@0 130
michael@0 131 /* Generate CNG excitation */
michael@0 132 silk_CNG_exc( CNG_sig_Q10 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q14, psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed );
michael@0 133
michael@0 134 /* Convert CNG NLSF to filter representation */
michael@0 135 silk_NLSF2A( A_Q12, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order );
michael@0 136
michael@0 137 /* Generate CNG signal, by synthesis filtering */
michael@0 138 silk_memcpy( CNG_sig_Q10, psCNG->CNG_synth_state, MAX_LPC_ORDER * sizeof( opus_int32 ) );
michael@0 139 for( i = 0; i < length; i++ ) {
michael@0 140 silk_assert( psDec->LPC_order == 10 || psDec->LPC_order == 16 );
michael@0 141 /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */
michael@0 142 sum_Q6 = silk_RSHIFT( psDec->LPC_order, 1 );
michael@0 143 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 1 ], A_Q12[ 0 ] );
michael@0 144 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 2 ], A_Q12[ 1 ] );
michael@0 145 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 3 ], A_Q12[ 2 ] );
michael@0 146 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 4 ], A_Q12[ 3 ] );
michael@0 147 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 5 ], A_Q12[ 4 ] );
michael@0 148 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 6 ], A_Q12[ 5 ] );
michael@0 149 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 7 ], A_Q12[ 6 ] );
michael@0 150 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 8 ], A_Q12[ 7 ] );
michael@0 151 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 9 ], A_Q12[ 8 ] );
michael@0 152 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 10 ], A_Q12[ 9 ] );
michael@0 153 if( psDec->LPC_order == 16 ) {
michael@0 154 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 11 ], A_Q12[ 10 ] );
michael@0 155 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 12 ], A_Q12[ 11 ] );
michael@0 156 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 13 ], A_Q12[ 12 ] );
michael@0 157 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 14 ], A_Q12[ 13 ] );
michael@0 158 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 15 ], A_Q12[ 14 ] );
michael@0 159 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 16 ], A_Q12[ 15 ] );
michael@0 160 }
michael@0 161
michael@0 162 /* Update states */
michael@0 163 CNG_sig_Q10[ MAX_LPC_ORDER + i ] = silk_ADD_LSHIFT( CNG_sig_Q10[ MAX_LPC_ORDER + i ], sum_Q6, 4 );
michael@0 164
michael@0 165 frame[ i ] = silk_ADD_SAT16( frame[ i ], silk_RSHIFT_ROUND( sum_Q6, 6 ) );
michael@0 166 }
michael@0 167 silk_memcpy( psCNG->CNG_synth_state, &CNG_sig_Q10[ length ], MAX_LPC_ORDER * sizeof( opus_int32 ) );
michael@0 168 } else {
michael@0 169 silk_memset( psCNG->CNG_synth_state, 0, psDec->LPC_order * sizeof( opus_int32 ) );
michael@0 170 }
michael@0 171 RESTORE_STACK;
michael@0 172 }

mercurial