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