Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2006-2011, Skype Limited. All rights reserved. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: /* Conversion between prediction filter coefficients and NLSFs */ Chris@69: /* Requires the order to be an even number */ Chris@69: /* A piecewise linear approximation maps LSF <-> cos(LSF) */ Chris@69: /* Therefore the result is not accurate NLSFs, but the two */ Chris@69: /* functions are accurate inverses of each other */ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include "SigProc_FIX.h" Chris@69: #include "tables.h" Chris@69: Chris@69: /* Number of binary divisions, when not in low complexity mode */ Chris@69: #define BIN_DIV_STEPS_A2NLSF_FIX 3 /* must be no higher than 16 - log2( LSF_COS_TAB_SZ_FIX ) */ Chris@69: #define MAX_ITERATIONS_A2NLSF_FIX 16 Chris@69: Chris@69: /* Helper function for A2NLSF(..) */ Chris@69: /* Transforms polynomials from cos(n*f) to cos(f)^n */ Chris@69: static OPUS_INLINE void silk_A2NLSF_trans_poly( Chris@69: opus_int32 *p, /* I/O Polynomial */ Chris@69: const opus_int dd /* I Polynomial order (= filter order / 2 ) */ Chris@69: ) Chris@69: { Chris@69: opus_int k, n; Chris@69: Chris@69: for( k = 2; k <= dd; k++ ) { Chris@69: for( n = dd; n > k; n-- ) { Chris@69: p[ n - 2 ] -= p[ n ]; Chris@69: } Chris@69: p[ k - 2 ] -= silk_LSHIFT( p[ k ], 1 ); Chris@69: } Chris@69: } Chris@69: /* Helper function for A2NLSF(..) */ Chris@69: /* Polynomial evaluation */ Chris@69: static OPUS_INLINE opus_int32 silk_A2NLSF_eval_poly( /* return the polynomial evaluation, in Q16 */ Chris@69: opus_int32 *p, /* I Polynomial, Q16 */ Chris@69: const opus_int32 x, /* I Evaluation point, Q12 */ Chris@69: const opus_int dd /* I Order */ Chris@69: ) Chris@69: { Chris@69: opus_int n; Chris@69: opus_int32 x_Q16, y32; Chris@69: Chris@69: y32 = p[ dd ]; /* Q16 */ Chris@69: x_Q16 = silk_LSHIFT( x, 4 ); Chris@69: Chris@69: if ( opus_likely( 8 == dd ) ) Chris@69: { Chris@69: y32 = silk_SMLAWW( p[ 7 ], y32, x_Q16 ); Chris@69: y32 = silk_SMLAWW( p[ 6 ], y32, x_Q16 ); Chris@69: y32 = silk_SMLAWW( p[ 5 ], y32, x_Q16 ); Chris@69: y32 = silk_SMLAWW( p[ 4 ], y32, x_Q16 ); Chris@69: y32 = silk_SMLAWW( p[ 3 ], y32, x_Q16 ); Chris@69: y32 = silk_SMLAWW( p[ 2 ], y32, x_Q16 ); Chris@69: y32 = silk_SMLAWW( p[ 1 ], y32, x_Q16 ); Chris@69: y32 = silk_SMLAWW( p[ 0 ], y32, x_Q16 ); Chris@69: } Chris@69: else Chris@69: { Chris@69: for( n = dd - 1; n >= 0; n-- ) { Chris@69: y32 = silk_SMLAWW( p[ n ], y32, x_Q16 ); /* Q16 */ Chris@69: } Chris@69: } Chris@69: return y32; Chris@69: } Chris@69: Chris@69: static OPUS_INLINE void silk_A2NLSF_init( Chris@69: const opus_int32 *a_Q16, Chris@69: opus_int32 *P, Chris@69: opus_int32 *Q, Chris@69: const opus_int dd Chris@69: ) Chris@69: { Chris@69: opus_int k; Chris@69: Chris@69: /* Convert filter coefs to even and odd polynomials */ Chris@69: P[dd] = silk_LSHIFT( 1, 16 ); Chris@69: Q[dd] = silk_LSHIFT( 1, 16 ); Chris@69: for( k = 0; k < dd; k++ ) { Chris@69: P[ k ] = -a_Q16[ dd - k - 1 ] - a_Q16[ dd + k ]; /* Q16 */ Chris@69: Q[ k ] = -a_Q16[ dd - k - 1 ] + a_Q16[ dd + k ]; /* Q16 */ Chris@69: } Chris@69: Chris@69: /* Divide out zeros as we have that for even filter orders, */ Chris@69: /* z = 1 is always a root in Q, and */ Chris@69: /* z = -1 is always a root in P */ Chris@69: for( k = dd; k > 0; k-- ) { Chris@69: P[ k - 1 ] -= P[ k ]; Chris@69: Q[ k - 1 ] += Q[ k ]; Chris@69: } Chris@69: Chris@69: /* Transform polynomials from cos(n*f) to cos(f)^n */ Chris@69: silk_A2NLSF_trans_poly( P, dd ); Chris@69: silk_A2NLSF_trans_poly( Q, dd ); Chris@69: } Chris@69: Chris@69: /* Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients */ Chris@69: /* If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence. */ Chris@69: void silk_A2NLSF( Chris@69: opus_int16 *NLSF, /* O Normalized Line Spectral Frequencies in Q15 (0..2^15-1) [d] */ Chris@69: opus_int32 *a_Q16, /* I/O Monic whitening filter coefficients in Q16 [d] */ Chris@69: const opus_int d /* I Filter order (must be even) */ Chris@69: ) Chris@69: { Chris@69: opus_int i, k, m, dd, root_ix, ffrac; Chris@69: opus_int32 xlo, xhi, xmid; Chris@69: opus_int32 ylo, yhi, ymid, thr; Chris@69: opus_int32 nom, den; Chris@69: opus_int32 P[ SILK_MAX_ORDER_LPC / 2 + 1 ]; Chris@69: opus_int32 Q[ SILK_MAX_ORDER_LPC / 2 + 1 ]; Chris@69: opus_int32 *PQ[ 2 ]; Chris@69: opus_int32 *p; Chris@69: Chris@69: /* Store pointers to array */ Chris@69: PQ[ 0 ] = P; Chris@69: PQ[ 1 ] = Q; Chris@69: Chris@69: dd = silk_RSHIFT( d, 1 ); Chris@69: Chris@69: silk_A2NLSF_init( a_Q16, P, Q, dd ); Chris@69: Chris@69: /* Find roots, alternating between P and Q */ Chris@69: p = P; /* Pointer to polynomial */ Chris@69: Chris@69: xlo = silk_LSFCosTab_FIX_Q12[ 0 ]; /* Q12*/ Chris@69: ylo = silk_A2NLSF_eval_poly( p, xlo, dd ); Chris@69: Chris@69: if( ylo < 0 ) { Chris@69: /* Set the first NLSF to zero and move on to the next */ Chris@69: NLSF[ 0 ] = 0; Chris@69: p = Q; /* Pointer to polynomial */ Chris@69: ylo = silk_A2NLSF_eval_poly( p, xlo, dd ); Chris@69: root_ix = 1; /* Index of current root */ Chris@69: } else { Chris@69: root_ix = 0; /* Index of current root */ Chris@69: } Chris@69: k = 1; /* Loop counter */ Chris@69: i = 0; /* Counter for bandwidth expansions applied */ Chris@69: thr = 0; Chris@69: while( 1 ) { Chris@69: /* Evaluate polynomial */ Chris@69: xhi = silk_LSFCosTab_FIX_Q12[ k ]; /* Q12 */ Chris@69: yhi = silk_A2NLSF_eval_poly( p, xhi, dd ); Chris@69: Chris@69: /* Detect zero crossing */ Chris@69: if( ( ylo <= 0 && yhi >= thr ) || ( ylo >= 0 && yhi <= -thr ) ) { Chris@69: if( yhi == 0 ) { Chris@69: /* If the root lies exactly at the end of the current */ Chris@69: /* interval, look for the next root in the next interval */ Chris@69: thr = 1; Chris@69: } else { Chris@69: thr = 0; Chris@69: } Chris@69: /* Binary division */ Chris@69: ffrac = -256; Chris@69: for( m = 0; m < BIN_DIV_STEPS_A2NLSF_FIX; m++ ) { Chris@69: /* Evaluate polynomial */ Chris@69: xmid = silk_RSHIFT_ROUND( xlo + xhi, 1 ); Chris@69: ymid = silk_A2NLSF_eval_poly( p, xmid, dd ); Chris@69: Chris@69: /* Detect zero crossing */ Chris@69: if( ( ylo <= 0 && ymid >= 0 ) || ( ylo >= 0 && ymid <= 0 ) ) { Chris@69: /* Reduce frequency */ Chris@69: xhi = xmid; Chris@69: yhi = ymid; Chris@69: } else { Chris@69: /* Increase frequency */ Chris@69: xlo = xmid; Chris@69: ylo = ymid; Chris@69: ffrac = silk_ADD_RSHIFT( ffrac, 128, m ); Chris@69: } Chris@69: } Chris@69: Chris@69: /* Interpolate */ Chris@69: if( silk_abs( ylo ) < 65536 ) { Chris@69: /* Avoid dividing by zero */ Chris@69: den = ylo - yhi; Chris@69: nom = silk_LSHIFT( ylo, 8 - BIN_DIV_STEPS_A2NLSF_FIX ) + silk_RSHIFT( den, 1 ); Chris@69: if( den != 0 ) { Chris@69: ffrac += silk_DIV32( nom, den ); Chris@69: } Chris@69: } else { Chris@69: /* No risk of dividing by zero because abs(ylo - yhi) >= abs(ylo) >= 65536 */ Chris@69: ffrac += silk_DIV32( ylo, silk_RSHIFT( ylo - yhi, 8 - BIN_DIV_STEPS_A2NLSF_FIX ) ); Chris@69: } Chris@69: NLSF[ root_ix ] = (opus_int16)silk_min_32( silk_LSHIFT( (opus_int32)k, 8 ) + ffrac, silk_int16_MAX ); Chris@69: Chris@69: silk_assert( NLSF[ root_ix ] >= 0 ); Chris@69: Chris@69: root_ix++; /* Next root */ Chris@69: if( root_ix >= d ) { Chris@69: /* Found all roots */ Chris@69: break; Chris@69: } Chris@69: /* Alternate pointer to polynomial */ Chris@69: p = PQ[ root_ix & 1 ]; Chris@69: Chris@69: /* Evaluate polynomial */ Chris@69: xlo = silk_LSFCosTab_FIX_Q12[ k - 1 ]; /* Q12*/ Chris@69: ylo = silk_LSHIFT( 1 - ( root_ix & 2 ), 12 ); Chris@69: } else { Chris@69: /* Increment loop counter */ Chris@69: k++; Chris@69: xlo = xhi; Chris@69: ylo = yhi; Chris@69: thr = 0; Chris@69: Chris@69: if( k > LSF_COS_TAB_SZ_FIX ) { Chris@69: i++; Chris@69: if( i > MAX_ITERATIONS_A2NLSF_FIX ) { Chris@69: /* Set NLSFs to white spectrum and exit */ Chris@69: NLSF[ 0 ] = (opus_int16)silk_DIV32_16( 1 << 15, d + 1 ); Chris@69: for( k = 1; k < d; k++ ) { Chris@69: NLSF[ k ] = (opus_int16)silk_ADD16( NLSF[ k-1 ], NLSF[ 0 ] ); Chris@69: } Chris@69: return; Chris@69: } Chris@69: Chris@69: /* Error: Apply progressively more bandwidth expansion and run again */ Chris@69: silk_bwexpander_32( a_Q16, d, 65536 - silk_LSHIFT( 1, i ) ); Chris@69: Chris@69: silk_A2NLSF_init( a_Q16, P, Q, dd ); Chris@69: p = P; /* Pointer to polynomial */ Chris@69: xlo = silk_LSFCosTab_FIX_Q12[ 0 ]; /* Q12*/ Chris@69: ylo = silk_A2NLSF_eval_poly( p, xlo, dd ); Chris@69: if( ylo < 0 ) { Chris@69: /* Set the first NLSF to zero and move on to the next */ Chris@69: NLSF[ 0 ] = 0; Chris@69: p = Q; /* Pointer to polynomial */ Chris@69: ylo = silk_A2NLSF_eval_poly( p, xlo, dd ); Chris@69: root_ix = 1; /* Index of current root */ Chris@69: } else { Chris@69: root_ix = 0; /* Index of current root */ Chris@69: } Chris@69: k = 1; /* Reset loop counter */ Chris@69: } Chris@69: } Chris@69: } Chris@69: }