media/libopus/silk/LPC_inv_pred_gain.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libopus/silk/LPC_inv_pred_gain.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,154 @@
     1.4 +/***********************************************************************
     1.5 +Copyright (c) 2006-2011, Skype Limited. All rights reserved.
     1.6 +Redistribution and use in source and binary forms, with or without
     1.7 +modification, are permitted provided that the following conditions
     1.8 +are met:
     1.9 +- Redistributions of source code must retain the above copyright notice,
    1.10 +this list of conditions and the following disclaimer.
    1.11 +- Redistributions in binary form must reproduce the above copyright
    1.12 +notice, this list of conditions and the following disclaimer in the
    1.13 +documentation and/or other materials provided with the distribution.
    1.14 +- Neither the name of Internet Society, IETF or IETF Trust, nor the
    1.15 +names of specific contributors, may be used to endorse or promote
    1.16 +products derived from this software without specific prior written
    1.17 +permission.
    1.18 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.19 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.20 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.21 +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.22 +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.23 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.24 +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.25 +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.26 +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.27 +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.28 +POSSIBILITY OF SUCH DAMAGE.
    1.29 +***********************************************************************/
    1.30 +
    1.31 +#ifdef HAVE_CONFIG_H
    1.32 +#include "config.h"
    1.33 +#endif
    1.34 +
    1.35 +#include "SigProc_FIX.h"
    1.36 +
    1.37 +#define QA                          24
    1.38 +#define A_LIMIT                     SILK_FIX_CONST( 0.99975, QA )
    1.39 +
    1.40 +#define MUL32_FRAC_Q(a32, b32, Q)   ((opus_int32)(silk_RSHIFT_ROUND64(silk_SMULL(a32, b32), Q)))
    1.41 +
    1.42 +/* Compute inverse of LPC prediction gain, and                          */
    1.43 +/* test if LPC coefficients are stable (all poles within unit circle)   */
    1.44 +static opus_int32 LPC_inverse_pred_gain_QA(                 /* O   Returns inverse prediction gain in energy domain, Q30    */
    1.45 +    opus_int32           A_QA[ 2 ][ SILK_MAX_ORDER_LPC ],   /* I   Prediction coefficients                                  */
    1.46 +    const opus_int       order                              /* I   Prediction order                                         */
    1.47 +)
    1.48 +{
    1.49 +    opus_int   k, n, mult2Q;
    1.50 +    opus_int32 invGain_Q30, rc_Q31, rc_mult1_Q30, rc_mult2, tmp_QA;
    1.51 +    opus_int32 *Aold_QA, *Anew_QA;
    1.52 +
    1.53 +    Anew_QA = A_QA[ order & 1 ];
    1.54 +
    1.55 +    invGain_Q30 = (opus_int32)1 << 30;
    1.56 +    for( k = order - 1; k > 0; k-- ) {
    1.57 +        /* Check for stability */
    1.58 +        if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) {
    1.59 +            return 0;
    1.60 +        }
    1.61 +
    1.62 +        /* Set RC equal to negated AR coef */
    1.63 +        rc_Q31 = -silk_LSHIFT( Anew_QA[ k ], 31 - QA );
    1.64 +
    1.65 +        /* rc_mult1_Q30 range: [ 1 : 2^30 ] */
    1.66 +        rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
    1.67 +        silk_assert( rc_mult1_Q30 > ( 1 << 15 ) );                   /* reduce A_LIMIT if fails */
    1.68 +        silk_assert( rc_mult1_Q30 <= ( 1 << 30 ) );
    1.69 +
    1.70 +        /* rc_mult2 range: [ 2^30 : silk_int32_MAX ] */
    1.71 +        mult2Q = 32 - silk_CLZ32( silk_abs( rc_mult1_Q30 ) );
    1.72 +        rc_mult2 = silk_INVERSE32_varQ( rc_mult1_Q30, mult2Q + 30 );
    1.73 +
    1.74 +        /* Update inverse gain */
    1.75 +        /* invGain_Q30 range: [ 0 : 2^30 ] */
    1.76 +        invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 );
    1.77 +        silk_assert( invGain_Q30 >= 0           );
    1.78 +        silk_assert( invGain_Q30 <= ( 1 << 30 ) );
    1.79 +
    1.80 +        /* Swap pointers */
    1.81 +        Aold_QA = Anew_QA;
    1.82 +        Anew_QA = A_QA[ k & 1 ];
    1.83 +
    1.84 +        /* Update AR coefficient */
    1.85 +        for( n = 0; n < k; n++ ) {
    1.86 +            tmp_QA = Aold_QA[ n ] - MUL32_FRAC_Q( Aold_QA[ k - n - 1 ], rc_Q31, 31 );
    1.87 +            Anew_QA[ n ] = MUL32_FRAC_Q( tmp_QA, rc_mult2 , mult2Q );
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    /* Check for stability */
    1.92 +    if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) {
    1.93 +        return 0;
    1.94 +    }
    1.95 +
    1.96 +    /* Set RC equal to negated AR coef */
    1.97 +    rc_Q31 = -silk_LSHIFT( Anew_QA[ 0 ], 31 - QA );
    1.98 +
    1.99 +    /* Range: [ 1 : 2^30 ] */
   1.100 +    rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
   1.101 +
   1.102 +    /* Update inverse gain */
   1.103 +    /* Range: [ 0 : 2^30 ] */
   1.104 +    invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 );
   1.105 +    silk_assert( invGain_Q30 >= 0     );
   1.106 +    silk_assert( invGain_Q30 <= 1<<30 );
   1.107 +
   1.108 +    return invGain_Q30;
   1.109 +}
   1.110 +
   1.111 +/* For input in Q12 domain */
   1.112 +opus_int32 silk_LPC_inverse_pred_gain(              /* O   Returns inverse prediction gain in energy domain, Q30        */
   1.113 +    const opus_int16            *A_Q12,             /* I   Prediction coefficients, Q12 [order]                         */
   1.114 +    const opus_int              order               /* I   Prediction order                                             */
   1.115 +)
   1.116 +{
   1.117 +    opus_int   k;
   1.118 +    opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ];
   1.119 +    opus_int32 *Anew_QA;
   1.120 +    opus_int32 DC_resp = 0;
   1.121 +
   1.122 +    Anew_QA = Atmp_QA[ order & 1 ];
   1.123 +
   1.124 +    /* Increase Q domain of the AR coefficients */
   1.125 +    for( k = 0; k < order; k++ ) {
   1.126 +        DC_resp += (opus_int32)A_Q12[ k ];
   1.127 +        Anew_QA[ k ] = silk_LSHIFT32( (opus_int32)A_Q12[ k ], QA - 12 );
   1.128 +    }
   1.129 +    /* If the DC is unstable, we don't even need to do the full calculations */
   1.130 +    if( DC_resp >= 4096 ) {
   1.131 +        return 0;
   1.132 +    }
   1.133 +    return LPC_inverse_pred_gain_QA( Atmp_QA, order );
   1.134 +}
   1.135 +
   1.136 +#ifdef FIXED_POINT
   1.137 +
   1.138 +/* For input in Q24 domain */
   1.139 +opus_int32 silk_LPC_inverse_pred_gain_Q24(          /* O    Returns inverse prediction gain in energy domain, Q30       */
   1.140 +    const opus_int32            *A_Q24,             /* I    Prediction coefficients [order]                             */
   1.141 +    const opus_int              order               /* I    Prediction order                                            */
   1.142 +)
   1.143 +{
   1.144 +    opus_int   k;
   1.145 +    opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ];
   1.146 +    opus_int32 *Anew_QA;
   1.147 +
   1.148 +    Anew_QA = Atmp_QA[ order & 1 ];
   1.149 +
   1.150 +    /* Increase Q domain of the AR coefficients */
   1.151 +    for( k = 0; k < order; k++ ) {
   1.152 +        Anew_QA[ k ] = silk_RSHIFT32( A_Q24[ k ], 24 - QA );
   1.153 +    }
   1.154 +
   1.155 +    return LPC_inverse_pred_gain_QA( Atmp_QA, order );
   1.156 +}
   1.157 +#endif

mercurial