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 <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 1194185 : 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 :
116 1194185 : error = IVAS_ERR_OK;
117 1194185 : push_wmops( "ivas_cpe_enc" );
118 :
119 1194185 : hCPE = st_ivas->hCPE[cpe_id];
120 1194185 : sts = hCPE->hCoreCoder;
121 1194185 : hEncoderConfig = st_ivas->hEncoderConfig;
122 1194185 : max_bwidth = hEncoderConfig->max_bwidth;
123 1194185 : ivas_format = hEncoderConfig->ivas_format;
124 1194185 : input_Fs = hEncoderConfig->input_Fs;
125 1194185 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
126 1194185 : element_brate_ref = hCPE->element_brate;
127 1194185 : last_bits_frame_nominal = sts[0]->bits_frame_nominal;
128 :
129 : /*------------------------------------------------------------------*
130 : * Initialization - general
131 : *-----------------------------------------------------------------*/
132 :
133 1194185 : tdm_SM_or_LRTD_Pri = 0;
134 1194185 : tdm_ratio_idx = -1;
135 1194185 : tdm_ratio_idx_SM = -1;
136 1194185 : tdm_last_ratio = 0;
137 :
138 : #ifdef DEBUGGING
139 : if ( hCPE->hCoreCoder[0]->ini_frame == 0 )
140 : {
141 : hCPE->stereo_mode_cmdl = hEncoderConfig->stereo_mode_cmdl;
142 : }
143 : #endif
144 :
145 : /*------------------------------------------------------------------*
146 : * CPE initialization - core coder
147 : *-----------------------------------------------------------------*/
148 :
149 3582555 : for ( n = 0; n < CPE_CHANNELS; n++ )
150 : {
151 2388370 : sts[n]->idchan = n;
152 2388370 : sts[n]->core = -1;
153 2388370 : sts[n]->core_brate = -1; /* updated in dtx() */
154 2388370 : sts[n]->max_bwidth = max_bwidth;
155 2388370 : if ( st_ivas->hMCT == NULL ) /*already updated before CPE call*/
156 : {
157 611754 : sts[n]->input_bwidth = sts[n]->last_input_bwidth; /* updated in BWD */
158 611754 : sts[n]->bwidth = sts[n]->last_bwidth; /* updated in BWD */
159 : }
160 2388370 : sts[n]->rate_switching_reset = 0;
161 : #ifdef DEBUGGING
162 : sts[n]->force = hEncoderConfig->force;
163 : sts[n]->id_element = cpe_id + st_ivas->nSCE;
164 : #endif
165 : }
166 :
167 1194185 : mvr2r( data_f_ch0, sts[0]->input, input_frame );
168 1194185 : if ( data_f_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
169 : {
170 1007716 : mvr2r( data_f_ch1, sts[1]->input, input_frame );
171 : }
172 :
173 : /*----------------------------------------------------------------*
174 : * Stereo technology selection
175 : * Front-VAD on input L and R channels
176 : *----------------------------------------------------------------*/
177 :
178 1194185 : if ( sts[0]->ini_frame > 0 && st_ivas->hMCT == NULL )
179 : {
180 303625 : hCPE->element_mode = select_stereo_mode( hCPE, ivas_format );
181 : }
182 :
183 1194185 : stereo_mode_combined_format_enc( st_ivas, hCPE );
184 :
185 1194185 : 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 )
186 : {
187 0 : return error;
188 : }
189 :
190 1194185 : sts[0]->element_mode = hCPE->element_mode;
191 1194185 : sts[1]->element_mode = hCPE->element_mode;
192 :
193 1194185 : n_CoreChannels = 2;
194 1194185 : if ( hCPE->element_mode == IVAS_CPE_DFT )
195 : {
196 110182 : n_CoreChannels = 1; /* in DFT stereo, only M channel is coded */
197 :
198 110182 : sts[1]->vad_flag = 0;
199 : }
200 :
201 : /*----------------------------------------------------------------*
202 : * dynamically allocate data structures depending on the actual stereo mode
203 : *----------------------------------------------------------------*/
204 :
205 1194185 : if ( ( error = stereo_memory_enc( hCPE, input_Fs, max_bwidth, &tdm_last_ratio, ivas_format, st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
206 : {
207 0 : return error;
208 : }
209 :
210 1194185 : hConfigDft = NULL;
211 1194185 : if ( hCPE->hStereoDft != NULL )
212 : {
213 110182 : hConfigDft = hCPE->hStereoDft->hConfig;
214 : }
215 :
216 : /*----------------------------------------------------------------*
217 : * Set TD stereo parameters
218 : *----------------------------------------------------------------*/
219 :
220 1194185 : if ( ( error = stereo_set_tdm( hCPE, input_frame ) ) != IVAS_ERR_OK )
221 : {
222 0 : return error;
223 : }
224 :
225 : /*----------------------------------------------------------------*
226 : * Resets/updates in case of stereo switching
227 : *----------------------------------------------------------------*/
228 :
229 1194185 : stereo_switching_enc( hCPE, sts[0]->old_input_signal, input_frame );
230 :
231 : /*----------------------------------------------------------------*
232 : * Temporal inter-channel alignment, stereo adjustment
233 : *----------------------------------------------------------------*/
234 :
235 1194185 : stereo_tca_enc( hCPE, input_frame );
236 :
237 : /*----------------------------------------------------------------*
238 : * Input signal buffering - needed in IC-BWE and TD ITD in MDCT stereo
239 : *----------------------------------------------------------------*/
240 :
241 3582555 : for ( n = 0; n < CPE_CHANNELS; n++ )
242 : {
243 2388370 : mvr2r( sts[n]->input, orig_input[n], input_frame );
244 :
245 2388370 : if ( hCPE->hStereoICBWE != NULL )
246 : {
247 220690 : hCPE->hStereoICBWE->dataChan[n] = &orig_input[n][0];
248 : }
249 : }
250 :
251 : /*---------------------------------------------------------------*
252 : * Time Domain Transient Detector
253 : *---------------------------------------------------------------*/
254 :
255 3582555 : for ( n = 0; n < CPE_CHANNELS; n++ )
256 : {
257 2388370 : if ( sts[n]->hTranDet == NULL )
258 : {
259 0 : currFlatness[n] = 0;
260 0 : continue;
261 : }
262 :
263 2388370 : if ( !( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC ) )
264 : {
265 2329610 : RunTransientDetection( sts[n]->input, input_frame, sts[n]->hTranDet );
266 : }
267 2388370 : currFlatness[n] = GetTCXAvgTemporalFlatnessMeasure( sts[n]->hTranDet, NSUBBLOCKS, 0 );
268 : }
269 :
270 : /* Synchonize detection for downmix-based stereo */
271 1194185 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
272 : {
273 114320 : set_transient_stereo( hCPE, currFlatness );
274 : }
275 :
276 : /*----------------------------------------------------------------*
277 : * Configuration of stereo encoder
278 : *----------------------------------------------------------------*/
279 :
280 3472373 : for ( n = 0; n < n_CoreChannels; n++ )
281 : {
282 : /* Force to MODE1 in IVAS */
283 2278188 : sts[n]->codec_mode = MODE1;
284 :
285 2278188 : sts[n]->element_mode = hCPE->element_mode;
286 : }
287 :
288 :
289 1194185 : 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 ||
290 112124 : ( 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 */
291 : {
292 13267 : if ( st_ivas->hQMetaData != NULL )
293 : {
294 2330 : if ( ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode != ISM_MODE_NONE )
295 : {
296 731 : 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 );
297 : }
298 : else
299 : {
300 1599 : stereo_dft_config( hConfigDft, st_ivas->hQMetaData->bits_frame_nominal * FRAMES_PER_SEC, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
301 : }
302 : }
303 : else
304 : {
305 : /* note; "bits_frame_nominal" needed in TD stereo as well */
306 10937 : stereo_dft_config( hConfigDft, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
307 : }
308 : }
309 :
310 1194185 : if ( hCPE->element_mode == IVAS_CPE_TD )
311 : {
312 4138 : if ( hCPE->hStereoTD->tdm_LRTD_flag )
313 : {
314 3975 : sts[0]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
315 3975 : sts[1]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
316 : }
317 : else
318 : {
319 163 : stereo_dft_config( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
320 : }
321 : }
322 :
323 1194185 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
324 : {
325 : /* compute bit-rate surplus per channel in combined format coding */
326 : int32_t brate_surplus[CPE_CHANNELS];
327 1079865 : 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 )
328 : {
329 43344 : brate_surplus[0] = ( ( hCPE->brate_surplus / FRAMES_PER_SEC ) >> 1 ) * FRAMES_PER_SEC;
330 43344 : brate_surplus[1] = hCPE->brate_surplus - brate_surplus[0];
331 : }
332 :
333 : /* this is just for initialization, the true values of "total_brate" and "bits_frame_channel" are set later */
334 3239595 : for ( n = 0; n < n_CoreChannels; n++ )
335 : {
336 2159730 : if ( st_ivas->hMCT )
337 : {
338 : int16_t lfe_bits;
339 1776616 : lfe_bits = ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT ? st_ivas->hLFE->lfe_bits : 0 );
340 1776616 : sts[n]->total_brate = hCPE->element_brate;
341 1776616 : sts[n]->bits_frame_nominal = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
342 1776616 : sts[n]->bits_frame_channel = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC - lfe_bits - nb_bits_metadata ) / st_ivas->hMCT->nchan_out_woLFE );
343 : }
344 : else
345 : {
346 383114 : sts[n]->bits_frame_nominal = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
347 383114 : sts[n]->bits_frame_channel = (int16_t) ( ( hCPE->element_brate / FRAMES_PER_SEC ) / n_CoreChannels );
348 383114 : sts[n]->total_brate = hCPE->element_brate / n_CoreChannels;
349 :
350 : /* subtract bit-rate for combined format coding */
351 383114 : 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 )
352 : {
353 86688 : sts[n]->bits_frame_channel += (int16_t) ( brate_surplus[n] / FRAMES_PER_SEC );
354 86688 : sts[n]->total_brate += brate_surplus[n];
355 : }
356 : }
357 : }
358 :
359 : /* reconfiguration in case of bitrate switching */
360 1079865 : if ( hCPE->element_brate != hCPE->last_element_brate && st_ivas->hMCT == NULL )
361 : {
362 : #ifdef DEBUGGING
363 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = hEncoderConfig->mdct_stereo_mode_cmdl;
364 : #endif
365 1918 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 0 );
366 1918 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
367 : }
368 : }
369 :
370 : /*----------------------------------------------------------------*
371 : * Stereo processing
372 : * Stereo down-mix
373 : *----------------------------------------------------------------*/
374 :
375 1194185 : if ( hCPE->element_mode == IVAS_CPE_DFT )
376 : {
377 110182 : stereo_dft_hybrid_ITD_flag( hConfigDft, input_Fs, hCPE->hStereoDft->hItd->hybrid_itd_max );
378 :
379 : /* Time Domain ITD compensation using extrapolation */
380 : #ifdef DEBUG_MODE_DFT
381 : 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 );
382 : #else
383 110182 : 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 );
384 : #endif
385 :
386 : /* DFT on right and left input channels */
387 110182 : stereo_dft_enc_analyze( sts, CPE_CHANNELS, input_frame, hCPE->hStereoDft, NULL, hCPE->hStereoDft->DFT, hCPE->input_mem );
388 :
389 110182 : 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() */
390 : #ifdef DEBUG_MODE_DFT
391 : hCPE->hStereoDft->res_cod_bits = (int16_t) ( ( hCPE->element_brate ) / FRAMES_PER_SEC - 0.8f * sts[0]->bits_frame_nominal );
392 : #endif
393 :
394 : /* Update DFT Stereo memories */
395 110182 : stereo_dft_enc_update( hCPE->hStereoDft, max_bwidth );
396 :
397 : /* DFT stereo processing */
398 110182 : stereo_dft_enc_process( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
399 : }
400 1084003 : else if ( hCPE->element_mode == IVAS_CPE_TD )
401 : {
402 : /* Determine the energy ratio between the 2 channels */
403 4138 : tdm_ratio_idx = stereo_tdm_ener_analysis( ivas_format, hCPE, input_frame, &tdm_SM_or_LRTD_Pri, &tdm_ratio_idx_SM );
404 :
405 : /* Compute the downmix signal based on the ratio index */
406 4138 : 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 );
407 :
408 : /* signal the bitrate for BW selection in the SCh */
409 4138 : sts[0]->bits_frame_channel = 0;
410 4138 : sts[1]->bits_frame_channel = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
411 4138 : sts[1]->bits_frame_channel += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
412 4138 : if ( st_ivas->hQMetaData != NULL )
413 : {
414 150 : sts[1]->bits_frame_channel -= st_ivas->hQMetaData->metadata_max_bits;
415 : }
416 :
417 4138 : Etot_last[0] = sts[0]->hNoiseEst->Etot_last;
418 4138 : Etot_last[1] = sts[1]->hNoiseEst->Etot_last;
419 : }
420 1079865 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
421 : {
422 1079865 : stereo_td_itd_mdct_stereo( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
423 : }
424 :
425 : /*----------------------------------------------------------------*
426 : * DFT stereo: iDFT and resampling on both channels
427 : *----------------------------------------------------------------*/
428 :
429 1194185 : if ( hCPE->element_mode == IVAS_CPE_DFT )
430 : {
431 : int32_t internal_Fs;
432 :
433 110182 : 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 );
434 :
435 : /* iDFT at input sampling rate */
436 110182 : stereo_dft_enc_synthesize( hCPE->hStereoDft, sts[0]->input, 0, input_Fs, input_Fs, 0 );
437 :
438 : /* iDFT & resampling to 12.8kHz internal sampling rate */
439 110182 : stereo_dft_enc_synthesize( hCPE->hStereoDft, old_inp_12k8[0] + L_INP_MEM, 0, input_Fs, INT_FS_12k8, 0 );
440 :
441 : /* iDFT & resampling to 16kHz internal sampling rate for M channel */
442 110182 : if ( input_Fs == internal_Fs )
443 : {
444 14863 : 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 );
445 : }
446 : else
447 : {
448 95319 : stereo_dft_enc_synthesize( hCPE->hStereoDft, old_inp_16k[0] + L_INP_MEM, 0, input_Fs, internal_Fs, 0 );
449 : }
450 :
451 : /* DFT Stereo: iDFT of residual signal at 8kHz sampling rate */
452 110182 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
453 : {
454 23489 : mvr2r( sts[1]->old_inp_12k8, old_inp_12k8[1], L_INP_MEM );
455 23489 : stereo_dft_enc_synthesize( hCPE->hStereoDft, old_inp_12k8[1] + L_INP_MEM, 1, input_Fs, 8000, 0 );
456 :
457 : /* update old input signal buffer */
458 23489 : mvr2r( old_inp_12k8[1] + L_FRAME8k, sts[1]->old_inp_12k8, L_INP_MEM );
459 : }
460 :
461 : /* no iDFT at input sampling rate for Side channel -> reset the buffer */
462 110182 : set_zero( sts[1]->input, input_frame );
463 : }
464 :
465 : #ifdef DEBUG_MODE_INFO
466 : for ( n = 0; n < n_CoreChannels; n++ )
467 : {
468 : 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 ) );
469 : }
470 : dbgwrite( &hCPE->element_mode, sizeof( int16_t ), 1, input_frame, fname( debug_dir, "element_mode", 0, sts[0]->id_element, ENC ) );
471 : #endif
472 :
473 : /*----------------------------------------------------------------*
474 : * Front Pre-processing
475 : *----------------------------------------------------------------*/
476 :
477 3472373 : for ( n = 0; n < n_CoreChannels; n++ )
478 : {
479 : #ifdef DEBUGGING
480 : st_ivas->noClipping += check_clipping( hCPE->hCoreCoder[n]->input, input_frame, &st_ivas->maxOverload, &st_ivas->minOverload );
481 :
482 : #endif
483 1064052 : error = pre_proc_front_ivas( NULL, hCPE, hCPE->element_brate, nb_bits_metadata, input_frame, n, old_inp_12k8[n], old_inp_16k[n],
484 2278188 : &ener[n], &relE[n], A[n], Aw[n], epsP[n], lsp_new[n], lsp_mid[n], &vad_hover_flag[n], &attack_flag[n],
485 2278188 : 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],
486 3342240 : 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 );
487 2278188 : if ( error != IVAS_ERR_OK )
488 : {
489 0 : return error;
490 : }
491 : }
492 :
493 : /* sanity check -> DTX not supported for more than one SCEs/CPEs */
494 1194185 : if ( st_ivas->nSCE + st_ivas->nCPE > 1 )
495 : {
496 954283 : if ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA )
497 : {
498 48 : sts[0]->core_brate = -1;
499 48 : sts[0]->total_brate = hCPE->element_brate;
500 : }
501 : }
502 :
503 : /*----------------------------------------------------------------*
504 : * Stereo DTX updates
505 : *----------------------------------------------------------------*/
506 :
507 1194185 : if ( ivas_format == MASA_FORMAT && nb_bits_metadata > 0 && hCPE->hCoreCoder[0]->Opt_DTX_ON )
508 : {
509 6266 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
510 : {
511 2878 : reset_metadata_spatial( ivas_format, hCPE->hMetaData, hCPE->element_brate, &tmp, sts[0]->core_brate, nb_bits_metadata );
512 : }
513 : }
514 :
515 : /* MDCT stereo DTX: active/inactive frame decision; compute FD CNG coherence */
516 1194185 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hEncoderConfig->Opt_DTX_ON )
517 : {
518 28468 : stereoFdCngCoherence( sts, hCPE->last_element_mode, fft_buff );
519 :
520 : /* Reset metadata */
521 28468 : if ( sts[0]->cng_sba_flag || ( ivas_format == SBA_FORMAT ) )
522 : {
523 14338 : reset_metadata_spatial( ivas_format, hCPE->hMetaData, hCPE->element_brate, &tmp, sts[0]->core_brate, nb_bits_metadata );
524 : }
525 : }
526 :
527 : /*----------------------------------------------------------------*
528 : * Core codec configuration
529 : *----------------------------------------------------------------*/
530 :
531 : #ifdef FIX_1411_IGF_CRASH_BW_SWITCHING
532 1194185 : if ( !( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) ) /* Reconfigurations not needed with DTX*/
533 : {
534 : #endif
535 1178077 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->hMCT == NULL )
536 : {
537 : /* set coded BW for MDCT stereo */
538 186567 : set_bw_stereo( hCPE );
539 :
540 : /* reconfiguration of MDCT stereo */
541 186567 : 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 ) )
542 : {
543 779 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, sts[0]->bwidth, 0, NULL, 0 );
544 779 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
545 :
546 779 : if ( hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
547 : {
548 87 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
549 : {
550 0 : return error;
551 : }
552 : }
553 : }
554 : }
555 :
556 : /* IGF reconfiguration */
557 3435167 : for ( n = 0; n < n_CoreChannels; n++ )
558 : {
559 2257090 : 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
560 : #ifdef FIX_1411_IGF_CRASH_BW_SWITCHING
561 2245867 : || ( sts[n]->last_core_brate == SID_2k40 || sts[n]->last_core_brate == FRAME_NO_DATA )
562 : #endif
563 11926 : ) &&
564 4943 : ( n == 0 || hCPE->element_mode == IVAS_CPE_MDCT ) )
565 : {
566 : int16_t igf;
567 : int16_t bw;
568 :
569 11802 : bw = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? sts[n]->bwidth : max_bwidth;
570 11802 : igf = getIgfPresent( sts[n]->element_mode, sts[n]->bits_frame_nominal * FRAMES_PER_SEC, bw, sts[n]->rf_mode );
571 : #ifdef FIX_1413_IGF_INIT_PRINTOUT
572 11802 : 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 )
573 : #else
574 : if ( ( error = IGF_Reconfig( &sts[n]->hIGFEnc, igf, 0, sts[n]->bits_frame_nominal * FRAMES_PER_SEC, max_bwidth, sts[n]->element_mode, sts[n]->rf_mode ) ) != IVAS_ERR_OK )
575 : #endif
576 : {
577 0 : return error;
578 : }
579 : }
580 : }
581 : #ifdef FIX_1411_IGF_CRASH_BW_SWITCHING
582 : }
583 : #endif
584 :
585 : /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
586 3472373 : for ( n = 0; n < n_CoreChannels; n++ )
587 : {
588 2278188 : if ( ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) && hCPE->element_mode == IVAS_CPE_DFT )
589 : {
590 11118 : 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 );
591 : }
592 : else
593 : {
594 2267070 : 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 );
595 : }
596 : }
597 :
598 : /* configure TD stereo encoder */
599 1194185 : if ( hCPE->element_mode == IVAS_CPE_TD )
600 : {
601 4138 : tdm_ol_pitch_comparison( hCPE, pitch_fr, voicing_fr );
602 :
603 4138 : 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 );
604 :
605 4138 : if ( hEncoderConfig->Opt_DTX_ON )
606 : {
607 1800 : stereo_cng_upd_counters( hCPE->hStereoCng, hCPE->element_mode, -1, NULL, sts[0]->hTdCngEnc->burst_ho_cnt, NULL );
608 : }
609 : }
610 :
611 : /* modify the coder_type depending on the total_brate per channel */
612 3472373 : for ( n = 0; n < n_CoreChannels; n++ )
613 : {
614 2278188 : if ( ( hCPE->element_mode != IVAS_CPE_DFT && hCPE->element_mode != IVAS_CPE_TD ) || n == 0 ) /* modify coder_type of primary channel */
615 : {
616 : /* limit coder_type depending on the bitrate */
617 2274050 : coder_type_modif( sts[n], relE[n] );
618 : }
619 : }
620 :
621 : /*----------------------------------------------------------------*
622 : * Write IVAS format signaling in SID frames
623 : *----------------------------------------------------------------*/
624 :
625 1194185 : if ( sts[0]->core_brate == SID_2k40 )
626 : {
627 2338 : ivas_write_format_sid( ivas_format, hCPE->element_mode, sts[0]->hBstr, hEncoderConfig->sba_order, hEncoderConfig->sba_planar );
628 : }
629 :
630 : /*----------------------------------------------------------------*
631 : * DFT Stereo residual coding
632 : * DFT Stereo parameters writing into the bitstream
633 : *----------------------------------------------------------------*/
634 :
635 1194185 : cpe_brate = 0;
636 1194185 : if ( hCPE->element_mode == IVAS_CPE_DFT )
637 : {
638 110182 : if ( hEncoderConfig->Opt_DTX_ON )
639 : {
640 34018 : if ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA )
641 : {
642 : /* Reconfigure DFT Stereo for inactive frames */
643 11118 : if ( sts[0]->core_brate == SID_2k40 )
644 : {
645 1636 : stereo_dft_config( hConfigDft, IVAS_SID_5k2, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
646 : }
647 : else
648 : {
649 9482 : stereo_dft_config( hConfigDft, FRAME_NO_DATA, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
650 : }
651 :
652 11118 : stereo_dft_cng_side_gain( hCPE->hStereoDft, hCPE->hStereoCng, sts[0]->core_brate, sts[0]->last_core_brate, sts[0]->bwidth );
653 : #ifdef DEBUG_MODE_DFT
654 : hCPE->hStereoDft->res_cod_bits = 0;
655 : #endif
656 : }
657 : else
658 : {
659 22900 : 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 );
660 : }
661 : }
662 :
663 : /* Write stereo bitstream */
664 110182 : cpe_brate = st_ivas->hCPE[0]->element_brate;
665 :
666 : /* DFT stereo side bits */
667 110182 : 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 )
668 : {
669 39272 : nb_bits = 0; /* Only mono downmix is transmitted in this case */
670 : }
671 70910 : else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) )
672 : {
673 530 : nb_bits = hCPE->hMetaData->nb_bits_tot;
674 : }
675 : else
676 : {
677 70380 : stereo_dft_enc_write_BS( hCPE, &nb_bits );
678 : }
679 :
680 : /* Residual coding in MDCT domain */
681 110182 : if ( !( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) ) )
682 : {
683 109652 : int16_t max_bits = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC - 0.8f * sts[0]->bits_frame_nominal );
684 109652 : if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
685 : {
686 55784 : max_bits -= nb_bits_metadata;
687 55784 : if ( hCPE->brate_surplus < 0 )
688 : {
689 18887 : max_bits += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
690 : }
691 : }
692 :
693 109652 : stereo_dft_enc_res( hCPE->hStereoDft, old_inp_12k8[1] + L_INP_MEM - STEREO_DFT_OVL_8k, hCPE->hMetaData, &nb_bits, max_bits );
694 : }
695 :
696 110182 : if ( sts[0]->core_brate == FRAME_NO_DATA || sts[0]->core_brate == SID_2k40 )
697 : {
698 11118 : assert( ( nb_bits <= ( ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS ) ) && "Stereo DFT CNG: bit budget is violated" );
699 : }
700 : else
701 : {
702 : #ifdef DEBUGGING
703 : if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
704 : {
705 : 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" );
706 : }
707 : else
708 : {
709 : assert( ( ( hCPE->element_brate / FRAMES_PER_SEC - nb_bits ) >= ( 0.8f * sts[0]->bits_frame_nominal ) ) && "Stereo DFT: bit budget is violated" );
710 : }
711 :
712 : #endif
713 : /* Flexible total bitrate in M channel */
714 99064 : sts[0]->total_brate = hCPE->element_brate - ( nb_bits * FRAMES_PER_SEC );
715 : }
716 :
717 : /* subtract metadata bitbudget */
718 110182 : sts[0]->total_brate -= ( nb_bits_metadata * FRAMES_PER_SEC );
719 :
720 : /* subtract bit-rate for combined format coding */
721 110182 : 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 ) )
722 : {
723 20856 : sts[0]->total_brate += hCPE->brate_surplus;
724 : }
725 : }
726 :
727 :
728 : /*----------------------------------------------------------------*
729 : * Core Encoder
730 : *----------------------------------------------------------------*/
731 :
732 1194185 : 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 )
733 : {
734 0 : return error;
735 : }
736 :
737 : /*----------------------------------------------------------------*
738 : * Common updates
739 : *----------------------------------------------------------------*/
740 :
741 1194185 : hCPE->last_element_brate = hCPE->element_brate;
742 1194185 : hCPE->last_element_mode = hCPE->element_mode;
743 :
744 1194185 : if ( ivas_format == MASA_ISM_FORMAT )
745 : {
746 84390 : hCPE->element_brate = element_brate_ref;
747 : }
748 :
749 1194185 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
750 : {
751 : /* update input samples buffer */
752 54135 : for ( n = 0; n < CPE_CHANNELS; n++ )
753 : {
754 36090 : mvr2r( orig_input[n], sts[n]->old_input_signal, input_frame );
755 : }
756 : }
757 1176140 : else if ( hCPE->element_mode == IVAS_CPE_DFT )
758 : {
759 110182 : mvr2r( sts[0]->input, sts[0]->old_input_signal, input_frame );
760 : }
761 1065958 : else if ( st_ivas->hMCT == NULL ) /* note: in MCT, input buffers are updated later in ivas_mct_enc() */
762 : {
763 : /* update input samples buffer */
764 532950 : for ( n = 0; n < CPE_CHANNELS; n++ )
765 : {
766 355300 : mvr2r( sts[n]->input, sts[n]->old_input_signal, input_frame );
767 : }
768 : }
769 :
770 1194185 : if ( hCPE->hFrontVad[0] != NULL )
771 : {
772 35818 : hCPE->hFrontVad[0]->ini_frame++;
773 35818 : hCPE->hFrontVad[0]->ini_frame = min( hCPE->hFrontVad[0]->ini_frame, MAX_FRAME_COUNTER );
774 : }
775 :
776 : /* Store previous attack detection flag */
777 3582555 : for ( n = 0; n < CPE_CHANNELS; n++ )
778 : {
779 2388370 : sts[n]->hTranDet->transientDetector.prev_bIsAttackPresent = sts[n]->hTranDet->transientDetector.bIsAttackPresent;
780 : }
781 :
782 : #ifdef DEBUG_MODE_INFO
783 : if ( hCPE->element_mode == IVAS_CPE_DFT )
784 : {
785 : n = (int16_t) hCPE->hStereoDft->hItd->itd[1];
786 : dbgwrite( &n, 2, 1, input_frame, "res/itd" );
787 : n = 0;
788 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_refChan" );
789 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_NCShift" );
790 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_ica_gD" );
791 : n = -1;
792 : // dbgwrite( &n, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
793 : }
794 : else if ( hCPE->element_mode == IVAS_CPE_TD )
795 : {
796 : dbgwrite( &hCPE->hStereoTCA->refChanIndx, 2, 1, input_frame, "res/TCA_idx_refChan" );
797 : n = hCPE->hStereoTCA->refChanIndx == 0 ? hCPE->hStereoTCA->indx_ica_NCShift : -hCPE->hStereoTCA->indx_ica_NCShift;
798 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_NCShift" );
799 : dbgwrite( &hCPE->hStereoTCA->indx_ica_gD, 2, 1, input_frame, "res/TCA_idx_ica_gD" );
800 :
801 : n = hCPE->hStereoTCA->corrLagStats[2];
802 : dbgwrite( &n, 2, 1, input_frame, "res/itd" );
803 :
804 : dbgwrite( &tdm_ratio_idx, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
805 : }
806 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
807 : {
808 : n = -2;
809 : // dbgwrite( &n, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
810 : }
811 :
812 : {
813 : float tmpF = ivas_total_brate / 1000.0f;
814 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, cpe_id, ENC ) );
815 : }
816 : #endif
817 :
818 1194185 : pop_wmops();
819 1194185 : return error;
820 : }
821 :
822 :
823 : /*-------------------------------------------------------------------------
824 : * create_cpe_enc()
825 : *
826 : * Create, allocate and initialize IVAS encoder CPE handle
827 : *-------------------------------------------------------------------------*/
828 :
829 10702 : ivas_error create_cpe_enc(
830 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
831 : const int16_t cpe_id, /* i : CPE # identifier */
832 : const int32_t element_brate /* i : element bitrate */
833 : )
834 : {
835 : int16_t n;
836 : int16_t ivas_format, element_mode_init, max_bwidth;
837 : int32_t input_Fs;
838 : CPE_ENC_HANDLE hCPE;
839 : Encoder_State *st;
840 : ENCODER_CONFIG_HANDLE hEncoderConfig;
841 : ivas_error error;
842 :
843 10702 : error = IVAS_ERR_OK;
844 :
845 10702 : hEncoderConfig = st_ivas->hEncoderConfig;
846 :
847 10702 : ivas_format = hEncoderConfig->ivas_format;
848 10702 : element_mode_init = hEncoderConfig->element_mode_init;
849 10702 : input_Fs = hEncoderConfig->input_Fs;
850 10702 : max_bwidth = hEncoderConfig->max_bwidth;
851 :
852 : /*-----------------------------------------------------------------*
853 : * Allocate CPE handle
854 : *-----------------------------------------------------------------*/
855 :
856 10702 : if ( ( hCPE = (CPE_ENC_HANDLE) malloc( sizeof( CPE_ENC_DATA ) ) ) == NULL )
857 : {
858 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
859 : }
860 :
861 : /*-----------------------------------------------------------------*
862 : * Initialization - general parameters
863 : *-----------------------------------------------------------------*/
864 :
865 10702 : hCPE->cpe_id = cpe_id;
866 10702 : hCPE->element_brate = element_brate;
867 10702 : hCPE->last_element_brate = hCPE->element_brate;
868 10702 : hCPE->element_mode = element_mode_init;
869 10702 : hCPE->last_element_mode = element_mode_init;
870 :
871 10702 : hCPE->hStereoDft = NULL;
872 10702 : hCPE->hStereoTD = NULL;
873 10702 : hCPE->hStereoMdct = NULL;
874 10702 : hCPE->hStereoTCA = NULL;
875 10702 : hCPE->hStereoICBWE = NULL;
876 10702 : hCPE->hMetaData = NULL;
877 :
878 10702 : hCPE->hStereoCng = NULL;
879 10702 : hCPE->hFrontVad[0] = NULL;
880 10702 : hCPE->hFrontVad[1] = NULL;
881 :
882 10702 : hCPE->brate_surplus = 0;
883 :
884 : /*-----------------------------------------------------------------*
885 : * Input memory buffer: allocate and initialize
886 : *-----------------------------------------------------------------*/
887 :
888 32106 : for ( n = 0; n < CPE_CHANNELS; n++ )
889 : {
890 21404 : 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 )
891 : {
892 2848 : if ( ( hCPE->input_mem[n] = (float *) malloc( sizeof( float ) * NS2SA( input_Fs, STEREO_DFT_OVL_NS ) ) ) == NULL )
893 : {
894 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
895 : }
896 :
897 2848 : set_zero( hCPE->input_mem[n], NS2SA( input_Fs, STEREO_DFT_OVL_NS ) );
898 : }
899 : else
900 : {
901 18556 : hCPE->input_mem[n] = NULL;
902 : }
903 : }
904 :
905 : /*-----------------------------------------------------------------*
906 : * stereo classifier: allocate and initialize
907 : *-----------------------------------------------------------------*/
908 :
909 10702 : if ( ( hCPE->hStereoClassif = (STEREO_CLASSIF_HANDLE) malloc( sizeof( STEREO_CLASSIF_DATA ) ) ) == NULL )
910 : {
911 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo classifier structure\n" ) );
912 : }
913 :
914 10702 : stereo_classifier_init( hCPE->hStereoClassif );
915 :
916 : /*-----------------------------------------------------------------*
917 : * Metadata: allocate and initialize
918 : *-----------------------------------------------------------------*/
919 :
920 10702 : if ( cpe_id == ( st_ivas->nCPE - 1 ) )
921 : {
922 5222 : if ( ( error = ivas_initialize_MD_bstr_enc( &( hCPE->hMetaData ), st_ivas ) ) != IVAS_ERR_OK )
923 : {
924 0 : return error;
925 : }
926 : }
927 :
928 : /*-----------------------------------------------------------------*
929 : * CoreCoder, 2 instances: allocate and initialize
930 : *-----------------------------------------------------------------*/
931 :
932 32106 : for ( n = 0; n < CPE_CHANNELS; n++ )
933 : {
934 21404 : if ( ( st = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
935 : {
936 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
937 : }
938 :
939 21404 : copy_encoder_config( st_ivas, st, 1 );
940 :
941 21404 : 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() */
942 21404 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
943 :
944 21404 : if ( ( error = init_encoder( st, st_ivas, n, 0, ISM_MODE_NONE, hCPE->element_brate ) ) != IVAS_ERR_OK )
945 : {
946 0 : return error;
947 : }
948 :
949 21404 : if ( st->hFdCngEnc != NULL )
950 : {
951 508 : st->hFdCngEnc->hFdCngCom->CngBitrate = hCPE->element_brate - 1;
952 : }
953 :
954 21404 : hCPE->hCoreCoder[n] = st;
955 : }
956 :
957 : /*-----------------------------------------------------------------*
958 : * LR VAD initialization
959 : *-----------------------------------------------------------------*/
960 :
961 10702 : if ( hEncoderConfig->Opt_DTX_ON )
962 : {
963 283 : if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT )
964 : {
965 58 : if ( ( hCPE->hStereoCng = (STEREO_CNG_ENC_HANDLE) malloc( sizeof( STEREO_CNG_ENC ) ) ) == NULL )
966 : {
967 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo Cng for Unified/TD \n" ) );
968 : }
969 :
970 58 : stereo_enc_cng_init( hCPE->hStereoCng );
971 : }
972 : }
973 : else
974 : {
975 10419 : hCPE->hStereoCng = NULL;
976 : }
977 :
978 : #ifdef DEBUGGING
979 : 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 ) )
980 : #else
981 10702 : if ( hEncoderConfig->Opt_DTX_ON && element_mode_init != IVAS_CPE_MDCT )
982 : #endif
983 : {
984 174 : for ( n = 0; n < CPE_CHANNELS; n++ )
985 : {
986 116 : if ( ( error = front_vad_create( &( hCPE->hFrontVad[n] ), hEncoderConfig ) ) != IVAS_ERR_OK )
987 : {
988 0 : return error;
989 : }
990 : }
991 : }
992 : else
993 : {
994 31932 : for ( n = 0; n < CPE_CHANNELS; n++ )
995 : {
996 21288 : hCPE->hFrontVad[n] = NULL;
997 : }
998 : }
999 :
1000 : /*-----------------------------------------------------------------*
1001 : * DFT stereo initialization
1002 : *-----------------------------------------------------------------*/
1003 :
1004 10702 : if ( hCPE->element_mode == IVAS_CPE_DFT )
1005 : {
1006 663 : if ( ( error = stereo_dft_enc_create( &( hCPE->hStereoDft ), input_Fs, max_bwidth ) ) != IVAS_ERR_OK )
1007 : {
1008 0 : return error;
1009 : }
1010 : }
1011 :
1012 : /*-----------------------------------------------------------------*
1013 : * Temporal inter-channel alignment initialization
1014 : *-----------------------------------------------------------------*/
1015 :
1016 10702 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1017 : {
1018 663 : if ( ( hCPE->hStereoTCA = (STEREO_TCA_ENC_HANDLE) malloc( sizeof( STEREO_TCA_ENC_DATA ) ) ) == NULL )
1019 : {
1020 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
1021 : }
1022 :
1023 663 : stereo_tca_init_enc( hCPE->hStereoTCA, input_Fs );
1024 : }
1025 :
1026 : /*-----------------------------------------------------------------*
1027 : * Stereo IC BWE initialization
1028 : *-----------------------------------------------------------------*/
1029 :
1030 10702 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1031 : {
1032 663 : if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_ENC_HANDLE) malloc( sizeof( STEREO_ICBWE_ENC_DATA ) ) ) == NULL )
1033 : {
1034 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
1035 : }
1036 :
1037 663 : stereo_icBWE_init_enc( hCPE->hStereoICBWE );
1038 : }
1039 :
1040 : /*-----------------------------------------------------------------*
1041 : * TD stereo initialization
1042 : *-----------------------------------------------------------------*/
1043 :
1044 10702 : if ( hCPE->element_mode == IVAS_CPE_TD )
1045 : {
1046 0 : if ( ( hCPE->hStereoTD = (STEREO_TD_ENC_DATA_HANDLE) malloc( sizeof( STEREO_TD_ENC_DATA ) ) ) == NULL )
1047 : {
1048 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
1049 : }
1050 :
1051 0 : stereo_td_init_enc( hCPE->hStereoTD, hCPE->last_element_mode );
1052 : }
1053 :
1054 : /*-----------------------------------------------------------------*
1055 : * MDCT stereo initialization
1056 : *-----------------------------------------------------------------*/
1057 :
1058 10702 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->nCPE == 1 )
1059 : {
1060 1589 : if ( ( hCPE->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
1061 : {
1062 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
1063 : }
1064 :
1065 : #ifdef DEBUGGING
1066 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = st_ivas->hEncoderConfig->mdct_stereo_mode_cmdl;
1067 : #endif
1068 1589 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 1 );
1069 1589 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
1070 :
1071 1589 : if ( hCPE->element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
1072 : {
1073 33 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
1074 : {
1075 0 : return error;
1076 : }
1077 : }
1078 : }
1079 :
1080 10702 : st_ivas->hCPE[cpe_id] = hCPE;
1081 :
1082 10702 : return error;
1083 : }
1084 :
1085 :
1086 : /*-------------------------------------------------------------------------
1087 : * destroy_cpe_enc()
1088 : *
1089 : * Destroy and deallocate IVAS encoder CPE handle
1090 : *-------------------------------------------------------------------------*/
1091 :
1092 10702 : void destroy_cpe_enc(
1093 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structure */
1094 : )
1095 : {
1096 : int16_t n;
1097 : Encoder_State *st;
1098 :
1099 10702 : if ( hCPE->hStereoClassif != NULL )
1100 : {
1101 10702 : free( hCPE->hStereoClassif );
1102 10702 : hCPE->hStereoClassif = NULL;
1103 : }
1104 :
1105 32106 : for ( n = 0; n < CPE_CHANNELS; n++ )
1106 : {
1107 21404 : if ( hCPE->input_mem[n] != NULL )
1108 : {
1109 2848 : free( hCPE->input_mem[n] );
1110 2848 : hCPE->input_mem[n] = NULL;
1111 : }
1112 : }
1113 :
1114 10702 : ivas_destroy_MD_bstr_enc( &( hCPE->hMetaData ) );
1115 :
1116 32106 : for ( n = 0; n < CPE_CHANNELS; n++ )
1117 : {
1118 21404 : st = hCPE->hCoreCoder[n];
1119 :
1120 21404 : if ( st != NULL )
1121 : {
1122 21404 : destroy_core_enc( st );
1123 21404 : st = NULL;
1124 : }
1125 : }
1126 :
1127 10702 : if ( hCPE->hStereoDft != NULL )
1128 : {
1129 642 : stereo_dft_enc_destroy( &hCPE->hStereoDft );
1130 642 : hCPE->hStereoDft = NULL;
1131 : }
1132 :
1133 10702 : if ( hCPE->hStereoTD != NULL )
1134 : {
1135 3 : free( hCPE->hStereoTD );
1136 3 : hCPE->hStereoTD = NULL;
1137 : }
1138 :
1139 10702 : if ( hCPE->hStereoMdct != NULL )
1140 : {
1141 1616 : stereo_mdct_enc_destroy( &hCPE->hStereoMdct );
1142 1616 : hCPE->hStereoMdct = NULL;
1143 : }
1144 :
1145 10702 : if ( hCPE->hStereoTCA != NULL )
1146 : {
1147 645 : free( hCPE->hStereoTCA );
1148 645 : hCPE->hStereoTCA = NULL;
1149 : }
1150 :
1151 10702 : if ( hCPE->hStereoICBWE != NULL )
1152 : {
1153 643 : free( hCPE->hStereoICBWE );
1154 643 : hCPE->hStereoICBWE = NULL;
1155 : }
1156 :
1157 10702 : if ( hCPE->hStereoCng != NULL )
1158 : {
1159 118 : free( hCPE->hStereoCng );
1160 118 : hCPE->hStereoCng = NULL;
1161 : }
1162 :
1163 10702 : if ( hCPE->hFrontVad[0] != NULL )
1164 : {
1165 162 : for ( n = 0; n < CPE_CHANNELS; n++ )
1166 : {
1167 108 : front_vad_destroy( &hCPE->hFrontVad[n] );
1168 108 : hCPE->hFrontVad[n] = NULL;
1169 : }
1170 : }
1171 :
1172 10702 : free( hCPE );
1173 :
1174 10702 : return;
1175 : }
1176 :
1177 :
1178 : /*-------------------------------------------------------------------------
1179 : * stereo_mode_combined_format_enc()
1180 : *
1181 : * Set stereo format in a combined format
1182 : *-------------------------------------------------------------------------*/
1183 :
1184 1194185 : static void stereo_mode_combined_format_enc(
1185 : const Encoder_Struct *st_ivas, /* i : encoder main structure */
1186 : CPE_ENC_HANDLE hCPE /* i/o: CPE handle */
1187 : )
1188 : {
1189 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1190 : int32_t element_brate_ref;
1191 :
1192 1194185 : hEncoderConfig = st_ivas->hEncoderConfig;
1193 :
1194 1194185 : if ( hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
1195 : {
1196 84390 : element_brate_ref = hCPE->element_brate;
1197 :
1198 84390 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC &&
1199 42482 : ( ( hEncoderConfig->nchan_ism == 3 && hEncoderConfig->ivas_total_brate == IVAS_96k ) ||
1200 41816 : ( hEncoderConfig->nchan_ism == 4 && hEncoderConfig->ivas_total_brate == IVAS_128k ) ) )
1201 : {
1202 1032 : if ( hCPE->element_brate + hCPE->brate_surplus > IVAS_64k )
1203 : {
1204 0 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = 0;
1205 : }
1206 : else
1207 : {
1208 1032 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt++;
1209 1032 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = min( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, OMASA_STEREO_SW_CNT_MAX );
1210 : }
1211 :
1212 1032 : if ( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt < OMASA_STEREO_SW_CNT_MAX )
1213 : {
1214 0 : hCPE->element_mode = IVAS_CPE_MDCT;
1215 0 : hCPE->element_brate = IVAS_64k;
1216 0 : hCPE->brate_surplus -= ( hCPE->element_brate - element_brate_ref );
1217 : }
1218 :
1219 : /* write OMASA stereo mode signalling */
1220 1032 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
1221 : {
1222 0 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 1, NBITS_ELEMENT_MODE );
1223 : }
1224 : else
1225 : {
1226 1032 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 0, NBITS_ELEMENT_MODE );
1227 : }
1228 : }
1229 : }
1230 :
1231 1194185 : return;
1232 : }
|