Coverage Report

Created: 2025-07-18 07:17

/src/opus/silk/float/burg_modified_FLP.c
Line
Count
Source
1
/***********************************************************************
2
Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3
Redistribution and use in source and binary forms, with or without
4
modification, are permitted provided that the following conditions
5
are met:
6
- Redistributions of source code must retain the above copyright notice,
7
this list of conditions and the following disclaimer.
8
- Redistributions in binary form must reproduce the above copyright
9
notice, this list of conditions and the following disclaimer in the
10
documentation and/or other materials provided with the distribution.
11
- Neither the name of Internet Society, IETF or IETF Trust, nor the
12
names of specific contributors, may be used to endorse or promote
13
products derived from this software without specific prior written
14
permission.
15
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
POSSIBILITY OF SUCH DAMAGE.
26
***********************************************************************/
27
28
#ifdef HAVE_CONFIG_H
29
#include "config.h"
30
#endif
31
32
#include "SigProc_FLP.h"
33
#include "tuning_parameters.h"
34
#include "define.h"
35
36
#define MAX_FRAME_SIZE              384 /* subfr_length * nb_subfr = ( 0.005 * 16000 + 16 ) * 4 = 384*/
37
38
/* Compute reflection coefficients from input signal */
39
silk_float silk_burg_modified_FLP(          /* O    returns residual energy                                     */
40
    silk_float          A[],                /* O    prediction coefficients (length order)                      */
41
    const silk_float    x[],                /* I    input signal, length: nb_subfr*(D+L_sub)                    */
42
    const silk_float    minInvGain,         /* I    minimum inverse prediction gain                             */
43
    const opus_int      subfr_length,       /* I    input signal subframe length (incl. D preceding samples)    */
44
    const opus_int      nb_subfr,           /* I    number of subframes stacked in x                            */
45
    const opus_int      D,                  /* I    order                                                       */
46
    int                 arch
47
)
48
44.8M
{
49
44.8M
    opus_int         k, n, s, reached_max_gain;
50
44.8M
    double           C0, invGain, num, nrg_f, nrg_b, rc, Atmp, tmp1, tmp2;
51
44.8M
    const silk_float *x_ptr;
52
44.8M
    double           C_first_row[ SILK_MAX_ORDER_LPC ], C_last_row[ SILK_MAX_ORDER_LPC ];
53
44.8M
    double           CAf[ SILK_MAX_ORDER_LPC + 1 ], CAb[ SILK_MAX_ORDER_LPC + 1 ];
54
44.8M
    double           Af[ SILK_MAX_ORDER_LPC ];
55
56
44.8M
    celt_assert( subfr_length * nb_subfr <= MAX_FRAME_SIZE );
57
58
    /* Compute autocorrelations, added over subframes */
59
44.8M
    C0 = silk_energy_FLP( x, nb_subfr * subfr_length );
60
44.8M
    silk_memset( C_first_row, 0, SILK_MAX_ORDER_LPC * sizeof( double ) );
61
178M
    for( s = 0; s < nb_subfr; s++ ) {
62
133M
        x_ptr = x + s * subfr_length;
63
1.69G
        for( n = 1; n < D + 1; n++ ) {
64
1.55G
            C_first_row[ n - 1 ] += silk_inner_product_FLP( x_ptr, x_ptr + n, subfr_length - n, arch );
65
1.55G
        }
66
133M
    }
67
44.8M
    silk_memcpy( C_last_row, C_first_row, SILK_MAX_ORDER_LPC * sizeof( double ) );
68
69
    /* Initialize */
70
44.8M
    CAb[ 0 ] = CAf[ 0 ] = C0 + FIND_LPC_COND_FAC * C0 + 1e-9f;
71
44.8M
    invGain = 1.0f;
72
44.8M
    reached_max_gain = 0;
73
562M
    for( n = 0; n < D; n++ ) {
74
        /* Update first row of correlation matrix (without first element) */
75
        /* Update last row of correlation matrix (without last element, stored in reversed order) */
76
        /* Update C * Af */
77
        /* Update C * flipud(Af) (stored in reversed order) */
78
2.06G
        for( s = 0; s < nb_subfr; s++ ) {
79
1.54G
            x_ptr = x + s * subfr_length;
80
1.54G
            tmp1 = x_ptr[ n ];
81
1.54G
            tmp2 = x_ptr[ subfr_length - n - 1 ];
82
10.2G
            for( k = 0; k < n; k++ ) {
83
8.72G
                C_first_row[ k ] -= x_ptr[ n ] * x_ptr[ n - k - 1 ];
84
8.72G
                C_last_row[ k ]  -= x_ptr[ subfr_length - n - 1 ] * x_ptr[ subfr_length - n + k ];
85
8.72G
                Atmp = Af[ k ];
86
8.72G
                tmp1 += x_ptr[ n - k - 1 ] * Atmp;
87
8.72G
                tmp2 += x_ptr[ subfr_length - n + k ] * Atmp;
88
8.72G
            }
89
11.8G
            for( k = 0; k <= n; k++ ) {
90
10.2G
                CAf[ k ] -= tmp1 * x_ptr[ n - k ];
91
10.2G
                CAb[ k ] -= tmp2 * x_ptr[ subfr_length - n + k - 1 ];
92
10.2G
            }
93
1.54G
        }
94
518M
        tmp1 = C_first_row[ n ];
95
518M
        tmp2 = C_last_row[ n ];
96
3.41G
        for( k = 0; k < n; k++ ) {
97
2.90G
            Atmp = Af[ k ];
98
2.90G
            tmp1 += C_last_row[  n - k - 1 ] * Atmp;
99
2.90G
            tmp2 += C_first_row[ n - k - 1 ] * Atmp;
100
2.90G
        }
101
518M
        CAf[ n + 1 ] = tmp1;
102
518M
        CAb[ n + 1 ] = tmp2;
103
104
        /* Calculate nominator and denominator for the next order reflection (parcor) coefficient */
105
518M
        num = CAb[ n + 1 ];
106
518M
        nrg_b = CAb[ 0 ];
107
518M
        nrg_f = CAf[ 0 ];
108
3.41G
        for( k = 0; k < n; k++ ) {
109
2.90G
            Atmp = Af[ k ];
110
2.90G
            num   += CAb[ n - k ] * Atmp;
111
2.90G
            nrg_b += CAb[ k + 1 ] * Atmp;
112
2.90G
            nrg_f += CAf[ k + 1 ] * Atmp;
113
2.90G
        }
114
518M
        silk_assert( nrg_f > 0.0 );
115
518M
        silk_assert( nrg_b > 0.0 );
116
117
        /* Calculate the next order reflection (parcor) coefficient */
118
518M
        rc = -2.0 * num / ( nrg_f + nrg_b );
119
518M
        silk_assert( rc > -1.0 && rc < 1.0 );
120
121
        /* Update inverse prediction gain */
122
518M
        tmp1 = invGain * ( 1.0 - rc * rc );
123
518M
        if( tmp1 <= minInvGain ) {
124
            /* Max prediction gain exceeded; set reflection coefficient such that max prediction gain is exactly hit */
125
275k
            rc = sqrt( 1.0 - minInvGain / invGain );
126
275k
            if( num > 0 ) {
127
                /* Ensure adjusted reflection coefficients has the original sign */
128
219k
                rc = -rc;
129
219k
            }
130
275k
            invGain = minInvGain;
131
275k
            reached_max_gain = 1;
132
517M
        } else {
133
517M
            invGain = tmp1;
134
517M
        }
135
136
        /* Update the AR coefficients */
137
2.09G
        for( k = 0; k < (n + 1) >> 1; k++ ) {
138
1.57G
            tmp1 = Af[ k ];
139
1.57G
            tmp2 = Af[ n - k - 1 ];
140
1.57G
            Af[ k ]         = tmp1 + rc * tmp2;
141
1.57G
            Af[ n - k - 1 ] = tmp2 + rc * tmp1;
142
1.57G
        }
143
518M
        Af[ n ] = rc;
144
145
518M
        if( reached_max_gain ) {
146
            /* Reached max prediction gain; set remaining coefficients to zero and exit loop */
147
2.85M
            for( k = n + 1; k < D; k++ ) {
148
2.57M
                Af[ k ] = 0.0;
149
2.57M
            }
150
275k
            break;
151
275k
        }
152
153
        /* Update C * Af and C * Ab */
154
4.45G
        for( k = 0; k <= n + 1; k++ ) {
155
3.93G
            tmp1 = CAf[ k ];
156
3.93G
            CAf[ k ]          += rc * CAb[ n - k + 1 ];
157
3.93G
            CAb[ n - k + 1  ] += rc * tmp1;
158
3.93G
        }
159
517M
    }
160
161
44.8M
    if( reached_max_gain ) {
162
        /* Convert to silk_float */
163
3.59M
        for( k = 0; k < D; k++ ) {
164
3.31M
            A[ k ] = (silk_float)( -Af[ k ] );
165
3.31M
        }
166
        /* Subtract energy of preceding samples from C0 */
167
1.16M
        for( s = 0; s < nb_subfr; s++ ) {
168
884k
            C0 -= silk_energy_FLP( x + s * subfr_length, D );
169
884k
        }
170
        /* Approximate residual energy */
171
275k
        nrg_f = C0 * invGain;
172
44.6M
    } else {
173
        /* Compute residual energy and store coefficients as silk_float */
174
44.6M
        nrg_f = CAf[ 0 ];
175
44.6M
        tmp1 = 1.0;
176
561M
        for( k = 0; k < D; k++ ) {
177
517M
            Atmp = Af[ k ];
178
517M
            nrg_f += CAf[ k + 1 ] * Atmp;
179
517M
            tmp1  += Atmp * Atmp;
180
517M
            A[ k ] = (silk_float)(-Atmp);
181
517M
        }
182
44.6M
        nrg_f -= FIND_LPC_COND_FAC * C0 * tmp1;
183
44.6M
    }
184
185
    /* Return residual energy */
186
44.8M
    return (silk_float)nrg_f;
187
44.8M
}