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 43629 : 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 43629 : Word16 fSwitchFromIO = 0;
213 : Word16 ppp_mode;
214 43629 : Word64 temp1 = 0;
215 43629 : Word64 temp2 = 0;
216 : Word32 tmp1, tmp2;
217 : Word16 norm;
218 43629 : move16();
219 43629 : move64();
220 43629 : move64();
221 :
222 :
223 43629 : TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
224 43629 : FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
225 :
226 43629 : 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 43629 : ppp_mode = 0;
234 43629 : move16();
235 : }
236 :
237 43629 : test();
238 43629 : test();
239 43629 : test();
240 43629 : test();
241 43629 : test();
242 43629 : test();
243 43629 : test();
244 43629 : test();
245 87253 : if ( ( EQ_32( st_fx->last_total_brate, ACELP_6k60 ) ) ||
246 87248 : ( EQ_32( st_fx->last_total_brate, ACELP_8k85 ) ) ||
247 87207 : ( EQ_32( st_fx->last_total_brate, ACELP_12k65 ) ) ||
248 87072 : ( EQ_32( st_fx->last_total_brate, ACELP_14k25 ) ) ||
249 86772 : ( EQ_32( st_fx->last_total_brate, ACELP_15k85 ) ) ||
250 86558 : ( EQ_32( st_fx->last_total_brate, ACELP_18k25 ) ) ||
251 86548 : ( EQ_32( st_fx->last_total_brate, ACELP_19k85 ) ) ||
252 86460 : ( EQ_32( st_fx->last_total_brate, ACELP_23k05 ) ) ||
253 43187 : ( EQ_32( st_fx->last_total_brate, ACELP_23k85 ) ) )
254 : {
255 513 : fSwitchFromIO = 1;
256 513 : move16();
257 : }
258 :
259 43629 : 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 43629 : max_wb = 1;
262 43629 : move16();
263 14004909 : FOR( i = 0; i < L_FRAME16k; i++ )
264 : {
265 13961280 : max_wb = s_max( max_wb, abs_s( new_inp_resamp16k[i] ) );
266 : }
267 43629 : Q_wb_sp = norm_s( max_wb );
268 :
269 43629 : Copy_Scale_sig( new_inp_resamp16k, temp_buf, L_FRAME16k, -3 );
270 43629 : temp1 = W_mac_32_32( temp1, temp_buf[0], temp_buf[0] );
271 13961280 : FOR( i = 1; i < L_FRAME16k; i++ )
272 : {
273 13917651 : temp1 = W_mac_32_32( temp1, temp_buf[i], temp_buf[i] );
274 13917651 : temp2 = W_mac_32_32( temp2, temp_buf[i - 1], temp_buf[i] );
275 : }
276 43629 : norm = s_min( W_norm( temp1 ), W_norm( temp2 ) );
277 43629 : tmp1 = W_extract_h( W_shl( temp1, norm ) );
278 43629 : tmp2 = W_extract_h( W_shl( temp2, norm ) );
279 :
280 43629 : IF( LT_32( tmp2, Mpy_32_16_1( tmp1, 31129 /*0.95f*/ ) ) )
281 : {
282 12098 : Q_wb_sp = sub( Q_wb_sp, 3 ); /* leave 3 bit headroom */
283 : }
284 43629 : Copy_Scale_sig( new_inp_resamp16k, temp_buf, L_FRAME16k, Q_wb_sp );
285 43629 : Scale_sig( hBWE_TD->decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), Q_wb_sp );
286 43629 : 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 43629 : test();
290 43629 : test();
291 43629 : IF( EQ_16( st_fx->extl, WB_BWE ) || EQ_16( st_fx->extl, WB_TBE ) || st_fx->igf )
292 : {
293 21099 : ramp_flag = 0;
294 21099 : move16();
295 :
296 21099 : test();
297 21099 : test();
298 21099 : test();
299 21099 : test();
300 21099 : 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 563 : ramp_flag = 1;
304 563 : move16();
305 : }
306 :
307 21099 : 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 21099 : IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) )
312 : {
313 5030 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
314 :
315 5030 : 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 5030 : 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 5030 : 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 5030 : Copy( temp_buf, &old_input[Sample_Delay_WB_BWE + STEREO_DFT_OVL_16k], L_FRAME16k );
329 :
330 : /*compute hb_speech on delayed input*/
331 5030 : 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 5030 : Copy_Scale_sig( hb_speech, hBWE_TD->old_speech_wb_fx + ( L_SUBFR * 5 / 16 ), STEREO_DFT_OVL_16k / 4, -Q_wb_sp );
335 5030 : Copy( hb_speech + STEREO_DFT_OVL_16k / 4, hb_speech, ( L_FRAME16k - STEREO_DFT_OVL_16k ) / 4 );
336 :
337 : /*rest without memory update*/
338 5030 : Copy( hBWE_TD->decim_state1_fx, decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
339 5030 : Copy( hBWE_TD->decim_state2_fx, decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
340 :
341 5030 : 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 16069 : ELSE IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) )
344 : {
345 1243 : Word16 l_recalc_16k = L_MEM_RECALC_16K + L_FILT16k + 1; /* Note: "+1" is used because L_FILT16k is not divisible by 4 */
346 1243 : move16();
347 1243 : Word16 l_recalc_4k = ( L_MEM_RECALC_16K + L_FILT16k + 1 ) / 4;
348 1243 : move16();
349 1243 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
350 :
351 1243 : IF( EQ_16( last_element_mode, IVAS_CPE_DFT ) )
352 : {
353 19 : set16_fx( hBWE_TD->decim_state1_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
354 19 : set16_fx( hBWE_TD->decim_state2_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
355 19 : ramp_flag = 1;
356 19 : move16();
357 : }
358 :
359 : /*Get past signal*/
360 1243 : Copy_Scale_sig( hBWE_FD->L_old_wtda_swb_fx + L_FRAME16k - l_recalc_16k, old_input, l_recalc_16k, Q_wb_sp );
361 1243 : 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 1243 : old_input[Sample_Delay_WB_BWE] = shl_sat( hBWE_FD->mem_old_wtda_swb_fx, Q_wb_sp );
365 :
366 : /*Get new signal*/
367 1243 : Copy( temp_buf, old_input + Sample_Delay_WB_BWE + l_recalc_16k, L_FRAME16k );
368 :
369 : /*compute hb_speech on delayed input*/
370 1243 : 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 1243 : 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 1243 : Copy( hb_speech + l_recalc_4k, hb_speech, ( L_FRAME16k / 4 ) - l_recalc_4k );
375 :
376 : /*rest without memory update*/
377 1243 : Copy( hBWE_TD->decim_state1_fx, decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
378 1243 : Copy( hBWE_TD->decim_state2_fx, decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
379 :
380 1243 : 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 14826 : ELSE IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
383 : {
384 14826 : 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 14826 : Word16 l_recalc_16k = L_FILT16k + 1; /* Note: "+1" is used because L_FILT16k is not divisible by 4 */
391 14826 : Word16 l_recalc_4k = ( L_FILT16k + 1 ) / 4;
392 :
393 14826 : Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
394 :
395 : /*Get past signal*/
396 14826 : Copy_Scale_sig( hBWE_FD->L_old_wtda_swb_fx + L_FRAME16k - l_recalc_16k, old_input, l_recalc_16k, Q_wb_sp );
397 14826 : 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 14826 : Copy( temp_buf, old_input + Sample_Delay_WB_BWE + l_recalc_16k, L_FRAME16k );
401 :
402 : /*compute hb_speech on delayed input*/
403 14826 : 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 14826 : 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 14826 : Copy( hb_speech + l_recalc_4k, hb_speech, ( L_FRAME16k / 4 ) - l_recalc_4k );
408 :
409 : /*rest without memory update*/
410 14826 : Copy( hBWE_TD->decim_state1_fx, decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
411 14826 : Copy( hBWE_TD->decim_state2_fx, decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
412 :
413 14826 : 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 21099 : Scale_sig( hb_speech, L_FRAME16k / 4, -Q_wb_sp );
423 21099 : Scale_sig( hBWE_TD->decim_state1_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), -Q_wb_sp );
424 21099 : Scale_sig( hBWE_TD->decim_state2_fx, ( 2 * ALLPASSSECTIONS_STEEP + 1 ), -Q_wb_sp );
425 :
426 21099 : IF( NE_16( st_fx->extl, WB_TBE ) )
427 : {
428 : /* Update the previous wideband speech buffer in case of a WB_BWE frame */
429 13756 : Sample_Delay_WB_BWE = ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16;
430 :
431 13756 : highband_new_speech = highband_old_speech + Sample_Delay_WB_BWE;
432 13756 : Copy( hb_speech, highband_new_speech, L_FRAME16k / 4 );
433 13756 : Copy( highband_old_speech + L_FRAME16k / 4, hBWE_TD->old_speech_wb_fx, Sample_Delay_WB_BWE );
434 : }
435 : }
436 : }
437 : ELSE
438 : {
439 22530 : set16_fx( hBWE_TD->decim_state1_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
440 22530 : set16_fx( hBWE_TD->decim_state2_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
441 22530 : set16_fx( hBWE_TD->old_speech_wb_fx, 0, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
442 : }
443 :
444 43629 : if ( hBWE_FD != NULL )
445 : {
446 43629 : hBWE_FD->mem_old_wtda_swb_fx = new_inp_resamp16k[L_FRAME16k - L_MEM_RECALC_16K - L_FILT16k - 1];
447 43629 : 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 43629 : test();
452 43629 : test();
453 43629 : test();
454 43629 : 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 43629 : 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 1043 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
552 1043 : Copy( hBWE_FD->old_fdbwe_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], L_FRAME32k );
553 1043 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
554 1043 : Copy( hBWE_FD->old_fdbwe_speech_fx + L_FRAME32k - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
555 :
556 1043 : IF( sub( st_fx->extl, WB_BWE ) != 0 )
557 : {
558 1043 : 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 1043 : 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 1044 : test();
574 1044 : test();
575 1044 : 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 370 : test();
583 370 : 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 370 : 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 370 : inner_frame = L_FRAME32k;
595 370 : inner_Fs = 32000;
596 370 : 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 370 : 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 370 : move16();
600 370 : move16();
601 :
602 370 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
603 370 : Copy( new_swb_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], inner_frame );
604 370 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
605 370 : Copy( new_swb_speech_fx + inner_frame - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
606 370 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, inner_frame );
607 370 : st_fx->Q_old_wtda = 0;
608 370 : move16();
609 : }
610 : /* resample 48 kHz to 32kHz */
611 370 : IF( EQ_16( st_fx->bwidth, FB ) )
612 : {
613 0 : Copy( input_fx, new_swb_speech_fx, L_FRAME48k );
614 : }
615 : ELSE
616 : {
617 370 : 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 370 : 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 1006 : IF( EQ_16( st_fx->bwidth, FB ) )
626 : {
627 0 : Copy( input_fx, new_swb_speech_fx, L_FRAME48k );
628 : }
629 : ELSE
630 : {
631 1006 : 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 1006 : 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 1357 : 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 604 : startB = 39;
677 604 : move16();
678 604 : endB = 19;
679 604 : move16();
680 10268 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
681 : {
682 9664 : uB = 0;
683 9664 : move16();
684 202944 : FOR( nB = startB; nB > endB; nB-- )
685 : {
686 193280 : realBufferFlipped[ts][uB] = L_negate( realBuffer[ts][nB] );
687 193280 : move32();
688 193280 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
689 193280 : move32();
690 193280 : 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 1357 : CldfbHB_ener = 0;
698 1357 : move32();
699 14927 : FOR( nB = 0; nB < 10; nB++ )
700 : {
701 230690 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
702 : {
703 217120 : realQ_neg1 = extract_l( L_shr( realBufferFlipped[ts][nB], 31 - ( 15 + cldfbScale->hb_scale ) + 1 ) );
704 217120 : imagQ_neg1 = extract_l( L_shr( imagBufferFlipped[ts][nB], 31 - ( 15 + cldfbScale->hb_scale ) + 1 ) ); /* Q(-1), headroom needed */
705 :
706 217120 : CldfbHB_ener = L_mac0_o( CldfbHB_ener, realQ_neg1, realQ_neg1, &Overflow );
707 217120 : CldfbHB_ener = L_mac0_o( CldfbHB_ener, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
708 : }
709 : }
710 :
711 1357 : exp = norm_l( CldfbHB_ener );
712 1357 : CldfbHB_ener = L_shl( CldfbHB_ener, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
713 1357 : Cldfbtemp1 = ( Log2_norm_lc( CldfbHB_ener ) );
714 1357 : 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 1357 : 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 1357 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_ener, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
717 : }
718 1357 : cldfbSynthesis_fx( st_fx->cldfbSynTd, realBufferFlipped, imagBufferFlipped, cldfbScale, shb_speech_fx, 0, CLDFB_NO_COL_MAX, cldfbWorkBuffer );
719 1357 : *Q_shb_spch = 0; /*shb_speech_fx : Q0*/
720 1357 : move16();
721 :
722 1357 : test();
723 1357 : test();
724 1357 : 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 7 : delay = L_LOOK_16k + L_SUBFR16k;
728 7 : Copy( shb_speech_fx + L_FRAME16k - delay, hBWE_TD->old_speech_shb_fx, delay );
729 : }
730 : }
731 : ELSE
732 : {
733 1743 : test();
734 1743 : 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 1069 : Copy( new_swb_speech_fx, spchTmp_fx, L_FRAME32k );
743 :
744 343149 : FOR( i = 0; i < L_FRAME32k; i = i + 2 )
745 : {
746 342080 : spchTmp_fx[i] = negate( spchTmp_fx[i] );
747 342080 : move16();
748 : }
749 :
750 1069 : Decimate_allpass_steep_fx( spchTmp_fx, hBWE_TD->state_ana_filt_shb_fx, L_FRAME32k, shb_speech_fx );
751 1069 : 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 1743 : 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 405 : delay = NS2SA_FX2( st_fx->input_Fs, DELAY_FIR_RESAMPL_NS );
760 18090 : FOR( i = 0; i < delay; i++ )
761 : {
762 17685 : shb_speech_fx[i] = mult_r( mult_r( i, 983 /*0.03f Q15*/ ), shb_speech_fx[2 * delay - 1 - i] );
763 17685 : 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 395439 : 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 - Q0 */
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 395439 : Word16 exp_lbEner = 0;
811 : Word16 Sample_Delay_SWB_BWE32k, lMemRecalc32k, dft_ovl32k;
812 395439 : Word32 one_by_50_Q31 = 42949673;
813 :
814 395439 : lMemRecalc32k = NS2SA( 32000, L_MEM_RECALC_NS );
815 395439 : move16(); // exp_lbEner
816 395439 : move16(); // lMemRecalc32k
817 395439 : move32(); // one_by_50_Q31
818 :
819 : /* initialization */
820 395439 : hBWE_TD = st->hBWE_TD;
821 395439 : hBWE_FD = st->hBWE_FD;
822 395439 : input_Fs = st->input_Fs;
823 :
824 6722463 : FOR( j = 0; j < CLDFB_NO_COL_MAX; j++ )
825 : {
826 6327024 : set32_fx( realBufferTmp[j], 0, 20 );
827 6327024 : set32_fx( imagBufferTmp[j], 0, 20 );
828 6327024 : realBufferFlipped[j] = realBufferTmp[j];
829 6327024 : imagBufferFlipped[j] = imagBufferTmp[j];
830 : }
831 :
832 395439 : set16_fx( old_input_fx, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k );
833 :
834 395439 : IF( EQ_32( st->input_Fs, 32000 ) )
835 : {
836 57474 : IF( st->element_mode > EVS_MONO )
837 : {
838 57474 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS );
839 57474 : IF( EQ_16( st->L_frame, L_FRAME16k ) )
840 : {
841 32468 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS );
842 : }
843 :
844 57474 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
845 57474 : Copy( st->input_fx - L_FRAME32k, hBWE_FD->old_fdbwe_speech_fx, L_FRAME32k );
846 :
847 57474 : test();
848 57474 : test();
849 57474 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) && GE_16( st->bwidth, SWB ) )
850 : {
851 3974 : 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 ) );
852 : }
853 53500 : ELSE IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && GE_16( st->bwidth, SWB ) )
854 : {
855 26886 : 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 ) );
856 : }
857 : }
858 :
859 57474 : Copy( st->input_fx, new_swb_speech, L_FRAME32k );
860 :
861 57474 : test();
862 57474 : test();
863 57474 : IF( NE_16( st->last_extl, SWB_BWE ) && NE_16( st->last_extl, FB_BWE ) && NE_16( st->extl, SWB_BWE_HIGHRATE ) )
864 : {
865 56593 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
866 56593 : test();
867 56593 : IF( st->element_mode > EVS_MONO && EQ_16( st->L_frame, L_FRAME16k ) )
868 : {
869 32342 : Sample_Delay_SWB_BWE = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
870 : }
871 56593 : IF( st->element_mode > EVS_MONO )
872 : {
873 56593 : Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA( 32000, DELAY_FIR_RESAMPL_NS ) );
874 : }
875 :
876 56593 : Copy( hBWE_FD->old_fdbwe_speech_fx, &old_input_fx[Sample_Delay_SWB_BWE], L_FRAME32k );
877 :
878 56593 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
879 56593 : Copy( hBWE_FD->old_fdbwe_speech_fx + L_FRAME32k - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
880 56593 : IF( NE_16( st->extl, WB_BWE ) )
881 : {
882 56558 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, L_FRAME32k );
883 : }
884 : }
885 :
886 57474 : test();
887 57474 : IF( NE_16( st->extl, SWB_BWE ) && NE_16( st->extl, FB_BWE ) )
888 : {
889 56585 : Copy( st->input_fx, hBWE_FD->old_fdbwe_speech_fx, L_FRAME32k );
890 : }
891 : }
892 : ELSE /* 48 kHz */
893 : {
894 :
895 337965 : Sample_Delay_SWB_BWE32k = NS2SA( 32000, DELAY_FD_BWE_ENC_12k8_NS );
896 337965 : move16();
897 337965 : Sample_Delay_SWB_BWE = NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS );
898 337965 : move16();
899 337965 : IF( EQ_16( st->L_frame, L_FRAME16k ) )
900 : {
901 118796 : Sample_Delay_SWB_BWE32k = NS2SA( 32000, DELAY_FD_BWE_ENC_16k_NS );
902 118796 : move16();
903 118796 : Sample_Delay_SWB_BWE = NS2SA( 48000, DELAY_FD_BWE_ENC_16k_NS );
904 118796 : move16();
905 : }
906 :
907 337965 : dft_ovl32k = 0;
908 337965 : move16();
909 337965 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
910 : {
911 20925 : dft_ovl32k = (Word16) ( STEREO_DFT_OVL_MAX * 32000 / 48000 );
912 20925 : move16();
913 : }
914 :
915 337965 : IF( EQ_16( st->codec_mode, MODE1 ) )
916 : {
917 337965 : IF( st->element_mode > EVS_MONO )
918 : {
919 :
920 337965 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
921 : {
922 : }
923 336009 : ELSE IF( EQ_16( st->bwidth, FB ) )
924 : {
925 210234 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
926 : }
927 :
928 337965 : Copy( st->input_fx - L_FRAME48k, hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k );
929 :
930 337965 : test();
931 337965 : test();
932 337965 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) && GE_16( st->bwidth, SWB ) )
933 : {
934 1838 : IF( EQ_16( st->bwidth, SWB ) )
935 : {
936 : /* buffers hBWE_FD->old_input[] and hBWE_FD->old_wtda_swb[] need to be at 32 kHz (inner) sampling rate */
937 :
938 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,
939 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 );
940 :
941 439 : Copy( spchTmp, hBWE_FD->L_old_wtda_swb_fx + L_FRAME32k - sub( lMemRecalc32k, Sample_Delay_SWB_BWE32k ), sub( lMemRecalc32k, Sample_Delay_SWB_BWE32k ) );
942 439 : Copy( spchTmp + lMemRecalc32k - Sample_Delay_SWB_BWE32k, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE32k );
943 : }
944 : ELSE /* FB_BWE */
945 : {
946 1399 : 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 ) );
947 1399 : Copy( st->input_fx - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
948 : }
949 : }
950 336127 : ELSE IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && GE_16( st->bwidth, SWB ) )
951 : {
952 19904 : IF( EQ_16( st->bwidth, SWB ) )
953 : {
954 10041 : lerp( st->input_fx - hCPE->hStereoDft->dft_ovl, spchTmp, dft_ovl32k - Sample_Delay_SWB_BWE32k, hCPE->hStereoDft->dft_ovl - Sample_Delay_SWB_BWE );
955 :
956 10041 : Copy( spchTmp, hBWE_FD->L_old_wtda_swb_fx + L_FRAME32k - ( dft_ovl32k - Sample_Delay_SWB_BWE32k ), dft_ovl32k - Sample_Delay_SWB_BWE32k );
957 : }
958 : ELSE
959 : {
960 9863 : 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 );
961 : }
962 : }
963 : }
964 :
965 337965 : test();
966 337965 : test();
967 337965 : test();
968 337965 : test();
969 337965 : 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*/ )
970 : {
971 : /* move the resampling out of the TDBWE path as new_swb_speech is not needed for TDBWE. */
972 114532 : Copy( st->input_fx, hBWE_FD->old_fdbwe_speech_fx, L_FRAME48k );
973 : }
974 : ELSE
975 : {
976 223433 : test();
977 223433 : IF( NE_16( st->last_extl, SWB_BWE ) && NE_16( st->last_extl, FB_BWE ) )
978 : {
979 : /* resample 48 kHz to 32kHz */
980 213543 : IF( EQ_16( st->last_bwidth, FB ) )
981 : {
982 157912 : inner_frame = L_FRAME48k;
983 157912 : move16();
984 157912 : inner_Fs = 48000;
985 157912 : move32();
986 157912 : Copy( hBWE_FD->old_fdbwe_speech_fx, new_swb_speech, L_FRAME48k );
987 : }
988 : ELSE
989 : {
990 55631 : inner_frame = L_FRAME32k;
991 55631 : move16();
992 55631 : inner_Fs = 32000;
993 55631 : move32();
994 :
995 55631 : IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
996 : {
997 55454 : 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 );
998 : }
999 : ELSE
1000 : {
1001 177 : lerp( hBWE_FD->old_fdbwe_speech_fx, new_swb_speech, inner_frame, L_FRAME48k );
1002 : }
1003 :
1004 55631 : test();
1005 55631 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && st->idchan == 0 )
1006 : {
1007 49737 : FOR( i = 0; i < STEREO_DFT_OVL_32k; i++ )
1008 : {
1009 49560 : 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
1010 49560 : move32();
1011 : }
1012 : }
1013 : }
1014 :
1015 213543 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
1016 213543 : move16();
1017 213543 : test();
1018 213543 : IF( st->element_mode > EVS_MONO && EQ_16( st->L_frame, L_FRAME16k ) )
1019 : {
1020 60690 : Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
1021 60690 : move16();
1022 : }
1023 213543 : IF( st->element_mode > EVS_MONO )
1024 : {
1025 213543 : Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) );
1026 : }
1027 :
1028 213543 : Copy( new_swb_speech, &old_input_fx[Sample_Delay_SWB_BWE], inner_frame );
1029 213543 : set16_fx( old_input_fx, 0, Sample_Delay_SWB_BWE );
1030 213543 : Copy( new_swb_speech + inner_frame - Sample_Delay_SWB_BWE, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
1031 213543 : Copy( old_input_fx, hBWE_FD->L_old_wtda_swb_fx, inner_frame );
1032 : }
1033 :
1034 : /* resample 48 kHz to 32kHz */
1035 223433 : IF( EQ_16( st->bwidth, FB ) )
1036 : {
1037 162566 : Copy( st->input_fx, new_swb_speech, L_FRAME48k );
1038 : }
1039 : ELSE
1040 : {
1041 60867 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
1042 : {
1043 : Word16 dec_2_over_3_mem_tmp[L_FILT_2OVER3], dec_2_over_3_mem_lp_tmp[L_FILT_2OVER3_LP];
1044 :
1045 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 );
1046 :
1047 16 : Copy( hBWE_TD->dec_2_over_3_mem_fx, dec_2_over_3_mem_tmp, L_FILT_2OVER3 );
1048 16 : Copy( hBWE_TD->dec_2_over_3_mem_lp_fx, dec_2_over_3_mem_lp_tmp, L_FILT_2OVER3_LP );
1049 :
1050 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 );
1051 : }
1052 60851 : ELSE IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1053 : {
1054 59357 : 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 );
1055 : }
1056 : ELSE /* IVAS_CPE_DFT */
1057 : {
1058 : Word16 out_start_ind, out_end_ind;
1059 1494 : stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, new_swb_speech_fx, &out_start_ind, &out_end_ind, st->idchan, input_Fs, 32000, 0, NULL );
1060 1494 : 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( q_reImBuffer, 1 ) ); // Q0
1061 1494 : Copy( new_swb_speech - Sample_Delay_SWB_BWE32k, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE32k ); // Q0
1062 : }
1063 : }
1064 : }
1065 : }
1066 : ELSE
1067 : {
1068 : /* resample 48 kHz to 32kHz */
1069 0 : IF( EQ_16( st->bwidth, FB ) )
1070 : {
1071 0 : Copy( st->input_fx, new_swb_speech, L_FRAME48k );
1072 : }
1073 : ELSE
1074 : {
1075 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 );
1076 : }
1077 : }
1078 : }
1079 :
1080 395439 : test();
1081 395439 : test();
1082 395439 : test();
1083 395439 : test();
1084 395439 : test();
1085 395439 : test();
1086 395439 : IF( ( ( st->core == ACELP_CORE ) && NE_16( st->extl, SWB_BWE_HIGHRATE ) && NE_16( st->extl, FB_BWE_HIGHRATE ) ) ||
1087 : ( ( EQ_32( st->total_brate, ACELP_9k60 ) || st->rf_mode ) && EQ_16( st->bwidth, SWB ) && ( st->element_mode == EVS_MONO ) ) )
1088 157402 : {
1089 157402 : Word32 CldfbHB_fx = 0; // fixed counterpart
1090 157402 : Word16 CldfbHB_fx_e = 0; // fixed counterpart
1091 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1092 157402 : Flag Overflow = 0;
1093 157402 : move16();
1094 : #endif
1095 157402 : Word16 realQ_neg1, imagQ_neg1, exp = 0, Cldfbtemp1;
1096 157402 : move32();
1097 157402 : move16();
1098 157402 : move16();
1099 :
1100 157402 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1101 : {
1102 : Word16 out_start_ind, out_end_ind;
1103 : Word16 exp_icbweRefEner_fx;
1104 35633 : 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
1105 35633 : 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 );
1106 35633 : 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
1107 :
1108 : /* delay corresponding to CLDFB delay */
1109 35633 : Copy( old_input_fx + STEREO_DFT_OVL_16k - 20, shb_speech, L_FRAME16k );
1110 35633 : 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 );
1111 35633 : Copy( old_input_fx, hCPE->hStereoICBWE->mem_shb_speech_ref_fx, STEREO_DFT_OVL_16k - 20 ); // Q0
1112 35633 : hCPE->hStereoICBWE->mem_shb_speech_ref_e = 15;
1113 35633 : move16();
1114 :
1115 35633 : IF( LE_32( CldfbHB_fx, 0 ) )
1116 : {
1117 1088 : CldfbHB_fx = EPSILON_FX;
1118 1088 : move32();
1119 : }
1120 35633 : CldfbHB_fx = L_shl( CldfbHB_fx, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
1121 35633 : Cldfbtemp1 = Log2_norm_lc( CldfbHB_fx ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1122 35633 : Cldfbtemp1 = sub( shr( Cldfbtemp1, 6 ), shl( add( sub( Q31 - Q30, CldfbHB_fx_e ), exp ), 9 ) );
1123 35633 : CldfbHB_fx = L_mult( Cldfbtemp1, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1124 35633 : CldfbHB_fx = Mpy_32_16_1( L_sub( CldfbHB_fx, 1 << Q25 ), 11611 /* 0.1 * 0.221462 in Q19 */ );
1125 : /* Resulting Q = Q25 + Q19 - Q15 = Q29 */
1126 35633 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_fx, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
1127 35633 : move16();
1128 :
1129 35633 : exp_lbEner = hCPE->hStereoDft->lbEner_fx_e;
1130 35633 : move32();
1131 35633 : lbEner = Mpy_32_16_1( Sqrt32( hCPE->hStereoDft->lbEner_fx, &exp_lbEner ), 1638 /* 0.05 in Q15 */ );
1132 35633 : exp_icbweRefEner_fx = hCPE->hStereoDft->icbweRefEner_fx_e;
1133 35633 : move16();
1134 35633 : hCPE->hStereoICBWE->icbweRefEner_fx = Mpy_32_16_1( Sqrt32( hCPE->hStereoDft->icbweRefEner_fx, &exp_icbweRefEner_fx ), 1638 /* 0.05 in Q15 */ );
1135 35633 : move32();
1136 35633 : hCPE->hStereoICBWE->icbweRefEner_fx_e = exp_icbweRefEner_fx;
1137 35633 : move16();
1138 : // lbEner = 0.05f * (float) sqrt( hCPE->hStereoDft->lbEner );
1139 35633 : thr = icbwe_thr_DFT_fx;
1140 35633 : regV = icbwe_regressionValuesDFT_fx;
1141 : }
1142 : ELSE
1143 : {
1144 121769 : IF( EQ_16( st->L_frame, L_FRAME ) )
1145 : {
1146 67131 : startB = 34;
1147 67131 : move16();
1148 67131 : endB = 14;
1149 67131 : move16();
1150 1141227 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1151 : {
1152 1074096 : uB = 0;
1153 1074096 : move16();
1154 22556016 : FOR( nB = startB; nB > endB; nB-- )
1155 : {
1156 21481920 : realBufferFlipped[ts][uB] = realBuffer[ts][nB];
1157 21481920 : move32();
1158 21481920 : if ( s_and( ts, 1 ) != 0 )
1159 : {
1160 10740960 : realBufferFlipped[ts][uB] = L_negate( realBufferFlipped[ts][uB] );
1161 10740960 : move32();
1162 : }
1163 21481920 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
1164 21481920 : move32();
1165 21481920 : if ( s_and( ts, 1 ) == 0 )
1166 : {
1167 10740960 : imagBufferFlipped[ts][uB] = L_negate( imagBufferFlipped[ts][uB] );
1168 10740960 : move32();
1169 : }
1170 21481920 : uB = add( uB, 1 ); /* uB ++ */
1171 : }
1172 : }
1173 : }
1174 : ELSE
1175 : {
1176 54638 : startB = 39;
1177 54638 : move16();
1178 54638 : endB = 19;
1179 54638 : move16();
1180 928846 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1181 : {
1182 874208 : uB = 0;
1183 874208 : move16();
1184 18358368 : FOR( nB = startB; nB > endB; nB-- )
1185 : {
1186 17484160 : realBufferFlipped[ts][uB] = L_negate( realBuffer[ts][nB] );
1187 17484160 : move32();
1188 17484160 : imagBufferFlipped[ts][uB] = imagBuffer[ts][nB];
1189 17484160 : move32();
1190 17484160 : uB = add( uB, 1 ); /* uB ++ */
1191 : }
1192 : }
1193 : }
1194 :
1195 1339459 : FOR( nB = 0; nB < 10; nB++ )
1196 : {
1197 20700730 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1198 : {
1199 19483040 : realQ_neg1 = extract_l( L_shr( realBufferFlipped[ts][nB], 16 ) );
1200 19483040 : imagQ_neg1 = extract_l( L_shr( imagBufferFlipped[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1201 :
1202 19483040 : CldfbHB_fx = L_mac0_o( CldfbHB_fx, realQ_neg1, realQ_neg1, &Overflow );
1203 19483040 : CldfbHB_fx = L_mac0_o( CldfbHB_fx, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1204 : }
1205 : }
1206 121769 : CldfbHB_fx_e = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1207 121769 : exp = norm_l( CldfbHB_fx );
1208 121769 : CldfbHB_fx = L_shl( CldfbHB_fx, exp ); /* CldfbHB_ener = CldfbHB_fl*2^(exp) */
1209 121769 : Cldfbtemp1 = Log2_norm_lc( CldfbHB_fx ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1210 121769 : Cldfbtemp1 = sub( shr( Cldfbtemp1, 6 ), shl( add( sub( Q31 - Q30, CldfbHB_fx_e ), exp ), 9 ) );
1211 121769 : CldfbHB_fx = L_mult( Cldfbtemp1, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1212 121769 : CldfbHB_fx = Mpy_32_16_1( L_sub( CldfbHB_fx, 1 << Q25 ), 11611 /* 0.1 * 0.221462 in Q19 */ );
1213 : /* Resulting Q = Q25 + Q19 - Q15 = Q29 */
1214 121769 : hBWE_TD->cldfbHBLT = mac_r( CldfbHB_fx, 29491 /*0.9 Q15*/, hBWE_TD->cldfbHBLT ); /* cldfbHBLT is in Q13 */
1215 121769 : move32();
1216 :
1217 121769 : IF( CldfbHB_fx == 0 )
1218 : {
1219 0 : CldfbHB_fx = EPSILLON_FX;
1220 0 : move32();
1221 : }
1222 :
1223 121769 : test();
1224 121769 : IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoICBWE != NULL )
1225 : {
1226 75 : Word32 icbweRefEner_fx = EPSILON_FX;
1227 75 : Word16 exp_icbweRefEner_fx = 0;
1228 :
1229 75 : move32();
1230 75 : move16();
1231 1575 : FOR( nB = 20; nB < 40; nB++ )
1232 : {
1233 25500 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1234 : {
1235 24000 : realQ_neg1 = extract_l( L_shr( realBuffer[ts][nB], 16 ) );
1236 24000 : imagQ_neg1 = extract_l( L_shr( imagBuffer[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1237 24000 : icbweRefEner_fx = L_mac0_o( icbweRefEner_fx, realQ_neg1, realQ_neg1, &Overflow );
1238 24000 : icbweRefEner_fx = L_mac0_o( icbweRefEner_fx, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1239 : }
1240 : }
1241 75 : exp_icbweRefEner_fx = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1242 75 : hCPE->hStereoICBWE->icbweRefEner_fx = Mpy_32_16_1( Sqrt32( icbweRefEner_fx, &exp_icbweRefEner_fx ), 1638 /* 0.05 in Q15 */ );
1243 75 : move32();
1244 75 : hCPE->hStereoICBWE->icbweRefEner_fx_e = exp_icbweRefEner_fx;
1245 75 : move16();
1246 : }
1247 :
1248 121769 : lbEner = EPSILON_FX;
1249 121769 : move32();
1250 2557149 : FOR( nB = 0; nB < 20; nB++ )
1251 : {
1252 41401460 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
1253 : {
1254 38966080 : realQ_neg1 = extract_l( L_shr( realBuffer[ts][nB], 16 ) );
1255 38966080 : imagQ_neg1 = extract_l( L_shr( imagBuffer[ts][nB], 16 ) ); /* Q(-1), headroom needed */
1256 :
1257 38966080 : lbEner = L_mac0_o( lbEner, realQ_neg1, realQ_neg1, &Overflow );
1258 38966080 : lbEner = L_mac0_o( lbEner, imagQ_neg1, imagQ_neg1, &Overflow ); /* Q(-2) */
1259 : }
1260 : }
1261 121769 : exp_lbEner = sub( Q31, shl( sub( q_reImBuffer, Q16 ), 1 ) );
1262 121769 : lbEner = Mpy_32_16_1( Sqrt32( lbEner, &exp_lbEner ), 1638 /* 0.05 in Q15 */ );
1263 121769 : thr = icbwe_thr_TDM_fx;
1264 121769 : regV = icbwe_regressionValuesTDM_fx;
1265 :
1266 : #ifdef OPT_AVOID_STATE_BUF_RESCALE
1267 121769 : cldfbSynthesis_ivas_fx( realBufferFlipped, imagBufferFlipped, shb_speech_fx_32, -1, 0, 0, st->cldfbSynTd );
1268 : #else /* OPT_AVOID_STATE_BUF_RESCALE */
1269 : cldfbSynthesis_ivas_fx( realBufferFlipped, imagBufferFlipped, shb_speech_fx_32, -1, 0, st->cldfbSynTd );
1270 : #endif /* OPT_AVOID_STATE_BUF_RESCALE */
1271 121769 : Copy_Scale_sig_32_16( shb_speech_fx_32, shb_speech, L_FRAME16k, negate( sub( q_reImBuffer, 1 ) ) );
1272 121769 : *Q_shb_spch = 0;
1273 121769 : move16();
1274 : }
1275 :
1276 157402 : IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoICBWE != NULL )
1277 : {
1278 : Word16 exp_t;
1279 35708 : hCPE->hStereoICBWE->MSFlag = 0; /* Init the multi-source flag */
1280 35708 : move16();
1281 35708 : 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 */ );
1282 :
1283 : // t = log10f( ( hCPE->hStereoICBWE->icbweRefEner + 1e-6f ) / ( lbEner + 1e-6f ) );
1284 35708 : t = L_deposit_h( BASOP_Util_Divide3232_Scale( L_add( hCPE->hStereoICBWE->icbweRefEner_fx, EPSILON_FX ), L_add( lbEner, EPSILON_FX ), &exp_t ) );
1285 35708 : exp_t = add( exp_t, sub( hCPE->hStereoICBWE->icbweRefEner_fx_e, exp_lbEner ) );
1286 35708 : exp = norm_l( t );
1287 35708 : t = L_shl( t, exp );
1288 35708 : t_16 = Log2_norm_lc( t ); /* Log2_norm_lc(t) = 2^15*(log2(t/2^30)) */
1289 35708 : t_16 = sub( shr( t_16, 6 ), shl( add( sub( Q31 - Q30, exp_t ), exp ), 9 ) );
1290 35708 : t = L_mult( t_16, 9864 ); /* Q9 + Q15 + Q1 = Q25 */
1291 : /* Three Level Decision Tree to calculate a regression value first */
1292 35708 : IF( LT_32( t, thr[0] ) ) /* level 1 */
1293 : {
1294 26347 : IF( LT_32( t, thr[1] ) ) /* level 2 */
1295 : {
1296 22981 : IF( LT_32( v, thr[3] ) )
1297 : {
1298 0 : regression = regV[0]; /* level 3 */
1299 0 : move32();
1300 : }
1301 : ELSE
1302 : {
1303 22981 : regression = regV[1];
1304 22981 : move32();
1305 : }
1306 : }
1307 : ELSE
1308 : {
1309 3366 : IF( LT_32( v, thr[4] ) )
1310 : {
1311 3366 : regression = regV[2]; /* level 3 */
1312 3366 : move32();
1313 : }
1314 : ELSE
1315 : {
1316 0 : regression = regV[3];
1317 0 : move32();
1318 : }
1319 : }
1320 : }
1321 : ELSE
1322 : {
1323 9361 : IF( LT_32( t, thr[2] ) ) /* level 2 */
1324 : {
1325 2434 : IF( LT_32( v, thr[5] ) )
1326 : {
1327 2434 : regression = regV[4]; /* level 3 */
1328 2434 : move32();
1329 : }
1330 : ELSE
1331 : {
1332 0 : regression = regV[5];
1333 0 : move32();
1334 : }
1335 : }
1336 : ELSE
1337 : {
1338 6927 : IF( LT_32( v, thr[6] ) )
1339 : {
1340 6927 : regression = regV[6]; /* level 3 */
1341 6927 : move32();
1342 : }
1343 : ELSE
1344 : {
1345 0 : regression = regV[7];
1346 0 : move32();
1347 : }
1348 : }
1349 : }
1350 :
1351 35708 : test();
1352 35708 : test();
1353 : /* Convert the regression to a hard decision (classification) */
1354 35708 : IF( GT_32( regression, 1696512081 /* 0.79f in Q31*/ ) && !( LT_16( st->bwidth, SWB ) || hCPE->hCoreCoder[0]->vad_flag == 0 ) )
1355 : {
1356 5978 : hCPE->hStereoICBWE->MSFlag = 1;
1357 5978 : move16();
1358 : }
1359 : }
1360 :
1361 157402 : test();
1362 157402 : test();
1363 157402 : IF( NE_16( st->extl, WB_TBE ) && NE_16( st->extl, SWB_TBE ) && NE_16( st->extl, FB_TBE ) )
1364 : {
1365 : /* Update the previous superwideband speech buffer in case of a SWB_BWE frame - this code is in swb_tbe_enc */
1366 38529 : delay = L_LOOK_16k + L_SUBFR16k;
1367 38529 : move16();
1368 38529 : Copy( shb_speech + sub( L_FRAME16k, delay ), hBWE_TD->old_speech_shb_fx, delay );
1369 : }
1370 : }
1371 : ELSE
1372 : {
1373 238037 : test();
1374 238037 : test();
1375 238037 : IF( ( EQ_16( st->bwidth, FB ) || st->core == ACELP_CORE ) && ( st->element_mode == EVS_MONO ) )
1376 : {
1377 0 : InitSWBencBufferStates_fx( st->hBWE_TD, shb_speech );
1378 : }
1379 : ELSE
1380 : {
1381 238037 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1382 : {
1383 12524 : IF( EQ_16( st->L_frame, L_FRAME ) )
1384 : {
1385 3614 : L_resamp = 560; /* 6.4 kHz core -> 6 - 14 kHz SHB target. 20 ms is 560 samples in 28 kHz sample rate */
1386 3614 : move16();
1387 : }
1388 : ELSE
1389 : {
1390 8910 : L_resamp = 620; /* 8 kHz core -> 7.5 - 15.5 kHz SHB target. 20 ms is 620 samples in 31 kHz sample rate */
1391 8910 : move16();
1392 : }
1393 :
1394 : /* Dirty downsampling to match Nyquist to upper frequency limit of target */
1395 12524 : lerp( st->input_fx, new_swb_speech, L_resamp, extract_l( Mpy_32_32( input_Fs, one_by_50_Q31 ) ) );
1396 :
1397 : /* flip the spectrum */
1398 12524 : Copy( new_swb_speech, spchTmp, L_resamp );
1399 3786544 : FOR( i = 0; i < L_resamp; i = i + 2 )
1400 : {
1401 3774020 : spchTmp[i] = negate( spchTmp[i] );
1402 3774020 : move16();
1403 : }
1404 :
1405 : /* Dirty upsampling to match Nyquist/2 to lower frequency limit of target (reversed spectrum)*/
1406 12524 : lerp( spchTmp, spchTmp2, L_FRAME32k, L_resamp );
1407 12524 : Copy( spchTmp2, spchTmp, L_FRAME32k );
1408 : }
1409 : ELSE
1410 : {
1411 : /* flip the spectrum */
1412 225513 : Copy( new_swb_speech, spchTmp, L_FRAME32k );
1413 :
1414 72389673 : FOR( i = 0; i < L_FRAME32k; i = i + 2 )
1415 : {
1416 72164160 : spchTmp[i] = negate( spchTmp[i] );
1417 72164160 : move16();
1418 : }
1419 : }
1420 :
1421 238037 : Decimate_allpass_steep_fx( spchTmp, hBWE_TD->state_ana_filt_shb_fx, L_FRAME32k, shb_speech );
1422 :
1423 238037 : Copy( shb_speech + L_FRAME16k - ( L_LOOK_16k + L_SUBFR16k ), hBWE_TD->old_speech_shb_fx, L_LOOK_16k + L_SUBFR16k );
1424 :
1425 : /*Compute the past overlap for potential next iDFTs SHB*/
1426 238037 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
1427 : {
1428 1765884 : FOR( i = 0; i < STEREO_DFT_OVL_16k; i++ )
1429 : {
1430 1753360 : 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
1431 1753360 : move32();
1432 : }
1433 : }
1434 : }
1435 :
1436 238037 : IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1437 : {
1438 : /* Reset CLDFB synthesis buffer */
1439 225513 : set32_fx( st->cldfbSynTd->cldfb_state_fx, 0, st->cldfbSynTd->p_filter_length );
1440 : }
1441 : ELSE
1442 : {
1443 12524 : hCPE->hStereoDft->flip_sign = negate( hCPE->hStereoDft->flip_sign ); /* Make sure sign is updated even if DFT SHB target is not generated */
1444 12524 : move16();
1445 : }
1446 : }
1447 :
1448 : /* Memory reset to compensate for 0.9375 ms offset when transitioning from IO to SWB */
1449 : /* When switching from n >1 to n = 1, we keep the enc/dec delay as 8.75/3.25 and below code not needed;
1450 : only when n = 1 start, it will be 9.6875/2.3125 in that case this reset is needed for IO->BWE.*/
1451 395439 : test();
1452 395439 : IF( EQ_16( st->last_extl, -1 ) && ( st->element_mode == EVS_MONO ) )
1453 : {
1454 0 : delay = NS2SA_FX2( input_Fs, DELAY_FIR_RESAMPL_NS );
1455 0 : move16();
1456 0 : FOR( i = 0; i < delay; i++ )
1457 : {
1458 0 : shb_speech[i] = mult_r( mult_r( i, 983 /*0.03f Q15*/ ), shb_speech[2 * delay - 1 - i] );
1459 0 : move16();
1460 : }
1461 : }
1462 :
1463 395439 : return;
1464 : }
|