Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 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 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
| 32 | #include "main_FLP.h" |
| 33 | #include "tuning_parameters.h" |
| 34 | |
| 35 | void silk_find_LTP_FLP( |
| 36 | silk_float b[ MAX_NB_SUBFR * LTP_ORDER ], /* O LTP coefs */ |
| 37 | silk_float WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */ |
| 38 | silk_float *LTPredCodGain, /* O LTP coding gain */ |
| 39 | const silk_float r_lpc[], /* I LPC residual */ |
| 40 | const opus_int lag[ MAX_NB_SUBFR ], /* I LTP lags */ |
| 41 | const silk_float Wght[ MAX_NB_SUBFR ], /* I Weights */ |
| 42 | const opus_int subfr_length, /* I Subframe length */ |
| 43 | const opus_int nb_subfr, /* I number of subframes */ |
| 44 | const opus_int mem_offset /* I Number of samples in LTP memory */ |
| 45 | ) |
| 46 | { |
| 47 | opus_int i, k; |
| 48 | silk_float *b_ptr, temp, *WLTP_ptr; |
| 49 | silk_float LPC_res_nrg, LPC_LTP_res_nrg; |
| 50 | silk_float d[ MAX_NB_SUBFR ], m, g, delta_b[ LTP_ORDER ]; |
| 51 | silk_float w[ MAX_NB_SUBFR ], nrg[ MAX_NB_SUBFR ], regu; |
| 52 | silk_float Rr[ LTP_ORDER ], rr[ MAX_NB_SUBFR ]; |
| 53 | const silk_float *r_ptr, *lag_ptr; |
| 54 | |
| 55 | b_ptr = b; |
| 56 | WLTP_ptr = WLTP; |
| 57 | r_ptr = &r_lpc[ mem_offset ]; |
| 58 | for( k = 0; k < nb_subfr; k++ ) { |
| 59 | lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 ); |
| 60 | |
| 61 | silk_corrMatrix_FLP( lag_ptr, subfr_length, LTP_ORDER, WLTP_ptr ); |
| 62 | silk_corrVector_FLP( lag_ptr, r_ptr, subfr_length, LTP_ORDER, Rr ); |
| 63 | |
| 64 | rr[ k ] = ( silk_float )silk_energy_FLP( r_ptr, subfr_length ); |
| 65 | regu = 1.0f + rr[ k ] + |
| 66 | matrix_ptr( WLTP_ptr, 0, 0, LTP_ORDER ) + |
| 67 | matrix_ptr( WLTP_ptr, LTP_ORDER-1, LTP_ORDER-1, LTP_ORDER ); |
| 68 | regu *= LTP_DAMPING / 3; |
| 69 | silk_regularize_correlations_FLP( WLTP_ptr, &rr[ k ], regu, LTP_ORDER ); |
| 70 | silk_solve_LDL_FLP( WLTP_ptr, LTP_ORDER, Rr, b_ptr ); |
| 71 | |
| 72 | /* Calculate residual energy */ |
| 73 | nrg[ k ] = silk_residual_energy_covar_FLP( b_ptr, WLTP_ptr, Rr, rr[ k ], LTP_ORDER ); |
| 74 | |
| 75 | temp = Wght[ k ] / ( nrg[ k ] * Wght[ k ] + 0.01f * subfr_length ); |
| 76 | silk_scale_vector_FLP( WLTP_ptr, temp, LTP_ORDER * LTP_ORDER ); |
| 77 | w[ k ] = matrix_ptr( WLTP_ptr, LTP_ORDER / 2, LTP_ORDER / 2, LTP_ORDER ); |
| 78 | |
| 79 | r_ptr += subfr_length; |
| 80 | b_ptr += LTP_ORDER; |
| 81 | WLTP_ptr += LTP_ORDER * LTP_ORDER; |
| 82 | } |
| 83 | |
| 84 | /* Compute LTP coding gain */ |
| 85 | if( LTPredCodGain != NULL ) { |
| 86 | LPC_LTP_res_nrg = 1e-6f; |
| 87 | LPC_res_nrg = 0.0f; |
| 88 | for( k = 0; k < nb_subfr; k++ ) { |
| 89 | LPC_res_nrg += rr[ k ] * Wght[ k ]; |
| 90 | LPC_LTP_res_nrg += nrg[ k ] * Wght[ k ]; |
| 91 | } |
| 92 | |
| 93 | silk_assert( LPC_LTP_res_nrg > 0 ); |
| 94 | *LTPredCodGain = 3.0f * silk_log2( LPC_res_nrg / LPC_LTP_res_nrg ); |
| 95 | } |
| 96 | |
| 97 | /* Smoothing */ |
| 98 | /* d = sum( B, 1 ); */ |
| 99 | b_ptr = b; |
| 100 | for( k = 0; k < nb_subfr; k++ ) { |
| 101 | d[ k ] = 0; |
| 102 | for( i = 0; i < LTP_ORDER; i++ ) { |
| 103 | d[ k ] += b_ptr[ i ]; |
| 104 | } |
| 105 | b_ptr += LTP_ORDER; |
| 106 | } |
| 107 | /* m = ( w * d' ) / ( sum( w ) + 1e-3 ); */ |
| 108 | temp = 1e-3f; |
| 109 | for( k = 0; k < nb_subfr; k++ ) { |
| 110 | temp += w[ k ]; |
| 111 | } |
| 112 | m = 0; |
| 113 | for( k = 0; k < nb_subfr; k++ ) { |
| 114 | m += d[ k ] * w[ k ]; |
| 115 | } |
| 116 | m = m / temp; |
| 117 | |
| 118 | b_ptr = b; |
| 119 | for( k = 0; k < nb_subfr; k++ ) { |
| 120 | g = LTP_SMOOTHING / ( LTP_SMOOTHING + w[ k ] ) * ( m - d[ k ] ); |
| 121 | temp = 0; |
| 122 | for( i = 0; i < LTP_ORDER; i++ ) { |
| 123 | delta_b[ i ] = silk_max_float( b_ptr[ i ], 0.1f ); |
| 124 | temp += delta_b[ i ]; |
| 125 | } |
| 126 | temp = g / temp; |
| 127 | for( i = 0; i < LTP_ORDER; i++ ) { |
| 128 | b_ptr[ i ] = b_ptr[ i ] + delta_b[ i ] * temp; |
| 129 | } |
| 130 | b_ptr += LTP_ORDER; |
| 131 | } |
| 132 | } |