media/libopus/silk/LPC_inv_pred_gain.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /***********************************************************************
michael@0 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
michael@0 3 Redistribution and use in source and binary forms, with or without
michael@0 4 modification, are permitted provided that the following conditions
michael@0 5 are met:
michael@0 6 - Redistributions of source code must retain the above copyright notice,
michael@0 7 this list of conditions and the following disclaimer.
michael@0 8 - Redistributions in binary form must reproduce the above copyright
michael@0 9 notice, this list of conditions and the following disclaimer in the
michael@0 10 documentation and/or other materials provided with the distribution.
michael@0 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
michael@0 12 names of specific contributors, may be used to endorse or promote
michael@0 13 products derived from this software without specific prior written
michael@0 14 permission.
michael@0 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
michael@0 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
michael@0 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
michael@0 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
michael@0 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
michael@0 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
michael@0 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
michael@0 25 POSSIBILITY OF SUCH DAMAGE.
michael@0 26 ***********************************************************************/
michael@0 27
michael@0 28 #ifdef HAVE_CONFIG_H
michael@0 29 #include "config.h"
michael@0 30 #endif
michael@0 31
michael@0 32 #include "SigProc_FIX.h"
michael@0 33
michael@0 34 #define QA 24
michael@0 35 #define A_LIMIT SILK_FIX_CONST( 0.99975, QA )
michael@0 36
michael@0 37 #define MUL32_FRAC_Q(a32, b32, Q) ((opus_int32)(silk_RSHIFT_ROUND64(silk_SMULL(a32, b32), Q)))
michael@0 38
michael@0 39 /* Compute inverse of LPC prediction gain, and */
michael@0 40 /* test if LPC coefficients are stable (all poles within unit circle) */
michael@0 41 static opus_int32 LPC_inverse_pred_gain_QA( /* O Returns inverse prediction gain in energy domain, Q30 */
michael@0 42 opus_int32 A_QA[ 2 ][ SILK_MAX_ORDER_LPC ], /* I Prediction coefficients */
michael@0 43 const opus_int order /* I Prediction order */
michael@0 44 )
michael@0 45 {
michael@0 46 opus_int k, n, mult2Q;
michael@0 47 opus_int32 invGain_Q30, rc_Q31, rc_mult1_Q30, rc_mult2, tmp_QA;
michael@0 48 opus_int32 *Aold_QA, *Anew_QA;
michael@0 49
michael@0 50 Anew_QA = A_QA[ order & 1 ];
michael@0 51
michael@0 52 invGain_Q30 = (opus_int32)1 << 30;
michael@0 53 for( k = order - 1; k > 0; k-- ) {
michael@0 54 /* Check for stability */
michael@0 55 if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) {
michael@0 56 return 0;
michael@0 57 }
michael@0 58
michael@0 59 /* Set RC equal to negated AR coef */
michael@0 60 rc_Q31 = -silk_LSHIFT( Anew_QA[ k ], 31 - QA );
michael@0 61
michael@0 62 /* rc_mult1_Q30 range: [ 1 : 2^30 ] */
michael@0 63 rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
michael@0 64 silk_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */
michael@0 65 silk_assert( rc_mult1_Q30 <= ( 1 << 30 ) );
michael@0 66
michael@0 67 /* rc_mult2 range: [ 2^30 : silk_int32_MAX ] */
michael@0 68 mult2Q = 32 - silk_CLZ32( silk_abs( rc_mult1_Q30 ) );
michael@0 69 rc_mult2 = silk_INVERSE32_varQ( rc_mult1_Q30, mult2Q + 30 );
michael@0 70
michael@0 71 /* Update inverse gain */
michael@0 72 /* invGain_Q30 range: [ 0 : 2^30 ] */
michael@0 73 invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 );
michael@0 74 silk_assert( invGain_Q30 >= 0 );
michael@0 75 silk_assert( invGain_Q30 <= ( 1 << 30 ) );
michael@0 76
michael@0 77 /* Swap pointers */
michael@0 78 Aold_QA = Anew_QA;
michael@0 79 Anew_QA = A_QA[ k & 1 ];
michael@0 80
michael@0 81 /* Update AR coefficient */
michael@0 82 for( n = 0; n < k; n++ ) {
michael@0 83 tmp_QA = Aold_QA[ n ] - MUL32_FRAC_Q( Aold_QA[ k - n - 1 ], rc_Q31, 31 );
michael@0 84 Anew_QA[ n ] = MUL32_FRAC_Q( tmp_QA, rc_mult2 , mult2Q );
michael@0 85 }
michael@0 86 }
michael@0 87
michael@0 88 /* Check for stability */
michael@0 89 if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) {
michael@0 90 return 0;
michael@0 91 }
michael@0 92
michael@0 93 /* Set RC equal to negated AR coef */
michael@0 94 rc_Q31 = -silk_LSHIFT( Anew_QA[ 0 ], 31 - QA );
michael@0 95
michael@0 96 /* Range: [ 1 : 2^30 ] */
michael@0 97 rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
michael@0 98
michael@0 99 /* Update inverse gain */
michael@0 100 /* Range: [ 0 : 2^30 ] */
michael@0 101 invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 );
michael@0 102 silk_assert( invGain_Q30 >= 0 );
michael@0 103 silk_assert( invGain_Q30 <= 1<<30 );
michael@0 104
michael@0 105 return invGain_Q30;
michael@0 106 }
michael@0 107
michael@0 108 /* For input in Q12 domain */
michael@0 109 opus_int32 silk_LPC_inverse_pred_gain( /* O Returns inverse prediction gain in energy domain, Q30 */
michael@0 110 const opus_int16 *A_Q12, /* I Prediction coefficients, Q12 [order] */
michael@0 111 const opus_int order /* I Prediction order */
michael@0 112 )
michael@0 113 {
michael@0 114 opus_int k;
michael@0 115 opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ];
michael@0 116 opus_int32 *Anew_QA;
michael@0 117 opus_int32 DC_resp = 0;
michael@0 118
michael@0 119 Anew_QA = Atmp_QA[ order & 1 ];
michael@0 120
michael@0 121 /* Increase Q domain of the AR coefficients */
michael@0 122 for( k = 0; k < order; k++ ) {
michael@0 123 DC_resp += (opus_int32)A_Q12[ k ];
michael@0 124 Anew_QA[ k ] = silk_LSHIFT32( (opus_int32)A_Q12[ k ], QA - 12 );
michael@0 125 }
michael@0 126 /* If the DC is unstable, we don't even need to do the full calculations */
michael@0 127 if( DC_resp >= 4096 ) {
michael@0 128 return 0;
michael@0 129 }
michael@0 130 return LPC_inverse_pred_gain_QA( Atmp_QA, order );
michael@0 131 }
michael@0 132
michael@0 133 #ifdef FIXED_POINT
michael@0 134
michael@0 135 /* For input in Q24 domain */
michael@0 136 opus_int32 silk_LPC_inverse_pred_gain_Q24( /* O Returns inverse prediction gain in energy domain, Q30 */
michael@0 137 const opus_int32 *A_Q24, /* I Prediction coefficients [order] */
michael@0 138 const opus_int order /* I Prediction order */
michael@0 139 )
michael@0 140 {
michael@0 141 opus_int k;
michael@0 142 opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ];
michael@0 143 opus_int32 *Anew_QA;
michael@0 144
michael@0 145 Anew_QA = Atmp_QA[ order & 1 ];
michael@0 146
michael@0 147 /* Increase Q domain of the AR coefficients */
michael@0 148 for( k = 0; k < order; k++ ) {
michael@0 149 Anew_QA[ k ] = silk_RSHIFT32( A_Q24[ k ], 24 - QA );
michael@0 150 }
michael@0 151
michael@0 152 return LPC_inverse_pred_gain_QA( Atmp_QA, order );
michael@0 153 }
michael@0 154 #endif

mercurial