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 1537 : static ivas_error allocate_CoreCoder_enc(
53 : ENC_CORE_HANDLE st /* i/o: Core encoder state structure */
54 : )
55 : {
56 1537 : if ( st->hLPDmem == NULL && st->element_mode != IVAS_CPE_MDCT )
57 : {
58 791 : 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 791 : LPDmem_enc_init( st->hLPDmem );
63 : }
64 :
65 1537 : if ( st->hGSCEnc == NULL && st->element_mode != IVAS_CPE_MDCT )
66 : {
67 791 : 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 791 : GSC_enc_init( st->hGSCEnc );
72 : }
73 :
74 1537 : if ( st->hNoiseEst == NULL )
75 : {
76 797 : 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 797 : noise_est_init( st->hNoiseEst );
81 : }
82 :
83 1537 : if ( st->hVAD == NULL )
84 : {
85 797 : 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 797 : wb_vad_init( st->hVAD );
90 : }
91 :
92 1537 : if ( st->hSpMusClas == NULL )
93 : {
94 797 : 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 797 : speech_music_clas_init( st->hSpMusClas );
99 : }
100 :
101 1537 : return IVAS_ERR_OK;
102 : }
103 :
104 :
105 : /*-------------------------------------------------------------------*
106 : * Function deallocate_CoreCoder_TCX_enc()
107 : *
108 : * Deallocate CoreCoder TCX modules
109 : *-------------------------------------------------------------------*/
110 :
111 838 : static void deallocate_CoreCoder_TCX_enc(
112 : ENC_CORE_HANDLE st /* i/o: Core encoder state structure */
113 : )
114 : {
115 838 : if ( st->hTcxEnc != NULL )
116 : {
117 732 : free( st->hTcxEnc );
118 732 : st->hTcxEnc = NULL;
119 : }
120 :
121 838 : if ( st->hTcxCfg != NULL )
122 : {
123 732 : free( st->hTcxCfg );
124 732 : st->hTcxCfg = NULL;
125 : }
126 :
127 838 : if ( st->hIGFEnc != NULL )
128 : {
129 533 : free( st->hIGFEnc );
130 533 : st->hIGFEnc = NULL;
131 : }
132 :
133 838 : if ( st->hHQ_core != NULL )
134 : {
135 0 : free( st->hHQ_core );
136 0 : st->hHQ_core = NULL;
137 : }
138 :
139 838 : return;
140 : }
141 :
142 :
143 : /*-------------------------------------------------------------------*
144 : * Function deallocate_CoreCoder_enc()
145 : *
146 : * Deallocate CoreCoder modules
147 : *-------------------------------------------------------------------*/
148 :
149 2277 : static void deallocate_CoreCoder_enc(
150 : ENC_CORE_HANDLE st /* i/o: Core encoder state structure */
151 : )
152 : {
153 2277 : if ( st->hLPDmem != NULL )
154 : {
155 807 : free( st->hLPDmem );
156 807 : st->hLPDmem = NULL;
157 : }
158 :
159 2277 : if ( st->hGSCEnc != NULL )
160 : {
161 807 : free( st->hGSCEnc );
162 807 : st->hGSCEnc = NULL;
163 : }
164 :
165 2277 : if ( st->hNoiseEst != NULL && st->element_mode != IVAS_CPE_MDCT )
166 : {
167 779 : free( st->hNoiseEst );
168 779 : st->hNoiseEst = NULL;
169 : }
170 :
171 2277 : if ( st->hVAD != NULL && st->element_mode != IVAS_CPE_MDCT )
172 : {
173 779 : free( st->hVAD );
174 779 : st->hVAD = NULL;
175 : }
176 :
177 2277 : if ( st->hSpMusClas != NULL && st->element_mode != IVAS_CPE_MDCT )
178 : {
179 779 : free( st->hSpMusClas );
180 779 : st->hSpMusClas = NULL;
181 : }
182 :
183 2277 : if ( st->cldfbAnaEnc != NULL )
184 : {
185 833 : deleteCldfb( &st->cldfbAnaEnc );
186 : }
187 :
188 2277 : if ( st->hBWE_TD != NULL )
189 : {
190 775 : free( st->hBWE_TD );
191 775 : st->hBWE_TD = NULL;
192 : }
193 :
194 2277 : if ( st->cldfbSynTd != NULL )
195 : {
196 775 : deleteCldfb( &st->cldfbSynTd );
197 : }
198 :
199 2277 : if ( st->hBWE_FD != NULL )
200 : {
201 775 : free( st->hBWE_FD );
202 775 : st->hBWE_FD = NULL;
203 : }
204 :
205 2277 : if ( st->element_mode != IVAS_CPE_MDCT )
206 : {
207 779 : deallocate_CoreCoder_TCX_enc( st );
208 : }
209 :
210 2277 : 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 420855 : 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 420855 : error = IVAS_ERR_OK;
233 :
234 420855 : 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 420855 : if ( hCPE->last_element_mode == IVAS_CPE_TD )
241 : {
242 3790 : *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 420855 : if ( hCPE->hStereoTCA != NULL && hCPE->last_element_mode == IVAS_CPE_DFT )
246 : {
247 59697 : set_s( hCPE->hStereoTCA->prevCorrLagStats, (int16_t) hCPE->hStereoDft->hItd->itd[1], 3 );
248 59697 : 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 420855 : if ( hCPE->element_mode != hCPE->last_element_mode )
256 : {
257 : /*--------------------------------------------------------------*
258 : * switching CPE mode to DFT stereo
259 : *---------------------------------------------------------------*/
260 :
261 1587 : if ( hCPE->element_mode == IVAS_CPE_DFT )
262 : {
263 : /* deallocate data structure of the previous CPE mode */
264 779 : if ( hCPE->hStereoTD != NULL )
265 : {
266 55 : free( hCPE->hStereoTD );
267 55 : hCPE->hStereoTD = NULL;
268 : }
269 :
270 779 : if ( hCPE->hStereoMdct != NULL )
271 : {
272 724 : stereo_mdct_enc_destroy( &( hCPE->hStereoMdct ) );
273 724 : hCPE->hStereoMdct = NULL;
274 : }
275 :
276 : /* deallocate CoreCoder secondary channel */
277 779 : deallocate_CoreCoder_enc( hCPE->hCoreCoder[1] );
278 :
279 : /* allocate DFT stereo data structure */
280 779 : 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 779 : if ( hCPE->hStereoICBWE == NULL )
287 : {
288 747 : 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 747 : stereo_icBWE_init_enc( hCPE->hStereoICBWE );
294 : }
295 :
296 : /* allocate HQ core in M channel */
297 779 : st = hCPE->hCoreCoder[0];
298 779 : if ( st->hHQ_core == NULL )
299 : {
300 3 : 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 3 : HQ_core_enc_init( st->hHQ_core );
306 : }
307 : }
308 :
309 : /*--------------------------------------------------------------*
310 : * switching CPE mode to TD stereo
311 : *---------------------------------------------------------------*/
312 :
313 1587 : if ( hCPE->element_mode == IVAS_CPE_TD )
314 : {
315 : /* deallocate data structure of the previous CPE mode */
316 59 : if ( hCPE->hStereoDft != NULL )
317 : {
318 51 : stereo_dft_enc_destroy( &( hCPE->hStereoDft ) );
319 51 : hCPE->hStereoDft = NULL;
320 : }
321 :
322 59 : if ( hCPE->hStereoMdct != NULL )
323 : {
324 8 : stereo_mdct_enc_destroy( &( hCPE->hStereoMdct ) );
325 8 : hCPE->hStereoMdct = NULL;
326 : }
327 :
328 : /* deallocated TCX/IGF structures for second channel */
329 59 : deallocate_CoreCoder_TCX_enc( hCPE->hCoreCoder[1] );
330 :
331 : /* allocate TD stereo data structure */
332 59 : if ( hCPE->hStereoTD != NULL )
333 : {
334 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
335 : }
336 :
337 59 : 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 59 : stereo_td_init_enc( hCPE->hStereoTD, hCPE->last_element_mode );
343 :
344 : /* allocate secondary channel */
345 59 : 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 1587 : 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 732 : deleteCldfb( &hCPE->hCoreCoder[0]->cldfbAnaEnc );
359 732 : deleteCldfb( &hCPE->hCoreCoder[1]->cldfbAnaEnc );
360 :
361 732 : if ( hCPE->element_mode == IVAS_CPE_DFT )
362 : {
363 724 : if ( hCPE->hCoreCoder[1]->hDtxEnc != NULL )
364 : {
365 58 : free( hCPE->hCoreCoder[1]->hDtxEnc );
366 58 : hCPE->hCoreCoder[1]->hDtxEnc = NULL;
367 : }
368 :
369 724 : if ( hCPE->hCoreCoder[1]->hFdCngEnc != NULL )
370 : {
371 58 : deleteFdCngEnc( &hCPE->hCoreCoder[1]->hFdCngEnc );
372 : }
373 : }
374 :
375 732 : if ( hCPE->hCoreCoder[0]->Opt_DTX_ON && hCPE->hCoreCoder[0]->hTdCngEnc == NULL )
376 : {
377 60 : 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 60 : 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 732 : if ( hCPE->hStereoTCA != NULL )
387 : {
388 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TCA Stereo memory already allocated\n" );
389 : }
390 :
391 732 : 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 732 : stereo_tca_init_enc( hCPE->hStereoTCA, input_Fs );
397 :
398 732 : st = hCPE->hCoreCoder[0];
399 :
400 : /* allocate primary channel substructures */
401 732 : if ( ( error = allocate_CoreCoder_enc( st ) ) != IVAS_ERR_OK )
402 : {
403 0 : return error;
404 : }
405 :
406 : /* allocate CLDFB for primary channel */
407 732 : if ( st->cldfbAnaEnc == NULL )
408 : {
409 732 : 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 732 : if ( st->hBWE_TD == NULL )
417 : {
418 732 : 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 732 : if ( st->cldfbSynTd == NULL )
424 : {
425 732 : if ( ( error = openCldfb( &st->cldfbSynTd, CLDFB_SYNTHESIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
426 : {
427 0 : return error;
428 : }
429 : }
430 :
431 732 : InitSWBencBuffer( st->hBWE_TD );
432 732 : ResetSHBbuffer_Enc( st->hBWE_TD );
433 :
434 732 : 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 732 : fd_bwe_enc_init( st->hBWE_FD );
440 : }
441 :
442 : /* allocate stereo CNG structure */
443 732 : if ( hCPE->hStereoCng == NULL )
444 : {
445 26 : 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 26 : stereo_enc_cng_init( hCPE->hStereoCng );
451 : }
452 : }
453 :
454 : /*--------------------------------------------------------------*
455 : * switching CPE mode to MDCT stereo
456 : *---------------------------------------------------------------*/
457 :
458 1587 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
459 : {
460 : int16_t i;
461 :
462 : /* deallocate data structure of the previous CPE mode */
463 749 : if ( hCPE->hStereoDft != NULL )
464 : {
465 746 : stereo_dft_enc_destroy( &( hCPE->hStereoDft ) );
466 746 : hCPE->hStereoDft = NULL;
467 : }
468 :
469 749 : if ( hCPE->hStereoTD != NULL )
470 : {
471 3 : free( hCPE->hStereoTD );
472 3 : hCPE->hStereoTD = NULL;
473 : }
474 :
475 749 : if ( hCPE->hStereoTCA != NULL )
476 : {
477 749 : free( hCPE->hStereoTCA );
478 749 : hCPE->hStereoTCA = NULL;
479 : }
480 :
481 749 : if ( hCPE->hStereoICBWE != NULL )
482 : {
483 746 : free( hCPE->hStereoICBWE );
484 746 : hCPE->hStereoICBWE = NULL;
485 : }
486 :
487 2247 : for ( i = 0; i < CPE_CHANNELS; i++ )
488 : {
489 : /* deallocate core-coder substructures */
490 1498 : deallocate_CoreCoder_enc( hCPE->hCoreCoder[i] );
491 : }
492 :
493 749 : if ( hCPE->last_element_mode == IVAS_CPE_DFT )
494 : {
495 : /* allocate secondary channel */
496 746 : 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 749 : st = hCPE->hCoreCoder[1];
504 :
505 749 : 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 749 : st->hTcxEnc->spectrum[0] = st->hTcxEnc->spectrum_long;
510 749 : st->hTcxEnc->spectrum[1] = st->hTcxEnc->spectrum_long + N_TCX10_MAX;
511 749 : set_f( st->hTcxEnc->old_out, 0, L_FRAME32k );
512 749 : set_f( st->hTcxEnc->spectrum_long, 0, N_MAX );
513 749 : st->hTcxEnc->tfm_mem = 0.75f;
514 :
515 749 : if ( hCPE->last_element_mode == IVAS_CPE_DFT )
516 : {
517 746 : st->last_core = ACELP_CORE; /* needed to set-up TCX core in SetTCXModeInfo() */
518 : }
519 :
520 749 : 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 749 : 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 749 : st->igf = getIgfPresent( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
530 :
531 : /* allocate and initialize MDCT stereo structure */
532 749 : 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 749 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, hCPE->hCoreCoder[0]->max_bwidth, 0, NULL, 1 );
541 :
542 749 : hCPE->hStereoMdct->isSBAStereoMode = ( ivas_format == SBA_FORMAT && nchan_transport == 2 );
543 :
544 749 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
545 : {
546 24 : 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 749 : if ( hCPE->hCoreCoder[0]->Opt_DTX_ON )
554 : {
555 189 : for ( i = 0; i < CPE_CHANNELS; i++ )
556 : {
557 126 : st = hCPE->hCoreCoder[i];
558 126 : 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 126 : st->currEnergyLookAhead = 6.1e-5f;
564 :
565 126 : if ( st->hDtxEnc == NULL )
566 : {
567 61 : 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 126 : dtx_enc_init( st, 0, FIXED_SID_RATE );
573 :
574 126 : if ( st->hTdCngEnc != NULL )
575 : {
576 63 : free( st->hTdCngEnc );
577 63 : st->hTdCngEnc = NULL;
578 : }
579 :
580 126 : if ( st->hFdCngEnc == NULL )
581 : {
582 61 : if ( ( error = createFdCngEnc( &st->hFdCngEnc ) ) != IVAS_ERR_OK )
583 : {
584 0 : return error;
585 : }
586 :
587 61 : initFdCngEnc( st->hFdCngEnc, st->input_Fs, st->cldfbAnaEnc->scale );
588 61 : 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 420855 : 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 30 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
599 : {
600 0 : return error;
601 : }
602 : }
603 :
604 420855 : 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 420855 : 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 420855 : sts = hCPE->hCoreCoder;
625 420855 : 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 420855 : if ( hCPE->element_mode > IVAS_CPE_DFT && hCPE->input_mem[0] != NULL && hCPE->element_mode != IVAS_CPE_MDCT )
630 : {
631 11373 : for ( n = 0; n < CPE_CHANNELS; n++ )
632 : {
633 7582 : 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 420855 : if ( hCPE->element_mode == IVAS_CPE_DFT )
639 : {
640 59679 : 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 59679 : 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 420855 : 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 306259 : for ( i = 0; i < dft_ovl; i++ )
649 : {
650 305480 : 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 779 : set_f( hCPE->hStereoDft->output_mem_dmx_32k, 0, STEREO_DFT_OVL_32k );
655 :
656 779 : stereo_dft_enc_reset( hCPE->hStereoDft );
657 :
658 : /* update ITD parameters */
659 779 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_TD )
660 : {
661 55 : set_f( hCPE->hStereoDft->hItd->itd, hCPE->hStereoTCA->prevCorrLagStats[2], STEREO_DFT_ENC_DFT_NB );
662 : }
663 :
664 : /* Update the side_gain[] parameters */
665 779 : if ( hCPE->hStereoTCA != NULL && hCPE->last_element_mode != IVAS_CPE_MDCT )
666 : {
667 55 : tmpF = usdequant( hCPE->hStereoTCA->indx_ica_gD, STEREO_TCA_GDMIN, STEREO_TCA_GDSTEP );
668 770 : for ( i = 0; i < STEREO_DFT_BAND_MAX; i++ )
669 : {
670 715 : hCPE->hStereoDft->side_gain[STEREO_DFT_BAND_MAX + i] = tmpF;
671 : }
672 : }
673 :
674 : /* do not allow differential coding of DFT side parameters */
675 779 : hCPE->hStereoDft->res_pred_counter = STEREO_DFT_FEC_THRESHOLD;
676 :
677 : /* update DFT synthesis overlap memory @12.8kHz */
678 88027 : for ( i = 0; i < STEREO_DFT_OVL_12k8; i++ )
679 : {
680 87248 : 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 779 : 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 779 : set_f( hCPE->hStereoDft->output_mem_res_8k, 0, STEREO_DFT_OVL_8k );
688 :
689 779 : hCPE->hCoreCoder[1]->vad_flag = 0;
690 : }
691 :
692 : /* MDCT -> TD stereo switching */
693 420855 : if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_MDCT )
694 : {
695 8 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
696 8 : hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_LEFT_IS_PRIM;
697 8 : hCPE->hStereoTD->tdm_last_SM_flag = 0;
698 8 : hCPE->hStereoTD->tdm_last_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
699 8 : hCPE->hStereoTD->tdm_last_ratio = tdm_ratio_tabl[LRTD_STEREO_LEFT_IS_PRIM];
700 : }
701 : /* DFT -> TD stereo switching */
702 420847 : else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_DFT )
703 : {
704 51 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
705 51 : hCPE->hStereoTD->tdm_last_ratio_idx_SM = LRTD_STEREO_MID_IS_PRIM;
706 51 : hCPE->hStereoTD->tdm_last_SM_flag = 0;
707 51 : 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 51 : if ( hCPE->hStereoClassif->lrtd_mode == 1 )
711 : {
712 51 : set_zero( sts[1]->input - input_frame, input_frame );
713 :
714 51 : hCPE->hStereoTD->tdm_last_ratio = tdm_ratio_tabl[LRTD_STEREO_LEFT_IS_PRIM];
715 51 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
716 :
717 51 : 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 16 : hCPE->hStereoTD->tdm_last_ratio = tdm_ratio_tabl[LRTD_STEREO_RIGHT_IS_PRIM];
720 16 : 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 420855 : if ( hCPE->element_mode > IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_DFT )
727 : {
728 797 : if ( sts[0]->cldfbAnaEnc != NULL )
729 : {
730 112 : offset = sts[0]->cldfbAnaEnc->p_filter_length - sts[0]->cldfbAnaEnc->no_channels;
731 112 : 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 797 : if ( sts[0]->cldfbSynTd != NULL )
735 : {
736 51 : cldfb_reset_memory( sts[0]->cldfbSynTd );
737 51 : sts[0]->currEnergyLookAhead = 6.1e-5f;
738 : }
739 :
740 797 : if ( hCPE->hStereoICBWE == NULL && sts[1]->cldfbAnaEnc != NULL )
741 : {
742 112 : offset = sts[1]->cldfbAnaEnc->p_filter_length - sts[1]->cldfbAnaEnc->no_channels;
743 :
744 112 : if ( hCPE->hStereoTD != NULL && hCPE->hStereoTD->tdm_last_ratio_idx == LRTD_STEREO_LEFT_IS_PRIM )
745 : {
746 35 : 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 77 : 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 112 : if ( sts[1]->cldfbSynTd != NULL )
754 : {
755 51 : cldfb_reset_memory( sts[1]->cldfbSynTd );
756 51 : sts[1]->currEnergyLookAhead = 6.1e-5f;
757 : }
758 : }
759 :
760 797 : sts[1]->last_extl = -1;
761 :
762 : /* no secondary channel in the previous frame -> memory resets */
763 797 : set_zero( sts[1]->old_inp_12k8, L_INP_MEM );
764 797 : set_zero( sts[1]->mem_decim, 2 * L_FILT_MAX );
765 797 : sts[1]->mem_preemph = 0;
766 :
767 797 : set_zero( sts[1]->buf_speech_enc, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
768 797 : set_zero( sts[1]->buf_speech_enc_pe, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
769 :
770 797 : if ( sts[1]->hTcxEnc != NULL )
771 : {
772 746 : set_zero( sts[1]->hTcxEnc->buf_speech_ltp, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
773 : }
774 797 : set_zero( sts[1]->buf_wspeech_enc, L_FRAME16k + L_SUBFR + L_FRAME16k + L_NEXT_MAX_16k );
775 797 : set_zero( sts[1]->buf_synth, OLD_SYNTH_SIZE_ENC + L_FRAME32k );
776 797 : sts[1]->mem_wsp = 0.0f;
777 797 : sts[1]->mem_wsp_enc = 0.0f;
778 797 : init_gp_clip( sts[1]->clip_var );
779 :
780 797 : set_f( sts[1]->Bin_E, 0, L_FFT );
781 797 : 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 797 : sts[1]->last_L_frame = sts[0]->last_L_frame;
786 :
787 797 : pitch_ol_init( &sts[1]->old_thres, &sts[1]->old_pitch, &sts[1]->delta_pit, &sts[1]->old_corr );
788 797 : set_zero( sts[1]->old_wsp, L_WSP_MEM );
789 797 : set_zero( sts[1]->old_wsp2, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
790 797 : set_zero( sts[1]->mem_decim2, 3 );
791 797 : mvs2s( sts[0]->pitch, sts[1]->pitch, 3 );
792 :
793 797 : sts[1]->Nb_ACELP_frames = 0;
794 :
795 : /* populate PCh memories into the SCh */
796 797 : if ( sts[0]->hLPDmem != NULL )
797 : {
798 51 : mvr2r( sts[0]->hLPDmem->old_exc, sts[1]->hLPDmem->old_exc, L_EXC_MEM );
799 : }
800 797 : mvr2r( sts[0]->lsf_old, sts[1]->lsf_old, M );
801 797 : mvr2r( sts[0]->lsp_old, sts[1]->lsp_old, M );
802 797 : mvr2r( sts[0]->lsf_old1, sts[1]->lsf_old1, M );
803 797 : mvr2r( sts[0]->lsp_old1, sts[1]->lsp_old1, M );
804 :
805 797 : sts[1]->last_core_brate = sts[0]->last_core_brate;
806 :
807 797 : sts[1]->GSC_noisy_speech = 0;
808 797 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
809 : {
810 : /* cross-fade overlap region of DFT Stereo downmix and original stereo channels */
811 746 : tmpF = 1.f / sts[0]->encoderLookahead_FB;
812 298246 : for ( i = 0; i < sts[0]->encoderLookahead_FB; i++ )
813 : {
814 297500 : 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 297500 : 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 746 : 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 746 : sts[1]->last_core = sts[0]->last_core;
821 746 : sts[1]->last_coder_type = sts[0]->last_coder_type;
822 746 : sts[1]->last_bwidth = sts[0]->last_bwidth;
823 : }
824 : }
825 420058 : else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_MDCT )
826 : {
827 8 : set_f( sts[0]->hLPDmem->old_exc, 0.0f, L_EXC_MEM );
828 8 : 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 420855 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode != IVAS_CPE_MDCT )
833 : {
834 749 : sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo;
835 749 : sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode;
836 : }
837 :
838 420855 : return;
839 : }
|