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.h"
39 : #include "ivas_prot.h"
40 : #include "wmc_auto.h"
41 : #include "rom_com.h"
42 : #include "ivas_rom_com.h"
43 : #ifdef DEBUGGING
44 : #include "debug.h"
45 : #endif
46 :
47 : /*-------------------------------------------------------------------*
48 : * ic_bwe_enc_reset()
49 : *
50 : * core switching reset of IC BWE memory
51 : *-------------------------------------------------------------------*/
52 :
53 363282 : static void ic_bwe_enc_reset(
54 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE /* i/o: Stereo ICBWE handle */
55 : )
56 : {
57 : /* unscaled & scaled SHB synthesis memory */
58 363282 : set_f( hStereoICBWE->mem_lpc_shbsynth_nonref, 0, LPC_SHB_ORDER );
59 :
60 : /* inter-channel BWE spectral shape adj. */
61 363282 : hStereoICBWE->prevSpecMapping = 0;
62 363282 : hStereoICBWE->prevgsMapping = 1.0f;
63 363282 : set_f( &( hStereoICBWE->memShbSpecMapping ), 0, 1 );
64 363282 : set_f( hStereoICBWE->memShbSpecXcorr, 0, 6 );
65 363282 : set_f( hStereoICBWE->memGsEnerMap, 1.0f, 2 );
66 363282 : set_f( hStereoICBWE->mem_nrg_L, 0, 2 );
67 363282 : set_f( hStereoICBWE->mem_nrg_R, 0, 2 );
68 363282 : set_f( hStereoICBWE->mem_nrg_DMX, 0, 2 );
69 363282 : hStereoICBWE->gDes_pastFrame = 1.0f;
70 363282 : hStereoICBWE->icbweRefEner = 0.0f;
71 :
72 363282 : return;
73 : }
74 :
75 : /*--------------------------------------------------------------------------*
76 : * ic_bwe_specMapping()
77 : *
78 : * Encode and apply the spectral shape mapping of ref to the non-ref channel
79 : *---------------------------------------------------------------------------*/
80 :
81 619 : static int16_t ic_bwe_enc_specMapping(
82 : const float *shb_frame_target, /* i : target shb */
83 : float *shb_synth_nonref, /* o : non-ref shb synth */
84 : float *specMapping, /* i/o: current frame's mapping */
85 : float *memShbSpecMapping, /* i/o: ic bwe filtering memory */
86 : float *memShbSpecXcorr /* i/o: ic bwe spec mapping scorr memory */
87 : )
88 : {
89 : int16_t idx;
90 619 : float Txx1 = 0, Txx2 = 0, Txx3 = 0, T_desired = 0;
91 : float T_nonref_target;
92 : float temp, temp0, temp1, temp2, temp3, alpha;
93 : float a, b, c;
94 : float u, u1, u2;
95 :
96 619 : alpha = 0.5f;
97 : /* Calculate rxx(1)/rxx(0) of the non ref target */
98 619 : temp0 = dotp( shb_frame_target, shb_frame_target, L_FRAME16k - 1 );
99 619 : temp1 = dotp( shb_frame_target, shb_frame_target + 1, L_FRAME16k - 1 );
100 :
101 : /* Smoothing */
102 619 : temp0 = alpha * memShbSpecXcorr[0] + ( 1 - alpha ) * temp0;
103 619 : temp1 = alpha * memShbSpecXcorr[1] + ( 1 - alpha ) * temp1;
104 619 : memShbSpecXcorr[0] = temp0;
105 619 : memShbSpecXcorr[1] = temp1;
106 :
107 619 : T_nonref_target = ( temp0 != 0 ) ? ( temp1 / temp0 ) : ( 0 );
108 :
109 : /* Calculate rxx(1)/rxx(0) of the non ref synth */
110 619 : temp0 = dotp( shb_synth_nonref, shb_synth_nonref, L_FRAME16k - 3 );
111 619 : temp1 = dotp( shb_synth_nonref, shb_synth_nonref + 1, L_FRAME16k - 3 );
112 619 : temp2 = dotp( shb_synth_nonref, shb_synth_nonref + 2, L_FRAME16k - 3 );
113 619 : temp3 = dotp( shb_synth_nonref, shb_synth_nonref + 3, L_FRAME16k - 3 );
114 :
115 : /* Smoothing */
116 619 : temp0 = alpha * memShbSpecXcorr[2] + ( 1 - alpha ) * temp0;
117 619 : temp1 = alpha * memShbSpecXcorr[3] + ( 1 - alpha ) * temp1;
118 619 : temp2 = alpha * memShbSpecXcorr[4] + ( 1 - alpha ) * temp2;
119 619 : temp3 = alpha * memShbSpecXcorr[5] + ( 1 - alpha ) * temp3;
120 :
121 619 : memShbSpecXcorr[2] = temp0;
122 619 : memShbSpecXcorr[3] = temp1;
123 619 : memShbSpecXcorr[4] = temp2;
124 619 : memShbSpecXcorr[5] = temp3;
125 :
126 619 : if ( temp0 != 0 )
127 : {
128 619 : Txx1 = temp1 / temp0;
129 619 : Txx2 = temp2 / temp0;
130 619 : Txx3 = temp3 / temp0;
131 : }
132 :
133 619 : T_desired = T_nonref_target;
134 :
135 619 : a = 2 * Txx2 * T_desired - Txx3 - Txx1;
136 619 : b = ( 2 * T_desired * Txx1 - Txx2 - 1.0f );
137 619 : c = ( T_desired - Txx1 );
138 :
139 619 : u = *specMapping;
140 619 : temp = ( b * b - 4 * a * c );
141 619 : if ( temp >= 0 && a != 0 )
142 : {
143 558 : temp = sqrtf( temp );
144 558 : u1 = ( -b + temp ) / ( 2 * a );
145 558 : u2 = ( -b - temp ) / ( 2 * a );
146 :
147 558 : if ( fabsf( u1 ) < 1.0f && fabsf( u2 ) < 1.0f )
148 : {
149 22 : u = min( u1, u2 );
150 : }
151 536 : else if ( fabsf( u1 ) < 1.0f )
152 : {
153 0 : u = u1;
154 : }
155 536 : else if ( fabsf( u2 ) < 1.0f )
156 : {
157 530 : u = u2;
158 : }
159 : }
160 619 : u = max( min( u, 0 ), -0.6f );
161 :
162 : /* Quantize spec. mapping **/
163 619 : idx = (int16_t) usquant( u, specMapping, -0.6f, 0.2f, 1 << STEREO_ICBWE_SPBITS );
164 :
165 : /* IC-BWE spec mapping */
166 619 : deemph( shb_synth_nonref, *specMapping, L_FRAME16k, memShbSpecMapping );
167 :
168 619 : return idx;
169 : }
170 :
171 : /*----------------------------------------------------------------------*
172 : * ic_bwe_gsMapping()
173 : *
174 : * Encode and apply the gain shape mapping of ref to the non-ref channel
175 : *-----------------------------------------------------------------------*/
176 :
177 : /*! r: return quant. index value */
178 318774 : static int16_t ic_bwe_enc_gsMapping(
179 : const float relG_targ, /* i : gDes, rel gain target */
180 : const float *shbSynth, /* i : ref synth signal */
181 : float *synthSHB_nonref, /* i : nonref synth signal */
182 : float *gsMapping, /* i/o: gs mapping */
183 : float *memEner,
184 : const int16_t element_mode /* i : element mode */
185 : )
186 : {
187 : int16_t i;
188 318774 : int16_t idx = 0;
189 : float temp1, temp2;
190 : float alpha;
191 : float prev_gsMapping;
192 :
193 318774 : alpha = 0;
194 318774 : prev_gsMapping = *gsMapping;
195 :
196 : /* rel gain of synth signals */
197 102326454 : for ( i = 0, temp1 = 0, temp2 = 0; i < L_FRAME16k; i++ )
198 : {
199 102007680 : temp1 += fabsf( shbSynth[i] );
200 102007680 : temp2 += fabsf( synthSHB_nonref[i] );
201 : }
202 318774 : temp1 = alpha * memEner[0] + ( 1 - alpha ) * temp1;
203 318774 : temp2 = alpha * memEner[1] + ( 1 - alpha ) * temp2;
204 :
205 318774 : memEner[0] = temp1;
206 318774 : memEner[1] = temp2;
207 :
208 318774 : *gsMapping = ( temp2 == 0 ) ? ( *gsMapping ) : ( ( relG_targ * temp1 ) / temp2 );
209 318774 : temp2 = ( 0.0f ) * log10f( prev_gsMapping ) + ( 1.0f ) * log10f( *gsMapping ); /* no impact from prev_gsMapping */
210 :
211 : /* quantize the IC-BWE GS mapping*/
212 318774 : if ( element_mode == IVAS_CPE_TD )
213 : {
214 1552 : idx = squant( temp2, gsMapping, icbwe_gsMapping_tbl, 1 << STEREO_ICBWE_GSBITS );
215 : }
216 : else
217 : {
218 317222 : idx = squant( temp2, gsMapping, icbwe_gsMappingDFT_tbl, 1 << STEREO_ICBWE_GSBITS_DFT );
219 : }
220 :
221 318774 : *gsMapping = powf( 10, *gsMapping );
222 :
223 318774 : return idx;
224 : }
225 :
226 :
227 : /*----------------------------------------------------------------------*
228 : * icbwe_dft_stereo_param()
229 : *
230 : * Estimate ICBWE parameters in DFT stereo
231 : *-----------------------------------------------------------------------*/
232 :
233 317222 : static void icbwe_dft_stereo_param(
234 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE, /* i/o: */
235 : STEREO_DFT_ENC_DATA_HANDLE hStereoDft, /* i : */
236 : Encoder_State *st, /* i/o: */
237 : float *shb_synth_nonref /* i/o: */
238 : )
239 : {
240 : float slopeILD, normFac, gDes, alpha;
241 : int16_t gsIndx, spIndx;
242 : float *nrg_L, *nrg_R, *nrg_DMX;
243 : float sum_nrg_L, sum_nrg_R, sum_nrg_DMX;
244 317222 : const float slope_table[4] = { -2.17141801027151f, -1.29118591145892f, -0.588623666024448f, 0 };
245 317222 : const float spec_table[4] = { -0.6f, -0.4f, -0.2f, 0 };
246 :
247 317222 : BSTR_ENC_HANDLE hBstr = st->hBstr;
248 :
249 317222 : normFac = 2.5f; /* 1000 * (10/((14400+10400)/2 - (6400+10400)/2)) */
250 :
251 317222 : nrg_L = hStereoDft->nrg_L;
252 317222 : nrg_R = hStereoDft->nrg_R;
253 317222 : nrg_DMX = hStereoDft->nrg_DMX;
254 :
255 317222 : alpha = 0.0f;
256 317222 : hStereoICBWE->mem_nrg_L[0] = alpha * hStereoICBWE->mem_nrg_L[0] + ( 1 - alpha ) * nrg_L[0];
257 317222 : hStereoICBWE->mem_nrg_R[0] = alpha * hStereoICBWE->mem_nrg_R[0] + ( 1 - alpha ) * nrg_R[0];
258 317222 : hStereoICBWE->mem_nrg_DMX[0] = alpha * hStereoICBWE->mem_nrg_DMX[0] + ( 1 - alpha ) * nrg_DMX[0];
259 317222 : hStereoICBWE->mem_nrg_L[1] = alpha * hStereoICBWE->mem_nrg_L[1] + ( 1 - alpha ) * nrg_L[1];
260 317222 : hStereoICBWE->mem_nrg_R[1] = alpha * hStereoICBWE->mem_nrg_R[1] + ( 1 - alpha ) * nrg_R[1];
261 317222 : hStereoICBWE->mem_nrg_DMX[1] = alpha * hStereoICBWE->mem_nrg_DMX[1] + ( 1 - alpha ) * nrg_DMX[1];
262 :
263 317222 : sum_nrg_L = sum_f( hStereoICBWE->mem_nrg_L, 2 );
264 317222 : sum_nrg_R = sum_f( hStereoICBWE->mem_nrg_R, 2 );
265 317222 : sum_nrg_DMX = sum_f( hStereoICBWE->mem_nrg_DMX, 2 );
266 :
267 : /* refChanIndex_bwe refinement */
268 317222 : if ( ( sum_nrg_R < 0.64f * sum_nrg_L && hStereoICBWE->refChanIndx_bwe == hStereoICBWE->prev_refChanIndx_bwe ) || sum_nrg_R < 0.36f * sum_nrg_L )
269 : {
270 62042 : hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
271 : }
272 255180 : else if ( ( sum_nrg_L < 0.64f * sum_nrg_R && hStereoICBWE->refChanIndx_bwe == hStereoICBWE->prev_refChanIndx_bwe ) || sum_nrg_L < 0.36f * sum_nrg_R )
273 : {
274 72794 : hStereoICBWE->refChanIndx_bwe = R_CH_INDX;
275 : }
276 :
277 : /* Initialization of spIndx */
278 317222 : slopeILD = 0;
279 317222 : spIndx = squant( slopeILD, &slopeILD, slope_table, 1 << STEREO_ICBWE_SPBITS_DFT );
280 :
281 317222 : if ( ( st->extl == SWB_TBE || st->extl == WB_TBE || st->extl == FB_TBE ) && st->flag_ACELP16k == 1 )
282 : {
283 : /* Spec Mapping Estimate */
284 135548 : slopeILD = normFac * ( log10f( ( hStereoICBWE->mem_nrg_L[1] * hStereoICBWE->mem_nrg_R[0] ) / ( hStereoICBWE->mem_nrg_R[1] * hStereoICBWE->mem_nrg_L[0] ) ) );
285 135548 : slopeILD = ( hStereoICBWE->refChanIndx_bwe == L_CH_INDX ) ? ( -slopeILD ) : ( slopeILD );
286 135548 : spIndx = squant( slopeILD, &slopeILD, slope_table, 1 << STEREO_ICBWE_SPBITS_DFT );
287 135548 : if ( spec_table[spIndx] * hStereoICBWE->prevSpecMapping < 0 )
288 : {
289 0 : slopeILD = 0;
290 0 : spIndx = squant( slopeILD, &slopeILD, slope_table, 1 << STEREO_ICBWE_SPBITS_DFT );
291 : }
292 135548 : hStereoICBWE->prevSpecMapping = spec_table[spIndx];
293 :
294 : /* ic bwe spec mapping application */
295 135548 : deemph( shb_synth_nonref, hStereoICBWE->prevSpecMapping, L_FRAME16k, &hStereoICBWE->memShbSpecMapping );
296 : }
297 : else
298 : {
299 181674 : hStereoICBWE->memShbSpecMapping = 0;
300 181674 : hStereoICBWE->prevSpecMapping = 0;
301 : }
302 :
303 : /* gsMapping estimate */
304 :
305 317222 : gDes = ( hStereoICBWE->refChanIndx_bwe == L_CH_INDX ) ? ( sum_nrg_R / sum_nrg_DMX ) : ( sum_nrg_L / sum_nrg_DMX );
306 317222 : gDes = sqrtf( gDes );
307 :
308 317222 : alpha = 0.5f;
309 : /* Geometric Mean */
310 317222 : hStereoICBWE->gDes_pastFrame = powf( hStereoICBWE->gDes_pastFrame, alpha ) * powf( gDes, 1 - alpha );
311 :
312 317222 : gsIndx = ic_bwe_enc_gsMapping( hStereoICBWE->gDes_pastFrame, hStereoICBWE->shbSynthRef, shb_synth_nonref, &( hStereoICBWE->prevgsMapping ), hStereoICBWE->memGsEnerMap, st->element_mode );
313 317222 : hStereoICBWE->gDes_pastFrame = gDes;
314 :
315 317222 : push_indice( hBstr, IND_STEREO_ICBWE_REF, hStereoICBWE->refChanIndx_bwe, STEREO_ICBWE_REFBITS );
316 317222 : if ( st->flag_ACELP16k == 1 )
317 : {
318 150688 : push_indice( hBstr, IND_STEREO_ICBWE_SP, spIndx, STEREO_ICBWE_SPBITS_DFT );
319 : }
320 :
321 : /* IC BWE GS mapping */
322 317222 : push_indice( hBstr, IND_STEREO_ICBWE_GS, gsIndx, STEREO_ICBWE_GSBITS_DFT );
323 :
324 317222 : return;
325 : }
326 :
327 :
328 : /*-------------------------------------------------------------------*
329 : * stereo_icBWE_enc()
330 : *
331 : * Encode the spatial mapping of reference to the non-reference channels in HB
332 : *-------------------------------------------------------------------*/
333 :
334 661830 : void stereo_icBWE_enc(
335 : CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure */
336 : const float shb_speech_ref[], /* i : SHB speech ref channel */
337 : float shb_speech_nonref[], /* i/o: SHB speech non-ref channel */
338 : const float *voice_factors /* i : voicing factors */
339 : )
340 : {
341 : int16_t i, j, k, nbSubFr;
342 : STEREO_DFT_ENC_DATA_HANDLE hStereoDft;
343 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE;
344 : BSTR_ENC_HANDLE hBstr;
345 : Encoder_State *st;
346 : int16_t spIndx, gsIndx;
347 : float shb_frame_ref[L_LOOK_16k + L_FRAME16k];
348 : float excSHB_nonref[L_FRAME16k];
349 : float shb_frame_nonref[L_LOOK_16k + L_FRAME16k];
350 : float shb_synth_nonref[L_FRAME16k];
351 :
352 : float nlMixFac[NB_SUBFR16k];
353 : float temp1, temp2;
354 : float gDes, gTarget;
355 : int16_t nonRefMemLen, refMemLen;
356 :
357 : /* initialization */
358 661830 : hStereoDft = hCPE->hStereoDft;
359 661830 : hStereoICBWE = hCPE->hStereoICBWE;
360 661830 : st = hCPE->hCoreCoder[0];
361 661830 : hBstr = st->hBstr;
362 :
363 661830 : gTarget = hCPE->hStereoTCA->targetGain; /* i : gain norm for target (or R) channel */
364 :
365 661830 : nonRefMemLen = L_MEM_RECALC_TBE_16K + L_FILT16k;
366 :
367 661830 : if ( st->element_mode == IVAS_CPE_TD )
368 : {
369 2115 : refMemLen = L_MEM_RECALC_TBE_16K + 20;
370 : }
371 659715 : else if ( st->element_mode == IVAS_CPE_DFT && st->core_brate > SID_2k40 )
372 : {
373 629509 : refMemLen = 20;
374 : }
375 : else
376 : {
377 30206 : ic_bwe_enc_reset( hStereoICBWE );
378 :
379 30206 : return;
380 : }
381 :
382 631624 : if ( st->element_mode == IVAS_CPE_DFT )
383 : {
384 : /* memory resets */
385 629509 : set_f( hStereoICBWE->mem_shb_speech_nonref, 0, L_LOOK_16k );
386 629509 : set_f( shb_speech_nonref, 0, L_FRAME16k );
387 :
388 : /* core switching reset */
389 629509 : if ( st->last_core != ACELP_CORE || st->core != ACELP_CORE || st->bwidth < SWB || st->extl == -1 )
390 : {
391 332453 : ic_bwe_enc_reset( hStereoICBWE );
392 332453 : if ( st->core != ACELP_CORE || st->bwidth < SWB || st->input_Fs < 32000 || st->extl == -1 )
393 : {
394 312287 : return;
395 : }
396 : }
397 :
398 317222 : set_f( hStereoICBWE->memShbSpecXcorr, 0, 6 );
399 :
400 317222 : hStereoICBWE->prevRefEner = 0;
401 317222 : hStereoICBWE->prevNonRefEner = 0;
402 :
403 : /* SHB frame buffer offsets for TBE processing << re-adjust check >>*/
404 317222 : set_f( shb_frame_ref, 0, L_LOOK_16k + L_FRAME16k );
405 317222 : mvr2r( hStereoICBWE->mem_shb_speech_ref, shb_frame_ref, L_LOOK_16k );
406 :
407 : /* shb_frame_ref_ICBWE @ (8.75 - shb_speech_delay (=1.25) ms) after shb_speech */
408 317222 : mvr2r( shb_speech_ref, shb_frame_ref + L_LOOK_16k - refMemLen, L_FRAME16k );
409 317222 : mvr2r( shb_frame_ref + L_FRAME16k, hStereoICBWE->mem_shb_speech_ref, L_LOOK_16k );
410 :
411 317222 : if ( ( st->extl == SWB_TBE || st->extl == FB_TBE ) && st->flag_ACELP16k == 1 )
412 : {
413 135548 : mvr2r( voice_factors, nlMixFac, NB_SUBFR16k );
414 : /*if( gDes < 0.5f || gDes > 2.0f )*/
415 135548 : if ( gTarget < 0.5f || gTarget > 2.0f )
416 : {
417 30415 : v_multc( voice_factors, 0.5f, nlMixFac, NB_SUBFR16k );
418 : }
419 :
420 135548 : nbSubFr = ( st->flag_ACELP16k == 0 ) ? NB_SUBFR : NB_SUBFR16k;
421 813288 : for ( i = 0, k = 0; i < nbSubFr; i++ )
422 : {
423 677740 : if ( st->coder_type == UNVOICED || hStereoICBWE->MSFlag == 1 )
424 : {
425 169225 : temp1 = 0;
426 169225 : temp2 = 1.0f;
427 : }
428 : else
429 : {
430 508515 : temp1 = sqrtf( nlMixFac[i] );
431 508515 : temp2 = sqrtf( 1.0f - nlMixFac[i] );
432 : }
433 :
434 44053100 : for ( j = 0; j < ( L_FRAME16k / nbSubFr ); j++, k++ )
435 : {
436 43375360 : excSHB_nonref[k] = temp1 * hStereoICBWE->nlExc16k[k] + temp2 * hStereoICBWE->mixExc16k[k];
437 : }
438 : }
439 :
440 : /* LP synthesis */
441 135548 : syn_filt( hStereoICBWE->lpSHBRef, LPC_SHB_ORDER, excSHB_nonref, shb_synth_nonref, L_FRAME16k, hStereoICBWE->mem_lpc_shbsynth_nonref, 1 );
442 : }
443 : else
444 : {
445 181674 : mvr2r( shb_frame_ref, hStereoICBWE->shbSynthRef, L_FRAME16k );
446 181674 : mvr2r( shb_frame_ref, shb_synth_nonref, L_FRAME16k );
447 181674 : set_f( hStereoICBWE->mem_lpc_shbsynth_nonref, 0, 10 );
448 : }
449 :
450 317222 : icbwe_dft_stereo_param( hStereoICBWE, hStereoDft, st, shb_synth_nonref );
451 : }
452 : else
453 : {
454 : /* SHB frame buffer offsets for TBE processing << re-adjust check >>*/
455 2115 : set_f( shb_frame_ref, 0, L_LOOK_16k + L_FRAME16k );
456 2115 : mvr2r( hStereoICBWE->mem_shb_speech_ref, shb_frame_ref, L_LOOK_16k );
457 :
458 : /* shb_frame_ref_ICBWE @ (8.75 - shb_speech_delay (=1.25) ms) after shb_speech */
459 2115 : mvr2r( shb_speech_ref, shb_frame_ref + L_LOOK_16k - refMemLen, L_FRAME16k );
460 2115 : mvr2r( shb_frame_ref + L_FRAME16k, hStereoICBWE->mem_shb_speech_ref, L_LOOK_16k );
461 :
462 2115 : set_f( shb_frame_nonref, 0, L_LOOK_16k + L_FRAME16k );
463 2115 : mvr2r( hStereoICBWE->mem_shb_speech_nonref, shb_frame_nonref, L_LOOK_16k );
464 2115 : mvr2r( shb_speech_nonref, shb_frame_nonref + L_LOOK_16k - nonRefMemLen, L_FRAME16k );
465 2115 : mvr2r( shb_frame_nonref + L_FRAME16k, hStereoICBWE->mem_shb_speech_nonref, L_LOOK_16k );
466 :
467 : /* core switching reset */
468 2115 : if ( st->last_core != ACELP_CORE || st->core != ACELP_CORE || st->bwidth < SWB || st->element_mode != IVAS_CPE_TD || st->extl == -1 )
469 : {
470 623 : ic_bwe_enc_reset( hStereoICBWE );
471 :
472 623 : if ( st->core != ACELP_CORE || st->bwidth < SWB || st->input_Fs < 32000 || st->element_mode != IVAS_CPE_TD || st->extl == -1 )
473 : {
474 563 : return;
475 : }
476 : }
477 :
478 : /* resets done here. Need to move them to a separate function */
479 1552 : if ( ( hStereoICBWE->prev_refChanIndx_bwe != hStereoICBWE->refChanIndx_bwe ) || ( st->last_extl != st->extl ) || st->flag_ACELP16k != 1 )
480 : {
481 356 : hStereoICBWE->prevSpecMapping = 0;
482 356 : hStereoICBWE->memShbSpecMapping = 0;
483 356 : set_f( hStereoICBWE->memShbSpecXcorr, 0, 6 );
484 : }
485 :
486 : /* voice fac adjustment based on IC intensities */
487 : /*temp2 = (float)sqrt(sum2_f(shb_frame_ref + L_LOOK_16k - refMemLen, L_FRAME16k));*/
488 1552 : temp2 = hStereoICBWE->icbweRefEner;
489 1552 : temp1 = sqrtf( sum2_f( shb_frame_nonref + L_LOOK_16k - refMemLen, L_FRAME16k ) );
490 : /*temp1 = 0.8f*hStereoICBWE->prevNonRefEner + 0.2f*temp1;*/
491 : /*temp2 = 0.8f*hStereoICBWE->prevRefEner + 0.2f*temp2;*/
492 1552 : gDes = ( temp2 == 0 ) ? 1.0f : ( temp1 / temp2 );
493 1552 : hStereoICBWE->gDes_pastFrame = powf( hStereoICBWE->gDes_pastFrame, 0.5f ) * powf( gDes, 1 - 0.5f );
494 :
495 1552 : hStereoICBWE->prevRefEner = temp2;
496 1552 : hStereoICBWE->prevNonRefEner = temp1;
497 :
498 1552 : if ( ( st->extl == SWB_TBE || st->extl == FB_TBE ) && st->flag_ACELP16k == 1 )
499 : {
500 619 : mvr2r( voice_factors, nlMixFac, NB_SUBFR16k );
501 :
502 619 : if ( gTarget < 0.5f || gTarget > 2.0f )
503 : {
504 5 : v_multc( voice_factors, 0.5f, nlMixFac, NB_SUBFR16k );
505 : }
506 :
507 619 : nbSubFr = ( st->flag_ACELP16k == 0 ) ? NB_SUBFR : NB_SUBFR16k;
508 3714 : for ( i = 0, k = 0; i < nbSubFr; i++ )
509 : {
510 3095 : if ( st->coder_type == UNVOICED || hStereoICBWE->MSFlag == 1 )
511 : {
512 1530 : temp1 = 0;
513 1530 : temp2 = 1.0f;
514 : }
515 : else
516 : {
517 1565 : temp1 = sqrtf( nlMixFac[i] );
518 1565 : temp2 = sqrtf( 1.0f - nlMixFac[i] );
519 : }
520 :
521 201175 : for ( j = 0; j < ( L_FRAME16k / nbSubFr ); j++, k++ )
522 : {
523 198080 : excSHB_nonref[k] = temp1 * hStereoICBWE->nlExc16k[k] + temp2 * hStereoICBWE->mixExc16k[k];
524 : }
525 : }
526 :
527 : /* LP synthesis */
528 619 : syn_filt( hStereoICBWE->lpSHBRef, LPC_SHB_ORDER, excSHB_nonref, shb_synth_nonref, L_FRAME16k, hStereoICBWE->mem_lpc_shbsynth_nonref, 1 );
529 : }
530 : else
531 : {
532 933 : mvr2r( shb_frame_ref, hStereoICBWE->shbSynthRef, L_FRAME16k );
533 933 : mvr2r( shb_frame_ref, shb_synth_nonref, L_FRAME16k );
534 933 : set_f( hStereoICBWE->mem_lpc_shbsynth_nonref, 0, 10 );
535 : }
536 :
537 1552 : if ( ( st->extl == SWB_TBE || st->extl == WB_TBE || st->extl == FB_TBE ) && st->flag_ACELP16k == 1 )
538 : {
539 : /* IC BWE spectral mapping */
540 619 : spIndx = ic_bwe_enc_specMapping( shb_frame_nonref, shb_synth_nonref, &( hStereoICBWE->prevSpecMapping ), &( hStereoICBWE->memShbSpecMapping ), hStereoICBWE->memShbSpecXcorr );
541 : }
542 : else
543 : {
544 933 : spIndx = 3;
545 933 : hStereoICBWE->prevSpecMapping = 0;
546 : }
547 :
548 1552 : push_indice( hBstr, IND_STEREO_ICBWE_REF, hStereoICBWE->refChanIndx_bwe, STEREO_ICBWE_REFBITS );
549 :
550 1552 : if ( st->flag_ACELP16k == 1 )
551 : {
552 1519 : push_indice( hBstr, IND_STEREO_ICBWE_SP, spIndx, STEREO_ICBWE_SPBITS );
553 : }
554 : else
555 : {
556 33 : hStereoICBWE->prevSpecMapping = -0.6f;
557 : }
558 :
559 : /* IC BWE GS mapping */
560 1552 : gsIndx = ic_bwe_enc_gsMapping( hStereoICBWE->gDes_pastFrame, hStereoICBWE->shbSynthRef, shb_synth_nonref, &( hStereoICBWE->prevgsMapping ), hStereoICBWE->memGsEnerMap, st->element_mode );
561 1552 : hStereoICBWE->gDes_pastFrame = gDes;
562 1552 : push_indice( hBstr, IND_STEREO_ICBWE_GS, gsIndx, STEREO_ICBWE_GSBITS );
563 : }
564 :
565 : /* IC-BWE updates */
566 318774 : hStereoICBWE->prev_refChanIndx_bwe = hStereoICBWE->refChanIndx_bwe;
567 :
568 318774 : return;
569 : }
570 :
571 : /*-------------------------------------------------------------------*
572 : * stereo_icBWE_init_enc()
573 : *
574 : * Stereo (inter-channel) BWE mapping - encoder initialization
575 : *-------------------------------------------------------------------*/
576 :
577 18702 : void stereo_icBWE_init_enc(
578 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE /* i/o: Stereo inter-channel BWE handle */
579 : )
580 : {
581 : /* SHB speech resampler memory */
582 18702 : set_f( hStereoICBWE->mem_decim_shb_ch0, 0, ( 2 * L_FILT_MAX ) );
583 :
584 : /* SHB ref channel */
585 18702 : set_f( hStereoICBWE->mem_shb_speech_ref, 0, L_LOOK_16k );
586 :
587 : /* SHB non-ref channel */
588 18702 : set_f( hStereoICBWE->mem_shb_speech_nonref, 0, L_LOOK_16k );
589 :
590 : /* unscaled & scaled SHB synthesis memory */
591 18702 : set_f( hStereoICBWE->mem_lpc_shbsynth_nonref, 0, LPC_SHB_ORDER );
592 :
593 : /* inter-channel BWE spectral shape adj. */
594 18702 : hStereoICBWE->prevSpecMapping = 0;
595 18702 : hStereoICBWE->prevgsMapping = 1.0f;
596 18702 : set_f( &( hStereoICBWE->memShbSpecMapping ), 0, 1 );
597 18702 : set_f( hStereoICBWE->memShbSpecXcorr, 0, 6 );
598 :
599 18702 : hStereoICBWE->prevNonRefEner = 1.0f;
600 18702 : hStereoICBWE->prevRefEner = 1.0f;
601 :
602 18702 : set_f( hStereoICBWE->memGsEnerMap, 1.0f, 2 );
603 :
604 18702 : set_f( hStereoICBWE->dec_2over3_mem, 0, L_FILT_2OVER3 );
605 18702 : set_f( hStereoICBWE->dec_2over3_mem_lp, 0, L_FILT_2OVER3_LP );
606 18702 : set_f( hStereoICBWE->memHPF, 0, 8 );
607 :
608 : /* BWE ref channel */
609 18702 : hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
610 18702 : hStereoICBWE->prev_refChanIndx_bwe = L_CH_INDX;
611 :
612 18702 : set_f( hStereoICBWE->memModifyFs_icbwe[0], 0, 2 * L_FILT32k );
613 18702 : set_f( hStereoICBWE->memModifyFs_icbwe[1], 0, 2 * L_FILT32k );
614 :
615 18702 : set_f( hStereoICBWE->mem_nrg_L, 0, 2 );
616 18702 : set_f( hStereoICBWE->mem_nrg_R, 0, 2 );
617 18702 : set_f( hStereoICBWE->mem_nrg_DMX, 0, 2 );
618 18702 : hStereoICBWE->gDes_pastFrame = 1.0f;
619 18702 : hStereoICBWE->icbweRefEner = 0.0f;
620 :
621 18702 : hStereoICBWE->MSFlag = 0;
622 :
623 18702 : return;
624 : }
625 :
626 :
627 : /*-------------------------------------------------------------------*
628 : * findRefChanBWE()
629 : *
630 : * Inter-channel BWE pre-proc of the non-ref channel
631 : *-------------------------------------------------------------------*/
632 :
633 1552 : static void findRefChanBWE(
634 : const float *input0,
635 : const float *input1,
636 : int16_t *refChanIndx_bwe,
637 : const int16_t input_frame,
638 : float *mem0,
639 : float *mem1 )
640 : {
641 : float inp0[L_FRAME48k], inp1[L_FRAME48k];
642 : float tempF, tempF1;
643 :
644 1552 : mvr2r( input0, inp0, input_frame );
645 1552 : mvr2r( input1, inp1, input_frame );
646 :
647 1552 : spectral_balancer( inp0, mem0, input_frame, 1 );
648 1552 : spectral_balancer( inp1, mem1, input_frame, 1 );
649 :
650 1552 : tempF = sum2_f( inp0, input_frame );
651 1552 : tempF1 = sum2_f( inp1, input_frame );
652 :
653 1552 : if ( tempF1 < 0.64f * tempF )
654 : {
655 138 : *refChanIndx_bwe = L_CH_INDX;
656 : }
657 1414 : else if ( tempF < 0.64f * tempF1 )
658 : {
659 226 : *refChanIndx_bwe = R_CH_INDX;
660 : }
661 :
662 1552 : return;
663 : }
664 :
665 :
666 : /*-------------------------------------------------------------------*
667 : * stereo_icBWE_preproc()
668 : *
669 : * Inter-channel BWE pre-proc of the non-ref channel
670 : *-------------------------------------------------------------------*/
671 :
672 661830 : void stereo_icBWE_preproc(
673 : CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure */
674 : const int16_t input_frame, /* i : input frame length */
675 : float shb_speech_nonref[] /* o : SHB speech non-ref channel */
676 : )
677 : {
678 : int16_t i, refChanIndx_bwe;
679 : STEREO_TCA_ENC_HANDLE hStereoTCA;
680 : STEREO_ICBWE_ENC_HANDLE hStereoICBWE;
681 : Encoder_State *st;
682 : float temp_inp[L_FRAME48k];
683 : float tempSHB[L_FRAME16k];
684 : float temp_in[L_FRAME48k];
685 : int16_t /*nonRefOffset,*/ nonRefMemLen;
686 : float gD;
687 : int32_t input_Fs;
688 :
689 : /* initialization */
690 661830 : hStereoTCA = hCPE->hStereoTCA; /* Stereo TCA encoder handle */
691 661830 : hStereoICBWE = hCPE->hStereoICBWE; /* Stereo IC BWE encoder handle */
692 661830 : st = hCPE->hCoreCoder[0];
693 661830 : input_Fs = st->input_Fs;
694 :
695 : /*nonRefOffset = NS2SA(st_ivas->input_Fs, ACELP_LOOK_NS - L_MEM_RECALC_TBE_NS);*/
696 661830 : nonRefMemLen = NS2SA( input_Fs, L_MEM_RECALC_TBE_NS );
697 :
698 661830 : if ( hCPE->element_mode == IVAS_CPE_MDCT || st->core != ACELP_CORE || st->extl == -1 || input_Fs < 32000 )
699 : {
700 306321 : set_f( hStereoICBWE->dec_2over3_mem, 0, L_FILT_2OVER3 );
701 306321 : set_f( hStereoICBWE->dec_2over3_mem_lp, 0, L_FILT_2OVER3_LP );
702 306321 : set_f( hStereoICBWE->mem_decim_shb_ch0, 0, ( 2 * L_FILT_MAX ) );
703 306321 : set_f( shb_speech_nonref, 0, L_FRAME16k );
704 306321 : set_f( hStereoICBWE->memHPF, 0, 8 );
705 306321 : hStereoICBWE->refChanIndx_bwe = hStereoICBWE->prev_refChanIndx_bwe = L_CH_INDX;
706 :
707 306321 : if ( hCPE->element_mode >= IVAS_CPE_DFT && input_Fs >= 32000 )
708 : {
709 306321 : set_f( temp_inp, 0, L_FRAME48k );
710 306321 : mvr2r( hStereoICBWE->dataChan[0], temp_inp + nonRefMemLen, input_frame - nonRefMemLen );
711 :
712 306321 : if ( input_Fs == 48000 )
713 : {
714 177475 : mvr2r( temp_inp, temp_in, L_FRAME48k );
715 177475 : set_f( temp_inp, 0, L_FRAME48k );
716 177475 : i = 2 * L_FILT48k + L_FILT_2OVER3;
717 177475 : decimate_2_over_3_allpass( temp_in + ( input_frame - i ), i, temp_inp + ( 2 * ( input_frame - i ) ) / 3, hStereoICBWE->dec_2over3_mem, hStereoICBWE->dec_2over3_mem_lp );
718 : }
719 : /* flip the spectrum */
720 98329041 : for ( i = 0; i < L_FRAME32k; i += 2 )
721 : {
722 98022720 : temp_inp[i] *= -1.0f;
723 : }
724 306321 : mvr2r( temp_inp + L_FRAME32k - 2 * L_FILT32k, hStereoICBWE->memModifyFs_icbwe[0], 2 * L_FILT32k );
725 :
726 306321 : set_f( temp_inp, 0, L_FRAME48k );
727 306321 : mvr2r( hStereoICBWE->dataChan[1], temp_inp + nonRefMemLen, input_frame - nonRefMemLen );
728 :
729 306321 : if ( input_Fs == 48000 )
730 : {
731 177475 : mvr2r( temp_inp, temp_in, L_FRAME48k );
732 177475 : set_f( temp_inp, 0, L_FRAME48k );
733 177475 : i = 2 * L_FILT48k + L_FILT_2OVER3;
734 : /* IVAS-219: the same filter memories are used for both channels; the continuity is thus lost; shouldn't the memories be separated? */
735 177475 : decimate_2_over_3_allpass( temp_in + ( input_frame - i ), i, temp_inp + ( 2 * ( input_frame - i ) ) / 3, hStereoICBWE->dec_2over3_mem, hStereoICBWE->dec_2over3_mem_lp );
736 : }
737 : /* flip the spectrum */
738 126725041 : for ( i = 0; i < input_frame; i += 2 )
739 : {
740 126418720 : temp_inp[i] *= -1.0f;
741 : }
742 306321 : mvr2r( temp_inp + L_FRAME32k - 2 * L_FILT32k, hStereoICBWE->memModifyFs_icbwe[1], 2 * L_FILT32k );
743 : }
744 :
745 306321 : return;
746 : }
747 :
748 355509 : if ( hCPE->element_mode != IVAS_CPE_TD )
749 : {
750 : /* initial estimate of refChanIndx_bwe */
751 353957 : gD = hStereoTCA->targetGain;
752 :
753 353957 : refChanIndx_bwe = hStereoTCA->refChanIndx;
754 353957 : if ( ( gD > 1.0f && refChanIndx_bwe == R_CH_INDX ) || ( gD < 1.0f && refChanIndx_bwe == L_CH_INDX ) )
755 : {
756 163352 : refChanIndx_bwe = !( hStereoTCA->refChanIndx );
757 : }
758 353957 : hStereoICBWE->refChanIndx_bwe = refChanIndx_bwe;
759 :
760 353957 : set_f( hStereoICBWE->dec_2over3_mem, 0, L_FILT_2OVER3 );
761 353957 : set_f( hStereoICBWE->dec_2over3_mem_lp, 0, L_FILT_2OVER3_LP );
762 353957 : set_f( hStereoICBWE->mem_decim_shb_ch0, 0, ( 2 * L_FILT_MAX ) );
763 353957 : set_f( shb_speech_nonref, 0, L_FRAME16k );
764 353957 : set_f( hStereoICBWE->memHPF, 0, 8 );
765 :
766 353957 : return;
767 : }
768 :
769 1552 : gD = hStereoTCA->targetGain;
770 :
771 1552 : refChanIndx_bwe = hStereoTCA->refChanIndx;
772 1552 : if ( ( gD > 1.0f && refChanIndx_bwe == R_CH_INDX ) || ( gD < 1.0f && refChanIndx_bwe == L_CH_INDX ) )
773 : {
774 49 : refChanIndx_bwe = !( hStereoTCA->refChanIndx );
775 : }
776 :
777 1552 : findRefChanBWE( hStereoICBWE->dataChan[0], hStereoICBWE->dataChan[1], &refChanIndx_bwe, input_frame, &hStereoICBWE->memHPF[0], &hStereoICBWE->memHPF[4] );
778 :
779 1552 : hStereoICBWE->refChanIndx_bwe = refChanIndx_bwe;
780 :
781 1552 : if ( refChanIndx_bwe == R_CH_INDX )
782 : {
783 823 : mvr2r( hStereoICBWE->icbwe_inp_mem[0], temp_inp, nonRefMemLen );
784 823 : mvr2r( hStereoICBWE->dataChan[0], temp_inp + nonRefMemLen, input_frame - nonRefMemLen );
785 : }
786 : else
787 : {
788 729 : mvr2r( hStereoICBWE->icbwe_inp_mem[1], temp_inp, nonRefMemLen );
789 729 : mvr2r( hStereoICBWE->dataChan[1], temp_inp + nonRefMemLen, input_frame - nonRefMemLen );
790 : }
791 :
792 1552 : if ( input_Fs == 48000 )
793 : {
794 : /*printf("\n Non-ref target HB generation for 48 kHz sample rate needs alignment verification \n");*/
795 1441 : mvr2r( temp_inp, temp_in, L_FRAME48k );
796 1441 : set_f( temp_inp, 0, L_FRAME48k );
797 1441 : decimate_2_over_3_allpass( temp_in, L_FRAME48k, temp_inp, hStereoICBWE->dec_2over3_mem, hStereoICBWE->dec_2over3_mem_lp );
798 : }
799 :
800 : /* flip the spectrum */
801 728752 : for ( i = 0; i < input_frame; i += 2 )
802 : {
803 727200 : temp_inp[i] *= -1.0f;
804 : }
805 :
806 1552 : if ( hCPE->element_mode >= IVAS_CPE_DFT && input_Fs >= 32000 && ( st->last_core != ACELP_CORE || st->last_extl == -1 ) )
807 : {
808 60 : mvr2r( hStereoICBWE->memModifyFs_icbwe[!refChanIndx_bwe], hStereoICBWE->mem_decim_shb_ch0, 2 * L_FILT32k );
809 : }
810 :
811 : /* IVAS-219: Re-wire the shb nonref estimation through a lite CLDFB */
812 1552 : modify_Fs( temp_inp, L_FRAME32k, 32000, tempSHB, 16000, hStereoICBWE->mem_decim_shb_ch0, 0 );
813 :
814 1552 : mvr2r( tempSHB, shb_speech_nonref, L_FRAME16k );
815 :
816 1552 : return;
817 : }
|