Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdlib.h>
6 : #include "options.h"
7 : #include "cnst.h"
8 : //#include "prot_fx.h"
9 : #include "rom_com_fx.h"
10 : #include "rom_com.h"
11 : #include "stl.h"
12 : #include "prot_fx.h" /* Function prototypes */
13 : #include "ivas_prot_fx.h" /* Function prototypes */
14 : #include "prot_fx_enc.h" /* Function prototypes */
15 : #include "basop_util.h"
16 : #include "ivas_rom_enc.h"
17 :
18 : #include <assert.h>
19 :
20 : /*========================================================================*/
21 : /* FUNCTION : wb_pre_proc_fx() */
22 : /*------------------------------------------------------------------------*/
23 : /* PURPOSE : Resampling of input signal when input signal sample rate */
24 : /* is above 16kHz */
25 : /*------------------------------------------------------------------------*/
26 : /* INPUT ARGUMENTS : */
27 : /* Encoder_State *st_fx : Encoder State Structure */
28 : /* _ (Word16*) input : original input signal */
29 : /*------------------------------------------------------------------------*/
30 : /* INPUT/OUTPUT ARGUMENTS : */
31 : /* _None */
32 : /*------------------------------------------------------------------------*/
33 : /* OUTPUT ARGUMENTS : */
34 : /* _ (Word16*) new_wb_speech : original input signal at 16kHz Q-1 */
35 : /*------------------------------------------------------------------------*/
36 :
37 : /* st_fx->old_wtda_wb_fx */
38 : /*------------------------------------------------------------------------*/
39 : /* RETURN ARGUMENTS : */
40 : /* _ None */
41 : /*========================================================================*/
42 :
43 0 : void wb_pre_proc_fx(
44 : Encoder_State *st_fx, /* i/o: encoder state structure */
45 : const Word16 *new_inp_resamp16k, /* i : original input signal in Q-1 */
46 : Word16 *hb_speech /* o : HB target signal (6-8kHz) at 16kHz in Q-1 */
47 : )
48 : {
49 : Word16 Sample_Delay_WB_BWE;
50 : Word16 ramp_flag;
51 : Word16 old_input[NS2SA( 16000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k];
52 : Word16 *highband_new_speech;
53 : Word16 highband_old_speech[( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16];
54 : Word16 temp_buf[320];
55 : Word16 Q_wb_sp, i, max_wb;
56 0 : Word16 fSwitchFromIO = 0;
57 0 : move16();
58 0 : Word32 temp1 = 0;
59 0 : move32();
60 0 : Word32 temp2 = 0;
61 0 : move32();
62 0 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
63 0 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
64 0 : FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
65 :
66 0 : test();
67 0 : test();
68 0 : test();
69 0 : test();
70 0 : test();
71 0 : test();
72 0 : IF( ( EQ_32( st_fx->last_total_brate, ACELP_6k60 ) ) ||
73 : ( EQ_32( st_fx->last_total_brate, ACELP_8k85 ) ) ||
74 : ( EQ_32( st_fx->last_total_brate, ACELP_12k65 ) ) ||
75 : ( EQ_32( st_fx->last_total_brate, ACELP_14k25 ) ) ||
76 : ( EQ_32( st_fx->last_total_brate, ACELP_15k85 ) ) ||
77 : ( GE_32( st_fx->last_total_brate, ACELP_18k25 ) && LE_32( st_fx->last_total_brate, ACELP_23k85 ) ) )
78 : {
79 0 : fSwitchFromIO = 1;
80 0 : move16();
81 : }
82 :
83 0 : set16_fx( old_input, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k );
84 :
85 0 : max_wb = 1;
86 0 : move16();
87 0 : FOR( i = 0; i < L_FRAME16k; i++ )
88 : {
89 0 : max_wb = s_max( max_wb, abs_s( new_inp_resamp16k[i] ) );
90 : }
91 0 : Q_wb_sp = norm_s( max_wb );
92 :
93 0 : Copy_Scale_sig( new_inp_resamp16k, temp_buf, L_FRAME16k, -3 );
94 0 : temp1 = L_mac0( temp1, temp_buf[0], temp_buf[0] );
95 0 : FOR( i = 1; i < L_FRAME16k; i++ )
96 : {
97 0 : temp1 = L_mac0( temp1, temp_buf[i], temp_buf[i] );
98 0 : temp2 = L_mac0( temp2, temp_buf[i - 1], temp_buf[i] );
99 : }
100 :
101 0 : IF( temp2 < Mult_32_16( temp1, 31129 /*0.95f*/ ) )
102 : {
103 0 : Q_wb_sp = sub( Q_wb_sp, 3 ); /* leave 3 bit headroom */
104 : }
105 0 : Copy_Scale_sig( new_inp_resamp16k, temp_buf, L_FRAME16k, Q_wb_sp );
106 0 : Scale_sig( hBWE_TD->decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), Q_wb_sp );
107 0 : Scale_sig( hBWE_TD->decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), Q_wb_sp );
108 : /* temp_buf, and the memory states are in Q_wb_sp */
109 :
110 0 : test();
111 0 : test();
112 0 : IF( EQ_16( st_fx->extl, WB_BWE ) || EQ_16( st_fx->extl, WB_TBE ) || st_fx->igf != 0 )
113 : {
114 0 : ramp_flag = 0;
115 0 : move16();
116 :
117 0 : test();
118 0 : test();
119 0 : test();
120 0 : test();
121 0 : if ( ( NE_16( st_fx->last_extl, WB_TBE ) && NE_16( st_fx->last_extl, WB_BWE ) && st_fx->igf == 0 ) ||
122 0 : ( st_fx->igf != 0 && fSwitchFromIO != 0 ) )
123 : {
124 0 : ramp_flag = 1;
125 0 : move16();
126 : }
127 :
128 0 : IF( !hSC_VBR->ppp_mode )
129 : {
130 : /* temp_buf is in Q_wb_sp
131 : hb_speech and the two decimator memories are in Q_wb_sp */
132 0 : flip_spectrum_and_decimby4_fx( temp_buf, hb_speech, L_FRAME16k, hBWE_TD->decim_state1_fx, hBWE_TD->decim_state2_fx, ramp_flag );
133 :
134 : /* rescale the hb_speech and memories back to Q-1 to keep the downstream BWE coding unaffected */
135 0 : Scale_sig( hb_speech, L_FRAME16k / 4, -Q_wb_sp );
136 0 : Scale_sig( hBWE_TD->decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), -Q_wb_sp );
137 0 : Scale_sig( hBWE_TD->decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), -Q_wb_sp );
138 :
139 0 : IF( NE_16( st_fx->extl, WB_TBE ) )
140 : {
141 : /* Update the previous wideband speech buffer in case of a WB_BWE frame */
142 0 : Sample_Delay_WB_BWE = ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16;
143 :
144 0 : highband_new_speech = highband_old_speech + Sample_Delay_WB_BWE;
145 0 : Copy( hb_speech, highband_new_speech, L_FRAME16k / 4 );
146 0 : Copy( highband_old_speech + L_FRAME16k / 4, hBWE_TD->old_speech_wb_fx, Sample_Delay_WB_BWE );
147 : }
148 : }
149 : }
150 : ELSE
151 : {
152 0 : set16_fx( hBWE_TD->decim_state1_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
153 0 : set16_fx( hBWE_TD->decim_state2_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
154 0 : set16_fx( hBWE_TD->old_speech_wb_fx, 0, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
155 : }
156 :
157 : /* st->old_input_wb and st->old_wtda_wb must be updated each frame, or there are often some clicks during WB TBE <-> WB BWE switching */
158 0 : test();
159 0 : test();
160 0 : test();
161 0 : IF( ( NE_16( st_fx->extl, WB_BWE ) || ( EQ_16( st_fx->extl, WB_BWE ) && LE_32( st_fx->total_brate, ACELP_8k00 ) ) ) && !hSC_VBR->ppp_mode )
162 : {
163 0 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
164 :
165 0 : Copy( new_inp_resamp16k, &old_input[Sample_Delay_WB_BWE], L_FRAME16k );
166 0 : Copy( hBWE_FD->old_input_wb_fx, old_input, Sample_Delay_WB_BWE );
167 0 : Copy( new_inp_resamp16k + L_FRAME16k - Sample_Delay_WB_BWE, hBWE_FD->old_input_wb_fx, Sample_Delay_WB_BWE );
168 0 : Copy( old_input, hBWE_FD->L_old_wtda_swb_fx, L_FRAME16k );
169 : }
170 0 : return;
171 : }
172 :
173 :
174 : /*========================================================================*/
175 : /* FUNCTION : wb_pre_proc_ivas_fx() */
176 : /*------------------------------------------------------------------------*/
177 : /* PURPOSE : Resampling of input signal when input signal sample rate */
178 : /* is above 16kHz */
179 : /*------------------------------------------------------------------------*/
180 : /* INPUT ARGUMENTS : */
181 : /* Encoder_State *st_fx : Encoder State Structure */
182 : /* _ (Word16*) input : original input signal */
183 : /*------------------------------------------------------------------------*/
184 : /* INPUT/OUTPUT ARGUMENTS : */
185 : /* _None */
186 : /*------------------------------------------------------------------------*/
187 : /* OUTPUT ARGUMENTS : */
188 : /* _ (Word16*) new_wb_speech : original input signal at 16kHz Q-1 */
189 : /*------------------------------------------------------------------------*/
190 :
191 : /* st_fx->old_wtda_wb_fx */
192 : /*------------------------------------------------------------------------*/
193 : /* RETURN ARGUMENTS : */
194 : /* _ None */
195 : /*========================================================================*/
196 :
197 43815 : void wb_pre_proc_ivas_fx(
198 : Encoder_State *st_fx, /* i/o: encoder state structure */
199 : const Word16 last_element_mode, /* i : last element mode */
200 : const Word16 *new_inp_resamp16k, /* i : original input signal in Q-1 */
201 : Word16 *hb_speech /* o : HB target signal (6-8kHz) at 16kHz in Q-1 */
202 : )
203 : {
204 : Word16 Sample_Delay_WB_BWE;
205 : Word16 ramp_flag;
206 : Word16 decim_state1_fx[( 2 * ALLPASSSECTIONS_STEEP + 1 )], decim_state2_fx[( 2 * ALLPASSSECTIONS_STEEP + 1 )];
207 : Word16 old_input[NS2SA( 16000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + STEREO_DFT_OVL_16k + L_FRAME16k];
208 : Word16 *highband_new_speech;
209 : Word16 highband_old_speech[( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16];
210 : Word16 temp_buf[320];
211 : Word16 Q_wb_sp, i, max_wb;
212 43815 : Word16 fSwitchFromIO = 0;
213 : Word16 ppp_mode;
214 43815 : Word64 temp1 = 0;
215 43815 : Word64 temp2 = 0;
216 : Word32 tmp1, tmp2;
217 : Word16 norm;
218 43815 : move16();
219 43815 : move64();
220 43815 : move64();
221 :
222 :
223 43815 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
224 43815 : FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
225 :
226 43815 : IF( st_fx->Opt_SC_VBR )
227 : {
228 0 : ppp_mode = st_fx->hSC_VBR->ppp_mode;
229 0 : move16();
230 : }
231 : ELSE
232 : {
233 43815 : ppp_mode = 0;
234 43815 : move16();
235 : }
236 :
237 43815 : test();
238 43815 : test();
239 43815 : test();
240 43815 : test();
241 43815 : test();
242 43815 : test();
243 43815 : test();
244 43815 : test();
245 87625 : if ( ( EQ_32( st_fx->last_total_brate, ACELP_6k60 ) ) ||
246 87619 : ( EQ_32( st_fx->last_total_brate, ACELP_8k85 ) ) ||
247 87573 : ( EQ_32( st_fx->last_total_brate, ACELP_12k65 ) ) ||
248 87428 : ( EQ_32( st_fx->last_total_brate, ACELP_14k25 ) ) ||
249 87124 : ( EQ_32( st_fx->last_total_brate, ACELP_15k85 ) ) ||
250 86908 : ( EQ_32( st_fx->last_total_brate, ACELP_18k25 ) ) ||
251 86894 : ( EQ_32( st_fx->last_total_brate, ACELP_19k85 ) ) ||
252 86800 : ( EQ_32( st_fx->last_total_brate, ACELP_23k05 ) ) ||
253 43354 : ( EQ_32( st_fx->last_total_brate, ACELP_23k85 ) ) )
254 : {
255 541 : fSwitchFromIO = 1;
256 541 : move16();
257 : }
258 :
259 43815 : set16_fx( old_input, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + STEREO_DFT_OVL_16k + L_FRAME16k );
260 :
261 43815 : max_wb = 1;
262 43815 : move16();
263 14064615 : FOR( i = 0; i < L_FRAME16k; i++ )
264 : {
265 14020800 : max_wb = s_max( max_wb, abs_s( new_inp_resamp16k[i] ) );
266 : }
267 43815 : Q_wb_sp = norm_s( max_wb );
268 :
269 43815 : Copy_Scale_sig( new_inp_resamp16k, temp_buf, L_FRAME16k, -3 );
270 43815 : temp1 = W_mac_32_32( temp1, temp_buf[0], temp_buf[0] );
271 14020800 : FOR( i = 1; i < L_FRAME16k; i++ )
272 : {
273 13976985 : temp1 = W_mac_32_32( temp1, temp_buf[i], temp_buf[i] );
274 13976985 : temp2 = W_mac_32_32( temp2, temp_buf[i - 1], temp_buf[i] );
275 : }
276 43815 : norm = s_min( W_norm( temp1 ), W_norm( temp2 ) );
277 43815 : tmp1 = W_extract_h( W_shl( temp1, norm ) );
278 43815 : tmp2 = W_extract_h( W_shl( temp2, norm ) );
279 :
280 43815 : IF( LT_32( tmp2, Mpy_32_16_1( tmp1, 31129 /*0.95f*/ ) ) )
281 : {
282 12219 : Q_wb_sp = sub( Q_wb_sp, 3 ); /* leave 3 bit headroom */
283 : }
284 43815 : Copy_Scale_sig( new_inp_resamp16k, temp_buf, L_FRAME16k, Q_wb_sp );
285 43815 : Scale_sig( hBWE_TD->decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), Q_wb_sp );
286 43815 : Scale_sig( hBWE_TD->decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), Q_wb_sp );
287 : /* temp_buf, and the memory states are in Q_wb_sp */
288 :
289 43815 : test();
290 43815 : test();
291 43815 : IF( EQ_16( st_fx->extl, WB_BWE ) || EQ_16( st_fx->extl, WB_TBE ) || st_fx->igf )
292 : {
293 21399 : ramp_flag = 0;
294 21399 : move16();
295 :
296 21399 : test();
297 21399 : test();
298 21399 : test();
299 21399 : test();
300 21399 : IF( ( NE_16( st_fx->last_extl, WB_TBE ) && NE_16( st_fx->last_extl, WB_BWE ) && !st_fx->igf ) ||
301 : ( st_fx->igf && fSwitchFromIO ) )
302 : {
303 585 : ramp_flag = 1;
304 585 : move16();
305 : }
306 :
307 21399 : IF( !ppp_mode )
308 : {
309 : /* temp_buf is in Q_wb_sp
310 : hb_speech and the two decimator memories are in Q_wb_sp */
311 21399 : IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) )
312 : {
313 5027 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
314 :
315 5027 : IF( EQ_16( last_element_mode, IVAS_CPE_TD ) )
316 : {
317 1 : set16_fx( hBWE_TD->decim_state1_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
318 1 : set16_fx( hBWE_TD->decim_state2_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
319 1 : ramp_flag = 1;
320 1 : move16();
321 : }
322 :
323 : /*Get past signal*/
324 5027 : Copy_Scale_sig( hBWE_FD->L_old_wtda_swb_fx + L_FRAME16k - STEREO_DFT_OVL_16k, old_input, STEREO_DFT_OVL_16k, Q_wb_sp );
325 5027 : Copy_Scale_sig( hBWE_FD->old_input_wb_fx, old_input + STEREO_DFT_OVL_16k, Sample_Delay_WB_BWE, Q_wb_sp );
326 :
327 : /*Get new signal*/
328 5027 : Copy( temp_buf, &old_input[Sample_Delay_WB_BWE + STEREO_DFT_OVL_16k], L_FRAME16k );
329 :
330 : /*compute hb_speech on delayed input*/
331 5027 : flip_spectrum_and_decimby4_fx( old_input + Sample_Delay_WB_BWE, hb_speech, L_FRAME16k, hBWE_TD->decim_state1_fx, hBWE_TD->decim_state2_fx, ramp_flag );
332 :
333 : /*Update memory*/
334 5027 : Copy_Scale_sig( hb_speech, hBWE_TD->old_speech_wb_fx + ( L_SUBFR * 5 / 16 ), STEREO_DFT_OVL_16k / 4, -Q_wb_sp );
335 5027 : Copy( hb_speech + STEREO_DFT_OVL_16k / 4, hb_speech, ( L_FRAME16k - STEREO_DFT_OVL_16k ) / 4 );
336 :
337 : /*rest without memory update*/
338 5027 : Copy( hBWE_TD->decim_state1_fx, decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
339 5027 : Copy( hBWE_TD->decim_state2_fx, decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
340 :
341 5027 : flip_spectrum_and_decimby4_fx( old_input + Sample_Delay_WB_BWE + L_FRAME16k, hb_speech + ( L_FRAME16k - STEREO_DFT_OVL_16k ) / 4, STEREO_DFT_OVL_16k, decim_state1_fx, decim_state2_fx, 0 );
342 : }
343 16372 : ELSE IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) )
344 : {
345 1232 : Word16 l_recalc_16k = L_MEM_RECALC_16K + L_FILT16k + 1; /* Note: "+1" is used because L_FILT16k is not divisible by 4 */
346 1232 : move16();
347 1232 : Word16 l_recalc_4k = ( L_MEM_RECALC_16K + L_FILT16k + 1 ) / 4;
348 1232 : move16();
349 1232 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
350 :
351 1232 : IF( EQ_16( last_element_mode, IVAS_CPE_DFT ) )
352 : {
353 17 : set16_fx( hBWE_TD->decim_state1_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
354 17 : set16_fx( hBWE_TD->decim_state2_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
355 17 : ramp_flag = 1;
356 17 : move16();
357 : }
358 :
359 : /*Get past signal*/
360 1232 : Copy_Scale_sig( hBWE_FD->L_old_wtda_swb_fx + L_FRAME16k - l_recalc_16k, old_input, l_recalc_16k, Q_wb_sp );
361 1232 : Copy_Scale_sig( hBWE_FD->old_input_wb_fx, old_input + l_recalc_16k, Sample_Delay_WB_BWE, Q_wb_sp );
362 :
363 : /* Saturation added to prevent the crash. Needs further review */
364 1232 : old_input[Sample_Delay_WB_BWE] = shl_sat( hBWE_FD->mem_old_wtda_swb_fx, Q_wb_sp );
365 :
366 : /*Get new signal*/
367 1232 : Copy( temp_buf, old_input + Sample_Delay_WB_BWE + l_recalc_16k, L_FRAME16k );
368 :
369 : /*compute hb_speech on delayed input*/
370 1232 : flip_spectrum_and_decimby4_fx( old_input + Sample_Delay_WB_BWE, hb_speech, L_FRAME16k, hBWE_TD->decim_state1_fx, hBWE_TD->decim_state2_fx, ramp_flag );
371 :
372 : /*Update memory*/
373 1232 : Copy_Scale_sig( hb_speech, hBWE_TD->old_speech_wb_fx + ( ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 ) - l_recalc_4k, l_recalc_4k, -Q_wb_sp );
374 1232 : Copy( hb_speech + l_recalc_4k, hb_speech, ( L_FRAME16k / 4 ) - l_recalc_4k );
375 :
376 : /*rest without memory update*/
377 1232 : Copy( hBWE_TD->decim_state1_fx, decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
378 1232 : Copy( hBWE_TD->decim_state2_fx, decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
379 :
380 1232 : flip_spectrum_and_decimby4_fx( old_input + Sample_Delay_WB_BWE + L_FRAME16k, hb_speech + ( L_FRAME16k / 4 ) - l_recalc_4k, l_recalc_16k, decim_state1_fx, decim_state2_fx, 0 );
381 : }
382 15140 : ELSE IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
383 : {
384 15140 : IF( EQ_32( st_fx->input_Fs, 16000 ) )
385 : {
386 0 : flip_spectrum_and_decimby4_fx( temp_buf, hb_speech, L_FRAME16k, hBWE_TD->decim_state1_fx, hBWE_TD->decim_state2_fx, ramp_flag );
387 : }
388 : ELSE
389 : {
390 15140 : Word16 l_recalc_16k = L_FILT16k + 1; /* Note: "+1" is used because L_FILT16k is not divisible by 4 */
391 15140 : Word16 l_recalc_4k = ( L_FILT16k + 1 ) / 4;
392 :
393 15140 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
394 :
395 : /*Get past signal*/
396 15140 : Copy_Scale_sig( hBWE_FD->L_old_wtda_swb_fx + L_FRAME16k - l_recalc_16k, old_input, l_recalc_16k, Q_wb_sp );
397 15140 : Copy_Scale_sig( hBWE_FD->old_input_wb_fx, old_input + l_recalc_16k, Sample_Delay_WB_BWE, Q_wb_sp );
398 :
399 : /*Get new signal*/
400 15140 : Copy( temp_buf, old_input + Sample_Delay_WB_BWE + l_recalc_16k, L_FRAME16k );
401 :
402 : /*compute hb_speech on delayed input*/
403 15140 : flip_spectrum_and_decimby4_fx( old_input + Sample_Delay_WB_BWE, hb_speech, L_FRAME16k, hBWE_TD->decim_state1_fx, hBWE_TD->decim_state2_fx, ramp_flag );
404 :
405 : /* update hBWE_TD->old_speech_wb memory */
406 15140 : Copy_Scale_sig( hb_speech, hBWE_TD->old_speech_wb_fx + ( ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 ) - l_recalc_4k, l_recalc_4k, -Q_wb_sp );
407 15140 : Copy( hb_speech + l_recalc_4k, hb_speech, ( L_FRAME16k / 4 ) - l_recalc_4k );
408 :
409 : /*rest without memory update*/
410 15140 : Copy( hBWE_TD->decim_state1_fx, decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
411 15140 : Copy( hBWE_TD->decim_state2_fx, decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
412 :
413 15140 : flip_spectrum_and_decimby4_fx( old_input + Sample_Delay_WB_BWE + L_FRAME16k, hb_speech + ( L_FRAME16k / 4 ) - l_recalc_4k, l_recalc_16k, decim_state1_fx, decim_state2_fx, 0 );
414 : }
415 : }
416 : ELSE
417 : {
418 0 : flip_spectrum_and_decimby4_fx( temp_buf, hb_speech, L_FRAME16k, hBWE_TD->decim_state1_fx, hBWE_TD->decim_state2_fx, ramp_flag );
419 : }
420 :
421 : /* rescale the hb_speech and memories back to Q-1 to keep the downstream BWE coding unaffected */
422 21399 : Scale_sig( hb_speech, L_FRAME16k / 4, -Q_wb_sp );
423 21399 : Scale_sig( hBWE_TD->decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), -Q_wb_sp );
424 21399 : Scale_sig( hBWE_TD->decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), -Q_wb_sp );
425 :
426 21399 : IF( NE_16( st_fx->extl, WB_TBE ) )
427 : {
428 : /* Update the previous wideband speech buffer in case of a WB_BWE frame */
429 13719 : Sample_Delay_WB_BWE = ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16;
430 :
431 13719 : highband_new_speech = highband_old_speech + Sample_Delay_WB_BWE;
432 13719 : Copy( hb_speech, highband_new_speech, L_FRAME16k / 4 );
433 13719 : Copy( highband_old_speech + L_FRAME16k / 4, hBWE_TD->old_speech_wb_fx, Sample_Delay_WB_BWE );
434 : }
435 : }
436 : }
437 : ELSE
438 : {
439 22416 : set16_fx( hBWE_TD->decim_state1_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
440 22416 : set16_fx( hBWE_TD->decim_state2_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
441 22416 : set16_fx( hBWE_TD->old_speech_wb_fx, 0, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
442 : }
443 :
444 43815 : if ( hBWE_FD != NULL )
445 : {
446 43815 : hBWE_FD->mem_old_wtda_swb_fx = new_inp_resamp16k[L_FRAME16k - L_MEM_RECALC_16K - L_FILT16k - 1];
447 43815 : move16();
448 : }
449 :
450 : /* st->old_input_wb and st->old_wtda_wb must be updated each frame, or there are often some clicks during WB TBE <-> WB BWE switching */
451 43815 : test();
452 43815 : test();
453 43815 : test();
454 43815 : IF( ( NE_16( st_fx->extl, WB_BWE ) || ( EQ_16( st_fx->extl, WB_BWE ) && EQ_32( st_fx->total_brate, 0 ) ) ) && !ppp_mode )
455 : {
456 31385 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
457 :
458 31385 : Copy( new_inp_resamp16k, &old_input[Sample_Delay_WB_BWE], L_FRAME16k );
459 31385 : Copy( hBWE_FD->old_input_wb_fx, old_input, Sample_Delay_WB_BWE );
460 31385 : Copy( new_inp_resamp16k + L_FRAME16k - Sample_Delay_WB_BWE, hBWE_FD->old_input_wb_fx, Sample_Delay_WB_BWE );
461 31385 : test();
462 31385 : IF( NE_16( st_fx->extl, SWB_BWE ) && NE_16( st_fx->extl, FB_BWE ) )
463 : {
464 31385 : Copy( old_input, hBWE_FD->L_old_wtda_swb_fx, L_FRAME16k );
465 31385 : st_fx->Q_old_wtda = -1;
466 31385 : move16();
467 : }
468 : }
469 43815 : return;
470 : }
471 :
472 :
473 : /*========================================================================*/
474 : /* FUNCTION : swb_pre_proc_fx() */
475 : /*------------------------------------------------------------------------*/
476 : /* PURPOSE : Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) */
477 : /* SHB target signal for SWB TBE or SWB BWE coding */
478 : /* Common SWB TBE and SWB BWE pre-processing */
479 : /*------------------------------------------------------------------------*/
480 : /* INPUT ARGUMENTS : */
481 : /* Encoder_State *st_fx : Encoder State Structure Q0 */
482 : /* _ (Word16*) input_fx : original input signal Q0 */
483 : /*------------------------------------------------------------------------*/
484 : /* INPUT/OUTPUT ARGUMENTS : */
485 : /* _None */
486 : /*------------------------------------------------------------------------*/
487 : /* OUTPUT ARGUMENTS : */
488 : /* _ (Word16*) new_swb_speech_fx : original input signal at 16kHz Q0 */
489 : /* _ (Word16*) shb_speech_fx : original input signal at 16kHz Q0 */
490 : /*------------------------------------------------------------------------*/
491 :
492 : /* st_fx->old_input_fx */
493 : /* st_fx->old_wtda_shb_fx */
494 : /*------------------------------------------------------------------------*/
495 : /* RETURN ARGUMENTS : */
496 : /* _ None */
497 : /*========================================================================*/
498 :
499 3100 : void swb_pre_proc_fx(
500 : Encoder_State *st_fx, /* i/o: encoder state structure */
501 : const Word16 *input_fx, /* i : original input signal */
502 : Word16 *new_swb_speech_fx, /* o : original input signal at 32kHz */
503 : Word16 *shb_speech_fx, /* o : SHB target signal (6-14kHz) at 16kHz */
504 : Word16 *Q_shb_spch,
505 : Word32 realBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i : real CLDFB buffer for target synthesis */
506 : Word32 imagBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i : imag CLDFB buffer for target synthesis */
507 : const CLDFB_SCALE_FACTOR *cldfbScale /* i : scale data of real and imag CLDFB buffers */
508 : )
509 : {
510 : Word16 Sample_Delay_SWB_BWE, delay;
511 : Word16 inner_frame;
512 : UWord16 inner_Fs;
513 : Word16 old_input_fx[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k];
514 : Word16 spchTmp_fx[L_FRAME32k];
515 : Word16 i;
516 : Word16 startB, endB;
517 : Word16 j;
518 : Word32 *realBufferFlipped[CLDFB_NO_COL_MAX];
519 : Word32 *imagBufferFlipped[CLDFB_NO_COL_MAX];
520 : Word32 realBufferTmp[CLDFB_NO_COL_MAX][20];
521 : Word32 imagBufferTmp[CLDFB_NO_COL_MAX][20];
522 : Word32 cldfbWorkBuffer[256];
523 : Word16 ts, nB, uB;
524 : /* Highband energy computation using CLDFB */
525 : Word32 CldfbHB_ener;
526 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
527 3100 : Flag Overflow = 0;
528 3100 : move16();
529 : #endif
530 : Word16 realQ_neg1, imagQ_neg1, exp, Cldfbtemp1;
531 3100 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
532 3100 : FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
533 :
534 52700 : FOR( j = 0; j < CLDFB_NO_COL_MAX; j++ )
535 : {
536 49600 : set32_fx( realBufferTmp[j], 0, 20 );
537 49600 : set32_fx( imagBufferTmp[j], 0, 20 );
538 49600 : realBufferFlipped[j] = realBufferTmp[j];
539 49600 : imagBufferFlipped[j] = imagBufferTmp[j];
540 : }
541 :
542 3100 : set16_fx( old_input_fx, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k );
543 :
544 3100 : IF( EQ_32( st_fx->input_Fs, 32000 ) )
545 : {
546 1050 : Copy( input_fx, new_swb_speech_fx, L_FRAME32k ); /*Q0 */
547 1050 : test();
548 1050 : test();
549 1050 : IF( NE_16( st_fx->last_extl, SWB_BWE ) && NE_16( st_fx->last_extl, FB_BWE ) && NE_16( st_fx->extl, SWB_BWE_HIGHRATE ) )
550 : {
551 1042 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
552 1042 : Copy( hBWE_FD->old_fdbwe_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], L_FRAME32k );
553 1042 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
554 1042 : Copy( hBWE_FD->old_fdbwe_speech_fx + L_FRAME32k - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
555 :
556 1042 : IF( sub( st_fx->extl, WB_BWE ) != 0 )
557 : {
558 1042 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, L_FRAME32k );
559 : }
560 : }
561 :
562 1050 : test();
563 1050 : IF( NE_16( st_fx->extl, SWB_BWE ) && NE_16( st_fx->extl, FB_BWE ) )
564 : {
565 1042 : Copy( input_fx, hBWE_FD->old_fdbwe_speech_fx, L_FRAME32k );
566 : }
567 : }
568 : ELSE /* 48 kHz */
569 : {
570 : /* 48kHz sampled processing needs review of FD2 memory handling/Q-factors */
571 2050 : IF( EQ_16( st_fx->codec_mode, MODE1 ) )
572 : {
573 1066 : test();
574 1066 : test();
575 1066 : IF( NE_16( st_fx->extl, SWB_BWE ) && NE_16( st_fx->extl, FB_BWE ) && EQ_16( st_fx->core, ACELP_CORE ) )
576 : {
577 : /* move the resampling out of the TDBWE path as new_swb_speech is not needed for TDBWE. */
578 674 : Copy( input_fx, hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k );
579 : }
580 : ELSE
581 : {
582 392 : test();
583 392 : IF( NE_16( st_fx->last_extl, SWB_BWE ) && NE_16( st_fx->last_extl, FB_BWE ) )
584 : {
585 : /* resample 48 kHz to 32kHz */
586 : #ifdef NONBE_1244_FIX_SWB_BWE_MEMORY
587 392 : IF( ( st_fx->last_bwidth == FB && st_fx->element_mode == EVS_MONO ) || ( EQ_16( st_fx->bwidth, FB ) && st_fx->element_mode > EVS_MONO ) ) // note: once EVS i CR fixed, the condition will simplify to "if ( st->bwidth == FB )" only
588 : #else
589 : IF( EQ_16( st_fx->last_bwidth, FB ) )
590 : #endif
591 : {
592 0 : inner_frame = L_FRAME48k;
593 0 : inner_Fs = 48000;
594 0 : Copy( hBWE_FD->old_fdbwe_speech_fx, new_swb_speech_fx, L_FRAME48k );
595 : }
596 : ELSE
597 : {
598 392 : inner_frame = L_FRAME32k;
599 392 : inner_Fs = 32000;
600 392 : decimate_2_over_3_allpass_fx( hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k, new_swb_speech_fx, hBWE_TD->dec_2_over_3_mem_fx, allpass_poles_3_ov_2,
601 392 : decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
602 : }
603 392 : move16();
604 392 : move16();
605 :
606 392 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
607 392 : Copy( new_swb_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], inner_frame );
608 392 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
609 392 : Copy( new_swb_speech_fx + inner_frame - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
610 392 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, inner_frame );
611 392 : st_fx->Q_old_wtda = 0;
612 392 : move16();
613 : }
614 : /* resample 48 kHz to 32kHz */
615 392 : IF( EQ_16( st_fx->bwidth, FB ) )
616 : {
617 0 : Copy( input_fx, new_swb_speech_fx, L_FRAME48k );
618 : }
619 : ELSE
620 : {
621 392 : decimate_2_over_3_allpass_fx( input_fx, L_FRAME48k, new_swb_speech_fx, hBWE_TD->dec_2_over_3_mem_fx, allpass_poles_3_ov_2,
622 392 : decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
623 : }
624 : }
625 : }
626 : ELSE
627 : {
628 : /* resample 48 kHz to 32kHz */
629 984 : IF( EQ_16( st_fx->bwidth, FB ) )
630 : {
631 0 : Copy( input_fx, new_swb_speech_fx, L_FRAME48k );
632 : }
633 : ELSE
634 : {
635 984 : decimate_2_over_3_allpass_fx( input_fx, L_FRAME48k, new_swb_speech_fx, hBWE_TD->dec_2_over_3_mem_fx, allpass_poles_3_ov_2,
636 984 : decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
637 : }
638 : }
639 : }
640 :
641 3100 : test();
642 3100 : test();
643 3100 : test();
644 3100 : test();
645 3100 : test();
646 3100 : IF( ( st_fx->core == ACELP_CORE && NE_16( st_fx->extl, SWB_BWE_HIGHRATE ) && NE_16( st_fx->extl, FB_BWE_HIGHRATE ) ) || ( ( EQ_32( st_fx->total_brate, 9600 ) || st_fx->rf_mode != 0 ) && EQ_16( st_fx->bwidth, SWB ) ) )
647 : {
648 1361 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
649 : {
650 749 : startB = 34;
651 749 : move16();
652 749 : endB = 14;
653 749 : move16();
654 12733 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
655 : {
656 11984 : uB = 0;
657 11984 : move16();
658 251664 : FOR( nB = startB; nB > endB; nB-- )
659 : {
660 239680 : realBufferFlipped[ts][uB] = realBuffer[ts][nB];
661 239680 : move32();
662 239680 : if ( s_and( ts, 1 ) != 0 )
663 : {
664 119840 : realBufferFlipped[ts][uB] = L_negate( realBufferFlipped[ts][uB] );
665 119840 : move32();
666 : }
667 239680 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
668 239680 : move32();
669 239680 : if ( s_and( ts, 1 ) == 0 )
670 : {
671 119840 : imagBufferFlipped[ts][uB] = L_negate( imagBufferFlipped[ts][uB] );
672 119840 : move32();
673 : }
674 239680 : uB = add( uB, 1 ); /* uB ++ */
675 : }
676 : }
677 : }
678 : ELSE
679 : {
680 612 : startB = 39;
681 612 : move16();
682 612 : endB = 19;
683 612 : move16();
684 10404 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
685 : {
686 9792 : uB = 0;
687 9792 : move16();
688 205632 : FOR( nB = startB; nB > endB; nB-- )
689 : {
690 195840 : realBufferFlipped[ts][uB] = L_negate( realBuffer[ts][nB] );
691 195840 : move32();
692 195840 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
693 195840 : move32();
694 195840 : uB = add( uB, 1 ); /* uB ++ */
695 : }
696 : }
697 : }
698 :
699 : /* Highband energy computation for gain shape control in case of bandwidth switching fix*/
700 : {
701 1361 : CldfbHB_ener = 0;
702 1361 : move32();
703 14971 : FOR( nB = 0; nB < 10; nB++ )
704 : {
705 231370 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
706 : {
707 217760 : realQ_neg1 = extract_l( L_shr( realBufferFlipped[ts][nB], 31 - ( 15 + cldfbScale->hb_scale ) + 1 ) );
708 217760 : imagQ_neg1 = extract_l( L_shr( imagBufferFlipped[ts][nB], 31 - ( 15 + cldfbScale->hb_scale ) + 1 ) ); /* Q(-1), headroom needed */
709 :
710 217760 : CldfbHB_ener = L_mac0_o( CldfbHB_ener, realQ_neg1, realQ_neg1, &Overflow );
711 217760 : CldfbHB_ener = L_mac0_o( CldfbHB_ener, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
712 : }
713 : }
714 :
715 1361 : exp = norm_l( CldfbHB_ener );
716 1361 : CldfbHB_ener = L_shl( CldfbHB_ener, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
717 1361 : Cldfbtemp1 = ( Log2_norm_lc( CldfbHB_ener ) );
718 1361 : Cldfbtemp1 = add( shr( Cldfbtemp1, 6 ), shl( sub( 30, sub( exp, 2 ) ), 9 ) ); /* Log2_norm_lc(CldfbHB_ener) = 2^15*(log2(CldfbHB_ener/2^30)) = 2^15*(log2(CldfbHB_fl*(2^-2)*2^exp/2^30)) = 2^15*(log2(CldfbHB_fl) + exp-2-30) => 2^(-6)*l2nc + 2^9(20-(exp-2)) = 2^9*log2(CldfbHB_fl), Q9 */
719 1361 : CldfbHB_ener = L_mult( sub( Cldfbtemp1, 1741 /*3.401 Q9*/ ), 3495 ); /* 3495 = Q19 log10(2)*0.1/log10(32768), Q = 19+9+1 = 29 */
720 1361 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_ener, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
721 : }
722 1361 : cldfbSynthesis_fx( st_fx->cldfbSynTd, realBufferFlipped, imagBufferFlipped, cldfbScale, shb_speech_fx, 0, CLDFB_NO_COL_MAX, cldfbWorkBuffer );
723 1361 : *Q_shb_spch = 0; /*shb_speech_fx : Q0*/
724 1361 : move16();
725 :
726 1361 : test();
727 1361 : test();
728 1361 : IF( NE_16( st_fx->extl, WB_TBE ) && NE_16( st_fx->extl, SWB_TBE ) && NE_16( st_fx->extl, FB_TBE ) )
729 : {
730 : /* Update the previous superwideband speech buffer in case of a SWB_BWE frame - this code is in swb_tbe_enc */
731 8 : delay = L_LOOK_16k + L_SUBFR16k;
732 8 : Copy( shb_speech_fx + L_FRAME16k - delay, hBWE_TD->old_speech_shb_fx, delay );
733 : }
734 : }
735 : ELSE
736 : {
737 1739 : test();
738 1739 : IF( EQ_16( st_fx->bwidth, FB ) || EQ_16( st_fx->core, ACELP_CORE ) )
739 : {
740 674 : set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
741 674 : set16_fx( shb_speech_fx, 0, L_FRAME16k ); /* shb_speech for FB/SWB BWE_HIGHRATE is not used at 64kbps */
742 : }
743 : ELSE
744 : {
745 : /* flip the spectrm */
746 1065 : Copy( new_swb_speech_fx, spchTmp_fx, L_FRAME32k );
747 :
748 341865 : FOR( i = 0; i < L_FRAME32k; i = i + 2 )
749 : {
750 340800 : spchTmp_fx[i] = negate( spchTmp_fx[i] );
751 340800 : move16();
752 : }
753 :
754 1065 : Decimate_allpass_steep_fx( spchTmp_fx, hBWE_TD->state_ana_filt_shb_fx, L_FRAME32k, shb_speech_fx );
755 1065 : Copy( shb_speech_fx + L_FRAME16k - ( L_LOOK_16k + L_SUBFR16k ), hBWE_TD->old_speech_shb_fx, L_LOOK_16k + L_SUBFR16k );
756 : }
757 :
758 : /* Reset CLDFB synthesis buffer */
759 1739 : set16_fx( st_fx->cldfbSynTd->FilterStates, 0, st_fx->cldfbSynTd->p_filter_length + st_fx->cldfbSynTd->no_channels * st_fx->cldfbSynTd->no_col );
760 : }
761 3100 : IF( EQ_16( st_fx->last_extl, -1 ) )
762 : {
763 429 : delay = NS2SA_FX2( st_fx->input_Fs, DELAY_FIR_RESAMPL_NS );
764 19164 : FOR( i = 0; i < delay; i++ )
765 : {
766 18735 : shb_speech_fx[i] = mult_r( mult_r( i, 983 /*0.03f Q15*/ ), shb_speech_fx[2 * delay - 1 - i] );
767 18735 : move16();
768 : }
769 : }
770 :
771 3100 : return;
772 : }
773 :
774 : /*-------------------------------------------------------------------*
775 : * swb_pre_proc()
776 : *
777 : * - Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB target signal
778 : * for SWB TBE or SWB BWE coding
779 : * - Common SWB TBE and SWB BWE pre-processing
780 : *-------------------------------------------------------------------*/
781 :
782 : /*full implementation pending*/
783 396042 : void swb_pre_proc_ivas_fx(
784 : Encoder_State *st, /* i/o: encoder state structure */
785 : Word16 *new_swb_speech, /* o : original input signal at 32kHz - st->q_inp */
786 : Word32 *new_swb_speech_fx, /* o : original input signal at 32kHz - Q - q_reImBuffer */
787 : Word16 *shb_speech, /* o : SHB target signal (6-14kHz) at 16kHz- Q(Q_shb_spch) */
788 : Word16 *Q_shb_spch,
789 : Word32 realBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i : real buffer Q - q_reImbuffer */
790 : Word32 imagBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i : imag buffer Q - q_reImbuffer */
791 : Word16 q_reImBuffer, /* i : scale data of real and imag CLDFB buffers */
792 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structure */
793 : )
794 : {
795 : Word16 Sample_Delay_SWB_BWE, inner_frame, delay;
796 : TD_BWE_ENC_HANDLE hBWE_TD;
797 : FD_BWE_ENC_HANDLE hBWE_FD;
798 : Word32 inner_Fs, input_Fs;
799 : Word16 old_input_fx[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k]; // fixed counterpart
800 : Word32 old_input_fx_32[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k]; // fixed counterpart
801 : Word16 spchTmp[L_FRAME32k], spchTmp2[L_FRAME32k];
802 : Word16 i, j, L_resamp;
803 : Word16 startB, endB;
804 : Word32 *realBufferFlipped[CLDFB_NO_COL_MAX];
805 : Word32 *imagBufferFlipped[CLDFB_NO_COL_MAX];
806 : Word32 realBufferTmp[CLDFB_NO_COL_MAX][20];
807 : Word32 imagBufferTmp[CLDFB_NO_COL_MAX][20];
808 : Word32 shb_speech_fx_32[L_FRAME16k];
809 : Word16 ts, nB, uB;
810 : Word32 v, t, regression;
811 : Word16 t_16;
812 : const Word32 *thr, *regV;
813 : Word32 lbEner;
814 396042 : Word16 exp_lbEner = 0;
815 : Word16 Sample_Delay_SWB_BWE32k, lMemRecalc32k, dft_ovl32k;
816 396042 : Word32 one_by_50_Q31 = 42949673;
817 :
818 396042 : lMemRecalc32k = NS2SA( 32000, L_MEM_RECALC_NS );
819 396042 : move16(); // exp_lbEner
820 396042 : move16(); // lMemRecalc32k
821 396042 : move32(); // one_by_50_Q31
822 :
823 : /* initialization */
824 396042 : hBWE_TD = st->hBWE_TD;
825 396042 : hBWE_FD = st->hBWE_FD;
826 396042 : input_Fs = st->input_Fs;
827 :
828 6732714 : FOR( j = 0; j < CLDFB_NO_COL_MAX; j++ )
829 : {
830 6336672 : set32_fx( realBufferTmp[j], 0, 20 );
831 6336672 : set32_fx( imagBufferTmp[j], 0, 20 );
832 6336672 : realBufferFlipped[j] = realBufferTmp[j];
833 6336672 : imagBufferFlipped[j] = imagBufferTmp[j];
834 : }
835 :
836 396042 : set16_fx( old_input_fx, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k );
837 :
838 396042 : IF( EQ_32( st->input_Fs, 32000 ) )
839 : {
840 57471 : IF( st->element_mode > EVS_MONO )
841 : {
842 57471 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS );
843 57471 : IF( EQ_16( st->L_frame, L_FRAME16k ) )
844 : {
845 32465 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS );
846 : }
847 :
848 57471 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
849 57471 : Copy( st->input_fx - L_FRAME32k, hBWE_FD->old_fdbwe_speech_fx, L_FRAME32k );
850 :
851 57471 : test();
852 57471 : test();
853 57471 : scale_sig( hBWE_FD->L_old_wtda_swb_fx, L_FRAME48k, sub( st->q_inp, st->Q_old_wtda ) ); // st->Q_old_wtda -> st->q_inp
854 57471 : st->Q_old_wtda = st->q_inp;
855 57471 : move16();
856 57471 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) && GE_16( st->bwidth, SWB ) )
857 : {
858 3975 : Copy( st->input_fx - hCPE->hStereoTCA->lMemRecalc, hBWE_FD->L_old_wtda_swb_fx + L_FRAME32k - sub( hCPE->hStereoTCA->lMemRecalc, Sample_Delay_SWB_BWE ), sub( hCPE->hStereoTCA->lMemRecalc, Sample_Delay_SWB_BWE ) );
859 : }
860 53496 : ELSE IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && GE_16( st->bwidth, SWB ) )
861 : {
862 26882 : Copy( st->input_fx - hCPE->hStereoDft->dft_ovl, hBWE_FD->L_old_wtda_swb_fx + L_FRAME32k - sub( hCPE->hStereoDft->dft_ovl, Sample_Delay_SWB_BWE ), sub( hCPE->hStereoDft->dft_ovl, Sample_Delay_SWB_BWE ) );
863 : }
864 : }
865 :
866 57471 : Copy( st->input_fx, new_swb_speech, L_FRAME32k );
867 :
868 57471 : test();
869 57471 : test();
870 57471 : IF( NE_16( st->last_extl, SWB_BWE ) && NE_16( st->last_extl, FB_BWE ) && NE_16( st->extl, SWB_BWE_HIGHRATE ) )
871 : {
872 56666 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
873 56666 : test();
874 56666 : IF( st->element_mode > EVS_MONO && EQ_16( st->L_frame, L_FRAME16k ) )
875 : {
876 32337 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
877 : }
878 56666 : IF( st->element_mode > EVS_MONO )
879 : {
880 56666 : Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA( 32000, DELAY_FIR_RESAMPL_NS ) );
881 : }
882 :
883 56666 : Copy( hBWE_FD->old_fdbwe_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], L_FRAME32k );
884 :
885 56666 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
886 56666 : Copy( hBWE_FD->old_fdbwe_speech_fx + L_FRAME32k - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
887 56666 : IF( NE_16( st->extl, WB_BWE ) )
888 : {
889 56630 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, L_FRAME32k );
890 56630 : st->Q_old_wtda = st->q_inp;
891 56630 : move16();
892 : }
893 : }
894 :
895 57471 : test();
896 57471 : IF( NE_16( st->extl, SWB_BWE ) && NE_16( st->extl, FB_BWE ) )
897 : {
898 56658 : Copy( st->input_fx, hBWE_FD->old_fdbwe_speech_fx, L_FRAME32k );
899 : }
900 : }
901 : ELSE /* 48 kHz */
902 : {
903 :
904 338571 : Sample_Delay_SWB_BWE32k = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS );
905 338571 : move16();
906 338571 : Sample_Delay_SWB_BWE = NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS );
907 338571 : move16();
908 338571 : IF( EQ_16( st->L_frame, L_FRAME16k ) )
909 : {
910 119109 : Sample_Delay_SWB_BWE32k = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS );
911 119109 : move16();
912 119109 : Sample_Delay_SWB_BWE = NS2SA( 48000, DELAY_FD_BWE_ENC_16k_NS );
913 119109 : move16();
914 : }
915 :
916 338571 : dft_ovl32k = 0;
917 338571 : move16();
918 338571 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
919 : {
920 20888 : dft_ovl32k = (Word16) ( STEREO_DFT_OVL_MAX * 32000 / 48000 );
921 20888 : move16();
922 : }
923 :
924 338571 : IF( EQ_16( st->codec_mode, MODE1 ) )
925 : {
926 338571 : IF( st->element_mode > EVS_MONO )
927 : {
928 :
929 338571 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
930 : {
931 : }
932 336541 : ELSE IF( EQ_16( st->bwidth, FB ) )
933 : {
934 210237 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
935 : }
936 :
937 338571 : Copy( st->input_fx - L_FRAME48k, hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k );
938 :
939 338571 : test();
940 338571 : test();
941 338571 : scale_sig( hBWE_FD->L_old_wtda_swb_fx, L_FRAME48k, sub( st->q_inp, st->Q_old_wtda ) ); // st->Q_old_wtda -> st->q_inp
942 338571 : st->Q_old_wtda = st->q_inp;
943 338571 : move16();
944 :
945 338571 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) && GE_16( st->bwidth, SWB ) )
946 : {
947 1912 : IF( EQ_16( st->bwidth, SWB ) )
948 : {
949 : /* buffers hBWE_FD->old_input[] and hBWE_FD->old_wtda_swb[] need to be at 32 kHz (inner) sampling rate */
950 :
951 439 : decimate_2_over_3_allpass_fx( st->input_fx - hCPE->hStereoTCA->lMemRecalc, hCPE->hStereoTCA->lMemRecalc, spchTmp, hBWE_TD->dec_2_over_3_mem_fx,
952 439 : allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
953 :
954 439 : Copy( spchTmp, hBWE_FD->L_old_wtda_swb_fx + L_FRAME32k - sub( lMemRecalc32k, Sample_Delay_SWB_BWE32k ), sub( lMemRecalc32k, Sample_Delay_SWB_BWE32k ) );
955 439 : Copy( spchTmp + lMemRecalc32k - Sample_Delay_SWB_BWE32k, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE32k );
956 : }
957 : ELSE /* FB_BWE */
958 : {
959 1473 : Copy( st->input_fx - hCPE->hStereoTCA->lMemRecalc, hBWE_FD->L_old_wtda_swb_fx + L_FRAME48k - sub( hCPE->hStereoTCA->lMemRecalc, Sample_Delay_SWB_BWE ), sub( hCPE->hStereoTCA->lMemRecalc, Sample_Delay_SWB_BWE ) );
960 1473 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
961 : }
962 : }
963 336659 : ELSE IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && GE_16( st->bwidth, SWB ) )
964 : {
965 19874 : IF( EQ_16( st->bwidth, SWB ) )
966 : {
967 10046 : lerp( st->input_fx - hCPE->hStereoDft->dft_ovl, spchTmp, dft_ovl32k - Sample_Delay_SWB_BWE32k, hCPE->hStereoDft->dft_ovl - Sample_Delay_SWB_BWE );
968 :
969 10046 : Copy( spchTmp, hBWE_FD->L_old_wtda_swb_fx + L_FRAME32k - ( dft_ovl32k - Sample_Delay_SWB_BWE32k ), dft_ovl32k - Sample_Delay_SWB_BWE32k );
970 : }
971 : ELSE
972 : {
973 9828 : Copy( st->input_fx - hCPE->hStereoDft->dft_ovl, hBWE_FD->L_old_wtda_swb_fx + L_FRAME48k - ( hCPE->hStereoDft->dft_ovl - Sample_Delay_SWB_BWE ), hCPE->hStereoDft->dft_ovl - Sample_Delay_SWB_BWE );
974 : }
975 : }
976 : }
977 :
978 338571 : test();
979 338571 : test();
980 338571 : test();
981 338571 : test();
982 338571 : IF( ( NE_16( st->extl, SWB_BWE ) && NE_16( st->extl, FB_BWE ) && ( st->core == ACELP_CORE ) ) || ( EQ_16( st->element_mode, IVAS_CPE_DFT ) && ( st->core != ACELP_CORE ) ) /*resampling not needed for MDCT cores*/ )
983 : {
984 : /* move the resampling out of the TDBWE path as new_swb_speech is not needed for TDBWE. */
985 114941 : Copy( st->input_fx, hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k );
986 : }
987 : ELSE
988 : {
989 223630 : test();
990 223630 : IF( NE_16( st->last_extl, SWB_BWE ) && NE_16( st->last_extl, FB_BWE ) )
991 : {
992 : /* resample 48 kHz to 32kHz */
993 : #ifdef NONBE_1244_FIX_SWB_BWE_MEMORY
994 213717 : IF( ( st->last_bwidth == FB && st->element_mode == EVS_MONO ) || ( EQ_16( st->bwidth, FB ) && st->element_mode > EVS_MONO ) ) // note: once EVS i CR fixed, the condition will simplify to "if ( st->bwidth == FB )" only
995 : #else
996 : IF( EQ_16( st->last_bwidth, FB ) )
997 : #endif
998 : {
999 157925 : inner_frame = L_FRAME48k;
1000 157925 : move16();
1001 157925 : inner_Fs = 48000;
1002 157925 : move32();
1003 157925 : Copy( hBWE_FD->old_fdbwe_speech_fx, new_swb_speech, L_FRAME48k );
1004 : }
1005 : ELSE
1006 : {
1007 55792 : inner_frame = L_FRAME32k;
1008 55792 : move16();
1009 55792 : inner_Fs = 32000;
1010 55792 : move32();
1011 :
1012 55792 : IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1013 : {
1014 55580 : decimate_2_over_3_allpass_fx( hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k, new_swb_speech, hBWE_TD->dec_2_over_3_mem_fx, allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
1015 : }
1016 : ELSE
1017 : {
1018 212 : lerp( hBWE_FD->old_fdbwe_speech_fx, new_swb_speech, inner_frame, L_FRAME48k );
1019 : }
1020 :
1021 55792 : test();
1022 55792 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && st->idchan == 0 )
1023 : {
1024 59572 : FOR( i = 0; i < STEREO_DFT_OVL_32k; i++ )
1025 : {
1026 59360 : hCPE->hStereoDft->output_mem_dmx_32k_fx[i] = L_shr( Mpy_32_16_1( hCPE->hStereoDft->win_32k_fx[STEREO_DFT_OVL_32k - 1 - i], new_swb_speech[( ( inner_frame + i ) - STEREO_DFT_OVL_32k )] ), 1 ); // Q15
1027 59360 : move32();
1028 : }
1029 : }
1030 : }
1031 :
1032 213717 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
1033 213717 : move16();
1034 213717 : test();
1035 213717 : IF( st->element_mode > EVS_MONO && EQ_16( st->L_frame, L_FRAME16k ) )
1036 : {
1037 60950 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
1038 60950 : move16();
1039 : }
1040 213717 : IF( st->element_mode > EVS_MONO )
1041 : {
1042 213717 : Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) );
1043 : }
1044 :
1045 213717 : Copy( new_swb_speech, &old_input_fx[Sample_Delay_SWB_BWE], inner_frame );
1046 213717 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
1047 213717 : Copy( new_swb_speech + inner_frame - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
1048 213717 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, inner_frame );
1049 213717 : st->Q_old_wtda = st->q_inp;
1050 213717 : move16();
1051 : }
1052 :
1053 : /* resample 48 kHz to 32kHz */
1054 223630 : IF( EQ_16( st->bwidth, FB ) )
1055 : {
1056 162521 : Copy( st->input_fx, new_swb_speech, L_FRAME48k );
1057 : }
1058 : ELSE
1059 : {
1060 61109 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
1061 : {
1062 : Word16 dec_2_over_3_mem_tmp[L_FILT_2OVER3], dec_2_over_3_mem_lp_tmp[L_FILT_2OVER3_LP];
1063 :
1064 16 : decimate_2_over_3_allpass_fx( st->input_fx, L_FRAME48k - hCPE->hStereoTCA->lMemRecalc, new_swb_speech, hBWE_TD->dec_2_over_3_mem_fx, allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
1065 :
1066 16 : Copy( hBWE_TD->dec_2_over_3_mem_fx, dec_2_over_3_mem_tmp, L_FILT_2OVER3 );
1067 16 : Copy( hBWE_TD->dec_2_over_3_mem_lp_fx, dec_2_over_3_mem_lp_tmp, L_FILT_2OVER3_LP );
1068 :
1069 16 : decimate_2_over_3_allpass_fx( st->input_fx + L_FRAME48k - hCPE->hStereoTCA->lMemRecalc, hCPE->hStereoTCA->lMemRecalc, new_swb_speech + L_FRAME32k - lMemRecalc32k, dec_2_over_3_mem_tmp, allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, dec_2_over_3_mem_lp_tmp );
1070 : }
1071 61093 : ELSE IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1072 : {
1073 59602 : decimate_2_over_3_allpass_fx( st->input_fx, L_FRAME48k, new_swb_speech, hBWE_TD->dec_2_over_3_mem_fx, allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
1074 : }
1075 : ELSE /* IVAS_CPE_DFT */
1076 : {
1077 : Word16 out_start_ind, out_end_ind;
1078 1491 : stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, new_swb_speech_fx, &out_start_ind, &out_end_ind, st->idchan, input_Fs, 32000, 0, NULL );
1079 1491 : Copy_Scale_sig32_16( new_swb_speech_fx - STEREO_DFT_OVL_MAX, new_swb_speech - STEREO_DFT_OVL_MAX, L_FRAME48k + STEREO_DFT_OVL_MAX, add( st->q_inp, add( q_reImBuffer, 1 ) ) ); // st->q_inp
1080 :
1081 1491 : Copy( new_swb_speech - Sample_Delay_SWB_BWE32k, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE32k ); // st->q_inp
1082 : }
1083 : }
1084 : }
1085 : }
1086 : ELSE
1087 : {
1088 : /* resample 48 kHz to 32kHz */
1089 0 : IF( EQ_16( st->bwidth, FB ) )
1090 : {
1091 0 : Copy( st->input_fx, new_swb_speech, L_FRAME48k );
1092 : }
1093 : ELSE
1094 : {
1095 0 : decimate_2_over_3_allpass_fx( st->input_fx, L_FRAME48k, new_swb_speech, hBWE_TD->dec_2_over_3_mem_fx, allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
1096 : }
1097 : }
1098 : }
1099 :
1100 396042 : test();
1101 396042 : test();
1102 396042 : test();
1103 396042 : test();
1104 396042 : test();
1105 396042 : test();
1106 396042 : IF( ( ( st->core == ACELP_CORE ) && NE_16( st->extl, SWB_BWE_HIGHRATE ) && NE_16( st->extl, FB_BWE_HIGHRATE ) ) ||
1107 : ( ( EQ_32( st->total_brate, ACELP_9k60 ) || st->rf_mode ) && EQ_16( st->bwidth, SWB ) && ( st->element_mode == EVS_MONO ) ) )
1108 157440 : {
1109 157440 : Word32 CldfbHB_fx = 0; // fixed counterpart
1110 157440 : Word16 CldfbHB_fx_e = 0; // fixed counterpart
1111 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1112 157440 : Flag Overflow = 0;
1113 157440 : move16();
1114 : #endif
1115 157440 : Word16 realQ_neg1, imagQ_neg1, exp = 0, Cldfbtemp1;
1116 157440 : move32();
1117 157440 : move16();
1118 157440 : move16();
1119 :
1120 157440 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1121 : {
1122 : Word16 out_start_ind, out_end_ind;
1123 : Word16 exp_icbweRefEner_fx;
1124 35803 : Copy_Scale_sig_16_32_DEPREC( (const Word16 *) &old_input_fx[0], &old_input_fx_32[0], NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k, Q15 ); // Q15
1125 35803 : CldfbHB_fx = stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, old_input_fx_32 + STEREO_DFT_OVL_16k, &out_start_ind, &out_end_ind, st->idchan, input_Fs, 16000, st->L_frame, &CldfbHB_fx_e );
1126 35803 : Copy_Scale_sig_32_16( (const Word32 *) &old_input_fx_32[0], &old_input_fx[0], NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k, -Q15 ); // Q0
1127 :
1128 : /* delay corresponding to CLDFB delay */
1129 35803 : Copy( old_input_fx + STEREO_DFT_OVL_16k - 20, shb_speech, L_FRAME16k );
1130 35803 : Copy( old_input_fx, hBWE_TD->old_speech_shb_fx + L_LOOK_16k + L_SUBFR16k - ( STEREO_DFT_OVL_16k - 20 ), STEREO_DFT_OVL_16k - 20 );
1131 35803 : Copy( old_input_fx, hCPE->hStereoICBWE->mem_shb_speech_ref_fx, STEREO_DFT_OVL_16k - 20 ); // Q0
1132 35803 : hCPE->hStereoICBWE->mem_shb_speech_ref_e = 15;
1133 35803 : move16();
1134 :
1135 35803 : IF( LE_32( CldfbHB_fx, 0 ) )
1136 : {
1137 1090 : CldfbHB_fx = EPSILON_FX;
1138 1090 : move32();
1139 : }
1140 35803 : CldfbHB_fx = L_shl( CldfbHB_fx, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
1141 35803 : Cldfbtemp1 = Log2_norm_lc( CldfbHB_fx ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1142 35803 : Cldfbtemp1 = sub( shr( Cldfbtemp1, 6 ), shl( add( sub( Q31 - Q30, CldfbHB_fx_e ), exp ), 9 ) );
1143 35803 : CldfbHB_fx = L_mult( Cldfbtemp1, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1144 35803 : CldfbHB_fx = Mpy_32_16_1( L_sub( CldfbHB_fx, 1 << Q25 ), 11611 /* 0.1 * 0.221462 in Q19 */ );
1145 : /* Resulting Q = Q25 + Q19 - Q15 = Q29 */
1146 35803 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_fx, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
1147 35803 : move16();
1148 :
1149 35803 : exp_lbEner = hCPE->hStereoDft->lbEner_fx_e;
1150 35803 : move32();
1151 35803 : lbEner = Mpy_32_16_1( Sqrt32( hCPE->hStereoDft->lbEner_fx, &exp_lbEner ), 1638 /* 0.05 in Q15 */ );
1152 35803 : exp_icbweRefEner_fx = hCPE->hStereoDft->icbweRefEner_fx_e;
1153 35803 : move16();
1154 35803 : hCPE->hStereoICBWE->icbweRefEner_fx = Mpy_32_16_1( Sqrt32( hCPE->hStereoDft->icbweRefEner_fx, &exp_icbweRefEner_fx ), 1638 /* 0.05 in Q15 */ );
1155 35803 : move32();
1156 35803 : hCPE->hStereoICBWE->icbweRefEner_fx_e = exp_icbweRefEner_fx;
1157 35803 : move16();
1158 : // lbEner = 0.05f * (float) sqrt( hCPE->hStereoDft->lbEner );
1159 35803 : thr = icbwe_thr_DFT_fx;
1160 35803 : regV = icbwe_regressionValuesDFT_fx;
1161 : }
1162 : ELSE
1163 : {
1164 121637 : IF( EQ_16( st->L_frame, L_FRAME ) )
1165 : {
1166 67392 : startB = 34;
1167 67392 : move16();
1168 67392 : endB = 14;
1169 67392 : move16();
1170 1145664 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1171 : {
1172 1078272 : uB = 0;
1173 1078272 : move16();
1174 22643712 : FOR( nB = startB; nB > endB; nB-- )
1175 : {
1176 21565440 : realBufferFlipped[ts][uB] = realBuffer[ts][nB];
1177 21565440 : move32();
1178 21565440 : if ( s_and( ts, 1 ) != 0 )
1179 : {
1180 10782720 : realBufferFlipped[ts][uB] = L_negate( realBufferFlipped[ts][uB] );
1181 10782720 : move32();
1182 : }
1183 21565440 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
1184 21565440 : move32();
1185 21565440 : if ( s_and( ts, 1 ) == 0 )
1186 : {
1187 10782720 : imagBufferFlipped[ts][uB] = L_negate( imagBufferFlipped[ts][uB] );
1188 10782720 : move32();
1189 : }
1190 21565440 : uB = add( uB, 1 ); /* uB ++ */
1191 : }
1192 : }
1193 : }
1194 : ELSE
1195 : {
1196 54245 : startB = 39;
1197 54245 : move16();
1198 54245 : endB = 19;
1199 54245 : move16();
1200 922165 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1201 : {
1202 867920 : uB = 0;
1203 867920 : move16();
1204 18226320 : FOR( nB = startB; nB > endB; nB-- )
1205 : {
1206 17358400 : realBufferFlipped[ts][uB] = L_negate( realBuffer[ts][nB] );
1207 17358400 : move32();
1208 17358400 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
1209 17358400 : move32();
1210 17358400 : uB = add( uB, 1 ); /* uB ++ */
1211 : }
1212 : }
1213 : }
1214 :
1215 1338007 : FOR( nB = 0; nB < 10; nB++ )
1216 : {
1217 20678290 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1218 : {
1219 19461920 : realQ_neg1 = extract_l( L_shr( realBufferFlipped[ts][nB], 16 ) );
1220 19461920 : imagQ_neg1 = extract_l( L_shr( imagBufferFlipped[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1221 :
1222 19461920 : CldfbHB_fx = L_mac0_o( CldfbHB_fx, realQ_neg1, realQ_neg1, &Overflow );
1223 19461920 : CldfbHB_fx = L_mac0_o( CldfbHB_fx, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1224 : }
1225 : }
1226 121637 : CldfbHB_fx_e = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1227 121637 : exp = norm_l( CldfbHB_fx );
1228 121637 : CldfbHB_fx = L_shl( CldfbHB_fx, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
1229 121637 : Cldfbtemp1 = Log2_norm_lc( CldfbHB_fx ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1230 121637 : Cldfbtemp1 = sub( shr( Cldfbtemp1, 6 ), shl( add( sub( Q31 - Q30, CldfbHB_fx_e ), exp ), 9 ) );
1231 121637 : CldfbHB_fx = L_mult( Cldfbtemp1, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1232 121637 : CldfbHB_fx = Mpy_32_16_1( L_sub( CldfbHB_fx, 1 << Q25 ), 11611 /* 0.1 * 0.221462 in Q19 */ );
1233 : /* Resulting Q = Q25 + Q19 - Q15 = Q29 */
1234 121637 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_fx, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
1235 121637 : move32();
1236 :
1237 121637 : IF( CldfbHB_fx == 0 )
1238 : {
1239 0 : CldfbHB_fx = EPSILLON_FX;
1240 0 : move32();
1241 : }
1242 :
1243 121637 : test();
1244 121637 : IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoICBWE != NULL )
1245 : {
1246 75 : Word32 icbweRefEner_fx = EPSILON_FX;
1247 75 : Word16 exp_icbweRefEner_fx = 0;
1248 :
1249 75 : move32();
1250 75 : move16();
1251 1575 : FOR( nB = 20; nB < 40; nB++ )
1252 : {
1253 25500 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1254 : {
1255 24000 : realQ_neg1 = extract_l( L_shr( realBuffer[ts][nB], 16 ) );
1256 24000 : imagQ_neg1 = extract_l( L_shr( imagBuffer[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1257 24000 : icbweRefEner_fx = L_mac0_o( icbweRefEner_fx, realQ_neg1, realQ_neg1, &Overflow );
1258 24000 : icbweRefEner_fx = L_mac0_o( icbweRefEner_fx, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1259 : }
1260 : }
1261 75 : exp_icbweRefEner_fx = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1262 75 : hCPE->hStereoICBWE->icbweRefEner_fx = Mpy_32_16_1( Sqrt32( icbweRefEner_fx, &exp_icbweRefEner_fx ), 1638 /* 0.05 in Q15 */ );
1263 75 : move32();
1264 75 : hCPE->hStereoICBWE->icbweRefEner_fx_e = exp_icbweRefEner_fx;
1265 75 : move16();
1266 : }
1267 :
1268 121637 : lbEner = EPSILON_FX;
1269 121637 : move32();
1270 2554377 : FOR( nB = 0; nB < 20; nB++ )
1271 : {
1272 41356580 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1273 : {
1274 38923840 : realQ_neg1 = extract_l( L_shr( realBuffer[ts][nB], 16 ) );
1275 38923840 : imagQ_neg1 = extract_l( L_shr( imagBuffer[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1276 :
1277 38923840 : lbEner = L_mac0_o( lbEner, realQ_neg1, realQ_neg1, &Overflow );
1278 38923840 : lbEner = L_mac0_o( lbEner, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1279 : }
1280 : }
1281 121637 : exp_lbEner = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1282 121637 : lbEner = Mpy_32_16_1( Sqrt32( lbEner, &exp_lbEner ), 1638 /* 0.05 in Q15 */ );
1283 121637 : thr = icbwe_thr_TDM_fx;
1284 121637 : regV = icbwe_regressionValuesTDM_fx;
1285 :
1286 121637 : cldfbSynthesis_ivas_fx( realBufferFlipped, imagBufferFlipped, shb_speech_fx_32, -1, 0, 0, st->cldfbSynTd );
1287 121637 : Copy_Scale_sig_32_16( shb_speech_fx_32, shb_speech, L_FRAME16k, negate( sub( q_reImBuffer, 1 ) ) );
1288 121637 : *Q_shb_spch = 0;
1289 121637 : move16();
1290 : }
1291 :
1292 157440 : IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoICBWE != NULL )
1293 : {
1294 : Word16 exp_t;
1295 35878 : hCPE->hStereoICBWE->MSFlag = 0; /* Init the multi-source flag */
1296 35878 : move16();
1297 35878 : v = Mpy_32_16_1( L_add( L_deposit_l( st->voicing_fx[0] ), L_add( L_deposit_l( st->voicing_fx[1] ), L_deposit_l( st->voicing_fx[2] ) ) ), 10923 /* 0.33333 in Q15 */ );
1298 :
1299 : // t = log10f( ( hCPE->hStereoICBWE->icbweRefEner + 1e-6f ) / ( lbEner + 1e-6f ) );
1300 35878 : t = L_deposit_h( BASOP_Util_Divide3232_Scale( L_add( hCPE->hStereoICBWE->icbweRefEner_fx, EPSILON_FX ), L_add( lbEner, EPSILON_FX ), &exp_t ) );
1301 35878 : exp_t = add( exp_t, sub( hCPE->hStereoICBWE->icbweRefEner_fx_e, exp_lbEner ) );
1302 35878 : exp = norm_l( t );
1303 35878 : t = L_shl( t, exp );
1304 35878 : t_16 = Log2_norm_lc( t ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1305 35878 : t_16 = sub( shr( t_16, 6 ), shl( add( sub( Q31 - Q30, exp_t ), exp ), 9 ) );
1306 35878 : t = L_mult( t_16, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1307 : /* Three Level Decision Tree to calculate a regression value first */
1308 35878 : IF( LT_32( t, thr[0] ) ) /* level 1 */
1309 : {
1310 26443 : IF( LT_32( t, thr[1] ) ) /* level 2 */
1311 : {
1312 23061 : IF( LT_32( v, thr[3] ) )
1313 : {
1314 0 : regression = regV[0]; /* level 3 */
1315 0 : move32();
1316 : }
1317 : ELSE
1318 : {
1319 23061 : regression = regV[1];
1320 23061 : move32();
1321 : }
1322 : }
1323 : ELSE
1324 : {
1325 3382 : IF( LT_32( v, thr[4] ) )
1326 : {
1327 3382 : regression = regV[2]; /* level 3 */
1328 3382 : move32();
1329 : }
1330 : ELSE
1331 : {
1332 0 : regression = regV[3];
1333 0 : move32();
1334 : }
1335 : }
1336 : }
1337 : ELSE
1338 : {
1339 9435 : IF( LT_32( t, thr[2] ) ) /* level 2 */
1340 : {
1341 2449 : IF( LT_32( v, thr[5] ) )
1342 : {
1343 2449 : regression = regV[4]; /* level 3 */
1344 2449 : move32();
1345 : }
1346 : ELSE
1347 : {
1348 0 : regression = regV[5];
1349 0 : move32();
1350 : }
1351 : }
1352 : ELSE
1353 : {
1354 6986 : IF( LT_32( v, thr[6] ) )
1355 : {
1356 6986 : regression = regV[6]; /* level 3 */
1357 6986 : move32();
1358 : }
1359 : ELSE
1360 : {
1361 0 : regression = regV[7];
1362 0 : move32();
1363 : }
1364 : }
1365 : }
1366 :
1367 35878 : test();
1368 35878 : test();
1369 : /* Convert the regression to a hard decision (classification) */
1370 35878 : IF( GT_32( regression, 1696512081 /* 0.79f in Q31*/ ) && !( LT_16( st->bwidth, SWB ) || hCPE->hCoreCoder[0]->vad_flag == 0 ) )
1371 : {
1372 6005 : hCPE->hStereoICBWE->MSFlag = 1;
1373 6005 : move16();
1374 : }
1375 : }
1376 :
1377 157440 : test();
1378 157440 : test();
1379 157440 : IF( NE_16( st->extl, WB_TBE ) && NE_16( st->extl, SWB_TBE ) && NE_16( st->extl, FB_TBE ) )
1380 : {
1381 : /* Update the previous superwideband speech buffer in case of a SWB_BWE frame - this code is in swb_tbe_enc */
1382 37632 : delay = L_LOOK_16k + L_SUBFR16k;
1383 37632 : move16();
1384 37632 : Copy( shb_speech + sub( L_FRAME16k, delay ), hBWE_TD->old_speech_shb_fx, delay );
1385 : }
1386 : }
1387 : ELSE
1388 : {
1389 238602 : test();
1390 238602 : test();
1391 238602 : IF( ( EQ_16( st->bwidth, FB ) || st->core == ACELP_CORE ) && ( st->element_mode == EVS_MONO ) )
1392 : {
1393 0 : InitSWBencBufferStates_fx( st->hBWE_TD, shb_speech );
1394 : }
1395 : ELSE
1396 : {
1397 238602 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1398 : {
1399 12313 : IF( EQ_16( st->L_frame, L_FRAME ) )
1400 : {
1401 3596 : L_resamp = 560; /* 6.4 kHz core -> 6 - 14 kHz SHB target. 20 ms is 560 samples in 28 kHz sample rate */
1402 3596 : move16();
1403 : }
1404 : ELSE
1405 : {
1406 8717 : L_resamp = 620; /* 8 kHz core -> 7.5 - 15.5 kHz SHB target. 20 ms is 620 samples in 31 kHz sample rate */
1407 8717 : move16();
1408 : }
1409 :
1410 : /* Dirty downsampling to match Nyquist to upper frequency limit of target */
1411 12313 : lerp( st->input_fx, new_swb_speech, L_resamp, extract_l( Mpy_32_32( input_Fs, one_by_50_Q31 ) ) );
1412 :
1413 : /* flip the spectrum */
1414 12313 : Copy( new_swb_speech, spchTmp, L_resamp );
1415 3721463 : FOR( i = 0; i < L_resamp; i = i + 2 )
1416 : {
1417 3709150 : spchTmp[i] = negate( spchTmp[i] );
1418 3709150 : move16();
1419 : }
1420 :
1421 : /* Dirty upsampling to match Nyquist/2 to lower frequency limit of target (reversed spectrum)*/
1422 12313 : lerp( spchTmp, spchTmp2, L_FRAME32k, L_resamp );
1423 12313 : Copy( spchTmp2, spchTmp, L_FRAME32k );
1424 : }
1425 : ELSE
1426 : {
1427 : /* flip the spectrum */
1428 226289 : Copy( new_swb_speech, spchTmp, L_FRAME32k );
1429 :
1430 72638769 : FOR( i = 0; i < L_FRAME32k; i = i + 2 )
1431 : {
1432 72412480 : spchTmp[i] = negate( spchTmp[i] );
1433 72412480 : move16();
1434 : }
1435 : }
1436 :
1437 238602 : Decimate_allpass_steep_fx( spchTmp, hBWE_TD->state_ana_filt_shb_fx, L_FRAME32k, shb_speech );
1438 :
1439 238602 : Copy( shb_speech + L_FRAME16k - ( L_LOOK_16k + L_SUBFR16k ), hBWE_TD->old_speech_shb_fx, L_LOOK_16k + L_SUBFR16k );
1440 :
1441 : /*Compute the past overlap for potential next iDFTs SHB*/
1442 238602 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1443 : {
1444 1736133 : FOR( i = 0; i < STEREO_DFT_OVL_16k; i++ )
1445 : {
1446 1723820 : hCPE->hStereoDft->output_mem_dmx_16k_shb_fx[i] = L_mult0( shb_speech[20 + i], mult_r( hCPE->hStereoDft->win_ana_16k_fx[STEREO_DFT_OVL_16k - 1 - i], hCPE->hStereoDft->win_ana_16k_fx[STEREO_DFT_OVL_16k - 1 - i] ) ); // Q15
1447 1723820 : move32();
1448 : }
1449 : }
1450 : }
1451 :
1452 238602 : IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1453 : {
1454 : /* Reset CLDFB synthesis buffer */
1455 226289 : set32_fx( st->cldfbSynTd->cldfb_state_fx, 0, st->cldfbSynTd->p_filter_length );
1456 : }
1457 : ELSE
1458 : {
1459 12313 : hCPE->hStereoDft->flip_sign = negate( hCPE->hStereoDft->flip_sign ); /* Make sure sign is updated even if DFT SHB target is not generated */
1460 12313 : move16();
1461 : }
1462 : }
1463 :
1464 : /* Memory reset to compensate for 0.9375 ms offset when transitioning from IO to SWB */
1465 : /* When switching from n >1 to n = 1, we keep the enc/dec delay as 8.75/3.25 and below code not needed;
1466 : only when n = 1 start, it will be 9.6875/2.3125 in that case this reset is needed for IO->BWE.*/
1467 396042 : test();
1468 396042 : IF( EQ_16( st->last_extl, -1 ) && ( st->element_mode == EVS_MONO ) )
1469 : {
1470 0 : delay = NS2SA_FX2( input_Fs, DELAY_FIR_RESAMPL_NS );
1471 0 : move16();
1472 0 : FOR( i = 0; i < delay; i++ )
1473 : {
1474 0 : shb_speech[i] = mult_r( mult_r( i, 983 /*0.03f Q15*/ ), shb_speech[2 * delay - 1 - i] );
1475 0 : move16();
1476 : }
1477 : }
1478 :
1479 396042 : return;
1480 : }
|