Thu, 15 Jan 2015 15:59:08 +0100
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 | /* conversion between prediction filter coefficients and LSFs */ |
michael@0 | 33 | /* order should be even */ |
michael@0 | 34 | /* a piecewise linear approximation maps LSF <-> cos(LSF) */ |
michael@0 | 35 | /* therefore the result is not accurate LSFs, but the two */ |
michael@0 | 36 | /* functions are accurate inverses of each other */ |
michael@0 | 37 | |
michael@0 | 38 | #include "SigProc_FIX.h" |
michael@0 | 39 | #include "tables.h" |
michael@0 | 40 | |
michael@0 | 41 | #define QA 16 |
michael@0 | 42 | |
michael@0 | 43 | /* helper function for NLSF2A(..) */ |
michael@0 | 44 | static OPUS_INLINE void silk_NLSF2A_find_poly( |
michael@0 | 45 | opus_int32 *out, /* O intermediate polynomial, QA [dd+1] */ |
michael@0 | 46 | const opus_int32 *cLSF, /* I vector of interleaved 2*cos(LSFs), QA [d] */ |
michael@0 | 47 | opus_int dd /* I polynomial order (= 1/2 * filter order) */ |
michael@0 | 48 | ) |
michael@0 | 49 | { |
michael@0 | 50 | opus_int k, n; |
michael@0 | 51 | opus_int32 ftmp; |
michael@0 | 52 | |
michael@0 | 53 | out[0] = silk_LSHIFT( 1, QA ); |
michael@0 | 54 | out[1] = -cLSF[0]; |
michael@0 | 55 | for( k = 1; k < dd; k++ ) { |
michael@0 | 56 | ftmp = cLSF[2*k]; /* QA*/ |
michael@0 | 57 | out[k+1] = silk_LSHIFT( out[k-1], 1 ) - (opus_int32)silk_RSHIFT_ROUND64( silk_SMULL( ftmp, out[k] ), QA ); |
michael@0 | 58 | for( n = k; n > 1; n-- ) { |
michael@0 | 59 | out[n] += out[n-2] - (opus_int32)silk_RSHIFT_ROUND64( silk_SMULL( ftmp, out[n-1] ), QA ); |
michael@0 | 60 | } |
michael@0 | 61 | out[1] -= ftmp; |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | /* compute whitening filter coefficients from normalized line spectral frequencies */ |
michael@0 | 66 | void silk_NLSF2A( |
michael@0 | 67 | opus_int16 *a_Q12, /* O monic whitening filter coefficients in Q12, [ d ] */ |
michael@0 | 68 | const opus_int16 *NLSF, /* I normalized line spectral frequencies in Q15, [ d ] */ |
michael@0 | 69 | const opus_int d /* I filter order (should be even) */ |
michael@0 | 70 | ) |
michael@0 | 71 | { |
michael@0 | 72 | /* This ordering was found to maximize quality. It improves numerical accuracy of |
michael@0 | 73 | silk_NLSF2A_find_poly() compared to "standard" ordering. */ |
michael@0 | 74 | static const unsigned char ordering16[16] = { |
michael@0 | 75 | 0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1 |
michael@0 | 76 | }; |
michael@0 | 77 | static const unsigned char ordering10[10] = { |
michael@0 | 78 | 0, 9, 6, 3, 4, 5, 8, 1, 2, 7 |
michael@0 | 79 | }; |
michael@0 | 80 | const unsigned char *ordering; |
michael@0 | 81 | opus_int k, i, dd; |
michael@0 | 82 | opus_int32 cos_LSF_QA[ SILK_MAX_ORDER_LPC ]; |
michael@0 | 83 | opus_int32 P[ SILK_MAX_ORDER_LPC / 2 + 1 ], Q[ SILK_MAX_ORDER_LPC / 2 + 1 ]; |
michael@0 | 84 | opus_int32 Ptmp, Qtmp, f_int, f_frac, cos_val, delta; |
michael@0 | 85 | opus_int32 a32_QA1[ SILK_MAX_ORDER_LPC ]; |
michael@0 | 86 | opus_int32 maxabs, absval, idx=0, sc_Q16; |
michael@0 | 87 | |
michael@0 | 88 | silk_assert( LSF_COS_TAB_SZ_FIX == 128 ); |
michael@0 | 89 | silk_assert( d==10||d==16 ); |
michael@0 | 90 | |
michael@0 | 91 | /* convert LSFs to 2*cos(LSF), using piecewise linear curve from table */ |
michael@0 | 92 | ordering = d == 16 ? ordering16 : ordering10; |
michael@0 | 93 | for( k = 0; k < d; k++ ) { |
michael@0 | 94 | silk_assert(NLSF[k] >= 0 ); |
michael@0 | 95 | |
michael@0 | 96 | /* f_int on a scale 0-127 (rounded down) */ |
michael@0 | 97 | f_int = silk_RSHIFT( NLSF[k], 15 - 7 ); |
michael@0 | 98 | |
michael@0 | 99 | /* f_frac, range: 0..255 */ |
michael@0 | 100 | f_frac = NLSF[k] - silk_LSHIFT( f_int, 15 - 7 ); |
michael@0 | 101 | |
michael@0 | 102 | silk_assert(f_int >= 0); |
michael@0 | 103 | silk_assert(f_int < LSF_COS_TAB_SZ_FIX ); |
michael@0 | 104 | |
michael@0 | 105 | /* Read start and end value from table */ |
michael@0 | 106 | cos_val = silk_LSFCosTab_FIX_Q12[ f_int ]; /* Q12 */ |
michael@0 | 107 | delta = silk_LSFCosTab_FIX_Q12[ f_int + 1 ] - cos_val; /* Q12, with a range of 0..200 */ |
michael@0 | 108 | |
michael@0 | 109 | /* Linear interpolation */ |
michael@0 | 110 | cos_LSF_QA[ordering[k]] = silk_RSHIFT_ROUND( silk_LSHIFT( cos_val, 8 ) + silk_MUL( delta, f_frac ), 20 - QA ); /* QA */ |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | dd = silk_RSHIFT( d, 1 ); |
michael@0 | 114 | |
michael@0 | 115 | /* generate even and odd polynomials using convolution */ |
michael@0 | 116 | silk_NLSF2A_find_poly( P, &cos_LSF_QA[ 0 ], dd ); |
michael@0 | 117 | silk_NLSF2A_find_poly( Q, &cos_LSF_QA[ 1 ], dd ); |
michael@0 | 118 | |
michael@0 | 119 | /* convert even and odd polynomials to opus_int32 Q12 filter coefs */ |
michael@0 | 120 | for( k = 0; k < dd; k++ ) { |
michael@0 | 121 | Ptmp = P[ k+1 ] + P[ k ]; |
michael@0 | 122 | Qtmp = Q[ k+1 ] - Q[ k ]; |
michael@0 | 123 | |
michael@0 | 124 | /* the Ptmp and Qtmp values at this stage need to fit in int32 */ |
michael@0 | 125 | a32_QA1[ k ] = -Qtmp - Ptmp; /* QA+1 */ |
michael@0 | 126 | a32_QA1[ d-k-1 ] = Qtmp - Ptmp; /* QA+1 */ |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | /* Limit the maximum absolute value of the prediction coefficients, so that they'll fit in int16 */ |
michael@0 | 130 | for( i = 0; i < 10; i++ ) { |
michael@0 | 131 | /* Find maximum absolute value and its index */ |
michael@0 | 132 | maxabs = 0; |
michael@0 | 133 | for( k = 0; k < d; k++ ) { |
michael@0 | 134 | absval = silk_abs( a32_QA1[k] ); |
michael@0 | 135 | if( absval > maxabs ) { |
michael@0 | 136 | maxabs = absval; |
michael@0 | 137 | idx = k; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | maxabs = silk_RSHIFT_ROUND( maxabs, QA + 1 - 12 ); /* QA+1 -> Q12 */ |
michael@0 | 141 | |
michael@0 | 142 | if( maxabs > silk_int16_MAX ) { |
michael@0 | 143 | /* Reduce magnitude of prediction coefficients */ |
michael@0 | 144 | maxabs = silk_min( maxabs, 163838 ); /* ( silk_int32_MAX >> 14 ) + silk_int16_MAX = 163838 */ |
michael@0 | 145 | sc_Q16 = SILK_FIX_CONST( 0.999, 16 ) - silk_DIV32( silk_LSHIFT( maxabs - silk_int16_MAX, 14 ), |
michael@0 | 146 | silk_RSHIFT32( silk_MUL( maxabs, idx + 1), 2 ) ); |
michael@0 | 147 | silk_bwexpander_32( a32_QA1, d, sc_Q16 ); |
michael@0 | 148 | } else { |
michael@0 | 149 | break; |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | if( i == 10 ) { |
michael@0 | 154 | /* Reached the last iteration, clip the coefficients */ |
michael@0 | 155 | for( k = 0; k < d; k++ ) { |
michael@0 | 156 | a_Q12[ k ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ) ); /* QA+1 -> Q12 */ |
michael@0 | 157 | a32_QA1[ k ] = silk_LSHIFT( (opus_int32)a_Q12[ k ], QA + 1 - 12 ); |
michael@0 | 158 | } |
michael@0 | 159 | } else { |
michael@0 | 160 | for( k = 0; k < d; k++ ) { |
michael@0 | 161 | a_Q12[ k ] = (opus_int16)silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ); /* QA+1 -> Q12 */ |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | for( i = 0; i < MAX_LPC_STABILIZE_ITERATIONS; i++ ) { |
michael@0 | 166 | if( silk_LPC_inverse_pred_gain( a_Q12, d ) < SILK_FIX_CONST( 1.0 / MAX_PREDICTION_POWER_GAIN, 30 ) ) { |
michael@0 | 167 | /* Prediction coefficients are (too close to) unstable; apply bandwidth expansion */ |
michael@0 | 168 | /* on the unscaled coefficients, convert to Q12 and measure again */ |
michael@0 | 169 | silk_bwexpander_32( a32_QA1, d, 65536 - silk_LSHIFT( 2, i ) ); |
michael@0 | 170 | for( k = 0; k < d; k++ ) { |
michael@0 | 171 | a_Q12[ k ] = (opus_int16)silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ); /* QA+1 -> Q12 */ |
michael@0 | 172 | } |
michael@0 | 173 | } else { |
michael@0 | 174 | break; |
michael@0 | 175 | } |
michael@0 | 176 | } |
michael@0 | 177 | } |
michael@0 | 178 |