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 43613 : 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 43613 : Word16 fSwitchFromIO = 0;
213 : Word16 ppp_mode;
214 43613 : Word64 temp1 = 0;
215 43613 : Word64 temp2 = 0;
216 : Word32 tmp1, tmp2;
217 : Word16 norm;
218 43613 : move16();
219 43613 : move64();
220 43613 : move64();
221 :
222 :
223 43613 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
224 43613 : FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
225 :
226 43613 : 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 43613 : ppp_mode = 0;
234 43613 : move16();
235 : }
236 :
237 43613 : test();
238 43613 : test();
239 43613 : test();
240 43613 : test();
241 43613 : test();
242 43613 : test();
243 43613 : test();
244 43613 : test();
245 87221 : if ( ( EQ_32( st_fx->last_total_brate, ACELP_6k60 ) ) ||
246 87216 : ( EQ_32( st_fx->last_total_brate, ACELP_8k85 ) ) ||
247 87175 : ( EQ_32( st_fx->last_total_brate, ACELP_12k65 ) ) ||
248 87033 : ( EQ_32( st_fx->last_total_brate, ACELP_14k25 ) ) ||
249 86728 : ( EQ_32( st_fx->last_total_brate, ACELP_15k85 ) ) ||
250 86512 : ( EQ_32( st_fx->last_total_brate, ACELP_18k25 ) ) ||
251 86498 : ( EQ_32( st_fx->last_total_brate, ACELP_19k85 ) ) ||
252 86408 : ( EQ_32( st_fx->last_total_brate, ACELP_23k05 ) ) ||
253 43160 : ( EQ_32( st_fx->last_total_brate, ACELP_23k85 ) ) )
254 : {
255 532 : fSwitchFromIO = 1;
256 532 : move16();
257 : }
258 :
259 43613 : 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 43613 : max_wb = 1;
262 43613 : move16();
263 13999773 : FOR( i = 0; i < L_FRAME16k; i++ )
264 : {
265 13956160 : max_wb = s_max( max_wb, abs_s( new_inp_resamp16k[i] ) );
266 : }
267 43613 : Q_wb_sp = norm_s( max_wb );
268 :
269 43613 : Copy_Scale_sig( new_inp_resamp16k, temp_buf, L_FRAME16k, -3 );
270 43613 : temp1 = W_mac_32_32( temp1, temp_buf[0], temp_buf[0] );
271 13956160 : FOR( i = 1; i < L_FRAME16k; i++ )
272 : {
273 13912547 : temp1 = W_mac_32_32( temp1, temp_buf[i], temp_buf[i] );
274 13912547 : temp2 = W_mac_32_32( temp2, temp_buf[i - 1], temp_buf[i] );
275 : }
276 43613 : norm = s_min( W_norm( temp1 ), W_norm( temp2 ) );
277 43613 : tmp1 = W_extract_h( W_shl( temp1, norm ) );
278 43613 : tmp2 = W_extract_h( W_shl( temp2, norm ) );
279 :
280 43613 : IF( LT_32( tmp2, Mpy_32_16_1( tmp1, 31129 /*0.95f*/ ) ) )
281 : {
282 12115 : Q_wb_sp = sub( Q_wb_sp, 3 ); /* leave 3 bit headroom */
283 : }
284 43613 : Copy_Scale_sig( new_inp_resamp16k, temp_buf, L_FRAME16k, Q_wb_sp );
285 43613 : Scale_sig( hBWE_TD->decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), Q_wb_sp );
286 43613 : 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 43613 : test();
290 43613 : test();
291 43613 : IF( EQ_16( st_fx->extl, WB_BWE ) || EQ_16( st_fx->extl, WB_TBE ) || st_fx->igf )
292 : {
293 21364 : ramp_flag = 0;
294 21364 : move16();
295 :
296 21364 : test();
297 21364 : test();
298 21364 : test();
299 21364 : test();
300 21364 : 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 583 : ramp_flag = 1;
304 583 : move16();
305 : }
306 :
307 21364 : 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 21364 : 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 16337 : 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 15105 : ELSE IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
383 : {
384 15105 : 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 15105 : Word16 l_recalc_16k = L_FILT16k + 1; /* Note: "+1" is used because L_FILT16k is not divisible by 4 */
391 15105 : Word16 l_recalc_4k = ( L_FILT16k + 1 ) / 4;
392 :
393 15105 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
394 :
395 : /*Get past signal*/
396 15105 : Copy_Scale_sig( hBWE_FD->L_old_wtda_swb_fx + L_FRAME16k - l_recalc_16k, old_input, l_recalc_16k, Q_wb_sp );
397 15105 : 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 15105 : Copy( temp_buf, old_input + Sample_Delay_WB_BWE + l_recalc_16k, L_FRAME16k );
401 :
402 : /*compute hb_speech on delayed input*/
403 15105 : 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 15105 : 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 15105 : Copy( hb_speech + l_recalc_4k, hb_speech, ( L_FRAME16k / 4 ) - l_recalc_4k );
408 :
409 : /*rest without memory update*/
410 15105 : Copy( hBWE_TD->decim_state1_fx, decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
411 15105 : Copy( hBWE_TD->decim_state2_fx, decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
412 :
413 15105 : 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 21364 : Scale_sig( hb_speech, L_FRAME16k / 4, -Q_wb_sp );
423 21364 : Scale_sig( hBWE_TD->decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), -Q_wb_sp );
424 21364 : Scale_sig( hBWE_TD->decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), -Q_wb_sp );
425 :
426 21364 : IF( NE_16( st_fx->extl, WB_TBE ) )
427 : {
428 : /* Update the previous wideband speech buffer in case of a WB_BWE frame */
429 13724 : Sample_Delay_WB_BWE = ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16;
430 :
431 13724 : highband_new_speech = highband_old_speech + Sample_Delay_WB_BWE;
432 13724 : Copy( hb_speech, highband_new_speech, L_FRAME16k / 4 );
433 13724 : Copy( highband_old_speech + L_FRAME16k / 4, hBWE_TD->old_speech_wb_fx, Sample_Delay_WB_BWE );
434 : }
435 : }
436 : }
437 : ELSE
438 : {
439 22249 : set16_fx( hBWE_TD->decim_state1_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
440 22249 : set16_fx( hBWE_TD->decim_state2_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
441 22249 : set16_fx( hBWE_TD->old_speech_wb_fx, 0, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
442 : }
443 :
444 43613 : if ( hBWE_FD != NULL )
445 : {
446 43613 : hBWE_FD->mem_old_wtda_swb_fx = new_inp_resamp16k[L_FRAME16k - L_MEM_RECALC_16K - L_FILT16k - 1];
447 43613 : 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 43613 : test();
452 43613 : test();
453 43613 : test();
454 43613 : 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 31175 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
457 :
458 31175 : Copy( new_inp_resamp16k, &old_input[Sample_Delay_WB_BWE], L_FRAME16k );
459 31175 : Copy( hBWE_FD->old_input_wb_fx, old_input, Sample_Delay_WB_BWE );
460 31175 : Copy( new_inp_resamp16k + L_FRAME16k - Sample_Delay_WB_BWE, hBWE_FD->old_input_wb_fx, Sample_Delay_WB_BWE );
461 31175 : test();
462 31175 : IF( NE_16( st_fx->extl, SWB_BWE ) && NE_16( st_fx->extl, FB_BWE ) )
463 : {
464 31175 : Copy( old_input, hBWE_FD->L_old_wtda_swb_fx, L_FRAME16k );
465 31175 : st_fx->Q_old_wtda = -1;
466 31175 : move16();
467 : }
468 : }
469 43613 : 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 1045 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
552 1045 : Copy( hBWE_FD->old_fdbwe_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], L_FRAME32k );
553 1045 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
554 1045 : Copy( hBWE_FD->old_fdbwe_speech_fx + L_FRAME32k - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
555 :
556 1045 : IF( sub( st_fx->extl, WB_BWE ) != 0 )
557 : {
558 1045 : 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 1045 : 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 1062 : test();
574 1062 : test();
575 1062 : 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 388 : test();
583 388 : 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 388 : IF( EQ_16( st_fx->last_bwidth, FB ) )
587 : {
588 0 : inner_frame = L_FRAME48k;
589 0 : inner_Fs = 48000;
590 0 : Copy( hBWE_FD->old_fdbwe_speech_fx, new_swb_speech_fx, L_FRAME48k );
591 : }
592 : ELSE
593 : {
594 388 : inner_frame = L_FRAME32k;
595 388 : inner_Fs = 32000;
596 388 : 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,
597 388 : decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
598 : }
599 388 : move16();
600 388 : move16();
601 :
602 388 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
603 388 : Copy( new_swb_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], inner_frame );
604 388 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
605 388 : Copy( new_swb_speech_fx + inner_frame - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
606 388 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, inner_frame );
607 388 : st_fx->Q_old_wtda = 0;
608 388 : move16();
609 : }
610 : /* resample 48 kHz to 32kHz */
611 388 : IF( EQ_16( st_fx->bwidth, FB ) )
612 : {
613 0 : Copy( input_fx, new_swb_speech_fx, L_FRAME48k );
614 : }
615 : ELSE
616 : {
617 388 : 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,
618 388 : decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
619 : }
620 : }
621 : }
622 : ELSE
623 : {
624 : /* resample 48 kHz to 32kHz */
625 988 : IF( EQ_16( st_fx->bwidth, FB ) )
626 : {
627 0 : Copy( input_fx, new_swb_speech_fx, L_FRAME48k );
628 : }
629 : ELSE
630 : {
631 988 : 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,
632 988 : decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hBWE_TD->dec_2_over_3_mem_lp_fx );
633 : }
634 : }
635 : }
636 :
637 3100 : test();
638 3100 : test();
639 3100 : test();
640 3100 : test();
641 3100 : test();
642 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 ) ) )
643 : {
644 1365 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
645 : {
646 753 : startB = 34;
647 753 : move16();
648 753 : endB = 14;
649 753 : move16();
650 12801 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
651 : {
652 12048 : uB = 0;
653 12048 : move16();
654 253008 : FOR( nB = startB; nB > endB; nB-- )
655 : {
656 240960 : realBufferFlipped[ts][uB] = realBuffer[ts][nB];
657 240960 : move32();
658 240960 : if ( s_and( ts, 1 ) != 0 )
659 : {
660 120480 : realBufferFlipped[ts][uB] = L_negate( realBufferFlipped[ts][uB] );
661 120480 : move32();
662 : }
663 240960 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
664 240960 : move32();
665 240960 : if ( s_and( ts, 1 ) == 0 )
666 : {
667 120480 : imagBufferFlipped[ts][uB] = L_negate( imagBufferFlipped[ts][uB] );
668 120480 : move32();
669 : }
670 240960 : uB = add( uB, 1 ); /* uB ++ */
671 : }
672 : }
673 : }
674 : ELSE
675 : {
676 612 : startB = 39;
677 612 : move16();
678 612 : endB = 19;
679 612 : move16();
680 10404 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
681 : {
682 9792 : uB = 0;
683 9792 : move16();
684 205632 : FOR( nB = startB; nB > endB; nB-- )
685 : {
686 195840 : realBufferFlipped[ts][uB] = L_negate( realBuffer[ts][nB] );
687 195840 : move32();
688 195840 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
689 195840 : move32();
690 195840 : uB = add( uB, 1 ); /* uB ++ */
691 : }
692 : }
693 : }
694 :
695 : /* Highband energy computation for gain shape control in case of bandwidth switching fix*/
696 : {
697 1365 : CldfbHB_ener = 0;
698 1365 : move32();
699 15015 : FOR( nB = 0; nB < 10; nB++ )
700 : {
701 232050 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
702 : {
703 218400 : realQ_neg1 = extract_l( L_shr( realBufferFlipped[ts][nB], 31 - ( 15 + cldfbScale->hb_scale ) + 1 ) );
704 218400 : imagQ_neg1 = extract_l( L_shr( imagBufferFlipped[ts][nB], 31 - ( 15 + cldfbScale->hb_scale ) + 1 ) ); /* Q(-1), headroom needed */
705 :
706 218400 : CldfbHB_ener = L_mac0_o( CldfbHB_ener, realQ_neg1, realQ_neg1, &Overflow );
707 218400 : CldfbHB_ener = L_mac0_o( CldfbHB_ener, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
708 : }
709 : }
710 :
711 1365 : exp = norm_l( CldfbHB_ener );
712 1365 : CldfbHB_ener = L_shl( CldfbHB_ener, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
713 1365 : Cldfbtemp1 = ( Log2_norm_lc( CldfbHB_ener ) );
714 1365 : 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 */
715 1365 : CldfbHB_ener = L_mult( sub( Cldfbtemp1, 1741 /*3.401 Q9*/ ), 3495 ); /* 3495 = Q19 log10(2)*0.1/log10(32768), Q = 19+9+1 = 29 */
716 1365 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_ener, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
717 : }
718 1365 : cldfbSynthesis_fx( st_fx->cldfbSynTd, realBufferFlipped, imagBufferFlipped, cldfbScale, shb_speech_fx, 0, CLDFB_NO_COL_MAX, cldfbWorkBuffer );
719 1365 : *Q_shb_spch = 0; /*shb_speech_fx : Q0*/
720 1365 : move16();
721 :
722 1365 : test();
723 1365 : test();
724 1365 : IF( NE_16( st_fx->extl, WB_TBE ) && NE_16( st_fx->extl, SWB_TBE ) && NE_16( st_fx->extl, FB_TBE ) )
725 : {
726 : /* Update the previous superwideband speech buffer in case of a SWB_BWE frame - this code is in swb_tbe_enc */
727 5 : delay = L_LOOK_16k + L_SUBFR16k;
728 5 : Copy( shb_speech_fx + L_FRAME16k - delay, hBWE_TD->old_speech_shb_fx, delay );
729 : }
730 : }
731 : ELSE
732 : {
733 1735 : test();
734 1735 : IF( EQ_16( st_fx->bwidth, FB ) || EQ_16( st_fx->core, ACELP_CORE ) )
735 : {
736 674 : set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
737 674 : set16_fx( shb_speech_fx, 0, L_FRAME16k ); /* shb_speech for FB/SWB BWE_HIGHRATE is not used at 64kbps */
738 : }
739 : ELSE
740 : {
741 : /* flip the spectrm */
742 1061 : Copy( new_swb_speech_fx, spchTmp_fx, L_FRAME32k );
743 :
744 340581 : FOR( i = 0; i < L_FRAME32k; i = i + 2 )
745 : {
746 339520 : spchTmp_fx[i] = negate( spchTmp_fx[i] );
747 339520 : move16();
748 : }
749 :
750 1061 : Decimate_allpass_steep_fx( spchTmp_fx, hBWE_TD->state_ana_filt_shb_fx, L_FRAME32k, shb_speech_fx );
751 1061 : Copy( shb_speech_fx + L_FRAME16k - ( L_LOOK_16k + L_SUBFR16k ), hBWE_TD->old_speech_shb_fx, L_LOOK_16k + L_SUBFR16k );
752 : }
753 :
754 : /* Reset CLDFB synthesis buffer */
755 1735 : set16_fx( st_fx->cldfbSynTd->FilterStates, 0, st_fx->cldfbSynTd->p_filter_length + st_fx->cldfbSynTd->no_channels * st_fx->cldfbSynTd->no_col );
756 : }
757 3100 : IF( EQ_16( st_fx->last_extl, -1 ) )
758 : {
759 423 : delay = NS2SA_FX2( st_fx->input_Fs, DELAY_FIR_RESAMPL_NS );
760 18918 : FOR( i = 0; i < delay; i++ )
761 : {
762 18495 : shb_speech_fx[i] = mult_r( mult_r( i, 983 /*0.03f Q15*/ ), shb_speech_fx[2 * delay - 1 - i] );
763 18495 : move16();
764 : }
765 : }
766 :
767 3100 : return;
768 : }
769 :
770 : /*-------------------------------------------------------------------*
771 : * swb_pre_proc()
772 : *
773 : * - Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB target signal
774 : * for SWB TBE or SWB BWE coding
775 : * - Common SWB TBE and SWB BWE pre-processing
776 : *-------------------------------------------------------------------*/
777 :
778 : /*full implementation pending*/
779 395472 : void swb_pre_proc_ivas_fx(
780 : Encoder_State *st, /* i/o: encoder state structure */
781 : Word16 *new_swb_speech, /* o : original input signal at 32kHz - st->q_inp */
782 : Word32 *new_swb_speech_fx, /* o : original input signal at 32kHz - Q - q_reImBuffer */
783 : Word16 *shb_speech, /* o : SHB target signal (6-14kHz) at 16kHz- Q(Q_shb_spch) */
784 : Word16 *Q_shb_spch,
785 : Word32 realBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i : real buffer Q - q_reImbuffer */
786 : Word32 imagBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* i : imag buffer Q - q_reImbuffer */
787 : Word16 q_reImBuffer, /* i : scale data of real and imag CLDFB buffers */
788 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structure */
789 : )
790 : {
791 : Word16 Sample_Delay_SWB_BWE, inner_frame, delay;
792 : TD_BWE_ENC_HANDLE hBWE_TD;
793 : FD_BWE_ENC_HANDLE hBWE_FD;
794 : Word32 inner_Fs, input_Fs;
795 : Word16 old_input_fx[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k]; // fixed counterpart
796 : Word32 old_input_fx_32[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k]; // fixed counterpart
797 : Word16 spchTmp[L_FRAME32k], spchTmp2[L_FRAME32k];
798 : Word16 i, j, L_resamp;
799 : Word16 startB, endB;
800 : Word32 *realBufferFlipped[CLDFB_NO_COL_MAX];
801 : Word32 *imagBufferFlipped[CLDFB_NO_COL_MAX];
802 : Word32 realBufferTmp[CLDFB_NO_COL_MAX][20];
803 : Word32 imagBufferTmp[CLDFB_NO_COL_MAX][20];
804 : Word32 shb_speech_fx_32[L_FRAME16k];
805 : Word16 ts, nB, uB;
806 : Word32 v, t, regression;
807 : Word16 t_16;
808 : const Word32 *thr, *regV;
809 : Word32 lbEner;
810 395472 : Word16 exp_lbEner = 0;
811 : Word16 Sample_Delay_SWB_BWE32k, lMemRecalc32k, dft_ovl32k;
812 395472 : Word32 one_by_50_Q31 = 42949673;
813 :
814 395472 : lMemRecalc32k = NS2SA( 32000, L_MEM_RECALC_NS );
815 395472 : move16(); // exp_lbEner
816 395472 : move16(); // lMemRecalc32k
817 395472 : move32(); // one_by_50_Q31
818 :
819 : /* initialization */
820 395472 : hBWE_TD = st->hBWE_TD;
821 395472 : hBWE_FD = st->hBWE_FD;
822 395472 : input_Fs = st->input_Fs;
823 :
824 6723024 : FOR( j = 0; j < CLDFB_NO_COL_MAX; j++ )
825 : {
826 6327552 : set32_fx( realBufferTmp[j], 0, 20 );
827 6327552 : set32_fx( imagBufferTmp[j], 0, 20 );
828 6327552 : realBufferFlipped[j] = realBufferTmp[j];
829 6327552 : imagBufferFlipped[j] = imagBufferTmp[j];
830 : }
831 :
832 395472 : set16_fx( old_input_fx, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k );
833 :
834 395472 : IF( EQ_32( st->input_Fs, 32000 ) )
835 : {
836 57471 : IF( st->element_mode > EVS_MONO )
837 : {
838 57471 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS );
839 57471 : IF( EQ_16( st->L_frame, L_FRAME16k ) )
840 : {
841 32465 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS );
842 : }
843 :
844 57471 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
845 57471 : Copy( st->input_fx - L_FRAME32k, hBWE_FD->old_fdbwe_speech_fx, L_FRAME32k );
846 :
847 57471 : test();
848 57471 : test();
849 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
850 57471 : st->Q_old_wtda = st->q_inp;
851 57471 : move16();
852 57471 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) && GE_16( st->bwidth, SWB ) )
853 : {
854 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 ) );
855 : }
856 53496 : ELSE IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && GE_16( st->bwidth, SWB ) )
857 : {
858 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 ) );
859 : }
860 : }
861 :
862 57471 : Copy( st->input_fx, new_swb_speech, L_FRAME32k );
863 :
864 57471 : test();
865 57471 : test();
866 57471 : IF( NE_16( st->last_extl, SWB_BWE ) && NE_16( st->last_extl, FB_BWE ) && NE_16( st->extl, SWB_BWE_HIGHRATE ) )
867 : {
868 56638 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
869 56638 : test();
870 56638 : IF( st->element_mode > EVS_MONO && EQ_16( st->L_frame, L_FRAME16k ) )
871 : {
872 32337 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
873 : }
874 56638 : IF( st->element_mode > EVS_MONO )
875 : {
876 56638 : Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA( 32000, DELAY_FIR_RESAMPL_NS ) );
877 : }
878 :
879 56638 : Copy( hBWE_FD->old_fdbwe_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], L_FRAME32k );
880 :
881 56638 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
882 56638 : Copy( hBWE_FD->old_fdbwe_speech_fx + L_FRAME32k - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
883 56638 : IF( NE_16( st->extl, WB_BWE ) )
884 : {
885 56602 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, L_FRAME32k );
886 56602 : st->Q_old_wtda = st->q_inp;
887 56602 : move16();
888 : }
889 : }
890 :
891 57471 : test();
892 57471 : IF( NE_16( st->extl, SWB_BWE ) && NE_16( st->extl, FB_BWE ) )
893 : {
894 56630 : Copy( st->input_fx, hBWE_FD->old_fdbwe_speech_fx, L_FRAME32k );
895 : }
896 : }
897 : ELSE /* 48 kHz */
898 : {
899 :
900 338001 : Sample_Delay_SWB_BWE32k = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS );
901 338001 : move16();
902 338001 : Sample_Delay_SWB_BWE = NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS );
903 338001 : move16();
904 338001 : IF( EQ_16( st->L_frame, L_FRAME16k ) )
905 : {
906 118789 : Sample_Delay_SWB_BWE32k = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS );
907 118789 : move16();
908 118789 : Sample_Delay_SWB_BWE = NS2SA( 48000, DELAY_FD_BWE_ENC_16k_NS );
909 118789 : move16();
910 : }
911 :
912 338001 : dft_ovl32k = 0;
913 338001 : move16();
914 338001 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
915 : {
916 20888 : dft_ovl32k = (Word16) ( STEREO_DFT_OVL_MAX * 32000 / 48000 );
917 20888 : move16();
918 : }
919 :
920 338001 : IF( EQ_16( st->codec_mode, MODE1 ) )
921 : {
922 338001 : IF( st->element_mode > EVS_MONO )
923 : {
924 :
925 338001 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
926 : {
927 : }
928 335971 : ELSE IF( EQ_16( st->bwidth, FB ) )
929 : {
930 210167 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
931 : }
932 :
933 338001 : Copy( st->input_fx - L_FRAME48k, hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k );
934 :
935 338001 : test();
936 338001 : test();
937 338001 : 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
938 338001 : st->Q_old_wtda = st->q_inp;
939 338001 : move16();
940 :
941 338001 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) && GE_16( st->bwidth, SWB ) )
942 : {
943 1912 : IF( EQ_16( st->bwidth, SWB ) )
944 : {
945 : /* buffers hBWE_FD->old_input[] and hBWE_FD->old_wtda_swb[] need to be at 32 kHz (inner) sampling rate */
946 :
947 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,
948 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 );
949 :
950 439 : Copy( spchTmp, hBWE_FD->L_old_wtda_swb_fx + L_FRAME32k - sub( lMemRecalc32k, Sample_Delay_SWB_BWE32k ), sub( lMemRecalc32k, Sample_Delay_SWB_BWE32k ) );
951 439 : Copy( spchTmp + lMemRecalc32k - Sample_Delay_SWB_BWE32k, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE32k );
952 : }
953 : ELSE /* FB_BWE */
954 : {
955 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 ) );
956 1473 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
957 : }
958 : }
959 336089 : ELSE IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && GE_16( st->bwidth, SWB ) )
960 : {
961 19874 : IF( EQ_16( st->bwidth, SWB ) )
962 : {
963 10046 : lerp( st->input_fx - hCPE->hStereoDft->dft_ovl, spchTmp, dft_ovl32k - Sample_Delay_SWB_BWE32k, hCPE->hStereoDft->dft_ovl - Sample_Delay_SWB_BWE );
964 :
965 10046 : Copy( spchTmp, hBWE_FD->L_old_wtda_swb_fx + L_FRAME32k - ( dft_ovl32k - Sample_Delay_SWB_BWE32k ), dft_ovl32k - Sample_Delay_SWB_BWE32k );
966 : }
967 : ELSE
968 : {
969 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 );
970 : }
971 : }
972 : }
973 :
974 338001 : test();
975 338001 : test();
976 338001 : test();
977 338001 : test();
978 338001 : 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*/ )
979 : {
980 : /* move the resampling out of the TDBWE path as new_swb_speech is not needed for TDBWE. */
981 114613 : Copy( st->input_fx, hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k );
982 : }
983 : ELSE
984 : {
985 223388 : test();
986 223388 : IF( NE_16( st->last_extl, SWB_BWE ) && NE_16( st->last_extl, FB_BWE ) )
987 : {
988 : /* resample 48 kHz to 32kHz */
989 213503 : IF( EQ_16( st->last_bwidth, FB ) )
990 : {
991 157834 : inner_frame = L_FRAME48k;
992 157834 : move16();
993 157834 : inner_Fs = 48000;
994 157834 : move32();
995 157834 : Copy( hBWE_FD->old_fdbwe_speech_fx, new_swb_speech, L_FRAME48k );
996 : }
997 : ELSE
998 : {
999 55669 : inner_frame = L_FRAME32k;
1000 55669 : move16();
1001 55669 : inner_Fs = 32000;
1002 55669 : move32();
1003 :
1004 55669 : IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1005 : {
1006 55495 : 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 );
1007 : }
1008 : ELSE
1009 : {
1010 174 : lerp( hBWE_FD->old_fdbwe_speech_fx, new_swb_speech, inner_frame, L_FRAME48k );
1011 : }
1012 :
1013 55669 : test();
1014 55669 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && st->idchan == 0 )
1015 : {
1016 48894 : FOR( i = 0; i < STEREO_DFT_OVL_32k; i++ )
1017 : {
1018 48720 : 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
1019 48720 : move32();
1020 : }
1021 : }
1022 : }
1023 :
1024 213503 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
1025 213503 : move16();
1026 213503 : test();
1027 213503 : IF( st->element_mode > EVS_MONO && EQ_16( st->L_frame, L_FRAME16k ) )
1028 : {
1029 60697 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
1030 60697 : move16();
1031 : }
1032 213503 : IF( st->element_mode > EVS_MONO )
1033 : {
1034 213503 : Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) );
1035 : }
1036 :
1037 213503 : Copy( new_swb_speech, &old_input_fx[Sample_Delay_SWB_BWE], inner_frame );
1038 213503 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
1039 213503 : Copy( new_swb_speech + inner_frame - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
1040 213503 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, inner_frame );
1041 213503 : st->Q_old_wtda = st->q_inp;
1042 213503 : move16();
1043 : }
1044 :
1045 : /* resample 48 kHz to 32kHz */
1046 223388 : IF( EQ_16( st->bwidth, FB ) )
1047 : {
1048 162505 : Copy( st->input_fx, new_swb_speech, L_FRAME48k );
1049 : }
1050 : ELSE
1051 : {
1052 60883 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
1053 : {
1054 : Word16 dec_2_over_3_mem_tmp[L_FILT_2OVER3], dec_2_over_3_mem_lp_tmp[L_FILT_2OVER3_LP];
1055 :
1056 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 );
1057 :
1058 16 : Copy( hBWE_TD->dec_2_over_3_mem_fx, dec_2_over_3_mem_tmp, L_FILT_2OVER3 );
1059 16 : Copy( hBWE_TD->dec_2_over_3_mem_lp_fx, dec_2_over_3_mem_lp_tmp, L_FILT_2OVER3_LP );
1060 :
1061 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 );
1062 : }
1063 60867 : ELSE IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1064 : {
1065 59376 : 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 );
1066 : }
1067 : ELSE /* IVAS_CPE_DFT */
1068 : {
1069 : Word16 out_start_ind, out_end_ind;
1070 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 );
1071 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
1072 :
1073 1491 : Copy( new_swb_speech - Sample_Delay_SWB_BWE32k, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE32k ); // st->q_inp
1074 : }
1075 : }
1076 : }
1077 : }
1078 : ELSE
1079 : {
1080 : /* resample 48 kHz to 32kHz */
1081 0 : IF( EQ_16( st->bwidth, FB ) )
1082 : {
1083 0 : Copy( st->input_fx, new_swb_speech, L_FRAME48k );
1084 : }
1085 : ELSE
1086 : {
1087 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 );
1088 : }
1089 : }
1090 : }
1091 :
1092 395472 : test();
1093 395472 : test();
1094 395472 : test();
1095 395472 : test();
1096 395472 : test();
1097 395472 : test();
1098 395472 : IF( ( ( st->core == ACELP_CORE ) && NE_16( st->extl, SWB_BWE_HIGHRATE ) && NE_16( st->extl, FB_BWE_HIGHRATE ) ) ||
1099 : ( ( EQ_32( st->total_brate, ACELP_9k60 ) || st->rf_mode ) && EQ_16( st->bwidth, SWB ) && ( st->element_mode == EVS_MONO ) ) )
1100 157043 : {
1101 157043 : Word32 CldfbHB_fx = 0; // fixed counterpart
1102 157043 : Word16 CldfbHB_fx_e = 0; // fixed counterpart
1103 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1104 157043 : Flag Overflow = 0;
1105 157043 : move16();
1106 : #endif
1107 157043 : Word16 realQ_neg1, imagQ_neg1, exp = 0, Cldfbtemp1;
1108 157043 : move32();
1109 157043 : move16();
1110 157043 : move16();
1111 :
1112 157043 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1113 : {
1114 : Word16 out_start_ind, out_end_ind;
1115 : Word16 exp_icbweRefEner_fx;
1116 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
1117 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 );
1118 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
1119 :
1120 : /* delay corresponding to CLDFB delay */
1121 35803 : Copy( old_input_fx + STEREO_DFT_OVL_16k - 20, shb_speech, L_FRAME16k );
1122 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 );
1123 35803 : Copy( old_input_fx, hCPE->hStereoICBWE->mem_shb_speech_ref_fx, STEREO_DFT_OVL_16k - 20 ); // Q0
1124 35803 : hCPE->hStereoICBWE->mem_shb_speech_ref_e = 15;
1125 35803 : move16();
1126 :
1127 35803 : IF( LE_32( CldfbHB_fx, 0 ) )
1128 : {
1129 1090 : CldfbHB_fx = EPSILON_FX;
1130 1090 : move32();
1131 : }
1132 35803 : CldfbHB_fx = L_shl( CldfbHB_fx, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
1133 35803 : Cldfbtemp1 = Log2_norm_lc( CldfbHB_fx ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1134 35803 : Cldfbtemp1 = sub( shr( Cldfbtemp1, 6 ), shl( add( sub( Q31 - Q30, CldfbHB_fx_e ), exp ), 9 ) );
1135 35803 : CldfbHB_fx = L_mult( Cldfbtemp1, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1136 35803 : CldfbHB_fx = Mpy_32_16_1( L_sub( CldfbHB_fx, 1 << Q25 ), 11611 /* 0.1 * 0.221462 in Q19 */ );
1137 : /* Resulting Q = Q25 + Q19 - Q15 = Q29 */
1138 35803 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_fx, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
1139 35803 : move16();
1140 :
1141 35803 : exp_lbEner = hCPE->hStereoDft->lbEner_fx_e;
1142 35803 : move32();
1143 35803 : lbEner = Mpy_32_16_1( Sqrt32( hCPE->hStereoDft->lbEner_fx, &exp_lbEner ), 1638 /* 0.05 in Q15 */ );
1144 35803 : exp_icbweRefEner_fx = hCPE->hStereoDft->icbweRefEner_fx_e;
1145 35803 : move16();
1146 35803 : hCPE->hStereoICBWE->icbweRefEner_fx = Mpy_32_16_1( Sqrt32( hCPE->hStereoDft->icbweRefEner_fx, &exp_icbweRefEner_fx ), 1638 /* 0.05 in Q15 */ );
1147 35803 : move32();
1148 35803 : hCPE->hStereoICBWE->icbweRefEner_fx_e = exp_icbweRefEner_fx;
1149 35803 : move16();
1150 : // lbEner = 0.05f * (float) sqrt( hCPE->hStereoDft->lbEner );
1151 35803 : thr = icbwe_thr_DFT_fx;
1152 35803 : regV = icbwe_regressionValuesDFT_fx;
1153 : }
1154 : ELSE
1155 : {
1156 121240 : IF( EQ_16( st->L_frame, L_FRAME ) )
1157 : {
1158 67081 : startB = 34;
1159 67081 : move16();
1160 67081 : endB = 14;
1161 67081 : move16();
1162 1140377 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1163 : {
1164 1073296 : uB = 0;
1165 1073296 : move16();
1166 22539216 : FOR( nB = startB; nB > endB; nB-- )
1167 : {
1168 21465920 : realBufferFlipped[ts][uB] = realBuffer[ts][nB];
1169 21465920 : move32();
1170 21465920 : if ( s_and( ts, 1 ) != 0 )
1171 : {
1172 10732960 : realBufferFlipped[ts][uB] = L_negate( realBufferFlipped[ts][uB] );
1173 10732960 : move32();
1174 : }
1175 21465920 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
1176 21465920 : move32();
1177 21465920 : if ( s_and( ts, 1 ) == 0 )
1178 : {
1179 10732960 : imagBufferFlipped[ts][uB] = L_negate( imagBufferFlipped[ts][uB] );
1180 10732960 : move32();
1181 : }
1182 21465920 : uB = add( uB, 1 ); /* uB ++ */
1183 : }
1184 : }
1185 : }
1186 : ELSE
1187 : {
1188 54159 : startB = 39;
1189 54159 : move16();
1190 54159 : endB = 19;
1191 54159 : move16();
1192 920703 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1193 : {
1194 866544 : uB = 0;
1195 866544 : move16();
1196 18197424 : FOR( nB = startB; nB > endB; nB-- )
1197 : {
1198 17330880 : realBufferFlipped[ts][uB] = L_negate( realBuffer[ts][nB] );
1199 17330880 : move32();
1200 17330880 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
1201 17330880 : move32();
1202 17330880 : uB = add( uB, 1 ); /* uB ++ */
1203 : }
1204 : }
1205 : }
1206 :
1207 1333640 : FOR( nB = 0; nB < 10; nB++ )
1208 : {
1209 20610800 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1210 : {
1211 19398400 : realQ_neg1 = extract_l( L_shr( realBufferFlipped[ts][nB], 16 ) );
1212 19398400 : imagQ_neg1 = extract_l( L_shr( imagBufferFlipped[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1213 :
1214 19398400 : CldfbHB_fx = L_mac0_o( CldfbHB_fx, realQ_neg1, realQ_neg1, &Overflow );
1215 19398400 : CldfbHB_fx = L_mac0_o( CldfbHB_fx, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1216 : }
1217 : }
1218 121240 : CldfbHB_fx_e = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1219 121240 : exp = norm_l( CldfbHB_fx );
1220 121240 : CldfbHB_fx = L_shl( CldfbHB_fx, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
1221 121240 : Cldfbtemp1 = Log2_norm_lc( CldfbHB_fx ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1222 121240 : Cldfbtemp1 = sub( shr( Cldfbtemp1, 6 ), shl( add( sub( Q31 - Q30, CldfbHB_fx_e ), exp ), 9 ) );
1223 121240 : CldfbHB_fx = L_mult( Cldfbtemp1, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1224 121240 : CldfbHB_fx = Mpy_32_16_1( L_sub( CldfbHB_fx, 1 << Q25 ), 11611 /* 0.1 * 0.221462 in Q19 */ );
1225 : /* Resulting Q = Q25 + Q19 - Q15 = Q29 */
1226 121240 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_fx, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
1227 121240 : move32();
1228 :
1229 121240 : IF( CldfbHB_fx == 0 )
1230 : {
1231 0 : CldfbHB_fx = EPSILLON_FX;
1232 0 : move32();
1233 : }
1234 :
1235 121240 : test();
1236 121240 : IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoICBWE != NULL )
1237 : {
1238 75 : Word32 icbweRefEner_fx = EPSILON_FX;
1239 75 : Word16 exp_icbweRefEner_fx = 0;
1240 :
1241 75 : move32();
1242 75 : move16();
1243 1575 : FOR( nB = 20; nB < 40; nB++ )
1244 : {
1245 25500 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1246 : {
1247 24000 : realQ_neg1 = extract_l( L_shr( realBuffer[ts][nB], 16 ) );
1248 24000 : imagQ_neg1 = extract_l( L_shr( imagBuffer[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1249 24000 : icbweRefEner_fx = L_mac0_o( icbweRefEner_fx, realQ_neg1, realQ_neg1, &Overflow );
1250 24000 : icbweRefEner_fx = L_mac0_o( icbweRefEner_fx, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1251 : }
1252 : }
1253 75 : exp_icbweRefEner_fx = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1254 75 : hCPE->hStereoICBWE->icbweRefEner_fx = Mpy_32_16_1( Sqrt32( icbweRefEner_fx, &exp_icbweRefEner_fx ), 1638 /* 0.05 in Q15 */ );
1255 75 : move32();
1256 75 : hCPE->hStereoICBWE->icbweRefEner_fx_e = exp_icbweRefEner_fx;
1257 75 : move16();
1258 : }
1259 :
1260 121240 : lbEner = EPSILON_FX;
1261 121240 : move32();
1262 2546040 : FOR( nB = 0; nB < 20; nB++ )
1263 : {
1264 41221600 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1265 : {
1266 38796800 : realQ_neg1 = extract_l( L_shr( realBuffer[ts][nB], 16 ) );
1267 38796800 : imagQ_neg1 = extract_l( L_shr( imagBuffer[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1268 :
1269 38796800 : lbEner = L_mac0_o( lbEner, realQ_neg1, realQ_neg1, &Overflow );
1270 38796800 : lbEner = L_mac0_o( lbEner, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1271 : }
1272 : }
1273 121240 : exp_lbEner = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1274 121240 : lbEner = Mpy_32_16_1( Sqrt32( lbEner, &exp_lbEner ), 1638 /* 0.05 in Q15 */ );
1275 121240 : thr = icbwe_thr_TDM_fx;
1276 121240 : regV = icbwe_regressionValuesTDM_fx;
1277 :
1278 121240 : cldfbSynthesis_ivas_fx( realBufferFlipped, imagBufferFlipped, shb_speech_fx_32, -1, 0, 0, st->cldfbSynTd );
1279 121240 : Copy_Scale_sig_32_16( shb_speech_fx_32, shb_speech, L_FRAME16k, negate( sub( q_reImBuffer, 1 ) ) );
1280 121240 : *Q_shb_spch = 0;
1281 121240 : move16();
1282 : }
1283 :
1284 157043 : IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoICBWE != NULL )
1285 : {
1286 : Word16 exp_t;
1287 35878 : hCPE->hStereoICBWE->MSFlag = 0; /* Init the multi-source flag */
1288 35878 : move16();
1289 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 */ );
1290 :
1291 : // t = log10f( ( hCPE->hStereoICBWE->icbweRefEner + 1e-6f ) / ( lbEner + 1e-6f ) );
1292 35878 : t = L_deposit_h( BASOP_Util_Divide3232_Scale( L_add( hCPE->hStereoICBWE->icbweRefEner_fx, EPSILON_FX ), L_add( lbEner, EPSILON_FX ), &exp_t ) );
1293 35878 : exp_t = add( exp_t, sub( hCPE->hStereoICBWE->icbweRefEner_fx_e, exp_lbEner ) );
1294 35878 : exp = norm_l( t );
1295 35878 : t = L_shl( t, exp );
1296 35878 : t_16 = Log2_norm_lc( t ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1297 35878 : t_16 = sub( shr( t_16, 6 ), shl( add( sub( Q31 - Q30, exp_t ), exp ), 9 ) );
1298 35878 : t = L_mult( t_16, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1299 : /* Three Level Decision Tree to calculate a regression value first */
1300 35878 : IF( LT_32( t, thr[0] ) ) /* level 1 */
1301 : {
1302 26443 : IF( LT_32( t, thr[1] ) ) /* level 2 */
1303 : {
1304 23061 : IF( LT_32( v, thr[3] ) )
1305 : {
1306 0 : regression = regV[0]; /* level 3 */
1307 0 : move32();
1308 : }
1309 : ELSE
1310 : {
1311 23061 : regression = regV[1];
1312 23061 : move32();
1313 : }
1314 : }
1315 : ELSE
1316 : {
1317 3382 : IF( LT_32( v, thr[4] ) )
1318 : {
1319 3382 : regression = regV[2]; /* level 3 */
1320 3382 : move32();
1321 : }
1322 : ELSE
1323 : {
1324 0 : regression = regV[3];
1325 0 : move32();
1326 : }
1327 : }
1328 : }
1329 : ELSE
1330 : {
1331 9435 : IF( LT_32( t, thr[2] ) ) /* level 2 */
1332 : {
1333 2449 : IF( LT_32( v, thr[5] ) )
1334 : {
1335 2449 : regression = regV[4]; /* level 3 */
1336 2449 : move32();
1337 : }
1338 : ELSE
1339 : {
1340 0 : regression = regV[5];
1341 0 : move32();
1342 : }
1343 : }
1344 : ELSE
1345 : {
1346 6986 : IF( LT_32( v, thr[6] ) )
1347 : {
1348 6986 : regression = regV[6]; /* level 3 */
1349 6986 : move32();
1350 : }
1351 : ELSE
1352 : {
1353 0 : regression = regV[7];
1354 0 : move32();
1355 : }
1356 : }
1357 : }
1358 :
1359 35878 : test();
1360 35878 : test();
1361 : /* Convert the regression to a hard decision (classification) */
1362 35878 : IF( GT_32( regression, 1696512081 /* 0.79f in Q31*/ ) && !( LT_16( st->bwidth, SWB ) || hCPE->hCoreCoder[0]->vad_flag == 0 ) )
1363 : {
1364 6005 : hCPE->hStereoICBWE->MSFlag = 1;
1365 6005 : move16();
1366 : }
1367 : }
1368 :
1369 157043 : test();
1370 157043 : test();
1371 157043 : IF( NE_16( st->extl, WB_TBE ) && NE_16( st->extl, SWB_TBE ) && NE_16( st->extl, FB_TBE ) )
1372 : {
1373 : /* Update the previous superwideband speech buffer in case of a SWB_BWE frame - this code is in swb_tbe_enc */
1374 37602 : delay = L_LOOK_16k + L_SUBFR16k;
1375 37602 : move16();
1376 37602 : Copy( shb_speech + sub( L_FRAME16k, delay ), hBWE_TD->old_speech_shb_fx, delay );
1377 : }
1378 : }
1379 : ELSE
1380 : {
1381 238429 : test();
1382 238429 : test();
1383 238429 : IF( ( EQ_16( st->bwidth, FB ) || st->core == ACELP_CORE ) && ( st->element_mode == EVS_MONO ) )
1384 : {
1385 0 : InitSWBencBufferStates_fx( st->hBWE_TD, shb_speech );
1386 : }
1387 : ELSE
1388 : {
1389 238429 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1390 : {
1391 12313 : IF( EQ_16( st->L_frame, L_FRAME ) )
1392 : {
1393 3596 : L_resamp = 560; /* 6.4 kHz core -> 6 - 14 kHz SHB target. 20 ms is 560 samples in 28 kHz sample rate */
1394 3596 : move16();
1395 : }
1396 : ELSE
1397 : {
1398 8717 : L_resamp = 620; /* 8 kHz core -> 7.5 - 15.5 kHz SHB target. 20 ms is 620 samples in 31 kHz sample rate */
1399 8717 : move16();
1400 : }
1401 :
1402 : /* Dirty downsampling to match Nyquist to upper frequency limit of target */
1403 12313 : lerp( st->input_fx, new_swb_speech, L_resamp, extract_l( Mpy_32_32( input_Fs, one_by_50_Q31 ) ) );
1404 :
1405 : /* flip the spectrum */
1406 12313 : Copy( new_swb_speech, spchTmp, L_resamp );
1407 3721463 : FOR( i = 0; i < L_resamp; i = i + 2 )
1408 : {
1409 3709150 : spchTmp[i] = negate( spchTmp[i] );
1410 3709150 : move16();
1411 : }
1412 :
1413 : /* Dirty upsampling to match Nyquist/2 to lower frequency limit of target (reversed spectrum)*/
1414 12313 : lerp( spchTmp, spchTmp2, L_FRAME32k, L_resamp );
1415 12313 : Copy( spchTmp2, spchTmp, L_FRAME32k );
1416 : }
1417 : ELSE
1418 : {
1419 : /* flip the spectrum */
1420 226116 : Copy( new_swb_speech, spchTmp, L_FRAME32k );
1421 :
1422 72583236 : FOR( i = 0; i < L_FRAME32k; i = i + 2 )
1423 : {
1424 72357120 : spchTmp[i] = negate( spchTmp[i] );
1425 72357120 : move16();
1426 : }
1427 : }
1428 :
1429 238429 : Decimate_allpass_steep_fx( spchTmp, hBWE_TD->state_ana_filt_shb_fx, L_FRAME32k, shb_speech );
1430 :
1431 238429 : Copy( shb_speech + L_FRAME16k - ( L_LOOK_16k + L_SUBFR16k ), hBWE_TD->old_speech_shb_fx, L_LOOK_16k + L_SUBFR16k );
1432 :
1433 : /*Compute the past overlap for potential next iDFTs SHB*/
1434 238429 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1435 : {
1436 1736133 : FOR( i = 0; i < STEREO_DFT_OVL_16k; i++ )
1437 : {
1438 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
1439 1723820 : move32();
1440 : }
1441 : }
1442 : }
1443 :
1444 238429 : IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1445 : {
1446 : /* Reset CLDFB synthesis buffer */
1447 226116 : set32_fx( st->cldfbSynTd->cldfb_state_fx, 0, st->cldfbSynTd->p_filter_length );
1448 : }
1449 : ELSE
1450 : {
1451 12313 : hCPE->hStereoDft->flip_sign = negate( hCPE->hStereoDft->flip_sign ); /* Make sure sign is updated even if DFT SHB target is not generated */
1452 12313 : move16();
1453 : }
1454 : }
1455 :
1456 : /* Memory reset to compensate for 0.9375 ms offset when transitioning from IO to SWB */
1457 : /* When switching from n >1 to n = 1, we keep the enc/dec delay as 8.75/3.25 and below code not needed;
1458 : only when n = 1 start, it will be 9.6875/2.3125 in that case this reset is needed for IO->BWE.*/
1459 395472 : test();
1460 395472 : IF( EQ_16( st->last_extl, -1 ) && ( st->element_mode == EVS_MONO ) )
1461 : {
1462 0 : delay = NS2SA_FX2( input_Fs, DELAY_FIR_RESAMPL_NS );
1463 0 : move16();
1464 0 : FOR( i = 0; i < delay; i++ )
1465 : {
1466 0 : shb_speech[i] = mult_r( mult_r( i, 983 /*0.03f Q15*/ ), shb_speech[2 * delay - 1 - i] );
1467 0 : move16();
1468 : }
1469 : }
1470 :
1471 395472 : return;
1472 : }
|