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 "cnst.h"
36 : #include "rom_com.h"
37 : #include "prot.h"
38 : #include "ivas_prot.h"
39 : #include "ivas_rom_com.h"
40 : #include "assert.h"
41 : #include "wmc_auto.h"
42 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 :
46 : /*-------------------------------------------------------------------*
47 : * Function allocate_CoreCoder_enc()
48 : *
49 : * Allocate CoreCoder modules
50 : *-------------------------------------------------------------------*/
51 :
52 36332 : static ivas_error allocate_CoreCoder_enc(
53 : ENC_CORE_HANDLE st /* i/o: Core encoder state structure */
54 : )
55 : {
56 36332 : if ( st->hLPDmem == NULL && st->element_mode != IVAS_CPE_MDCT )
57 : {
58 18549 : if ( ( st->hLPDmem = (LPD_state_HANDLE) malloc( sizeof( LPD_state ) ) ) == NULL )
59 : {
60 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LPDmem\n" ) );
61 : }
62 18549 : LPDmem_enc_init( st->hLPDmem );
63 : }
64 :
65 36332 : if ( st->hGSCEnc == NULL && st->element_mode != IVAS_CPE_MDCT )
66 : {
67 18549 : if ( ( st->hGSCEnc = (GSC_ENC_HANDLE) malloc( sizeof( GSC_ENC_DATA ) ) ) == NULL )
68 : {
69 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for GSC\n" ) );
70 : }
71 18549 : GSC_enc_init( st->hGSCEnc );
72 : }
73 :
74 36332 : if ( st->hNoiseEst == NULL )
75 : {
76 18163 : if ( ( st->hNoiseEst = (NOISE_EST_HANDLE) malloc( sizeof( NOISE_EST_DATA ) ) ) == NULL )
77 : {
78 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Noise estimation\n" ) );
79 : }
80 18163 : noise_est_init( st->hNoiseEst );
81 : }
82 :
83 36332 : if ( st->hVAD == NULL )
84 : {
85 18163 : if ( ( st->hVAD = (VAD_HANDLE) malloc( sizeof( VAD_DATA ) ) ) == NULL )
86 : {
87 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for VAD\n" ) );
88 : }
89 18163 : wb_vad_init( st->hVAD );
90 : }
91 :
92 36332 : if ( st->hSpMusClas == NULL )
93 : {
94 18163 : if ( ( st->hSpMusClas = (SP_MUS_CLAS_HANDLE) malloc( sizeof( SP_MUS_CLAS_DATA ) ) ) == NULL )
95 : {
96 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Speech/music classifier\n" ) );
97 : }
98 18163 : speech_music_clas_init( st->hSpMusClas );
99 : }
100 :
101 36332 : return IVAS_ERR_OK;
102 : }
103 :
104 :
105 : /*-------------------------------------------------------------------*
106 : * Function deallocate_CoreCoder_TCX_enc()
107 : *
108 : * Deallocate CoreCoder TCX modules
109 : *-------------------------------------------------------------------*/
110 :
111 18716 : static void deallocate_CoreCoder_TCX_enc(
112 : ENC_CORE_HANDLE st /* i/o: Core encoder state structure */
113 : )
114 : {
115 18716 : if ( st->hTcxEnc != NULL )
116 : {
117 17968 : free( st->hTcxEnc );
118 17968 : st->hTcxEnc = NULL;
119 : }
120 :
121 18716 : if ( st->hTcxCfg != NULL )
122 : {
123 17968 : free( st->hTcxCfg );
124 17968 : st->hTcxCfg = NULL;
125 : }
126 :
127 18716 : if ( st->hIGFEnc != NULL )
128 : {
129 13846 : free( st->hIGFEnc );
130 13846 : st->hIGFEnc = NULL;
131 : }
132 :
133 18716 : if ( st->hHQ_core != NULL )
134 : {
135 0 : free( st->hHQ_core );
136 0 : st->hHQ_core = NULL;
137 : }
138 :
139 18716 : return;
140 : }
141 :
142 :
143 : /*-------------------------------------------------------------------*
144 : * Function deallocate_CoreCoder_enc()
145 : *
146 : * Deallocate CoreCoder modules
147 : *-------------------------------------------------------------------*/
148 :
149 54119 : static void deallocate_CoreCoder_enc(
150 : ENC_CORE_HANDLE st /* i/o: Core encoder state structure */
151 : )
152 : {
153 54119 : if ( st->hLPDmem != NULL )
154 : {
155 18569 : free( st->hLPDmem );
156 18569 : st->hLPDmem = NULL;
157 : }
158 :
159 54119 : if ( st->hGSCEnc != NULL )
160 : {
161 18569 : free( st->hGSCEnc );
162 18569 : st->hGSCEnc = NULL;
163 : }
164 :
165 54119 : if ( st->hNoiseEst != NULL && st->element_mode != IVAS_CPE_MDCT )
166 : {
167 18135 : free( st->hNoiseEst );
168 18135 : st->hNoiseEst = NULL;
169 : }
170 :
171 54119 : if ( st->hVAD != NULL && st->element_mode != IVAS_CPE_MDCT )
172 : {
173 18135 : free( st->hVAD );
174 18135 : st->hVAD = NULL;
175 : }
176 :
177 54119 : if ( st->hSpMusClas != NULL && st->element_mode != IVAS_CPE_MDCT )
178 : {
179 18135 : free( st->hSpMusClas );
180 18135 : st->hSpMusClas = NULL;
181 : }
182 :
183 54119 : if ( st->cldfbAnaEnc != NULL )
184 : {
185 19027 : deleteCldfb( &st->cldfbAnaEnc );
186 : }
187 :
188 54119 : if ( st->hBWE_TD != NULL )
189 : {
190 18437 : free( st->hBWE_TD );
191 18437 : st->hBWE_TD = NULL;
192 : }
193 :
194 54119 : if ( st->cldfbSynTd != NULL )
195 : {
196 18437 : deleteCldfb( &st->cldfbSynTd );
197 : }
198 :
199 54119 : if ( st->hBWE_FD != NULL )
200 : {
201 18437 : free( st->hBWE_FD );
202 18437 : st->hBWE_FD = NULL;
203 : }
204 :
205 54119 : if ( st->element_mode != IVAS_CPE_MDCT )
206 : {
207 18135 : deallocate_CoreCoder_TCX_enc( st );
208 : }
209 :
210 54119 : return;
211 : }
212 :
213 :
214 : /*-------------------------------------------------------------------*
215 : * Function stereo_memory_enc()
216 : *
217 : * Dynamically allocate/deallocate data structures depending on the actual CPE mode
218 : *-------------------------------------------------------------------*/
219 :
220 6355819 : ivas_error stereo_memory_enc(
221 : CPE_ENC_HANDLE hCPE, /* i : CPE encoder structure */
222 : const int32_t input_Fs, /* i : input sampling rate */
223 : const int16_t max_bwidth, /* i : maximum audio bandwidth */
224 : float *tdm_last_ratio, /* o : TD stereo last ratio */
225 : const IVAS_FORMAT ivas_format, /* i : ivas format */
226 : const int16_t nchan_transport /* i : number transport chans */
227 : )
228 : {
229 : Encoder_State *st;
230 : ivas_error error;
231 :
232 6355819 : error = IVAS_ERR_OK;
233 :
234 6355819 : assert( hCPE->last_element_mode >= IVAS_CPE_DFT && "Switching from SCE to CPE is not a valid configuration!" );
235 :
236 : /*--------------------------------------------------------------*
237 : * save parameters from structures that will be freed
238 : *---------------------------------------------------------------*/
239 :
240 6355819 : if ( hCPE->last_element_mode == IVAS_CPE_TD )
241 : {
242 28628 : *tdm_last_ratio = hCPE->hStereoTD->tdm_last_ratio; /* note: this must be set to local variable before data structures are allocated/deallocated */
243 : }
244 :
245 6355819 : if ( hCPE->hStereoTCA != NULL && hCPE->last_element_mode == IVAS_CPE_DFT )
246 : {
247 801512 : set_s( hCPE->hStereoTCA->prevCorrLagStats, (int16_t) hCPE->hStereoDft->hItd->itd[1], 3 );
248 801512 : hCPE->hStereoTCA->prevRefChanIndx = ( hCPE->hStereoDft->hItd->itd[1] >= 0 ) ? ( L_CH_INDX ) : ( R_CH_INDX );
249 : }
250 :
251 : /*--------------------------------------------------------------*
252 : * allocate/deallocate data structures
253 : *---------------------------------------------------------------*/
254 :
255 6355819 : if ( hCPE->element_mode != hCPE->last_element_mode )
256 : {
257 : /*--------------------------------------------------------------*
258 : * switching CPE mode to DFT stereo
259 : *---------------------------------------------------------------*/
260 :
261 36708 : if ( hCPE->element_mode == IVAS_CPE_DFT )
262 : {
263 : /* deallocate data structure of the previous CPE mode */
264 18135 : if ( hCPE->hStereoTD != NULL )
265 : {
266 368 : free( hCPE->hStereoTD );
267 368 : hCPE->hStereoTD = NULL;
268 : }
269 :
270 18135 : if ( hCPE->hStereoMdct != NULL )
271 : {
272 17767 : stereo_mdct_enc_destroy( &( hCPE->hStereoMdct ) );
273 17767 : hCPE->hStereoMdct = NULL;
274 : }
275 :
276 : /* deallocate CoreCoder secondary channel */
277 18135 : deallocate_CoreCoder_enc( hCPE->hCoreCoder[1] );
278 :
279 : /* allocate DFT stereo data structure */
280 18135 : if ( ( error = stereo_dft_enc_create( &( hCPE->hStereoDft ), input_Fs, max_bwidth ) ) != IVAS_ERR_OK )
281 : {
282 0 : return error;
283 : }
284 :
285 : /* allocate ICBWE structure */
286 18135 : if ( hCPE->hStereoICBWE == NULL )
287 : {
288 18003 : if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_ENC_HANDLE) malloc( sizeof( STEREO_ICBWE_ENC_DATA ) ) ) == NULL )
289 : {
290 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
291 : }
292 :
293 18003 : stereo_icBWE_init_enc( hCPE->hStereoICBWE );
294 : }
295 :
296 : /* allocate HQ core in M channel */
297 18135 : st = hCPE->hCoreCoder[0];
298 18135 : if ( st->hHQ_core == NULL )
299 : {
300 8 : if ( ( st->hHQ_core = (HQ_ENC_HANDLE) malloc( sizeof( HQ_ENC_DATA ) ) ) == NULL )
301 : {
302 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
303 : }
304 :
305 8 : HQ_core_enc_init( st->hHQ_core );
306 : }
307 : }
308 :
309 : /*--------------------------------------------------------------*
310 : * switching CPE mode to TD stereo
311 : *---------------------------------------------------------------*/
312 :
313 36708 : if ( hCPE->element_mode == IVAS_CPE_TD )
314 : {
315 : /* deallocate data structure of the previous CPE mode */
316 581 : if ( hCPE->hStereoDft != NULL )
317 : {
318 380 : stereo_dft_enc_destroy( &( hCPE->hStereoDft ) );
319 380 : hCPE->hStereoDft = NULL;
320 : }
321 :
322 581 : if ( hCPE->hStereoMdct != NULL )
323 : {
324 201 : stereo_mdct_enc_destroy( &( hCPE->hStereoMdct ) );
325 201 : hCPE->hStereoMdct = NULL;
326 : }
327 :
328 : /* deallocated TCX/IGF structures for second channel */
329 581 : deallocate_CoreCoder_TCX_enc( hCPE->hCoreCoder[1] );
330 :
331 : /* allocate TD stereo data structure */
332 581 : if ( hCPE->hStereoTD != NULL )
333 : {
334 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
335 : }
336 :
337 581 : if ( ( hCPE->hStereoTD = (STEREO_TD_ENC_DATA_HANDLE) malloc( sizeof( STEREO_TD_ENC_DATA ) ) ) == NULL )
338 : {
339 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
340 : }
341 :
342 581 : stereo_td_init_enc( hCPE->hStereoTD, hCPE->last_element_mode );
343 :
344 : /* allocate secondary channel */
345 581 : if ( ( error = allocate_CoreCoder_enc( hCPE->hCoreCoder[1] ) ) != IVAS_ERR_OK )
346 : {
347 0 : return error;
348 : }
349 : }
350 :
351 : /*--------------------------------------------------------------*
352 : * allocate DFT/TD stereo structures after MDCT stereo frame
353 : *---------------------------------------------------------------*/
354 :
355 36708 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT && ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD ) )
356 : {
357 : /* Deallocate MDCT CNG structures */
358 17968 : deleteCldfb( &hCPE->hCoreCoder[0]->cldfbAnaEnc );
359 17968 : deleteCldfb( &hCPE->hCoreCoder[1]->cldfbAnaEnc );
360 :
361 17968 : if ( hCPE->element_mode == IVAS_CPE_DFT )
362 : {
363 17767 : if ( hCPE->hCoreCoder[1]->hDtxEnc != NULL )
364 : {
365 590 : free( hCPE->hCoreCoder[1]->hDtxEnc );
366 590 : hCPE->hCoreCoder[1]->hDtxEnc = NULL;
367 : }
368 :
369 17767 : if ( hCPE->hCoreCoder[1]->hFdCngEnc != NULL )
370 : {
371 590 : deleteFdCngEnc( &hCPE->hCoreCoder[1]->hFdCngEnc );
372 : }
373 : }
374 :
375 17968 : if ( hCPE->hCoreCoder[0]->Opt_DTX_ON && hCPE->hCoreCoder[0]->hTdCngEnc == NULL )
376 : {
377 598 : if ( ( hCPE->hCoreCoder[0]->hTdCngEnc = (TD_CNG_ENC_HANDLE) malloc( sizeof( TD_CNG_ENC_DATA ) ) ) == NULL )
378 : {
379 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
380 : }
381 :
382 598 : td_cng_enc_init( hCPE->hCoreCoder[0]->hTdCngEnc, hCPE->hCoreCoder[0]->Opt_DTX_ON, hCPE->hCoreCoder[0]->max_bwidth );
383 : }
384 :
385 : /* allocate TCA data structure */
386 17968 : if ( hCPE->hStereoTCA != NULL )
387 : {
388 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TCA Stereo memory already allocated\n" );
389 : }
390 :
391 17968 : if ( ( hCPE->hStereoTCA = (STEREO_TCA_ENC_HANDLE) malloc( sizeof( STEREO_TCA_ENC_DATA ) ) ) == NULL )
392 : {
393 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
394 : }
395 :
396 17968 : stereo_tca_init_enc( hCPE->hStereoTCA, input_Fs );
397 :
398 17968 : st = hCPE->hCoreCoder[0];
399 :
400 : /* allocate primary channel substructures */
401 17968 : if ( ( error = allocate_CoreCoder_enc( st ) ) != IVAS_ERR_OK )
402 : {
403 0 : return error;
404 : }
405 :
406 : /* allocate CLDFB for primary channel */
407 17968 : if ( st->cldfbAnaEnc == NULL )
408 : {
409 17968 : if ( ( error = openCldfb( &st->cldfbAnaEnc, CLDFB_ANALYSIS, input_Fs, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
410 : {
411 0 : return error;
412 : }
413 : }
414 :
415 : /* allocate BWEs for primary channel */
416 17968 : if ( st->hBWE_TD == NULL )
417 : {
418 17968 : if ( ( st->hBWE_TD = (TD_BWE_ENC_HANDLE) malloc( sizeof( TD_BWE_ENC_DATA ) ) ) == NULL )
419 : {
420 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
421 : }
422 :
423 17968 : if ( st->cldfbSynTd == NULL )
424 : {
425 17968 : if ( ( error = openCldfb( &st->cldfbSynTd, CLDFB_SYNTHESIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
426 : {
427 0 : return error;
428 : }
429 : }
430 :
431 17968 : InitSWBencBuffer( st->hBWE_TD );
432 17968 : ResetSHBbuffer_Enc( st->hBWE_TD );
433 :
434 17968 : if ( ( st->hBWE_FD = (FD_BWE_ENC_HANDLE) malloc( sizeof( FD_BWE_ENC_DATA ) ) ) == NULL )
435 : {
436 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
437 : }
438 :
439 17968 : fd_bwe_enc_init( st->hBWE_FD );
440 : }
441 :
442 : /* allocate stereo CNG structure */
443 17968 : if ( hCPE->hStereoCng == NULL )
444 : {
445 83 : if ( ( hCPE->hStereoCng = (STEREO_CNG_ENC_HANDLE) malloc( sizeof( STEREO_CNG_ENC ) ) ) == NULL )
446 : {
447 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo Cng for Unified/TD\n" ) );
448 : }
449 :
450 83 : stereo_enc_cng_init( hCPE->hStereoCng );
451 : }
452 : }
453 :
454 : /*--------------------------------------------------------------*
455 : * switching CPE mode to MDCT stereo
456 : *---------------------------------------------------------------*/
457 :
458 36708 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
459 : {
460 : int16_t i;
461 :
462 : /* deallocate data structure of the previous CPE mode */
463 17992 : if ( hCPE->hStereoDft != NULL )
464 : {
465 17783 : stereo_dft_enc_destroy( &( hCPE->hStereoDft ) );
466 17783 : hCPE->hStereoDft = NULL;
467 : }
468 :
469 17992 : if ( hCPE->hStereoTD != NULL )
470 : {
471 209 : free( hCPE->hStereoTD );
472 209 : hCPE->hStereoTD = NULL;
473 : }
474 :
475 17992 : if ( hCPE->hStereoTCA != NULL )
476 : {
477 17992 : free( hCPE->hStereoTCA );
478 17992 : hCPE->hStereoTCA = NULL;
479 : }
480 :
481 17992 : if ( hCPE->hStereoICBWE != NULL )
482 : {
483 17783 : free( hCPE->hStereoICBWE );
484 17783 : hCPE->hStereoICBWE = NULL;
485 : }
486 :
487 53976 : for ( i = 0; i < CPE_CHANNELS; i++ )
488 : {
489 : /* deallocate core-coder substructures */
490 35984 : deallocate_CoreCoder_enc( hCPE->hCoreCoder[i] );
491 : }
492 :
493 17992 : if ( hCPE->last_element_mode == IVAS_CPE_DFT )
494 : {
495 : /* allocate secondary channel */
496 17783 : if ( ( error = allocate_CoreCoder_enc( hCPE->hCoreCoder[1] ) ) != IVAS_ERR_OK )
497 : {
498 0 : return error;
499 : }
500 : }
501 :
502 : /* allocate TCX/IGF structures for second channel */
503 17992 : st = hCPE->hCoreCoder[1];
504 :
505 17992 : if ( ( st->hTcxEnc = (TCX_ENC_HANDLE) malloc( sizeof( TCX_ENC_DATA ) ) ) == NULL )
506 : {
507 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hTcxEnc\n" ) );
508 : }
509 17992 : st->hTcxEnc->spectrum[0] = st->hTcxEnc->spectrum_long;
510 17992 : st->hTcxEnc->spectrum[1] = st->hTcxEnc->spectrum_long + N_TCX10_MAX;
511 17992 : set_f( st->hTcxEnc->old_out, 0, L_FRAME32k );
512 17992 : set_f( st->hTcxEnc->spectrum_long, 0, N_MAX );
513 17992 : st->hTcxEnc->tfm_mem = 0.75f;
514 :
515 17992 : if ( hCPE->last_element_mode == IVAS_CPE_DFT )
516 : {
517 17783 : st->last_core = ACELP_CORE; /* needed to set-up TCX core in SetTCXModeInfo() */
518 : }
519 :
520 17992 : if ( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
521 : {
522 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hTcxCfg\n" ) );
523 : }
524 :
525 17992 : if ( ( st->hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
526 : {
527 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
528 : }
529 17992 : st->igf = getIgfPresent( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
530 :
531 : /* allocate and initialize MDCT stereo structure */
532 17992 : if ( ( hCPE->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
533 : {
534 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
535 : }
536 :
537 : #ifdef DEBUGGING
538 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
539 : #endif
540 17992 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, hCPE->hCoreCoder[0]->max_bwidth, 0, NULL, 1 );
541 :
542 17992 : hCPE->hStereoMdct->isSBAStereoMode = ( ivas_format == SBA_FORMAT && nchan_transport == 2 );
543 :
544 17992 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
545 : {
546 248 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
547 : {
548 0 : return error;
549 : }
550 : }
551 :
552 : /* allocate/deallocate and initialize DTX/CNG structures */
553 17992 : if ( hCPE->hCoreCoder[0]->Opt_DTX_ON )
554 : {
555 1806 : for ( i = 0; i < CPE_CHANNELS; i++ )
556 : {
557 1204 : st = hCPE->hCoreCoder[i];
558 1204 : if ( ( error = openCldfb( &st->cldfbAnaEnc, CLDFB_ANALYSIS, st->input_Fs, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
559 : {
560 0 : return error;
561 : }
562 :
563 1204 : st->currEnergyLookAhead = 6.1e-5f;
564 :
565 1204 : if ( st->hDtxEnc == NULL )
566 : {
567 594 : if ( ( st->hDtxEnc = (DTX_ENC_HANDLE) malloc( sizeof( DTX_ENC_DATA ) ) ) == NULL )
568 : {
569 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX variables\n" ) );
570 : }
571 : }
572 1204 : dtx_enc_init( st, 0, FIXED_SID_RATE );
573 :
574 1204 : if ( st->hTdCngEnc != NULL )
575 : {
576 602 : free( st->hTdCngEnc );
577 602 : st->hTdCngEnc = NULL;
578 : }
579 :
580 1204 : if ( st->hFdCngEnc == NULL )
581 : {
582 594 : if ( ( error = createFdCngEnc( &st->hFdCngEnc ) ) != IVAS_ERR_OK )
583 : {
584 0 : return error;
585 : }
586 :
587 594 : initFdCngEnc( st->hFdCngEnc, st->input_Fs, st->cldfbAnaEnc->scale );
588 594 : configureFdCngEnc( st->hFdCngEnc, st->bwidth, st->rf_mode && st->total_brate == ACELP_13k20 ? ACELP_9k60 : st->total_brate );
589 : }
590 : }
591 : }
592 : }
593 : }
594 :
595 6355819 : if ( ivas_format == STEREO_FORMAT && hCPE->element_mode == IVAS_CPE_MDCT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE && hCPE->last_element_brate > MAX_MDCT_ITD_BRATE )
596 : {
597 : /* allocate MDCT stereo ITD handling structure */
598 343 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
599 : {
600 0 : return error;
601 : }
602 : }
603 :
604 6355819 : return error;
605 : }
606 :
607 :
608 : /*-------------------------------------------------------------------*
609 : * Function stereo_switching_enc()
610 : *
611 : * Handling of memories in case of CPE modes switching
612 : *-------------------------------------------------------------------*/
613 :
614 6355819 : void stereo_switching_enc(
615 : CPE_ENC_HANDLE hCPE, /* i : CPE encoder structure */
616 : float old_input_signal_pri[], /* i : old input signal of primary channel */
617 : const int16_t input_frame /* i : input frame length */
618 : )
619 : {
620 : int16_t i, n, dft_ovl, offset;
621 : float tmpF;
622 : Encoder_State **sts;
623 :
624 6355819 : sts = hCPE->hCoreCoder;
625 6355819 : dft_ovl = STEREO_DFT_OVL_MAX * input_frame / L_FRAME48k;
626 :
627 : /* update DFT analysis overlap memory */
628 : /* note: in MDCT stereo, the update is done in stereo_td_itd_mdct_stereo() */
629 6355819 : if ( hCPE->element_mode > IVAS_CPE_DFT && hCPE->input_mem[0] != NULL && hCPE->element_mode != IVAS_CPE_MDCT )
630 : {
631 85896 : for ( n = 0; n < CPE_CHANNELS; n++ )
632 : {
633 57264 : mvr2r( sts[n]->input + input_frame - dft_ovl, hCPE->input_mem[n], dft_ovl );
634 : }
635 : }
636 :
637 : /* save original stereo input (MDCT overlap part) for both channels in unused old input of right channel for possible DFT->MDCT transition */
638 6355819 : if ( hCPE->element_mode == IVAS_CPE_DFT )
639 : {
640 801484 : mvr2r( sts[0]->input + sts[0]->input_Fs / FRAMES_PER_SEC - sts[0]->encoderLookahead_FB, sts[1]->input - 2 * sts[0]->encoderLookahead_FB, sts[0]->encoderLookahead_FB );
641 801484 : mvr2r( sts[1]->input + sts[1]->input_Fs / FRAMES_PER_SEC - sts[1]->encoderLookahead_FB, sts[1]->input - sts[1]->encoderLookahead_FB, sts[1]->encoderLookahead_FB );
642 : }
643 :
644 : /* TD/MDCT -> DFT stereo switching */
645 6355819 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->last_element_mode != IVAS_CPE_DFT )
646 : {
647 : /* window DFT synthesis overlap memory @input_Fs, primary channel */
648 7300235 : for ( i = 0; i < dft_ovl; i++ )
649 : {
650 7282100 : hCPE->hStereoDft->output_mem_dmx[i] = old_input_signal_pri[input_frame - dft_ovl + i] * hCPE->hStereoDft->win[dft_ovl - 1 - i];
651 : }
652 :
653 : /* reset 48kHz BWE overlap memory */
654 18135 : set_f( hCPE->hStereoDft->output_mem_dmx_32k, 0, STEREO_DFT_OVL_32k );
655 :
656 18135 : stereo_dft_enc_reset( hCPE->hStereoDft );
657 :
658 : /* update ITD parameters */
659 18135 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_TD )
660 : {
661 368 : set_f( hCPE->hStereoDft->hItd->itd, hCPE->hStereoTCA->prevCorrLagStats[2], STEREO_DFT_ENC_DFT_NB );
662 : }
663 :
664 : /* Update the side_gain[] parameters */
665 18135 : if ( hCPE->hStereoTCA != NULL && hCPE->last_element_mode != IVAS_CPE_MDCT )
666 : {
667 368 : tmpF = usdequant( hCPE->hStereoTCA->indx_ica_gD, STEREO_TCA_GDMIN, STEREO_TCA_GDSTEP );
668 5152 : for ( i = 0; i < STEREO_DFT_BAND_MAX; i++ )
669 : {
670 4784 : hCPE->hStereoDft->side_gain[STEREO_DFT_BAND_MAX + i] = tmpF;
671 : }
672 : }
673 :
674 : /* do not allow differential coding of DFT side parameters */
675 18135 : hCPE->hStereoDft->res_pred_counter = STEREO_DFT_FEC_THRESHOLD;
676 :
677 : /* update DFT synthesis overlap memory @12.8kHz */
678 2049255 : for ( i = 0; i < STEREO_DFT_OVL_12k8; i++ )
679 : {
680 2031120 : hCPE->hStereoDft->output_mem_dmx_12k8[i] = sts[0]->buf_speech_enc[L_FRAME32k + L_FRAME - STEREO_DFT_OVL_12k8 + i] * hCPE->hStereoDft->win_12k8[STEREO_DFT_OVL_12k8 - 1 - i];
681 : }
682 :
683 : /* update DFT synthesis overlap memory @16kHz, primary channel only */
684 18135 : lerp( hCPE->hStereoDft->output_mem_dmx, hCPE->hStereoDft->output_mem_dmx_16k, STEREO_DFT_OVL_16k, dft_ovl );
685 :
686 : /* reset DFT synthesis overlap memory @8kHz, secondary channel */
687 18135 : set_f( hCPE->hStereoDft->output_mem_res_8k, 0, STEREO_DFT_OVL_8k );
688 :
689 18135 : hCPE->hCoreCoder[1]->vad_flag = 0;
690 : }
691 :
692 : /* MDCT -> TD stereo switching */
693 6355819 : if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_MDCT )
694 : {
695 201 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
696 201 : hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_LEFT_IS_PRIM;
697 201 : hCPE->hStereoTD->tdm_last_SM_flag = 0;
698 201 : hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
699 201 : hCPE->hStereoTD->tdm_last_ratio = tdm_ratio_tabl[LRTD_STEREO_LEFT_IS_PRIM];
700 : }
701 : /* DFT -> TD stereo switching */
702 6355618 : else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_DFT )
703 : {
704 380 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
705 380 : hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_MID_IS_PRIM;
706 380 : hCPE->hStereoTD->tdm_last_SM_flag = 0;
707 380 : hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
708 :
709 : /* First frame after DFT frame AND the content is uncorrelated or xtalk -> the primary channel is forced to left */
710 380 : if ( hCPE->hStereoClassif->lrtd_mode == 1 )
711 : {
712 380 : set_zero( sts[1]->input - input_frame, input_frame );
713 :
714 380 : hCPE->hStereoTD->tdm_last_ratio = tdm_ratio_tabl[LRTD_STEREO_LEFT_IS_PRIM];
715 380 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
716 :
717 380 : if ( hCPE->hStereoTCA->instTargetGain < 0.05f && ( hCPE->hCoreCoder[0]->vad_flag || hCPE->hCoreCoder[1]->vad_flag ) ) /* but if there is no content in the L channel -> the primary channel is forced to right */
718 : {
719 104 : hCPE->hStereoTD->tdm_last_ratio = tdm_ratio_tabl[LRTD_STEREO_RIGHT_IS_PRIM];
720 104 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM;
721 : }
722 : }
723 : }
724 :
725 : /* no secondary channel in the previous frame -> memory resets */
726 6355819 : if ( hCPE->element_mode > IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_DFT )
727 : {
728 18163 : if ( sts[0]->cldfbAnaEnc != NULL )
729 : {
730 958 : offset = sts[0]->cldfbAnaEnc->p_filter_length - sts[0]->cldfbAnaEnc->no_channels;
731 958 : mvr2r( old_input_signal_pri + input_frame - offset - NS2SA( input_frame * FRAMES_PER_SEC, L_MEM_RECALC_TBE_NS ), sts[0]->cldfbAnaEnc->cldfb_state, offset );
732 : }
733 :
734 18163 : if ( sts[0]->cldfbSynTd != NULL )
735 : {
736 380 : cldfb_reset_memory( sts[0]->cldfbSynTd );
737 380 : sts[0]->currEnergyLookAhead = 6.1e-5f;
738 : }
739 :
740 18163 : if ( hCPE->hStereoICBWE == NULL && sts[1]->cldfbAnaEnc != NULL )
741 : {
742 958 : offset = sts[1]->cldfbAnaEnc->p_filter_length - sts[1]->cldfbAnaEnc->no_channels;
743 :
744 958 : if ( hCPE->hStereoTD != NULL && hCPE->hStereoTD->tdm_last_ratio_idx == LRTD_STEREO_LEFT_IS_PRIM )
745 : {
746 276 : v_multc( hCPE->hCoreCoder[1]->old_input_signal + input_frame - offset - NS2SA( input_frame * FRAMES_PER_SEC, L_MEM_RECALC_TBE_NS ), -1.0f, sts[1]->cldfbAnaEnc->cldfb_state, offset );
747 : }
748 : else
749 : {
750 682 : mvr2r( hCPE->hCoreCoder[1]->old_input_signal + input_frame - offset - NS2SA( input_frame * FRAMES_PER_SEC, L_MEM_RECALC_TBE_NS ), sts[1]->cldfbAnaEnc->cldfb_state, offset );
751 : }
752 :
753 958 : if ( sts[1]->cldfbSynTd != NULL )
754 : {
755 380 : cldfb_reset_memory( sts[1]->cldfbSynTd );
756 380 : sts[1]->currEnergyLookAhead = 6.1e-5f;
757 : }
758 : }
759 :
760 18163 : sts[1]->last_extl = -1;
761 :
762 : /* no secondary channel in the previous frame -> memory resets */
763 18163 : set_zero( sts[1]->old_inp_12k8, L_INP_MEM );
764 18163 : set_zero( sts[1]->mem_decim, 2 * L_FILT_MAX );
765 18163 : sts[1]->mem_preemph = 0;
766 :
767 18163 : set_zero( sts[1]->buf_speech_enc, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
768 18163 : set_zero( sts[1]->buf_speech_enc_pe, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
769 :
770 18163 : if ( sts[1]->hTcxEnc != NULL )
771 : {
772 17783 : set_zero( sts[1]->hTcxEnc->buf_speech_ltp, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
773 : }
774 18163 : set_zero( sts[1]->buf_wspeech_enc, L_FRAME16k + L_SUBFR + L_FRAME16k + L_NEXT_MAX_16k );
775 18163 : set_zero( sts[1]->buf_synth, OLD_SYNTH_SIZE_ENC + L_FRAME32k );
776 18163 : sts[1]->mem_wsp = 0.0f;
777 18163 : sts[1]->mem_wsp_enc = 0.0f;
778 18163 : init_gp_clip( sts[1]->clip_var );
779 :
780 18163 : set_f( sts[1]->Bin_E, 0, L_FFT );
781 18163 : set_f( sts[1]->Bin_E_old, 0, L_FFT / 2 );
782 :
783 : /* sts[1]->hLPDmem reset already done in allocation of handles */
784 :
785 18163 : sts[1]->last_L_frame = sts[0]->last_L_frame;
786 :
787 18163 : pitch_ol_init( &sts[1]->old_thres, &sts[1]->old_pitch, &sts[1]->delta_pit, &sts[1]->old_corr );
788 18163 : set_zero( sts[1]->old_wsp, L_WSP_MEM );
789 18163 : set_zero( sts[1]->old_wsp2, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
790 18163 : set_zero( sts[1]->mem_decim2, 3 );
791 18163 : mvs2s( sts[0]->pitch, sts[1]->pitch, 3 );
792 :
793 18163 : sts[1]->Nb_ACELP_frames = 0;
794 :
795 : /* populate PCh memories into the SCh */
796 18163 : if ( sts[0]->hLPDmem != NULL )
797 : {
798 380 : mvr2r( sts[0]->hLPDmem->old_exc, sts[1]->hLPDmem->old_exc, L_EXC_MEM );
799 : }
800 18163 : mvr2r( sts[0]->lsf_old, sts[1]->lsf_old, M );
801 18163 : mvr2r( sts[0]->lsp_old, sts[1]->lsp_old, M );
802 18163 : mvr2r( sts[0]->lsf_old1, sts[1]->lsf_old1, M );
803 18163 : mvr2r( sts[0]->lsp_old1, sts[1]->lsp_old1, M );
804 :
805 18163 : sts[1]->last_core_brate = sts[0]->last_core_brate;
806 :
807 18163 : sts[1]->GSC_noisy_speech = 0;
808 18163 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
809 : {
810 : /* cross-fade overlap region of DFT Stereo downmix and original stereo channels */
811 17783 : tmpF = 1.f / sts[0]->encoderLookahead_FB;
812 7173743 : for ( i = 0; i < sts[0]->encoderLookahead_FB; i++ )
813 : {
814 7155960 : sts[1]->input[-sts[0]->encoderLookahead_FB + i] = ( ( sts[0]->encoderLookahead_FB - i ) * sts[0]->input[-sts[0]->encoderLookahead_FB + i] + i * sts[1]->input[-sts[0]->encoderLookahead_FB + i] ) * tmpF;
815 7155960 : sts[0]->input[-sts[0]->encoderLookahead_FB + i] = ( ( sts[0]->encoderLookahead_FB - i ) * sts[0]->input[-sts[0]->encoderLookahead_FB + i] + i * sts[1]->input[-2 * sts[0]->encoderLookahead_FB + i] ) * tmpF;
816 : }
817 : /* restore continuous signal in right channel (part of old_output was used to store original left channel) */
818 17783 : mvr2r( sts[0]->input - sts[0]->hTcxEnc->L_frameTCX, sts[1]->input - sts[0]->hTcxEnc->L_frameTCX, sts[0]->hTcxEnc->L_frameTCX - sts[0]->encoderLookahead_FB );
819 :
820 17783 : sts[1]->last_core = sts[0]->last_core;
821 17783 : sts[1]->last_coder_type = sts[0]->last_coder_type;
822 17783 : sts[1]->last_bwidth = sts[0]->last_bwidth;
823 : }
824 : }
825 6337656 : else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_MDCT )
826 : {
827 201 : set_f( sts[0]->hLPDmem->old_exc, 0.0f, L_EXC_MEM );
828 201 : set_f( sts[1]->hLPDmem->old_exc, 0.0f, L_EXC_MEM );
829 : }
830 :
831 : /* TD/DFT -> MDCT stereo switching (there is no TCX in the TD stereo secondary channel, or DFT stereo) */
832 6355819 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode != IVAS_CPE_MDCT )
833 : {
834 17992 : sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo;
835 17992 : sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode;
836 : }
837 :
838 6355819 : return;
839 : }
|