michael@0: /*********************************************************************** michael@0: Copyright (c) 2006-2011, Skype Limited. All rights reserved. michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: - Redistributions of source code must retain the above copyright notice, michael@0: this list of conditions and the following disclaimer. michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: - Neither the name of Internet Society, IETF or IETF Trust, nor the michael@0: names of specific contributors, may be used to endorse or promote michael@0: products derived from this software without specific prior written michael@0: permission. michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" michael@0: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE michael@0: POSSIBILITY OF SUCH DAMAGE. michael@0: ***********************************************************************/ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: /* conversion between prediction filter coefficients and LSFs */ michael@0: /* order should be even */ michael@0: /* a piecewise linear approximation maps LSF <-> cos(LSF) */ michael@0: /* therefore the result is not accurate LSFs, but the two */ michael@0: /* functions are accurate inverses of each other */ michael@0: michael@0: #include "SigProc_FIX.h" michael@0: #include "tables.h" michael@0: michael@0: #define QA 16 michael@0: michael@0: /* helper function for NLSF2A(..) */ michael@0: static OPUS_INLINE void silk_NLSF2A_find_poly( michael@0: opus_int32 *out, /* O intermediate polynomial, QA [dd+1] */ michael@0: const opus_int32 *cLSF, /* I vector of interleaved 2*cos(LSFs), QA [d] */ michael@0: opus_int dd /* I polynomial order (= 1/2 * filter order) */ michael@0: ) michael@0: { michael@0: opus_int k, n; michael@0: opus_int32 ftmp; michael@0: michael@0: out[0] = silk_LSHIFT( 1, QA ); michael@0: out[1] = -cLSF[0]; michael@0: for( k = 1; k < dd; k++ ) { michael@0: ftmp = cLSF[2*k]; /* QA*/ michael@0: out[k+1] = silk_LSHIFT( out[k-1], 1 ) - (opus_int32)silk_RSHIFT_ROUND64( silk_SMULL( ftmp, out[k] ), QA ); michael@0: for( n = k; n > 1; n-- ) { michael@0: out[n] += out[n-2] - (opus_int32)silk_RSHIFT_ROUND64( silk_SMULL( ftmp, out[n-1] ), QA ); michael@0: } michael@0: out[1] -= ftmp; michael@0: } michael@0: } michael@0: michael@0: /* compute whitening filter coefficients from normalized line spectral frequencies */ michael@0: void silk_NLSF2A( michael@0: opus_int16 *a_Q12, /* O monic whitening filter coefficients in Q12, [ d ] */ michael@0: const opus_int16 *NLSF, /* I normalized line spectral frequencies in Q15, [ d ] */ michael@0: const opus_int d /* I filter order (should be even) */ michael@0: ) michael@0: { michael@0: /* This ordering was found to maximize quality. It improves numerical accuracy of michael@0: silk_NLSF2A_find_poly() compared to "standard" ordering. */ michael@0: static const unsigned char ordering16[16] = { michael@0: 0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1 michael@0: }; michael@0: static const unsigned char ordering10[10] = { michael@0: 0, 9, 6, 3, 4, 5, 8, 1, 2, 7 michael@0: }; michael@0: const unsigned char *ordering; michael@0: opus_int k, i, dd; michael@0: opus_int32 cos_LSF_QA[ SILK_MAX_ORDER_LPC ]; michael@0: opus_int32 P[ SILK_MAX_ORDER_LPC / 2 + 1 ], Q[ SILK_MAX_ORDER_LPC / 2 + 1 ]; michael@0: opus_int32 Ptmp, Qtmp, f_int, f_frac, cos_val, delta; michael@0: opus_int32 a32_QA1[ SILK_MAX_ORDER_LPC ]; michael@0: opus_int32 maxabs, absval, idx=0, sc_Q16; michael@0: michael@0: silk_assert( LSF_COS_TAB_SZ_FIX == 128 ); michael@0: silk_assert( d==10||d==16 ); michael@0: michael@0: /* convert LSFs to 2*cos(LSF), using piecewise linear curve from table */ michael@0: ordering = d == 16 ? ordering16 : ordering10; michael@0: for( k = 0; k < d; k++ ) { michael@0: silk_assert(NLSF[k] >= 0 ); michael@0: michael@0: /* f_int on a scale 0-127 (rounded down) */ michael@0: f_int = silk_RSHIFT( NLSF[k], 15 - 7 ); michael@0: michael@0: /* f_frac, range: 0..255 */ michael@0: f_frac = NLSF[k] - silk_LSHIFT( f_int, 15 - 7 ); michael@0: michael@0: silk_assert(f_int >= 0); michael@0: silk_assert(f_int < LSF_COS_TAB_SZ_FIX ); michael@0: michael@0: /* Read start and end value from table */ michael@0: cos_val = silk_LSFCosTab_FIX_Q12[ f_int ]; /* Q12 */ michael@0: delta = silk_LSFCosTab_FIX_Q12[ f_int + 1 ] - cos_val; /* Q12, with a range of 0..200 */ michael@0: michael@0: /* Linear interpolation */ michael@0: cos_LSF_QA[ordering[k]] = silk_RSHIFT_ROUND( silk_LSHIFT( cos_val, 8 ) + silk_MUL( delta, f_frac ), 20 - QA ); /* QA */ michael@0: } michael@0: michael@0: dd = silk_RSHIFT( d, 1 ); michael@0: michael@0: /* generate even and odd polynomials using convolution */ michael@0: silk_NLSF2A_find_poly( P, &cos_LSF_QA[ 0 ], dd ); michael@0: silk_NLSF2A_find_poly( Q, &cos_LSF_QA[ 1 ], dd ); michael@0: michael@0: /* convert even and odd polynomials to opus_int32 Q12 filter coefs */ michael@0: for( k = 0; k < dd; k++ ) { michael@0: Ptmp = P[ k+1 ] + P[ k ]; michael@0: Qtmp = Q[ k+1 ] - Q[ k ]; michael@0: michael@0: /* the Ptmp and Qtmp values at this stage need to fit in int32 */ michael@0: a32_QA1[ k ] = -Qtmp - Ptmp; /* QA+1 */ michael@0: a32_QA1[ d-k-1 ] = Qtmp - Ptmp; /* QA+1 */ michael@0: } michael@0: michael@0: /* Limit the maximum absolute value of the prediction coefficients, so that they'll fit in int16 */ michael@0: for( i = 0; i < 10; i++ ) { michael@0: /* Find maximum absolute value and its index */ michael@0: maxabs = 0; michael@0: for( k = 0; k < d; k++ ) { michael@0: absval = silk_abs( a32_QA1[k] ); michael@0: if( absval > maxabs ) { michael@0: maxabs = absval; michael@0: idx = k; michael@0: } michael@0: } michael@0: maxabs = silk_RSHIFT_ROUND( maxabs, QA + 1 - 12 ); /* QA+1 -> Q12 */ michael@0: michael@0: if( maxabs > silk_int16_MAX ) { michael@0: /* Reduce magnitude of prediction coefficients */ michael@0: maxabs = silk_min( maxabs, 163838 ); /* ( silk_int32_MAX >> 14 ) + silk_int16_MAX = 163838 */ michael@0: sc_Q16 = SILK_FIX_CONST( 0.999, 16 ) - silk_DIV32( silk_LSHIFT( maxabs - silk_int16_MAX, 14 ), michael@0: silk_RSHIFT32( silk_MUL( maxabs, idx + 1), 2 ) ); michael@0: silk_bwexpander_32( a32_QA1, d, sc_Q16 ); michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if( i == 10 ) { michael@0: /* Reached the last iteration, clip the coefficients */ michael@0: for( k = 0; k < d; k++ ) { michael@0: a_Q12[ k ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ) ); /* QA+1 -> Q12 */ michael@0: a32_QA1[ k ] = silk_LSHIFT( (opus_int32)a_Q12[ k ], QA + 1 - 12 ); michael@0: } michael@0: } else { michael@0: for( k = 0; k < d; k++ ) { michael@0: a_Q12[ k ] = (opus_int16)silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ); /* QA+1 -> Q12 */ michael@0: } michael@0: } michael@0: michael@0: for( i = 0; i < MAX_LPC_STABILIZE_ITERATIONS; i++ ) { michael@0: if( silk_LPC_inverse_pred_gain( a_Q12, d ) < SILK_FIX_CONST( 1.0 / MAX_PREDICTION_POWER_GAIN, 30 ) ) { michael@0: /* Prediction coefficients are (too close to) unstable; apply bandwidth expansion */ michael@0: /* on the unscaled coefficients, convert to Q12 and measure again */ michael@0: silk_bwexpander_32( a32_QA1, d, 65536 - silk_LSHIFT( 2, i ) ); michael@0: for( k = 0; k < d; k++ ) { michael@0: a_Q12[ k ] = (opus_int16)silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ); /* QA+1 -> Q12 */ michael@0: } michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: