Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/silk/LPC_inv_pred_gain.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_FIX.h"
33
#include "define.h"
34
35
#define QA                          24
36
12.1M
#define A_LIMIT                     SILK_FIX_CONST( 0.99975, QA )
37
38
#define MUL32_FRAC_Q(a32, b32, Q)   ((opus_int32)(silk_RSHIFT_ROUND64(silk_SMULL(a32, b32), Q)))
39
40
/* Compute inverse of LPC prediction gain, and                          */
41
/* test if LPC coefficients are stable (all poles within unit circle)   */
42
static opus_int32 LPC_inverse_pred_gain_QA_c(               /* O   Returns inverse prediction gain in energy domain, Q30    */
43
    opus_int32           A_QA[ SILK_MAX_ORDER_LPC ],        /* I   Prediction coefficients                                  */
44
    const opus_int       order                              /* I   Prediction order                                         */
45
)
46
521k
{
47
521k
    opus_int   k, n, mult2Q;
48
521k
    opus_int32 invGain_Q30, rc_Q31, rc_mult1_Q30, rc_mult2, tmp1, tmp2;
49
50
521k
    invGain_Q30 = SILK_FIX_CONST( 1, 30 );
51
6.06M
    for( k = order - 1; k > 0; k-- ) {
52
        /* Check for stability */
53
5.59M
        if( ( A_QA[ k ] > A_LIMIT ) || ( A_QA[ k ] < -A_LIMIT ) ) {
54
45.3k
            return 0;
55
45.3k
        }
56
57
        /* Set RC equal to negated AR coef */
58
5.54M
        rc_Q31 = -silk_LSHIFT( A_QA[ k ], 31 - QA );
59
60
        /* rc_mult1_Q30 range: [ 1 : 2^30 ] */
61
5.54M
        rc_mult1_Q30 = silk_SUB32( SILK_FIX_CONST( 1, 30 ), silk_SMMUL( rc_Q31, rc_Q31 ) );
62
5.54M
        silk_assert( rc_mult1_Q30 > ( 1 << 15 ) );                   /* reduce A_LIMIT if fails */
63
5.54M
        silk_assert( rc_mult1_Q30 <= ( 1 << 30 ) );
64
65
        /* Update inverse gain */
66
        /* invGain_Q30 range: [ 0 : 2^30 ] */
67
5.54M
        invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 );
68
5.54M
        silk_assert( invGain_Q30 >= 0           );
69
5.54M
        silk_assert( invGain_Q30 <= ( 1 << 30 ) );
70
5.54M
        if( invGain_Q30 < SILK_FIX_CONST( 1.0f / MAX_PREDICTION_POWER_GAIN, 30 ) ) {
71
2.33k
            return 0;
72
2.33k
        }
73
74
        /* rc_mult2 range: [ 2^30 : silk_int32_MAX ] */
75
5.54M
        mult2Q = 32 - silk_CLZ32( silk_abs( rc_mult1_Q30 ) );
76
5.54M
        rc_mult2 = silk_INVERSE32_varQ( rc_mult1_Q30, mult2Q + 30 );
77
78
        /* Update AR coefficient */
79
24.5M
        for( n = 0; n < (k + 1) >> 1; n++ ) {
80
19.0M
            opus_int64 tmp64;
81
19.0M
            tmp1 = A_QA[ n ];
82
19.0M
            tmp2 = A_QA[ k - n - 1 ];
83
19.0M
            tmp64 = silk_RSHIFT_ROUND64( silk_SMULL( silk_SUB_SAT32(tmp1,
84
19.0M
                  MUL32_FRAC_Q( tmp2, rc_Q31, 31 ) ), rc_mult2 ), mult2Q);
85
19.0M
            if( tmp64 > silk_int32_MAX || tmp64 < silk_int32_MIN ) {
86
1.02k
               return 0;
87
1.02k
            }
88
19.0M
            A_QA[ n ] = ( opus_int32 )tmp64;
89
19.0M
            tmp64 = silk_RSHIFT_ROUND64( silk_SMULL( silk_SUB_SAT32(tmp2,
90
19.0M
                  MUL32_FRAC_Q( tmp1, rc_Q31, 31 ) ), rc_mult2), mult2Q);
91
19.0M
            if( tmp64 > silk_int32_MAX || tmp64 < silk_int32_MIN ) {
92
0
               return 0;
93
0
            }
94
19.0M
            A_QA[ k - n - 1 ] = ( opus_int32 )tmp64;
95
19.0M
        }
96
5.54M
    }
97
98
    /* Check for stability */
99
473k
    if( ( A_QA[ k ] > A_LIMIT ) || ( A_QA[ k ] < -A_LIMIT ) ) {
100
9.46k
        return 0;
101
9.46k
    }
102
103
    /* Set RC equal to negated AR coef */
104
463k
    rc_Q31 = -silk_LSHIFT( A_QA[ 0 ], 31 - QA );
105
106
    /* Range: [ 1 : 2^30 ] */
107
463k
    rc_mult1_Q30 = silk_SUB32( SILK_FIX_CONST( 1, 30 ), silk_SMMUL( rc_Q31, rc_Q31 ) );
108
109
    /* Update inverse gain */
110
    /* Range: [ 0 : 2^30 ] */
111
463k
    invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 );
112
463k
    silk_assert( invGain_Q30 >= 0           );
113
463k
    silk_assert( invGain_Q30 <= ( 1 << 30 ) );
114
463k
    if( invGain_Q30 < SILK_FIX_CONST( 1.0f / MAX_PREDICTION_POWER_GAIN, 30 ) ) {
115
54.3k
        return 0;
116
54.3k
    }
117
118
409k
    return invGain_Q30;
119
463k
}
120
121
/* For input in Q12 domain */
122
opus_int32 silk_LPC_inverse_pred_gain_c(            /* O   Returns inverse prediction gain in energy domain, Q30        */
123
    const opus_int16            *A_Q12,             /* I   Prediction coefficients, Q12 [order]                         */
124
    const opus_int              order               /* I   Prediction order                                             */
125
)
126
534k
{
127
534k
    opus_int   k;
128
534k
    opus_int32 Atmp_QA[ SILK_MAX_ORDER_LPC ];
129
534k
    opus_int32 DC_resp = 0;
130
131
    /* Increase Q domain of the AR coefficients */
132
6.84M
    for( k = 0; k < order; k++ ) {
133
6.31M
        DC_resp += (opus_int32)A_Q12[ k ];
134
6.31M
        Atmp_QA[ k ] = silk_LSHIFT32( (opus_int32)A_Q12[ k ], QA - 12 );
135
6.31M
    }
136
    /* If the DC is unstable, we don't even need to do the full calculations */
137
534k
    if( DC_resp >= 4096 ) {
138
13.2k
        return 0;
139
13.2k
    }
140
521k
    return LPC_inverse_pred_gain_QA_c( Atmp_QA, order );
141
534k
}