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