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 309015 : 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 309015 : error = IVAS_ERR_OK;
118 309015 : push_wmops( "ivas_cpe_enc" );
119 :
120 309015 : hCPE = st_ivas->hCPE[cpe_id];
121 309015 : sts = hCPE->hCoreCoder;
122 309015 : hEncoderConfig = st_ivas->hEncoderConfig;
123 309015 : max_bwidth = hEncoderConfig->max_bwidth;
124 309015 : ivas_format = hEncoderConfig->ivas_format;
125 309015 : input_Fs = hEncoderConfig->input_Fs;
126 309015 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
127 309015 : element_brate_ref = hCPE->element_brate;
128 309015 : last_bits_frame_nominal = sts[0]->bits_frame_nominal;
129 :
130 : /*------------------------------------------------------------------*
131 : * Initialization - general
132 : *-----------------------------------------------------------------*/
133 :
134 309015 : tdm_SM_or_LRTD_Pri = 0;
135 309015 : tdm_ratio_idx = -1;
136 309015 : tdm_ratio_idx_SM = -1;
137 309015 : 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 927045 : for ( n = 0; n < CPE_CHANNELS; n++ )
151 : {
152 618030 : sts[n]->idchan = n;
153 618030 : sts[n]->core = -1;
154 618030 : sts[n]->core_brate = -1; /* updated in dtx() */
155 618030 : sts[n]->max_bwidth = max_bwidth;
156 618030 : if ( st_ivas->hMCT == NULL ) /*already updated before CPE call*/
157 : {
158 245154 : sts[n]->input_bwidth = sts[n]->last_input_bwidth; /* updated in BWD */
159 245154 : sts[n]->bwidth = sts[n]->last_bwidth; /* updated in BWD */
160 : }
161 618030 : 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 309015 : mvr2r( data_f_ch0, sts[0]->input, input_frame );
169 309015 : if ( data_f_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
170 : {
171 275436 : 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 309015 : if ( sts[0]->ini_frame > 0 && st_ivas->hMCT == NULL )
180 : {
181 122257 : hCPE->element_mode = select_stereo_mode( hCPE, ivas_format );
182 : }
183 :
184 309015 : stereo_mode_combined_format_enc( st_ivas, hCPE );
185 :
186 309015 : 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 309015 : sts[0]->element_mode = hCPE->element_mode;
192 309015 : sts[1]->element_mode = hCPE->element_mode;
193 :
194 309015 : n_CoreChannels = 2;
195 309015 : if ( hCPE->element_mode == IVAS_CPE_DFT )
196 : {
197 50352 : n_CoreChannels = 1; /* in DFT stereo, only M channel is coded */
198 :
199 50352 : sts[1]->vad_flag = 0;
200 : }
201 :
202 : /*----------------------------------------------------------------*
203 : * dynamically allocate data structures depending on the actual stereo mode
204 : *----------------------------------------------------------------*/
205 :
206 309015 : 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 309015 : hConfigDft = NULL;
212 309015 : if ( hCPE->hStereoDft != NULL )
213 : {
214 50352 : hConfigDft = hCPE->hStereoDft->hConfig;
215 : }
216 :
217 : /*----------------------------------------------------------------*
218 : * Set TD stereo parameters
219 : *----------------------------------------------------------------*/
220 :
221 309015 : 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 309015 : stereo_switching_enc( hCPE, sts[0]->old_input_signal, input_frame );
231 :
232 : /*----------------------------------------------------------------*
233 : * Temporal inter-channel alignment, stereo adjustment
234 : *----------------------------------------------------------------*/
235 :
236 309015 : 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 927045 : for ( n = 0; n < CPE_CHANNELS; n++ )
243 : {
244 618030 : mvr2r( sts[n]->input, orig_input[n], input_frame );
245 :
246 618030 : if ( hCPE->hStereoICBWE != NULL )
247 : {
248 100920 : hCPE->hStereoICBWE->dataChan[n] = &orig_input[n][0];
249 : }
250 : }
251 :
252 : /*---------------------------------------------------------------*
253 : * Time Domain Transient Detector
254 : *---------------------------------------------------------------*/
255 :
256 927045 : for ( n = 0; n < CPE_CHANNELS; n++ )
257 : {
258 618030 : if ( sts[n]->hTranDet == NULL )
259 : {
260 0 : currFlatness[n] = 0;
261 0 : continue;
262 : }
263 :
264 618030 : if ( !( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC ) )
265 : {
266 608150 : RunTransientDetection( sts[n]->input, input_frame, sts[n]->hTranDet );
267 : }
268 618030 : currFlatness[n] = GetTCXAvgTemporalFlatnessMeasure( sts[n]->hTranDet, NSUBBLOCKS, 0 );
269 : }
270 :
271 : /* Synchonize detection for downmix-based stereo */
272 309015 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
273 : {
274 52414 : set_transient_stereo( hCPE, currFlatness );
275 : }
276 :
277 : /*----------------------------------------------------------------*
278 : * Configuration of stereo encoder
279 : *----------------------------------------------------------------*/
280 :
281 876693 : for ( n = 0; n < n_CoreChannels; n++ )
282 : {
283 : /* Force to MODE1 in IVAS */
284 567678 : sts[n]->codec_mode = MODE1;
285 :
286 567678 : sts[n]->element_mode = hCPE->element_mode;
287 : }
288 :
289 :
290 309015 : 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 49036 : ( 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 8365 : if ( st_ivas->hQMetaData != NULL )
294 : {
295 3658 : if ( ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode != ISM_MODE_NONE )
296 : {
297 1536 : 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 2122 : 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 4707 : stereo_dft_config( hConfigDft, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
308 : }
309 : }
310 :
311 309015 : if ( hCPE->element_mode == IVAS_CPE_TD )
312 : {
313 2062 : if ( hCPE->hStereoTD->tdm_LRTD_flag )
314 : {
315 1954 : sts[0]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
316 1954 : sts[1]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
317 : }
318 : else
319 : {
320 108 : stereo_dft_config( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
321 : }
322 : }
323 :
324 309015 : 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 256601 : 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 14160 : brate_surplus[0] = ( ( hCPE->brate_surplus / FRAMES_PER_SEC ) >> 1 ) * FRAMES_PER_SEC;
331 14160 : 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 769803 : for ( n = 0; n < n_CoreChannels; n++ )
336 : {
337 513202 : if ( st_ivas->hMCT )
338 : {
339 : int16_t lfe_bits;
340 372876 : lfe_bits = ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT ? st_ivas->hLFE->lfe_bits : 0 );
341 372876 : sts[n]->total_brate = hCPE->element_brate;
342 372876 : sts[n]->bits_frame_nominal = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
343 372876 : 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 140326 : sts[n]->bits_frame_nominal = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
348 140326 : sts[n]->bits_frame_channel = (int16_t) ( ( hCPE->element_brate / FRAMES_PER_SEC ) / n_CoreChannels );
349 140326 : sts[n]->total_brate = hCPE->element_brate / n_CoreChannels;
350 :
351 : /* subtract bit-rate for combined format coding */
352 140326 : 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 28320 : sts[n]->bits_frame_channel += (int16_t) ( brate_surplus[n] / FRAMES_PER_SEC );
355 28320 : sts[n]->total_brate += brate_surplus[n];
356 : }
357 : }
358 : }
359 :
360 : /* reconfiguration in case of bitrate switching */
361 256601 : 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 2804 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 0 );
367 2804 : 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 309015 : if ( hCPE->element_mode == IVAS_CPE_DFT )
377 : {
378 50352 : 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 50352 : 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 50352 : stereo_dft_enc_analyze( sts, CPE_CHANNELS, input_frame, hCPE->hStereoDft, NULL, hCPE->hStereoDft->DFT, hCPE->input_mem );
389 :
390 50352 : 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 50352 : stereo_dft_enc_update( hCPE->hStereoDft, max_bwidth );
397 :
398 : /* DFT stereo processing */
399 50352 : stereo_dft_enc_process( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
400 : }
401 258663 : else if ( hCPE->element_mode == IVAS_CPE_TD )
402 : {
403 : /* Determine the energy ratio between the 2 channels */
404 2062 : 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 2062 : 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 2062 : sts[0]->bits_frame_channel = 0;
411 2062 : sts[1]->bits_frame_channel = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
412 2062 : sts[1]->bits_frame_channel += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
413 2062 : if ( st_ivas->hQMetaData != NULL )
414 : {
415 248 : sts[1]->bits_frame_channel -= st_ivas->hQMetaData->metadata_max_bits;
416 : }
417 :
418 2062 : Etot_last[0] = sts[0]->hNoiseEst->Etot_last;
419 2062 : Etot_last[1] = sts[1]->hNoiseEst->Etot_last;
420 : }
421 256601 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
422 : {
423 256601 : 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 309015 : if ( hCPE->element_mode == IVAS_CPE_DFT )
431 : {
432 : int32_t internal_Fs;
433 :
434 50352 : 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 50352 : 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 50352 : 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 50352 : if ( input_Fs == internal_Fs )
444 : {
445 8519 : 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 41833 : 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 50352 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
454 : {
455 11427 : mvr2r( sts[1]->old_inp_12k8, old_inp_12k8[1], L_INP_MEM );
456 11427 : 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 11427 : 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 50352 : 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 876693 : 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 220382 : pre_proc_front_ivas( NULL, hCPE, hCPE->element_brate, nb_bits_metadata, input_frame, n, old_inp_12k8[n], old_inp_16k[n],
485 567678 : &ener[n], &relE[n], A[n], Aw[n], epsP[n], lsp_new[n], lsp_mid[n], &vad_hover_flag[n], &attack_flag[n],
486 567678 : 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 788060 : 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 309015 : if ( st_ivas->nSCE + st_ivas->nCPE > 1 )
492 : {
493 212071 : if ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA )
494 : {
495 0 : sts[0]->core_brate = -1;
496 0 : sts[0]->total_brate = hCPE->element_brate;
497 : }
498 : }
499 :
500 : /*----------------------------------------------------------------*
501 : * Stereo DTX updates
502 : *----------------------------------------------------------------*/
503 :
504 309015 : if ( ivas_format == MASA_FORMAT && nb_bits_metadata > 0 && hCPE->hCoreCoder[0]->Opt_DTX_ON )
505 : {
506 4166 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
507 : {
508 2428 : 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 309015 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hEncoderConfig->Opt_DTX_ON )
514 : {
515 12398 : stereoFdCngCoherence( sts, hCPE->last_element_mode, fft_buff );
516 :
517 : /* Reset metadata */
518 12398 : if ( sts[0]->cng_sba_flag || ( ivas_format == SBA_FORMAT ) )
519 : {
520 6298 : 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 309015 : if ( !( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) ) /* Reconfigurations not needed with DTX*/
529 : {
530 302307 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->hMCT == NULL )
531 : {
532 : /* set coded BW for MDCT stereo */
533 68460 : set_bw_stereo( hCPE );
534 :
535 : /* reconfiguration of MDCT stereo */
536 68460 : 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 1647 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, sts[0]->bwidth, 0, NULL, 0 );
539 1647 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
540 :
541 1647 : if ( hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
542 : {
543 87 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
544 : {
545 0 : return error;
546 : }
547 : }
548 : }
549 : }
550 :
551 : /* IGF reconfiguration */
552 861574 : for ( n = 0; n < n_CoreChannels; n++ )
553 : {
554 559267 : 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 13296 : bw = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? sts[n]->bwidth : max_bwidth;
560 13296 : igf = getIgfPresent( sts[n]->element_mode, sts[n]->bits_frame_nominal * FRAMES_PER_SEC, bw, sts[n]->rf_mode );
561 :
562 13296 : 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 876693 : for ( n = 0; n < n_CoreChannels; n++ )
572 : {
573 567678 : if ( ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) && hCPE->element_mode == IVAS_CPE_DFT )
574 : {
575 5005 : 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 562673 : 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 309015 : if ( hCPE->element_mode == IVAS_CPE_TD )
585 : {
586 2062 : tdm_ol_pitch_comparison( hCPE, pitch_fr, voicing_fr );
587 :
588 2062 : 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 2062 : if ( hEncoderConfig->Opt_DTX_ON )
591 : {
592 826 : 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 876693 : for ( n = 0; n < n_CoreChannels; n++ )
598 : {
599 567678 : 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 565616 : coder_type_modif( sts[n], relE[n] );
603 : }
604 : }
605 :
606 : /*----------------------------------------------------------------*
607 : * Write IVAS format signaling in SID frames
608 : *----------------------------------------------------------------*/
609 :
610 309015 : if ( sts[0]->core_brate == SID_2k40 )
611 : {
612 973 : 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 309015 : cpe_brate = 0;
621 309015 : if ( hCPE->element_mode == IVAS_CPE_DFT )
622 : {
623 50352 : if ( hEncoderConfig->Opt_DTX_ON )
624 : {
625 16302 : 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 5005 : NFFT_inner = STEREO_DFT_N_MAX_ENC * inner_frame_tbl[sts[0]->bwidth] / L_FRAME48k;
630 5005 : hCPE->hStereoDft->nbands = stereo_dft_band_config( hCPE->hStereoDft->band_limits, hCPE->hStereoDft->hConfig->band_res, NFFT_inner, ENC );
631 5005 : if ( sts[0]->core_brate == SID_2k40 )
632 : {
633 723 : stereo_dft_config( hConfigDft, IVAS_SID_5k2, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
634 : }
635 : else
636 : {
637 4282 : stereo_dft_config( hConfigDft, FRAME_NO_DATA, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
638 : }
639 :
640 5005 : 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 11297 : 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 50352 : cpe_brate = st_ivas->hCPE[0]->element_brate;
653 :
654 : /* DFT stereo side bits */
655 50352 : 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 13532 : nb_bits = 0; /* Only mono downmix is transmitted in this case */
658 : }
659 36820 : 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 530 : nb_bits = hCPE->hMetaData->nb_bits_tot;
662 : }
663 : else
664 : {
665 36290 : stereo_dft_enc_write_BS( hCPE, &nb_bits );
666 : }
667 :
668 : /* Residual coding in MDCT domain */
669 50352 : 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 49822 : int16_t max_bits = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC - 0.8f * sts[0]->bits_frame_nominal );
672 49822 : if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
673 : {
674 24500 : max_bits -= nb_bits_metadata;
675 24500 : if ( hCPE->brate_surplus < 0 )
676 : {
677 8379 : max_bits += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
678 : }
679 : }
680 :
681 49822 : 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 50352 : if ( sts[0]->core_brate == FRAME_NO_DATA || sts[0]->core_brate == SID_2k40 )
685 : {
686 5005 : 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 45347 : sts[0]->total_brate = hCPE->element_brate - ( nb_bits * FRAMES_PER_SEC );
703 : }
704 :
705 : /* subtract metadata bitbudget */
706 50352 : sts[0]->total_brate -= ( nb_bits_metadata * FRAMES_PER_SEC );
707 :
708 : /* subtract bit-rate for combined format coding */
709 50352 : 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 10883 : sts[0]->total_brate += hCPE->brate_surplus;
712 : }
713 : }
714 :
715 :
716 : /*----------------------------------------------------------------*
717 : * Core Encoder
718 : *----------------------------------------------------------------*/
719 :
720 309015 : 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 309015 : hCPE->last_element_brate = hCPE->element_brate;
730 309015 : hCPE->last_element_mode = hCPE->element_mode;
731 :
732 309015 : if ( ivas_format == MASA_ISM_FORMAT )
733 : {
734 32350 : hCPE->element_brate = element_brate_ref;
735 : }
736 :
737 309015 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
738 : {
739 : /* update input samples buffer */
740 31500 : for ( n = 0; n < CPE_CHANNELS; n++ )
741 : {
742 21000 : mvr2r( orig_input[n], sts[n]->old_input_signal, input_frame );
743 : }
744 : }
745 298515 : else if ( hCPE->element_mode == IVAS_CPE_DFT )
746 : {
747 50352 : mvr2r( sts[0]->input, sts[0]->old_input_signal, input_frame );
748 : }
749 248163 : 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 185175 : for ( n = 0; n < CPE_CHANNELS; n++ )
753 : {
754 123450 : mvr2r( sts[n]->input, sts[n]->old_input_signal, input_frame );
755 : }
756 : }
757 :
758 309015 : if ( hCPE->hFrontVad[0] != NULL )
759 : {
760 17128 : hCPE->hFrontVad[0]->ini_frame++;
761 17128 : hCPE->hFrontVad[0]->ini_frame = min( hCPE->hFrontVad[0]->ini_frame, MAX_FRAME_COUNTER );
762 : }
763 :
764 : /* Store previous attack detection flag */
765 927045 : for ( n = 0; n < CPE_CHANNELS; n++ )
766 : {
767 618030 : 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 309015 : pop_wmops();
807 309015 : return error;
808 : }
809 :
810 :
811 : /*-------------------------------------------------------------------------
812 : * create_cpe_enc()
813 : *
814 : * Create, allocate and initialize IVAS encoder CPE handle
815 : *-------------------------------------------------------------------------*/
816 :
817 2206 : 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 2206 : error = IVAS_ERR_OK;
832 :
833 2206 : hEncoderConfig = st_ivas->hEncoderConfig;
834 :
835 2206 : ivas_format = hEncoderConfig->ivas_format;
836 2206 : element_mode_init = hEncoderConfig->element_mode_init;
837 2206 : input_Fs = hEncoderConfig->input_Fs;
838 2206 : max_bwidth = hEncoderConfig->max_bwidth;
839 :
840 : /*-----------------------------------------------------------------*
841 : * Allocate CPE handle
842 : *-----------------------------------------------------------------*/
843 :
844 2206 : 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 2206 : hCPE->cpe_id = cpe_id;
854 2206 : hCPE->element_brate = element_brate;
855 2206 : hCPE->last_element_brate = hCPE->element_brate;
856 2206 : hCPE->element_mode = element_mode_init;
857 2206 : hCPE->last_element_mode = element_mode_init;
858 :
859 2206 : hCPE->hStereoDft = NULL;
860 2206 : hCPE->hStereoTD = NULL;
861 2206 : hCPE->hStereoMdct = NULL;
862 2206 : hCPE->hStereoTCA = NULL;
863 2206 : hCPE->hStereoICBWE = NULL;
864 2206 : hCPE->hMetaData = NULL;
865 :
866 2206 : hCPE->hStereoCng = NULL;
867 2206 : hCPE->hFrontVad[0] = NULL;
868 2206 : hCPE->hFrontVad[1] = NULL;
869 :
870 2206 : hCPE->brate_surplus = 0;
871 :
872 : /*-----------------------------------------------------------------*
873 : * Input memory buffer: allocate and initialize
874 : *-----------------------------------------------------------------*/
875 :
876 6618 : for ( n = 0; n < CPE_CHANNELS; n++ )
877 : {
878 4412 : 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 232 : 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 232 : set_zero( hCPE->input_mem[n], NS2SA( input_Fs, STEREO_DFT_OVL_NS ) );
886 : }
887 : else
888 : {
889 4180 : hCPE->input_mem[n] = NULL;
890 : }
891 : }
892 :
893 : /*-----------------------------------------------------------------*
894 : * stereo classifier: allocate and initialize
895 : *-----------------------------------------------------------------*/
896 :
897 2206 : 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 2206 : stereo_classifier_init( hCPE->hStereoClassif );
903 :
904 : /*-----------------------------------------------------------------*
905 : * Metadata: allocate and initialize
906 : *-----------------------------------------------------------------*/
907 :
908 2206 : if ( cpe_id == ( st_ivas->nCPE - 1 ) )
909 : {
910 1087 : 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 6618 : for ( n = 0; n < CPE_CHANNELS; n++ )
921 : {
922 4412 : 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 4412 : copy_encoder_config( st_ivas, st, 1 );
928 :
929 4412 : 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 4412 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
931 :
932 4412 : 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 4412 : if ( st->hFdCngEnc != NULL )
938 : {
939 211 : st->hFdCngEnc->hFdCngCom->CngBitrate = hCPE->element_brate - 1;
940 : }
941 :
942 4412 : hCPE->hCoreCoder[n] = st;
943 : }
944 :
945 : /*-----------------------------------------------------------------*
946 : * LR VAD initialization
947 : *-----------------------------------------------------------------*/
948 :
949 2206 : if ( hEncoderConfig->Opt_DTX_ON )
950 : {
951 115 : if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT )
952 : {
953 19 : 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 19 : stereo_enc_cng_init( hCPE->hStereoCng );
959 : }
960 : }
961 : else
962 : {
963 2091 : 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 2206 : if ( hEncoderConfig->Opt_DTX_ON && element_mode_init != IVAS_CPE_MDCT )
970 : #endif
971 : {
972 57 : for ( n = 0; n < CPE_CHANNELS; n++ )
973 : {
974 38 : if ( ( error = front_vad_create( &( hCPE->hFrontVad[n] ), hEncoderConfig ) ) != IVAS_ERR_OK )
975 : {
976 0 : return error;
977 : }
978 : }
979 : }
980 : else
981 : {
982 6561 : for ( n = 0; n < CPE_CHANNELS; n++ )
983 : {
984 4374 : hCPE->hFrontVad[n] = NULL;
985 : }
986 : }
987 :
988 : /*-----------------------------------------------------------------*
989 : * DFT stereo initialization
990 : *-----------------------------------------------------------------*/
991 :
992 2206 : if ( hCPE->element_mode == IVAS_CPE_DFT )
993 : {
994 60 : 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 2206 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1005 : {
1006 60 : 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 60 : stereo_tca_init_enc( hCPE->hStereoTCA, input_Fs );
1012 : }
1013 :
1014 : /*-----------------------------------------------------------------*
1015 : * Stereo IC BWE initialization
1016 : *-----------------------------------------------------------------*/
1017 :
1018 2206 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1019 : {
1020 60 : 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 60 : stereo_icBWE_init_enc( hCPE->hStereoICBWE );
1026 : }
1027 :
1028 : /*-----------------------------------------------------------------*
1029 : * TD stereo initialization
1030 : *-----------------------------------------------------------------*/
1031 :
1032 2206 : 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 2206 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->nCPE == 1 )
1047 : {
1048 260 : 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 260 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 1 );
1057 260 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
1058 :
1059 260 : if ( hCPE->element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
1060 : {
1061 9 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
1062 : {
1063 0 : return error;
1064 : }
1065 : }
1066 : }
1067 :
1068 2206 : st_ivas->hCPE[cpe_id] = hCPE;
1069 :
1070 2206 : return error;
1071 : }
1072 :
1073 :
1074 : /*-------------------------------------------------------------------------
1075 : * destroy_cpe_enc()
1076 : *
1077 : * Destroy and deallocate IVAS encoder CPE handle
1078 : *-------------------------------------------------------------------------*/
1079 :
1080 2206 : 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 2206 : if ( hCPE->hStereoClassif != NULL )
1088 : {
1089 2206 : free( hCPE->hStereoClassif );
1090 2206 : hCPE->hStereoClassif = NULL;
1091 : }
1092 :
1093 6618 : for ( n = 0; n < CPE_CHANNELS; n++ )
1094 : {
1095 4412 : if ( hCPE->input_mem[n] != NULL )
1096 : {
1097 232 : free( hCPE->input_mem[n] );
1098 232 : hCPE->input_mem[n] = NULL;
1099 : }
1100 : }
1101 :
1102 2206 : ivas_destroy_MD_bstr_enc( &( hCPE->hMetaData ) );
1103 :
1104 6618 : for ( n = 0; n < CPE_CHANNELS; n++ )
1105 : {
1106 4412 : st = hCPE->hCoreCoder[n];
1107 :
1108 4412 : if ( st != NULL )
1109 : {
1110 4412 : destroy_core_enc( st );
1111 4412 : st = NULL;
1112 : }
1113 : }
1114 :
1115 2206 : if ( hCPE->hStereoDft != NULL )
1116 : {
1117 52 : stereo_dft_enc_destroy( &hCPE->hStereoDft );
1118 52 : hCPE->hStereoDft = NULL;
1119 : }
1120 :
1121 2206 : if ( hCPE->hStereoTD != NULL )
1122 : {
1123 1 : free( hCPE->hStereoTD );
1124 1 : hCPE->hStereoTD = NULL;
1125 : }
1126 :
1127 2206 : if ( hCPE->hStereoMdct != NULL )
1128 : {
1129 278 : stereo_mdct_enc_destroy( &hCPE->hStereoMdct );
1130 278 : hCPE->hStereoMdct = NULL;
1131 : }
1132 :
1133 2206 : if ( hCPE->hStereoTCA != NULL )
1134 : {
1135 53 : free( hCPE->hStereoTCA );
1136 53 : hCPE->hStereoTCA = NULL;
1137 : }
1138 :
1139 2206 : if ( hCPE->hStereoICBWE != NULL )
1140 : {
1141 53 : free( hCPE->hStereoICBWE );
1142 53 : hCPE->hStereoICBWE = NULL;
1143 : }
1144 :
1145 2206 : if ( hCPE->hStereoCng != NULL )
1146 : {
1147 32 : free( hCPE->hStereoCng );
1148 32 : hCPE->hStereoCng = NULL;
1149 : }
1150 :
1151 2206 : if ( hCPE->hFrontVad[0] != NULL )
1152 : {
1153 42 : for ( n = 0; n < CPE_CHANNELS; n++ )
1154 : {
1155 28 : front_vad_destroy( &hCPE->hFrontVad[n] );
1156 28 : hCPE->hFrontVad[n] = NULL;
1157 : }
1158 : }
1159 :
1160 2206 : free( hCPE );
1161 :
1162 2206 : return;
1163 : }
1164 :
1165 :
1166 : /*-------------------------------------------------------------------------
1167 : * stereo_mode_combined_format_enc()
1168 : *
1169 : * Set stereo format in a combined format
1170 : *-------------------------------------------------------------------------*/
1171 :
1172 309015 : 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 309015 : hEncoderConfig = st_ivas->hEncoderConfig;
1181 :
1182 309015 : if ( hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
1183 : {
1184 32350 : element_brate_ref = hCPE->element_brate;
1185 :
1186 32350 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC &&
1187 11592 : ( ( hEncoderConfig->nchan_ism == 3 && hEncoderConfig->ivas_total_brate == IVAS_96k ) ||
1188 10472 : ( hEncoderConfig->nchan_ism == 4 && hEncoderConfig->ivas_total_brate == IVAS_128k ) ) )
1189 : {
1190 1288 : 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 1288 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt++;
1197 1288 : 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 1288 : 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 1288 : 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 1288 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 0, NBITS_ELEMENT_MODE );
1215 : }
1216 : }
1217 : }
1218 :
1219 309015 : return;
1220 : }
|