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