annotate src/opus-1.3/silk/NLSF2A.c @ 69:7aeed7906520

Add Opus sources and macOS builds
author Chris Cannam
date Wed, 23 Jan 2019 13:48:08 +0000
parents
children
rev   line source
Chris@69 1 /***********************************************************************
Chris@69 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
Chris@69 3 Redistribution and use in source and binary forms, with or without
Chris@69 4 modification, are permitted provided that the following conditions
Chris@69 5 are met:
Chris@69 6 - Redistributions of source code must retain the above copyright notice,
Chris@69 7 this list of conditions and the following disclaimer.
Chris@69 8 - Redistributions in binary form must reproduce the above copyright
Chris@69 9 notice, this list of conditions and the following disclaimer in the
Chris@69 10 documentation and/or other materials provided with the distribution.
Chris@69 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
Chris@69 12 names of specific contributors, may be used to endorse or promote
Chris@69 13 products derived from this software without specific prior written
Chris@69 14 permission.
Chris@69 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@69 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@69 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Chris@69 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Chris@69 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Chris@69 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Chris@69 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Chris@69 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Chris@69 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Chris@69 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Chris@69 25 POSSIBILITY OF SUCH DAMAGE.
Chris@69 26 ***********************************************************************/
Chris@69 27
Chris@69 28 #ifdef HAVE_CONFIG_H
Chris@69 29 #include "config.h"
Chris@69 30 #endif
Chris@69 31
Chris@69 32 /* conversion between prediction filter coefficients and LSFs */
Chris@69 33 /* order should be even */
Chris@69 34 /* a piecewise linear approximation maps LSF <-> cos(LSF) */
Chris@69 35 /* therefore the result is not accurate LSFs, but the two */
Chris@69 36 /* functions are accurate inverses of each other */
Chris@69 37
Chris@69 38 #include "SigProc_FIX.h"
Chris@69 39 #include "tables.h"
Chris@69 40
Chris@69 41 #define QA 16
Chris@69 42
Chris@69 43 /* helper function for NLSF2A(..) */
Chris@69 44 static OPUS_INLINE void silk_NLSF2A_find_poly(
Chris@69 45 opus_int32 *out, /* O intermediate polynomial, QA [dd+1] */
Chris@69 46 const opus_int32 *cLSF, /* I vector of interleaved 2*cos(LSFs), QA [d] */
Chris@69 47 opus_int dd /* I polynomial order (= 1/2 * filter order) */
Chris@69 48 )
Chris@69 49 {
Chris@69 50 opus_int k, n;
Chris@69 51 opus_int32 ftmp;
Chris@69 52
Chris@69 53 out[0] = silk_LSHIFT( 1, QA );
Chris@69 54 out[1] = -cLSF[0];
Chris@69 55 for( k = 1; k < dd; k++ ) {
Chris@69 56 ftmp = cLSF[2*k]; /* QA*/
Chris@69 57 out[k+1] = silk_LSHIFT( out[k-1], 1 ) - (opus_int32)silk_RSHIFT_ROUND64( silk_SMULL( ftmp, out[k] ), QA );
Chris@69 58 for( n = k; n > 1; n-- ) {
Chris@69 59 out[n] += out[n-2] - (opus_int32)silk_RSHIFT_ROUND64( silk_SMULL( ftmp, out[n-1] ), QA );
Chris@69 60 }
Chris@69 61 out[1] -= ftmp;
Chris@69 62 }
Chris@69 63 }
Chris@69 64
Chris@69 65 /* compute whitening filter coefficients from normalized line spectral frequencies */
Chris@69 66 void silk_NLSF2A(
Chris@69 67 opus_int16 *a_Q12, /* O monic whitening filter coefficients in Q12, [ d ] */
Chris@69 68 const opus_int16 *NLSF, /* I normalized line spectral frequencies in Q15, [ d ] */
Chris@69 69 const opus_int d, /* I filter order (should be even) */
Chris@69 70 int arch /* I Run-time architecture */
Chris@69 71 )
Chris@69 72 {
Chris@69 73 /* This ordering was found to maximize quality. It improves numerical accuracy of
Chris@69 74 silk_NLSF2A_find_poly() compared to "standard" ordering. */
Chris@69 75 static const unsigned char ordering16[16] = {
Chris@69 76 0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1
Chris@69 77 };
Chris@69 78 static const unsigned char ordering10[10] = {
Chris@69 79 0, 9, 6, 3, 4, 5, 8, 1, 2, 7
Chris@69 80 };
Chris@69 81 const unsigned char *ordering;
Chris@69 82 opus_int k, i, dd;
Chris@69 83 opus_int32 cos_LSF_QA[ SILK_MAX_ORDER_LPC ];
Chris@69 84 opus_int32 P[ SILK_MAX_ORDER_LPC / 2 + 1 ], Q[ SILK_MAX_ORDER_LPC / 2 + 1 ];
Chris@69 85 opus_int32 Ptmp, Qtmp, f_int, f_frac, cos_val, delta;
Chris@69 86 opus_int32 a32_QA1[ SILK_MAX_ORDER_LPC ];
Chris@69 87
Chris@69 88 silk_assert( LSF_COS_TAB_SZ_FIX == 128 );
Chris@69 89 celt_assert( d==10 || d==16 );
Chris@69 90
Chris@69 91 /* convert LSFs to 2*cos(LSF), using piecewise linear curve from table */
Chris@69 92 ordering = d == 16 ? ordering16 : ordering10;
Chris@69 93 for( k = 0; k < d; k++ ) {
Chris@69 94 silk_assert( NLSF[k] >= 0 );
Chris@69 95
Chris@69 96 /* f_int on a scale 0-127 (rounded down) */
Chris@69 97 f_int = silk_RSHIFT( NLSF[k], 15 - 7 );
Chris@69 98
Chris@69 99 /* f_frac, range: 0..255 */
Chris@69 100 f_frac = NLSF[k] - silk_LSHIFT( f_int, 15 - 7 );
Chris@69 101
Chris@69 102 silk_assert(f_int >= 0);
Chris@69 103 silk_assert(f_int < LSF_COS_TAB_SZ_FIX );
Chris@69 104
Chris@69 105 /* Read start and end value from table */
Chris@69 106 cos_val = silk_LSFCosTab_FIX_Q12[ f_int ]; /* Q12 */
Chris@69 107 delta = silk_LSFCosTab_FIX_Q12[ f_int + 1 ] - cos_val; /* Q12, with a range of 0..200 */
Chris@69 108
Chris@69 109 /* Linear interpolation */
Chris@69 110 cos_LSF_QA[ordering[k]] = silk_RSHIFT_ROUND( silk_LSHIFT( cos_val, 8 ) + silk_MUL( delta, f_frac ), 20 - QA ); /* QA */
Chris@69 111 }
Chris@69 112
Chris@69 113 dd = silk_RSHIFT( d, 1 );
Chris@69 114
Chris@69 115 /* generate even and odd polynomials using convolution */
Chris@69 116 silk_NLSF2A_find_poly( P, &cos_LSF_QA[ 0 ], dd );
Chris@69 117 silk_NLSF2A_find_poly( Q, &cos_LSF_QA[ 1 ], dd );
Chris@69 118
Chris@69 119 /* convert even and odd polynomials to opus_int32 Q12 filter coefs */
Chris@69 120 for( k = 0; k < dd; k++ ) {
Chris@69 121 Ptmp = P[ k+1 ] + P[ k ];
Chris@69 122 Qtmp = Q[ k+1 ] - Q[ k ];
Chris@69 123
Chris@69 124 /* the Ptmp and Qtmp values at this stage need to fit in int32 */
Chris@69 125 a32_QA1[ k ] = -Qtmp - Ptmp; /* QA+1 */
Chris@69 126 a32_QA1[ d-k-1 ] = Qtmp - Ptmp; /* QA+1 */
Chris@69 127 }
Chris@69 128
Chris@69 129 /* Convert int32 coefficients to Q12 int16 coefs */
Chris@69 130 silk_LPC_fit( a_Q12, a32_QA1, 12, QA + 1, d );
Chris@69 131
Chris@69 132 for( i = 0; silk_LPC_inverse_pred_gain( a_Q12, d, arch ) == 0 && i < MAX_LPC_STABILIZE_ITERATIONS; i++ ) {
Chris@69 133 /* Prediction coefficients are (too close to) unstable; apply bandwidth expansion */
Chris@69 134 /* on the unscaled coefficients, convert to Q12 and measure again */
Chris@69 135 silk_bwexpander_32( a32_QA1, d, 65536 - silk_LSHIFT( 2, i ) );
Chris@69 136 for( k = 0; k < d; k++ ) {
Chris@69 137 a_Q12[ k ] = (opus_int16)silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ); /* QA+1 -> Q12 */
Chris@69 138 }
Chris@69 139 }
Chris@69 140 }
Chris@69 141