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