media/libopus/silk/fixed/find_LTP_FIX.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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_FIX.h"
michael@0 33 #include "tuning_parameters.h"
michael@0 34
michael@0 35 /* Head room for correlations */
michael@0 36 #define LTP_CORRS_HEAD_ROOM 2
michael@0 37
michael@0 38 void silk_fit_LTP(
michael@0 39 opus_int32 LTP_coefs_Q16[ LTP_ORDER ],
michael@0 40 opus_int16 LTP_coefs_Q14[ LTP_ORDER ]
michael@0 41 );
michael@0 42
michael@0 43 void silk_find_LTP_FIX(
michael@0 44 opus_int16 b_Q14[ MAX_NB_SUBFR * LTP_ORDER ], /* O LTP coefs */
michael@0 45 opus_int32 WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */
michael@0 46 opus_int *LTPredCodGain_Q7, /* O LTP coding gain */
michael@0 47 const opus_int16 r_lpc[], /* I residual signal after LPC signal + state for first 10 ms */
michael@0 48 const opus_int lag[ MAX_NB_SUBFR ], /* I LTP lags */
michael@0 49 const opus_int32 Wght_Q15[ MAX_NB_SUBFR ], /* I weights */
michael@0 50 const opus_int subfr_length, /* I subframe length */
michael@0 51 const opus_int nb_subfr, /* I number of subframes */
michael@0 52 const opus_int mem_offset, /* I number of samples in LTP memory */
michael@0 53 opus_int corr_rshifts[ MAX_NB_SUBFR ] /* O right shifts applied to correlations */
michael@0 54 )
michael@0 55 {
michael@0 56 opus_int i, k, lshift;
michael@0 57 const opus_int16 *r_ptr, *lag_ptr;
michael@0 58 opus_int16 *b_Q14_ptr;
michael@0 59
michael@0 60 opus_int32 regu;
michael@0 61 opus_int32 *WLTP_ptr;
michael@0 62 opus_int32 b_Q16[ LTP_ORDER ], delta_b_Q14[ LTP_ORDER ], d_Q14[ MAX_NB_SUBFR ], nrg[ MAX_NB_SUBFR ], g_Q26;
michael@0 63 opus_int32 w[ MAX_NB_SUBFR ], WLTP_max, max_abs_d_Q14, max_w_bits;
michael@0 64
michael@0 65 opus_int32 temp32, denom32;
michael@0 66 opus_int extra_shifts;
michael@0 67 opus_int rr_shifts, maxRshifts, maxRshifts_wxtra, LZs;
michael@0 68 opus_int32 LPC_res_nrg, LPC_LTP_res_nrg, div_Q16;
michael@0 69 opus_int32 Rr[ LTP_ORDER ], rr[ MAX_NB_SUBFR ];
michael@0 70 opus_int32 wd, m_Q12;
michael@0 71
michael@0 72 b_Q14_ptr = b_Q14;
michael@0 73 WLTP_ptr = WLTP;
michael@0 74 r_ptr = &r_lpc[ mem_offset ];
michael@0 75 for( k = 0; k < nb_subfr; k++ ) {
michael@0 76 lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 );
michael@0 77
michael@0 78 silk_sum_sqr_shift( &rr[ k ], &rr_shifts, r_ptr, subfr_length ); /* rr[ k ] in Q( -rr_shifts ) */
michael@0 79
michael@0 80 /* Assure headroom */
michael@0 81 LZs = silk_CLZ32( rr[k] );
michael@0 82 if( LZs < LTP_CORRS_HEAD_ROOM ) {
michael@0 83 rr[ k ] = silk_RSHIFT_ROUND( rr[ k ], LTP_CORRS_HEAD_ROOM - LZs );
michael@0 84 rr_shifts += ( LTP_CORRS_HEAD_ROOM - LZs );
michael@0 85 }
michael@0 86 corr_rshifts[ k ] = rr_shifts;
michael@0 87 silk_corrMatrix_FIX( lag_ptr, subfr_length, LTP_ORDER, LTP_CORRS_HEAD_ROOM, WLTP_ptr, &corr_rshifts[ k ] ); /* WLTP_fix_ptr in Q( -corr_rshifts[ k ] ) */
michael@0 88
michael@0 89 /* The correlation vector always has lower max abs value than rr and/or RR so head room is assured */
michael@0 90 silk_corrVector_FIX( lag_ptr, r_ptr, subfr_length, LTP_ORDER, Rr, corr_rshifts[ k ] ); /* Rr_fix_ptr in Q( -corr_rshifts[ k ] ) */
michael@0 91 if( corr_rshifts[ k ] > rr_shifts ) {
michael@0 92 rr[ k ] = silk_RSHIFT( rr[ k ], corr_rshifts[ k ] - rr_shifts ); /* rr[ k ] in Q( -corr_rshifts[ k ] ) */
michael@0 93 }
michael@0 94 silk_assert( rr[ k ] >= 0 );
michael@0 95
michael@0 96 regu = 1;
michael@0 97 regu = silk_SMLAWB( regu, rr[ k ], SILK_FIX_CONST( LTP_DAMPING/3, 16 ) );
michael@0 98 regu = silk_SMLAWB( regu, matrix_ptr( WLTP_ptr, 0, 0, LTP_ORDER ), SILK_FIX_CONST( LTP_DAMPING/3, 16 ) );
michael@0 99 regu = silk_SMLAWB( regu, matrix_ptr( WLTP_ptr, LTP_ORDER-1, LTP_ORDER-1, LTP_ORDER ), SILK_FIX_CONST( LTP_DAMPING/3, 16 ) );
michael@0 100 silk_regularize_correlations_FIX( WLTP_ptr, &rr[k], regu, LTP_ORDER );
michael@0 101
michael@0 102 silk_solve_LDL_FIX( WLTP_ptr, LTP_ORDER, Rr, b_Q16 ); /* WLTP_fix_ptr and Rr_fix_ptr both in Q(-corr_rshifts[k]) */
michael@0 103
michael@0 104 /* Limit and store in Q14 */
michael@0 105 silk_fit_LTP( b_Q16, b_Q14_ptr );
michael@0 106
michael@0 107 /* Calculate residual energy */
michael@0 108 nrg[ k ] = silk_residual_energy16_covar_FIX( b_Q14_ptr, WLTP_ptr, Rr, rr[ k ], LTP_ORDER, 14 ); /* nrg_fix in Q( -corr_rshifts[ k ] ) */
michael@0 109
michael@0 110 /* temp = Wght[ k ] / ( nrg[ k ] * Wght[ k ] + 0.01f * subfr_length ); */
michael@0 111 extra_shifts = silk_min_int( corr_rshifts[ k ], LTP_CORRS_HEAD_ROOM );
michael@0 112 denom32 = silk_LSHIFT_SAT32( silk_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 + extra_shifts ) + /* Q( -corr_rshifts[ k ] + extra_shifts ) */
michael@0 113 silk_RSHIFT( silk_SMULWB( (opus_int32)subfr_length, 655 ), corr_rshifts[ k ] - extra_shifts ); /* Q( -corr_rshifts[ k ] + extra_shifts ) */
michael@0 114 denom32 = silk_max( denom32, 1 );
michael@0 115 silk_assert( ((opus_int64)Wght_Q15[ k ] << 16 ) < silk_int32_MAX ); /* Wght always < 0.5 in Q0 */
michael@0 116 temp32 = silk_DIV32( silk_LSHIFT( (opus_int32)Wght_Q15[ k ], 16 ), denom32 ); /* Q( 15 + 16 + corr_rshifts[k] - extra_shifts ) */
michael@0 117 temp32 = silk_RSHIFT( temp32, 31 + corr_rshifts[ k ] - extra_shifts - 26 ); /* Q26 */
michael@0 118
michael@0 119 /* Limit temp such that the below scaling never wraps around */
michael@0 120 WLTP_max = 0;
michael@0 121 for( i = 0; i < LTP_ORDER * LTP_ORDER; i++ ) {
michael@0 122 WLTP_max = silk_max( WLTP_ptr[ i ], WLTP_max );
michael@0 123 }
michael@0 124 lshift = silk_CLZ32( WLTP_max ) - 1 - 3; /* keep 3 bits free for vq_nearest_neighbor_fix */
michael@0 125 silk_assert( 26 - 18 + lshift >= 0 );
michael@0 126 if( 26 - 18 + lshift < 31 ) {
michael@0 127 temp32 = silk_min_32( temp32, silk_LSHIFT( (opus_int32)1, 26 - 18 + lshift ) );
michael@0 128 }
michael@0 129
michael@0 130 silk_scale_vector32_Q26_lshift_18( WLTP_ptr, temp32, LTP_ORDER * LTP_ORDER ); /* WLTP_ptr in Q( 18 - corr_rshifts[ k ] ) */
michael@0 131
michael@0 132 w[ k ] = matrix_ptr( WLTP_ptr, LTP_ORDER/2, LTP_ORDER/2, LTP_ORDER ); /* w in Q( 18 - corr_rshifts[ k ] ) */
michael@0 133 silk_assert( w[k] >= 0 );
michael@0 134
michael@0 135 r_ptr += subfr_length;
michael@0 136 b_Q14_ptr += LTP_ORDER;
michael@0 137 WLTP_ptr += LTP_ORDER * LTP_ORDER;
michael@0 138 }
michael@0 139
michael@0 140 maxRshifts = 0;
michael@0 141 for( k = 0; k < nb_subfr; k++ ) {
michael@0 142 maxRshifts = silk_max_int( corr_rshifts[ k ], maxRshifts );
michael@0 143 }
michael@0 144
michael@0 145 /* Compute LTP coding gain */
michael@0 146 if( LTPredCodGain_Q7 != NULL ) {
michael@0 147 LPC_LTP_res_nrg = 0;
michael@0 148 LPC_res_nrg = 0;
michael@0 149 silk_assert( LTP_CORRS_HEAD_ROOM >= 2 ); /* Check that no overflow will happen when adding */
michael@0 150 for( k = 0; k < nb_subfr; k++ ) {
michael@0 151 LPC_res_nrg = silk_ADD32( LPC_res_nrg, silk_RSHIFT( silk_ADD32( silk_SMULWB( rr[ k ], Wght_Q15[ k ] ), 1 ), 1 + ( maxRshifts - corr_rshifts[ k ] ) ) ); /* Q( -maxRshifts ) */
michael@0 152 LPC_LTP_res_nrg = silk_ADD32( LPC_LTP_res_nrg, silk_RSHIFT( silk_ADD32( silk_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 ), 1 + ( maxRshifts - corr_rshifts[ k ] ) ) ); /* Q( -maxRshifts ) */
michael@0 153 }
michael@0 154 LPC_LTP_res_nrg = silk_max( LPC_LTP_res_nrg, 1 ); /* avoid division by zero */
michael@0 155
michael@0 156 div_Q16 = silk_DIV32_varQ( LPC_res_nrg, LPC_LTP_res_nrg, 16 );
michael@0 157 *LTPredCodGain_Q7 = ( opus_int )silk_SMULBB( 3, silk_lin2log( div_Q16 ) - ( 16 << 7 ) );
michael@0 158
michael@0 159 silk_assert( *LTPredCodGain_Q7 == ( opus_int )silk_SAT16( silk_MUL( 3, silk_lin2log( div_Q16 ) - ( 16 << 7 ) ) ) );
michael@0 160 }
michael@0 161
michael@0 162 /* smoothing */
michael@0 163 /* d = sum( B, 1 ); */
michael@0 164 b_Q14_ptr = b_Q14;
michael@0 165 for( k = 0; k < nb_subfr; k++ ) {
michael@0 166 d_Q14[ k ] = 0;
michael@0 167 for( i = 0; i < LTP_ORDER; i++ ) {
michael@0 168 d_Q14[ k ] += b_Q14_ptr[ i ];
michael@0 169 }
michael@0 170 b_Q14_ptr += LTP_ORDER;
michael@0 171 }
michael@0 172
michael@0 173 /* m = ( w * d' ) / ( sum( w ) + 1e-3 ); */
michael@0 174
michael@0 175 /* Find maximum absolute value of d_Q14 and the bits used by w in Q0 */
michael@0 176 max_abs_d_Q14 = 0;
michael@0 177 max_w_bits = 0;
michael@0 178 for( k = 0; k < nb_subfr; k++ ) {
michael@0 179 max_abs_d_Q14 = silk_max_32( max_abs_d_Q14, silk_abs( d_Q14[ k ] ) );
michael@0 180 /* w[ k ] is in Q( 18 - corr_rshifts[ k ] ) */
michael@0 181 /* Find bits needed in Q( 18 - maxRshifts ) */
michael@0 182 max_w_bits = silk_max_32( max_w_bits, 32 - silk_CLZ32( w[ k ] ) + corr_rshifts[ k ] - maxRshifts );
michael@0 183 }
michael@0 184
michael@0 185 /* max_abs_d_Q14 = (5 << 15); worst case, i.e. LTP_ORDER * -silk_int16_MIN */
michael@0 186 silk_assert( max_abs_d_Q14 <= ( 5 << 15 ) );
michael@0 187
michael@0 188 /* How many bits is needed for w*d' in Q( 18 - maxRshifts ) in the worst case, of all d_Q14's being equal to max_abs_d_Q14 */
michael@0 189 extra_shifts = max_w_bits + 32 - silk_CLZ32( max_abs_d_Q14 ) - 14;
michael@0 190
michael@0 191 /* Subtract what we got available; bits in output var plus maxRshifts */
michael@0 192 extra_shifts -= ( 32 - 1 - 2 + maxRshifts ); /* Keep sign bit free as well as 2 bits for accumulation */
michael@0 193 extra_shifts = silk_max_int( extra_shifts, 0 );
michael@0 194
michael@0 195 maxRshifts_wxtra = maxRshifts + extra_shifts;
michael@0 196
michael@0 197 temp32 = silk_RSHIFT( 262, maxRshifts + extra_shifts ) + 1; /* 1e-3f in Q( 18 - (maxRshifts + extra_shifts) ) */
michael@0 198 wd = 0;
michael@0 199 for( k = 0; k < nb_subfr; k++ ) {
michael@0 200 /* w has at least 2 bits of headroom so no overflow should happen */
michael@0 201 temp32 = silk_ADD32( temp32, silk_RSHIFT( w[ k ], maxRshifts_wxtra - corr_rshifts[ k ] ) ); /* Q( 18 - maxRshifts_wxtra ) */
michael@0 202 wd = silk_ADD32( wd, silk_LSHIFT( silk_SMULWW( silk_RSHIFT( w[ k ], maxRshifts_wxtra - corr_rshifts[ k ] ), d_Q14[ k ] ), 2 ) ); /* Q( 18 - maxRshifts_wxtra ) */
michael@0 203 }
michael@0 204 m_Q12 = silk_DIV32_varQ( wd, temp32, 12 );
michael@0 205
michael@0 206 b_Q14_ptr = b_Q14;
michael@0 207 for( k = 0; k < nb_subfr; k++ ) {
michael@0 208 /* w_fix[ k ] from Q( 18 - corr_rshifts[ k ] ) to Q( 16 ) */
michael@0 209 if( 2 - corr_rshifts[k] > 0 ) {
michael@0 210 temp32 = silk_RSHIFT( w[ k ], 2 - corr_rshifts[ k ] );
michael@0 211 } else {
michael@0 212 temp32 = silk_LSHIFT_SAT32( w[ k ], corr_rshifts[ k ] - 2 );
michael@0 213 }
michael@0 214
michael@0 215 g_Q26 = silk_MUL(
michael@0 216 silk_DIV32(
michael@0 217 SILK_FIX_CONST( LTP_SMOOTHING, 26 ),
michael@0 218 silk_RSHIFT( SILK_FIX_CONST( LTP_SMOOTHING, 26 ), 10 ) + temp32 ), /* Q10 */
michael@0 219 silk_LSHIFT_SAT32( silk_SUB_SAT32( (opus_int32)m_Q12, silk_RSHIFT( d_Q14[ k ], 2 ) ), 4 ) ); /* Q16 */
michael@0 220
michael@0 221 temp32 = 0;
michael@0 222 for( i = 0; i < LTP_ORDER; i++ ) {
michael@0 223 delta_b_Q14[ i ] = silk_max_16( b_Q14_ptr[ i ], 1638 ); /* 1638_Q14 = 0.1_Q0 */
michael@0 224 temp32 += delta_b_Q14[ i ]; /* Q14 */
michael@0 225 }
michael@0 226 temp32 = silk_DIV32( g_Q26, temp32 ); /* Q14 -> Q12 */
michael@0 227 for( i = 0; i < LTP_ORDER; i++ ) {
michael@0 228 b_Q14_ptr[ i ] = silk_LIMIT_32( (opus_int32)b_Q14_ptr[ i ] + silk_SMULWB( silk_LSHIFT_SAT32( temp32, 4 ), delta_b_Q14[ i ] ), -16000, 28000 );
michael@0 229 }
michael@0 230 b_Q14_ptr += LTP_ORDER;
michael@0 231 }
michael@0 232 }
michael@0 233
michael@0 234 void silk_fit_LTP(
michael@0 235 opus_int32 LTP_coefs_Q16[ LTP_ORDER ],
michael@0 236 opus_int16 LTP_coefs_Q14[ LTP_ORDER ]
michael@0 237 )
michael@0 238 {
michael@0 239 opus_int i;
michael@0 240
michael@0 241 for( i = 0; i < LTP_ORDER; i++ ) {
michael@0 242 LTP_coefs_Q14[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( LTP_coefs_Q16[ i ], 2 ) );
michael@0 243 }
michael@0 244 }

mercurial