xref: /aosp_15_r20/external/libopus/silk/float/find_LPC_FLP.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1*a58d3d2aSXin Li /***********************************************************************
2*a58d3d2aSXin Li Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3*a58d3d2aSXin Li Redistribution and use in source and binary forms, with or without
4*a58d3d2aSXin Li modification, are permitted provided that the following conditions
5*a58d3d2aSXin Li are met:
6*a58d3d2aSXin Li - Redistributions of source code must retain the above copyright notice,
7*a58d3d2aSXin Li this list of conditions and the following disclaimer.
8*a58d3d2aSXin Li - Redistributions in binary form must reproduce the above copyright
9*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer in the
10*a58d3d2aSXin Li documentation and/or other materials provided with the distribution.
11*a58d3d2aSXin Li - Neither the name of Internet Society, IETF or IETF Trust, nor the
12*a58d3d2aSXin Li names of specific contributors, may be used to endorse or promote
13*a58d3d2aSXin Li products derived from this software without specific prior written
14*a58d3d2aSXin Li permission.
15*a58d3d2aSXin Li THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16*a58d3d2aSXin Li AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*a58d3d2aSXin Li IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*a58d3d2aSXin Li ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19*a58d3d2aSXin Li LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*a58d3d2aSXin Li CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*a58d3d2aSXin Li SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*a58d3d2aSXin Li INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*a58d3d2aSXin Li CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*a58d3d2aSXin Li ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*a58d3d2aSXin Li POSSIBILITY OF SUCH DAMAGE.
26*a58d3d2aSXin Li ***********************************************************************/
27*a58d3d2aSXin Li 
28*a58d3d2aSXin Li #ifdef HAVE_CONFIG_H
29*a58d3d2aSXin Li #include "config.h"
30*a58d3d2aSXin Li #endif
31*a58d3d2aSXin Li 
32*a58d3d2aSXin Li #include "define.h"
33*a58d3d2aSXin Li #include "main_FLP.h"
34*a58d3d2aSXin Li #include "tuning_parameters.h"
35*a58d3d2aSXin Li 
36*a58d3d2aSXin Li /* LPC analysis */
silk_find_LPC_FLP(silk_encoder_state * psEncC,opus_int16 NLSF_Q15[],const silk_float x[],const silk_float minInvGain,int arch)37*a58d3d2aSXin Li void silk_find_LPC_FLP(
38*a58d3d2aSXin Li     silk_encoder_state              *psEncC,                            /* I/O  Encoder state                               */
39*a58d3d2aSXin Li     opus_int16                      NLSF_Q15[],                         /* O    NLSFs                                       */
40*a58d3d2aSXin Li     const silk_float                x[],                                /* I    Input signal                                */
41*a58d3d2aSXin Li     const silk_float                minInvGain,                         /* I    Inverse of max prediction gain              */
42*a58d3d2aSXin Li     int                             arch
43*a58d3d2aSXin Li )
44*a58d3d2aSXin Li {
45*a58d3d2aSXin Li     opus_int    k, subfr_length;
46*a58d3d2aSXin Li     silk_float  a[ MAX_LPC_ORDER ];
47*a58d3d2aSXin Li 
48*a58d3d2aSXin Li     /* Used only for NLSF interpolation */
49*a58d3d2aSXin Li     silk_float  res_nrg, res_nrg_2nd, res_nrg_interp;
50*a58d3d2aSXin Li     opus_int16  NLSF0_Q15[ MAX_LPC_ORDER ];
51*a58d3d2aSXin Li     silk_float  a_tmp[ MAX_LPC_ORDER ];
52*a58d3d2aSXin Li     silk_float  LPC_res[ MAX_FRAME_LENGTH + MAX_NB_SUBFR * MAX_LPC_ORDER ];
53*a58d3d2aSXin Li 
54*a58d3d2aSXin Li     subfr_length = psEncC->subfr_length + psEncC->predictLPCOrder;
55*a58d3d2aSXin Li 
56*a58d3d2aSXin Li     /* Default: No interpolation */
57*a58d3d2aSXin Li     psEncC->indices.NLSFInterpCoef_Q2 = 4;
58*a58d3d2aSXin Li 
59*a58d3d2aSXin Li     /* Burg AR analysis for the full frame */
60*a58d3d2aSXin Li     res_nrg = silk_burg_modified_FLP( a, x, minInvGain, subfr_length, psEncC->nb_subfr, psEncC->predictLPCOrder, arch );
61*a58d3d2aSXin Li 
62*a58d3d2aSXin Li     if( psEncC->useInterpolatedNLSFs && !psEncC->first_frame_after_reset && psEncC->nb_subfr == MAX_NB_SUBFR ) {
63*a58d3d2aSXin Li         /* Optimal solution for last 10 ms; subtract residual energy here, as that's easier than        */
64*a58d3d2aSXin Li         /* adding it to the residual energy of the first 10 ms in each iteration of the search below    */
65*a58d3d2aSXin Li         res_nrg -= silk_burg_modified_FLP( a_tmp, x + ( MAX_NB_SUBFR / 2 ) * subfr_length, minInvGain, subfr_length, MAX_NB_SUBFR / 2, psEncC->predictLPCOrder, arch );
66*a58d3d2aSXin Li 
67*a58d3d2aSXin Li         /* Convert to NLSFs */
68*a58d3d2aSXin Li         silk_A2NLSF_FLP( NLSF_Q15, a_tmp, psEncC->predictLPCOrder );
69*a58d3d2aSXin Li 
70*a58d3d2aSXin Li         /* Search over interpolation indices to find the one with lowest residual energy */
71*a58d3d2aSXin Li         res_nrg_2nd = silk_float_MAX;
72*a58d3d2aSXin Li         for( k = 3; k >= 0; k-- ) {
73*a58d3d2aSXin Li             /* Interpolate NLSFs for first half */
74*a58d3d2aSXin Li             silk_interpolate( NLSF0_Q15, psEncC->prev_NLSFq_Q15, NLSF_Q15, k, psEncC->predictLPCOrder );
75*a58d3d2aSXin Li 
76*a58d3d2aSXin Li             /* Convert to LPC for residual energy evaluation */
77*a58d3d2aSXin Li             silk_NLSF2A_FLP( a_tmp, NLSF0_Q15, psEncC->predictLPCOrder, psEncC->arch );
78*a58d3d2aSXin Li 
79*a58d3d2aSXin Li             /* Calculate residual energy with LSF interpolation */
80*a58d3d2aSXin Li             silk_LPC_analysis_filter_FLP( LPC_res, a_tmp, x, 2 * subfr_length, psEncC->predictLPCOrder );
81*a58d3d2aSXin Li             res_nrg_interp = (silk_float)(
82*a58d3d2aSXin Li                 silk_energy_FLP( LPC_res + psEncC->predictLPCOrder,                subfr_length - psEncC->predictLPCOrder ) +
83*a58d3d2aSXin Li                 silk_energy_FLP( LPC_res + psEncC->predictLPCOrder + subfr_length, subfr_length - psEncC->predictLPCOrder ) );
84*a58d3d2aSXin Li 
85*a58d3d2aSXin Li             /* Determine whether current interpolated NLSFs are best so far */
86*a58d3d2aSXin Li             if( res_nrg_interp < res_nrg ) {
87*a58d3d2aSXin Li                 /* Interpolation has lower residual energy */
88*a58d3d2aSXin Li                 res_nrg = res_nrg_interp;
89*a58d3d2aSXin Li                 psEncC->indices.NLSFInterpCoef_Q2 = (opus_int8)k;
90*a58d3d2aSXin Li             } else if( res_nrg_interp > res_nrg_2nd ) {
91*a58d3d2aSXin Li                 /* No reason to continue iterating - residual energies will continue to climb */
92*a58d3d2aSXin Li                 break;
93*a58d3d2aSXin Li             }
94*a58d3d2aSXin Li             res_nrg_2nd = res_nrg_interp;
95*a58d3d2aSXin Li         }
96*a58d3d2aSXin Li     }
97*a58d3d2aSXin Li 
98*a58d3d2aSXin Li     if( psEncC->indices.NLSFInterpCoef_Q2 == 4 ) {
99*a58d3d2aSXin Li         /* NLSF interpolation is currently inactive, calculate NLSFs from full frame AR coefficients */
100*a58d3d2aSXin Li         silk_A2NLSF_FLP( NLSF_Q15, a, psEncC->predictLPCOrder );
101*a58d3d2aSXin Li     }
102*a58d3d2aSXin Li 
103*a58d3d2aSXin Li     celt_assert( psEncC->indices.NLSFInterpCoef_Q2 == 4 ||
104*a58d3d2aSXin Li         ( psEncC->useInterpolatedNLSFs && !psEncC->first_frame_after_reset && psEncC->nb_subfr == MAX_NB_SUBFR ) );
105*a58d3d2aSXin Li }
106