media/libopus/silk/float/burg_modified_FLP.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_FLP.h"
michael@0 33 #include "tuning_parameters.h"
michael@0 34 #include "define.h"
michael@0 35
michael@0 36 #define MAX_FRAME_SIZE 384 /* subfr_length * nb_subfr = ( 0.005 * 16000 + 16 ) * 4 = 384*/
michael@0 37
michael@0 38 /* Compute reflection coefficients from input signal */
michael@0 39 silk_float silk_burg_modified_FLP( /* O returns residual energy */
michael@0 40 silk_float A[], /* O prediction coefficients (length order) */
michael@0 41 const silk_float x[], /* I input signal, length: nb_subfr*(D+L_sub) */
michael@0 42 const silk_float minInvGain, /* I minimum inverse prediction gain */
michael@0 43 const opus_int subfr_length, /* I input signal subframe length (incl. D preceding samples) */
michael@0 44 const opus_int nb_subfr, /* I number of subframes stacked in x */
michael@0 45 const opus_int D /* I order */
michael@0 46 )
michael@0 47 {
michael@0 48 opus_int k, n, s, reached_max_gain;
michael@0 49 double C0, invGain, num, nrg_f, nrg_b, rc, Atmp, tmp1, tmp2;
michael@0 50 const silk_float *x_ptr;
michael@0 51 double C_first_row[ SILK_MAX_ORDER_LPC ], C_last_row[ SILK_MAX_ORDER_LPC ];
michael@0 52 double CAf[ SILK_MAX_ORDER_LPC + 1 ], CAb[ SILK_MAX_ORDER_LPC + 1 ];
michael@0 53 double Af[ SILK_MAX_ORDER_LPC ];
michael@0 54
michael@0 55 silk_assert( subfr_length * nb_subfr <= MAX_FRAME_SIZE );
michael@0 56
michael@0 57 /* Compute autocorrelations, added over subframes */
michael@0 58 C0 = silk_energy_FLP( x, nb_subfr * subfr_length );
michael@0 59 silk_memset( C_first_row, 0, SILK_MAX_ORDER_LPC * sizeof( double ) );
michael@0 60 for( s = 0; s < nb_subfr; s++ ) {
michael@0 61 x_ptr = x + s * subfr_length;
michael@0 62 for( n = 1; n < D + 1; n++ ) {
michael@0 63 C_first_row[ n - 1 ] += silk_inner_product_FLP( x_ptr, x_ptr + n, subfr_length - n );
michael@0 64 }
michael@0 65 }
michael@0 66 silk_memcpy( C_last_row, C_first_row, SILK_MAX_ORDER_LPC * sizeof( double ) );
michael@0 67
michael@0 68 /* Initialize */
michael@0 69 CAb[ 0 ] = CAf[ 0 ] = C0 + FIND_LPC_COND_FAC * C0 + 1e-9f;
michael@0 70 invGain = 1.0f;
michael@0 71 reached_max_gain = 0;
michael@0 72 for( n = 0; n < D; n++ ) {
michael@0 73 /* Update first row of correlation matrix (without first element) */
michael@0 74 /* Update last row of correlation matrix (without last element, stored in reversed order) */
michael@0 75 /* Update C * Af */
michael@0 76 /* Update C * flipud(Af) (stored in reversed order) */
michael@0 77 for( s = 0; s < nb_subfr; s++ ) {
michael@0 78 x_ptr = x + s * subfr_length;
michael@0 79 tmp1 = x_ptr[ n ];
michael@0 80 tmp2 = x_ptr[ subfr_length - n - 1 ];
michael@0 81 for( k = 0; k < n; k++ ) {
michael@0 82 C_first_row[ k ] -= x_ptr[ n ] * x_ptr[ n - k - 1 ];
michael@0 83 C_last_row[ k ] -= x_ptr[ subfr_length - n - 1 ] * x_ptr[ subfr_length - n + k ];
michael@0 84 Atmp = Af[ k ];
michael@0 85 tmp1 += x_ptr[ n - k - 1 ] * Atmp;
michael@0 86 tmp2 += x_ptr[ subfr_length - n + k ] * Atmp;
michael@0 87 }
michael@0 88 for( k = 0; k <= n; k++ ) {
michael@0 89 CAf[ k ] -= tmp1 * x_ptr[ n - k ];
michael@0 90 CAb[ k ] -= tmp2 * x_ptr[ subfr_length - n + k - 1 ];
michael@0 91 }
michael@0 92 }
michael@0 93 tmp1 = C_first_row[ n ];
michael@0 94 tmp2 = C_last_row[ n ];
michael@0 95 for( k = 0; k < n; k++ ) {
michael@0 96 Atmp = Af[ k ];
michael@0 97 tmp1 += C_last_row[ n - k - 1 ] * Atmp;
michael@0 98 tmp2 += C_first_row[ n - k - 1 ] * Atmp;
michael@0 99 }
michael@0 100 CAf[ n + 1 ] = tmp1;
michael@0 101 CAb[ n + 1 ] = tmp2;
michael@0 102
michael@0 103 /* Calculate nominator and denominator for the next order reflection (parcor) coefficient */
michael@0 104 num = CAb[ n + 1 ];
michael@0 105 nrg_b = CAb[ 0 ];
michael@0 106 nrg_f = CAf[ 0 ];
michael@0 107 for( k = 0; k < n; k++ ) {
michael@0 108 Atmp = Af[ k ];
michael@0 109 num += CAb[ n - k ] * Atmp;
michael@0 110 nrg_b += CAb[ k + 1 ] * Atmp;
michael@0 111 nrg_f += CAf[ k + 1 ] * Atmp;
michael@0 112 }
michael@0 113 silk_assert( nrg_f > 0.0 );
michael@0 114 silk_assert( nrg_b > 0.0 );
michael@0 115
michael@0 116 /* Calculate the next order reflection (parcor) coefficient */
michael@0 117 rc = -2.0 * num / ( nrg_f + nrg_b );
michael@0 118 silk_assert( rc > -1.0 && rc < 1.0 );
michael@0 119
michael@0 120 /* Update inverse prediction gain */
michael@0 121 tmp1 = invGain * ( 1.0 - rc * rc );
michael@0 122 if( tmp1 <= minInvGain ) {
michael@0 123 /* Max prediction gain exceeded; set reflection coefficient such that max prediction gain is exactly hit */
michael@0 124 rc = sqrt( 1.0 - minInvGain / invGain );
michael@0 125 if( num > 0 ) {
michael@0 126 /* Ensure adjusted reflection coefficients has the original sign */
michael@0 127 rc = -rc;
michael@0 128 }
michael@0 129 invGain = minInvGain;
michael@0 130 reached_max_gain = 1;
michael@0 131 } else {
michael@0 132 invGain = tmp1;
michael@0 133 }
michael@0 134
michael@0 135 /* Update the AR coefficients */
michael@0 136 for( k = 0; k < (n + 1) >> 1; k++ ) {
michael@0 137 tmp1 = Af[ k ];
michael@0 138 tmp2 = Af[ n - k - 1 ];
michael@0 139 Af[ k ] = tmp1 + rc * tmp2;
michael@0 140 Af[ n - k - 1 ] = tmp2 + rc * tmp1;
michael@0 141 }
michael@0 142 Af[ n ] = rc;
michael@0 143
michael@0 144 if( reached_max_gain ) {
michael@0 145 /* Reached max prediction gain; set remaining coefficients to zero and exit loop */
michael@0 146 for( k = n + 1; k < D; k++ ) {
michael@0 147 Af[ k ] = 0.0;
michael@0 148 }
michael@0 149 break;
michael@0 150 }
michael@0 151
michael@0 152 /* Update C * Af and C * Ab */
michael@0 153 for( k = 0; k <= n + 1; k++ ) {
michael@0 154 tmp1 = CAf[ k ];
michael@0 155 CAf[ k ] += rc * CAb[ n - k + 1 ];
michael@0 156 CAb[ n - k + 1 ] += rc * tmp1;
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 if( reached_max_gain ) {
michael@0 161 /* Convert to silk_float */
michael@0 162 for( k = 0; k < D; k++ ) {
michael@0 163 A[ k ] = (silk_float)( -Af[ k ] );
michael@0 164 }
michael@0 165 /* Subtract energy of preceding samples from C0 */
michael@0 166 for( s = 0; s < nb_subfr; s++ ) {
michael@0 167 C0 -= silk_energy_FLP( x + s * subfr_length, D );
michael@0 168 }
michael@0 169 /* Approximate residual energy */
michael@0 170 nrg_f = C0 * invGain;
michael@0 171 } else {
michael@0 172 /* Compute residual energy and store coefficients as silk_float */
michael@0 173 nrg_f = CAf[ 0 ];
michael@0 174 tmp1 = 1.0;
michael@0 175 for( k = 0; k < D; k++ ) {
michael@0 176 Atmp = Af[ k ];
michael@0 177 nrg_f += CAf[ k + 1 ] * Atmp;
michael@0 178 tmp1 += Atmp * Atmp;
michael@0 179 A[ k ] = (silk_float)(-Atmp);
michael@0 180 }
michael@0 181 nrg_f -= FIND_LPC_COND_FAC * C0 * tmp1;
michael@0 182 }
michael@0 183
michael@0 184 /* Return residual energy */
michael@0 185 return (silk_float)nrg_f;
michael@0 186 }

mercurial