xref: /aosp_15_r20/external/libopus/silk/resampler_private_down_FIR.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1*a58d3d2aSXin Li /***********************************************************************
2*a58d3d2aSXin Li Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3*a58d3d2aSXin Li Redistribution and use in source and binary forms, with or without
4*a58d3d2aSXin Li modification, are permitted provided that the following conditions
5*a58d3d2aSXin Li are met:
6*a58d3d2aSXin Li - Redistributions of source code must retain the above copyright notice,
7*a58d3d2aSXin Li this list of conditions and the following disclaimer.
8*a58d3d2aSXin Li - Redistributions in binary form must reproduce the above copyright
9*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer in the
10*a58d3d2aSXin Li documentation and/or other materials provided with the distribution.
11*a58d3d2aSXin Li - Neither the name of Internet Society, IETF or IETF Trust, nor the
12*a58d3d2aSXin Li names of specific contributors, may be used to endorse or promote
13*a58d3d2aSXin Li products derived from this software without specific prior written
14*a58d3d2aSXin Li permission.
15*a58d3d2aSXin Li THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16*a58d3d2aSXin Li AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*a58d3d2aSXin Li IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*a58d3d2aSXin Li ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19*a58d3d2aSXin Li LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*a58d3d2aSXin Li CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*a58d3d2aSXin Li SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*a58d3d2aSXin Li INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*a58d3d2aSXin Li CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*a58d3d2aSXin Li ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*a58d3d2aSXin Li POSSIBILITY OF SUCH DAMAGE.
26*a58d3d2aSXin Li ***********************************************************************/
27*a58d3d2aSXin Li 
28*a58d3d2aSXin Li #ifdef HAVE_CONFIG_H
29*a58d3d2aSXin Li #include "config.h"
30*a58d3d2aSXin Li #endif
31*a58d3d2aSXin Li 
32*a58d3d2aSXin Li #include "SigProc_FIX.h"
33*a58d3d2aSXin Li #include "resampler_private.h"
34*a58d3d2aSXin Li #include "stack_alloc.h"
35*a58d3d2aSXin Li 
silk_resampler_private_down_FIR_INTERPOL(opus_int16 * out,opus_int32 * buf,const opus_int16 * FIR_Coefs,opus_int FIR_Order,opus_int FIR_Fracs,opus_int32 max_index_Q16,opus_int32 index_increment_Q16)36*a58d3d2aSXin Li static OPUS_INLINE opus_int16 *silk_resampler_private_down_FIR_INTERPOL(
37*a58d3d2aSXin Li     opus_int16          *out,
38*a58d3d2aSXin Li     opus_int32          *buf,
39*a58d3d2aSXin Li     const opus_int16    *FIR_Coefs,
40*a58d3d2aSXin Li     opus_int            FIR_Order,
41*a58d3d2aSXin Li     opus_int            FIR_Fracs,
42*a58d3d2aSXin Li     opus_int32          max_index_Q16,
43*a58d3d2aSXin Li     opus_int32          index_increment_Q16
44*a58d3d2aSXin Li )
45*a58d3d2aSXin Li {
46*a58d3d2aSXin Li     opus_int32 index_Q16, res_Q6;
47*a58d3d2aSXin Li     opus_int32 *buf_ptr;
48*a58d3d2aSXin Li     opus_int32 interpol_ind;
49*a58d3d2aSXin Li     const opus_int16 *interpol_ptr;
50*a58d3d2aSXin Li 
51*a58d3d2aSXin Li     switch( FIR_Order ) {
52*a58d3d2aSXin Li         case RESAMPLER_DOWN_ORDER_FIR0:
53*a58d3d2aSXin Li             for( index_Q16 = 0; index_Q16 < max_index_Q16; index_Q16 += index_increment_Q16 ) {
54*a58d3d2aSXin Li                 /* Integer part gives pointer to buffered input */
55*a58d3d2aSXin Li                 buf_ptr = buf + silk_RSHIFT( index_Q16, 16 );
56*a58d3d2aSXin Li 
57*a58d3d2aSXin Li                 /* Fractional part gives interpolation coefficients */
58*a58d3d2aSXin Li                 interpol_ind = silk_SMULWB( index_Q16 & 0xFFFF, FIR_Fracs );
59*a58d3d2aSXin Li 
60*a58d3d2aSXin Li                 /* Inner product */
61*a58d3d2aSXin Li                 interpol_ptr = &FIR_Coefs[ RESAMPLER_DOWN_ORDER_FIR0 / 2 * interpol_ind ];
62*a58d3d2aSXin Li                 res_Q6 = silk_SMULWB(         buf_ptr[ 0 ], interpol_ptr[ 0 ] );
63*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 1 ], interpol_ptr[ 1 ] );
64*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 2 ], interpol_ptr[ 2 ] );
65*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 3 ], interpol_ptr[ 3 ] );
66*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 4 ], interpol_ptr[ 4 ] );
67*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 5 ], interpol_ptr[ 5 ] );
68*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 6 ], interpol_ptr[ 6 ] );
69*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 7 ], interpol_ptr[ 7 ] );
70*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 8 ], interpol_ptr[ 8 ] );
71*a58d3d2aSXin Li                 interpol_ptr = &FIR_Coefs[ RESAMPLER_DOWN_ORDER_FIR0 / 2 * ( FIR_Fracs - 1 - interpol_ind ) ];
72*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 17 ], interpol_ptr[ 0 ] );
73*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 16 ], interpol_ptr[ 1 ] );
74*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 15 ], interpol_ptr[ 2 ] );
75*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 14 ], interpol_ptr[ 3 ] );
76*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 13 ], interpol_ptr[ 4 ] );
77*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 12 ], interpol_ptr[ 5 ] );
78*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 11 ], interpol_ptr[ 6 ] );
79*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[ 10 ], interpol_ptr[ 7 ] );
80*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, buf_ptr[  9 ], interpol_ptr[ 8 ] );
81*a58d3d2aSXin Li 
82*a58d3d2aSXin Li                 /* Scale down, saturate and store in output array */
83*a58d3d2aSXin Li                 *out++ = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( res_Q6, 6 ) );
84*a58d3d2aSXin Li             }
85*a58d3d2aSXin Li             break;
86*a58d3d2aSXin Li         case RESAMPLER_DOWN_ORDER_FIR1:
87*a58d3d2aSXin Li             for( index_Q16 = 0; index_Q16 < max_index_Q16; index_Q16 += index_increment_Q16 ) {
88*a58d3d2aSXin Li                 /* Integer part gives pointer to buffered input */
89*a58d3d2aSXin Li                 buf_ptr = buf + silk_RSHIFT( index_Q16, 16 );
90*a58d3d2aSXin Li 
91*a58d3d2aSXin Li                 /* Inner product */
92*a58d3d2aSXin Li                 res_Q6 = silk_SMULWB(         silk_ADD32( buf_ptr[  0 ], buf_ptr[ 23 ] ), FIR_Coefs[  0 ] );
93*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  1 ], buf_ptr[ 22 ] ), FIR_Coefs[  1 ] );
94*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  2 ], buf_ptr[ 21 ] ), FIR_Coefs[  2 ] );
95*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  3 ], buf_ptr[ 20 ] ), FIR_Coefs[  3 ] );
96*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  4 ], buf_ptr[ 19 ] ), FIR_Coefs[  4 ] );
97*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  5 ], buf_ptr[ 18 ] ), FIR_Coefs[  5 ] );
98*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  6 ], buf_ptr[ 17 ] ), FIR_Coefs[  6 ] );
99*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  7 ], buf_ptr[ 16 ] ), FIR_Coefs[  7 ] );
100*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  8 ], buf_ptr[ 15 ] ), FIR_Coefs[  8 ] );
101*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  9 ], buf_ptr[ 14 ] ), FIR_Coefs[  9 ] );
102*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 10 ], buf_ptr[ 13 ] ), FIR_Coefs[ 10 ] );
103*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 11 ], buf_ptr[ 12 ] ), FIR_Coefs[ 11 ] );
104*a58d3d2aSXin Li 
105*a58d3d2aSXin Li                 /* Scale down, saturate and store in output array */
106*a58d3d2aSXin Li                 *out++ = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( res_Q6, 6 ) );
107*a58d3d2aSXin Li             }
108*a58d3d2aSXin Li             break;
109*a58d3d2aSXin Li         case RESAMPLER_DOWN_ORDER_FIR2:
110*a58d3d2aSXin Li             for( index_Q16 = 0; index_Q16 < max_index_Q16; index_Q16 += index_increment_Q16 ) {
111*a58d3d2aSXin Li                 /* Integer part gives pointer to buffered input */
112*a58d3d2aSXin Li                 buf_ptr = buf + silk_RSHIFT( index_Q16, 16 );
113*a58d3d2aSXin Li 
114*a58d3d2aSXin Li                 /* Inner product */
115*a58d3d2aSXin Li                 res_Q6 = silk_SMULWB(         silk_ADD32( buf_ptr[  0 ], buf_ptr[ 35 ] ), FIR_Coefs[  0 ] );
116*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  1 ], buf_ptr[ 34 ] ), FIR_Coefs[  1 ] );
117*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  2 ], buf_ptr[ 33 ] ), FIR_Coefs[  2 ] );
118*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  3 ], buf_ptr[ 32 ] ), FIR_Coefs[  3 ] );
119*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  4 ], buf_ptr[ 31 ] ), FIR_Coefs[  4 ] );
120*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  5 ], buf_ptr[ 30 ] ), FIR_Coefs[  5 ] );
121*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  6 ], buf_ptr[ 29 ] ), FIR_Coefs[  6 ] );
122*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  7 ], buf_ptr[ 28 ] ), FIR_Coefs[  7 ] );
123*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  8 ], buf_ptr[ 27 ] ), FIR_Coefs[  8 ] );
124*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[  9 ], buf_ptr[ 26 ] ), FIR_Coefs[  9 ] );
125*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 10 ], buf_ptr[ 25 ] ), FIR_Coefs[ 10 ] );
126*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 11 ], buf_ptr[ 24 ] ), FIR_Coefs[ 11 ] );
127*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 12 ], buf_ptr[ 23 ] ), FIR_Coefs[ 12 ] );
128*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 13 ], buf_ptr[ 22 ] ), FIR_Coefs[ 13 ] );
129*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 14 ], buf_ptr[ 21 ] ), FIR_Coefs[ 14 ] );
130*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 15 ], buf_ptr[ 20 ] ), FIR_Coefs[ 15 ] );
131*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 16 ], buf_ptr[ 19 ] ), FIR_Coefs[ 16 ] );
132*a58d3d2aSXin Li                 res_Q6 = silk_SMLAWB( res_Q6, silk_ADD32( buf_ptr[ 17 ], buf_ptr[ 18 ] ), FIR_Coefs[ 17 ] );
133*a58d3d2aSXin Li 
134*a58d3d2aSXin Li                 /* Scale down, saturate and store in output array */
135*a58d3d2aSXin Li                 *out++ = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( res_Q6, 6 ) );
136*a58d3d2aSXin Li             }
137*a58d3d2aSXin Li             break;
138*a58d3d2aSXin Li         default:
139*a58d3d2aSXin Li             celt_assert( 0 );
140*a58d3d2aSXin Li     }
141*a58d3d2aSXin Li     return out;
142*a58d3d2aSXin Li }
143*a58d3d2aSXin Li 
144*a58d3d2aSXin Li /* Resample with a 2nd order AR filter followed by FIR interpolation */
silk_resampler_private_down_FIR(void * SS,opus_int16 out[],const opus_int16 in[],opus_int32 inLen)145*a58d3d2aSXin Li void silk_resampler_private_down_FIR(
146*a58d3d2aSXin Li     void                            *SS,            /* I/O  Resampler state             */
147*a58d3d2aSXin Li     opus_int16                      out[],          /* O    Output signal               */
148*a58d3d2aSXin Li     const opus_int16                in[],           /* I    Input signal                */
149*a58d3d2aSXin Li     opus_int32                      inLen           /* I    Number of input samples     */
150*a58d3d2aSXin Li )
151*a58d3d2aSXin Li {
152*a58d3d2aSXin Li     silk_resampler_state_struct *S = (silk_resampler_state_struct *)SS;
153*a58d3d2aSXin Li     opus_int32 nSamplesIn;
154*a58d3d2aSXin Li     opus_int32 max_index_Q16, index_increment_Q16;
155*a58d3d2aSXin Li     VARDECL( opus_int32, buf );
156*a58d3d2aSXin Li     const opus_int16 *FIR_Coefs;
157*a58d3d2aSXin Li     SAVE_STACK;
158*a58d3d2aSXin Li 
159*a58d3d2aSXin Li     ALLOC( buf, S->batchSize + S->FIR_Order, opus_int32 );
160*a58d3d2aSXin Li 
161*a58d3d2aSXin Li     /* Copy buffered samples to start of buffer */
162*a58d3d2aSXin Li     silk_memcpy( buf, S->sFIR.i32, S->FIR_Order * sizeof( opus_int32 ) );
163*a58d3d2aSXin Li 
164*a58d3d2aSXin Li     FIR_Coefs = &S->Coefs[ 2 ];
165*a58d3d2aSXin Li 
166*a58d3d2aSXin Li     /* Iterate over blocks of frameSizeIn input samples */
167*a58d3d2aSXin Li     index_increment_Q16 = S->invRatio_Q16;
168*a58d3d2aSXin Li     while( 1 ) {
169*a58d3d2aSXin Li         nSamplesIn = silk_min( inLen, S->batchSize );
170*a58d3d2aSXin Li 
171*a58d3d2aSXin Li         /* Second-order AR filter (output in Q8) */
172*a58d3d2aSXin Li         silk_resampler_private_AR2( S->sIIR, &buf[ S->FIR_Order ], in, S->Coefs, nSamplesIn );
173*a58d3d2aSXin Li 
174*a58d3d2aSXin Li         max_index_Q16 = silk_LSHIFT32( nSamplesIn, 16 );
175*a58d3d2aSXin Li 
176*a58d3d2aSXin Li         /* Interpolate filtered signal */
177*a58d3d2aSXin Li         out = silk_resampler_private_down_FIR_INTERPOL( out, buf, FIR_Coefs, S->FIR_Order,
178*a58d3d2aSXin Li             S->FIR_Fracs, max_index_Q16, index_increment_Q16 );
179*a58d3d2aSXin Li 
180*a58d3d2aSXin Li         in += nSamplesIn;
181*a58d3d2aSXin Li         inLen -= nSamplesIn;
182*a58d3d2aSXin Li 
183*a58d3d2aSXin Li         if( inLen > 1 ) {
184*a58d3d2aSXin Li             /* More iterations to do; copy last part of filtered signal to beginning of buffer */
185*a58d3d2aSXin Li             silk_memcpy( buf, &buf[ nSamplesIn ], S->FIR_Order * sizeof( opus_int32 ) );
186*a58d3d2aSXin Li         } else {
187*a58d3d2aSXin Li             break;
188*a58d3d2aSXin Li         }
189*a58d3d2aSXin Li     }
190*a58d3d2aSXin Li 
191*a58d3d2aSXin Li     /* Copy last part of filtered signal to the state for the next call */
192*a58d3d2aSXin Li     silk_memcpy( S->sFIR.i32, &buf[ nSamplesIn ], S->FIR_Order * sizeof( opus_int32 ) );
193*a58d3d2aSXin Li     RESTORE_STACK;
194*a58d3d2aSXin Li }
195