Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <stdint.h>
34 : #include "options.h"
35 : #include <math.h>
36 : #include "cnst.h"
37 : #include "ivas_cnst.h"
38 : #include "prot_fx.h"
39 : #include "wmc_auto.h"
40 : #include "rom_com.h"
41 : #include "ivas_rom_com.h"
42 :
43 : #include "ivas_prot_fx.h"
44 : #include "ivas_rom_com_fx.h"
45 :
46 : /*-------------------------------------------------------------------*
47 : * ic_bwe_enc_reset()
48 : *
49 : * core switching reset of IC BWE memory
50 : *-------------------------------------------------------------------*/
51 22617 : static void ic_bwe_enc_reset_fx(
52 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE /* i/o: Stereo ICBWE handle */
53 : )
54 : {
55 : /* unscaled & scaled SHB synthesis memory */
56 22617 : set32_fx( hStereoICBWE->mem_lpc_shbsynth_nonref_fx, 0, LPC_SHB_ORDER );
57 22617 : hStereoICBWE->mem_lpc_shbsynth_nonref_e = 0;
58 22617 : move16();
59 : /* inter-channel BWE spectral shape adj. */
60 22617 : hStereoICBWE->prevSpecMapping_fx = 0;
61 22617 : move32();
62 22617 : hStereoICBWE->prevgsMapping_fx = ONE_IN_Q31; /* Q31 */
63 22617 : move32();
64 22617 : hStereoICBWE->prevgsMapping_e = 0;
65 22617 : move16();
66 22617 : set32_fx( &( hStereoICBWE->memShbSpecMapping_fx ), 0, 1 );
67 22617 : hStereoICBWE->memShbSpecMapping_e = 0;
68 22617 : move16();
69 22617 : set32_fx( hStereoICBWE->memShbSpecXcorr_fx, 0, 6 );
70 22617 : hStereoICBWE->memShbSpecXcorr_e = 0;
71 22617 : move16();
72 22617 : set32_fx( hStereoICBWE->memGsEnerMap_fx, ONE_IN_Q31, 2 ); /* Q31 */
73 22617 : hStereoICBWE->memGsEnerMap_e = 0;
74 22617 : move16();
75 22617 : set32_fx( hStereoICBWE->mem_nrg_L_fx, 0, 2 );
76 22617 : hStereoICBWE->mem_nrg_L_fx_e = 0;
77 22617 : move16();
78 22617 : set32_fx( hStereoICBWE->mem_nrg_R_fx, 0, 2 );
79 22617 : hStereoICBWE->mem_nrg_R_fx_e = 0;
80 22617 : move16();
81 22617 : set32_fx( hStereoICBWE->mem_nrg_DMX_fx, 0, 2 );
82 22617 : hStereoICBWE->mem_nrg_DMX_fx_e = 0;
83 22617 : move16();
84 22617 : hStereoICBWE->gDes_pastFrame_fx = ONE_IN_Q31; /* Q31 */
85 22617 : move32();
86 22617 : hStereoICBWE->gDes_pastFrame_e = 0;
87 22617 : move16();
88 22617 : hStereoICBWE->icbweRefEner_fx = 0;
89 22617 : move32();
90 22617 : hStereoICBWE->icbweRefEner_fx_e = 0;
91 22617 : move16();
92 :
93 22617 : return;
94 : }
95 :
96 : /*--------------------------------------------------------------------------*
97 : * ic_bwe_specMapping()
98 : *
99 : * Encode and apply the spectral shape mapping of ref to the non-ref channel
100 : *---------------------------------------------------------------------------*/
101 62 : static Word16 ic_bwe_enc_specMapping_ivas_fx(
102 : const Word32 *shb_frame_target_fx, /* i : target shb Q31-shb_frame_target_e*/
103 : Word16 shb_frame_target_e,
104 : Word32 *shb_synth_nonref_fx, /* o : non-ref shb synth Q31-shb_synth_nonref_e*/
105 : Word16 *shb_synth_nonref_e,
106 : Word32 *specMapping_fx, /* i/o: current frame's mapping Qx*/
107 : Word32 *memShbSpecMapping_fx, /* i/o: current frame's mapping Q31-memShbSpecMapping_e*/
108 : Word16 *memShbSpecMapping_e,
109 : Word32 *memShbSpecXcorr_fx, /* i/o: ic bwe spec mapping scorr memory Q31-memShbSpecXcorr_e*/
110 : Word16 *memShbSpecXcorr_e )
111 : {
112 : Word16 idx;
113 : Word16 max_exp;
114 62 : Word16 Txx1_fx = 0, Txx2_fx = 0, Txx3_fx = 0, T_desired_fx = 0;
115 62 : Word16 Txx1_e = 0, Txx2_e = 0, Txx3_e = 0, T_desired_e = 0;
116 : Word16 T_nonref_target_fx, temp_fx;
117 : Word32 temp00_fx, temp11_fx;
118 : Word16 temp00_exp, temp11_exp;
119 : Word32 temp0_fx, temp1_fx, temp2_fx, temp3_fx;
120 : Word16 a_fx, b_fx, c_fx, a_e, b_e, c_e;
121 62 : Word16 u_fx, u1_fx, u2_fx, u_e = 0, u1_e, u2_e;
122 : Word16 temp0_exp, temp1_exp, temp2_exp, temp3_exp, exp, T_nonref_target_e;
123 :
124 62 : move16();
125 62 : move16();
126 62 : move16();
127 62 : move16();
128 62 : move16();
129 62 : move16();
130 62 : move16();
131 62 : move16();
132 62 : move16();
133 :
134 : /* Calculate rxx(1)/rxx(0) of the non ref target */
135 :
136 62 : temp00_fx = dotp_fixed_ivas_fx( shb_frame_target_fx, shb_frame_target_e, shb_frame_target_fx, shb_frame_target_e, L_FRAME16k - 1, &temp00_exp ); /* Q31-temp0_exp */
137 62 : temp11_fx = dotp_fixed_ivas_fx( shb_frame_target_fx, shb_frame_target_e, shb_frame_target_fx + 1, shb_frame_target_e, L_FRAME16k - 1, &temp11_exp ); /* Q31-temp1_exp */
138 :
139 : /* Calculate rxx(1)/rxx(0) of the non ref synth */
140 62 : temp0_fx = dotp_fixed_ivas_fx( shb_synth_nonref_fx, *shb_synth_nonref_e, shb_synth_nonref_fx, *shb_synth_nonref_e, L_FRAME16k - 3, &temp0_exp ); /* Q31-temp0_exp */
141 62 : temp1_fx = dotp_fixed_ivas_fx( shb_synth_nonref_fx, *shb_synth_nonref_e, shb_synth_nonref_fx + 1, *shb_synth_nonref_e, L_FRAME16k - 3, &temp1_exp ); /* Q31-temp1_exp */
142 62 : temp2_fx = dotp_fixed_ivas_fx( shb_synth_nonref_fx, *shb_synth_nonref_e, shb_synth_nonref_fx + 2, *shb_synth_nonref_e, L_FRAME16k - 3, &temp2_exp ); /* Q31-temp2_exp */
143 62 : temp3_fx = dotp_fixed_ivas_fx( shb_synth_nonref_fx, *shb_synth_nonref_e, shb_synth_nonref_fx + 3, *shb_synth_nonref_e, L_FRAME16k - 3, &temp3_exp ); /* Q31-temp3_exp */
144 :
145 62 : exp = s_max( *memShbSpecXcorr_e, s_max( s_max( s_max( temp00_exp, temp11_exp ), s_max( temp0_exp, temp1_exp ) ), s_max( temp2_exp, temp3_exp ) ) );
146 :
147 62 : temp00_fx = L_shr( temp00_fx, sub( exp, temp00_exp ) );
148 62 : temp11_fx = L_shr( temp11_fx, sub( exp, temp11_exp ) );
149 62 : temp0_fx = L_shr( temp0_fx, sub( exp, temp0_exp ) );
150 62 : temp1_fx = L_shr( temp1_fx, sub( exp, temp1_exp ) );
151 62 : temp2_fx = L_shr( temp2_fx, sub( exp, temp2_exp ) );
152 62 : temp3_fx = L_shr( temp3_fx, sub( exp, temp3_exp ) );
153 :
154 : /* Smoothing */
155 :
156 434 : FOR( Word16 i = 0; i < 6; i++ )
157 : {
158 372 : memShbSpecXcorr_fx[i] = L_shl( memShbSpecXcorr_fx[i], sub( *memShbSpecXcorr_e, exp ) );
159 372 : move32();
160 : }
161 62 : *memShbSpecXcorr_e = exp;
162 62 : move16();
163 :
164 62 : temp00_fx = L_add( L_shr( temp00_fx, 1 ), L_shr( memShbSpecXcorr_fx[0], 1 ) );
165 62 : temp11_fx = L_add( L_shr( temp11_fx, 1 ), L_shr( memShbSpecXcorr_fx[1], 1 ) );
166 62 : temp0_fx = L_add( L_shr( temp0_fx, 1 ), L_shr( memShbSpecXcorr_fx[2], 1 ) );
167 62 : temp1_fx = L_add( L_shr( temp1_fx, 1 ), L_shr( memShbSpecXcorr_fx[3], 1 ) );
168 62 : temp2_fx = L_add( L_shr( temp2_fx, 1 ), L_shr( memShbSpecXcorr_fx[4], 1 ) );
169 62 : temp3_fx = L_add( L_shr( temp3_fx, 1 ), L_shr( memShbSpecXcorr_fx[5], 1 ) );
170 :
171 62 : T_nonref_target_fx = 0;
172 62 : move32();
173 62 : T_nonref_target_e = 31;
174 62 : move16();
175 :
176 62 : IF( temp00_fx != 0 )
177 : {
178 62 : T_nonref_target_fx = BASOP_Util_Divide3232_Scale( temp11_fx, temp00_fx, &T_nonref_target_e );
179 : }
180 :
181 :
182 62 : memShbSpecXcorr_fx[0] = temp00_fx;
183 62 : move32();
184 62 : memShbSpecXcorr_fx[1] = temp11_fx;
185 62 : move32();
186 62 : memShbSpecXcorr_fx[2] = temp0_fx; /* Q31-exp */
187 62 : move32();
188 62 : memShbSpecXcorr_fx[3] = temp1_fx; /* Q31-exp */
189 62 : move32();
190 62 : memShbSpecXcorr_fx[4] = temp2_fx; /* Q31-exp */
191 62 : move32();
192 62 : memShbSpecXcorr_fx[5] = temp3_fx; /* Q31-exp */
193 62 : move32();
194 :
195 :
196 62 : IF( temp0_fx != 0 )
197 : {
198 62 : Txx1_fx = BASOP_Util_Divide3232_Scale( temp1_fx, temp0_fx, &Txx1_e ); /* Q31-Txx1_e */
199 62 : Txx2_fx = BASOP_Util_Divide3232_Scale( temp2_fx, temp0_fx, &Txx2_e ); /* Q31-Txx1_e */
200 62 : Txx3_fx = BASOP_Util_Divide3232_Scale( temp3_fx, temp0_fx, &Txx3_e ); /* Q31-Txx1_e */
201 : }
202 :
203 62 : T_desired_fx = T_nonref_target_fx;
204 62 : T_desired_e = T_nonref_target_e;
205 62 : move32();
206 62 : move16();
207 :
208 : Word16 tmp, tmp1;
209 62 : exp = BASOP_Util_Add_MantExp( Txx3_fx, Txx3_e, Txx1_fx, Txx1_e, &tmp ); /* Q15-exp */
210 62 : tmp1 = mult( Txx2_fx, T_desired_fx ); // Txx2_e + T_desired_e
211 62 : a_e = BASOP_Util_Add_MantExp( tmp1, sub( add( Txx2_e, T_desired_e ), 1 ), negate( tmp ), exp, &a_fx ); /* Q15-a_e */
212 :
213 62 : exp = BASOP_Util_Add_MantExp( Txx2_fx, Txx2_e, 32767, 1, &tmp ); /* Q15-exp */
214 62 : tmp1 = mult( Txx1_fx, T_desired_fx ); // Txx1_e + T_desired_e
215 62 : b_e = BASOP_Util_Add_MantExp( tmp1, sub( add( Txx1_e, T_desired_e ), 1 ), negate( tmp ), exp, &b_fx ); /* Q15-b_fx */
216 :
217 62 : c_e = BASOP_Util_Add_MantExp( T_desired_fx, T_desired_e, negate( Txx1_fx ), Txx1_e, &c_fx ); /* Q15-c_fx */
218 :
219 62 : u_fx = extract_l( *specMapping_fx );
220 :
221 :
222 : /* while performing (-32768)*(-32768), overflow occurs because the result comes out as 32768 after mult. This is the only case for mult where overflow happens. In order to avoid this scenario mult_sat is used */
223 :
224 62 : tmp = mult_sat( b_fx, b_fx ); // b_e + b_e;
225 62 : tmp1 = mult_sat( a_fx, c_fx ); // a_e + c_e + 2
226 :
227 62 : exp = BASOP_Util_Add_MantExp( tmp, add( b_e, b_e ), tmp1, add( a_e, add( c_e, 2 ) ), &temp_fx ); /* Q15-exp */
228 62 : test();
229 62 : IF( temp_fx >= 0 && a_fx != 0 )
230 : {
231 62 : temp_fx = Sqrt16( temp_fx, &exp ); /* Q15-exp */
232 62 : exp = BASOP_Util_Add_MantExp( negate( b_fx ), b_e, temp_fx, exp, &tmp ); /* Q15-exp */
233 62 : u1_e = BASOP_Util_Divide1616_Scale( tmp, a_fx, &u1_fx ); /* Q15-u1_e */
234 62 : u1_e = add( u1_e, sub( exp, add( a_e, 1 ) ) );
235 :
236 62 : exp = BASOP_Util_Add_MantExp( negate( b_fx ), b_e, negate( temp_fx ), exp, &tmp ); /* Q15-exp */
237 62 : u2_e = BASOP_Util_Divide1616_Scale( tmp, a_fx, &u2_fx ); /* Q15-u2_e */
238 62 : u2_e = add( u2_e, sub( exp, add( a_e, 1 ) ) );
239 :
240 62 : test();
241 62 : IF( ( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( abs_s( u1_fx ) ), u1_e, 1, 31 ) < 0 ) && ( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( abs_s( u2_fx ) ), u2_e, 1, 31 ) < 0 ) )
242 : {
243 0 : IF( ( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( abs_s( u1_fx ) ), u1_e, L_deposit_h( abs_s( u2_fx ) ), u2_e ) < 0 ) )
244 : {
245 0 : u_fx = u1_fx;
246 0 : u_e = u1_e;
247 : }
248 : ELSE
249 : {
250 0 : u_fx = u2_fx;
251 0 : u_e = u2_e;
252 : }
253 0 : move32();
254 0 : move16();
255 : }
256 62 : ELSE IF( ( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( abs_s( u1_fx ) ), u1_e, 1, 31 ) < 0 ) )
257 : {
258 4 : u_fx = u1_fx;
259 4 : u_e = u1_e;
260 4 : move32();
261 4 : move16();
262 : }
263 58 : ELSE IF( ( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( abs_s( u2_fx ) ), u2_e, 1, 31 ) < 0 ) )
264 : {
265 58 : u_fx = u2_fx;
266 58 : u_e = u2_e;
267 58 : move32();
268 58 : move16();
269 : }
270 : }
271 :
272 62 : IF( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( u_fx ), u_e, 0, 0 ) > 0 )
273 : {
274 62 : u_fx = 0;
275 62 : u_e = 0;
276 62 : move32();
277 62 : move16();
278 : }
279 :
280 62 : IF( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( u_fx ), u_e, -1288490188, 0 ) < 0 ) // -0.6 in Q31
281 : {
282 0 : u_fx = -1288490188 >> 16; //-0.6 in Q31 -> Q15
283 0 : u_e = 0;
284 0 : move32();
285 0 : move16();
286 : }
287 :
288 :
289 : /* Quantize spec. mapping **/
290 : Word16 specMapping16;
291 62 : idx = usquant_fx( shr( u_fx, sub( 0, u_e ) ), &specMapping16, -19660, 6553 >> 1, ( 1 << STEREO_ICBWE_SPBITS ) ); // -0.6 in in Q15 -> -19660, 0.2 in Q15 -> 6553
292 62 : *specMapping_fx = L_deposit_h( specMapping16 );
293 62 : move32();
294 :
295 62 : max_exp = s_max( sub( *memShbSpecMapping_e, norm_l( *memShbSpecMapping_fx ) ), *shb_synth_nonref_e );
296 :
297 62 : *memShbSpecMapping_fx = L_shl( *memShbSpecMapping_fx, sub( *memShbSpecMapping_e, max_exp ) ); // max_exp
298 62 : move32();
299 62 : *memShbSpecMapping_e = max_exp;
300 62 : move16();
301 :
302 62 : scale_sig32( shb_synth_nonref_fx, L_FRAME16k, sub( *shb_synth_nonref_e, max_exp ) ); // max_exp
303 62 : *shb_synth_nonref_e = max_exp;
304 62 : move16();
305 :
306 : /* IC-BWE spec mapping */
307 62 : deemph_fx_32( shb_synth_nonref_fx, specMapping16, L_FRAME16k, memShbSpecMapping_fx ); // shb_synth_nonref_e
308 :
309 62 : return idx;
310 : }
311 :
312 :
313 : /*----------------------------------------------------------------------*
314 : * ic_bwe_gsMapping()
315 : *
316 : * Encode and apply the gain shape mapping of ref to the non-ref channel
317 : *-----------------------------------------------------------------------*/
318 :
319 : /*! r: return quant. index value */
320 : /*----------------------------------------------------------------------*
321 : * ic_bwe_gsMapping()
322 : *
323 : * Encode and apply the gain shape mapping of ref to the non-ref channel
324 : *-----------------------------------------------------------------------*/
325 :
326 : /*! r: return quant. index value */
327 26817 : static Word16 ic_bwe_enc_gsMapping_ivas_fx(
328 : const Word32 relG_targ_fx, /* i : gDes, rel gain target Q31-relG_targ_e*/
329 : Word16 relG_targ_e,
330 : const Word16 *shbSynth_fx, /* i : ref synth signal Q15-shbSynth_e*/
331 : Word16 shbSynth_e, /* i : ref synth signal */
332 : Word32 *synthSHB_nonref_fx, /* i : nonref synth signal Q31-synthSHB_nonref_e*/
333 : Word16 *synthSHB_nonref_e, /* i : nonref synth signal */
334 : Word32 *gsMapping_fx, /* i/o: gs mapping Q31-gsMapping_e*/
335 : Word16 *gsMapping_e, /* i/o: gs mapping */
336 : Word32 *memEner_fx, /* Q31-memEner_e*/
337 : Word16 *memEner_e,
338 : const Word16 element_mode /* i : element mode Q0*/
339 : )
340 : {
341 : Word16 i;
342 26817 : Word16 idx = 0, temp1;
343 26817 : move16();
344 26817 : Word32 temp1_fx32 = 0, temp2_fx32 = 0;
345 26817 : move32();
346 26817 : move32();
347 : Word32 L_mult;
348 : Word16 temp2_fx, gsMapping_fx16;
349 :
350 : /* rel gain of synth signals */
351 26817 : Word16 exp1 = shbSynth_e, exp2 = *synthSHB_nonref_e, temp1_exp = 0, temp2_exp = 0, exp = 0;
352 26817 : move16();
353 26817 : move16();
354 26817 : move16();
355 26817 : move16();
356 26817 : move16();
357 8608257 : FOR( i = 0; i < L_FRAME16k; i++ )
358 : {
359 8581440 : temp1_fx32 = BASOP_Util_Add_Mant32Exp( temp1_fx32, temp1_exp, L_abs( L_deposit_h( shbSynth_fx[i] ) ), exp1, &temp1_exp ); /* Q31-temp1_exp */
360 8581440 : temp2_fx32 = BASOP_Util_Add_Mant32Exp( temp2_fx32, temp2_exp, L_abs( synthSHB_nonref_fx[i] ), exp2, &temp2_exp ); /* Q31-temp2_exp */
361 : }
362 :
363 26817 : memEner_fx[0] = L_shr( temp1_fx32, sub( s_max( temp1_exp, temp2_exp ), temp1_exp ) );
364 26817 : memEner_fx[1] = L_shr( temp2_fx32, sub( s_max( temp1_exp, temp2_exp ), temp2_exp ) );
365 26817 : move32();
366 26817 : move32();
367 26817 : *memEner_e = s_max( temp1_exp, temp2_exp );
368 26817 : move16();
369 :
370 26817 : IF( temp2_fx32 == 0 )
371 : {
372 0 : *gsMapping_fx = *gsMapping_fx;
373 0 : move32();
374 : }
375 : ELSE
376 : {
377 26817 : L_mult = Mpy_32_32( relG_targ_fx, temp1_fx32 ); // relG_targ_e + temp1_e
378 26817 : temp1 = BASOP_Util_Divide3232_Scale( L_mult, temp2_fx32, &exp ); // exp = exp - ( relG_targ_e + temp1_e - temp2_e )
379 26817 : exp = add( exp, sub( add( relG_targ_e, temp1_exp ), temp2_exp ) );
380 26817 : *gsMapping_fx = L_deposit_h( temp1 ); // exp
381 26817 : move32();
382 : }
383 :
384 26817 : temp2_fx32 = BASOP_Util_Log10( *gsMapping_fx, exp ); /* no impact from prev_gsMapping */ // Q25
385 : /* quantize the IC-BWE GS mapping*/
386 26817 : IF( EQ_16( element_mode, IVAS_CPE_TD ) )
387 : {
388 74 : IF( LT_32( temp2_fx32, -( 2 << 25 ) ) )
389 : {
390 0 : temp2_fx = -( 2 << 12 ); // Q12
391 0 : move16();
392 : }
393 74 : ELSE IF( GT_32( temp2_fx32, 1 << 25 ) )
394 : {
395 61 : temp2_fx = 1 << 12; // Q12
396 61 : move16();
397 : }
398 :
399 74 : temp2_fx = extract_h( L_shl_sat( temp2_fx32, Q3 ) ); // Q12
400 74 : gsMapping_fx16 = 0;
401 74 : move16();
402 74 : idx = squant_fx( temp2_fx, &gsMapping_fx16, icbwe_gsMapping_tbl_fx, ( 1 << STEREO_ICBWE_GSBITS ) ); // Q12
403 : }
404 : ELSE
405 : {
406 26743 : IF( LT_32( temp2_fx32, -( 5 << 25 ) ) )
407 : {
408 0 : temp2_fx = -( 5 << 12 ); // Q12
409 0 : move16();
410 : }
411 26743 : ELSE IF( GT_32( temp2_fx32, 1 << 25 ) )
412 : {
413 0 : temp2_fx = 1 << 12; // Q12
414 0 : move16();
415 : }
416 :
417 26743 : temp2_fx = extract_h( L_shl_sat( temp2_fx32, Q3 ) ); // Q12
418 26743 : gsMapping_fx16 = 0;
419 26743 : move16();
420 26743 : idx = squant_fx( temp2_fx, &gsMapping_fx16, icbwe_gsMappingDFT_tbl_fx, ( 1 << STEREO_ICBWE_GSBITS_DFT ) ); // Q12
421 : }
422 26817 : *gsMapping_fx = L_deposit_h( gsMapping_fx16 ); // Q28
423 26817 : move32();
424 : Word16 e;
425 26817 : L_mult = Mult_32_16( *gsMapping_fx, 27213 /*=log2(10)*2^13*/ ); // Q28 + Q13 - Q15 -> Q26
426 26817 : *gsMapping_fx = BASOP_util_Pow2( L_mult, 5, &e ); /* Q31-e */
427 26817 : move32();
428 :
429 26817 : *gsMapping_e = e; // Q31
430 26817 : move16();
431 26817 : return idx;
432 : }
433 :
434 : /*----------------------------------------------------------------------*
435 : * icbwe_dft_stereo_param()
436 : *
437 : * Estimate ICBWE parameters in DFT stereo
438 : *-----------------------------------------------------------------------*/
439 26743 : static void icbwe_dft_stereo_param_ivas_fx(
440 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE, /* i/o: */
441 : STEREO_DFT_ENC_DATA_HANDLE hStereoDft, /* i : */
442 : Encoder_State *st, /* i/o: */
443 : Word32 *shb_synth_nonref_fx, /* i/o: Q31-shb_synth_nonref_e*/
444 : Word16 *shb_synth_nonref_e /* i/o: */
445 : )
446 : {
447 : Word16 slopeILD_fx;
448 : Word32 normFac_fx;
449 : Word16 gsIndx, spIndx, gDes_fx;
450 :
451 : Word32 *nrg_L_fx, *nrg_R_fx, *nrg_DMX_fx;
452 : Word32 sum_nrg_L_fx, sum_nrg_R_fx, sum_nrg_DMX_fx;
453 26743 : const Word32 spec_table_fx[4] = { -1288490189, -858993459, -429496730, 0 }; // Q31
454 26743 : const Word16 slope_table_fx16[4] = { -278, -165, -75, 0 }; // Q7
455 :
456 26743 : BSTR_ENC_HANDLE hBstr = st->hBstr;
457 26743 : normFac_fx = 1342177280; /* 1000 * (10/((14400+10400)/2 - (6400+10400)/2)) */ // Q29
458 26743 : move32();
459 :
460 26743 : nrg_L_fx = hStereoDft->nrg_L_fx;
461 26743 : nrg_R_fx = hStereoDft->nrg_R_fx;
462 26743 : nrg_DMX_fx = hStereoDft->nrg_DMX_fx;
463 :
464 26743 : hStereoICBWE->mem_nrg_L_fx_e = s_max( hStereoDft->nrg_L_fx_e[0], hStereoDft->nrg_L_fx_e[1] );
465 26743 : hStereoICBWE->mem_nrg_R_fx_e = s_max( hStereoDft->nrg_R_fx_e[0], hStereoDft->nrg_R_fx_e[1] );
466 26743 : hStereoICBWE->mem_nrg_DMX_fx_e = s_max( hStereoDft->nrg_DMX_fx_e[0], hStereoDft->nrg_DMX_fx_e[1] );
467 26743 : move16();
468 26743 : move16();
469 26743 : move16();
470 26743 : hStereoICBWE->mem_nrg_L_fx[0] = L_shr( nrg_L_fx[0], sub( hStereoICBWE->mem_nrg_L_fx_e, hStereoDft->nrg_L_fx_e[0] ) );
471 26743 : hStereoICBWE->mem_nrg_R_fx[0] = L_shr( nrg_R_fx[0], sub( hStereoICBWE->mem_nrg_R_fx_e, hStereoDft->nrg_R_fx_e[0] ) );
472 26743 : hStereoICBWE->mem_nrg_DMX_fx[0] = L_shr( nrg_DMX_fx[0], sub( hStereoICBWE->mem_nrg_DMX_fx_e, hStereoDft->nrg_DMX_fx_e[0] ) );
473 26743 : hStereoICBWE->mem_nrg_L_fx[1] = L_shr( nrg_L_fx[1], sub( hStereoICBWE->mem_nrg_L_fx_e, hStereoDft->nrg_L_fx_e[1] ) );
474 26743 : hStereoICBWE->mem_nrg_R_fx[1] = L_shr( nrg_R_fx[1], sub( hStereoICBWE->mem_nrg_R_fx_e, hStereoDft->nrg_R_fx_e[1] ) );
475 26743 : hStereoICBWE->mem_nrg_DMX_fx[1] = L_shr( nrg_DMX_fx[1], sub( hStereoICBWE->mem_nrg_DMX_fx_e, hStereoDft->nrg_DMX_fx_e[1] ) );
476 26743 : move32();
477 26743 : move32();
478 26743 : move32();
479 26743 : move32();
480 26743 : move32();
481 26743 : move32();
482 :
483 26743 : Word16 sum_nrg_L_e = hStereoICBWE->mem_nrg_L_fx_e, sum_nrg_R_e = hStereoICBWE->mem_nrg_R_fx_e, sum_nrg_DMX_e = hStereoICBWE->mem_nrg_DMX_fx_e;
484 26743 : sum_nrg_L_fx = sum_32_fx( hStereoICBWE->mem_nrg_L_fx, 2, &sum_nrg_L_e ); // hStereoICBWE->mem_nrg_L_fx_e
485 26743 : sum_nrg_R_fx = sum_32_fx( hStereoICBWE->mem_nrg_R_fx, 2, &sum_nrg_R_e ); // hStereoICBWE->mem_nrg_R_fx_e
486 26743 : sum_nrg_DMX_fx = sum_32_fx( hStereoICBWE->mem_nrg_DMX_fx, 2, &sum_nrg_DMX_e ); // hStereoICBWE->mem_nrg_DMX_fx_e
487 26743 : move16();
488 26743 : move16();
489 26743 : move16();
490 :
491 : /* refChanIndex_bwe refinement */
492 :
493 26743 : test();
494 26743 : test();
495 26743 : test();
496 26743 : test();
497 26743 : IF( ( ( BASOP_Util_Cmp_Mant32Exp( sum_nrg_R_fx, sum_nrg_R_e, Mpy_32_32( 1374389534, sum_nrg_L_fx ), sum_nrg_L_e ) < 0 ) && EQ_16( hStereoICBWE->refChanIndx_bwe, hStereoICBWE->prev_refChanIndx_bwe ) ) || ( BASOP_Util_Cmp_Mant32Exp( sum_nrg_R_fx, sum_nrg_R_e, Mpy_32_32( 773094113, sum_nrg_L_fx ), sum_nrg_L_e ) < 0 ) ) // 0.64 in Q31 -> 1374389534, 0.36 in Q31 -> 773094113
498 : {
499 4402 : hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
500 4402 : move16();
501 : }
502 22341 : ELSE IF( ( ( BASOP_Util_Cmp_Mant32Exp( sum_nrg_L_fx, sum_nrg_L_e, Mpy_32_32( 1374389534, sum_nrg_R_fx ), sum_nrg_R_e ) < 0 ) && EQ_16( hStereoICBWE->refChanIndx_bwe, hStereoICBWE->prev_refChanIndx_bwe ) ) || ( BASOP_Util_Cmp_Mant32Exp( sum_nrg_L_fx, sum_nrg_L_e, Mpy_32_32( 773094113, sum_nrg_R_fx ), sum_nrg_R_e ) < 0 ) ) // 0.64 in Q31 -> 1374389534, 0.36 in Q31 -> 773094113
503 : {
504 9694 : hStereoICBWE->refChanIndx_bwe = R_CH_INDX;
505 9694 : move16();
506 : }
507 :
508 : /* Initialization of spIndx */
509 26743 : slopeILD_fx = 0;
510 26743 : move32();
511 :
512 26743 : spIndx = squant_fx( slopeILD_fx, &slopeILD_fx, slope_table_fx16, 1 << STEREO_ICBWE_SPBITS_DFT ); /* Q0 */
513 :
514 26743 : test();
515 26743 : test();
516 26743 : test();
517 26743 : IF( ( EQ_16( st->extl, SWB_TBE ) || EQ_16( st->extl, WB_TBE ) || EQ_16( st->extl, FB_TBE ) ) && EQ_16( st->flag_ACELP16k, 1 ) )
518 12673 : {
519 : /* Spec Mapping Estimate */
520 : Word16 max_exp;
521 : Word16 tmp1, exp1;
522 : Word32 L_tmp, L_tmp1;
523 12673 : L_tmp = Mpy_32_32( hStereoICBWE->mem_nrg_L_fx[1], hStereoICBWE->mem_nrg_R_fx[0] ); // hStereoICBWE->mem_nrg_L_fx_e[1] + hStereoICBWE->mem_nrg_R_fx_e[1]
524 12673 : L_tmp1 = Mpy_32_32( hStereoICBWE->mem_nrg_R_fx[1], hStereoICBWE->mem_nrg_L_fx[0] ); // hStereoICBWE->mem_nrg_L_fx_e[1] + hStereoICBWE->mem_nrg_R_fx_e[1]
525 12673 : tmp1 = BASOP_Util_Divide3232_Scale( L_tmp, L_tmp1, &exp1 ); /* Q15-exp1 */
526 12673 : exp1 = add( exp1, sub( add( hStereoICBWE->mem_nrg_L_fx_e, hStereoICBWE->mem_nrg_R_fx_e ), add( hStereoICBWE->mem_nrg_L_fx_e, hStereoICBWE->mem_nrg_R_fx_e ) ) );
527 12673 : L_tmp = BASOP_Util_Log10( L_deposit_h( tmp1 ), exp1 );
528 12673 : L_tmp = Mpy_32_32( normFac_fx, L_tmp ); // // Q25 + Q29 - Q31 -> Q23
529 12673 : slopeILD_fx = extract_l( L_shr( L_tmp, 16 ) ); // Q23 - Q16 -> Q7
530 12673 : IF( hStereoICBWE->refChanIndx_bwe == L_CH_INDX )
531 : {
532 4134 : slopeILD_fx = negate( slopeILD_fx ); /* Q7 */
533 : }
534 12673 : spIndx = squant_fx( slopeILD_fx, &slopeILD_fx, slope_table_fx16, 1 << STEREO_ICBWE_SPBITS_DFT );
535 12673 : IF( Mpy_32_32( spec_table_fx[spIndx], hStereoICBWE->prevSpecMapping_fx ) < 0 )
536 : {
537 0 : slopeILD_fx = 0;
538 0 : move32();
539 0 : spIndx = squant_fx( slopeILD_fx, &slopeILD_fx, slope_table_fx16, 1 << STEREO_ICBWE_SPBITS_DFT ); /* Q0 */
540 : }
541 :
542 12673 : hStereoICBWE->prevSpecMapping_fx = spec_table_fx[spIndx]; // q31
543 :
544 12673 : max_exp = s_max( *shb_synth_nonref_e, sub( hStereoICBWE->memShbSpecMapping_e, norm_l( hStereoICBWE->memShbSpecMapping_fx ) ) );
545 :
546 12673 : hStereoICBWE->memShbSpecMapping_fx = L_shl( hStereoICBWE->memShbSpecMapping_fx, sub( hStereoICBWE->memShbSpecMapping_e, max_exp ) ); // max_exp
547 12673 : move32();
548 12673 : hStereoICBWE->memShbSpecMapping_e = max_exp;
549 12673 : move16();
550 :
551 12673 : scale_sig32( shb_synth_nonref_fx, L_FRAME16k, sub( *shb_synth_nonref_e, max_exp ) );
552 12673 : *shb_synth_nonref_e = max_exp;
553 12673 : move16();
554 :
555 : /* ic bwe spec mapping application */
556 12673 : deemph_fx_32( shb_synth_nonref_fx, extract_h( hStereoICBWE->prevSpecMapping_fx ), L_FRAME16k, &hStereoICBWE->memShbSpecMapping_fx ); // shb_synth_nonref_e
557 : }
558 : ELSE
559 : {
560 14070 : hStereoICBWE->memShbSpecMapping_e = 0;
561 14070 : move16();
562 14070 : hStereoICBWE->memShbSpecMapping_fx = 0;
563 14070 : hStereoICBWE->prevSpecMapping_fx = 0;
564 14070 : move32();
565 14070 : move32();
566 : }
567 :
568 : /* gsMapping estimate */
569 :
570 : Word16 exp;
571 26743 : IF( hStereoICBWE->refChanIndx_bwe == L_CH_INDX )
572 : {
573 8993 : gDes_fx = BASOP_Util_Divide3232_Scale( sum_nrg_R_fx, sum_nrg_DMX_fx, &exp ); /* Q15-exp */
574 8993 : exp = add( exp, sub( sum_nrg_R_e, sum_nrg_DMX_e ) );
575 : }
576 : ELSE
577 : {
578 17750 : gDes_fx = BASOP_Util_Divide3232_Scale( sum_nrg_L_fx, sum_nrg_DMX_fx, &exp ); /* Q15-exp */
579 17750 : exp = add( exp, sub( sum_nrg_L_e, sum_nrg_DMX_e ) );
580 : }
581 :
582 26743 : gDes_fx = Sqrt16( gDes_fx, &exp ); /* Q15-exp */
583 26743 : Word16 exp1 = hStereoICBWE->gDes_pastFrame_e, exp2, tmp;
584 26743 : Word32 tmp321 = 0, tmp322 = 0;
585 26743 : move32();
586 26743 : move32();
587 : /* Geometric Mean */
588 26743 : tmp321 = Sqrt32( hStereoICBWE->gDes_pastFrame_fx, &exp1 ); // exp1
589 26743 : exp2 = exp;
590 26743 : move16();
591 26743 : tmp = Sqrt16( gDes_fx, &exp2 ); /* Q15-exp2 */
592 26743 : tmp322 = Mpy_32_32( tmp321, L_deposit_h( tmp ) ); // exp1 + exp2
593 26743 : hStereoICBWE->gDes_pastFrame_fx = tmp322;
594 26743 : hStereoICBWE->gDes_pastFrame_e = add( exp1, exp2 );
595 26743 : move32();
596 26743 : move16();
597 :
598 26743 : gsIndx = ic_bwe_enc_gsMapping_ivas_fx( hStereoICBWE->gDes_pastFrame_fx, hStereoICBWE->gDes_pastFrame_e, hStereoICBWE->shbSynthRef_fx, hStereoICBWE->shbSynthRef_e, shb_synth_nonref_fx, shb_synth_nonref_e, &( hStereoICBWE->prevgsMapping_fx ), &( hStereoICBWE->prevgsMapping_e ), hStereoICBWE->memGsEnerMap_fx, &hStereoICBWE->memGsEnerMap_e, st->element_mode ); /* Q0 */
599 :
600 26743 : hStereoICBWE->gDes_pastFrame_fx = L_deposit_h( gDes_fx ); /* Q31-exp */
601 26743 : hStereoICBWE->gDes_pastFrame_e = exp;
602 26743 : move32();
603 26743 : move16();
604 :
605 26743 : push_indice( hBstr, IND_STEREO_ICBWE_REF, hStereoICBWE->refChanIndx_bwe, STEREO_ICBWE_REFBITS );
606 26743 : IF( EQ_16( st->flag_ACELP16k, 1 ) )
607 : {
608 13212 : push_indice( hBstr, IND_STEREO_ICBWE_SP, spIndx, STEREO_ICBWE_SPBITS_DFT );
609 : }
610 :
611 : /* IC BWE GS mapping */
612 26743 : push_indice( hBstr, IND_STEREO_ICBWE_GS, gsIndx, STEREO_ICBWE_GSBITS_DFT );
613 :
614 26743 : return;
615 : }
616 :
617 48219 : void stereo_icBWE_enc_ivas_fx(
618 : CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure */
619 : const Word32 shb_speech_ref_fx[], /* i : SHB speech ref channel Q31-shb_speech_ref_e*/
620 : const Word16 shb_speech_ref_e, /* i : SHB speech ref channel */
621 : Word32 shb_speech_nonref_fx[], /* i/o: SHB speech non-ref channel Q31-shb_speech_nonref_e*/
622 : Word16 shb_speech_nonref_e, /* i/o: SHB speech non-ref channel */
623 : const Word32 *voice_factors_fx /* i : voicing factors Q31 */
624 : )
625 : {
626 : Word16 i, j, k, nbSubFr;
627 : STEREO_DFT_ENC_DATA_HANDLE hStereoDft;
628 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE;
629 : BSTR_ENC_HANDLE hBstr;
630 : Encoder_State *st;
631 : Word16 spIndx, gsIndx;
632 :
633 : Word16 nonRefMemLen, refMemLen;
634 48219 : Word16 shb_synth_nonref_e = 0, max_e;
635 48219 : move16();
636 :
637 : Word32 nlMixFac_fx[NB_SUBFR16k];
638 : Word32 excSHB_nonref_fx[L_FRAME16k];
639 48219 : set32_fx( excSHB_nonref_fx, 0, L_FRAME16k );
640 : Word32 shb_synth_nonref_fx[L_FRAME16k];
641 : Word32 shb_frame_nonref_fx[L_LOOK_16k + L_FRAME16k], gTarget_fx;
642 : Word32 shb_frame_ref_fx[L_LOOK_16k + L_FRAME16k];
643 : Word32 gDes_fx;
644 : Word32 temp1_fx, temp2_fx;
645 : Word16 temp1_e, temp2_e, shb_frame_ref_e, shb_frame_nonref_e, gDes_e, tmp, exp;
646 :
647 :
648 : /* initialization */
649 48219 : hStereoDft = hCPE->hStereoDft;
650 48219 : hStereoICBWE = hCPE->hStereoICBWE;
651 48219 : st = hCPE->hCoreCoder[0];
652 48219 : hBstr = st->hBstr;
653 :
654 48219 : gTarget_fx = hCPE->hStereoTCA->targetGain_fx; /* i : gain norm for target (or R) channel */ // Q29
655 48219 : move32();
656 :
657 48219 : nonRefMemLen = L_MEM_RECALC_TBE_16K + L_FILT16k;
658 48219 : move16();
659 :
660 48219 : test();
661 48219 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
662 : {
663 103 : refMemLen = L_MEM_RECALC_TBE_16K + 20;
664 103 : move16();
665 : }
666 48116 : ELSE IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && GT_32( st->core_brate, SID_2k40 ) )
667 : {
668 39868 : refMemLen = 20;
669 39868 : move16();
670 : }
671 : ELSE
672 : {
673 8248 : ic_bwe_enc_reset_fx( hStereoICBWE );
674 8248 : return;
675 : }
676 :
677 39971 : IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
678 : {
679 : /* memory resets */
680 39868 : set16_fx( hStereoICBWE->mem_shb_speech_nonref_fx, 0, L_LOOK_16k );
681 39868 : set32_fx( shb_speech_nonref_fx, 0, L_FRAME16k );
682 :
683 : /* core switching reset */
684 39868 : test();
685 39868 : test();
686 39868 : test();
687 39868 : IF( st->last_core != ACELP_CORE || st->core != ACELP_CORE || LT_16( st->bwidth, SWB ) || EQ_16( st->extl, -1 ) )
688 : {
689 :
690 14340 : ic_bwe_enc_reset_fx( hStereoICBWE );
691 14340 : test();
692 14340 : test();
693 14340 : test();
694 14340 : IF( st->core != ACELP_CORE || LT_16( st->bwidth, SWB ) || LT_32( st->input_Fs, 32000 ) || EQ_16( st->extl, -1 ) )
695 : {
696 13125 : return;
697 : }
698 : }
699 :
700 26743 : set32_fx( hStereoICBWE->memShbSpecXcorr_fx, 0, 6 );
701 :
702 26743 : hStereoICBWE->prevRefEner_fx = 0;
703 26743 : hStereoICBWE->prevNonRefEner_fx = 0;
704 26743 : move32();
705 26743 : move32();
706 :
707 : /* SHB frame buffer offsets for TBE processing << re-adjust check >>*/
708 :
709 26743 : max_e = s_max( hStereoICBWE->mem_shb_speech_ref_e, shb_speech_ref_e );
710 26743 : Copy_Scale_sig( hStereoICBWE->mem_shb_speech_ref_fx, hStereoICBWE->mem_shb_speech_ref_fx, L_LOOK_16k, negate( sub( max_e, hStereoICBWE->mem_shb_speech_ref_e ) ) ); // mem_shb_speech_ref_e
711 :
712 26743 : set32_fx( shb_frame_ref_fx, 0, L_LOOK_16k + L_FRAME16k );
713 26743 : Copy_Scale_sig_16_32_DEPREC( hStereoICBWE->mem_shb_speech_ref_fx, shb_frame_ref_fx, L_LOOK_16k, 0 ); // Q = 15 - mem_shb_speech_ref_e
714 26743 : scale_sig32( shb_frame_ref_fx, L_LOOK_16k, add( negate( sub( max_e, hStereoICBWE->mem_shb_speech_ref_e ) ), Q16 ) ); // Q = 31 - mem_shb_speech_ref_e
715 :
716 26743 : hStereoICBWE->mem_shb_speech_ref_e = max_e;
717 26743 : shb_frame_ref_e = max_e;
718 26743 : move16();
719 26743 : move16();
720 : /* shb_frame_ref_ICBWE @ (8.75 - shb_speech_delay (=1.25) ms) after shb_speech */
721 :
722 26743 : shb_synth_nonref_e = max_e;
723 26743 : move16();
724 :
725 26743 : Copy_Scale_sig32( shb_speech_ref_fx, shb_frame_ref_fx + L_LOOK_16k - refMemLen, L_FRAME16k, negate( sub( max_e, shb_speech_ref_e ) ) ); /* shb_speech_ref_e */
726 26743 : Copy_Scale_sig_32_16( shb_frame_ref_fx + L_FRAME16k, hStereoICBWE->mem_shb_speech_ref_fx, L_LOOK_16k, negate( sub( max_e, hStereoICBWE->mem_shb_speech_ref_e ) ) - Q16 ); // mem_shb_speech_ref_e
727 :
728 :
729 26743 : test();
730 26743 : test();
731 26743 : IF( ( EQ_16( st->extl, SWB_TBE ) || EQ_16( st->extl, FB_TBE ) ) && EQ_16( st->flag_ACELP16k, 1 ) )
732 12673 : {
733 12673 : Copy32( voice_factors_fx, nlMixFac_fx, NB_SUBFR16k ); // Q31
734 : /*if( gDes < 0.5f || gDes > 2.0f )*/
735 12673 : test();
736 12673 : IF( LT_32( gTarget_fx, 268435456 ) || GT_32( gTarget_fx, 1073741824 ) ) // 0.5 in Q29 or 2.0 in Q29
737 : {
738 1307 : v_multc_fixed( voice_factors_fx, 1073741824, nlMixFac_fx, NB_SUBFR16k ); // Q31, 0.5 in Q31 -> 1073741824
739 : }
740 :
741 12673 : Word16 L_FRAME16k_by_nbSubFr = L_FRAME16k / NB_SUBFR;
742 12673 : move16();
743 12673 : IF( st->flag_ACELP16k == 0 )
744 : {
745 0 : nbSubFr = NB_SUBFR;
746 0 : move16();
747 : }
748 : ELSE
749 : {
750 12673 : nbSubFr = NB_SUBFR16k;
751 12673 : move16();
752 12673 : L_FRAME16k_by_nbSubFr = L_FRAME16k / NB_SUBFR16k;
753 12673 : move16();
754 : }
755 :
756 : Word16 exp_buf[L_FRAME16k];
757 12673 : set16_fx( exp_buf, 0, L_FRAME16k );
758 76038 : FOR( ( i = 0, k = 0 ); i < nbSubFr; i++ )
759 : {
760 63365 : test();
761 63365 : IF( EQ_16( st->coder_type, UNVOICED ) || EQ_16( hStereoICBWE->MSFlag, 1 ) )
762 : {
763 15285 : temp1_fx = 0;
764 15285 : temp1_e = 0;
765 15285 : temp2_fx = ONE_IN_Q31; /* Q31 */
766 15285 : temp2_e = 0;
767 15285 : move16();
768 15285 : move32();
769 15285 : move16();
770 15285 : move32();
771 : }
772 : ELSE
773 : {
774 48080 : temp1_e = 0, temp2_e = 0;
775 48080 : move16();
776 48080 : move16();
777 48080 : temp1_fx = Sqrt32( nlMixFac_fx[i], &temp1_e );
778 48080 : temp2_fx = Sqrt32( L_sub( ONE_IN_Q31, nlMixFac_fx[i] ), &temp2_e ); /* Q31-temp2_e */
779 : }
780 :
781 4118725 : FOR( j = 0; j < L_FRAME16k_by_nbSubFr; ( j++, k++ ) )
782 : {
783 4055360 : excSHB_nonref_fx[k] = BASOP_Util_Add_Mant32Exp( Mpy_32_32( temp1_fx, L_deposit_h( hStereoICBWE->nlExc16k_fx[k] ) ), add( temp1_e, hStereoICBWE->nlExc16k_e ), Mpy_32_32( temp2_fx, L_deposit_h( hStereoICBWE->mixExc16k_fx[k] ) ), add( temp2_e, hStereoICBWE->mixExc16k_e ), &exp_buf[k] ); /* Q31-exp_buf */
784 4055360 : move32();
785 : }
786 : }
787 :
788 12673 : max_e = exp_buf[0];
789 12673 : move16();
790 4068033 : FOR( j = 0; j < L_FRAME16k; j++ )
791 : {
792 4055360 : IF( GT_16( exp_buf[j], max_e ) )
793 : {
794 25417 : max_e = exp_buf[j];
795 25417 : move16();
796 : }
797 : }
798 :
799 4068033 : FOR( j = 0; j < L_FRAME16k; j++ )
800 : {
801 4055360 : excSHB_nonref_fx[j] = L_shr( excSHB_nonref_fx[j], sub( max_e, exp_buf[j] ) ); /* Q31-max_e */
802 4055360 : move32();
803 : }
804 : /* LP synthesis */
805 :
806 12673 : Copy_Scale_sig32( hStereoICBWE->mem_lpc_shbsynth_nonref_fx, hStereoICBWE->mem_lpc_shbsynth_nonref_fx, LPC_SHB_ORDER, sub( hStereoICBWE->mem_lpc_shbsynth_nonref_e, s_max( hStereoICBWE->mem_lpc_shbsynth_nonref_e, max_e ) ) ); /* Q31-tmp_e */
807 12673 : Copy_Scale_sig32( excSHB_nonref_fx, excSHB_nonref_fx, L_FRAME16k, sub( max_e, s_max( hStereoICBWE->mem_lpc_shbsynth_nonref_e, max_e ) ) ); /* Q31-tmp_e */
808 12673 : max_e = s_max( hStereoICBWE->mem_lpc_shbsynth_nonref_e, max_e );
809 12673 : hStereoICBWE->mem_lpc_shbsynth_nonref_e = s_max( hStereoICBWE->mem_lpc_shbsynth_nonref_e, max_e );
810 :
811 12673 : syn_filt_fx32( hStereoICBWE->lpSHBRef_e, hStereoICBWE->lpSHBRef_fx, LPC_SHB_ORDER, excSHB_nonref_fx, max_e, shb_synth_nonref_fx, &shb_synth_nonref_e, L_FRAME16k, hStereoICBWE->mem_lpc_shbsynth_nonref_fx, &hStereoICBWE->mem_lpc_shbsynth_nonref_e, 1 );
812 : }
813 : ELSE
814 : {
815 14070 : Copy_Scale_sig_32_16( shb_frame_ref_fx, hStereoICBWE->shbSynthRef_fx, L_FRAME16k, -16 ); /* Q15-shb_frame_ref_e */
816 14070 : hStereoICBWE->shbSynthRef_e = shb_frame_ref_e;
817 14070 : move16();
818 14070 : Copy32( shb_frame_ref_fx, shb_synth_nonref_fx, L_FRAME16k ); /* Q31-shb_frame_ref_e */
819 14070 : shb_synth_nonref_e = shb_frame_ref_e;
820 14070 : move16();
821 14070 : set32_fx( hStereoICBWE->mem_lpc_shbsynth_nonref_fx, 0, 10 );
822 14070 : hStereoICBWE->mem_lpc_shbsynth_nonref_e = 0;
823 14070 : move16();
824 : }
825 :
826 26743 : icbwe_dft_stereo_param_ivas_fx( hStereoICBWE, hStereoDft, st, shb_synth_nonref_fx, &shb_synth_nonref_e );
827 : }
828 : ELSE
829 : {
830 :
831 103 : max_e = s_max( hStereoICBWE->mem_shb_speech_ref_e, shb_speech_ref_e );
832 103 : Copy_Scale_sig( hStereoICBWE->mem_shb_speech_ref_fx, hStereoICBWE->mem_shb_speech_ref_fx, L_LOOK_16k, negate( sub( max_e, hStereoICBWE->mem_shb_speech_ref_e ) ) ); // mem_shb_speech_ref_e
833 :
834 103 : set32_fx( shb_frame_ref_fx, 0, L_LOOK_16k + L_FRAME16k );
835 103 : Copy_Scale_sig_16_32_no_sat( hStereoICBWE->mem_shb_speech_ref_fx, shb_frame_ref_fx, L_LOOK_16k, add( negate( sub( max_e, hStereoICBWE->mem_shb_speech_ref_e ) ), Q16 ) ); // mem_shb_speech_ref_e
836 103 : hStereoICBWE->mem_shb_speech_ref_e = max_e;
837 103 : shb_frame_ref_e = max_e;
838 103 : move16();
839 103 : move16();
840 : /* shb_frame_ref_ICBWE @ (8.75 - shb_speech_delay (=1.25) ms) after shb_speech */
841 :
842 103 : Copy_Scale_sig32( shb_speech_ref_fx, shb_frame_ref_fx + L_LOOK_16k - refMemLen, L_FRAME16k, negate( sub( max_e, shb_speech_ref_e ) ) ); /* Q31-max_e */
843 103 : Copy_Scale_sig_32_16( shb_frame_ref_fx + L_FRAME16k, hStereoICBWE->mem_shb_speech_ref_fx, L_LOOK_16k, sub( negate( sub( max_e, hStereoICBWE->mem_shb_speech_ref_e ) ), Q16 ) ); // mem_shb_speech_ref_e
844 :
845 103 : set32_fx( shb_frame_nonref_fx, 0, L_LOOK_16k + L_FRAME16k );
846 :
847 103 : max_e = s_max( hStereoICBWE->mem_shb_speech_nonref_e, shb_speech_nonref_e );
848 103 : Copy_Scale_sig( hStereoICBWE->mem_shb_speech_nonref_fx, hStereoICBWE->mem_shb_speech_nonref_fx, L_LOOK_16k, negate( sub( max_e, hStereoICBWE->mem_shb_speech_nonref_e ) ) ); // mem_shb_speech_ref_e
849 103 : Copy_Scale_sig_16_32_no_sat( hStereoICBWE->mem_shb_speech_nonref_fx, shb_frame_nonref_fx, L_LOOK_16k, add( negate( sub( max_e, hStereoICBWE->mem_shb_speech_nonref_e ) ), Q16 ) ); // mem_shb_speech_ref_e
850 103 : hStereoICBWE->mem_shb_speech_nonref_e = max_e;
851 103 : shb_frame_nonref_e = max_e;
852 103 : move16();
853 103 : move16();
854 : /* shb_frame_ref_ICBWE @ (8.75 - shb_speech_delay (=1.25) ms) after shb_speech */
855 :
856 103 : Copy_Scale_sig32( shb_speech_nonref_fx, shb_frame_nonref_fx + L_LOOK_16k - nonRefMemLen, L_FRAME16k, negate( sub( max_e, shb_speech_nonref_e ) ) ); /* Q31-max_e */
857 103 : Copy_Scale_sig_32_16( shb_frame_nonref_fx + L_FRAME16k, hStereoICBWE->mem_shb_speech_nonref_fx, L_LOOK_16k, sub( negate( sub( max_e, hStereoICBWE->mem_shb_speech_nonref_e ) ), Q16 ) ); // mem_shb_speech_ref_e
858 :
859 : /* core switching reset */
860 103 : test();
861 103 : test();
862 103 : test();
863 103 : test();
864 103 : IF( st->last_core != ACELP_CORE || st->core != ACELP_CORE || LT_16( st->bwidth, SWB ) || NE_16( st->element_mode, IVAS_CPE_TD ) || EQ_16( st->extl, -1 ) )
865 : {
866 29 : ic_bwe_enc_reset_fx( hStereoICBWE );
867 29 : test();
868 29 : test();
869 29 : test();
870 29 : test();
871 29 : IF( st->core != ACELP_CORE || LT_16( st->bwidth, SWB ) || LT_32( st->input_Fs, 32000 ) || NE_16( st->element_mode, IVAS_CPE_TD ) || EQ_16( st->extl, -1 ) )
872 : {
873 29 : return;
874 : }
875 : }
876 :
877 : /* resets done here. Need to move them to a separate function */
878 74 : test();
879 74 : test();
880 74 : IF( ( NE_16( hStereoICBWE->prev_refChanIndx_bwe, hStereoICBWE->refChanIndx_bwe ) ) || NE_16( st->last_extl, st->extl ) || NE_16( st->flag_ACELP16k, 1 ) )
881 : {
882 24 : hStereoICBWE->prevSpecMapping_fx = 0;
883 24 : hStereoICBWE->memShbSpecMapping_e = 0;
884 24 : move16();
885 24 : hStereoICBWE->memShbSpecMapping_fx = 0;
886 24 : move32();
887 24 : move32();
888 24 : set32_fx( hStereoICBWE->memShbSpecXcorr_fx, 0, 6 );
889 : }
890 :
891 : /* voice fac adjustment based on IC intensities */
892 : /*temp2 = (float)sqrt(sum2_f(shb_frame_ref + L_LOOK_16k - refMemLen, L_FRAME16k));*/
893 :
894 : Word32 L_tmp, L_tmp1;
895 74 : temp2_fx = hStereoICBWE->icbweRefEner_fx;
896 74 : temp2_e = hStereoICBWE->icbweRefEner_e;
897 74 : temp1_e = shb_frame_nonref_e;
898 74 : move16();
899 74 : move32();
900 74 : move32();
901 74 : L_tmp = sum2_32_fx( shb_frame_nonref_fx + sub( L_LOOK_16k, refMemLen ), L_FRAME16k, &temp1_e );
902 74 : temp1_fx = Sqrt32( L_tmp, &temp1_e ); /* Q31-temp1_e */
903 :
904 : /*temp1 = 0.8f*hStereoICBWE->prevNonRefEner + 0.2f*temp1;*/
905 : /*temp2 = 0.8f*hStereoICBWE->prevRefEner + 0.2f*temp2;*/
906 :
907 74 : IF( temp2_fx == 0 )
908 : {
909 0 : gDes_fx = 1073741824; /* 1.0 in Q30 */
910 0 : gDes_e = 1;
911 0 : move16();
912 0 : move32();
913 : }
914 : ELSE
915 : {
916 74 : tmp = BASOP_Util_Divide3232_Scale( temp1_fx, temp2_fx, &exp );
917 74 : gDes_fx = L_deposit_h( tmp ); // exp
918 74 : gDes_e = exp;
919 74 : move16();
920 : }
921 74 : tmp = gDes_e;
922 74 : exp = hStereoICBWE->gDes_pastFrame_e;
923 74 : move16();
924 74 : move16();
925 74 : L_tmp = Sqrt32( gDes_fx, &tmp ); /* Q31-tmp */
926 74 : L_tmp1 = Sqrt32( hStereoICBWE->gDes_pastFrame_fx, &exp ); /* Q31-exp */
927 :
928 74 : hStereoICBWE->gDes_pastFrame_fx = Mpy_32_32( L_tmp, L_tmp1 ); // tmp + exp
929 74 : hStereoICBWE->gDes_pastFrame_e = add( tmp, exp );
930 74 : move32();
931 74 : move16();
932 :
933 74 : hStereoICBWE->prevRefEner_fx = temp2_fx;
934 74 : hStereoICBWE->prevRefEner_e = temp2_e;
935 74 : hStereoICBWE->prevNonRefEner_fx = temp1_fx;
936 74 : hStereoICBWE->prevNonRefEner_e = temp1_e;
937 74 : move32();
938 74 : move32();
939 74 : move16();
940 74 : move16();
941 :
942 74 : test();
943 74 : test();
944 74 : IF( ( EQ_16( st->extl, SWB_TBE ) || EQ_16( st->extl, FB_TBE ) ) && EQ_16( st->flag_ACELP16k, 1 ) )
945 62 : {
946 62 : Copy32( voice_factors_fx, nlMixFac_fx, NB_SUBFR16k ); // Q31
947 :
948 62 : test();
949 62 : IF( LT_32( gTarget_fx, 268435456 ) || GT_32( gTarget_fx, 1073741824 ) ) // 0.5 in Q29 & 2.0 in Q29
950 : {
951 0 : v_multc_fixed( voice_factors_fx, 1073741824 /*0.5 in Q31*/, nlMixFac_fx, NB_SUBFR16k ); /* Q31 */
952 : }
953 :
954 62 : Word16 L_FRAME16k_by_nbSubFr = L_FRAME16k / NB_SUBFR;
955 62 : move16();
956 62 : IF( st->flag_ACELP16k == 0 )
957 : {
958 0 : nbSubFr = NB_SUBFR;
959 0 : move16();
960 : }
961 : ELSE
962 : {
963 62 : nbSubFr = NB_SUBFR16k;
964 62 : move16();
965 62 : L_FRAME16k_by_nbSubFr = L_FRAME16k / NB_SUBFR16k;
966 62 : move16();
967 : }
968 :
969 : Word16 exp_buf[L_FRAME16k];
970 62 : set16_fx( exp_buf, 0, L_FRAME16k );
971 372 : FOR( ( i = 0, k = 0 ); i < nbSubFr; i++ )
972 : {
973 310 : test();
974 310 : IF( EQ_16( st->coder_type, UNVOICED ) || EQ_16( hStereoICBWE->MSFlag, 1 ) )
975 : {
976 :
977 310 : temp1_fx = 0;
978 310 : temp1_e = 0;
979 310 : temp2_fx = ONE_IN_Q31; /* Q31 */
980 310 : temp2_e = 0;
981 310 : move32();
982 310 : move32();
983 310 : move16();
984 310 : move16();
985 : }
986 : ELSE
987 : {
988 0 : temp1_e = 0, temp2_e = 0;
989 0 : temp1_fx = Sqrt32( nlMixFac_fx[i], &temp1_e ); /* Q31-temp1_e */
990 0 : temp2_fx = Sqrt32( L_sub( ONE_IN_Q31, nlMixFac_fx[i] ), &temp2_e ); /* Q31 */
991 0 : move16();
992 0 : move16();
993 : }
994 :
995 20150 : FOR( j = 0; j < L_FRAME16k_by_nbSubFr; ( j++, k++ ) )
996 : {
997 19840 : excSHB_nonref_fx[k] = BASOP_Util_Add_Mant32Exp( Mpy_32_32( temp1_fx, L_deposit_h( hStereoICBWE->nlExc16k_fx[k] ) ), add( temp1_e, hStereoICBWE->nlExc16k_e ), Mpy_32_32( temp2_fx, L_deposit_h( hStereoICBWE->mixExc16k_fx[k] ) ), add( temp2_e, hStereoICBWE->mixExc16k_e ), &exp_buf[k] ); /* Q31-exp_buf */
998 19840 : move32();
999 : }
1000 : }
1001 :
1002 62 : max_e = exp_buf[0];
1003 62 : move16();
1004 19902 : FOR( j = 0; j < L_FRAME16k; j++ )
1005 : {
1006 19840 : IF( GT_16( exp_buf[j], max_e ) )
1007 : {
1008 94 : max_e = exp_buf[j];
1009 94 : move16();
1010 : }
1011 : }
1012 :
1013 19902 : FOR( j = 0; j < L_FRAME16k; j++ )
1014 : {
1015 19840 : excSHB_nonref_fx[j] = L_shr( excSHB_nonref_fx[j], sub( max_e, exp_buf[j] ) ); /* Q31-max_e */
1016 19840 : move32();
1017 : }
1018 : /* LP synthesis */
1019 62 : Copy_Scale_sig32( hStereoICBWE->mem_lpc_shbsynth_nonref_fx, hStereoICBWE->mem_lpc_shbsynth_nonref_fx, LPC_SHB_ORDER, sub( hStereoICBWE->mem_lpc_shbsynth_nonref_e, s_max( hStereoICBWE->mem_lpc_shbsynth_nonref_e, max_e ) ) ); /* Q31-tmp_e */
1020 62 : Copy_Scale_sig32( excSHB_nonref_fx, excSHB_nonref_fx, L_FRAME16k, sub( max_e, s_max( hStereoICBWE->mem_lpc_shbsynth_nonref_e, max_e ) ) ); /* Q31-tmp_e */
1021 62 : max_e = s_max( hStereoICBWE->mem_lpc_shbsynth_nonref_e, max_e );
1022 62 : hStereoICBWE->mem_lpc_shbsynth_nonref_e = s_max( hStereoICBWE->mem_lpc_shbsynth_nonref_e, max_e );
1023 :
1024 62 : syn_filt_fx32( hStereoICBWE->lpSHBRef_e, hStereoICBWE->lpSHBRef_fx, LPC_SHB_ORDER, excSHB_nonref_fx, max_e, shb_synth_nonref_fx, &shb_synth_nonref_e, L_FRAME16k, hStereoICBWE->mem_lpc_shbsynth_nonref_fx, &hStereoICBWE->mem_lpc_shbsynth_nonref_e, 1 );
1025 : }
1026 : ELSE
1027 : {
1028 12 : Copy_Scale_sig_32_16( shb_frame_ref_fx, hStereoICBWE->shbSynthRef_fx, L_FRAME16k, -16 ); // max_e
1029 12 : hStereoICBWE->shbSynthRef_e = shb_frame_ref_e;
1030 12 : move16();
1031 12 : Copy32( shb_frame_ref_fx, shb_synth_nonref_fx, L_FRAME16k ); /* Q31-shb_synth_nonref_e */
1032 12 : shb_synth_nonref_e = shb_frame_ref_e;
1033 12 : move16();
1034 12 : set32_fx( hStereoICBWE->mem_lpc_shbsynth_nonref_fx, 0, 10 );
1035 12 : hStereoICBWE->mem_lpc_shbsynth_nonref_e = 0;
1036 12 : move16();
1037 : }
1038 :
1039 74 : test();
1040 74 : test();
1041 74 : test();
1042 74 : IF( ( EQ_16( st->extl, SWB_TBE ) || EQ_16( st->extl, WB_TBE ) || EQ_16( st->extl, FB_TBE ) ) && EQ_16( st->flag_ACELP16k, 1 ) )
1043 : {
1044 : /* IC BWE spectral mapping */
1045 62 : spIndx = ic_bwe_enc_specMapping_ivas_fx( shb_frame_nonref_fx, shb_frame_nonref_e, shb_synth_nonref_fx, &shb_synth_nonref_e, &( hStereoICBWE->prevSpecMapping_fx ), &( hStereoICBWE->memShbSpecMapping_fx ), &( hStereoICBWE->memShbSpecMapping_e ), hStereoICBWE->memShbSpecXcorr_fx, &( hStereoICBWE->memShbSpecXcorr_e ) ); /* Q0 */
1046 : }
1047 : ELSE
1048 : {
1049 12 : spIndx = 3;
1050 12 : hStereoICBWE->prevSpecMapping_fx = 0;
1051 12 : move16();
1052 12 : move32();
1053 : }
1054 :
1055 74 : push_indice( hBstr, IND_STEREO_ICBWE_REF, hStereoICBWE->refChanIndx_bwe, STEREO_ICBWE_REFBITS );
1056 :
1057 74 : IF( EQ_16( st->flag_ACELP16k, 1 ) )
1058 : {
1059 62 : push_indice( hBstr, IND_STEREO_ICBWE_SP, spIndx, STEREO_ICBWE_SPBITS );
1060 : }
1061 : ELSE
1062 : {
1063 12 : hStereoICBWE->prevSpecMapping_fx = -1288490188; // -0.6 in Q31
1064 12 : move32();
1065 : }
1066 :
1067 : /* IC BWE GS mapping */
1068 :
1069 74 : gsIndx = ic_bwe_enc_gsMapping_ivas_fx( hStereoICBWE->gDes_pastFrame_fx, hStereoICBWE->gDes_pastFrame_e, hStereoICBWE->shbSynthRef_fx, hStereoICBWE->shbSynthRef_e, shb_synth_nonref_fx, &shb_synth_nonref_e, &( hStereoICBWE->prevgsMapping_fx ), &( hStereoICBWE->prevgsMapping_e ), hStereoICBWE->memGsEnerMap_fx, &hStereoICBWE->memGsEnerMap_e, st->element_mode ); /* Q0 */
1070 :
1071 74 : hStereoICBWE->gDes_pastFrame_fx = gDes_fx; /* Q31-hStereoICBWE->gDes_pastFrame_e */
1072 74 : hStereoICBWE->gDes_pastFrame_e = gDes_e;
1073 74 : move32();
1074 74 : move16();
1075 74 : push_indice( hBstr, IND_STEREO_ICBWE_GS, gsIndx, STEREO_ICBWE_GSBITS );
1076 : }
1077 :
1078 : /* IC-BWE updates */
1079 26817 : hStereoICBWE->prev_refChanIndx_bwe = hStereoICBWE->refChanIndx_bwe; /* Q0 */
1080 26817 : move16();
1081 :
1082 26817 : return;
1083 : }
1084 :
1085 :
1086 : /*-------------------------------------------------------------------*
1087 : * stereo_icBWE_init_enc()
1088 : *
1089 : * Stereo (inter-channel) BWE mapping - encoder initialization
1090 : *-------------------------------------------------------------------*/
1091 880 : void stereo_icBWE_init_enc_fx(
1092 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE /* i/o: Stereo inter-channel BWE handle */
1093 : )
1094 : {
1095 : /* SHB speech resampler memory */
1096 880 : set16_fx( hStereoICBWE->mem_decim_shb_ch0_fx, 0, shl( L_FILT_MAX, 1 ) );
1097 :
1098 : /* SHB ref channel */
1099 880 : set16_fx( hStereoICBWE->mem_shb_speech_ref_fx, 0, L_LOOK_16k );
1100 880 : hStereoICBWE->mem_shb_speech_ref_e = 0;
1101 880 : move16();
1102 :
1103 : /* SHB non-ref channel */
1104 880 : set16_fx( hStereoICBWE->mem_shb_speech_nonref_fx, 0, L_LOOK_16k );
1105 880 : hStereoICBWE->mem_shb_speech_nonref_e = 0;
1106 880 : move16();
1107 :
1108 : /* unscaled & scaled SHB synthesis memory */
1109 880 : set32_fx( hStereoICBWE->mem_lpc_shbsynth_nonref_fx, 0, LPC_SHB_ORDER );
1110 880 : hStereoICBWE->mem_lpc_shbsynth_nonref_e = 0;
1111 880 : move16();
1112 :
1113 : /* inter-channel BWE spectral shape adj. */
1114 880 : hStereoICBWE->prevSpecMapping_fx = 0;
1115 880 : hStereoICBWE->prevgsMapping_fx = MAX_32; // Q31
1116 880 : move32();
1117 880 : move32();
1118 880 : set32_fx( &( hStereoICBWE->memShbSpecMapping_fx ), 0, 1 );
1119 880 : hStereoICBWE->memShbSpecMapping_e = 0;
1120 880 : move16();
1121 880 : set32_fx( hStereoICBWE->memShbSpecXcorr_fx, 0, 6 );
1122 880 : hStereoICBWE->memShbSpecXcorr_e = 0;
1123 880 : move16();
1124 :
1125 880 : hStereoICBWE->prevNonRefEner_fx = MAX_32; // Q31
1126 880 : hStereoICBWE->prevRefEner_fx = MAX_32; // Q31
1127 880 : move32();
1128 880 : move32();
1129 :
1130 880 : set32_fx( hStereoICBWE->memGsEnerMap_fx, MAX_32, 2 ); // Q31
1131 :
1132 880 : set16_fx( hStereoICBWE->dec_2over3_mem_fx, 0, L_FILT_2OVER3 );
1133 880 : set16_fx( hStereoICBWE->dec_2over3_mem_lp_fx, 0, L_FILT_2OVER3_LP );
1134 880 : set16_fx( hStereoICBWE->memHPF_fx, 0, 8 );
1135 :
1136 : /* BWE ref channel */
1137 880 : hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
1138 880 : hStereoICBWE->prev_refChanIndx_bwe = L_CH_INDX;
1139 880 : move16();
1140 880 : move16();
1141 :
1142 880 : set16_fx( hStereoICBWE->memModifyFs_icbwe_fx[0], 0, shl( L_FILT32k, 1 ) );
1143 880 : set16_fx( hStereoICBWE->memModifyFs_icbwe_fx[1], 0, shl( L_FILT32k, 1 ) );
1144 :
1145 880 : set32_fx( hStereoICBWE->mem_nrg_L_fx, 0, 2 );
1146 880 : hStereoICBWE->mem_nrg_L_fx_e = 0;
1147 880 : move16();
1148 880 : set32_fx( hStereoICBWE->mem_nrg_R_fx, 0, 2 );
1149 880 : hStereoICBWE->mem_nrg_R_fx_e = 0;
1150 880 : move16();
1151 880 : set32_fx( hStereoICBWE->mem_nrg_DMX_fx, 0, 2 );
1152 880 : hStereoICBWE->mem_nrg_DMX_fx_e = 0;
1153 880 : move16();
1154 :
1155 880 : hStereoICBWE->gDes_pastFrame_fx = MAX_32; // Q31
1156 880 : hStereoICBWE->icbweRefEner_fx = 0;
1157 880 : move32();
1158 880 : move32();
1159 880 : hStereoICBWE->gDes_pastFrame_fx = MAX_32; // Q31
1160 880 : move32();
1161 880 : hStereoICBWE->gDes_pastFrame_e = 0; // Q31
1162 880 : move16();
1163 880 : hStereoICBWE->prevgsMapping_fx = MAX_32;
1164 880 : move32();
1165 880 : hStereoICBWE->prevgsMapping_e = 0;
1166 880 : move16();
1167 880 : hStereoICBWE->MSFlag = 0;
1168 880 : move16();
1169 :
1170 880 : set16_fx( hStereoICBWE->nlExc16k_fx, 0, L_FRAME16k );
1171 880 : hStereoICBWE->nlExc16k_e = 0;
1172 880 : move16();
1173 880 : set16_fx( hStereoICBWE->mixExc16k_fx, 0, L_FRAME16k );
1174 880 : hStereoICBWE->mixExc16k_e = 0;
1175 880 : move16();
1176 :
1177 880 : return;
1178 : }
1179 : /*-------------------------------------------------------------------*
1180 : * findRefChanBWE()
1181 : *
1182 : * Inter-channel BWE pre-proc of the non-ref channel
1183 : *-------------------------------------------------------------------*/
1184 74 : static void findRefChanBWE_fx(
1185 : const Word16 *input0_fx, /* i/o : input 0 Qx q_dataChan_fx*/
1186 : const Word16 *input1_fx, /* i/o : input 1 Qx q_dataChan_fx*/
1187 : Word16 *refChanIndx_bwe, /* o : Ref Channel Index bwe Q0*/
1188 : const Word16 input_frame, /* i : input 0 & 1 frame length Q0*/
1189 : Word16 *mem0_fx, /* i/o : mem 0 for input 0 Qx q_dataChan_fx*/
1190 : Word16 *mem1_fx /* i/o : mem 1 for input 1 Qx q_dataChan_fx*/
1191 : )
1192 : {
1193 : Word16 inp0_fx[L_FRAME48k], inp1_fx[L_FRAME48k];
1194 : Word32 tempF_fx, tempF1_fx;
1195 :
1196 74 : Copy( input0_fx, inp0_fx, input_frame ); /* q_dataChan_fx */
1197 74 : Copy( input1_fx, inp1_fx, input_frame ); /* q_dataChan_fx */
1198 :
1199 74 : spectral_balancer_fx16( inp0_fx, mem0_fx, input_frame, 1 );
1200 74 : spectral_balancer_fx16( inp1_fx, mem1_fx, input_frame, 1 );
1201 :
1202 74 : tempF_fx = sum2_f_16_gb_fx( inp0_fx, input_frame, find_guarded_bits_fx( input_frame ) ); // Q(2x-find_guarded_bits_fx( input_frame ))
1203 74 : tempF1_fx = sum2_f_16_gb_fx( inp1_fx, input_frame, find_guarded_bits_fx( input_frame ) ); // Q(2x-find_guarded_bits_fx( input_frame ))
1204 :
1205 74 : IF( LT_32( tempF1_fx, Mpy_32_32( 1374389535 /* 0.64f Q31 */, tempF_fx ) ) )
1206 : {
1207 0 : *refChanIndx_bwe = L_CH_INDX; /* Q0 */
1208 0 : move16();
1209 : }
1210 74 : ELSE IF( LT_32( tempF_fx, Mpy_32_32( 1374389535 /* 0.64f Q31 */, tempF1_fx ) ) )
1211 : {
1212 10 : *refChanIndx_bwe = R_CH_INDX; /* Q0 */
1213 10 : move16();
1214 : }
1215 :
1216 74 : return;
1217 : }
1218 :
1219 :
1220 : /*-------------------------------------------------------------------*
1221 : * stereo_icBWE_preproc()
1222 : *
1223 : * Inter-channel BWE pre-proc of the non-ref channel
1224 : *-------------------------------------------------------------------*/
1225 48219 : void stereo_icBWE_preproc_fx(
1226 : CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure */
1227 : const Word16 input_frame, /* i : input frame length Q0*/
1228 : Word16 shb_speech_nonref_fx[], /* o : SHB speech non-ref channel q_shb_speech_nonref_fx*/
1229 : Word16 q_shb_speech_nonref_fx /* i : Q SHB speech non-ref channel */
1230 : )
1231 : {
1232 : Word16 i, refChanIndx_bwe;
1233 : STEREO_TCA_ENC_HANDLE hStereoTCA;
1234 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE;
1235 : Encoder_State *st;
1236 : Word16 temp_inp_fx[L_FRAME48k];
1237 : Word16 tempSHB_fx[L_FRAME16k];
1238 : Word16 temp_in_fx[L_FRAME48k];
1239 : Word16 /*nonRefOffset,*/ nonRefMemLen;
1240 : Word32 gD_fx;
1241 : Word32 input_Fs;
1242 :
1243 48219 : set16_fx( temp_inp_fx, 0, L_FRAME48k );
1244 :
1245 : /* initialization */
1246 48219 : hStereoTCA = hCPE->hStereoTCA; /* Stereo TCA encoder handle */
1247 48219 : hStereoICBWE = hCPE->hStereoICBWE; /* Stereo IC BWE encoder handle */
1248 48219 : st = hCPE->hCoreCoder[0];
1249 48219 : input_Fs = st->input_Fs;
1250 48219 : move32();
1251 :
1252 : /*nonRefOffset = NS2SA(st_ivas->input_Fs, ACELP_LOOK_NS - L_MEM_RECALC_TBE_NS);*/
1253 48219 : nonRefMemLen = NS2SA_FX2( input_Fs, L_MEM_RECALC_TBE_NS );
1254 :
1255 48219 : test();
1256 48219 : test();
1257 48219 : test();
1258 48219 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) || NE_16( st->core, ACELP_CORE ) || EQ_16( st->extl, -1 ) || LT_32( input_Fs, 32000 ) )
1259 : {
1260 12844 : set16_fx( hStereoICBWE->dec_2over3_mem_fx, 0, L_FILT_2OVER3 );
1261 12844 : set16_fx( hStereoICBWE->dec_2over3_mem_lp_fx, 0, L_FILT_2OVER3_LP );
1262 12844 : set16_fx( hStereoICBWE->mem_decim_shb_ch0_fx, 0, ( 2 * L_FILT_MAX ) );
1263 12844 : set16_fx( shb_speech_nonref_fx, 0, L_FRAME16k );
1264 12844 : set16_fx( hStereoICBWE->memHPF_fx, 0, 8 );
1265 12844 : hStereoICBWE->refChanIndx_bwe = hStereoICBWE->prev_refChanIndx_bwe = L_CH_INDX;
1266 12844 : move16();
1267 12844 : move16();
1268 :
1269 12844 : test();
1270 12844 : IF( GE_16( hCPE->element_mode, IVAS_CPE_DFT ) && GE_32( input_Fs, 32000 ) )
1271 : {
1272 12844 : set16_fx( temp_inp_fx, 0, L_FRAME48k );
1273 12844 : Copy( hStereoICBWE->dataChan_fx[0], temp_inp_fx + nonRefMemLen, sub( input_frame, nonRefMemLen ) ); /* q_dataChan_fx */
1274 :
1275 12844 : IF( EQ_32( input_Fs, 48000 ) )
1276 : {
1277 5867 : Copy( temp_inp_fx, temp_in_fx, L_FRAME48k ); /* q_dataChan_fx */
1278 5867 : set16_fx( temp_inp_fx, 0, L_FRAME48k );
1279 5867 : i = 2 * L_FILT48k + L_FILT_2OVER3;
1280 5867 : move16();
1281 :
1282 5867 : decimate_2_over_3_allpass_fx( temp_in_fx + sub( input_frame, i ), i, temp_inp_fx + mult( sub( input_frame, i ), 21845 /* 2/3 Q15*/ ), hStereoICBWE->dec_2over3_mem_fx, allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hStereoICBWE->dec_2over3_mem_lp_fx );
1283 : }
1284 : /* flip the spectrum */
1285 4122924 : FOR( i = 0; i < L_FRAME32k; i += 2 )
1286 : {
1287 4110080 : temp_inp_fx[i] = negate( temp_inp_fx[i] ); /* q_dataChan_fx */
1288 4110080 : move16();
1289 : }
1290 12844 : Copy( temp_inp_fx + L_FRAME32k - 2 * L_FILT32k, hStereoICBWE->memModifyFs_icbwe_fx[0], 2 * L_FILT32k ); /* q_dataChan_fx */
1291 :
1292 12844 : set16_fx( temp_inp_fx, 0, L_FRAME48k );
1293 12844 : Copy( hStereoICBWE->dataChan_fx[1], temp_inp_fx + nonRefMemLen, sub( input_frame, nonRefMemLen ) ); /* q_dataChan_fx */
1294 :
1295 12844 : IF( EQ_32( input_Fs, 48000 ) )
1296 : {
1297 5867 : Copy( temp_inp_fx, temp_in_fx, L_FRAME48k ); /* q_dataChan_fx */
1298 5867 : set16_fx( temp_inp_fx, 0, L_FRAME48k );
1299 5867 : i = 2 * L_FILT48k + L_FILT_2OVER3;
1300 5867 : move16();
1301 :
1302 : /* IVAS-219: the same filter memories are used for both channels; the continuity is thus lost; shouldn't the memories be separated? */
1303 5867 : decimate_2_over_3_allpass_fx( temp_in_fx + sub( input_frame, i ), i, temp_inp_fx + mult( sub( input_frame, i ), 21845 /* 2/3 Q15*/ ), hStereoICBWE->dec_2over3_mem_fx, allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hStereoICBWE->dec_2over3_mem_lp_fx );
1304 : }
1305 : /* flip the spectrum */
1306 5061644 : FOR( i = 0; i < input_frame; i += 2 )
1307 : {
1308 5048800 : temp_inp_fx[i] = negate( temp_inp_fx[i] ); /* q_dataChan_fx */
1309 5048800 : move16();
1310 : }
1311 12844 : Copy( temp_inp_fx + L_FRAME32k - 2 * L_FILT32k, hStereoICBWE->memModifyFs_icbwe_fx[1], 2 * L_FILT32k ); /* q_dataChan_fx */
1312 : }
1313 :
1314 12844 : return;
1315 : }
1316 :
1317 35375 : IF( NE_16( hCPE->element_mode, IVAS_CPE_TD ) )
1318 : {
1319 : /* initial estimate of refChanIndx_bwe */
1320 35301 : gD_fx = hStereoTCA->targetGain_fx; /* Q29 */
1321 35301 : move32();
1322 :
1323 35301 : refChanIndx_bwe = hStereoTCA->refChanIndx; /* Q0 */
1324 35301 : move16();
1325 35301 : test();
1326 35301 : test();
1327 35301 : test();
1328 35301 : IF( ( GT_32( gD_fx, ONE_IN_Q29 ) && EQ_16( refChanIndx_bwe, R_CH_INDX ) ) || ( LT_32( gD_fx, ONE_IN_Q29 ) && EQ_16( refChanIndx_bwe, L_CH_INDX ) ) )
1329 : {
1330 21783 : refChanIndx_bwe = !( hStereoTCA->refChanIndx );
1331 21783 : move16();
1332 : }
1333 35301 : hStereoICBWE->refChanIndx_bwe = refChanIndx_bwe; /* Q0 */
1334 35301 : move16();
1335 :
1336 35301 : set16_fx( hStereoICBWE->dec_2over3_mem_fx, 0, L_FILT_2OVER3 );
1337 35301 : set16_fx( hStereoICBWE->dec_2over3_mem_lp_fx, 0, L_FILT_2OVER3_LP );
1338 35301 : set16_fx( hStereoICBWE->mem_decim_shb_ch0_fx, 0, ( 2 * L_FILT_MAX ) );
1339 35301 : set16_fx( shb_speech_nonref_fx, 0, L_FRAME16k );
1340 35301 : set16_fx( hStereoICBWE->memHPF_fx, 0, 8 );
1341 :
1342 35301 : return;
1343 : }
1344 :
1345 74 : gD_fx = hStereoTCA->targetGain_fx; /* Q29 */
1346 74 : move32();
1347 :
1348 74 : refChanIndx_bwe = hStereoTCA->refChanIndx; /* Q0 */
1349 74 : move16();
1350 74 : test();
1351 74 : test();
1352 74 : test();
1353 74 : IF( ( GT_32( gD_fx, ONE_IN_Q29 ) && EQ_16( refChanIndx_bwe, R_CH_INDX ) ) || ( LT_32( gD_fx, ONE_IN_Q29 ) && EQ_16( refChanIndx_bwe, L_CH_INDX ) ) )
1354 : {
1355 3 : refChanIndx_bwe = !( hStereoTCA->refChanIndx );
1356 3 : move16();
1357 : }
1358 :
1359 74 : findRefChanBWE_fx( hStereoICBWE->dataChan_fx[0], hStereoICBWE->dataChan_fx[1], &refChanIndx_bwe, input_frame, &hStereoICBWE->memHPF_fx[0], &hStereoICBWE->memHPF_fx[4] );
1360 :
1361 74 : hStereoICBWE->refChanIndx_bwe = refChanIndx_bwe;
1362 74 : move16();
1363 :
1364 74 : IF( EQ_16( refChanIndx_bwe, R_CH_INDX ) )
1365 : {
1366 10 : Copy( hStereoICBWE->icbwe_inp_mem_fx[0], temp_inp_fx, nonRefMemLen ); /* q_dataChan_fx */
1367 10 : Copy( hStereoICBWE->dataChan_fx[0], temp_inp_fx + nonRefMemLen, sub( input_frame, nonRefMemLen ) ); /* q_dataChan_fx */
1368 : }
1369 : ELSE
1370 : {
1371 64 : Copy( hStereoICBWE->icbwe_inp_mem_fx[1], temp_inp_fx, nonRefMemLen ); /* q_dataChan_fx */
1372 64 : Copy( hStereoICBWE->dataChan_fx[1], temp_inp_fx + nonRefMemLen, sub( input_frame, nonRefMemLen ) ); /* q_dataChan_fx */
1373 : }
1374 :
1375 74 : IF( EQ_32( input_Fs, 48000 ) )
1376 : {
1377 : /*printf("\n Non-ref target HB generation for 48 kHz sample rate needs alignment verification \n");*/
1378 2 : Copy( temp_inp_fx, temp_in_fx, L_FRAME48k ); /* q_dataChan_fx */
1379 2 : set16_fx( temp_inp_fx, 0, L_FRAME48k );
1380 :
1381 2 : decimate_2_over_3_allpass_fx( temp_in_fx, L_FRAME48k, temp_inp_fx, hStereoICBWE->dec_2over3_mem_fx, allpass_poles_3_ov_2, decimate_3_ov_2_lowpass_num_fx, decimate_3_ov_2_lowpass_den_fx, hStereoICBWE->dec_2over3_mem_lp_fx );
1382 : }
1383 :
1384 : /* flip the spectrum */
1385 24074 : FOR( i = 0; i < input_frame; i += 2 )
1386 : {
1387 24000 : temp_inp_fx[i] = negate( temp_inp_fx[i] ); /* q_dataChan_fx */
1388 24000 : move16();
1389 : }
1390 :
1391 74 : test();
1392 74 : test();
1393 74 : test();
1394 74 : IF( GE_16( hCPE->element_mode, IVAS_CPE_DFT ) && GE_32( input_Fs, 32000 ) && ( ( st->last_core != ACELP_CORE ) || EQ_16( st->last_extl, -1 ) ) )
1395 : {
1396 0 : Copy_Scale_sig( hStereoICBWE->memModifyFs_icbwe_fx[!refChanIndx_bwe], hStereoICBWE->mem_decim_shb_ch0_fx, 2 * L_FILT32k, sub( 0, hStereoICBWE->q_dataChan_fx ) ); /* Q0 */
1397 : }
1398 :
1399 : Word16 Q_new_inp, mem_decim_size;
1400 74 : Scale_sig( temp_inp_fx, L_FRAME48k, sub( 0, hStereoICBWE->q_dataChan_fx ) ); /* q_dataChan_fx */
1401 :
1402 : /* IVAS-219: Re-wire the shb nonref estimation through a lite CLDFB */
1403 74 : modify_Fs_ivas_fx( temp_inp_fx, L_FRAME32k, 32000, tempSHB_fx, 16000, hStereoICBWE->mem_decim_shb_ch0_fx, 0, &Q_new_inp, &mem_decim_size );
1404 :
1405 74 : Copy_Scale_sig( tempSHB_fx, shb_speech_nonref_fx, L_FRAME16k, sub( q_shb_speech_nonref_fx, Q_new_inp ) ); /* q_shb_speech_nonref_fx */
1406 :
1407 74 : return;
1408 : }
|