Coverage Report

Created: 2026-10-17 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/silk/gain_quant.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 "main.h"
33
34
1.27G
#define OFFSET                  ( ( MIN_QGAIN_DB * 128 ) / 6 + 16 * 128 )
35
#define SCALE_Q16               ( ( 65536 * ( N_LEVELS_QGAIN - 1 ) ) / ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) )
36
#define INV_SCALE_Q16           ( ( 65536 * ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) / ( N_LEVELS_QGAIN - 1 ) )
37
38
/* Gain scalar quantization with hysteresis, uniform on log scale */
39
void silk_gains_quant(
40
    opus_int8                   ind[ MAX_NB_SUBFR ],            /* O    gain indices                                */
41
    opus_int32                  gain_Q16[ MAX_NB_SUBFR ],       /* I/O  gains (quantized out)                       */
42
    opus_int8                   *prev_ind,                      /* I/O  last index in previous frame                */
43
    const opus_int              conditional,                    /* I    first gain is delta coded if 1              */
44
    const opus_int              nb_subfr                        /* I    number of subframes                         */
45
)
46
399M
{
47
399M
    opus_int k, double_step_size_threshold;
48
49
1.66G
    for( k = 0; k < nb_subfr; k++ ) {
50
        /* Convert to log scale, scale, floor() */
51
1.26G
        ind[ k ] = silk_SMULWB( SCALE_Q16, silk_lin2log( gain_Q16[ k ] ) - OFFSET );
52
53
        /* Round towards previous quantized gain (hysteresis) */
54
1.26G
        if( ind[ k ] < *prev_ind ) {
55
1.10G
            ind[ k ]++;
56
1.10G
        }
57
1.26G
        ind[ k ] = silk_LIMIT_int( ind[ k ], 0, N_LEVELS_QGAIN - 1 );
58
59
        /* Compute delta indices and limit */
60
1.26G
        if( k == 0 && conditional == 0 ) {
61
            /* Full index */
62
331M
            ind[ k ] = silk_LIMIT_int( ind[ k ], *prev_ind + MIN_DELTA_GAIN_QUANT, N_LEVELS_QGAIN - 1 );
63
331M
            *prev_ind = ind[ k ];
64
937M
        } else {
65
            /* Delta index */
66
937M
            ind[ k ] = ind[ k ] - *prev_ind;
67
68
            /* Double the quantization step size for large gain increases, so that the max gain level can be reached */
69
937M
            double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind;
70
937M
            if( ind[ k ] > double_step_size_threshold ) {
71
340k
                ind[ k ] = double_step_size_threshold + silk_RSHIFT( ind[ k ] - double_step_size_threshold + 1, 1 );
72
340k
            }
73
74
937M
            ind[ k ] = silk_LIMIT_int( ind[ k ], MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT );
75
76
            /* Accumulate deltas */
77
937M
            if( ind[ k ] > double_step_size_threshold ) {
78
340k
                *prev_ind += silk_LSHIFT( ind[ k ], 1 ) - double_step_size_threshold;
79
340k
                *prev_ind = silk_min_int( *prev_ind, N_LEVELS_QGAIN - 1 );
80
936M
            } else {
81
936M
                *prev_ind += ind[ k ];
82
936M
            }
83
84
            /* Shift to make non-negative */
85
937M
            ind[ k ] -= MIN_DELTA_GAIN_QUANT;
86
937M
        }
87
88
        /* Scale and convert to linear scale */
89
1.26G
        gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */
90
1.26G
    }
91
399M
}
92
93
/* Gains scalar dequantization, uniform on log scale */
94
void silk_gains_dequant(
95
    opus_int32                  gain_Q16[ MAX_NB_SUBFR ],       /* O    quantized gains                             */
96
    const opus_int8             ind[ MAX_NB_SUBFR ],            /* I    gain indices                                */
97
    opus_int8                   *prev_ind,                      /* I/O  last index in previous frame                */
98
    const opus_int              conditional,                    /* I    first gain is delta coded if 1              */
99
    const opus_int              nb_subfr                        /* I    number of subframes                          */
100
)
101
1.41M
{
102
1.41M
    opus_int   k, ind_tmp, double_step_size_threshold;
103
104
6.45M
    for( k = 0; k < nb_subfr; k++ ) {
105
5.04M
        if( k == 0 && conditional == 0 ) {
106
            /* Gain index is not allowed to go down more than 16 steps (~21.8 dB) */
107
1.00M
            *prev_ind = silk_max_int( ind[ k ], *prev_ind - 16 );
108
4.03M
        } else {
109
            /* Delta index */
110
4.03M
            ind_tmp = ind[ k ] + MIN_DELTA_GAIN_QUANT;
111
112
            /* Accumulate deltas */
113
4.03M
            double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind;
114
4.03M
            if( ind_tmp > double_step_size_threshold ) {
115
24.7k
                *prev_ind += silk_LSHIFT( ind_tmp, 1 ) - double_step_size_threshold;
116
4.01M
            } else {
117
4.01M
                *prev_ind += ind_tmp;
118
4.01M
            }
119
4.03M
        }
120
5.04M
        *prev_ind = silk_LIMIT_int( *prev_ind, 0, N_LEVELS_QGAIN - 1 );
121
122
        /* Scale and convert to linear scale */
123
5.04M
        gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */
124
5.04M
    }
125
1.41M
}
126
127
/* Compute unique identifier of gain indices vector */
128
opus_int32 silk_gains_ID(                                       /* O    returns unique identifier of gains          */
129
    const opus_int8             ind[ MAX_NB_SUBFR ],            /* I    gain indices                                */
130
    const opus_int              nb_subfr                        /* I    number of subframes                         */
131
)
132
399M
{
133
399M
    opus_int   k;
134
399M
    opus_int32 gainsID;
135
136
399M
    gainsID = 0;
137
1.66G
    for( k = 0; k < nb_subfr; k++ ) {
138
1.26G
        gainsID = silk_ADD_LSHIFT32( ind[ k ], gainsID, 8 );
139
1.26G
    }
140
141
399M
    return gainsID;
142
399M
}