Coverage Report

Created: 2025-11-16 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/silk/resampler.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
/*
33
 * Matrix of resampling methods used:
34
 *                                 Fs_out (kHz)
35
 *                        8      12     16     24     48
36
 *
37
 *               8        C      UF     U      UF     UF
38
 *              12        AF     C      UF     U      UF
39
 * Fs_in (kHz)  16        D      AF     C      UF     UF
40
 *              24        AF     D      AF     C      U
41
 *              48        AF     AF     AF     D      C
42
 *
43
 * C   -> Copy (no resampling)
44
 * D   -> Allpass-based 2x downsampling
45
 * U   -> Allpass-based 2x upsampling
46
 * UF  -> Allpass-based 2x upsampling followed by FIR interpolation
47
 * AF  -> AR2 filter followed by FIR interpolation
48
 */
49
50
#include "resampler_private.h"
51
52
/* Tables with delay compensation values to equalize total delay for different modes */
53
static const opus_int8 delay_matrix_enc[ 6 ][ 3 ] = {
54
/* in  \ out  8  12  16 */
55
/*  8 */   {  6,  0,  3 },
56
/* 12 */   {  0,  7,  3 },
57
/* 16 */   {  0,  1, 10 },
58
/* 24 */   {  0,  2,  6 },
59
/* 48 */   { 18, 10, 12 },
60
/* 96 */   {  0,  0,  44 }
61
};
62
63
static const opus_int8 delay_matrix_dec[ 3 ][ 6 ] = {
64
/* in  \ out  8  12  16  24  48  96*/
65
/*  8 */   {  4,  0,  2,  0,  0,  0 },
66
/* 12 */   {  0,  9,  4,  7,  4,  4 },
67
/* 16 */   {  0,  3, 12,  7,  7,  7 }
68
};
69
70
/* Simple way to make [8000, 12000, 16000, 24000, 48000] to [0, 1, 2, 3, 4] */
71
12.1k
#define rateID(R) IMIN(5, ( ( ( ((R)>>12) - ((R)>16000) ) >> ((R)>24000) ) - 1 ))
72
73
2.57k
#define USE_silk_resampler_copy                     (0)
74
84
#define USE_silk_resampler_private_up2_HQ_wrapper   (1)
75
118
#define USE_silk_resampler_private_IIR_FIR          (2)
76
16.3M
#define USE_silk_resampler_private_down_FIR         (3)
77
78
/* Initialize/reset the resampler state for a given pair of input/output sampling rates */
79
opus_int silk_resampler_init(
80
    silk_resampler_state_struct *S,                 /* I/O  Resampler state                                             */
81
    opus_int32                  Fs_Hz_in,           /* I    Input sampling rate (Hz)                                    */
82
    opus_int32                  Fs_Hz_out,          /* I    Output sampling rate (Hz)                                   */
83
    opus_int                    forEnc              /* I    If 1: encoder; if 0: decoder                                */
84
)
85
6.05k
{
86
6.05k
    opus_int up2x;
87
88
    /* Clear state */
89
6.05k
    silk_memset( S, 0, sizeof( silk_resampler_state_struct ) );
90
91
    /* Input checking */
92
6.05k
    if( forEnc ) {
93
5.95k
        if( ( Fs_Hz_in  != 8000 && Fs_Hz_in  != 12000 && Fs_Hz_in  != 16000 && Fs_Hz_in  != 24000 && Fs_Hz_in  != 48000
94
#ifdef ENABLE_QEXT
95
                  && Fs_Hz_in != 96000
96
#endif
97
5.95k
              ) ||
98
5.95k
            ( Fs_Hz_out != 8000 && Fs_Hz_out != 12000 && Fs_Hz_out != 16000 ) ) {
99
0
            celt_assert( 0 );
100
0
            return -1;
101
0
        }
102
5.95k
        S->inputDelay = delay_matrix_enc[ rateID( Fs_Hz_in ) ][ rateID( Fs_Hz_out ) ];
103
5.95k
    } else {
104
101
        if( ( Fs_Hz_in  != 8000 && Fs_Hz_in  != 12000 && Fs_Hz_in  != 16000 ) ||
105
101
            ( Fs_Hz_out != 8000 && Fs_Hz_out != 12000 && Fs_Hz_out != 16000 && Fs_Hz_out != 24000 && Fs_Hz_out != 48000
106
#ifdef ENABLE_QEXT
107
                  && Fs_Hz_out != 96000
108
#endif
109
101
                  ) ) {
110
0
            celt_assert( 0 );
111
0
            return -1;
112
0
        }
113
101
        S->inputDelay = delay_matrix_dec[ rateID( Fs_Hz_in ) ][ rateID( Fs_Hz_out ) ];
114
101
    }
115
116
6.05k
    S->Fs_in_kHz  = silk_DIV32_16( Fs_Hz_in,  1000 );
117
6.05k
    S->Fs_out_kHz = silk_DIV32_16( Fs_Hz_out, 1000 );
118
119
    /* Number of samples processed per batch */
120
6.05k
    S->batchSize = S->Fs_in_kHz * RESAMPLER_MAX_BATCH_SIZE_MS;
121
122
    /* Find resampler with the right sampling ratio */
123
6.05k
    up2x = 0;
124
6.05k
    if( Fs_Hz_out > Fs_Hz_in ) {
125
        /* Upsample */
126
101
        if( Fs_Hz_out == silk_MUL( Fs_Hz_in, 2 ) ) {                            /* Fs_out : Fs_in = 2 : 1 */
127
            /* Special case: directly use 2x upsampler */
128
42
            S->resampler_function = USE_silk_resampler_private_up2_HQ_wrapper;
129
59
        } else {
130
            /* Default resampler */
131
59
            S->resampler_function = USE_silk_resampler_private_IIR_FIR;
132
59
            up2x = 1;
133
59
        }
134
5.95k
    } else if ( Fs_Hz_out < Fs_Hz_in ) {
135
        /* Downsample */
136
3.37k
         S->resampler_function = USE_silk_resampler_private_down_FIR;
137
3.37k
        if( silk_MUL( Fs_Hz_out, 4 ) == silk_MUL( Fs_Hz_in, 3 ) ) {             /* Fs_out : Fs_in = 3 : 4 */
138
291
            S->FIR_Fracs = 3;
139
291
            S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR0;
140
291
            S->Coefs = silk_Resampler_3_4_COEFS;
141
3.08k
        } else if( silk_MUL( Fs_Hz_out, 3 ) == silk_MUL( Fs_Hz_in, 2 ) ) {      /* Fs_out : Fs_in = 2 : 3 */
142
1.02k
            S->FIR_Fracs = 2;
143
1.02k
            S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR0;
144
1.02k
            S->Coefs = silk_Resampler_2_3_COEFS;
145
2.06k
        } else if( silk_MUL( Fs_Hz_out, 2 ) == Fs_Hz_in ) {                     /* Fs_out : Fs_in = 1 : 2 */
146
568
            S->FIR_Fracs = 1;
147
568
            S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR1;
148
568
            S->Coefs = silk_Resampler_1_2_COEFS;
149
1.49k
        } else if( silk_MUL( Fs_Hz_out, 3 ) == Fs_Hz_in ) {                     /* Fs_out : Fs_in = 1 : 3 */
150
989
            S->FIR_Fracs = 1;
151
989
            S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR2;
152
989
            S->Coefs = silk_Resampler_1_3_COEFS;
153
989
        } else if( silk_MUL( Fs_Hz_out, 4 ) == Fs_Hz_in ) {                     /* Fs_out : Fs_in = 1 : 4 */
154
205
            S->FIR_Fracs = 1;
155
205
            S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR2;
156
205
            S->Coefs = silk_Resampler_1_4_COEFS;
157
299
        } else if( silk_MUL( Fs_Hz_out, 6 ) == Fs_Hz_in ) {                     /* Fs_out : Fs_in = 1 : 6 */
158
299
            S->FIR_Fracs = 1;
159
299
            S->FIR_Order = RESAMPLER_DOWN_ORDER_FIR2;
160
299
            S->Coefs = silk_Resampler_1_6_COEFS;
161
299
        } else {
162
            /* None available */
163
0
            celt_assert( 0 );
164
0
            return -1;
165
0
        }
166
3.37k
    } else {
167
        /* Input and output sampling rates are equal: copy */
168
2.57k
        S->resampler_function = USE_silk_resampler_copy;
169
2.57k
    }
170
171
    /* Ratio of input/output samples */
172
6.05k
    S->invRatio_Q16 = silk_LSHIFT32( silk_DIV32( silk_LSHIFT32( Fs_Hz_in, 14 + up2x ), Fs_Hz_out ), 2 );
173
    /* Make sure the ratio is rounded up */
174
6.72k
    while( silk_SMULWW( S->invRatio_Q16, Fs_Hz_out ) < silk_LSHIFT32( Fs_Hz_in, up2x ) ) {
175
674
        S->invRatio_Q16++;
176
674
    }
177
178
6.05k
    return 0;
179
6.05k
}
180
181
/* Resampler: convert from one sampling rate to another */
182
/* Input and output sampling rate are at most 48000 Hz  */
183
opus_int silk_resampler(
184
    silk_resampler_state_struct *S,                 /* I/O  Resampler state                                             */
185
    opus_int16                  out[],              /* O    Output signal                                               */
186
    const opus_int16            in[],               /* I    Input signal                                                */
187
    opus_int32                  inLen               /* I    Number of input samples                                     */
188
)
189
34.3M
{
190
34.3M
    opus_int nSamples;
191
192
    /* Need at least 1 ms of input data */
193
34.3M
    celt_assert( inLen >= S->Fs_in_kHz );
194
    /* Delay can't exceed the 1 ms of buffering */
195
34.3M
    celt_assert( S->inputDelay <= S->Fs_in_kHz );
196
197
34.3M
    nSamples = S->Fs_in_kHz - S->inputDelay;
198
199
    /* Copy to delay buffer */
200
34.3M
    silk_memcpy( &S->delayBuf[ S->inputDelay ], in, nSamples * sizeof( opus_int16 ) );
201
202
34.3M
    switch( S->resampler_function ) {
203
42
        case USE_silk_resampler_private_up2_HQ_wrapper:
204
42
            silk_resampler_private_up2_HQ_wrapper( S, out, S->delayBuf, S->Fs_in_kHz );
205
42
            silk_resampler_private_up2_HQ_wrapper( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz );
206
42
            break;
207
59
        case USE_silk_resampler_private_IIR_FIR:
208
59
            silk_resampler_private_IIR_FIR( S, out, S->delayBuf, S->Fs_in_kHz );
209
59
            silk_resampler_private_IIR_FIR( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz );
210
59
            break;
211
16.3M
        case USE_silk_resampler_private_down_FIR:
212
16.3M
            silk_resampler_private_down_FIR( S, out, S->delayBuf, S->Fs_in_kHz );
213
16.3M
            silk_resampler_private_down_FIR( S, &out[ S->Fs_out_kHz ], &in[ nSamples ], inLen - S->Fs_in_kHz );
214
16.3M
            break;
215
17.9M
        default:
216
17.9M
            silk_memcpy( out, S->delayBuf, S->Fs_in_kHz * sizeof( opus_int16 ) );
217
17.9M
            silk_memcpy( &out[ S->Fs_out_kHz ], &in[ nSamples ], ( inLen - S->Fs_in_kHz ) * sizeof( opus_int16 ) );
218
34.3M
    }
219
220
    /* Copy to delay buffer */
221
34.3M
    silk_memcpy( S->delayBuf, &in[ inLen - S->inputDelay ], S->inputDelay * sizeof( opus_int16 ) );
222
223
34.3M
    return 0;
224
34.3M
}