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 420855 : 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 420855 : error = IVAS_ERR_OK;
117 420855 : push_wmops( "ivas_cpe_enc" );
118 :
119 420855 : hCPE = st_ivas->hCPE[cpe_id];
120 420855 : sts = hCPE->hCoreCoder;
121 420855 : hEncoderConfig = st_ivas->hEncoderConfig;
122 420855 : max_bwidth = hEncoderConfig->max_bwidth;
123 420855 : ivas_format = hEncoderConfig->ivas_format;
124 420855 : input_Fs = hEncoderConfig->input_Fs;
125 420855 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
126 420855 : element_brate_ref = hCPE->element_brate;
127 420855 : last_bits_frame_nominal = sts[0]->bits_frame_nominal;
128 :
129 : /*------------------------------------------------------------------*
130 : * Initialization - general
131 : *-----------------------------------------------------------------*/
132 :
133 420855 : tdm_SM_or_LRTD_Pri = 0;
134 420855 : tdm_ratio_idx = -1;
135 420855 : tdm_ratio_idx_SM = -1;
136 420855 : 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 1262565 : for ( n = 0; n < CPE_CHANNELS; n++ )
150 : {
151 841710 : sts[n]->idchan = n;
152 841710 : sts[n]->core = -1;
153 841710 : sts[n]->core_brate = -1; /* updated in dtx() */
154 841710 : sts[n]->max_bwidth = max_bwidth;
155 841710 : if ( st_ivas->hMCT == NULL ) /*already updated before CPE call*/
156 : {
157 301094 : sts[n]->input_bwidth = sts[n]->last_input_bwidth; /* updated in BWD */
158 301094 : sts[n]->bwidth = sts[n]->last_bwidth; /* updated in BWD */
159 : }
160 841710 : 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 420855 : mvr2r( data_f_ch0, sts[0]->input, input_frame );
168 420855 : if ( data_f_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
169 : {
170 362026 : 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 420855 : if ( sts[0]->ini_frame > 0 && st_ivas->hMCT == NULL )
179 : {
180 150083 : hCPE->element_mode = select_stereo_mode( hCPE, ivas_format );
181 : }
182 :
183 420855 : stereo_mode_combined_format_enc( st_ivas, hCPE );
184 :
185 420855 : 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 420855 : sts[0]->element_mode = hCPE->element_mode;
191 420855 : sts[1]->element_mode = hCPE->element_mode;
192 :
193 420855 : n_CoreChannels = 2;
194 420855 : if ( hCPE->element_mode == IVAS_CPE_DFT )
195 : {
196 59679 : n_CoreChannels = 1; /* in DFT stereo, only M channel is coded */
197 :
198 59679 : sts[1]->vad_flag = 0;
199 : }
200 :
201 : /*----------------------------------------------------------------*
202 : * dynamically allocate data structures depending on the actual stereo mode
203 : *----------------------------------------------------------------*/
204 :
205 420855 : 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 420855 : hConfigDft = NULL;
211 420855 : if ( hCPE->hStereoDft != NULL )
212 : {
213 59679 : hConfigDft = hCPE->hStereoDft->hConfig;
214 : }
215 :
216 : /*----------------------------------------------------------------*
217 : * Set TD stereo parameters
218 : *----------------------------------------------------------------*/
219 :
220 420855 : 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 420855 : stereo_switching_enc( hCPE, sts[0]->old_input_signal, input_frame );
230 :
231 : /*----------------------------------------------------------------*
232 : * Temporal inter-channel alignment, stereo adjustment
233 : *----------------------------------------------------------------*/
234 :
235 420855 : 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 1262565 : for ( n = 0; n < CPE_CHANNELS; n++ )
242 : {
243 841710 : mvr2r( sts[n]->input, orig_input[n], input_frame );
244 :
245 841710 : if ( hCPE->hStereoICBWE != NULL )
246 : {
247 119610 : hCPE->hStereoICBWE->dataChan[n] = &orig_input[n][0];
248 : }
249 : }
250 :
251 : /*---------------------------------------------------------------*
252 : * Time Domain Transient Detector
253 : *---------------------------------------------------------------*/
254 :
255 1262565 : for ( n = 0; n < CPE_CHANNELS; n++ )
256 : {
257 841710 : if ( sts[n]->hTranDet == NULL )
258 : {
259 0 : currFlatness[n] = 0;
260 0 : continue;
261 : }
262 :
263 841710 : if ( !( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC ) )
264 : {
265 820990 : RunTransientDetection( sts[n]->input, input_frame, sts[n]->hTranDet );
266 : }
267 841710 : currFlatness[n] = GetTCXAvgTemporalFlatnessMeasure( sts[n]->hTranDet, NSUBBLOCKS, 0 );
268 : }
269 :
270 : /* Synchonize detection for downmix-based stereo */
271 420855 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
272 : {
273 63470 : set_transient_stereo( hCPE, currFlatness );
274 : }
275 :
276 : /*----------------------------------------------------------------*
277 : * Configuration of stereo encoder
278 : *----------------------------------------------------------------*/
279 :
280 1202886 : for ( n = 0; n < n_CoreChannels; n++ )
281 : {
282 : /* Force to MODE1 in IVAS */
283 782031 : sts[n]->codec_mode = MODE1;
284 :
285 782031 : sts[n]->element_mode = hCPE->element_mode;
286 : }
287 :
288 :
289 420855 : 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 61996 : ( 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 11780 : if ( st_ivas->hQMetaData != NULL )
293 : {
294 1722 : if ( ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode != ISM_MODE_NONE )
295 : {
296 501 : 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 1221 : 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 10058 : stereo_dft_config( hConfigDft, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
307 : }
308 : }
309 :
310 420855 : if ( hCPE->element_mode == IVAS_CPE_TD )
311 : {
312 3791 : if ( hCPE->hStereoTD->tdm_LRTD_flag )
313 : {
314 3665 : sts[0]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
315 3665 : sts[1]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
316 : }
317 : else
318 : {
319 126 : stereo_dft_config( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
320 : }
321 : }
322 :
323 420855 : 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 357385 : 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 4274 : brate_surplus[0] = ( ( hCPE->brate_surplus / FRAMES_PER_SEC ) >> 1 ) * FRAMES_PER_SEC;
330 4274 : 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 1072155 : for ( n = 0; n < n_CoreChannels; n++ )
335 : {
336 714770 : if ( st_ivas->hMCT )
337 : {
338 : int16_t lfe_bits;
339 540616 : lfe_bits = ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT ? st_ivas->hLFE->lfe_bits : 0 );
340 540616 : sts[n]->total_brate = hCPE->element_brate;
341 540616 : sts[n]->bits_frame_nominal = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
342 540616 : 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 174154 : sts[n]->bits_frame_nominal = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
347 174154 : sts[n]->bits_frame_channel = (int16_t) ( ( hCPE->element_brate / FRAMES_PER_SEC ) / n_CoreChannels );
348 174154 : sts[n]->total_brate = hCPE->element_brate / n_CoreChannels;
349 :
350 : /* subtract bit-rate for combined format coding */
351 174154 : 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 8548 : sts[n]->bits_frame_channel += (int16_t) ( brate_surplus[n] / FRAMES_PER_SEC );
354 8548 : sts[n]->total_brate += brate_surplus[n];
355 : }
356 : }
357 : }
358 :
359 : /* reconfiguration in case of bitrate switching */
360 357385 : 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 1678 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 0 );
366 1678 : 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 420855 : if ( hCPE->element_mode == IVAS_CPE_DFT )
376 : {
377 59679 : 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 59679 : 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 59679 : stereo_dft_enc_analyze( sts, CPE_CHANNELS, input_frame, hCPE->hStereoDft, NULL, hCPE->hStereoDft->DFT, hCPE->input_mem );
388 :
389 59679 : 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 59679 : stereo_dft_enc_update( hCPE->hStereoDft, max_bwidth );
396 :
397 : /* DFT stereo processing */
398 59679 : stereo_dft_enc_process( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
399 : }
400 361176 : else if ( hCPE->element_mode == IVAS_CPE_TD )
401 : {
402 : /* Determine the energy ratio between the 2 channels */
403 3791 : 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 3791 : 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 3791 : sts[0]->bits_frame_channel = 0;
410 3791 : sts[1]->bits_frame_channel = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
411 3791 : sts[1]->bits_frame_channel += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
412 3791 : if ( st_ivas->hQMetaData != NULL )
413 : {
414 60 : sts[1]->bits_frame_channel -= st_ivas->hQMetaData->metadata_max_bits;
415 : }
416 :
417 3791 : Etot_last[0] = sts[0]->hNoiseEst->Etot_last;
418 3791 : Etot_last[1] = sts[1]->hNoiseEst->Etot_last;
419 : }
420 357385 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
421 : {
422 357385 : 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 420855 : if ( hCPE->element_mode == IVAS_CPE_DFT )
430 : {
431 : int32_t internal_Fs;
432 :
433 59679 : 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 59679 : 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 59679 : 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 59679 : if ( input_Fs == internal_Fs )
443 : {
444 11555 : 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 48124 : 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 59679 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
453 : {
454 19955 : mvr2r( sts[1]->old_inp_12k8, old_inp_12k8[1], L_INP_MEM );
455 19955 : 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 19955 : 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 59679 : 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 1202886 : 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 335295 : 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 782031 : &ener[n], &relE[n], A[n], Aw[n], epsP[n], lsp_new[n], lsp_mid[n], &vad_hover_flag[n], &attack_flag[n],
485 782031 : 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 1117326 : 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 782031 : 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 420855 : if ( st_ivas->nSCE + st_ivas->nCPE > 1 )
495 : {
496 277991 : 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 420855 : if ( ivas_format == MASA_FORMAT && nb_bits_metadata > 0 && hCPE->hCoreCoder[0]->Opt_DTX_ON )
508 : {
509 4016 : if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
510 : {
511 2368 : 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 420855 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hEncoderConfig->Opt_DTX_ON )
517 : {
518 18228 : stereoFdCngCoherence( sts, hCPE->last_element_mode, fft_buff );
519 :
520 : /* Reset metadata */
521 18228 : if ( sts[0]->cng_sba_flag || ( ivas_format == SBA_FORMAT ) )
522 : {
523 8278 : 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 420855 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->hMCT == NULL )
532 : {
533 : /* set coded BW for MDCT stereo */
534 87077 : set_bw_stereo( hCPE );
535 :
536 : /* reconfiguration of MDCT stereo */
537 87077 : 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 ) )
538 : {
539 728 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, sts[0]->bwidth, 0, NULL, 0 );
540 728 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
541 :
542 728 : if ( hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
543 : {
544 84 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
545 : {
546 0 : return error;
547 : }
548 : }
549 : }
550 : }
551 :
552 : /* IGF reconfiguration */
553 1202886 : for ( n = 0; n < n_CoreChannels; n++ )
554 : {
555 782031 : 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 ) && ( n == 0 || hCPE->element_mode == IVAS_CPE_MDCT ) )
556 : {
557 : int16_t igf;
558 : int16_t bw;
559 :
560 9189 : bw = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? sts[n]->bwidth : max_bwidth;
561 9189 : igf = getIgfPresent( sts[n]->element_mode, sts[n]->bits_frame_nominal * FRAMES_PER_SEC, bw, sts[n]->rf_mode );
562 9189 : 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 )
563 : {
564 0 : return error;
565 : }
566 : }
567 : }
568 :
569 : /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
570 1202886 : for ( n = 0; n < n_CoreChannels; n++ )
571 : {
572 782031 : if ( ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) && hCPE->element_mode == IVAS_CPE_DFT )
573 : {
574 10351 : 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 );
575 : }
576 : else
577 : {
578 771680 : 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 );
579 : }
580 : }
581 :
582 : /* configure TD stereo encoder */
583 420855 : if ( hCPE->element_mode == IVAS_CPE_TD )
584 : {
585 3791 : tdm_ol_pitch_comparison( hCPE, pitch_fr, voicing_fr );
586 :
587 3791 : 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 );
588 :
589 3791 : if ( hEncoderConfig->Opt_DTX_ON )
590 : {
591 1665 : stereo_cng_upd_counters( hCPE->hStereoCng, hCPE->element_mode, -1, NULL, sts[0]->hTdCngEnc->burst_ho_cnt, NULL );
592 : }
593 : }
594 :
595 : /* modify the coder_type depending on the total_brate per channel */
596 1202886 : for ( n = 0; n < n_CoreChannels; n++ )
597 : {
598 782031 : if ( ( hCPE->element_mode != IVAS_CPE_DFT && hCPE->element_mode != IVAS_CPE_TD ) || n == 0 ) /* modify coder_type of primary channel */
599 : {
600 : /* limit coder_type depending on the bitrate */
601 778240 : coder_type_modif( sts[n], relE[n] );
602 : }
603 : }
604 :
605 : /*----------------------------------------------------------------*
606 : * Write IVAS format signaling in SID frames
607 : *----------------------------------------------------------------*/
608 :
609 420855 : if ( sts[0]->core_brate == SID_2k40 )
610 : {
611 2142 : ivas_write_format_sid( ivas_format, hCPE->element_mode, sts[0]->hBstr, hEncoderConfig->sba_order, hEncoderConfig->sba_planar );
612 : }
613 :
614 : /*----------------------------------------------------------------*
615 : * DFT Stereo residual coding
616 : * DFT Stereo parameters writing into the bitstream
617 : *----------------------------------------------------------------*/
618 :
619 420855 : cpe_brate = 0;
620 420855 : if ( hCPE->element_mode == IVAS_CPE_DFT )
621 : {
622 59679 : if ( hEncoderConfig->Opt_DTX_ON )
623 : {
624 30303 : if ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA )
625 : {
626 : /* Reconfigure DFT Stereo for inactive frames */
627 10351 : if ( sts[0]->core_brate == SID_2k40 )
628 : {
629 1519 : stereo_dft_config( hConfigDft, IVAS_SID_5k2, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
630 : }
631 : else
632 : {
633 8832 : stereo_dft_config( hConfigDft, FRAME_NO_DATA, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
634 : }
635 :
636 10351 : stereo_dft_cng_side_gain( hCPE->hStereoDft, hCPE->hStereoCng, sts[0]->core_brate, sts[0]->last_core_brate, sts[0]->bwidth );
637 : #ifdef DEBUG_MODE_DFT
638 : hCPE->hStereoDft->res_cod_bits = 0;
639 : #endif
640 : }
641 : else
642 : {
643 19952 : 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 );
644 : }
645 : }
646 :
647 : /* Write stereo bitstream */
648 59679 : cpe_brate = st_ivas->hCPE[0]->element_brate;
649 :
650 : /* DFT stereo side bits */
651 59679 : 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 )
652 : {
653 5638 : nb_bits = 0; /* Only mono downmix is transmitted in this case */
654 : }
655 54041 : else if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) )
656 : {
657 530 : nb_bits = hCPE->hMetaData->nb_bits_tot;
658 : }
659 : else
660 : {
661 53511 : stereo_dft_enc_write_BS( hCPE, &nb_bits );
662 : }
663 :
664 : /* Residual coding in MDCT domain */
665 59679 : if ( !( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && ( sts[0]->core_brate == SID_2k40 || sts[0]->core_brate == FRAME_NO_DATA ) ) )
666 : {
667 59149 : int16_t max_bits = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC - 0.8f * sts[0]->bits_frame_nominal );
668 59149 : if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
669 : {
670 11394 : max_bits -= nb_bits_metadata;
671 11394 : if ( hCPE->brate_surplus < 0 )
672 : {
673 2293 : max_bits += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
674 : }
675 : }
676 :
677 59149 : stereo_dft_enc_res( hCPE->hStereoDft, old_inp_12k8[1] + L_INP_MEM - STEREO_DFT_OVL_8k, hCPE->hMetaData, &nb_bits, max_bits );
678 : }
679 :
680 59679 : if ( sts[0]->core_brate == FRAME_NO_DATA || sts[0]->core_brate == SID_2k40 )
681 : {
682 10351 : assert( ( nb_bits <= ( ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS ) ) && "Stereo DFT CNG: bit budget is violated" );
683 : }
684 : else
685 : {
686 : #ifdef DEBUGGING
687 : if ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT )
688 : {
689 : 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" );
690 : }
691 : else
692 : {
693 : assert( ( ( hCPE->element_brate / FRAMES_PER_SEC - nb_bits ) >= ( 0.8f * sts[0]->bits_frame_nominal ) ) && "Stereo DFT: bit budget is violated" );
694 : }
695 :
696 : #endif
697 : /* Flexible total bitrate in M channel */
698 49328 : sts[0]->total_brate = hCPE->element_brate - ( nb_bits * FRAMES_PER_SEC );
699 : }
700 :
701 : /* subtract metadata bitbudget */
702 59679 : sts[0]->total_brate -= ( nb_bits_metadata * FRAMES_PER_SEC );
703 :
704 : /* subtract bit-rate for combined format coding */
705 59679 : 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 ) )
706 : {
707 2784 : sts[0]->total_brate += hCPE->brate_surplus;
708 : }
709 : }
710 :
711 :
712 : /*----------------------------------------------------------------*
713 : * Core Encoder
714 : *----------------------------------------------------------------*/
715 :
716 420855 : 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 )
717 : {
718 0 : return error;
719 : }
720 :
721 : /*----------------------------------------------------------------*
722 : * Common updates
723 : *----------------------------------------------------------------*/
724 :
725 420855 : hCPE->last_element_brate = hCPE->element_brate;
726 420855 : hCPE->last_element_mode = hCPE->element_mode;
727 :
728 420855 : if ( ivas_format == MASA_ISM_FORMAT )
729 : {
730 9000 : hCPE->element_brate = element_brate_ref;
731 : }
732 :
733 420855 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
734 : {
735 : /* update input samples buffer */
736 44070 : for ( n = 0; n < CPE_CHANNELS; n++ )
737 : {
738 29380 : mvr2r( orig_input[n], sts[n]->old_input_signal, input_frame );
739 : }
740 : }
741 406165 : else if ( hCPE->element_mode == IVAS_CPE_DFT )
742 : {
743 59679 : mvr2r( sts[0]->input, sts[0]->old_input_signal, input_frame );
744 : }
745 346486 : else if ( st_ivas->hMCT == NULL ) /* note: in MCT, input buffers are updated later in ivas_mct_enc() */
746 : {
747 : /* update input samples buffer */
748 228534 : for ( n = 0; n < CPE_CHANNELS; n++ )
749 : {
750 152356 : mvr2r( sts[n]->input, sts[n]->old_input_signal, input_frame );
751 : }
752 : }
753 :
754 420855 : if ( hCPE->hFrontVad[0] != NULL )
755 : {
756 31968 : hCPE->hFrontVad[0]->ini_frame++;
757 31968 : hCPE->hFrontVad[0]->ini_frame = min( hCPE->hFrontVad[0]->ini_frame, MAX_FRAME_COUNTER );
758 : }
759 :
760 : /* Store previous attack detection flag */
761 1262565 : for ( n = 0; n < CPE_CHANNELS; n++ )
762 : {
763 841710 : sts[n]->hTranDet->transientDetector.prev_bIsAttackPresent = sts[n]->hTranDet->transientDetector.bIsAttackPresent;
764 : }
765 :
766 : #ifdef DEBUG_MODE_INFO
767 : if ( hCPE->element_mode == IVAS_CPE_DFT )
768 : {
769 : n = (int16_t) hCPE->hStereoDft->hItd->itd[1];
770 : dbgwrite( &n, 2, 1, input_frame, "res/itd" );
771 : n = 0;
772 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_refChan" );
773 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_NCShift" );
774 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_ica_gD" );
775 : n = -1;
776 : // dbgwrite( &n, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
777 : }
778 : else if ( hCPE->element_mode == IVAS_CPE_TD )
779 : {
780 : dbgwrite( &hCPE->hStereoTCA->refChanIndx, 2, 1, input_frame, "res/TCA_idx_refChan" );
781 : n = hCPE->hStereoTCA->refChanIndx == 0 ? hCPE->hStereoTCA->indx_ica_NCShift : -hCPE->hStereoTCA->indx_ica_NCShift;
782 : dbgwrite( &n, 2, 1, input_frame, "res/TCA_idx_NCShift" );
783 : dbgwrite( &hCPE->hStereoTCA->indx_ica_gD, 2, 1, input_frame, "res/TCA_idx_ica_gD" );
784 :
785 : n = hCPE->hStereoTCA->corrLagStats[2];
786 : dbgwrite( &n, 2, 1, input_frame, "res/itd" );
787 :
788 : dbgwrite( &tdm_ratio_idx, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
789 : }
790 : else if ( hCPE->element_mode == IVAS_CPE_MDCT )
791 : {
792 : n = -2;
793 : // dbgwrite( &n, 2, 1, input_frame, "res/tdm_ratio_idx.enc" );
794 : }
795 :
796 : {
797 : float tmpF = ivas_total_brate / 1000.0f;
798 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, cpe_id, ENC ) );
799 : }
800 : #endif
801 :
802 420855 : pop_wmops();
803 420855 : return error;
804 : }
805 :
806 :
807 : /*-------------------------------------------------------------------------
808 : * create_cpe_enc()
809 : *
810 : * Create, allocate and initialize IVAS encoder CPE handle
811 : *-------------------------------------------------------------------------*/
812 :
813 2880 : ivas_error create_cpe_enc(
814 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
815 : const int16_t cpe_id, /* i : CPE # identifier */
816 : const int32_t element_brate /* i : element bitrate */
817 : )
818 : {
819 : int16_t n;
820 : int16_t ivas_format, element_mode_init, max_bwidth;
821 : int32_t input_Fs;
822 : CPE_ENC_HANDLE hCPE;
823 : Encoder_State *st;
824 : ENCODER_CONFIG_HANDLE hEncoderConfig;
825 : ivas_error error;
826 :
827 2880 : error = IVAS_ERR_OK;
828 :
829 2880 : hEncoderConfig = st_ivas->hEncoderConfig;
830 :
831 2880 : ivas_format = hEncoderConfig->ivas_format;
832 2880 : element_mode_init = hEncoderConfig->element_mode_init;
833 2880 : input_Fs = hEncoderConfig->input_Fs;
834 2880 : max_bwidth = hEncoderConfig->max_bwidth;
835 :
836 : /*-----------------------------------------------------------------*
837 : * Allocate CPE handle
838 : *-----------------------------------------------------------------*/
839 :
840 2880 : if ( ( hCPE = (CPE_ENC_HANDLE) malloc( sizeof( CPE_ENC_DATA ) ) ) == NULL )
841 : {
842 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
843 : }
844 :
845 : /*-----------------------------------------------------------------*
846 : * Initialization - general parameters
847 : *-----------------------------------------------------------------*/
848 :
849 2880 : hCPE->cpe_id = cpe_id;
850 2880 : hCPE->element_brate = element_brate;
851 2880 : hCPE->last_element_brate = hCPE->element_brate;
852 2880 : hCPE->element_mode = element_mode_init;
853 2880 : hCPE->last_element_mode = element_mode_init;
854 :
855 2880 : hCPE->hStereoDft = NULL;
856 2880 : hCPE->hStereoTD = NULL;
857 2880 : hCPE->hStereoMdct = NULL;
858 2880 : hCPE->hStereoTCA = NULL;
859 2880 : hCPE->hStereoICBWE = NULL;
860 2880 : hCPE->hMetaData = NULL;
861 :
862 2880 : hCPE->hStereoCng = NULL;
863 2880 : hCPE->hFrontVad[0] = NULL;
864 2880 : hCPE->hFrontVad[1] = NULL;
865 :
866 2880 : hCPE->brate_surplus = 0;
867 :
868 : /*-----------------------------------------------------------------*
869 : * Input memory buffer: allocate and initialize
870 : *-----------------------------------------------------------------*/
871 :
872 8640 : for ( n = 0; n < CPE_CHANNELS; n++ )
873 : {
874 5760 : 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 )
875 : {
876 358 : if ( ( hCPE->input_mem[n] = (float *) malloc( sizeof( float ) * NS2SA( input_Fs, STEREO_DFT_OVL_NS ) ) ) == NULL )
877 : {
878 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
879 : }
880 :
881 358 : set_zero( hCPE->input_mem[n], NS2SA( input_Fs, STEREO_DFT_OVL_NS ) );
882 : }
883 : else
884 : {
885 5402 : hCPE->input_mem[n] = NULL;
886 : }
887 : }
888 :
889 : /*-----------------------------------------------------------------*
890 : * stereo classifier: allocate and initialize
891 : *-----------------------------------------------------------------*/
892 :
893 2880 : if ( ( hCPE->hStereoClassif = (STEREO_CLASSIF_HANDLE) malloc( sizeof( STEREO_CLASSIF_DATA ) ) ) == NULL )
894 : {
895 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo classifier structure\n" ) );
896 : }
897 :
898 2880 : stereo_classifier_init( hCPE->hStereoClassif );
899 :
900 : /*-----------------------------------------------------------------*
901 : * Metadata: allocate and initialize
902 : *-----------------------------------------------------------------*/
903 :
904 2880 : if ( cpe_id == ( st_ivas->nCPE - 1 ) )
905 : {
906 1446 : if ( ( error = ivas_initialize_MD_bstr_enc( &( hCPE->hMetaData ), st_ivas ) ) != IVAS_ERR_OK )
907 : {
908 0 : return error;
909 : }
910 : }
911 :
912 : /*-----------------------------------------------------------------*
913 : * CoreCoder, 2 instances: allocate and initialize
914 : *-----------------------------------------------------------------*/
915 :
916 8640 : for ( n = 0; n < CPE_CHANNELS; n++ )
917 : {
918 5760 : if ( ( st = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
919 : {
920 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
921 : }
922 :
923 5760 : copy_encoder_config( st_ivas, st, 1 );
924 :
925 5760 : 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() */
926 5760 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
927 :
928 5760 : if ( ( error = init_encoder( st, st_ivas, n, hEncoderConfig->var_SID_rate_flag, hEncoderConfig->interval_SID, 0, ISM_MODE_NONE, hCPE->element_brate ) ) != IVAS_ERR_OK )
929 : {
930 0 : return error;
931 : }
932 :
933 5760 : if ( st->hFdCngEnc != NULL )
934 : {
935 145 : st->hFdCngEnc->hFdCngCom->CngBitrate = hCPE->element_brate - 1;
936 : }
937 :
938 5760 : hCPE->hCoreCoder[n] = st;
939 : }
940 :
941 : /*-----------------------------------------------------------------*
942 : * LR VAD initialization
943 : *-----------------------------------------------------------------*/
944 :
945 2880 : if ( hEncoderConfig->Opt_DTX_ON )
946 : {
947 88 : if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT )
948 : {
949 31 : if ( ( hCPE->hStereoCng = (STEREO_CNG_ENC_HANDLE) malloc( sizeof( STEREO_CNG_ENC ) ) ) == NULL )
950 : {
951 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo Cng for Unified/TD \n" ) );
952 : }
953 :
954 31 : stereo_enc_cng_init( hCPE->hStereoCng );
955 : }
956 : }
957 : else
958 : {
959 2792 : hCPE->hStereoCng = NULL;
960 : }
961 :
962 : #ifdef DEBUGGING
963 : 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 ) )
964 : #else
965 2880 : if ( hEncoderConfig->Opt_DTX_ON && element_mode_init != IVAS_CPE_MDCT )
966 : #endif
967 : {
968 93 : for ( n = 0; n < CPE_CHANNELS; n++ )
969 : {
970 62 : if ( ( error = front_vad_create( &( hCPE->hFrontVad[n] ), hEncoderConfig ) ) != IVAS_ERR_OK )
971 : {
972 0 : return error;
973 : }
974 : }
975 : }
976 : else
977 : {
978 8547 : for ( n = 0; n < CPE_CHANNELS; n++ )
979 : {
980 5698 : hCPE->hFrontVad[n] = NULL;
981 : }
982 : }
983 :
984 : /*-----------------------------------------------------------------*
985 : * DFT stereo initialization
986 : *-----------------------------------------------------------------*/
987 :
988 2880 : if ( hCPE->element_mode == IVAS_CPE_DFT )
989 : {
990 99 : if ( ( error = stereo_dft_enc_create( &( hCPE->hStereoDft ), input_Fs, max_bwidth ) ) != IVAS_ERR_OK )
991 : {
992 0 : return error;
993 : }
994 : }
995 :
996 : /*-----------------------------------------------------------------*
997 : * Temporal inter-channel alignment initialization
998 : *-----------------------------------------------------------------*/
999 :
1000 2880 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1001 : {
1002 99 : if ( ( hCPE->hStereoTCA = (STEREO_TCA_ENC_HANDLE) malloc( sizeof( STEREO_TCA_ENC_DATA ) ) ) == NULL )
1003 : {
1004 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
1005 : }
1006 :
1007 99 : stereo_tca_init_enc( hCPE->hStereoTCA, input_Fs );
1008 : }
1009 :
1010 : /*-----------------------------------------------------------------*
1011 : * Stereo IC BWE initialization
1012 : *-----------------------------------------------------------------*/
1013 :
1014 2880 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1015 : {
1016 99 : if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_ENC_HANDLE) malloc( sizeof( STEREO_ICBWE_ENC_DATA ) ) ) == NULL )
1017 : {
1018 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
1019 : }
1020 :
1021 99 : stereo_icBWE_init_enc( hCPE->hStereoICBWE );
1022 : }
1023 :
1024 : /*-----------------------------------------------------------------*
1025 : * TD stereo initialization
1026 : *-----------------------------------------------------------------*/
1027 :
1028 2880 : if ( hCPE->element_mode == IVAS_CPE_TD )
1029 : {
1030 0 : if ( ( hCPE->hStereoTD = (STEREO_TD_ENC_DATA_HANDLE) malloc( sizeof( STEREO_TD_ENC_DATA ) ) ) == NULL )
1031 : {
1032 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
1033 : }
1034 :
1035 0 : stereo_td_init_enc( hCPE->hStereoTD, hCPE->last_element_mode );
1036 : }
1037 :
1038 : /*-----------------------------------------------------------------*
1039 : * MDCT stereo initialization
1040 : *-----------------------------------------------------------------*/
1041 :
1042 2880 : if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->nCPE == 1 )
1043 : {
1044 365 : if ( ( hCPE->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
1045 : {
1046 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
1047 : }
1048 :
1049 : #ifdef DEBUGGING
1050 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = st_ivas->hEncoderConfig->mdct_stereo_mode_cmdl;
1051 : #endif
1052 365 : initMdctStereoEncData( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 1 );
1053 365 : hCPE->hStereoMdct->isSBAStereoMode = ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && ( st_ivas->nchan_transport == 2 ) );
1054 :
1055 365 : if ( hCPE->element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT )
1056 : {
1057 13 : if ( ( error = initMdctItdHandling( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
1058 : {
1059 0 : return error;
1060 : }
1061 : }
1062 : }
1063 :
1064 2880 : st_ivas->hCPE[cpe_id] = hCPE;
1065 :
1066 2880 : return error;
1067 : }
1068 :
1069 :
1070 : /*-------------------------------------------------------------------------
1071 : * destroy_cpe_enc()
1072 : *
1073 : * Destroy and deallocate IVAS encoder CPE handle
1074 : *-------------------------------------------------------------------------*/
1075 :
1076 2880 : void destroy_cpe_enc(
1077 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structure */
1078 : )
1079 : {
1080 : int16_t n;
1081 : Encoder_State *st;
1082 :
1083 2880 : if ( hCPE->hStereoClassif != NULL )
1084 : {
1085 2880 : free( hCPE->hStereoClassif );
1086 2880 : hCPE->hStereoClassif = NULL;
1087 : }
1088 :
1089 8640 : for ( n = 0; n < CPE_CHANNELS; n++ )
1090 : {
1091 5760 : if ( hCPE->input_mem[n] != NULL )
1092 : {
1093 358 : free( hCPE->input_mem[n] );
1094 358 : hCPE->input_mem[n] = NULL;
1095 : }
1096 : }
1097 :
1098 2880 : ivas_destroy_MD_bstr_enc( &( hCPE->hMetaData ) );
1099 :
1100 8640 : for ( n = 0; n < CPE_CHANNELS; n++ )
1101 : {
1102 5760 : st = hCPE->hCoreCoder[n];
1103 :
1104 5760 : if ( st != NULL )
1105 : {
1106 5760 : destroy_core_enc( st );
1107 5760 : st = NULL;
1108 : }
1109 : }
1110 :
1111 2880 : if ( hCPE->hStereoDft != NULL )
1112 : {
1113 81 : stereo_dft_enc_destroy( &hCPE->hStereoDft );
1114 81 : hCPE->hStereoDft = NULL;
1115 : }
1116 :
1117 2880 : if ( hCPE->hStereoTD != NULL )
1118 : {
1119 1 : free( hCPE->hStereoTD );
1120 1 : hCPE->hStereoTD = NULL;
1121 : }
1122 :
1123 2880 : if ( hCPE->hStereoMdct != NULL )
1124 : {
1125 391 : stereo_mdct_enc_destroy( &hCPE->hStereoMdct );
1126 391 : hCPE->hStereoMdct = NULL;
1127 : }
1128 :
1129 2880 : if ( hCPE->hStereoTCA != NULL )
1130 : {
1131 82 : free( hCPE->hStereoTCA );
1132 82 : hCPE->hStereoTCA = NULL;
1133 : }
1134 :
1135 2880 : if ( hCPE->hStereoICBWE != NULL )
1136 : {
1137 82 : free( hCPE->hStereoICBWE );
1138 82 : hCPE->hStereoICBWE = NULL;
1139 : }
1140 :
1141 2880 : if ( hCPE->hStereoCng != NULL )
1142 : {
1143 57 : free( hCPE->hStereoCng );
1144 57 : hCPE->hStereoCng = NULL;
1145 : }
1146 :
1147 2880 : if ( hCPE->hFrontVad[0] != NULL )
1148 : {
1149 84 : for ( n = 0; n < CPE_CHANNELS; n++ )
1150 : {
1151 56 : front_vad_destroy( &hCPE->hFrontVad[n] );
1152 56 : hCPE->hFrontVad[n] = NULL;
1153 : }
1154 : }
1155 :
1156 2880 : free( hCPE );
1157 :
1158 2880 : return;
1159 : }
1160 :
1161 :
1162 : /*-------------------------------------------------------------------------
1163 : * stereo_mode_combined_format_enc()
1164 : *
1165 : * Set stereo format in a combined format
1166 : *-------------------------------------------------------------------------*/
1167 :
1168 420855 : static void stereo_mode_combined_format_enc(
1169 : const Encoder_Struct *st_ivas, /* i : encoder main structure */
1170 : CPE_ENC_HANDLE hCPE /* i/o: CPE handle */
1171 : )
1172 : {
1173 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1174 : int32_t element_brate_ref;
1175 :
1176 420855 : hEncoderConfig = st_ivas->hEncoderConfig;
1177 :
1178 420855 : if ( hEncoderConfig->ivas_format == MASA_ISM_FORMAT )
1179 : {
1180 9000 : element_brate_ref = hCPE->element_brate;
1181 :
1182 9000 : if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC &&
1183 3142 : ( ( hEncoderConfig->nchan_ism == 3 && hEncoderConfig->ivas_total_brate == IVAS_96k ) ||
1184 2782 : ( hEncoderConfig->nchan_ism == 4 && hEncoderConfig->ivas_total_brate == IVAS_128k ) ) )
1185 : {
1186 426 : if ( hCPE->element_brate + hCPE->brate_surplus > IVAS_64k )
1187 : {
1188 0 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = 0;
1189 : }
1190 : else
1191 : {
1192 426 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt++;
1193 426 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = min( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, OMASA_STEREO_SW_CNT_MAX );
1194 : }
1195 :
1196 426 : if ( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt < OMASA_STEREO_SW_CNT_MAX )
1197 : {
1198 0 : hCPE->element_mode = IVAS_CPE_MDCT;
1199 0 : hCPE->element_brate = IVAS_64k;
1200 0 : hCPE->brate_surplus -= ( hCPE->element_brate - element_brate_ref );
1201 : }
1202 :
1203 : /* write OMASA stereo mode signalling */
1204 426 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
1205 : {
1206 0 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 1, NBITS_ELEMENT_MODE );
1207 : }
1208 : else
1209 : {
1210 426 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 0, NBITS_ELEMENT_MODE );
1211 : }
1212 : }
1213 : }
1214 :
1215 420855 : return;
1216 : }
|