1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/silk/fixed/find_LPC_FIX.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/*********************************************************************** 1.5 +Copyright (c) 2006-2011, Skype Limited. All rights reserved. 1.6 +Redistribution and use in source and binary forms, with or without 1.7 +modification, are permitted provided that the following conditions 1.8 +are met: 1.9 +- Redistributions of source code must retain the above copyright notice, 1.10 +this list of conditions and the following disclaimer. 1.11 +- Redistributions in binary form must reproduce the above copyright 1.12 +notice, this list of conditions and the following disclaimer in the 1.13 +documentation and/or other materials provided with the distribution. 1.14 +- Neither the name of Internet Society, IETF or IETF Trust, nor the 1.15 +names of specific contributors, may be used to endorse or promote 1.16 +products derived from this software without specific prior written 1.17 +permission. 1.18 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.19 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.20 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.21 +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 1.22 +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.23 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.24 +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.25 +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.26 +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.27 +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.28 +POSSIBILITY OF SUCH DAMAGE. 1.29 +***********************************************************************/ 1.30 + 1.31 +#ifdef HAVE_CONFIG_H 1.32 +#include "config.h" 1.33 +#endif 1.34 + 1.35 +#include "main_FIX.h" 1.36 +#include "stack_alloc.h" 1.37 +#include "tuning_parameters.h" 1.38 + 1.39 +/* Finds LPC vector from correlations, and converts to NLSF */ 1.40 +void silk_find_LPC_FIX( 1.41 + silk_encoder_state *psEncC, /* I/O Encoder state */ 1.42 + opus_int16 NLSF_Q15[], /* O NLSFs */ 1.43 + const opus_int16 x[], /* I Input signal */ 1.44 + const opus_int32 minInvGain_Q30 /* I Inverse of max prediction gain */ 1.45 +) 1.46 +{ 1.47 + opus_int k, subfr_length; 1.48 + opus_int32 a_Q16[ MAX_LPC_ORDER ]; 1.49 + opus_int isInterpLower, shift; 1.50 + opus_int32 res_nrg0, res_nrg1; 1.51 + opus_int rshift0, rshift1; 1.52 + 1.53 + /* Used only for LSF interpolation */ 1.54 + opus_int32 a_tmp_Q16[ MAX_LPC_ORDER ], res_nrg_interp, res_nrg, res_tmp_nrg; 1.55 + opus_int res_nrg_interp_Q, res_nrg_Q, res_tmp_nrg_Q; 1.56 + opus_int16 a_tmp_Q12[ MAX_LPC_ORDER ]; 1.57 + opus_int16 NLSF0_Q15[ MAX_LPC_ORDER ]; 1.58 + SAVE_STACK; 1.59 + 1.60 + subfr_length = psEncC->subfr_length + psEncC->predictLPCOrder; 1.61 + 1.62 + /* Default: no interpolation */ 1.63 + psEncC->indices.NLSFInterpCoef_Q2 = 4; 1.64 + 1.65 + /* Burg AR analysis for the full frame */ 1.66 + silk_burg_modified( &res_nrg, &res_nrg_Q, a_Q16, x, minInvGain_Q30, subfr_length, psEncC->nb_subfr, psEncC->predictLPCOrder, psEncC->arch ); 1.67 + 1.68 + if( psEncC->useInterpolatedNLSFs && !psEncC->first_frame_after_reset && psEncC->nb_subfr == MAX_NB_SUBFR ) { 1.69 + VARDECL( opus_int16, LPC_res ); 1.70 + 1.71 + /* Optimal solution for last 10 ms */ 1.72 + silk_burg_modified( &res_tmp_nrg, &res_tmp_nrg_Q, a_tmp_Q16, x + 2 * subfr_length, minInvGain_Q30, subfr_length, 2, psEncC->predictLPCOrder, psEncC->arch ); 1.73 + 1.74 + /* subtract residual energy here, as that's easier than adding it to the */ 1.75 + /* residual energy of the first 10 ms in each iteration of the search below */ 1.76 + shift = res_tmp_nrg_Q - res_nrg_Q; 1.77 + if( shift >= 0 ) { 1.78 + if( shift < 32 ) { 1.79 + res_nrg = res_nrg - silk_RSHIFT( res_tmp_nrg, shift ); 1.80 + } 1.81 + } else { 1.82 + silk_assert( shift > -32 ); 1.83 + res_nrg = silk_RSHIFT( res_nrg, -shift ) - res_tmp_nrg; 1.84 + res_nrg_Q = res_tmp_nrg_Q; 1.85 + } 1.86 + 1.87 + /* Convert to NLSFs */ 1.88 + silk_A2NLSF( NLSF_Q15, a_tmp_Q16, psEncC->predictLPCOrder ); 1.89 + 1.90 + ALLOC( LPC_res, 2 * subfr_length, opus_int16 ); 1.91 + 1.92 + /* Search over interpolation indices to find the one with lowest residual energy */ 1.93 + for( k = 3; k >= 0; k-- ) { 1.94 + /* Interpolate NLSFs for first half */ 1.95 + silk_interpolate( NLSF0_Q15, psEncC->prev_NLSFq_Q15, NLSF_Q15, k, psEncC->predictLPCOrder ); 1.96 + 1.97 + /* Convert to LPC for residual energy evaluation */ 1.98 + silk_NLSF2A( a_tmp_Q12, NLSF0_Q15, psEncC->predictLPCOrder ); 1.99 + 1.100 + /* Calculate residual energy with NLSF interpolation */ 1.101 + silk_LPC_analysis_filter( LPC_res, x, a_tmp_Q12, 2 * subfr_length, psEncC->predictLPCOrder ); 1.102 + 1.103 + silk_sum_sqr_shift( &res_nrg0, &rshift0, LPC_res + psEncC->predictLPCOrder, subfr_length - psEncC->predictLPCOrder ); 1.104 + silk_sum_sqr_shift( &res_nrg1, &rshift1, LPC_res + psEncC->predictLPCOrder + subfr_length, subfr_length - psEncC->predictLPCOrder ); 1.105 + 1.106 + /* Add subframe energies from first half frame */ 1.107 + shift = rshift0 - rshift1; 1.108 + if( shift >= 0 ) { 1.109 + res_nrg1 = silk_RSHIFT( res_nrg1, shift ); 1.110 + res_nrg_interp_Q = -rshift0; 1.111 + } else { 1.112 + res_nrg0 = silk_RSHIFT( res_nrg0, -shift ); 1.113 + res_nrg_interp_Q = -rshift1; 1.114 + } 1.115 + res_nrg_interp = silk_ADD32( res_nrg0, res_nrg1 ); 1.116 + 1.117 + /* Compare with first half energy without NLSF interpolation, or best interpolated value so far */ 1.118 + shift = res_nrg_interp_Q - res_nrg_Q; 1.119 + if( shift >= 0 ) { 1.120 + if( silk_RSHIFT( res_nrg_interp, shift ) < res_nrg ) { 1.121 + isInterpLower = silk_TRUE; 1.122 + } else { 1.123 + isInterpLower = silk_FALSE; 1.124 + } 1.125 + } else { 1.126 + if( -shift < 32 ) { 1.127 + if( res_nrg_interp < silk_RSHIFT( res_nrg, -shift ) ) { 1.128 + isInterpLower = silk_TRUE; 1.129 + } else { 1.130 + isInterpLower = silk_FALSE; 1.131 + } 1.132 + } else { 1.133 + isInterpLower = silk_FALSE; 1.134 + } 1.135 + } 1.136 + 1.137 + /* Determine whether current interpolated NLSFs are best so far */ 1.138 + if( isInterpLower == silk_TRUE ) { 1.139 + /* Interpolation has lower residual energy */ 1.140 + res_nrg = res_nrg_interp; 1.141 + res_nrg_Q = res_nrg_interp_Q; 1.142 + psEncC->indices.NLSFInterpCoef_Q2 = (opus_int8)k; 1.143 + } 1.144 + } 1.145 + } 1.146 + 1.147 + if( psEncC->indices.NLSFInterpCoef_Q2 == 4 ) { 1.148 + /* NLSF interpolation is currently inactive, calculate NLSFs from full frame AR coefficients */ 1.149 + silk_A2NLSF( NLSF_Q15, a_Q16, psEncC->predictLPCOrder ); 1.150 + } 1.151 + 1.152 + silk_assert( psEncC->indices.NLSFInterpCoef_Q2 == 4 || ( psEncC->useInterpolatedNLSFs && !psEncC->first_frame_after_reset && psEncC->nb_subfr == MAX_NB_SUBFR ) ); 1.153 + RESTORE_STACK; 1.154 +}