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_fx.h"
40 : #include "prot_fx_enc.h"
41 : #include "ivas_rom_com.h"
42 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 : #include "wmc_auto.h"
46 : #include "ivas_prot_fx.h"
47 : #include "ivas_rom_enc.h"
48 : #include "prot_fx_enc.h"
49 :
50 :
51 : /*--------------------------------------------------------------------------*
52 : * Local function prototypes
53 : *--------------------------------------------------------------------------*/
54 :
55 : static void stereo_mode_combined_format_enc_fx( const Encoder_Struct *st_ivas, CPE_ENC_HANDLE hCPE );
56 :
57 :
58 : /*-------------------------------------------------------------------*
59 : * ivas_cpe_enc()
60 : *
61 : * Channel Pair Element (CPE) encoding routine
62 : *-------------------------------------------------------------------*/
63 :
64 422855 : ivas_error ivas_cpe_enc_fx(
65 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
66 : const Word16 cpe_id, /* i : CPE # identifier Q0*/
67 : Word32 data_fx_ch0[], /* i : input signal for channel 0 Q(q_data_fx)*/
68 : Word32 data_fx_ch1[], /* i : input signal for channel 1 Q(q_data_fx)*/
69 : Word16 q_data_fx, /* i : Q-factor of input signal for both channels */
70 : const Word16 input_frame, /* i : input frame length per channel Q0*/
71 : const Word16 nb_bits_metadata, /* i : number of metadata bits Q0*/
72 : Word16 Q_new_out[] )
73 : {
74 : CPE_ENC_HANDLE hCPE;
75 : STEREO_DFT_CONFIG_DATA_HANDLE hConfigDft;
76 : Encoder_State **sts;
77 : Word16 n, n_CoreChannels;
78 : Word32 old_inp_12k8_fx[CPE_CHANNELS][L_INP_12k8]; /* buffer of input signal @ 12k8 Q15 */
79 : Word16 old_inp_12k8_16fx[CPE_CHANNELS][L_INP_12k8]; /* buffer of input signal @ 12k8 Q_new-1 */
80 : Word32 old_inp_16k_fx[CPE_CHANNELS][L_INP]; /* buffer of input signal @ 16kHz Q15 */
81 : Word16 old_inp_16k_16fx[CPE_CHANNELS][L_INP]; /* buffer of input signal @ 16kHz Q_new-1 */
82 : Word16 fft_buff_fx[CPE_CHANNELS][2 * L_FFT]; /* FFT buffer */
83 : Word16 fft_buff_fx_q[CPE_CHANNELS]; /* FFT buffer */
84 422855 : set16_fx( fft_buff_fx_q, 0, CPE_CHANNELS );
85 422855 : Word16 fft_buff_fx_final_q = MAX_16;
86 422855 : move16();
87 : Word32 ener_fx[CPE_CHANNELS]; /* residual energy from Levinson-Durbin Q6 */
88 : Word16 relE_fx[CPE_CHANNELS]; /* frame relative energy Q8 */
89 422855 : Word16 A_fx[CPE_CHANNELS][NB_SUBFR16k * ( M + 1 )] = { 0 }; /* A(z) unquantized for subframes Q12 */
90 422855 : Word16 Aw_fx[CPE_CHANNELS][NB_SUBFR16k * ( M + 1 )] = { 0 }; /* weighted A(z) unquantized for subframes Q12 */
91 : Word32 epsP_fx[CPE_CHANNELS][M + 1]; /* LP prediction errors */
92 : Word16 epsP_fx_q[CPE_CHANNELS];
93 : Word16 lsp_new_fx[CPE_CHANNELS][M]; /* LSPs at the end of the frame Q15 */
94 : Word16 lsp_mid_fx[CPE_CHANNELS][M]; /* ISPs in the middle of the frame Q15 */
95 : Word16 vad_hover_flag[CPE_CHANNELS]; /* VAD hangover flag */
96 : Word16 attack_flag[CPE_CHANNELS]; /* attack flag (GSC or TC) */
97 : Word32 realBuffer_fx[CPE_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer */
98 : Word32 imagBuffer_fx[CPE_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer */
99 : Word16 q_re_im_buf[CPE_CHANNELS];
100 : Word16 pitch_fr_fx[CPE_CHANNELS][NB_SUBFR]; /* Q6, fractional pitch values */
101 : Word16 voicing_fr_fx[CPE_CHANNELS][NB_SUBFR]; /* Q15, fractional pitch gains */
102 : Word16 loc_harm[CPE_CHANNELS]; /* harmonicity flag */
103 : Word16 cor_map_sum_fx[CPE_CHANNELS]; /* speech/music clasif. parameter Q8 */
104 : Word16 vad_flag_dtx[CPE_CHANNELS]; /* HE-SAD flag with additional DTX HO */
105 : Word32 enerBuffer_fx[CPE_CHANNELS][CLDFB_NO_CHANNELS_MAX]; /* energy buffer */
106 : Word16 enerBuffer_fx_exp[CPE_CHANNELS]; /* energy buffer */
107 : Word32 currFlatness_fx[CPE_CHANNELS]; /* flatness parameter Q21 */
108 : Word16 tdm_ratio_idx, tdm_ratio_idx_SM; /* temp. TD stereo parameters */
109 : Word16 tdm_SM_or_LRTD_Pri; /* temp. TD stereo parameters */
110 : Word16 nb_bits; /* number of DFT stereo side bits */
111 : Word32 fr_bands_fx[CPE_CHANNELS][2 * NB_BANDS]; /* energy in frequency bands (Q_buffer + QSCALE) */
112 : Word16 Etot_LR_fx[CPE_CHANNELS]; /* total energy Q8 */
113 : Word32 lf_E_fx[CPE_CHANNELS][2 * VOIC_BINS]; /* per bin spectrum energy in lf (Q_buffer + QSCALE - 2) */
114 : Word16 localVAD_HE_SAD[CPE_CHANNELS]; /* HE-SAD flag without hangover, LR channels */
115 : Word32 band_energies_LR_fx[2 * NB_BANDS]; /* energy in critical bands without minimum noise floor E_MIN (Q_buffer + QSCALE - band_ener_guardbits) */
116 : Word16 Q_buffer[2];
117 : Word16 orig_input_fx[CPE_CHANNELS][L_FRAME48k];
118 : Word32 orig_input_fx32[CPE_CHANNELS][L_FRAME48k];
119 : Word16 Q_orig_inp[CPE_CHANNELS];
120 : Word16 Q_orig_inp32[CPE_CHANNELS];
121 : Word16 Etot_last_fx[CPE_CHANNELS];
122 : Word32 tmp, input_Fs;
123 : Word16 max_bwidth, ivas_format;
124 : ENCODER_CONFIG_HANDLE hEncoderConfig;
125 : Word32 ivas_total_brate;
126 : ivas_error error;
127 : Word32 cpe_brate;
128 : Word32 element_brate_ref;
129 : Word16 last_bits_frame_nominal; /* last_bits_frame_nominal for M or PCh channel */
130 : Word16 old_wsp_fx[CPE_CHANNELS][L_WSP];
131 : Word16 e_old_wsp[CPE_CHANNELS], q_old_wsp;
132 422855 : Word16 Q_new[CPE_CHANNELS] = { 0 };
133 422855 : move16();
134 422855 : move16();
135 : Word16 q_com, shift, q_min, gb;
136 :
137 422855 : error = IVAS_ERR_OK;
138 422855 : move32();
139 422855 : push_wmops( "ivas_cpe_enc" );
140 :
141 422855 : hCPE = st_ivas->hCPE[cpe_id];
142 422855 : sts = hCPE->hCoreCoder;
143 422855 : hEncoderConfig = st_ivas->hEncoderConfig;
144 422855 : max_bwidth = hEncoderConfig->max_bwidth;
145 422855 : ivas_format = hEncoderConfig->ivas_format;
146 422855 : input_Fs = hEncoderConfig->input_Fs;
147 422855 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
148 422855 : element_brate_ref = hCPE->element_brate;
149 422855 : last_bits_frame_nominal = sts[0]->bits_frame_nominal;
150 422855 : move16();
151 422855 : move16();
152 422855 : move16();
153 422855 : move32();
154 422855 : move32();
155 422855 : move32();
156 :
157 : /*------------------------------------------------------------------*
158 : * Initialization - general
159 : *-----------------------------------------------------------------*/
160 :
161 422855 : set16_fx( q_re_im_buf, 0, CPE_CHANNELS );
162 :
163 422855 : tdm_SM_or_LRTD_Pri = 0;
164 422855 : tdm_ratio_idx = -1;
165 422855 : tdm_ratio_idx_SM = -1;
166 : // tdm_last_ratio = 0;
167 422855 : move16();
168 422855 : move16();
169 422855 : move16();
170 :
171 : #ifdef DEBUGGING
172 : if ( hCPE->hCoreCoder[0]->ini_frame == 0 )
173 : {
174 : hCPE->stereo_mode_cmdl = hEncoderConfig->stereo_mode_cmdl;
175 : }
176 : #endif
177 :
178 422855 : set16_fx( pitch_fr_fx[0], 0, NB_SUBFR );
179 422855 : set16_fx( pitch_fr_fx[1], 0, NB_SUBFR );
180 422855 : set16_fx( voicing_fr_fx[0], 0, NB_SUBFR );
181 422855 : set16_fx( voicing_fr_fx[1], 0, NB_SUBFR );
182 :
183 1268565 : FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
184 : {
185 845710 : set16_zero_fx( fft_buff_fx[i], 2 * L_FFT );
186 845710 : set16_zero_fx( old_inp_16k_16fx[i], L_INP );
187 845710 : set_zero_fx( old_inp_16k_fx[i], L_INP );
188 845710 : set16_zero_fx( old_inp_12k8_16fx[i], L_INP_12k8 );
189 845710 : set_zero_fx( old_inp_12k8_fx[i], L_INP_12k8 );
190 : }
191 :
192 : /*------------------------------------------------------------------*
193 : * CPE initialization - core coder
194 : *-----------------------------------------------------------------*/
195 :
196 1268565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
197 : {
198 845710 : sts[n]->idchan = n; /* Q0 */
199 845710 : sts[n]->core = -1; /* Q0 */
200 845710 : sts[n]->core_brate = -1; /* updated in dtx() Q0*/
201 845710 : sts[n]->max_bwidth = max_bwidth; /* Q0 */
202 845710 : move32();
203 845710 : move16();
204 845710 : move16();
205 845710 : move16();
206 :
207 845710 : IF( st_ivas->hMCT == NULL ) /*already updated before CPE call*/
208 : {
209 301094 : sts[n]->input_bwidth = sts[n]->last_input_bwidth; /* updated in BWD Q0*/
210 301094 : sts[n]->bwidth = sts[n]->last_bwidth; /* updated in BWD Q0*/
211 301094 : move16();
212 301094 : move16();
213 : }
214 845710 : sts[n]->rate_switching_reset = 0;
215 845710 : move16();
216 :
217 : #ifdef DEBUGGING
218 : sts[n]->force = hEncoderConfig->force;
219 : sts[n]->id_element = cpe_id + st_ivas->nSCE;
220 : #ifdef DEBUG_FORCE_DIR
221 : sts[n]->force_dir = hEncoderConfig->force_dir;
222 : #endif
223 : #endif
224 : }
225 :
226 422855 : Word16 norm, Q_min = s_min( q_data_fx, add( sts[0]->q_old_inp32, L_norm_arr( sts[0]->input32_fx - input_frame, input_frame ) ) );
227 422855 : norm = L_norm_arr( data_fx_ch0, input_frame );
228 422855 : IF( data_fx_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
229 : {
230 363026 : Q_min = s_min( Q_min, add( sts[1]->q_old_inp32, L_norm_arr( sts[1]->input32_fx - input_frame, input_frame ) ) );
231 363026 : norm = s_min( norm, L_norm_arr( data_fx_ch1, input_frame ) );
232 : }
233 422855 : scale_sig32( sts[0]->input32_fx - input_frame, input_frame, sub( Q_min, sts[0]->q_old_inp32 ) );
234 422855 : sts[0]->q_old_inp32 = Q_min;
235 422855 : move16();
236 422855 : Copy_Scale_sig32( data_fx_ch0, sts[0]->input32_fx, input_frame, sub( Q_min, q_data_fx ) ); // Q(Q_min)
237 422855 : sts[0]->q_inp32 = Q_min;
238 422855 : move16();
239 :
240 422855 : Copy_Scale_sig32_16( sts[0]->input32_fx, sts[0]->input_fx, input_frame, norm );
241 422855 : sts[0]->q_inp = add( sub( sts[0]->q_inp32, Q16 ), norm );
242 422855 : move16();
243 422855 : IF( data_fx_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
244 : {
245 363026 : scale_sig32( sts[1]->input32_fx - input_frame, input_frame, sub( Q_min, sts[1]->q_old_inp32 ) );
246 363026 : sts[1]->q_old_inp32 = Q_min;
247 363026 : move16();
248 363026 : Copy_Scale_sig32( data_fx_ch1, sts[1]->input32_fx, input_frame, sub( Q_min, q_data_fx ) ); // Q(Q_min)
249 363026 : sts[1]->q_inp32 = Q_min;
250 363026 : move16();
251 :
252 363026 : Copy_Scale_sig32_16( sts[1]->input32_fx, sts[1]->input_fx, input_frame, norm );
253 363026 : sts[1]->q_inp = add( sub( sts[1]->q_inp32, Q16 ), norm );
254 363026 : move16();
255 : }
256 :
257 1268565 : FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
258 : {
259 14377070 : FOR( Word16 j = 0; j < CLDFB_NO_COL_MAX; j++ )
260 : {
261 13531360 : set_zero_fx( realBuffer_fx[i][j], CLDFB_NO_CHANNELS_MAX );
262 13531360 : set_zero_fx( imagBuffer_fx[i][j], CLDFB_NO_CHANNELS_MAX );
263 : }
264 : }
265 :
266 : /*----------------------------------------------------------------*
267 : * Stereo technology selection
268 : * Front-VAD on input L and R channels
269 : *----------------------------------------------------------------*/
270 :
271 422855 : test();
272 422855 : IF( sts[0]->ini_frame > 0 && st_ivas->hMCT == NULL )
273 : {
274 150083 : hCPE->element_mode = select_stereo_mode_fx( hCPE, ivas_format );
275 : }
276 : #ifdef DEBUG_FORCE_DIR
277 : else
278 : {
279 : if ( hCPE->hCoreCoder[0]->force_dir[0] != '\0' )
280 : {
281 : dbgread( &hCPE->element_mode, sizeof( int16_t ), 1, fname( hCPE->hCoreCoder[0]->force_dir, "force_element_mode.enf", -1, -1, -1 ) );
282 : }
283 : else
284 : {
285 : dbgwrite( &hCPE->element_mode, sizeof( int16_t ), 1, 1, "res/force_element_mode.enf" );
286 : }
287 : }
288 : #endif
289 :
290 422855 : stereo_mode_combined_format_enc_fx( st_ivas, hCPE );
291 :
292 422855 : Word16 Q_inp = 0;
293 422855 : move16();
294 :
295 422855 : Word16 Q_add = 2;
296 422855 : move16();
297 422855 : Word16 front_create_flag = 0;
298 422855 : move16();
299 422855 : set32_fx( band_energies_LR_fx, 0, 2 * NB_BANDS );
300 :
301 422855 : IF( hCPE->hFrontVad[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
302 : {
303 31908 : Word16 scale_inp = Q15;
304 31908 : Q_add = 0;
305 31908 : move16();
306 95724 : FOR( n = 0; n < CPE_CHANNELS; n++ )
307 : {
308 63816 : scale_inp = s_min( scale_inp, s_min( sts[n]->q_inp, hCPE->hFrontVad[n]->q_mem_decim ) );
309 : }
310 95724 : FOR( n = 0; n < CPE_CHANNELS; n++ )
311 : {
312 63816 : scale_sig( sts[n]->input_fx, input_frame, sub( scale_inp, sts[n]->q_inp ) );
313 63816 : scale_sig( hCPE->hFrontVad[n]->mem_decim_fx, 2 * L_FILT_MAX, sub( scale_inp, hCPE->hFrontVad[n]->q_mem_decim ) );
314 63816 : sts[n]->q_inp = scale_inp;
315 63816 : move16();
316 63816 : hCPE->hFrontVad[n]->q_mem_decim = scale_inp;
317 : }
318 31908 : Q_inp = scale_inp;
319 31908 : move16();
320 : }
321 :
322 1268565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
323 : {
324 845710 : Q_buffer[n] = 15;
325 845710 : move16();
326 : }
327 422855 : test();
328 422855 : IF( hCPE->hFrontVad[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
329 : {
330 95724 : FOR( n = 0; n < CPE_CHANNELS; n++ )
331 : {
332 63816 : Q_buffer[n] = hCPE->hFrontVad[n]->q_buffer_12k8;
333 63816 : move16();
334 : }
335 : }
336 :
337 : Word16 q_band_energies_LR;
338 : Word16 q_fr_bands[2], q_lf_E[2];
339 422855 : IF( NE_32( ( error = front_vad_fx( hCPE, NULL, hEncoderConfig, &hCPE->hFrontVad[0], st_ivas->hMCT != NULL, input_frame, vad_flag_dtx,
340 : fr_bands_fx, q_fr_bands, Etot_LR_fx, lf_E_fx, q_lf_E, localVAD_HE_SAD, vad_hover_flag, band_energies_LR_fx,
341 : &q_band_energies_LR, NULL, NULL, NULL, Q_inp, Q_buffer, Q_add, &front_create_flag ) ),
342 : IVAS_ERR_OK ) )
343 : {
344 0 : return error;
345 : }
346 :
347 422855 : IF( hCPE->hFrontVad[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
348 : {
349 95904 : FOR( n = 0; n < CPE_CHANNELS; n++ )
350 : {
351 : // Scale_sig( hCPE->hFrontVad[n]->buffer_12k8_fx + 384, 3 * L_FRAME / 2 - 384, sub( Q_buffer[n], hCPE->hFrontVad[n]->q_buffer_12k8 ) );
352 63936 : hCPE->hFrontVad[n]->q_buffer_12k8 = Q_buffer[n];
353 63936 : move16();
354 : }
355 : }
356 422855 : sts[0]->element_mode = hCPE->element_mode;
357 422855 : sts[1]->element_mode = hCPE->element_mode;
358 422855 : move16();
359 422855 : move16();
360 :
361 422855 : n_CoreChannels = 2;
362 422855 : move16();
363 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
364 : {
365 59654 : n_CoreChannels = 1; /* in DFT stereo, only M channel is coded */
366 59654 : move16();
367 :
368 59654 : sts[1]->vad_flag = 0;
369 59654 : move16();
370 : }
371 :
372 : /*----------------------------------------------------------------*
373 : * dynamically allocate data structures depending on the actual stereo mode
374 : *----------------------------------------------------------------*/
375 :
376 422855 : IF( ( error = stereo_memory_enc_fx( hCPE, input_Fs, max_bwidth, ivas_format, st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
377 : {
378 0 : return error;
379 : }
380 :
381 422855 : hConfigDft = NULL;
382 422855 : IF( hCPE->hStereoDft != NULL )
383 : {
384 59654 : hConfigDft = hCPE->hStereoDft->hConfig;
385 : }
386 :
387 : /*----------------------------------------------------------------*
388 : * Set TD stereo parameters
389 : *----------------------------------------------------------------*/
390 :
391 422855 : IF( ( error = stereo_set_tdm_fx( hCPE, input_frame, sts[1]->q_inp32 ) ) != IVAS_ERR_OK )
392 : {
393 0 : return error;
394 : }
395 :
396 :
397 : /*----------------------------------------------------------------*
398 : * Resets/updates in case of stereo switching
399 : *----------------------------------------------------------------*/
400 :
401 422855 : test();
402 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
403 : {
404 746 : gb = find_guarded_bits_fx( sts[0]->encoderLookahead_FB );
405 746 : shift = L_norm_arr( sts[1]->old_input_signal32_fx, shl( input_frame, 1 ) );
406 746 : shift = s_min( shift, L_norm_arr( sts[0]->old_input_signal32_fx, shl( input_frame, 1 ) ) );
407 :
408 746 : IF( LT_16( shift, gb ) )
409 : {
410 622 : shift = sub( shift, gb );
411 622 : scale_sig32( sts[1]->input32_fx, input_frame, shift ); /* q_inp32 + shift */
412 622 : scale_sig32( sts[0]->input32_fx, input_frame, shift ); /* q_inp32 + shift */
413 622 : scale_sig32( sts[1]->old_input_signal32_fx, input_frame, shift ); /* q_old_inp32 + shift */
414 622 : scale_sig32( sts[0]->old_input_signal32_fx, input_frame, shift ); /* q_old_inp32 + shift */
415 622 : sts[1]->q_inp32 = add( sts[1]->q_inp32, shift );
416 622 : sts[0]->q_inp32 = add( sts[0]->q_inp32, shift );
417 622 : sts[1]->q_old_inp32 = add( sts[1]->q_old_inp32, shift );
418 622 : sts[0]->q_old_inp32 = add( sts[0]->q_old_inp32, shift );
419 622 : move16();
420 622 : move16();
421 622 : move16();
422 622 : move16();
423 : }
424 : }
425 :
426 422855 : stereo_switching_enc_fx( hCPE, sts[0]->old_input_signal32_fx, input_frame, sts[0]->q_old_inp32 );
427 :
428 : /*----------------------------------------------------------------*
429 : * Temporal inter-channel alignment, stereo adjustment
430 : *----------------------------------------------------------------*/
431 :
432 422855 : shift = s_min( L_norm_arr( sts[0]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) ), L_norm_arr( sts[1]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) ) );
433 422855 : q_min = add( sts[0]->q_inp32, sub( shift, find_guarded_bits_fx( input_frame ) ) );
434 422855 : scale_sig32( sts[1]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), sub( q_min, sts[1]->q_inp32 ) ); /* q_min */
435 422855 : scale_sig32( sts[0]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), sub( q_min, sts[0]->q_inp32 ) ); /* q_min */
436 422855 : sts[0]->q_inp32 = sts[1]->q_inp32 = sts[0]->q_old_inp32 = sts[1]->q_old_inp32 = q_min;
437 422855 : move16();
438 422855 : move16();
439 422855 : move16();
440 422855 : move16();
441 :
442 422855 : stereo_tca_enc_fx( hCPE, input_frame );
443 :
444 422855 : shift = sub( add( L_norm_arr( sts[0]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) ), sts[0]->q_inp32 ), 16 );
445 422855 : shift = s_min( shift, sub( add( L_norm_arr( sts[1]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) ), sts[1]->q_inp32 ), 16 ) );
446 422855 : Copy_Scale_sig32_16( sts[0]->input_buff32_fx, sts[0]->input_buff_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), sub( add( Q16, shift ), sts[0]->q_inp32 ) ); // shift
447 422855 : Copy_Scale_sig32_16( sts[1]->input_buff32_fx, sts[1]->input_buff_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), sub( add( Q16, shift ), sts[1]->q_inp32 ) ); // shift
448 :
449 422855 : sts[0]->q_inp = sts[1]->q_inp = sts[0]->q_old_inp = sts[1]->q_old_inp = shift;
450 422855 : move16();
451 422855 : move16();
452 422855 : move16();
453 422855 : move16();
454 :
455 : /*----------------------------------------------------------------*
456 : * Input signal buffering - needed in IC-BWE and TD ITD in MDCT stereo
457 : *----------------------------------------------------------------*/
458 :
459 1268565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
460 : {
461 845710 : Copy32( sts[n]->input32_fx, orig_input_fx32[n], input_frame ); /* sts->q_inp32 */
462 845710 : Copy( sts[n]->input_fx, orig_input_fx[n], input_frame ); /* sts->q_inp */
463 845710 : Scale_sig( orig_input_fx[n], input_frame, sub( -1, sts[n]->q_inp ) ); // Q(-1)
464 845710 : Q_orig_inp[n] = -1;
465 845710 : Q_orig_inp32[n] = sts[n]->q_inp32;
466 845710 : move16();
467 845710 : move16();
468 :
469 845710 : IF( hCPE->hStereoICBWE != NULL )
470 : {
471 119550 : hCPE->hStereoICBWE->dataChan_fx[n] = &orig_input_fx[n][0]; // Q_orig_inp
472 119550 : hCPE->hStereoICBWE->q_dataChan_fx = Q_orig_inp[n];
473 119550 : move16();
474 : }
475 : }
476 :
477 : /*---------------------------------------------------------------*
478 : * Time Domain Transient Detector
479 : *---------------------------------------------------------------*/
480 :
481 1268565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
482 : {
483 845710 : IF( sts[n]->hTranDet == NULL )
484 : {
485 0 : currFlatness_fx[n] = 0;
486 0 : move32();
487 0 : CONTINUE;
488 : }
489 :
490 845710 : test();
491 845710 : IF( !( EQ_32( ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) ) )
492 : {
493 824990 : RunTransientDetection_ivas_fx( sts[n]->input_fx, input_frame, sts[n]->hTranDet, sts[n]->q_inp ); // Note q of sts[n]->input_fx changes inside function
494 : }
495 :
496 845710 : currFlatness_fx[n] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( sts[n]->hTranDet, NSUBBLOCKS, 0 ); // Q21
497 845710 : move32();
498 : }
499 :
500 : /* Synchonize detection for downmix-based stereo */
501 422855 : test();
502 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
503 : {
504 63470 : set_transient_stereo_fx( hCPE, currFlatness_fx );
505 : }
506 :
507 : /*----------------------------------------------------------------*
508 : * Configuration of stereo encoder
509 : *----------------------------------------------------------------*/
510 :
511 1208911 : FOR( n = 0; n < n_CoreChannels; n++ )
512 : {
513 : /* Force to MODE1 in IVAS */
514 786056 : sts[n]->codec_mode = MODE1;
515 786056 : move16();
516 :
517 786056 : sts[n]->element_mode = hCPE->element_mode;
518 786056 : move16();
519 : }
520 :
521 422855 : test();
522 422855 : test();
523 422855 : test();
524 422855 : test();
525 422855 : test();
526 : /* No Basop - sts[0]->ini_frame == 0 as comparison with 0 */
527 422855 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && ( NE_32( hCPE->element_brate, hCPE->last_element_brate ) || NE_16( hCPE->last_element_mode, hCPE->element_mode ) || sts[0]->ini_frame == 0 ||
528 : ( NE_32( ivas_total_brate, st_ivas->hEncoderConfig->last_ivas_total_brate ) ) || LE_32( 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 */
529 : {
530 11825 : IF( st_ivas->hQMetaData != NULL )
531 : {
532 1720 : test();
533 : /* No Basop - st_ivas->ism_mode != ISM_MODE_NONE as comparison with 0 */
534 1720 : IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) && st_ivas->ism_mode != ISM_MODE_NONE )
535 : {
536 501 : stereo_dft_config_fx( hConfigDft, L_mult0( st_ivas->hQMetaData->bits_frame_nominal, 35 /* 0.70f * FRAMES_PER_SEC */ ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
537 : }
538 : ELSE
539 : {
540 1219 : stereo_dft_config_fx( hConfigDft, L_mult0( st_ivas->hQMetaData->bits_frame_nominal, FRAMES_PER_SEC ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
541 : }
542 : }
543 : ELSE
544 : {
545 : /* note; "bits_frame_nominal" needed in TD stereo as well */
546 10105 : stereo_dft_config_fx( hConfigDft, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
547 : }
548 : }
549 :
550 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
551 : {
552 3816 : IF( hCPE->hStereoTD->tdm_LRTD_flag )
553 : {
554 3695 : sts[0]->bits_frame_nominal = extract_l( Mult_32_32( L_shr( hCPE->element_brate, 1 ), ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
555 3695 : sts[1]->bits_frame_nominal = extract_l( Mult_32_32( L_shr( hCPE->element_brate, 1 ), ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
556 : }
557 : ELSE
558 : {
559 121 : stereo_dft_config_fx( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
560 : }
561 : }
562 :
563 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
564 : {
565 : /* compute bit-rate surplus per channel in combined format coding */
566 : Word32 brate_surplus[CPE_CHANNELS];
567 359385 : test();
568 359385 : test();
569 359385 : IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
570 : {
571 4274 : brate_surplus[0] = imult3216( L_shr( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ), 1 ), FRAMES_PER_SEC ); /* Q0 */
572 4274 : brate_surplus[1] = L_sub( hCPE->brate_surplus, brate_surplus[0] ); /* Q0 */
573 4274 : move32();
574 4274 : move32();
575 : }
576 :
577 : /* this is just for initialization, the true values of "total_brate" and "bits_frame_channel" are set later */
578 1078155 : FOR( n = 0; n < n_CoreChannels; n++ )
579 : {
580 718770 : IF( st_ivas->hMCT )
581 : {
582 : Word16 lfe_bits;
583 544616 : test();
584 544616 : IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
585 : {
586 169800 : lfe_bits = st_ivas->hLFE->lfe_bits;
587 169800 : move16();
588 : }
589 : ELSE
590 : {
591 374816 : lfe_bits = 0;
592 374816 : move16();
593 : }
594 544616 : sts[n]->total_brate = hCPE->element_brate; /* Q0 */
595 544616 : sts[n]->bits_frame_nominal = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
596 544616 : sts[n]->bits_frame_channel = ( idiv1616( sub( sub( extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ), lfe_bits ), nb_bits_metadata ), st_ivas->hMCT->nchan_out_woLFE ) ); /* Q0 */
597 544616 : move32();
598 544616 : move16();
599 544616 : move16();
600 : }
601 : ELSE
602 : {
603 174154 : sts[n]->bits_frame_nominal = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
604 174154 : sts[n]->bits_frame_channel = idiv1616( extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ), n_CoreChannels ); /* Q0 */
605 174154 : sts[n]->total_brate = L_shl( div_l( hCPE->element_brate, n_CoreChannels ), 1 ); /* Q0 */
606 174154 : move32();
607 174154 : move16();
608 174154 : move16();
609 :
610 : /* subtract bit-rate for combined format coding */
611 174154 : IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
612 : {
613 8548 : sts[n]->bits_frame_channel = add( sts[n]->bits_frame_channel, extract_l( Mpy_32_32_r( brate_surplus[n], ONE_BY_FRAMES_PER_SEC_Q31 ) ) ); /* Q0 */
614 8548 : sts[n]->total_brate = L_add( sts[n]->total_brate, brate_surplus[n] ); /* Q0 */
615 8548 : move32();
616 8548 : move16();
617 : }
618 : }
619 : }
620 :
621 : /* reconfiguration in case of bitrate switching */
622 359385 : IF( NE_32( hCPE->element_brate, hCPE->last_element_brate ) && st_ivas->hMCT == NULL )
623 : {
624 : #ifdef DEBUGGING
625 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = hEncoderConfig->mdct_stereo_mode_cmdl;
626 : #endif
627 1678 : initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 0 );
628 1678 : test();
629 1678 : test();
630 1678 : hCPE->hStereoMdct->isSBAStereoMode = ( ( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) ) && ( EQ_16( st_ivas->nchan_transport, 2 ) ) ); /* Q0 */
631 1678 : move16();
632 : }
633 : }
634 :
635 : /*----------------------------------------------------------------*
636 : * Stereo processing
637 : * Stereo down-mix
638 : *----------------------------------------------------------------*/
639 :
640 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
641 : {
642 59654 : test();
643 59654 : if ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
644 : {
645 17588 : sts[1]->exp_old_inp_12k8 = Q15;
646 17588 : move16();
647 : }
648 59654 : Word16 temp = getScaleFactor32( hCPE->hStereoDft->Spd_L_smooth_fx, STEREO_DFT_N_32k_ENC / 2 );
649 59654 : scale_sig32( hCPE->hStereoDft->Spd_L_smooth_fx, STEREO_DFT_N_32k_ENC / 2, temp ); /* exp(hCPE->hStereoDft->Spd_L_smooth_fx_e - temp)*/
650 59654 : hCPE->hStereoDft->Spd_L_smooth_fx_e = sub( hCPE->hStereoDft->Spd_L_smooth_fx_e, temp );
651 59654 : move16();
652 :
653 59654 : temp = getScaleFactor32( hCPE->hStereoDft->Spd_R_smooth_fx, STEREO_DFT_N_32k_ENC / 2 );
654 59654 : scale_sig32( hCPE->hStereoDft->Spd_R_smooth_fx, STEREO_DFT_N_32k_ENC / 2, temp ); /* exp(hCPE->hStereoDft->Spd_R_smooth_fx_e - temp) */
655 59654 : hCPE->hStereoDft->Spd_R_smooth_fx_e = sub( hCPE->hStereoDft->Spd_R_smooth_fx_e, temp );
656 59654 : move16();
657 :
658 59654 : q_com = MAX_16;
659 59654 : move16();
660 178962 : FOR( n = 0; n < CPE_CHANNELS; n++ )
661 : {
662 119308 : q_com = s_min( q_com, sub( add( L_norm_arr( sts[n]->input32_fx, input_frame ), sts[n]->q_inp32 ), 16 ) );
663 119308 : q_com = s_min( q_com, sub( add( L_norm_arr( sts[n]->old_input_signal32_fx, input_frame ), sts[n]->q_old_inp32 ), 16 ) );
664 119308 : q_com = s_min( q_com, add( norm_arr( hCPE->input_mem_fx[n], hCPE->hStereoDft->dft_ovl ), hCPE->q_input_mem[n] ) );
665 :
666 119308 : if ( EQ_16( q_com, Q15 ) )
667 : {
668 0 : q_com = 0;
669 0 : move16();
670 : }
671 : }
672 178962 : FOR( n = 0; n < CPE_CHANNELS; n++ )
673 : {
674 119308 : Copy_Scale_sig_32_16( sts[n]->input32_fx, sts[n]->input_fx, input_frame, sub( q_com, sts[n]->q_inp32 ) );
675 119308 : sts[n]->q_inp = q_com;
676 119308 : move16();
677 :
678 119308 : Copy_Scale_sig_32_16( sts[n]->old_input_signal32_fx, sts[n]->old_input_signal_fx, input_frame, sub( q_com, sts[n]->q_old_inp32 ) );
679 119308 : sts[n]->q_old_inp = q_com;
680 119308 : move16();
681 :
682 119308 : scale_sig( hCPE->input_mem_fx[n], hCPE->hStereoDft->dft_ovl, sub( q_com, hCPE->q_input_mem[n] ) );
683 119308 : hCPE->q_input_mem[n] = q_com;
684 119308 : move16();
685 : }
686 :
687 59654 : stereo_dft_hybrid_ITD_flag_fx( hConfigDft, input_Fs, hCPE->hStereoDft->hItd->hybrid_itd_max );
688 :
689 : /* Time Domain ITD compensation using extrapolation */
690 59654 : stereo_td_itd_fx( hCPE->hStereoDft->hItd, hCPE->hStereoDft->input_mem_itd_fx, hCPE->hStereoDft->q_input_mem_itd, hConfigDft->hybrid_itd_flag, hCPE->hStereoDft->dft_ovl, sts, input_frame, hCPE->input_mem_fx, hCPE->q_input_mem );
691 :
692 59654 : stereo_dft_enc_analyze_fx( sts, CPE_CHANNELS, input_frame, hCPE->hStereoDft, NULL, hCPE->hStereoDft->DFT_fx, hCPE->hStereoDft->DFT_fx_e, hCPE->input_mem_fx, hCPE->q_input_mem );
693 :
694 59654 : sts[0]->total_brate = L_mult0( add( sts[0]->bits_frame_nominal, 10 ), FRAMES_PER_SEC ); /* add small overhead; st[0]->total_brate used in coder_type_modif() Q0*/
695 : /* Update DFT Stereo memories */
696 :
697 59654 : stereo_dft_enc_update_fx( hCPE->hStereoDft, max_bwidth );
698 :
699 : /* DFT stereo processing */
700 59654 : stereo_dft_enc_process_fx( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
701 : }
702 363201 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
703 : {
704 3816 : Scale_sig( sts[0]->input_fx, input_frame, negate( sts[0]->q_inp ) ); /* Q0 */
705 3816 : sts[0]->q_inp = 0;
706 3816 : move16();
707 3816 : Scale_sig( sts[1]->input_fx, input_frame, negate( sts[1]->q_inp ) ); /* Q0 */
708 3816 : sts[1]->q_inp = 0;
709 3816 : move16();
710 :
711 : /* Determine the energy ratio between the 2 channels */
712 3816 : tdm_ratio_idx = stereo_tdm_ener_analysis_fx(
713 : ivas_format,
714 : hCPE, input_frame, &tdm_SM_or_LRTD_Pri, &tdm_ratio_idx_SM ); /* Q0 */
715 :
716 : /* Compute the downmix signal based on the ratio index */
717 :
718 : Word16 tdm_SM_flag;
719 3816 : IF( hCPE->hStereoTD->tdm_LRTD_flag == 0 )
720 : {
721 121 : tdm_SM_flag = tdm_SM_or_LRTD_Pri; /* Q0 */
722 121 : move16();
723 : }
724 : ELSE
725 : {
726 3695 : tdm_SM_flag = 0;
727 3695 : move16();
728 : }
729 3816 : stereo_tdm_downmix_ivas_fx( hCPE->hStereoTD, sts[0]->input_fx, sts[1]->input_fx, input_frame, tdm_ratio_idx, tdm_SM_flag, tdm_ratio_idx_SM );
730 3816 : Copy_Scale_sig_16_32_no_sat( sts[0]->input_fx, sts[0]->input32_fx, input_frame, sub( sts[0]->q_inp32, sts[0]->q_inp ) );
731 3816 : Copy_Scale_sig_16_32_no_sat( sts[1]->input_fx, sts[1]->input32_fx, input_frame, sub( sts[1]->q_inp32, sts[1]->q_inp ) );
732 :
733 : /* signal the bitrate for BW selection in the SCh */
734 3816 : sts[0]->bits_frame_channel = 0;
735 3816 : sts[1]->bits_frame_channel = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
736 3816 : sts[1]->bits_frame_channel = add( sts[1]->bits_frame_channel, extract_l( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) ) ); /* Q0 */
737 3816 : move16();
738 3816 : move16();
739 3816 : move16();
740 3816 : if ( st_ivas->hQMetaData != NULL )
741 : {
742 60 : sts[1]->bits_frame_channel = sub( sts[1]->bits_frame_channel, st_ivas->hQMetaData->metadata_max_bits ); /* Q0 */
743 60 : move16();
744 : }
745 :
746 3816 : Etot_last_fx[0] = extract_h( sts[0]->hNoiseEst->Etot_last_32fx ); /* Q8 */
747 3816 : Etot_last_fx[1] = extract_h( sts[1]->hNoiseEst->Etot_last_32fx ); /* Q8 */
748 3816 : move16();
749 3816 : move16();
750 : }
751 359385 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
752 : {
753 359385 : test();
754 359385 : IF( hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
755 : {
756 14690 : test();
757 14690 : if ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
758 : {
759 0 : sts[1]->exp_old_inp_12k8 = Q15;
760 0 : move16();
761 : }
762 :
763 14690 : q_min = sts[1]->q_old_inp;
764 14690 : move16();
765 14690 : q_min = s_min( q_min, sts[1]->q_inp );
766 14690 : q_min = s_min( q_min, sts[0]->q_old_inp );
767 14690 : q_min = s_min( q_min, sts[0]->q_inp );
768 14690 : q_min = s_min( q_min, hCPE->q_input_mem[0] );
769 14690 : q_min = s_min( q_min, hCPE->q_input_mem[1] );
770 :
771 14690 : shift = norm_arr( sts[1]->old_input_signal_fx, input_frame );
772 14690 : shift = s_min( shift, norm_arr( sts[1]->input_fx, input_frame ) );
773 14690 : shift = s_min( shift, norm_arr( sts[0]->old_input_signal_fx, input_frame ) );
774 14690 : shift = s_min( shift, norm_arr( sts[0]->input_fx, input_frame ) );
775 14690 : shift = s_min( shift, norm_arr( hCPE->input_mem_fx[0], hCPE->hStereoMdct->hDft_ana->dft_ovl ) );
776 14690 : shift = s_min( shift, norm_arr( hCPE->input_mem_fx[1], hCPE->hStereoMdct->hDft_ana->dft_ovl ) );
777 :
778 14690 : q_min = add( q_min, shift );
779 :
780 14690 : scale_sig( sts[1]->input_fx, input_frame, sub( q_min, sts[1]->q_inp ) ); /* q_min */
781 14690 : scale_sig( sts[1]->old_input_signal_fx, input_frame, sub( q_min, sts[1]->q_old_inp ) ); /* q_min */
782 14690 : scale_sig( sts[0]->input_fx, input_frame, sub( q_min, sts[0]->q_inp ) ); /* q_min */
783 14690 : scale_sig( sts[0]->old_input_signal_fx, input_frame, sub( q_min, sts[0]->q_old_inp ) ); /* q_min */
784 14690 : scale_sig( hCPE->input_mem_fx[0], hCPE->hStereoMdct->hDft_ana->dft_ovl, sub( q_min, hCPE->q_input_mem[0] ) ); /* q_min */
785 14690 : scale_sig( hCPE->input_mem_fx[1], hCPE->hStereoMdct->hDft_ana->dft_ovl, sub( q_min, hCPE->q_input_mem[1] ) ); /* q_min */
786 14690 : hCPE->q_input_mem[1] = q_min;
787 14690 : hCPE->q_input_mem[0] = q_min;
788 14690 : sts[1]->q_old_inp = q_min;
789 14690 : sts[0]->q_old_inp = q_min;
790 14690 : sts[1]->q_inp = q_min;
791 14690 : sts[0]->q_inp = q_min;
792 14690 : move16();
793 14690 : move16();
794 14690 : move16();
795 14690 : move16();
796 14690 : move16();
797 14690 : move16();
798 : }
799 :
800 359385 : stereo_td_itd_mdct_stereo_fx( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
801 : }
802 :
803 : /*----------------------------------------------------------------*
804 : * DFT stereo: iDFT and resampling on both channels
805 : *----------------------------------------------------------------*/
806 :
807 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
808 : {
809 : Word32 internal_Fs;
810 :
811 59654 : IF( getTcxonly_ivas_fx( IVAS_CPE_MDCT, imult1616( sts[0]->bits_frame_nominal, FRAMES_PER_SEC ), 0, sts[0]->is_ism_format ) == 0 )
812 : {
813 59654 : internal_Fs = INT_FS_16k;
814 59654 : move32();
815 : }
816 : ELSE
817 : {
818 0 : internal_Fs = L_max( INT_FS_16k, sts[0]->sr_core );
819 : }
820 :
821 : /* iDFT at input sampling rate */
822 : Word16 out_start_ind, out_end_ind;
823 : Word16 out_12k8_start_ind[CPE_CHANNELS], out_12k8_end_ind[CPE_CHANNELS];
824 59654 : Word16 out_16k_start_ind = 0, out_16k_end_ind = 0;
825 59654 : move16();
826 59654 : move16();
827 :
828 59654 : stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, sts[0]->input32_fx, &out_start_ind, &out_end_ind, 0, input_Fs, input_Fs, 0, NULL );
829 :
830 59654 : scale_sig32( sts[0]->input32_fx - input_frame, add( out_start_ind, input_frame ), sub( Q15, sts[0]->q_old_inp32 ) ); // scaling initial part of the input buffer
831 59654 : scale_sig32( sts[0]->input32_fx + out_end_ind, sub( input_frame, out_end_ind ), sub( Q15, sts[0]->q_inp32 ) ); // scaling initial part of the input buffer
832 59654 : sts[0]->q_inp32 = Q15;
833 59654 : sts[0]->q_old_inp32 = Q15;
834 59654 : move16();
835 59654 : move16();
836 :
837 : // Normalise the input buffer from Q15
838 : Word16 input_norm, q_inp; //, common_q, fir_delay_len;
839 59654 : input_norm = L_norm_arr( sts[0]->input32_fx - input_frame, shl( input_frame, 1 ) );
840 59654 : q_inp = sub( add( Q15, input_norm ), 16 );
841 :
842 : // Rescale the old input, input and FIR delay section of input buffer
843 59654 : Copy_Scale_sig32_16( sts[0]->input32_fx - input_frame, sts[0]->input_fx - input_frame, shl( input_frame, 1 ), sub( add( Q16, q_inp ), Q15 ) ); // Q15 -> q_inp
844 :
845 : // Update the Q-factors
846 59654 : sts[0]->q_inp = q_inp;
847 59654 : move16();
848 59654 : sts[0]->q_old_inp = q_inp;
849 59654 : move16();
850 :
851 : /* iDFT & resampling to 12.8kHz internal sampling rate */
852 59654 : stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, old_inp_12k8_fx[0] + L_INP_MEM, &out_12k8_start_ind[0], &out_12k8_end_ind[0], 0, input_Fs, INT_FS_12k8, 0, NULL );
853 59654 : Copy_Scale_sig_32_16( old_inp_12k8_fx[0], old_inp_12k8_16fx[0], L_INP_12k8, -Q16 ); // Q-1
854 :
855 : /* iDFT & resampling to 16kHz internal sampling rate for M channel */
856 59654 : IF( EQ_32( input_Fs, internal_Fs ) )
857 : {
858 11547 : Copy32( sts[0]->input32_fx - STEREO_DFT_OVL_16k, old_inp_16k_fx[0] + L_INP_MEM - STEREO_DFT_OVL_16k, add( input_frame, STEREO_DFT_OVL_16k ) ); /* sts[0]->q_inp32 */
859 11547 : out_16k_start_ind = -STEREO_DFT_OVL_16k;
860 11547 : out_16k_end_ind = add( out_16k_start_ind, add( input_frame, STEREO_DFT_OVL_16k ) );
861 11547 : move16();
862 11547 : move16();
863 : }
864 : ELSE
865 : {
866 48107 : stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, old_inp_16k_fx[0] + L_INP_MEM, &out_16k_start_ind, &out_16k_end_ind, 0, input_Fs, internal_Fs, 0, NULL );
867 : }
868 59654 : Copy_Scale_sig32_16( old_inp_16k_fx[0], old_inp_16k_16fx[0], L_INP, 0 ); // Q15->Q-1
869 :
870 : /* DFT Stereo: iDFT of residual signal at 8kHz sampling rate */
871 59654 : test();
872 59654 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
873 : {
874 19964 : Copy_Scale_sig_16_32_no_sat( sts[1]->old_inp_12k8_fx, old_inp_12k8_fx[1], L_INP_MEM, 15 - 0 ); // Q15
875 19964 : stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, old_inp_12k8_fx[1] + L_INP_MEM, &out_12k8_start_ind[1], &out_12k8_end_ind[1], 1, input_Fs, 8000, 0, NULL );
876 :
877 : /* update old input signal buffer */
878 19964 : Copy_Scale_sig_32_16( old_inp_12k8_fx[1] + L_FRAME8k, sts[1]->old_inp_12k8_fx, L_INP_MEM, 0 - 15 ); // Q0
879 19964 : sts[1]->exp_old_inp_12k8 = Q15;
880 19964 : move16();
881 19964 : Copy_Scale_sig_32_16( old_inp_12k8_fx[1], old_inp_12k8_16fx[1], L_INP_12k8, -Q16 ); // Q-1
882 : }
883 :
884 : /* no iDFT at input sampling rate for Side channel -> reset the buffer */
885 59654 : set16_fx( sts[1]->input_fx, 0, input_frame );
886 59654 : sts[1]->q_inp = Q15;
887 59654 : move16();
888 59654 : set32_fx( sts[1]->input32_fx, 0, input_frame );
889 59654 : sts[1]->q_inp32 = Q31;
890 59654 : move16();
891 : }
892 :
893 : #ifdef DEBUG_MODE_INFO
894 : dbgwrite( &hCPE->element_mode, sizeof( int16_t ), 1, input_frame, fname( debug_dir, "element_mode", 0, sts[0]->id_element, ENC ) );
895 : #endif
896 :
897 : /*----------------------------------------------------------------*
898 : * Front Pre-processing
899 : *----------------------------------------------------------------*/
900 :
901 1208911 : FOR( n = 0; n < n_CoreChannels; n++ )
902 : {
903 786056 : set16_fx( old_wsp_fx[n], 0, L_WSP );
904 786056 : q_old_wsp = Q15;
905 786056 : move16();
906 : Word16 front_vad_flag;
907 786056 : IF( st_ivas->hSpar != NULL )
908 : {
909 450736 : front_vad_flag = st_ivas->hSpar->front_vad_flag;
910 : }
911 : ELSE
912 : {
913 335320 : front_vad_flag = 0;
914 : }
915 :
916 786056 : error = pre_proc_front_ivas_fx( NULL, hCPE, hCPE->element_brate, nb_bits_metadata, input_frame, n, old_inp_12k8_16fx[n], old_inp_16k_16fx[n],
917 786056 : &ener_fx[n], &relE_fx[n], A_fx[n], Aw_fx[n], epsP_fx[n], &epsP_fx_q[n], lsp_new_fx[n], lsp_mid_fx[n], &vad_hover_flag[n], &attack_flag[n],
918 786056 : realBuffer_fx[n], imagBuffer_fx[n], &q_re_im_buf[n], old_wsp_fx[n], &q_old_wsp, pitch_fr_fx[n], voicing_fr_fx[n], &loc_harm[n], &cor_map_sum_fx[n], &vad_flag_dtx[n], enerBuffer_fx[n], &enerBuffer_fx_exp[n],
919 786056 : fft_buff_fx[n], &fft_buff_fx_q[n], A_fx[0], lsp_new_fx[0], currFlatness_fx[n], tdm_ratio_idx, fr_bands_fx, q_fr_bands, Etot_LR_fx, lf_E_fx, q_lf_E[n], localVAD_HE_SAD,
920 786056 : band_energies_LR_fx, q_band_energies_LR, 0, front_vad_flag, 0, 0, ivas_format, st_ivas->hMCT != NULL, st_ivas->hEncoderConfig->last_ivas_total_brate, ivas_total_brate, &Q_new[n]
921 : #ifdef DEBUG_MODE_INFO
922 : ,
923 : ( st_ivas->nSCE + ( cpe_id * CPE_CHANNELS ) + n )
924 : #endif
925 : );
926 786056 : e_old_wsp[n] = sub( Q15, q_old_wsp );
927 786056 : move16();
928 786056 : IF( error != IVAS_ERR_OK )
929 : {
930 0 : return error;
931 : }
932 : }
933 :
934 : /*making Q common*/
935 1208911 : FOR( n = 0; n < n_CoreChannels; n++ )
936 : {
937 786056 : fft_buff_fx_final_q = s_min( fft_buff_fx_final_q, fft_buff_fx_q[n] );
938 : }
939 422855 : fft_buff_fx_final_q = sub( fft_buff_fx_final_q, 2 ); // headroom
940 1208911 : FOR( n = 0; n < n_CoreChannels; n++ )
941 : {
942 786056 : Scale_sig( fft_buff_fx[n], 2 * L_FFT, sub( fft_buff_fx_final_q, fft_buff_fx_q[n] ) ); /* fft_buff_fx_final_q */
943 : }
944 :
945 : /* sanity check -> DTX not supported for more than one SCEs/CPEs */
946 422855 : IF( GT_16( add( st_ivas->nSCE, st_ivas->nCPE ), 1 ) )
947 : {
948 279991 : test();
949 279991 : IF( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA )
950 : {
951 48 : sts[0]->core_brate = -1; /* Q0 */
952 48 : sts[0]->total_brate = hCPE->element_brate; /* Q0 */
953 48 : move32();
954 48 : move32();
955 : }
956 : }
957 :
958 : /*----------------------------------------------------------------*
959 : * Stereo DTX updates
960 : *----------------------------------------------------------------*/
961 :
962 422855 : test();
963 422855 : test();
964 422855 : IF( EQ_16( ivas_format, MASA_FORMAT ) && nb_bits_metadata > 0 && hCPE->hCoreCoder[0]->Opt_DTX_ON )
965 : {
966 4016 : test();
967 4016 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
968 : {
969 2368 : reset_metadata_spatial_fx( ivas_format, hCPE->hMetaData, hCPE->element_brate, &tmp, sts[0]->core_brate, nb_bits_metadata );
970 : }
971 : }
972 :
973 : /* MDCT stereo DTX: active/inactive frame decision; compute FD CNG coherence */
974 422855 : test();
975 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && hEncoderConfig->Opt_DTX_ON )
976 : {
977 18228 : stereoFdCngCoherence_fx( sts, hCPE->last_element_mode, fft_buff_fx, sub( 15, fft_buff_fx_final_q ) /*fft exp*/ );
978 :
979 : /* Reset metadata */
980 18228 : test();
981 18228 : IF( sts[0]->cng_sba_flag || EQ_32( ivas_format, SBA_FORMAT ) )
982 : {
983 8278 : reset_metadata_spatial_fx( ivas_format, hCPE->hMetaData, hCPE->element_brate, &tmp, sts[0]->core_brate, nb_bits_metadata );
984 : }
985 : }
986 :
987 : /*----------------------------------------------------------------*
988 : * Core codec configuration
989 : *----------------------------------------------------------------*/
990 :
991 : #ifdef FIX_1411_IGF_CRASH_BW_SWITCHING
992 422855 : test();
993 422855 : test();
994 422855 : IF( NE_32( sts[0]->core_brate, SID_2k40 ) && NE_32( sts[0]->core_brate, FRAME_NO_DATA ) ) /* Reconfigurations not needed with DTX*/
995 : {
996 : #endif
997 409177 : test();
998 409177 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && st_ivas->hMCT == NULL )
999 : {
1000 : /* set coded BW for MDCT stereo */
1001 83793 : set_bw_stereo_fx( hCPE );
1002 :
1003 : /* reconfiguration of MDCT stereo */
1004 83793 : test();
1005 83793 : test();
1006 83793 : test();
1007 83793 : 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 ) )
1008 : {
1009 729 : initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, sts[0]->bwidth, 0, NULL, 0 );
1010 :
1011 729 : test();
1012 729 : test();
1013 729 : test();
1014 729 : hCPE->hStereoMdct->isSBAStereoMode = ( ( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) ) && ( EQ_16( st_ivas->nchan_transport, 2 ) ) ); /* Q0 */
1015 729 : move16();
1016 :
1017 729 : test();
1018 729 : IF( LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) && EQ_16( ivas_format, STEREO_FORMAT ) )
1019 : {
1020 84 : IF( ( error = initMdctItdHandling_fx( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
1021 : {
1022 0 : return error;
1023 : }
1024 : }
1025 : }
1026 : }
1027 :
1028 : /* IGF reconfiguration */
1029 1178271 : FOR( n = 0; n < n_CoreChannels; n++ )
1030 : {
1031 769094 : test();
1032 769094 : test();
1033 769094 : test();
1034 769094 : test();
1035 769094 : test();
1036 769094 : test();
1037 : #ifdef FIX_1411_IGF_CRASH_BW_SWITCHING
1038 769094 : test();
1039 769094 : test();
1040 769094 : IF( ( NE_32( hCPE->last_element_brate, hCPE->element_brate ) || NE_16( hCPE->element_mode, hCPE->last_element_mode ) || ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && NE_16( sts[0]->bits_frame_nominal, last_bits_frame_nominal ) ) || NE_16( sts[n]->last_bwidth, sts[n]->bwidth ) || EQ_32( sts[n]->last_core_brate, SID_2k40 ) || EQ_32( sts[n]->last_core_brate, FRAME_NO_DATA ) ) && ( EQ_16( n, 0 ) || EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) ) )
1041 : #else
1042 : IF( ( NE_32( hCPE->last_element_brate, hCPE->element_brate ) || NE_16( hCPE->element_mode, hCPE->last_element_mode ) || ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && NE_16( sts[0]->bits_frame_nominal, last_bits_frame_nominal ) ) || NE_16( sts[n]->last_bwidth, sts[n]->bwidth ) ) && ( n == 0 || EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) ) )
1043 : #endif
1044 : {
1045 : Word16 igf;
1046 : Word16 bw;
1047 :
1048 : /* float uses ternary operator for this: bw = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? sts[n]->bwidth : sts[n]->max_bwidth; */
1049 9777 : bw = max_bwidth;
1050 9777 : move16();
1051 9777 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1052 : {
1053 7942 : bw = sts[n]->bwidth;
1054 7942 : move16();
1055 : }
1056 9777 : igf = getIgfPresent_fx( sts[n]->element_mode, L_mult0( sts[n]->bits_frame_nominal, FRAMES_PER_SEC ), bw, sts[n]->rf_mode ); /* Q0 */
1057 9777 : move16();
1058 : #ifdef FIX_1413_IGF_INIT_PRINTOUT
1059 9777 : IF( ( error = IGF_Reconfig_fx( &sts[n]->hIGFEnc, igf, 0, L_mult0( sts[n]->bits_frame_nominal, FRAMES_PER_SEC ), bw, sts[n]->element_mode, sts[n]->rf_mode ) ) != IVAS_ERR_OK )
1060 : #else
1061 : IF( ( error = IGF_Reconfig_fx( &sts[n]->hIGFEnc, igf, 0, L_mult0( sts[n]->bits_frame_nominal, FRAMES_PER_SEC ), max_bwidth, sts[n]->element_mode, sts[n]->rf_mode ) ) != IVAS_ERR_OK )
1062 : #endif
1063 :
1064 : {
1065 0 : return error;
1066 : }
1067 : }
1068 : }
1069 : #ifdef FIX_1411_IGF_CRASH_BW_SWITCHING
1070 : }
1071 : #endif
1072 :
1073 : /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
1074 1208911 : FOR( n = 0; n < n_CoreChannels; n++ )
1075 : {
1076 786056 : test();
1077 786056 : test();
1078 786056 : IF( ( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA ) && EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1079 : {
1080 10394 : sts[n]->flag_ACELP16k = set_ACELP_flag_IVAS( hCPE->element_mode, hCPE->element_brate, sts[n]->core_brate, n, sts[0]->tdm_LRTD_flag, sts[n]->bwidth, sts[n]->cng_type ); /* Q0 */
1081 10394 : move16();
1082 : }
1083 : ELSE
1084 : {
1085 775662 : sts[n]->flag_ACELP16k = set_ACELP_flag_IVAS( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, sts[0]->tdm_LRTD_flag, sts[n]->bwidth, sts[n]->cng_type ); /* Q0 */
1086 775662 : move16();
1087 : }
1088 : }
1089 :
1090 : /* configure TD stereo encoder */
1091 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
1092 : {
1093 3816 : tdm_ol_pitch_comparison_fx( hCPE, pitch_fr_fx, voicing_fr_fx );
1094 3816 : tdm_configure_enc_fx( ivas_format, st_ivas->ism_mode, hCPE, Etot_last_fx, tdm_SM_or_LRTD_Pri, tdm_ratio_idx, tdm_ratio_idx_SM, attack_flag[0], nb_bits_metadata );
1095 :
1096 3816 : IF( hEncoderConfig->Opt_DTX_ON )
1097 : {
1098 1668 : stereo_cng_upd_counters_fx( hCPE->hStereoCng, hCPE->element_mode, -1, NULL, sts[0]->hTdCngEnc->burst_ho_cnt, NULL );
1099 : }
1100 : }
1101 :
1102 : /* modify the coder_type depending on the total_brate per channel */
1103 1208911 : FOR( n = 0; n < n_CoreChannels; n++ )
1104 : {
1105 786056 : test();
1106 786056 : test();
1107 786056 : IF( ( NE_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->element_mode, IVAS_CPE_TD ) ) || n == 0 ) /* modify coder_type of primary channel */
1108 : {
1109 : /* limit coder_type depending on the bitrate */
1110 782240 : coder_type_modif_ivas_fx( sts[n], relE_fx[n] );
1111 : }
1112 : }
1113 :
1114 : /*----------------------------------------------------------------*
1115 : * Write IVAS format signaling in SID frames
1116 : *----------------------------------------------------------------*/
1117 :
1118 422855 : IF( EQ_32( sts[0]->core_brate, SID_2k40 ) )
1119 : {
1120 2004 : ivas_write_format_sid_fx( ivas_format, hCPE->element_mode, sts[0]->hBstr, hEncoderConfig->sba_order, hEncoderConfig->sba_planar );
1121 : }
1122 :
1123 : /*----------------------------------------------------------------*
1124 : * DFT Stereo residual coding
1125 : * DFT Stereo parameters writing into the bitstream
1126 : *----------------------------------------------------------------*/
1127 :
1128 422855 : cpe_brate = 0;
1129 422855 : move16();
1130 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1131 : {
1132 59654 : IF( hEncoderConfig->Opt_DTX_ON )
1133 : {
1134 30300 : test();
1135 30300 : IF( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA )
1136 : {
1137 : /* Reconfigure DFT Stereo for inactive frames */
1138 10394 : IF( EQ_32( sts[0]->core_brate, SID_2k40 ) )
1139 : {
1140 1533 : stereo_dft_config_fx( hConfigDft, IVAS_SID_5k2, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
1141 : }
1142 : ELSE
1143 : {
1144 8861 : stereo_dft_config_fx( hConfigDft, FRAME_NO_DATA, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
1145 : }
1146 :
1147 10394 : stereo_dft_cng_side_gain_fx( hCPE->hStereoDft, hCPE->hStereoCng, sts[0]->core_brate, sts[0]->last_core_brate, sts[0]->bwidth );
1148 : }
1149 : ELSE
1150 : {
1151 19906 : stereo_cng_upd_counters_fx( hCPE->hStereoCng, hCPE->element_mode, hCPE->hStereoDft->nbands, hCPE->hStereoDft->sidSideGain_fx, sts[0]->hTdCngEnc->burst_ho_cnt, &hCPE->hStereoDft->coh_fade_counter );
1152 : }
1153 : }
1154 :
1155 : /* Write stereo bitstream */
1156 59654 : cpe_brate = st_ivas->hCPE[0]->element_brate; /* Q0 */
1157 59654 : move16();
1158 :
1159 : /* DFT stereo side bits */
1160 59654 : test();
1161 59654 : test();
1162 59654 : test();
1163 59654 : test();
1164 59654 : test();
1165 59654 : test();
1166 59654 : test();
1167 59654 : IF( ( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) ) && LT_32( cpe_brate, MASA_STEREO_MIN_BITRATE ) && NE_32( sts[0]->core_brate, SID_2k40 ) && sts[0]->core_brate != FRAME_NO_DATA )
1168 : {
1169 5639 : nb_bits = 0; /* Only mono downmix is transmitted in this case */
1170 5639 : move16();
1171 : }
1172 54015 : ELSE IF( ( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) ) && ( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA ) )
1173 : {
1174 528 : nb_bits = hCPE->hMetaData->nb_bits_tot; /* Q0 */
1175 528 : move16();
1176 : }
1177 : ELSE
1178 : {
1179 53487 : stereo_dft_enc_write_BS_fx( hCPE, &nb_bits );
1180 : }
1181 :
1182 : /* Residual coding in MDCT domain */
1183 59654 : test();
1184 59654 : test();
1185 59654 : IF( !( ( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) ) && ( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA ) ) )
1186 : {
1187 59126 : Word16 max_bits = extract_l( W_extract_h( W_sub( W_mult_32_32( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ), W_mult_32_32( L_deposit_l( sts[0]->bits_frame_nominal ), 1717986918 /* 0.8f in Q31 */ ) ) ) );
1188 59126 : test();
1189 59126 : IF( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) )
1190 : {
1191 11396 : max_bits = sub( max_bits, nb_bits_metadata );
1192 11396 : IF( hCPE->brate_surplus < 0 )
1193 : {
1194 2293 : max_bits = add( max_bits, extract_l( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) ) );
1195 : }
1196 : }
1197 :
1198 177378 : FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
1199 : {
1200 118252 : Copy_Scale_sig_16_32_no_sat( old_inp_12k8_16fx[i], old_inp_12k8_fx[i], L_INP_12k8, Q16 ); // Q(-1) -> Q15
1201 : }
1202 59126 : stereo_dft_enc_res_fx( hCPE->hStereoDft, old_inp_12k8_fx[1] + L_INP_MEM - STEREO_DFT_OVL_8k, hCPE->hMetaData, &nb_bits, max_bits );
1203 : }
1204 :
1205 59654 : IF( sts[0]->core_brate == FRAME_NO_DATA || EQ_32( sts[0]->core_brate, SID_2k40 ) )
1206 : {
1207 10394 : assert( ( nb_bits <= ( ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS ) ) && "Stereo DFT CNG: bit budget is violated" );
1208 : }
1209 : ELSE
1210 : {
1211 : /* Flexible total bitrate in M channel */
1212 49260 : sts[0]->total_brate = L_sub( hCPE->element_brate, L_deposit_l( imult1616( nb_bits, FRAMES_PER_SEC ) ) );
1213 49260 : move32();
1214 : }
1215 :
1216 : /* subtract metadata bitbudget */
1217 59654 : sts[0]->total_brate = L_sub( sts[0]->total_brate, L_deposit_l( imult1616( nb_bits_metadata, FRAMES_PER_SEC ) ) );
1218 59654 : move32();
1219 :
1220 : /* subtract bit-rate for combined format coding */
1221 59654 : test();
1222 59654 : test();
1223 59654 : test();
1224 59654 : if ( EQ_16( ivas_format, MASA_ISM_FORMAT ) && ( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) ) )
1225 : {
1226 2784 : sts[0]->total_brate = L_add( sts[0]->total_brate, hCPE->brate_surplus );
1227 2784 : move32();
1228 : }
1229 : }
1230 :
1231 : /*----------------------------------------------------------------*
1232 : * Core Encoder
1233 : *----------------------------------------------------------------*/
1234 :
1235 1208911 : FOR( n = 0; n < n_CoreChannels; n++ )
1236 : {
1237 786056 : Scale_sig( old_wsp_fx[n], L_WSP, sub( e_old_wsp[n], Q16 ) ); /* exp(16) */
1238 786056 : e_old_wsp[n] = 16;
1239 786056 : move16();
1240 : }
1241 422855 : IF( NE_32( ( error = ivas_core_enc_fx( NULL, hCPE, st_ivas->hMCT, n_CoreChannels, old_inp_12k8_16fx, old_inp_16k_16fx, Q_new, ener_fx, A_fx, Aw_fx, epsP_fx, epsP_fx_q, lsp_new_fx, lsp_mid_fx, vad_hover_flag, attack_flag, realBuffer_fx, imagBuffer_fx, q_re_im_buf, old_wsp_fx, e_old_wsp, loc_harm, cor_map_sum_fx, vad_flag_dtx, enerBuffer_fx, enerBuffer_fx_exp, fft_buff_fx, tdm_SM_or_LRTD_Pri, ivas_format, 0 ) ), IVAS_ERR_OK ) )
1242 : {
1243 0 : return error;
1244 : }
1245 :
1246 : /*----------------------------------------------------------------*
1247 : * Common updates
1248 : *----------------------------------------------------------------*/
1249 :
1250 422855 : hCPE->last_element_brate = hCPE->element_brate; /* Q0 */
1251 422855 : move32();
1252 422855 : hCPE->last_element_mode = hCPE->element_mode; /* Q0 */
1253 422855 : move16();
1254 :
1255 422855 : if ( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
1256 : {
1257 9000 : hCPE->element_brate = element_brate_ref; /* Q0 */
1258 9000 : move32();
1259 : }
1260 :
1261 422855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
1262 : {
1263 : /* update input samples buffer */
1264 44070 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1265 : {
1266 29380 : Copy32( orig_input_fx32[n], sts[n]->old_input_signal32_fx, input_frame ); /* Q_orig_inp32 */
1267 29380 : sts[n]->q_old_inp32 = Q_orig_inp32[n];
1268 29380 : move16();
1269 29380 : Copy( orig_input_fx[n], sts[n]->old_input_signal_fx, input_frame ); /* Q_orig_inp */
1270 29380 : sts[n]->q_old_inp = Q_orig_inp[n];
1271 29380 : move16();
1272 : }
1273 : }
1274 408165 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1275 : {
1276 59654 : Copy( sts[0]->input_fx, sts[0]->old_input_signal_fx, input_frame ); /* sts[n]->q_inp */
1277 59654 : sts[0]->q_old_inp = sts[0]->q_inp;
1278 59654 : move16();
1279 59654 : Copy32( sts[0]->input32_fx, sts[0]->old_input_signal32_fx, input_frame ); /* st->q_inp32 */
1280 59654 : sts[0]->q_old_inp32 = sts[0]->q_inp32;
1281 59654 : move16();
1282 : }
1283 348511 : ELSE IF( st_ivas->hMCT == NULL ) /* note: in MCT, input buffers are updated later in ivas_mct_enc() */
1284 : {
1285 : /* update input samples buffer */
1286 228609 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1287 : {
1288 152406 : Copy( sts[n]->input_fx, sts[n]->old_input_signal_fx, input_frame ); /* sts[n]->q_inp */
1289 152406 : sts[n]->q_old_inp = sts[n]->q_inp;
1290 152406 : move16();
1291 152406 : Copy32( sts[n]->input32_fx, sts[n]->old_input_signal32_fx, input_frame ); /* st->q_inp32 */
1292 152406 : sts[n]->q_old_inp32 = sts[n]->q_inp32;
1293 152406 : move16();
1294 : }
1295 : }
1296 :
1297 422855 : IF( hCPE->hFrontVad[0] != NULL )
1298 : {
1299 31968 : hCPE->hFrontVad[0]->ini_frame = add( hCPE->hFrontVad[0]->ini_frame, 1 );
1300 31968 : hCPE->hFrontVad[0]->ini_frame = s_min( hCPE->hFrontVad[0]->ini_frame, MAX_FRAME_COUNTER );
1301 31968 : move16();
1302 31968 : move16();
1303 : }
1304 :
1305 : /* Store previous attack detection flag */
1306 1268565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1307 : {
1308 845710 : sts[n]->hTranDet->transientDetector.prev_bIsAttackPresent = sts[n]->hTranDet->transientDetector.bIsAttackPresent; /* Q0 */
1309 845710 : move16();
1310 : }
1311 :
1312 422855 : IF( Q_new_out )
1313 : {
1314 272308 : Copy( Q_new, Q_new_out, CPE_CHANNELS );
1315 : }
1316 :
1317 :
1318 : #ifdef DEBUG_MODE_INFO
1319 : {
1320 : float tmpF = ivas_total_brate / 1000.0f;
1321 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, cpe_id, ENC ) );
1322 : }
1323 : #endif
1324 :
1325 422855 : pop_wmops();
1326 422855 : return error;
1327 : }
1328 :
1329 :
1330 : /*-------------------------------------------------------------------------
1331 : * create_cpe_enc()
1332 : *
1333 : * Create, allocate and initialize IVAS encoder CPE handle
1334 : *-------------------------------------------------------------------------*/
1335 :
1336 2882 : ivas_error create_cpe_enc_fx(
1337 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
1338 : const Word16 cpe_id, /* i : CPE # identifier Q0*/
1339 : const Word32 element_brate /* i : element bitrate Q0*/
1340 : )
1341 : {
1342 : Word16 n;
1343 : Word16 ivas_format, element_mode_init, max_bwidth;
1344 : Word32 input_Fs;
1345 : CPE_ENC_HANDLE hCPE;
1346 : Encoder_State *st;
1347 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1348 : ivas_error error;
1349 :
1350 2882 : error = IVAS_ERR_OK;
1351 :
1352 2882 : hEncoderConfig = st_ivas->hEncoderConfig;
1353 :
1354 2882 : ivas_format = hEncoderConfig->ivas_format;
1355 2882 : move16();
1356 2882 : element_mode_init = hEncoderConfig->element_mode_init; /* Q0 */
1357 2882 : move16();
1358 2882 : input_Fs = hEncoderConfig->input_Fs; /* Q0 */
1359 2882 : move16();
1360 2882 : max_bwidth = hEncoderConfig->max_bwidth; /* Q0 */
1361 2882 : move16();
1362 :
1363 : /*-----------------------------------------------------------------*
1364 : * Allocate CPE handle
1365 : *-----------------------------------------------------------------*/
1366 :
1367 2882 : IF( ( hCPE = (CPE_ENC_HANDLE) malloc( sizeof( CPE_ENC_DATA ) ) ) == NULL )
1368 : {
1369 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
1370 : }
1371 :
1372 : /*-----------------------------------------------------------------*
1373 : * Initialization - general parameters
1374 : *-----------------------------------------------------------------*/
1375 :
1376 2882 : hCPE->cpe_id = cpe_id; /* Q0 */
1377 2882 : move16();
1378 2882 : hCPE->element_brate = element_brate; /* Q0 */
1379 2882 : move16();
1380 2882 : hCPE->last_element_brate = hCPE->element_brate; /* Q0 */
1381 2882 : move16();
1382 2882 : hCPE->element_mode = element_mode_init; /* Q0 */
1383 2882 : move16();
1384 2882 : hCPE->last_element_mode = element_mode_init; /* Q0 */
1385 2882 : move16();
1386 :
1387 2882 : hCPE->hStereoDft = NULL;
1388 2882 : hCPE->hStereoTD = NULL;
1389 2882 : hCPE->hStereoMdct = NULL;
1390 2882 : hCPE->hStereoTCA = NULL;
1391 2882 : hCPE->hStereoICBWE = NULL;
1392 2882 : hCPE->hMetaData = NULL;
1393 :
1394 2882 : hCPE->hStereoCng = NULL;
1395 2882 : hCPE->hFrontVad[0] = NULL;
1396 2882 : hCPE->hFrontVad[1] = NULL;
1397 :
1398 2882 : hCPE->brate_surplus = 0;
1399 2882 : move16();
1400 : /*-----------------------------------------------------------------*
1401 : * Input memory buffer: allocate and initialize
1402 : *-----------------------------------------------------------------*/
1403 :
1404 8646 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1405 : {
1406 5764 : test();
1407 5764 : test();
1408 5764 : test();
1409 5764 : test();
1410 5764 : IF( EQ_16( ivas_format, STEREO_FORMAT ) || EQ_16( ivas_format, MASA_FORMAT ) || ( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) )
1411 : {
1412 358 : IF( ( hCPE->input_mem_fx[n] = (Word16 *) malloc( sizeof( Word16 ) * NS2SA_FX2( input_Fs, STEREO_DFT_OVL_NS ) ) ) == NULL )
1413 : {
1414 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
1415 : }
1416 :
1417 358 : set16_zero_fx( hCPE->input_mem_fx[n], NS2SA_FX2( input_Fs, STEREO_DFT_OVL_NS ) );
1418 : }
1419 : ELSE
1420 : {
1421 5406 : hCPE->input_mem_fx[n] = NULL;
1422 : }
1423 : }
1424 2882 : set16_fx( hCPE->q_input_mem, Q15, CPE_CHANNELS );
1425 :
1426 : /*-----------------------------------------------------------------*
1427 : * stereo classifier: allocate and initialize
1428 : *-----------------------------------------------------------------*/
1429 :
1430 2882 : IF( ( hCPE->hStereoClassif = (STEREO_CLASSIF_HANDLE) malloc( sizeof( STEREO_CLASSIF_DATA ) ) ) == NULL )
1431 : {
1432 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo classifier structure\n" ) );
1433 : }
1434 :
1435 2882 : stereo_classifier_init_fx( hCPE->hStereoClassif );
1436 :
1437 : /*-----------------------------------------------------------------*
1438 : * Metadata: allocate and initialize
1439 : *-----------------------------------------------------------------*/
1440 :
1441 2882 : IF( EQ_16( cpe_id, sub( st_ivas->nCPE, 1 ) ) )
1442 : {
1443 1447 : IF( NE_32( ( error = ivas_initialize_MD_bstr_enc_fx( &( hCPE->hMetaData ), st_ivas ) ), IVAS_ERR_OK ) )
1444 : {
1445 0 : return error;
1446 : }
1447 : }
1448 :
1449 : /*-----------------------------------------------------------------*
1450 : * CoreCoder, 2 instances: allocate and initialize
1451 : *-----------------------------------------------------------------*/
1452 :
1453 8646 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1454 : {
1455 5764 : IF( ( st = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
1456 : {
1457 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
1458 : }
1459 :
1460 5764 : copy_encoder_config_fx( st_ivas, st, 1 );
1461 :
1462 5764 : IF( GT_16( st_ivas->nCPE, 1 ) )
1463 : {
1464 4836 : st->total_brate = hCPE->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() Q0*/
1465 4836 : move32();
1466 : }
1467 : ELSE
1468 : {
1469 928 : st->total_brate = L_shr( hCPE->element_brate, 1 ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() Q0*/
1470 928 : move32();
1471 : }
1472 5764 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
1473 5764 : move16();
1474 5764 : IF( NE_32( ( error = init_encoder_ivas_fx( st, st_ivas, n, hEncoderConfig->var_SID_rate_flag, hEncoderConfig->interval_SID, 0, ISM_MODE_NONE, hCPE->element_brate ) ), IVAS_ERR_OK ) )
1475 : {
1476 0 : return error;
1477 : }
1478 :
1479 5764 : IF( st->hFdCngEnc != NULL )
1480 : {
1481 145 : st->hFdCngEnc->hFdCngCom->CngBitrate = L_sub( hCPE->element_brate, 1 ); /* Q0 */
1482 145 : move32();
1483 : }
1484 :
1485 5764 : hCPE->hCoreCoder[n] = st;
1486 : }
1487 :
1488 : /*-----------------------------------------------------------------*
1489 : * LR VAD initialization
1490 : *-----------------------------------------------------------------*/
1491 :
1492 : #ifdef DEBUGGING
1493 : 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 ) )
1494 : #else
1495 2882 : IF( hEncoderConfig->Opt_DTX_ON )
1496 : #endif
1497 : {
1498 88 : test();
1499 88 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1500 : {
1501 31 : IF( ( hCPE->hStereoCng = (STEREO_CNG_ENC_HANDLE) malloc( sizeof( STEREO_CNG_ENC ) ) ) == NULL )
1502 : {
1503 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo Cng for Unified/TD \n" ) );
1504 : }
1505 :
1506 31 : stereo_enc_cng_init_fx( hCPE->hStereoCng );
1507 : }
1508 : }
1509 : ELSE
1510 : {
1511 2794 : hCPE->hStereoCng = NULL;
1512 : }
1513 :
1514 2882 : IF( hEncoderConfig->Opt_DTX_ON && NE_16( element_mode_init, IVAS_CPE_MDCT ) )
1515 : {
1516 93 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1517 : {
1518 62 : IF( ( error = front_vad_create_fx( &( hCPE->hFrontVad[n] ), hEncoderConfig ) ) != IVAS_ERR_OK )
1519 : {
1520 0 : return error;
1521 : }
1522 : }
1523 : }
1524 : ELSE
1525 : {
1526 8553 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1527 : {
1528 5702 : hCPE->hFrontVad[n] = NULL;
1529 : }
1530 : }
1531 :
1532 : /*-----------------------------------------------------------------*
1533 : * DFT stereo initialization
1534 : *-----------------------------------------------------------------*/
1535 :
1536 2882 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1537 : {
1538 99 : IF( ( error = stereo_dft_enc_create_fx( &( hCPE->hStereoDft ), input_Fs, max_bwidth ) ) != IVAS_ERR_OK )
1539 : {
1540 0 : return error;
1541 : }
1542 : }
1543 :
1544 : /*-----------------------------------------------------------------*
1545 : * Temporal inter-channel alignment initialization
1546 : *-----------------------------------------------------------------*/
1547 :
1548 2882 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1549 : {
1550 99 : IF( ( hCPE->hStereoTCA = (STEREO_TCA_ENC_HANDLE) malloc( sizeof( STEREO_TCA_ENC_DATA ) ) ) == NULL )
1551 : {
1552 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
1553 : }
1554 :
1555 99 : stereo_tca_init_enc_fx( hCPE->hStereoTCA, input_Fs );
1556 : }
1557 :
1558 : /*-----------------------------------------------------------------*
1559 : * Stereo IC BWE initialization
1560 : *-----------------------------------------------------------------*/
1561 :
1562 2882 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1563 : {
1564 99 : IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_ENC_HANDLE) malloc( sizeof( STEREO_ICBWE_ENC_DATA ) ) ) == NULL )
1565 : {
1566 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
1567 : }
1568 :
1569 99 : stereo_icBWE_init_enc_fx( hCPE->hStereoICBWE );
1570 : }
1571 :
1572 : /*-----------------------------------------------------------------*
1573 : * TD stereo initialization
1574 : *-----------------------------------------------------------------*/
1575 :
1576 2882 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
1577 : {
1578 0 : IF( ( hCPE->hStereoTD = (STEREO_TD_ENC_DATA_HANDLE) malloc( sizeof( STEREO_TD_ENC_DATA ) ) ) == NULL )
1579 : {
1580 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
1581 : }
1582 0 : stereo_td_init_enc_fx( hCPE->hStereoTD, hCPE->last_element_mode );
1583 : }
1584 :
1585 : /*-----------------------------------------------------------------*
1586 : * MDCT stereo initialization
1587 : *-----------------------------------------------------------------*/
1588 :
1589 2882 : test();
1590 2882 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( st_ivas->nCPE, 1 ) )
1591 : {
1592 365 : IF( ( hCPE->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
1593 : {
1594 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
1595 : }
1596 :
1597 : #ifdef DEBUGGING
1598 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = st_ivas->hEncoderConfig->mdct_stereo_mode_cmdl;
1599 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
1600 : /*set all other members to defined states */
1601 : hCPE->hStereoMdct->fDualMono = 0;
1602 : hCPE->hStereoMdct->fMSstereo = 0;
1603 :
1604 : if ( hCPE->hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_LR )
1605 : {
1606 : hCPE->hStereoMdct->fDualMono = 1;
1607 : }
1608 : else if ( hCPE->hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_MS )
1609 : {
1610 : hCPE->hStereoMdct->fMSstereo = 1;
1611 : }
1612 : #endif
1613 : #endif
1614 :
1615 365 : initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 1 );
1616 :
1617 365 : test();
1618 365 : test();
1619 365 : hCPE->hStereoMdct->isSBAStereoMode = ( ( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) ) && EQ_16( st_ivas->nchan_transport, 2 ) );
1620 365 : move16();
1621 :
1622 365 : test();
1623 365 : test();
1624 365 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && LE_32( element_brate, MAX_MDCT_ITD_BRATE ) && EQ_16( ivas_format, STEREO_FORMAT ) )
1625 : {
1626 13 : IF( ( error = initMdctItdHandling_fx( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
1627 : {
1628 0 : return error;
1629 : }
1630 : }
1631 : }
1632 :
1633 2882 : st_ivas->hCPE[cpe_id] = hCPE;
1634 :
1635 2882 : return error;
1636 : }
1637 :
1638 :
1639 : /*-------------------------------------------------------------------------
1640 : * destroy_cpe_enc()
1641 : *
1642 : * Destroy and deallocate IVAS encoder CPE handle
1643 : *-------------------------------------------------------------------------*/
1644 :
1645 2882 : void destroy_cpe_enc(
1646 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structure */
1647 : )
1648 : {
1649 : Word16 n;
1650 : Encoder_State *st;
1651 :
1652 2882 : IF( hCPE->hStereoClassif != NULL )
1653 : {
1654 2882 : free( hCPE->hStereoClassif );
1655 2882 : hCPE->hStereoClassif = NULL;
1656 : }
1657 :
1658 8646 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1659 : {
1660 5764 : IF( hCPE->input_mem_fx[n] != NULL )
1661 : {
1662 358 : free( hCPE->input_mem_fx[n] );
1663 358 : hCPE->input_mem_fx[n] = NULL;
1664 : }
1665 : }
1666 :
1667 2882 : ivas_destroy_MD_bstr_enc_fx( &( hCPE->hMetaData ) );
1668 :
1669 8646 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1670 : {
1671 5764 : st = hCPE->hCoreCoder[n];
1672 :
1673 5764 : IF( st != NULL )
1674 : {
1675 5764 : destroy_core_enc_fx( st );
1676 5764 : st = NULL;
1677 : }
1678 : }
1679 :
1680 2882 : IF( hCPE->hStereoDft != NULL )
1681 : {
1682 81 : stereo_dft_enc_destroy_fx( &hCPE->hStereoDft );
1683 81 : hCPE->hStereoDft = NULL;
1684 : }
1685 :
1686 2882 : IF( hCPE->hStereoTD != NULL )
1687 : {
1688 1 : free( hCPE->hStereoTD );
1689 1 : hCPE->hStereoTD = NULL;
1690 : }
1691 :
1692 2882 : IF( hCPE->hStereoMdct != NULL )
1693 : {
1694 391 : stereo_mdct_enc_destroy_fx( &hCPE->hStereoMdct );
1695 391 : hCPE->hStereoMdct = NULL;
1696 : }
1697 :
1698 2882 : IF( hCPE->hStereoTCA != NULL )
1699 : {
1700 82 : free( hCPE->hStereoTCA );
1701 82 : hCPE->hStereoTCA = NULL;
1702 : }
1703 :
1704 2882 : IF( hCPE->hStereoICBWE != NULL )
1705 : {
1706 82 : free( hCPE->hStereoICBWE );
1707 82 : hCPE->hStereoICBWE = NULL;
1708 : }
1709 :
1710 2882 : IF( hCPE->hStereoCng != NULL )
1711 : {
1712 57 : free( hCPE->hStereoCng );
1713 57 : hCPE->hStereoCng = NULL;
1714 : }
1715 :
1716 2882 : IF( hCPE->hFrontVad[0] != NULL )
1717 : {
1718 84 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1719 : {
1720 56 : front_vad_destroy_fx( &hCPE->hFrontVad[n] );
1721 56 : hCPE->hFrontVad[n] = NULL;
1722 : }
1723 : }
1724 :
1725 2882 : free( hCPE );
1726 :
1727 2882 : return;
1728 : }
1729 :
1730 :
1731 : /*-------------------------------------------------------------------------
1732 : * stereo_mode_combined_format_enc()
1733 : *
1734 : * Set stereo format in a combined format
1735 : *-------------------------------------------------------------------------*/
1736 :
1737 422855 : static void stereo_mode_combined_format_enc_fx(
1738 : const Encoder_Struct *st_ivas, /* i : encoder main structure */
1739 : CPE_ENC_HANDLE hCPE /* i/o: CPE handle */
1740 : )
1741 : {
1742 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1743 : Word32 element_brate_ref;
1744 :
1745 422855 : hEncoderConfig = st_ivas->hEncoderConfig;
1746 :
1747 422855 : IF( EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
1748 : {
1749 9000 : element_brate_ref = hCPE->element_brate; /* Q0 */
1750 9000 : move32();
1751 :
1752 9000 : test();
1753 9000 : test();
1754 9000 : test();
1755 9000 : test();
1756 9000 : IF( EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) &&
1757 : ( ( EQ_16( hEncoderConfig->nchan_ism, 3 ) && EQ_32( hEncoderConfig->ivas_total_brate, IVAS_96k ) ) ||
1758 : ( EQ_16( hEncoderConfig->nchan_ism, 4 ) && EQ_32( hEncoderConfig->ivas_total_brate, IVAS_128k ) ) ) )
1759 : {
1760 426 : IF( GT_32( L_add( hCPE->element_brate, hCPE->brate_surplus ), IVAS_64k ) )
1761 : {
1762 0 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = 0;
1763 0 : move16();
1764 : }
1765 : ELSE
1766 : {
1767 426 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = add( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, 1 ); /* Q0 */
1768 426 : move16();
1769 426 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = s_min( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, OMASA_STEREO_SW_CNT_MAX ); /* Q0 */
1770 426 : move16();
1771 : }
1772 :
1773 426 : IF( LT_16( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, OMASA_STEREO_SW_CNT_MAX ) )
1774 : {
1775 0 : hCPE->element_mode = IVAS_CPE_MDCT;
1776 0 : move16();
1777 0 : hCPE->element_brate = IVAS_64k;
1778 0 : move32();
1779 0 : hCPE->brate_surplus = L_sub( hCPE->brate_surplus, L_sub( hCPE->element_brate, element_brate_ref ) ); /* Q0 */
1780 0 : move32();
1781 : }
1782 :
1783 : /* write OMASA stereo mode signalling */
1784 426 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1785 : {
1786 0 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 1, NBITS_ELEMENT_MODE );
1787 : }
1788 : ELSE
1789 : {
1790 426 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 0, NBITS_ELEMENT_MODE );
1791 : }
1792 : }
1793 : }
1794 :
1795 422855 : return;
1796 : }
|