Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 <assert.h>
34 : #include <stdint.h>
35 : #include "options.h"
36 : #include "cnst.h"
37 : #include "ivas_cnst.h"
38 : #include "rom_com.h"
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #include "ivas_rom_com.h"
42 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 : #include "wmc_auto.h"
46 :
47 :
48 : /*--------------------------------------------------------------------------*
49 : * Local function prototypes
50 : *--------------------------------------------------------------------------*/
51 :
52 : static void stereo_mode_combined_format_enc( const Encoder_Struct *st_ivas, CPE_ENC_HANDLE hCPE );
53 :
54 :
55 : /*-------------------------------------------------------------------*
56 : * ivas_cpe_enc()
57 : *
58 : * Channel Pair Element (CPE) encoding routine
59 : *-------------------------------------------------------------------*/
60 :
61 5766789 : ivas_error ivas_cpe_enc(
62 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
63 : const int16_t cpe_id, /* i : CPE # identifier */
64 : const float data_f_ch0[], /* i : input signal for channel 0 */
65 : const float data_f_ch1[], /* i : input signal for channel 1 */
66 : const int16_t input_frame, /* i : input frame length per channel */
67 : const int16_t nb_bits_metadata /* i : number of metadata bits */
68 : )
69 : {
70 : CPE_ENC_HANDLE hCPE;
71 : STEREO_DFT_CONFIG_DATA_HANDLE hConfigDft;
72 : Encoder_State **sts;
73 : int16_t n, n_CoreChannels;
74 : float old_inp_12k8[CPE_CHANNELS][L_INP_12k8]; /* buffer of input signal @ 12k8 */
75 : float old_inp_16k[CPE_CHANNELS][L_INP]; /* buffer of input signal @ 16kHz */
76 : float ener[CPE_CHANNELS]; /* residual energy from Levinson-Durbin */
77 : float relE[CPE_CHANNELS]; /* frame relative energy */
78 : float A[CPE_CHANNELS][NB_SUBFR16k * ( M + 1 )]; /* A(z) unquantized for subframes */
79 : float Aw[CPE_CHANNELS][NB_SUBFR16k * ( M + 1 )]; /* weighted A(z) unquantized for subframes */
80 : float epsP[CPE_CHANNELS][M + 1]; /* LP prediction errors */
81 : float lsp_new[CPE_CHANNELS][M]; /* LSPs at the end of the frame */
82 : float lsp_mid[CPE_CHANNELS][M]; /* ISPs in the middle of the frame */
83 : int16_t vad_hover_flag[CPE_CHANNELS]; /* VAD hangover flag */
84 : int16_t attack_flag[CPE_CHANNELS]; /* attack flag (GSC or TC) */
85 : float realBuffer[CPE_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer */
86 : float imagBuffer[CPE_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer */
87 : float old_wsp[CPE_CHANNELS][L_WSP]; /* old weighted input signal */
88 : float pitch_fr[CPE_CHANNELS][NB_SUBFR]; /* fractional pitch values */
89 : float voicing_fr[CPE_CHANNELS][NB_SUBFR]; /* fractional pitch gains */
90 : int16_t loc_harm[CPE_CHANNELS]; /* harmonicity flag */
91 : float cor_map_sum[CPE_CHANNELS]; /* speech/music clasif. parameter */
92 : int16_t vad_flag_dtx[CPE_CHANNELS]; /* HE-SAD flag with additional DTX HO */
93 : float enerBuffer[CPE_CHANNELS][CLDFB_NO_CHANNELS_MAX]; /* energy buffer */
94 : float currFlatness[CPE_CHANNELS]; /* flatness parameter */
95 : float fft_buff[CPE_CHANNELS][2 * L_FFT]; /* FFT buffer */
96 : int16_t tdm_ratio_idx, tdm_ratio_idx_SM; /* temp. TD stereo parameters */
97 : int16_t tdm_SM_or_LRTD_Pri; /* temp. TD stereo parameters */
98 : float tdm_last_ratio; /* temp. TD stereo parameters */
99 : int16_t nb_bits; /* number of DFT stereo side bits */
100 : float fr_bands[CPE_CHANNELS][2 * NB_BANDS]; /* energy in frequency bands */
101 : float Etot_LR[CPE_CHANNELS]; /* total energy */
102 : float lf_E[CPE_CHANNELS][2 * VOIC_BINS]; /* per bin spectrum energy in lf */
103 : int16_t localVAD_HE_SAD[CPE_CHANNELS]; /* HE-SAD flag without hangover, LR channels */
104 : float band_energies_LR[2 * NB_BANDS]; /* energy in critical bands without minimum noise floor E_MIN */
105 : float orig_input[CPE_CHANNELS][L_FRAME48k];
106 : float Etot_last[CPE_CHANNELS];
107 : int32_t tmp, input_Fs;
108 : int16_t max_bwidth, ivas_format;
109 : ENCODER_CONFIG_HANDLE hEncoderConfig;
110 : int32_t ivas_total_brate;
111 : ivas_error error;
112 : int32_t cpe_brate;
113 : int32_t element_brate_ref;
114 : int16_t last_bits_frame_nominal; /* last_bits_frame_nominal for M or PCh channel */
115 : int16_t NFFT_inner;
116 :
117 5766789 : error = IVAS_ERR_OK;
118 5766789 : push_wmops( "ivas_cpe_enc" );
119 :
120 5766789 : hCPE = st_ivas->hCPE[cpe_id];
121 5766789 : sts = hCPE->hCoreCoder;
122 5766789 : hEncoderConfig = st_ivas->hEncoderConfig;
123 5766789 : max_bwidth = hEncoderConfig->max_bwidth;
124 5766789 : ivas_format = hEncoderConfig->ivas_format;
125 5766789 : input_Fs = hEncoderConfig->input_Fs;
126 5766789 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
127 5766789 : element_brate_ref = hCPE->element_brate;
128 5766789 : last_bits_frame_nominal = sts[0]->bits_frame_nominal;
129 :
130 : /*------------------------------------------------------------------*
131 : * Initialization - general
132 : *-----------------------------------------------------------------*/
133 :
134 5766789 : tdm_SM_or_LRTD_Pri = 0;
135 5766789 : tdm_ratio_idx = -1;
136 5766789 : tdm_ratio_idx_SM = -1;
137 5766789 : tdm_last_ratio = 0;
138 :
139 : #ifdef DEBUGGING
140 : if ( hCPE->hCoreCoder[0]->ini_frame == 0 )
141 : {
142 : hCPE->stereo_mode_cmdl = hEncoderConfig->stereo_mode_cmdl;
143 : }
144 : #endif
145 :
146 : /*------------------------------------------------------------------*
147 : * CPE initialization - core coder
148 : *-----------------------------------------------------------------*/
149 :
150 17300367 : for ( n = 0; n < CPE_CHANNELS; n++ )
151 : {
152 11533578 : sts[n]->idchan = n;
153 11533578 : sts[n]->core = -1;
154 11533578 : sts[n]->core_brate = -1; /* updated in dtx() */
155 11533578 : sts[n]->max_bwidth = max_bwidth;
156 11533578 : if ( st_ivas->hMCT == NULL ) /*already updated before CPE call*/
157 : {
158 4144122 : sts[n]->input_bwidth = sts[n]->last_input_bwidth; /* updated in BWD */
159 4144122 : sts[n]->bwidth = sts[n]->last_bwidth; /* updated in BWD */
160 : }
161 11533578 : sts[n]->rate_switching_reset = 0;
162 : #ifdef DEBUGGING
163 : sts[n]->force = hEncoderConfig->force;
164 : sts[n]->id_element = cpe_id + st_ivas->nSCE;
165 : #endif
166 : }
167 :
168 5766789 : mvr2r( data_f_ch0, sts[0]->input, input_frame );
169 5766789 : if ( data_f_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
170 : {
171 4972303 : mvr2r( data_f_ch1, sts[1]->input, input_frame );
172 : }
173 :
174 : /*----------------------------------------------------------------*
175 : * Stereo technology selection
176 : * Front-VAD on input L and R channels
177 : *----------------------------------------------------------------*/
178 :
179 5766789 : if ( sts[0]->ini_frame > 0 && st_ivas->hMCT == NULL )
180 : {
181 2069413 : hCPE->element_mode = select_stereo_mode( hCPE, ivas_format );
182 : }
183 :
184 5766789 : stereo_mode_combined_format_enc( st_ivas, hCPE );
185 :
186 5766789 : if ( ( error = front_vad( hCPE, NULL, hEncoderConfig, &hCPE->hFrontVad[0], st_ivas->hMCT != NULL, input_frame, vad_flag_dtx, fr_bands, Etot_LR, lf_E, localVAD_HE_SAD, vad_hover_flag, band_energies_LR, NULL, NULL ) ) != IVAS_ERR_OK )
187 : {
188 0 : return error;
189 : }
190 :
191 5766789 : sts[0]->element_mode = hCPE->element_mode;
192 5766789 : sts[1]->element_mode = hCPE->element_mode;
193 :
194 5766789 : n_CoreChannels = 2;
195 5766789 : if ( hCPE->element_mode == IVAS_CPE_DFT )
196 : {
197 727812 : n_CoreChannels = 1; /* in DFT stereo, only M channel is coded */
198 :
199 727812 : sts[1]->vad_flag = 0;
200 : }
201 :
202 : /*----------------------------------------------------------------*
203 : * dynamically allocate data structures depending on the actual stereo mode
204 : *----------------------------------------------------------------*/
205 :
206 5766789 : if ( ( error = stereo_memory_enc( hCPE, input_Fs, max_bwidth, &tdm_last_ratio, ivas_format, st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
207 : {
208 0 : return error;
209 : }
210 :
211 5766789 : hConfigDft = NULL;
212 5766789 : if ( hCPE->hStereoDft != NULL )
213 : {
214 727812 : hConfigDft = hCPE->hStereoDft->hConfig;
215 : }
216 :
217 : /*----------------------------------------------------------------*
218 : * Set TD stereo parameters
219 : *----------------------------------------------------------------*/
220 :
221 5766789 : if ( ( error = stereo_set_tdm( hCPE, input_frame ) ) != IVAS_ERR_OK )
222 : {
223 0 : return error;
224 : }
225 :
226 : /*----------------------------------------------------------------*
227 : * Resets/updates in case of stereo switching
228 : *----------------------------------------------------------------*/
229 :
230 5766789 : stereo_switching_enc( hCPE, sts[0]->old_input_signal, input_frame );
231 :
232 : /*----------------------------------------------------------------*
233 : * Temporal inter-channel alignment, stereo adjustment
234 : *----------------------------------------------------------------*/
235 :
236 5766789 : stereo_tca_enc( hCPE, input_frame );
237 :
238 : /*----------------------------------------------------------------*
239 : * Input signal buffering - needed in IC-BWE and TD ITD in MDCT stereo
240 : *----------------------------------------------------------------*/
241 :
242 17300367 : for ( n = 0; n < CPE_CHANNELS; n++ )
243 : {
244 11533578 : mvr2r( sts[n]->input, orig_input[n], input_frame );
245 :
246 11533578 : if ( hCPE->hStereoICBWE != NULL )
247 : {
248 1459634 : hCPE->hStereoICBWE->dataChan[n] = &orig_input[n][0];
249 : }
250 : }
251 :
252 : /*---------------------------------------------------------------*
253 : * Time Domain Transient Detector
254 : *---------------------------------------------------------------*/
255 :
256 17300367 : for ( n = 0; n < CPE_CHANNELS; n++ )
257 : {
258 11533578 : if ( sts[n]->hTranDet == NULL )
259 : {
260 0 : currFlatness[n] = 0;
261 0 : continue;
262 : }
263 :
264 11533578 : if ( !( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC ) )
265 : {
266 11289342 : RunTransientDetection( sts[n]->input, input_frame, sts[n]->hTranDet );
267 : }
268 11533578 : currFlatness[n] = GetTCXAvgTemporalFlatnessMeasure( sts[n]->hTranDet, NSUBBLOCKS, 0 );
269 : }
270 :
271 : /* Synchonize detection for downmix-based stereo */
272 5766789 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
273 : {
274 752336 : set_transient_stereo( hCPE, currFlatness );
275 : }
276 :
277 : /*----------------------------------------------------------------*
278 : * Configuration of stereo encoder
279 : *----------------------------------------------------------------*/
280 :
281 16572555 : for ( n = 0; n < n_CoreChannels; n++ )
282 : {
283 : /* Force to MODE1 in IVAS */
284 10805766 : sts[n]->codec_mode = MODE1;
285 :
286 10805766 : sts[n]->element_mode = hCPE->element_mode;
287 : }
288 :
289 :
290 5766789 : if ( hCPE->element_mode != IVAS_CPE_MDCT && ( hCPE->element_brate != hCPE->last_element_brate || hCPE->last_element_mode != hCPE->element_mode || sts[0]->ini_frame == 0 ||
291 721300 : ( ivas_total_brate != hEncoderConfig->last_ivas_total_brate ) || sts[0]->last_core_brate <= SID_2k40 ) ) /* If the last frame was SID or NO_DATA, we need to run stereo_dft_config here since VAD decision is not known yet */
292 : {
293 57944 : if ( st_ivas->hQMetaData != NULL )
294 : {
295 30889 : if ( ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode != ISM_MODE_NONE )
296 : {
297 14084 : stereo_dft_config( hConfigDft, (int32_t) ( 0.70f * st_ivas->hQMetaData->bits_frame_nominal * FRAMES_PER_SEC ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
298 : }
299 : else
300 : {
301 16805 : stereo_dft_config( hConfigDft, st_ivas->hQMetaData->bits_frame_nominal * FRAMES_PER_SEC, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
302 : }
303 : }
304 : else
305 : {
306 : /* note; "bits_frame_nominal" needed in TD stereo as well */
307 27055 : stereo_dft_config( hConfigDft, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
308 : }
309 : }
310 :
311 5766789 : if ( hCPE->element_mode == IVAS_CPE_TD )
312 : {
313 24524 : if ( hCPE->hStereoTD->tdm_LRTD_flag )
314 : {
315 22519 : sts[0]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
316 22519 : sts[1]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
317 : }
318 : else
319 : {
320 2005 : stereo_dft_config( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
321 : }
322 : }
323 :
324 5766789 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
325 : {
326 : /* compute bit-rate surplus per channel in combined format coding */
327 : int32_t brate_surplus[CPE_CHANNELS];
328 5014453 : if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC )
329 : {
330 125390 : brate_surplus[0] = ( ( hCPE->brate_surplus / FRAMES_PER_SEC ) >> 1 ) * FRAMES_PER_SEC;
331 125390 : brate_surplus[1] = hCPE->brate_surplus - brate_surplus[0];
332 : }
333 :
334 : /* this is just for initialization, the true values of "total_brate" and "bits_frame_channel" are set later */
335 15043359 : for ( n = 0; n < n_CoreChannels; n++ )
336 : {
337 10028906 : if ( st_ivas->hMCT )
338 : {
339 : int16_t lfe_bits;
340 7389456 : lfe_bits = ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT ? st_ivas->hLFE->lfe_bits : 0 );
341 7389456 : sts[n]->total_brate = hCPE->element_brate;
342 7389456 : sts[n]->bits_frame_nominal = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
343 7389456 : sts[n]->bits_frame_channel = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC - lfe_bits - nb_bits_metadata ) / st_ivas->hMCT->nchan_out_woLFE );
344 : }
345 : else
346 : {
347 2639450 : sts[n]->bits_frame_nominal = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
348 2639450 : sts[n]->bits_frame_channel = (int16_t) ( ( hCPE->element_brate / FRAMES_PER_SEC ) / n_CoreChannels );
349 2639450 : sts[n]->total_brate = hCPE->element_brate / n_CoreChannels;
350 :
351 : /* subtract bit-rate for combined format coding */
352 2639450 : if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC )
353 : {
354 250780 : sts[n]->bits_frame_channel += (int16_t) ( brate_surplus[n] / FRAMES_PER_SEC );
355 250780 : sts[n]->total_brate += brate_surplus[n];
356 : }
357 : }
358 : }
359 :
360 : /* reconfiguration in case of bitrate switching */
361 5014453 : if ( hCPE->element_brate != hCPE->last_element_brate && st_ivas->hMCT == NULL )
362 : {
363 : #ifdef DEBUGGING
364 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = hEncoderConfig->mdct_stereo_mode_cmdl;
365 : #endif
366 33107 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 0 );
367 33107 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
368 : }
369 : }
370 :
371 : /*----------------------------------------------------------------*
372 : * Stereo processing
373 : * Stereo down-mix
374 : *----------------------------------------------------------------*/
375 :
376 5766789 : if ( hCPE->element_mode == IVAS_CPE_DFT )
377 : {
378 727812 : stereo_dft_hybrid_ITD_flag( hConfigDft, input_Fs, hCPE->hStereoDft->hItd->hybrid_itd_max );
379 :
380 : /* Time Domain ITD compensation using extrapolation */
381 : #ifdef DEBUG_MODE_DFT
382 : stereo_td_itd( hCPE->hStereoDft->hItd, hCPE->hStereoDft->input_mem_itd, hConfigDft->hybrid_itd_flag, hConfigDft->itd_mode, hCPE->hStereoDft->dft_ovl, sts, input_frame, hCPE->input_mem );
383 : #else
384 727812 : stereo_td_itd( hCPE->hStereoDft->hItd, hCPE->hStereoDft->input_mem_itd, hConfigDft->hybrid_itd_flag, hCPE->hStereoDft->dft_ovl, sts, input_frame, hCPE->input_mem );
385 : #endif
386 :
387 : /* DFT on right and left input channels */
388 727812 : stereo_dft_enc_analyze( sts, CPE_CHANNELS, input_frame, hCPE->hStereoDft, NULL, hCPE->hStereoDft->DFT, hCPE->input_mem );
389 :
390 727812 : sts[0]->total_brate = ( sts[0]->bits_frame_nominal + 10 ) * FRAMES_PER_SEC; /* add small overhead; st[0]->total_brate used in coder_type_modif() */
391 : #ifdef DEBUG_MODE_DFT
392 : hCPE->hStereoDft->res_cod_bits = (int16_t) ( ( hCPE->element_brate ) / FRAMES_PER_SEC - 0.8f * sts[0]->bits_frame_nominal );
393 : #endif
394 :
395 : /* Update DFT Stereo memories */
396 727812 : stereo_dft_enc_update( hCPE->hStereoDft, max_bwidth );
397 :
398 : /* DFT stereo processing */
399 727812 : stereo_dft_enc_process( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
400 : }
401 5038977 : else if ( hCPE->element_mode == IVAS_CPE_TD )
402 : {
403 : /* Determine the energy ratio between the 2 channels */
404 24524 : tdm_ratio_idx = stereo_tdm_ener_analysis( ivas_format, hCPE, input_frame, &tdm_SM_or_LRTD_Pri, &tdm_ratio_idx_SM );
405 :
406 : /* Compute the downmix signal based on the ratio index */
407 24524 : stereo_tdm_downmix( hCPE->hStereoTD, sts[0]->input, sts[1]->input, input_frame, tdm_ratio_idx, ( ( hCPE->hStereoTD->tdm_LRTD_flag == 0 ) ? tdm_SM_or_LRTD_Pri : 0 ), tdm_ratio_idx_SM );
408 :
409 : /* signal the bitrate for BW selection in the SCh */
410 24524 : sts[0]->bits_frame_channel = 0;
411 24524 : sts[1]->bits_frame_channel = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
412 24524 : sts[1]->bits_frame_channel += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
413 24524 : if ( st_ivas->hQMetaData != NULL )
414 : {
415 16082 : sts[1]->bits_frame_channel -= st_ivas->hQMetaData->metadata_max_bits;
416 : }
417 :
418 24524 : Etot_last[0] = sts[0]->hNoiseEst->Etot_last;
419 24524 : Etot_last[1] = sts[1]->hNoiseEst->Etot_last;
420 : }
421 5014453 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
422 : {
423 5014453 : stereo_td_itd_mdct_stereo( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
424 : }
425 :
426 : /*----------------------------------------------------------------*
427 : * DFT stereo: iDFT and resampling on both channels
428 : *----------------------------------------------------------------*/
429 :
430 5766789 : if ( hCPE->element_mode == IVAS_CPE_DFT )
431 : {
432 : int32_t internal_Fs;
433 :
434 727812 : internal_Fs = getTcxonly( IVAS_CPE_MDCT, sts[0]->bits_frame_nominal * FRAMES_PER_SEC, 0, sts[0]->is_ism_format ) == 0 ? INT_FS_16k : max( INT_FS_16k, sts[0]->sr_core );
435 :
436 : /* iDFT at input sampling rate */
437 727812 : stereo_dft_enc_synthesize( hCPE->hStereoDft, sts[0]->input, 0, input_Fs, input_Fs, 0 );
438 :
439 : /* iDFT & resampling to 12.8kHz internal sampling rate */
440 727812 : stereo_dft_enc_synthesize( hCPE->hStereoDft, old_inp_12k8[0] + L_INP_MEM, 0, input_Fs, INT_FS_12k8, 0 );
441 :
442 : /* iDFT & resampling to 16kHz internal sampling rate for M channel */
443 727812 : if ( input_Fs == internal_Fs )
444 : {
445 126906 : mvr2r( sts[0]->input - STEREO_DFT_OVL_16k, old_inp_16k[0] + L_INP_MEM - STEREO_DFT_OVL_16k, input_frame + STEREO_DFT_OVL_16k );
446 : }
447 : else
448 : {
449 600906 : stereo_dft_enc_synthesize( hCPE->hStereoDft, old_inp_16k[0] + L_INP_MEM, 0, input_Fs, internal_Fs, 0 );
450 : }
451 :
452 : /* DFT Stereo: iDFT of residual signal at 8kHz sampling rate */
453 727812 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
454 : {
455 231640 : mvr2r( sts[1]->old_inp_12k8, old_inp_12k8[1], L_INP_MEM );
456 231640 : stereo_dft_enc_synthesize( hCPE->hStereoDft, old_inp_12k8[1] + L_INP_MEM, 1, input_Fs, 8000, 0 );
457 :
458 : /* update old input signal buffer */
459 231640 : mvr2r( old_inp_12k8[1] + L_FRAME8k, sts[1]->old_inp_12k8, L_INP_MEM );
460 : }
461 :
462 : /* no iDFT at input sampling rate for Side channel -> reset the buffer */
463 727812 : set_zero( sts[1]->input, input_frame );
464 : }
465 :
466 : #ifdef DEBUG_MODE_INFO
467 : for ( n = 0; n < n_CoreChannels; n++ )
468 : {
469 : dbgwrite( sts[0]->input - NS2SA( sts[0]->input_Fs, ACELP_LOOK_NS ), sizeof( float ), input_frame, 1, fname( debug_dir, "input_DMX", n, sts[n]->id_element, ENC ) );
470 : }
471 : dbgwrite( &hCPE->element_mode, sizeof( int16_t ), 1, input_frame, fname( debug_dir, "element_mode", 0, sts[0]->id_element, ENC ) );
472 : #endif
473 :
474 : /*----------------------------------------------------------------*
475 : * Front Pre-processing
476 : *----------------------------------------------------------------*/
477 :
478 16572555 : for ( n = 0; n < n_CoreChannels; n++ )
479 : {
480 : #ifdef DEBUGGING
481 : st_ivas->noClipping += check_clipping( hCPE->hCoreCoder[n]->input, input_frame, &st_ivas->maxOverload, &st_ivas->minOverload );
482 :
483 : #endif
484 5397358 : pre_proc_front_ivas( NULL, hCPE, hCPE->element_brate, nb_bits_metadata, input_frame, n, old_inp_12k8[n], old_inp_16k[n],
485 10805766 : &ener[n], &relE[n], A[n], Aw[n], epsP[n], lsp_new[n], lsp_mid[n], &vad_hover_flag[n], &attack_flag[n],
486 10805766 : realBuffer[n], imagBuffer[n], old_wsp[n], pitch_fr[n], voicing_fr[n], &loc_harm[n], &cor_map_sum[n], &vad_flag_dtx[n], enerBuffer[n],
487 16203124 : fft_buff[n], A[0], lsp_new[0], currFlatness[n], tdm_ratio_idx, fr_bands, Etot_LR, lf_E, localVAD_HE_SAD, band_energies_LR, 0, st_ivas->hSpar != NULL ? st_ivas->hSpar->front_vad_flag : 0, 0, 0, ivas_format, st_ivas->hMCT != NULL, hEncoderConfig->last_ivas_total_brate, ivas_total_brate );
488 : }
489 :
490 : /* sanity check -> DTX not supported for more than one SCEs/CPEs */
491 5766789 : if ( st_ivas->nSCE + st_ivas->nCPE > 1 )
492 : {
493 3937002 : if ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA )
494 : {
495 112 : sts[0]->core_brate = -1;
496 112 : sts[0]->total_brate = hCPE->element_brate;
497 : }
498 : }
499 :
500 : /*----------------------------------------------------------------*
501 : * Stereo DTX updates
502 : *----------------------------------------------------------------*/
503 :
504 5766789 : if ( ivas_format == MASA_FORMAT && nb_bits_metadata > 0 && hCPE->hCoreCoder[0]->Opt_DTX_ON )
505 : {
506 34680 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
507 : {
508 19860 : reset_metadata_spatial( ivas_format, hCPE->hMetaData, hCPE->element_brate, &tmp, sts[0]->core_brate, nb_bits_metadata );
509 : }
510 : }
511 :
512 : /* MDCT stereo DTX: active/inactive frame decision; compute FD CNG coherence */
513 5766789 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hEncoderConfig->Opt_DTX_ON )
514 : {
515 331242 : stereoFdCngCoherence( sts, hCPE->last_element_mode, fft_buff );
516 :
517 : /* Reset metadata */
518 331242 : if ( sts[0]->cng_sba_flag || ( ivas_format == SBA_FORMAT ) )
519 : {
520 237452 : reset_metadata_spatial( ivas_format, hCPE->hMetaData, hCPE->element_brate, &tmp, sts[0]->core_brate, nb_bits_metadata );
521 : }
522 : }
523 :
524 : /*----------------------------------------------------------------*
525 : * Core codec configuration
526 : *----------------------------------------------------------------*/
527 :
528 5766789 : if ( !( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) ) /* Reconfigurations not needed with DTX*/
529 : {
530 5730499 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->hMCT == NULL )
531 : {
532 : /* set coded BW for MDCT stereo */
533 1310285 : set_bw_stereo( hCPE );
534 :
535 : /* reconfiguration of MDCT stereo */
536 1310285 : if ( sts[0]->bwidth != sts[0]->last_bwidth || ( ( hCPE->last_element_brate != hCPE->element_brate || hCPE->last_element_mode != hCPE->element_mode ) && sts[0]->bwidth != max_bwidth ) )
537 : {
538 15040 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, sts[0]->bwidth, 0, NULL, 0 );
539 15040 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
540 :
541 15040 : if ( hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
542 : {
543 959 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
544 : {
545 0 : return error;
546 : }
547 : }
548 : }
549 : }
550 :
551 : /* IGF reconfiguration */
552 16490535 : for ( n = 0; n < n_CoreChannels; n++ )
553 : {
554 10760036 : if ( ( hCPE->last_element_brate != hCPE->element_brate || hCPE->element_mode != hCPE->last_element_mode || ( hCPE->element_mode == IVAS_CPE_TD && sts[0]->bits_frame_nominal != last_bits_frame_nominal ) || sts[n]->last_bwidth != sts[n]->bwidth || ( sts[n]->last_core_brate == SID_2k40 || sts[n]->last_core_brate == FRAME_NO_DATA ) ) && ( n == 0 || hCPE->element_mode == IVAS_CPE_MDCT ) )
555 : {
556 : int16_t igf;
557 : int16_t bw;
558 :
559 166860 : bw = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? sts[n]->bwidth : max_bwidth;
560 166860 : igf = getIgfPresent( sts[n]->element_mode, sts[n]->bits_frame_nominal * FRAMES_PER_SEC, bw, sts[n]->rf_mode );
561 :
562 166860 : if ( ( error = IGF_Reconfig( &sts[n]->hIGFEnc, igf, 0, sts[n]->bits_frame_nominal * FRAMES_PER_SEC, bw, sts[n]->element_mode, sts[n]->rf_mode ) ) != IVAS_ERR_OK )
563 : {
564 0 : return error;
565 : }
566 : }
567 : }
568 : }
569 :
570 : /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
571 16572555 : for ( n = 0; n < n_CoreChannels; n++ )
572 : {
573 10805766 : if ( ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) && hCPE->element_mode == IVAS_CPE_DFT )
574 : {
575 26850 : sts[n]->flag_ACELP16k = set_ACELP_flag( hCPE->element_mode, hCPE->element_brate, sts[n]->core_brate, n, sts[0]->tdm_LRTD_flag, sts[n]->bwidth, sts[n]->cng_type );
576 : }
577 : else
578 : {
579 10778916 : sts[n]->flag_ACELP16k = set_ACELP_flag( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, sts[0]->tdm_LRTD_flag, sts[n]->bwidth, sts[n]->cng_type );
580 : }
581 : }
582 :
583 : /* configure TD stereo encoder */
584 5766789 : if ( hCPE->element_mode == IVAS_CPE_TD )
585 : {
586 24524 : tdm_ol_pitch_comparison( hCPE, pitch_fr, voicing_fr );
587 :
588 24524 : tdm_configure_enc( ivas_format, st_ivas->ism_mode, hCPE, Etot_last, tdm_SM_or_LRTD_Pri, tdm_ratio_idx, tdm_ratio_idx_SM, attack_flag[0], nb_bits_metadata );
589 :
590 24524 : if ( hEncoderConfig->Opt_DTX_ON )
591 : {
592 6384 : stereo_cng_upd_counters( hCPE->hStereoCng, hCPE->element_mode, -1, NULL, sts[0]->hTdCngEnc->burst_ho_cnt, NULL );
593 : }
594 : }
595 :
596 : /* modify the coder_type depending on the total_brate per channel */
597 16572555 : for ( n = 0; n < n_CoreChannels; n++ )
598 : {
599 10805766 : if ( ( hCPE->element_mode != IVAS_CPE_DFT && hCPE->element_mode != IVAS_CPE_TD ) || n == 0 ) /* modify coder_type of primary channel */
600 : {
601 : /* limit coder_type depending on the bitrate */
602 10781242 : coder_type_modif( sts[n], relE[n] );
603 : }
604 : }
605 :
606 : /*----------------------------------------------------------------*
607 : * Write IVAS format signaling in SID frames
608 : *----------------------------------------------------------------*/
609 :
610 5766789 : if ( sts[0]->core_brate == SID_2k40 )
611 : {
612 5501 : ivas_write_format_sid( ivas_format, hCPE->element_mode, sts[0]->hBstr, hEncoderConfig->sba_order, hEncoderConfig->sba_planar );
613 : }
614 :
615 : /*----------------------------------------------------------------*
616 : * DFT Stereo residual coding
617 : * DFT Stereo parameters writing into the bitstream
618 : *----------------------------------------------------------------*/
619 :
620 5766789 : cpe_brate = 0;
621 5766789 : if ( hCPE->element_mode == IVAS_CPE_DFT )
622 : {
623 727812 : if ( hEncoderConfig->Opt_DTX_ON )
624 : {
625 291386 : if ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA )
626 : {
627 : /* Reconfigure DFT Stereo for inactive frames */
628 : /* -- nbands may need to be updated here in case the bandwidth was changed due to inactive frames */
629 26850 : NFFT_inner = STEREO_DFT_N_MAX_ENC * inner_frame_tbl[sts[0]->bwidth] / L_FRAME48k;
630 26850 : hCPE->hStereoDft->nbands = stereo_dft_band_config( hCPE->hStereoDft->band_limits, hCPE->hStereoDft->hConfig->band_res, NFFT_inner, ENC );
631 26850 : if ( sts[0]->core_brate == SID_2k40 )
632 : {
633 4051 : stereo_dft_config( hConfigDft, IVAS_SID_5k2, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
634 : }
635 : else
636 : {
637 22799 : stereo_dft_config( hConfigDft, FRAME_NO_DATA, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
638 : }
639 :
640 26850 : stereo_dft_cng_side_gain( hCPE->hStereoDft, hCPE->hStereoCng, sts[0]->core_brate, sts[0]->last_core_brate, sts[0]->bwidth );
641 : #ifdef DEBUG_MODE_DFT
642 : hCPE->hStereoDft->res_cod_bits = 0;
643 : #endif
644 : }
645 : else
646 : {
647 264536 : stereo_cng_upd_counters( hCPE->hStereoCng, hCPE->element_mode, hCPE->hStereoDft->nbands, hCPE->hStereoDft->sidSideGain, sts[0]->hTdCngEnc->burst_ho_cnt, &hCPE->hStereoDft->coh_fade_counter );
648 : }
649 : }
650 :
651 : /* Write stereo bitstream */
652 727812 : cpe_brate = st_ivas->hCPE[0]->element_brate;
653 :
654 : /* DFT stereo side bits */
655 727812 : if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && cpe_brate < MASA_STEREO_MIN_BITRATE && sts[0]->core_brate != SID_2k40 && sts[0]->core_brate != FRAME_NO_DATA )
656 : {
657 119544 : nb_bits = 0; /* Only mono downmix is transmitted in this case */
658 : }
659 608268 : else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) )
660 : {
661 1402 : nb_bits = hCPE->hMetaData->nb_bits_tot;
662 : }
663 : else
664 : {
665 606866 : stereo_dft_enc_write_BS( hCPE, &nb_bits );
666 : }
667 :
668 : /* Residual coding in MDCT domain */
669 727812 : if ( !( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) ) )
670 : {
671 726410 : int16_t max_bits = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC - 0.8f * sts[0]->bits_frame_nominal );
672 726410 : if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
673 : {
674 245326 : max_bits -= nb_bits_metadata;
675 245326 : if ( hCPE->brate_surplus < 0 )
676 : {
677 61085 : max_bits += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
678 : }
679 : }
680 :
681 726410 : stereo_dft_enc_res( hCPE->hStereoDft, old_inp_12k8[1] + L_INP_MEM - STEREO_DFT_OVL_8k, hCPE->hMetaData, &nb_bits, max_bits );
682 : }
683 :
684 727812 : if ( sts[0]->core_brate == FRAME_NO_DATA || sts[0]->core_brate == SID_2k40 )
685 : {
686 26850 : assert( ( nb_bits <= ( ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS ) ) && "Stereo DFT CNG: bit budget is violated" );
687 : }
688 : else
689 : {
690 : #ifdef DEBUGGING
691 : if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
692 : {
693 : assert( ( ( hCPE->element_brate / FRAMES_PER_SEC - nb_bits - nb_bits_metadata + (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC ) ) >= ( 0.8f * sts[0]->bits_frame_nominal ) ) && "Stereo DFT: bit budget is violated" );
694 : }
695 : else
696 : {
697 : assert( ( ( hCPE->element_brate / FRAMES_PER_SEC - nb_bits ) >= ( 0.8f * sts[0]->bits_frame_nominal ) ) && "Stereo DFT: bit budget is violated" );
698 : }
699 :
700 : #endif
701 : /* Flexible total bitrate in M channel */
702 700962 : sts[0]->total_brate = hCPE->element_brate - ( nb_bits * FRAMES_PER_SEC );
703 : }
704 :
705 : /* subtract metadata bitbudget */
706 727812 : sts[0]->total_brate -= ( nb_bits_metadata * FRAMES_PER_SEC );
707 :
708 : /* subtract bit-rate for combined format coding */
709 727812 : if ( ivas_format == MASA_ISM_FORMAT && ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
710 : {
711 81299 : sts[0]->total_brate += hCPE->brate_surplus;
712 : }
713 : }
714 :
715 :
716 : /*----------------------------------------------------------------*
717 : * Core Encoder
718 : *----------------------------------------------------------------*/
719 :
720 5766789 : if ( ( error = ivas_core_enc( NULL, hCPE, st_ivas->hMCT, n_CoreChannels, old_inp_12k8, old_inp_16k, ener, A, Aw, epsP, lsp_new, lsp_mid, vad_hover_flag, attack_flag, realBuffer, imagBuffer, old_wsp, loc_harm, cor_map_sum, vad_flag_dtx, enerBuffer, fft_buff, tdm_SM_or_LRTD_Pri, ivas_format, 0 ) ) != IVAS_ERR_OK )
721 : {
722 0 : return error;
723 : }
724 :
725 : /*----------------------------------------------------------------*
726 : * Common updates
727 : *----------------------------------------------------------------*/
728 :
729 5766789 : hCPE->last_element_brate = hCPE->element_brate;
730 5766789 : hCPE->last_element_mode = hCPE->element_mode;
731 :
732 5766789 : if ( ivas_format == MASA_ISM_FORMAT )
733 : {
734 264000 : hCPE->element_brate = element_brate_ref;
735 : }
736 :
737 5766789 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
738 : {
739 : /* update input samples buffer */
740 452145 : for ( n = 0; n < CPE_CHANNELS; n++ )
741 : {
742 301430 : mvr2r( orig_input[n], sts[n]->old_input_signal, input_frame );
743 : }
744 : }
745 5616074 : else if ( hCPE->element_mode == IVAS_CPE_DFT )
746 : {
747 727812 : mvr2r( sts[0]->input, sts[0]->old_input_signal, input_frame );
748 : }
749 4888262 : else if ( st_ivas->hMCT == NULL ) /* note: in MCT, input buffers are updated later in ivas_mct_enc() */
750 : {
751 : /* update input samples buffer */
752 3580602 : for ( n = 0; n < CPE_CHANNELS; n++ )
753 : {
754 2387068 : mvr2r( sts[n]->input, sts[n]->old_input_signal, input_frame );
755 : }
756 : }
757 :
758 5766789 : if ( hCPE->hFrontVad[0] != NULL )
759 : {
760 297770 : hCPE->hFrontVad[0]->ini_frame++;
761 297770 : hCPE->hFrontVad[0]->ini_frame = min( hCPE->hFrontVad[0]->ini_frame, MAX_FRAME_COUNTER );
762 : }
763 :
764 : /* Store previous attack detection flag */
765 17300367 : for ( n = 0; n < CPE_CHANNELS; n++ )
766 : {
767 11533578 : sts[n]->hTranDet->transientDetector.prev_bIsAttackPresent = sts[n]->hTranDet->transientDetector.bIsAttackPresent;
768 : }
769 :
770 : #ifdef DEBUG_MODE_INFO
771 : if ( hCPE->element_mode == IVAS_CPE_DFT )
772 : {
773 : n = (int16_t) hCPE->hStereoDft->hItd->itd[1];
774 : dbgwrite( &n, 2, 1, input_frame, "res/itd" );
775 : n = 0;
776 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_refChan" );
777 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_NCShift" );
778 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_ica_gD" );
779 : n = -1;
780 : // dbgwrite( &n, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
781 : }
782 : else if ( hCPE->element_mode == IVAS_CPE_TD )
783 : {
784 : dbgwrite( &hCPE->hStereoTCA->refChanIndx, 2, 1, input_frame, "res/TCA_idx_refChan" );
785 : n = hCPE->hStereoTCA->refChanIndx == 0 ? hCPE->hStereoTCA->indx_ica_NCShift : -hCPE->hStereoTCA->indx_ica_NCShift;
786 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_NCShift" );
787 : dbgwrite( &hCPE->hStereoTCA->indx_ica_gD, 2, 1, input_frame, "res/TCA_idx_ica_gD" );
788 :
789 : n = hCPE->hStereoTCA->corrLagStats[2];
790 : dbgwrite( &n, 2, 1, input_frame, "res/itd" );
791 :
792 : dbgwrite( &tdm_ratio_idx, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
793 : }
794 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
795 : {
796 : n = -2;
797 : // dbgwrite( &n, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
798 : }
799 :
800 : {
801 : float tmpF = ivas_total_brate / 1000.0f;
802 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, cpe_id, ENC ) );
803 : }
804 : #endif
805 :
806 5766789 : pop_wmops();
807 5766789 : return error;
808 : }
809 :
810 :
811 : /*-------------------------------------------------------------------------
812 : * create_cpe_enc()
813 : *
814 : * Create, allocate and initialize IVAS encoder CPE handle
815 : *-------------------------------------------------------------------------*/
816 :
817 30268 : ivas_error create_cpe_enc(
818 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
819 : const int16_t cpe_id, /* i : CPE # identifier */
820 : const int32_t element_brate /* i : element bitrate */
821 : )
822 : {
823 : int16_t n;
824 : int16_t ivas_format, element_mode_init, max_bwidth;
825 : int32_t input_Fs;
826 : CPE_ENC_HANDLE hCPE;
827 : Encoder_State *st;
828 : ENCODER_CONFIG_HANDLE hEncoderConfig;
829 : ivas_error error;
830 :
831 30268 : error = IVAS_ERR_OK;
832 :
833 30268 : hEncoderConfig = st_ivas->hEncoderConfig;
834 :
835 30268 : ivas_format = hEncoderConfig->ivas_format;
836 30268 : element_mode_init = hEncoderConfig->element_mode_init;
837 30268 : input_Fs = hEncoderConfig->input_Fs;
838 30268 : max_bwidth = hEncoderConfig->max_bwidth;
839 :
840 : /*-----------------------------------------------------------------*
841 : * Allocate CPE handle
842 : *-----------------------------------------------------------------*/
843 :
844 30268 : if ( ( hCPE = (CPE_ENC_HANDLE) malloc( sizeof( CPE_ENC_DATA ) ) ) == NULL )
845 : {
846 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
847 : }
848 :
849 : /*-----------------------------------------------------------------*
850 : * Initialization - general parameters
851 : *-----------------------------------------------------------------*/
852 :
853 30268 : hCPE->cpe_id = cpe_id;
854 30268 : hCPE->element_brate = element_brate;
855 30268 : hCPE->last_element_brate = hCPE->element_brate;
856 30268 : hCPE->element_mode = element_mode_init;
857 30268 : hCPE->last_element_mode = element_mode_init;
858 :
859 30268 : hCPE->hStereoDft = NULL;
860 30268 : hCPE->hStereoTD = NULL;
861 30268 : hCPE->hStereoMdct = NULL;
862 30268 : hCPE->hStereoTCA = NULL;
863 30268 : hCPE->hStereoICBWE = NULL;
864 30268 : hCPE->hMetaData = NULL;
865 :
866 30268 : hCPE->hStereoCng = NULL;
867 30268 : hCPE->hFrontVad[0] = NULL;
868 30268 : hCPE->hFrontVad[1] = NULL;
869 :
870 30268 : hCPE->brate_surplus = 0;
871 :
872 : /*-----------------------------------------------------------------*
873 : * Input memory buffer: allocate and initialize
874 : *-----------------------------------------------------------------*/
875 :
876 90804 : for ( n = 0; n < CPE_CHANNELS; n++ )
877 : {
878 60536 : if ( ivas_format == STEREO_FORMAT || ivas_format == MASA_FORMAT || ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA ) || ivas_format == MASA_ISM_FORMAT )
879 : {
880 1604 : if ( ( hCPE->input_mem[n] = (float *) malloc( sizeof( float ) * NS2SA( input_Fs, STEREO_DFT_OVL_NS ) ) ) == NULL )
881 : {
882 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
883 : }
884 :
885 1604 : set_zero( hCPE->input_mem[n], NS2SA( input_Fs, STEREO_DFT_OVL_NS ) );
886 : }
887 : else
888 : {
889 58932 : hCPE->input_mem[n] = NULL;
890 : }
891 : }
892 :
893 : /*-----------------------------------------------------------------*
894 : * stereo classifier: allocate and initialize
895 : *-----------------------------------------------------------------*/
896 :
897 30268 : if ( ( hCPE->hStereoClassif = (STEREO_CLASSIF_HANDLE) malloc( sizeof( STEREO_CLASSIF_DATA ) ) ) == NULL )
898 : {
899 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo classifier structure\n" ) );
900 : }
901 :
902 30268 : stereo_classifier_init( hCPE->hStereoClassif );
903 :
904 : /*-----------------------------------------------------------------*
905 : * Metadata: allocate and initialize
906 : *-----------------------------------------------------------------*/
907 :
908 30268 : if ( cpe_id == ( st_ivas->nCPE - 1 ) )
909 : {
910 13122 : if ( ( error = ivas_initialize_MD_bstr_enc( &( hCPE->hMetaData ), st_ivas ) ) != IVAS_ERR_OK )
911 : {
912 0 : return error;
913 : }
914 : }
915 :
916 : /*-----------------------------------------------------------------*
917 : * CoreCoder, 2 instances: allocate and initialize
918 : *-----------------------------------------------------------------*/
919 :
920 90804 : for ( n = 0; n < CPE_CHANNELS; n++ )
921 : {
922 60536 : if ( ( st = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
923 : {
924 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
925 : }
926 :
927 60536 : copy_encoder_config( st_ivas, st, 1 );
928 :
929 60536 : st->total_brate = hCPE->element_brate / ( st_ivas->nCPE > 1 ? 1 : CPE_CHANNELS ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
930 60536 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
931 :
932 60536 : if ( ( error = init_encoder( st, st_ivas, n, 0, ISM_MODE_NONE, hCPE->element_brate ) ) != IVAS_ERR_OK )
933 : {
934 0 : return error;
935 : }
936 :
937 60536 : if ( st->hFdCngEnc != NULL )
938 : {
939 2021 : st->hFdCngEnc->hFdCngCom->CngBitrate = hCPE->element_brate - 1;
940 : }
941 :
942 60536 : hCPE->hCoreCoder[n] = st;
943 : }
944 :
945 : /*-----------------------------------------------------------------*
946 : * LR VAD initialization
947 : *-----------------------------------------------------------------*/
948 :
949 30268 : if ( hEncoderConfig->Opt_DTX_ON )
950 : {
951 1026 : if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT )
952 : {
953 31 : if ( ( hCPE->hStereoCng = (STEREO_CNG_ENC_HANDLE) malloc( sizeof( STEREO_CNG_ENC ) ) ) == NULL )
954 : {
955 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo Cng for Unified/TD \n" ) );
956 : }
957 :
958 31 : stereo_enc_cng_init( hCPE->hStereoCng );
959 : }
960 : }
961 : else
962 : {
963 29242 : hCPE->hStereoCng = NULL;
964 : }
965 :
966 : #ifdef DEBUGGING
967 : if ( hEncoderConfig->Opt_DTX_ON && ( hCPE->element_mode == IVAS_CPE_TD || hEncoderConfig->stereo_mode_cmdl == 1 ) && !( ivas_format == MASA_FORMAT && element_mode_init == IVAS_CPE_MDCT ) )
968 : #else
969 30268 : if ( hEncoderConfig->Opt_DTX_ON && element_mode_init != IVAS_CPE_MDCT )
970 : #endif
971 : {
972 93 : for ( n = 0; n < CPE_CHANNELS; n++ )
973 : {
974 62 : if ( ( error = front_vad_create( &( hCPE->hFrontVad[n] ), hEncoderConfig ) ) != IVAS_ERR_OK )
975 : {
976 0 : return error;
977 : }
978 : }
979 : }
980 : else
981 : {
982 90711 : for ( n = 0; n < CPE_CHANNELS; n++ )
983 : {
984 60474 : hCPE->hFrontVad[n] = NULL;
985 : }
986 : }
987 :
988 : /*-----------------------------------------------------------------*
989 : * DFT stereo initialization
990 : *-----------------------------------------------------------------*/
991 :
992 30268 : if ( hCPE->element_mode == IVAS_CPE_DFT )
993 : {
994 97 : if ( ( error = stereo_dft_enc_create( &( hCPE->hStereoDft ), input_Fs, max_bwidth ) ) != IVAS_ERR_OK )
995 : {
996 0 : return error;
997 : }
998 : }
999 :
1000 : /*-----------------------------------------------------------------*
1001 : * Temporal inter-channel alignment initialization
1002 : *-----------------------------------------------------------------*/
1003 :
1004 30268 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1005 : {
1006 97 : if ( ( hCPE->hStereoTCA = (STEREO_TCA_ENC_HANDLE) malloc( sizeof( STEREO_TCA_ENC_DATA ) ) ) == NULL )
1007 : {
1008 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
1009 : }
1010 :
1011 97 : stereo_tca_init_enc( hCPE->hStereoTCA, input_Fs );
1012 : }
1013 :
1014 : /*-----------------------------------------------------------------*
1015 : * Stereo IC BWE initialization
1016 : *-----------------------------------------------------------------*/
1017 :
1018 30268 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1019 : {
1020 97 : if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_ENC_HANDLE) malloc( sizeof( STEREO_ICBWE_ENC_DATA ) ) ) == NULL )
1021 : {
1022 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
1023 : }
1024 :
1025 97 : stereo_icBWE_init_enc( hCPE->hStereoICBWE );
1026 : }
1027 :
1028 : /*-----------------------------------------------------------------*
1029 : * TD stereo initialization
1030 : *-----------------------------------------------------------------*/
1031 :
1032 30268 : if ( hCPE->element_mode == IVAS_CPE_TD )
1033 : {
1034 0 : if ( ( hCPE->hStereoTD = (STEREO_TD_ENC_DATA_HANDLE) malloc( sizeof( STEREO_TD_ENC_DATA ) ) ) == NULL )
1035 : {
1036 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
1037 : }
1038 :
1039 0 : stereo_td_init_enc( hCPE->hStereoTD, hCPE->last_element_mode );
1040 : }
1041 :
1042 : /*-----------------------------------------------------------------*
1043 : * MDCT stereo initialization
1044 : *-----------------------------------------------------------------*/
1045 :
1046 30268 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->nCPE == 1 )
1047 : {
1048 2551 : if ( ( hCPE->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
1049 : {
1050 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
1051 : }
1052 :
1053 : #ifdef DEBUGGING
1054 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = st_ivas->hEncoderConfig->mdct_stereo_mode_cmdl;
1055 : #endif
1056 2551 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 1 );
1057 2551 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
1058 :
1059 2551 : if ( hCPE->element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
1060 : {
1061 14 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
1062 : {
1063 0 : return error;
1064 : }
1065 : }
1066 : }
1067 :
1068 30268 : st_ivas->hCPE[cpe_id] = hCPE;
1069 :
1070 30268 : return error;
1071 : }
1072 :
1073 :
1074 : /*-------------------------------------------------------------------------
1075 : * destroy_cpe_enc()
1076 : *
1077 : * Destroy and deallocate IVAS encoder CPE handle
1078 : *-------------------------------------------------------------------------*/
1079 :
1080 30268 : void destroy_cpe_enc(
1081 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structure */
1082 : )
1083 : {
1084 : int16_t n;
1085 : Encoder_State *st;
1086 :
1087 30268 : if ( hCPE->hStereoClassif != NULL )
1088 : {
1089 30268 : free( hCPE->hStereoClassif );
1090 30268 : hCPE->hStereoClassif = NULL;
1091 : }
1092 :
1093 90804 : for ( n = 0; n < CPE_CHANNELS; n++ )
1094 : {
1095 60536 : if ( hCPE->input_mem[n] != NULL )
1096 : {
1097 1604 : free( hCPE->input_mem[n] );
1098 1604 : hCPE->input_mem[n] = NULL;
1099 : }
1100 : }
1101 :
1102 30268 : ivas_destroy_MD_bstr_enc( &( hCPE->hMetaData ) );
1103 :
1104 90804 : for ( n = 0; n < CPE_CHANNELS; n++ )
1105 : {
1106 60536 : st = hCPE->hCoreCoder[n];
1107 :
1108 60536 : if ( st != NULL )
1109 : {
1110 60536 : destroy_core_enc( st );
1111 60536 : st = NULL;
1112 : }
1113 : }
1114 :
1115 30268 : if ( hCPE->hStereoDft != NULL )
1116 : {
1117 87 : stereo_dft_enc_destroy( &hCPE->hStereoDft );
1118 87 : hCPE->hStereoDft = NULL;
1119 : }
1120 :
1121 30268 : if ( hCPE->hStereoTD != NULL )
1122 : {
1123 1 : free( hCPE->hStereoTD );
1124 1 : hCPE->hStereoTD = NULL;
1125 : }
1126 :
1127 30268 : if ( hCPE->hStereoMdct != NULL )
1128 : {
1129 2557 : stereo_mdct_enc_destroy( &hCPE->hStereoMdct );
1130 2557 : hCPE->hStereoMdct = NULL;
1131 : }
1132 :
1133 30268 : if ( hCPE->hStereoTCA != NULL )
1134 : {
1135 88 : free( hCPE->hStereoTCA );
1136 88 : hCPE->hStereoTCA = NULL;
1137 : }
1138 :
1139 30268 : if ( hCPE->hStereoICBWE != NULL )
1140 : {
1141 88 : free( hCPE->hStereoICBWE );
1142 88 : hCPE->hStereoICBWE = NULL;
1143 : }
1144 :
1145 30268 : if ( hCPE->hStereoCng != NULL )
1146 : {
1147 55 : free( hCPE->hStereoCng );
1148 55 : hCPE->hStereoCng = NULL;
1149 : }
1150 :
1151 30268 : if ( hCPE->hFrontVad[0] != NULL )
1152 : {
1153 87 : for ( n = 0; n < CPE_CHANNELS; n++ )
1154 : {
1155 58 : front_vad_destroy( &hCPE->hFrontVad[n] );
1156 58 : hCPE->hFrontVad[n] = NULL;
1157 : }
1158 : }
1159 :
1160 30268 : free( hCPE );
1161 :
1162 30268 : return;
1163 : }
1164 :
1165 :
1166 : /*-------------------------------------------------------------------------
1167 : * stereo_mode_combined_format_enc()
1168 : *
1169 : * Set stereo format in a combined format
1170 : *-------------------------------------------------------------------------*/
1171 :
1172 5766789 : static void stereo_mode_combined_format_enc(
1173 : const Encoder_Struct *st_ivas, /* i : encoder main structure */
1174 : CPE_ENC_HANDLE hCPE /* i/o: CPE handle */
1175 : )
1176 : {
1177 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1178 : int32_t element_brate_ref;
1179 :
1180 5766789 : hEncoderConfig = st_ivas->hEncoderConfig;
1181 :
1182 5766789 : if ( hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
1183 : {
1184 264000 : element_brate_ref = hCPE->element_brate;
1185 :
1186 264000 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC &&
1187 86500 : ( ( hEncoderConfig->nchan_ism == 3 && hEncoderConfig->ivas_total_brate == IVAS_96k ) ||
1188 79204 : ( hEncoderConfig->nchan_ism == 4 && hEncoderConfig->ivas_total_brate == IVAS_128k ) ) )
1189 : {
1190 9060 : if ( hCPE->element_brate + hCPE->brate_surplus > IVAS_64k )
1191 : {
1192 0 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = 0;
1193 : }
1194 : else
1195 : {
1196 9060 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt++;
1197 9060 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = min( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, OMASA_STEREO_SW_CNT_MAX );
1198 : }
1199 :
1200 9060 : if ( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt < OMASA_STEREO_SW_CNT_MAX )
1201 : {
1202 0 : hCPE->element_mode = IVAS_CPE_MDCT;
1203 0 : hCPE->element_brate = IVAS_64k;
1204 0 : hCPE->brate_surplus -= ( hCPE->element_brate - element_brate_ref );
1205 : }
1206 :
1207 : /* write OMASA stereo mode signalling */
1208 9060 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
1209 : {
1210 0 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 1, NBITS_ELEMENT_MODE );
1211 : }
1212 : else
1213 : {
1214 9060 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 0, NBITS_ELEMENT_MODE );
1215 : }
1216 : }
1217 : }
1218 :
1219 5766789 : return;
1220 : }
|