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 420855 : 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 420855 : set16_fx( fft_buff_fx_q, 0, CPE_CHANNELS );
85 420855 : Word16 fft_buff_fx_final_q = MAX_16;
86 420855 : move16();
87 : Word32 ener_fx[CPE_CHANNELS]; /* residual energy from Levinson-Durbin Q6 */
88 : Word16 relE_fx[CPE_CHANNELS]; /* frame relative energy Q8 */
89 420855 : Word16 A_fx[CPE_CHANNELS][NB_SUBFR16k * ( M + 1 )] = { 0 }; /* A(z) unquantized for subframes Q12 */
90 420855 : 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 420855 : Word16 Q_new[CPE_CHANNELS] = { 0 };
133 420855 : move16();
134 420855 : move16();
135 : Word16 q_com, shift, q_min, gb;
136 :
137 420855 : error = IVAS_ERR_OK;
138 420855 : move32();
139 420855 : push_wmops( "ivas_cpe_enc" );
140 :
141 420855 : hCPE = st_ivas->hCPE[cpe_id];
142 420855 : sts = hCPE->hCoreCoder;
143 420855 : hEncoderConfig = st_ivas->hEncoderConfig;
144 420855 : max_bwidth = hEncoderConfig->max_bwidth;
145 420855 : ivas_format = hEncoderConfig->ivas_format;
146 420855 : input_Fs = hEncoderConfig->input_Fs;
147 420855 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
148 420855 : element_brate_ref = hCPE->element_brate;
149 420855 : last_bits_frame_nominal = sts[0]->bits_frame_nominal;
150 420855 : move16();
151 420855 : move16();
152 420855 : move16();
153 420855 : move32();
154 420855 : move32();
155 420855 : move32();
156 :
157 : /*------------------------------------------------------------------*
158 : * Initialization - general
159 : *-----------------------------------------------------------------*/
160 :
161 420855 : set16_fx( q_re_im_buf, 0, CPE_CHANNELS );
162 :
163 420855 : tdm_SM_or_LRTD_Pri = 0;
164 420855 : tdm_ratio_idx = -1;
165 420855 : tdm_ratio_idx_SM = -1;
166 : // tdm_last_ratio = 0;
167 420855 : move16();
168 420855 : move16();
169 420855 : 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 420855 : set16_fx( pitch_fr_fx[0], 0, NB_SUBFR );
179 420855 : set16_fx( pitch_fr_fx[1], 0, NB_SUBFR );
180 420855 : set16_fx( voicing_fr_fx[0], 0, NB_SUBFR );
181 420855 : set16_fx( voicing_fr_fx[1], 0, NB_SUBFR );
182 :
183 1262565 : FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
184 : {
185 841710 : set16_zero_fx( fft_buff_fx[i], 2 * L_FFT );
186 841710 : set16_zero_fx( old_inp_16k_16fx[i], L_INP );
187 841710 : set_zero_fx( old_inp_16k_fx[i], L_INP );
188 841710 : set16_zero_fx( old_inp_12k8_16fx[i], L_INP_12k8 );
189 841710 : set_zero_fx( old_inp_12k8_fx[i], L_INP_12k8 );
190 : }
191 :
192 : /*------------------------------------------------------------------*
193 : * CPE initialization - core coder
194 : *-----------------------------------------------------------------*/
195 :
196 1262565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
197 : {
198 841710 : sts[n]->idchan = n; /* Q0 */
199 841710 : sts[n]->core = -1; /* Q0 */
200 841710 : sts[n]->core_brate = -1; /* updated in dtx() Q0*/
201 841710 : sts[n]->max_bwidth = max_bwidth; /* Q0 */
202 841710 : move32();
203 841710 : move16();
204 841710 : move16();
205 841710 : move16();
206 :
207 841710 : 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 841710 : sts[n]->rate_switching_reset = 0;
215 841710 : 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 420855 : 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 420855 : norm = L_norm_arr( data_fx_ch0, input_frame );
228 420855 : IF( data_fx_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
229 : {
230 362026 : Q_min = s_min( Q_min, add( sts[1]->q_old_inp32, L_norm_arr( sts[1]->input32_fx - input_frame, input_frame ) ) );
231 362026 : norm = s_min( norm, L_norm_arr( data_fx_ch1, input_frame ) );
232 : }
233 420855 : scale_sig32( sts[0]->input32_fx - input_frame, input_frame, sub( Q_min, sts[0]->q_old_inp32 ) );
234 420855 : sts[0]->q_old_inp32 = Q_min;
235 420855 : move16();
236 420855 : Copy_Scale_sig32( data_fx_ch0, sts[0]->input32_fx, input_frame, sub( Q_min, q_data_fx ) ); // Q(Q_min)
237 420855 : sts[0]->q_inp32 = Q_min;
238 420855 : move16();
239 :
240 420855 : Copy_Scale_sig32_16( sts[0]->input32_fx, sts[0]->input_fx, input_frame, norm );
241 420855 : sts[0]->q_inp = add( sub( sts[0]->q_inp32, Q16 ), norm );
242 420855 : move16();
243 420855 : IF( data_fx_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
244 : {
245 362026 : scale_sig32( sts[1]->input32_fx - input_frame, input_frame, sub( Q_min, sts[1]->q_old_inp32 ) );
246 362026 : sts[1]->q_old_inp32 = Q_min;
247 362026 : move16();
248 362026 : Copy_Scale_sig32( data_fx_ch1, sts[1]->input32_fx, input_frame, sub( Q_min, q_data_fx ) ); // Q(Q_min)
249 362026 : sts[1]->q_inp32 = Q_min;
250 362026 : move16();
251 :
252 362026 : Copy_Scale_sig32_16( sts[1]->input32_fx, sts[1]->input_fx, input_frame, norm );
253 362026 : sts[1]->q_inp = add( sub( sts[1]->q_inp32, Q16 ), norm );
254 362026 : move16();
255 : }
256 :
257 1262565 : FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
258 : {
259 14309070 : FOR( Word16 j = 0; j < CLDFB_NO_COL_MAX; j++ )
260 : {
261 13467360 : set_zero_fx( realBuffer_fx[i][j], CLDFB_NO_CHANNELS_MAX );
262 13467360 : 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 420855 : test();
272 420855 : 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 420855 : stereo_mode_combined_format_enc_fx( st_ivas, hCPE );
291 :
292 420855 : Word16 Q_inp = 0;
293 420855 : move16();
294 :
295 420855 : Word16 Q_add = 2;
296 420855 : move16();
297 420855 : Word16 front_create_flag = 0;
298 420855 : move16();
299 420855 : set32_fx( band_energies_LR_fx, 0, 2 * NB_BANDS );
300 :
301 420855 : 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 1262565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
323 : {
324 841710 : Q_buffer[n] = 15;
325 841710 : move16();
326 : }
327 420855 : test();
328 420855 : 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 420855 : 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 420855 : 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 420855 : sts[0]->element_mode = hCPE->element_mode;
357 420855 : sts[1]->element_mode = hCPE->element_mode;
358 420855 : move16();
359 420855 : move16();
360 :
361 420855 : n_CoreChannels = 2;
362 420855 : move16();
363 420855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
364 : {
365 59659 : n_CoreChannels = 1; /* in DFT stereo, only M channel is coded */
366 59659 : move16();
367 :
368 59659 : sts[1]->vad_flag = 0;
369 59659 : move16();
370 : }
371 :
372 : /*----------------------------------------------------------------*
373 : * dynamically allocate data structures depending on the actual stereo mode
374 : *----------------------------------------------------------------*/
375 :
376 420855 : 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 420855 : hConfigDft = NULL;
382 420855 : IF( hCPE->hStereoDft != NULL )
383 : {
384 59659 : hConfigDft = hCPE->hStereoDft->hConfig;
385 : }
386 :
387 : /*----------------------------------------------------------------*
388 : * Set TD stereo parameters
389 : *----------------------------------------------------------------*/
390 :
391 420855 : 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 420855 : test();
402 420855 : 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 420855 : 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 420855 : 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 420855 : q_min = add( sts[0]->q_inp32, sub( shift, find_guarded_bits_fx( input_frame ) ) );
434 420855 : 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 420855 : 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 420855 : sts[0]->q_inp32 = sts[1]->q_inp32 = sts[0]->q_old_inp32 = sts[1]->q_old_inp32 = q_min;
437 420855 : move16();
438 420855 : move16();
439 420855 : move16();
440 420855 : move16();
441 :
442 420855 : stereo_tca_enc_fx( hCPE, input_frame );
443 :
444 420855 : 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 420855 : 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 420855 : 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 420855 : 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 420855 : sts[0]->q_inp = sts[1]->q_inp = sts[0]->q_old_inp = sts[1]->q_old_inp = shift;
450 420855 : move16();
451 420855 : move16();
452 420855 : move16();
453 420855 : move16();
454 :
455 : /*----------------------------------------------------------------*
456 : * Input signal buffering - needed in IC-BWE and TD ITD in MDCT stereo
457 : *----------------------------------------------------------------*/
458 :
459 1262565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
460 : {
461 841710 : Copy32( sts[n]->input32_fx, orig_input_fx32[n], input_frame ); /* sts->q_inp32 */
462 841710 : Copy( sts[n]->input_fx, orig_input_fx[n], input_frame ); /* sts->q_inp */
463 841710 : Scale_sig( orig_input_fx[n], input_frame, sub( -1, sts[n]->q_inp ) ); // Q(-1)
464 841710 : Q_orig_inp[n] = -1;
465 841710 : Q_orig_inp32[n] = sts[n]->q_inp32;
466 841710 : move16();
467 841710 : move16();
468 :
469 841710 : IF( hCPE->hStereoICBWE != NULL )
470 : {
471 119560 : hCPE->hStereoICBWE->dataChan_fx[n] = &orig_input_fx[n][0]; // Q_orig_inp
472 119560 : hCPE->hStereoICBWE->q_dataChan_fx = Q_orig_inp[n];
473 119560 : move16();
474 : }
475 : }
476 :
477 : /*---------------------------------------------------------------*
478 : * Time Domain Transient Detector
479 : *---------------------------------------------------------------*/
480 :
481 1262565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
482 : {
483 841710 : IF( sts[n]->hTranDet == NULL )
484 : {
485 0 : currFlatness_fx[n] = 0;
486 0 : move32();
487 0 : CONTINUE;
488 : }
489 :
490 841710 : test();
491 841710 : IF( !( EQ_32( ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) ) )
492 : {
493 820990 : 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 841710 : currFlatness_fx[n] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) sts[n]->hTranDet, NSUBBLOCKS, 0 ); // Q21
497 841710 : move32();
498 : }
499 :
500 : /* Synchonize detection for downmix-based stereo */
501 420855 : test();
502 420855 : 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 1202906 : FOR( n = 0; n < n_CoreChannels; n++ )
512 : {
513 : /* Force to MODE1 in IVAS */
514 782051 : sts[n]->codec_mode = MODE1;
515 782051 : move16();
516 :
517 782051 : sts[n]->element_mode = hCPE->element_mode;
518 782051 : move16();
519 : }
520 :
521 420855 : test();
522 420855 : test();
523 420855 : test();
524 420855 : test();
525 420855 : test();
526 : /* No Basop - sts[0]->ini_frame == 0 as comparison with 0 */
527 420855 : 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 420855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
551 : {
552 3811 : IF( hCPE->hStereoTD->tdm_LRTD_flag )
553 : {
554 3690 : sts[0]->bits_frame_nominal = extract_l( Mult_32_32( L_shr( hCPE->element_brate, 1 ), ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
555 3690 : 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 420855 : 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 357385 : test();
568 357385 : test();
569 357385 : 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 1072155 : FOR( n = 0; n < n_CoreChannels; n++ )
579 : {
580 714770 : IF( st_ivas->hMCT )
581 : {
582 : Word16 lfe_bits;
583 540616 : test();
584 540616 : 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 370816 : lfe_bits = 0;
592 370816 : move16();
593 : }
594 540616 : sts[n]->total_brate = hCPE->element_brate; /* Q0 */
595 540616 : sts[n]->bits_frame_nominal = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
596 540616 : 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 540616 : move32();
598 540616 : move16();
599 540616 : 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 357385 : 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 420855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
641 : {
642 59659 : test();
643 59659 : if ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
644 : {
645 17592 : sts[1]->exp_old_inp_12k8 = Q15;
646 17592 : move16();
647 : }
648 59659 : Word16 temp = getScaleFactor32( hCPE->hStereoDft->Spd_L_smooth_fx, STEREO_DFT_N_32k_ENC / 2 );
649 59659 : 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 59659 : hCPE->hStereoDft->Spd_L_smooth_fx_e = sub( hCPE->hStereoDft->Spd_L_smooth_fx_e, temp );
651 59659 : move16();
652 :
653 59659 : temp = getScaleFactor32( hCPE->hStereoDft->Spd_R_smooth_fx, STEREO_DFT_N_32k_ENC / 2 );
654 59659 : 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 59659 : hCPE->hStereoDft->Spd_R_smooth_fx_e = sub( hCPE->hStereoDft->Spd_R_smooth_fx_e, temp );
656 59659 : move16();
657 :
658 59659 : q_com = MAX_16;
659 59659 : move16();
660 178977 : FOR( n = 0; n < CPE_CHANNELS; n++ )
661 : {
662 119318 : q_com = s_min( q_com, sub( add( L_norm_arr( sts[n]->input32_fx, input_frame ), sts[n]->q_inp32 ), 16 ) );
663 119318 : 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 119318 : 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 119318 : if ( EQ_16( q_com, Q15 ) )
667 : {
668 0 : q_com = 0;
669 0 : move16();
670 : }
671 : }
672 178977 : FOR( n = 0; n < CPE_CHANNELS; n++ )
673 : {
674 119318 : Copy_Scale_sig_32_16( sts[n]->input32_fx, sts[n]->input_fx, input_frame, sub( q_com, sts[n]->q_inp32 ) );
675 119318 : sts[n]->q_inp = q_com;
676 119318 : move16();
677 :
678 119318 : 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 119318 : sts[n]->q_old_inp = q_com;
680 119318 : move16();
681 :
682 119318 : scale_sig( hCPE->input_mem_fx[n], hCPE->hStereoDft->dft_ovl, sub( q_com, hCPE->q_input_mem[n] ) );
683 119318 : hCPE->q_input_mem[n] = q_com;
684 119318 : move16();
685 : }
686 :
687 59659 : stereo_dft_hybrid_ITD_flag_fx( hConfigDft, input_Fs, hCPE->hStereoDft->hItd->hybrid_itd_max );
688 :
689 : /* Time Domain ITD compensation using extrapolation */
690 59659 : 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 59659 : 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 59659 : 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 59659 : stereo_dft_enc_update_fx( hCPE->hStereoDft, max_bwidth );
698 :
699 : /* DFT stereo processing */
700 59659 : stereo_dft_enc_process_fx( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
701 : }
702 361196 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
703 : {
704 3811 : Scale_sig( sts[0]->input_fx, input_frame, negate( sts[0]->q_inp ) ); /* Q0 */
705 3811 : sts[0]->q_inp = 0;
706 3811 : move16();
707 3811 : Scale_sig( sts[1]->input_fx, input_frame, negate( sts[1]->q_inp ) ); /* Q0 */
708 3811 : sts[1]->q_inp = 0;
709 3811 : move16();
710 :
711 : /* Determine the energy ratio between the 2 channels */
712 3811 : 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 3811 : 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 3690 : tdm_SM_flag = 0;
727 3690 : move16();
728 : }
729 3811 : 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 3811 : 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 3811 : 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 3811 : sts[0]->bits_frame_channel = 0;
735 3811 : sts[1]->bits_frame_channel = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
736 3811 : 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 3811 : move16();
738 3811 : move16();
739 3811 : move16();
740 3811 : 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 3811 : Etot_last_fx[0] = extract_h( sts[0]->hNoiseEst->Etot_last_32fx ); /* Q8 */
747 3811 : Etot_last_fx[1] = extract_h( sts[1]->hNoiseEst->Etot_last_32fx ); /* Q8 */
748 3811 : move16();
749 3811 : move16();
750 : }
751 357385 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
752 : {
753 357385 : test();
754 357385 : 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 357385 : 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 420855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
808 : {
809 : Word32 internal_Fs;
810 :
811 59659 : 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 59659 : internal_Fs = INT_FS_16k;
814 59659 : 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 59659 : Word16 out_16k_start_ind = 0, out_16k_end_ind = 0;
825 59659 : move16();
826 59659 : move16();
827 :
828 59659 : 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 59659 : 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 59659 : 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 59659 : sts[0]->q_inp32 = Q15;
833 59659 : sts[0]->q_old_inp32 = Q15;
834 59659 : move16();
835 59659 : move16();
836 :
837 : // Normalise the input buffer from Q15
838 : Word16 input_norm, q_inp; //, common_q, fir_delay_len;
839 59659 : input_norm = L_norm_arr( sts[0]->input32_fx - input_frame, shl( input_frame, 1 ) );
840 59659 : q_inp = sub( add( Q15, input_norm ), 16 );
841 :
842 : // Rescale the old input, input and FIR delay section of input buffer
843 59659 : 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 59659 : sts[0]->q_inp = q_inp;
847 59659 : move16();
848 59659 : sts[0]->q_old_inp = q_inp;
849 59659 : move16();
850 :
851 : /* iDFT & resampling to 12.8kHz internal sampling rate */
852 59659 : 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 59659 : 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 59659 : IF( EQ_32( input_Fs, internal_Fs ) )
857 : {
858 11543 : 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 11543 : out_16k_start_ind = -STEREO_DFT_OVL_16k;
860 11543 : out_16k_end_ind = add( out_16k_start_ind, add( input_frame, STEREO_DFT_OVL_16k ) );
861 11543 : move16();
862 11543 : move16();
863 : }
864 : ELSE
865 : {
866 48116 : 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 59659 : 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 59659 : test();
872 59659 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
873 : {
874 19968 : 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 19968 : 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 19968 : 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 19968 : sts[1]->exp_old_inp_12k8 = Q15;
880 19968 : move16();
881 19968 : 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 59659 : set16_fx( sts[1]->input_fx, 0, input_frame );
886 59659 : sts[1]->q_inp = Q15;
887 59659 : move16();
888 59659 : set32_fx( sts[1]->input32_fx, 0, input_frame );
889 59659 : sts[1]->q_inp32 = Q31;
890 59659 : 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 1202906 : FOR( n = 0; n < n_CoreChannels; n++ )
902 : {
903 782051 : set16_fx( old_wsp_fx[n], 0, L_WSP );
904 782051 : q_old_wsp = Q15;
905 782051 : move16();
906 : Word16 front_vad_flag;
907 782051 : IF( st_ivas->hSpar != NULL )
908 : {
909 446736 : front_vad_flag = st_ivas->hSpar->front_vad_flag;
910 : }
911 : ELSE
912 : {
913 335315 : front_vad_flag = 0;
914 : }
915 :
916 782051 : 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 782051 : &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 782051 : 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 782051 : 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 782051 : 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 782051 : e_old_wsp[n] = sub( Q15, q_old_wsp );
927 782051 : move16();
928 782051 : IF( error != IVAS_ERR_OK )
929 : {
930 0 : return error;
931 : }
932 : }
933 :
934 : /*making Q common*/
935 1202906 : FOR( n = 0; n < n_CoreChannels; n++ )
936 : {
937 782051 : fft_buff_fx_final_q = s_min( fft_buff_fx_final_q, fft_buff_fx_q[n] );
938 : }
939 420855 : fft_buff_fx_final_q = sub( fft_buff_fx_final_q, 2 ); // headroom
940 1202906 : FOR( n = 0; n < n_CoreChannels; n++ )
941 : {
942 782051 : 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 420855 : IF( GT_16( add( st_ivas->nSCE, st_ivas->nCPE ), 1 ) )
947 : {
948 277991 : test();
949 277991 : 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 420855 : test();
963 420855 : test();
964 420855 : 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 420855 : test();
975 420855 : 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 420855 : test();
992 420855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && st_ivas->hMCT == NULL )
993 : {
994 : /* set coded BW for MDCT stereo */
995 87077 : set_bw_stereo_fx( hCPE );
996 :
997 : /* reconfiguration of MDCT stereo */
998 87077 : test();
999 87077 : test();
1000 87077 : test();
1001 87077 : IF( sts[0]->bwidth != sts[0]->last_bwidth || ( ( hCPE->last_element_brate != hCPE->element_brate || hCPE->last_element_mode != hCPE->element_mode ) && sts[0]->bwidth != max_bwidth ) )
1002 : {
1003 728 : initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, sts[0]->bwidth, 0, NULL, 0 );
1004 :
1005 728 : test();
1006 728 : test();
1007 728 : test();
1008 728 : hCPE->hStereoMdct->isSBAStereoMode = ( ( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) ) && ( EQ_16( st_ivas->nchan_transport, 2 ) ) ); /* Q0 */
1009 :
1010 728 : test();
1011 728 : IF( LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) && EQ_16( ivas_format, STEREO_FORMAT ) )
1012 : {
1013 84 : IF( ( error = initMdctItdHandling_fx( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
1014 : {
1015 0 : return error;
1016 : }
1017 : }
1018 : }
1019 : }
1020 :
1021 : /* IGF reconfiguration */
1022 1202906 : FOR( n = 0; n < n_CoreChannels; n++ )
1023 : {
1024 782051 : test();
1025 782051 : test();
1026 782051 : test();
1027 782051 : test();
1028 782051 : test();
1029 782051 : test();
1030 782051 : 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 ) ) )
1031 : {
1032 : Word16 igf;
1033 : Word16 bw;
1034 :
1035 : /* float uses ternary operator for this: bw = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? sts[n]->bwidth : sts[n]->max_bwidth; */
1036 9240 : bw = max_bwidth;
1037 9240 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1038 : {
1039 7732 : bw = sts[n]->bwidth;
1040 : }
1041 9240 : igf = getIgfPresent_fx( sts[n]->element_mode, L_mult0( sts[n]->bits_frame_nominal, FRAMES_PER_SEC ), bw, sts[n]->rf_mode ); /* Q0 */
1042 9240 : 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 )
1043 : {
1044 0 : return error;
1045 : }
1046 : }
1047 : }
1048 :
1049 : /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
1050 1202906 : FOR( n = 0; n < n_CoreChannels; n++ )
1051 : {
1052 782051 : test();
1053 782051 : test();
1054 782051 : IF( ( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA ) && EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1055 : {
1056 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 */
1057 10394 : move16();
1058 : }
1059 : ELSE
1060 : {
1061 771657 : 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 */
1062 771657 : move16();
1063 : }
1064 : }
1065 :
1066 : /* configure TD stereo encoder */
1067 420855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
1068 : {
1069 3811 : tdm_ol_pitch_comparison_fx( hCPE, pitch_fr_fx, voicing_fr_fx );
1070 3811 : 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 );
1071 :
1072 3811 : IF( hEncoderConfig->Opt_DTX_ON )
1073 : {
1074 1668 : stereo_cng_upd_counters_fx( hCPE->hStereoCng, hCPE->element_mode, -1, NULL, sts[0]->hTdCngEnc->burst_ho_cnt, NULL );
1075 : }
1076 : }
1077 :
1078 : /* modify the coder_type depending on the total_brate per channel */
1079 1202906 : FOR( n = 0; n < n_CoreChannels; n++ )
1080 : {
1081 782051 : test();
1082 782051 : test();
1083 782051 : 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 */
1084 : {
1085 : /* limit coder_type depending on the bitrate */
1086 778240 : coder_type_modif_ivas_fx( sts[n], relE_fx[n] );
1087 : }
1088 : }
1089 :
1090 : /*----------------------------------------------------------------*
1091 : * Write IVAS format signaling in SID frames
1092 : *----------------------------------------------------------------*/
1093 :
1094 420855 : IF( EQ_32( sts[0]->core_brate, SID_2k40 ) )
1095 : {
1096 2010 : ivas_write_format_sid_fx( ivas_format, hCPE->element_mode, sts[0]->hBstr, hEncoderConfig->sba_order, hEncoderConfig->sba_planar );
1097 : }
1098 :
1099 : /*----------------------------------------------------------------*
1100 : * DFT Stereo residual coding
1101 : * DFT Stereo parameters writing into the bitstream
1102 : *----------------------------------------------------------------*/
1103 :
1104 420855 : cpe_brate = 0;
1105 420855 : move16();
1106 420855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1107 : {
1108 59659 : IF( hEncoderConfig->Opt_DTX_ON )
1109 : {
1110 30300 : test();
1111 30300 : IF( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA )
1112 : {
1113 : /* Reconfigure DFT Stereo for inactive frames */
1114 10394 : IF( EQ_32( sts[0]->core_brate, SID_2k40 ) )
1115 : {
1116 1533 : stereo_dft_config_fx( hConfigDft, IVAS_SID_5k2, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
1117 : }
1118 : ELSE
1119 : {
1120 8861 : stereo_dft_config_fx( hConfigDft, FRAME_NO_DATA, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
1121 : }
1122 :
1123 10394 : stereo_dft_cng_side_gain_fx( hCPE->hStereoDft, hCPE->hStereoCng, sts[0]->core_brate, sts[0]->last_core_brate, sts[0]->bwidth );
1124 : }
1125 : ELSE
1126 : {
1127 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 );
1128 : }
1129 : }
1130 :
1131 : /* Write stereo bitstream */
1132 59659 : cpe_brate = st_ivas->hCPE[0]->element_brate; /* Q0 */
1133 59659 : move16();
1134 :
1135 : /* DFT stereo side bits */
1136 59659 : test();
1137 59659 : test();
1138 59659 : test();
1139 59659 : test();
1140 59659 : test();
1141 59659 : test();
1142 59659 : test();
1143 59659 : 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 )
1144 : {
1145 5639 : nb_bits = 0; /* Only mono downmix is transmitted in this case */
1146 5639 : move16();
1147 : }
1148 54020 : 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 ) )
1149 : {
1150 528 : nb_bits = hCPE->hMetaData->nb_bits_tot; /* Q0 */
1151 528 : move16();
1152 : }
1153 : ELSE
1154 : {
1155 53492 : stereo_dft_enc_write_BS_fx( hCPE, &nb_bits );
1156 : }
1157 :
1158 : /* Residual coding in MDCT domain */
1159 59659 : test();
1160 59659 : test();
1161 59659 : 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 ) ) )
1162 : {
1163 59131 : 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 */ ) ) ) );
1164 59131 : test();
1165 59131 : IF( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) )
1166 : {
1167 11396 : max_bits = sub( max_bits, nb_bits_metadata );
1168 11396 : IF( hCPE->brate_surplus < 0 )
1169 : {
1170 2293 : max_bits = add( max_bits, extract_l( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) ) );
1171 : }
1172 : }
1173 :
1174 177393 : FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
1175 : {
1176 118262 : Copy_Scale_sig_16_32_no_sat( old_inp_12k8_16fx[i], old_inp_12k8_fx[i], L_INP_12k8, Q16 ); // Q(-1) -> Q15
1177 : }
1178 59131 : 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 );
1179 : }
1180 :
1181 59659 : IF( sts[0]->core_brate == FRAME_NO_DATA || EQ_32( sts[0]->core_brate, SID_2k40 ) )
1182 : {
1183 10394 : assert( ( nb_bits <= ( ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS ) ) && "Stereo DFT CNG: bit budget is violated" );
1184 : }
1185 : ELSE
1186 : {
1187 : /* Flexible total bitrate in M channel */
1188 49265 : sts[0]->total_brate = L_sub( hCPE->element_brate, L_deposit_l( imult1616( nb_bits, FRAMES_PER_SEC ) ) );
1189 49265 : move32();
1190 : }
1191 :
1192 : /* subtract metadata bitbudget */
1193 59659 : sts[0]->total_brate = L_sub( sts[0]->total_brate, L_deposit_l( imult1616( nb_bits_metadata, FRAMES_PER_SEC ) ) );
1194 59659 : move32();
1195 :
1196 : /* subtract bit-rate for combined format coding */
1197 59659 : test();
1198 59659 : test();
1199 59659 : test();
1200 59659 : 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 ) ) )
1201 : {
1202 2784 : sts[0]->total_brate = L_add( sts[0]->total_brate, hCPE->brate_surplus );
1203 2784 : move32();
1204 : }
1205 : }
1206 :
1207 : /*----------------------------------------------------------------*
1208 : * Core Encoder
1209 : *----------------------------------------------------------------*/
1210 :
1211 1202906 : FOR( n = 0; n < n_CoreChannels; n++ )
1212 : {
1213 782051 : Scale_sig( old_wsp_fx[n], L_WSP, sub( e_old_wsp[n], Q16 ) ); /* exp(16) */
1214 782051 : e_old_wsp[n] = 16;
1215 782051 : move16();
1216 : }
1217 420855 : 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 ) )
1218 : {
1219 0 : return error;
1220 : }
1221 :
1222 : /*----------------------------------------------------------------*
1223 : * Common updates
1224 : *----------------------------------------------------------------*/
1225 :
1226 420855 : hCPE->last_element_brate = hCPE->element_brate; /* Q0 */
1227 420855 : move32();
1228 420855 : hCPE->last_element_mode = hCPE->element_mode; /* Q0 */
1229 420855 : move16();
1230 :
1231 420855 : if ( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
1232 : {
1233 9000 : hCPE->element_brate = element_brate_ref; /* Q0 */
1234 9000 : move32();
1235 : }
1236 :
1237 420855 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
1238 : {
1239 : /* update input samples buffer */
1240 44070 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1241 : {
1242 29380 : Copy32( orig_input_fx32[n], sts[n]->old_input_signal32_fx, input_frame ); /* Q_orig_inp32 */
1243 29380 : sts[n]->q_old_inp32 = Q_orig_inp32[n];
1244 29380 : move16();
1245 29380 : Copy( orig_input_fx[n], sts[n]->old_input_signal_fx, input_frame ); /* Q_orig_inp */
1246 29380 : sts[n]->q_old_inp = Q_orig_inp[n];
1247 29380 : move16();
1248 : }
1249 : }
1250 406165 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1251 : {
1252 59659 : Copy( sts[0]->input_fx, sts[0]->old_input_signal_fx, input_frame ); /* sts[n]->q_inp */
1253 59659 : sts[0]->q_old_inp = sts[0]->q_inp;
1254 59659 : move16();
1255 59659 : Copy32( sts[0]->input32_fx, sts[0]->old_input_signal32_fx, input_frame ); /* st->q_inp32 */
1256 59659 : sts[0]->q_old_inp32 = sts[0]->q_inp32;
1257 59659 : move16();
1258 : }
1259 346506 : ELSE IF( st_ivas->hMCT == NULL ) /* note: in MCT, input buffers are updated later in ivas_mct_enc() */
1260 : {
1261 : /* update input samples buffer */
1262 228594 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1263 : {
1264 152396 : Copy( sts[n]->input_fx, sts[n]->old_input_signal_fx, input_frame ); /* sts[n]->q_inp */
1265 152396 : sts[n]->q_old_inp = sts[n]->q_inp;
1266 152396 : move16();
1267 152396 : Copy32( sts[n]->input32_fx, sts[n]->old_input_signal32_fx, input_frame ); /* st->q_inp32 */
1268 152396 : sts[n]->q_old_inp32 = sts[n]->q_inp32;
1269 152396 : move16();
1270 : }
1271 : }
1272 :
1273 420855 : IF( hCPE->hFrontVad[0] != NULL )
1274 : {
1275 31968 : hCPE->hFrontVad[0]->ini_frame = add( hCPE->hFrontVad[0]->ini_frame, 1 );
1276 31968 : hCPE->hFrontVad[0]->ini_frame = s_min( hCPE->hFrontVad[0]->ini_frame, MAX_FRAME_COUNTER );
1277 31968 : move16();
1278 31968 : move16();
1279 : }
1280 :
1281 : /* Store previous attack detection flag */
1282 1262565 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1283 : {
1284 841710 : sts[n]->hTranDet->transientDetector.prev_bIsAttackPresent = sts[n]->hTranDet->transientDetector.bIsAttackPresent; /* Q0 */
1285 841710 : move16();
1286 : }
1287 :
1288 420855 : IF( Q_new_out )
1289 : {
1290 270308 : Copy( Q_new, Q_new_out, CPE_CHANNELS );
1291 : }
1292 :
1293 :
1294 : #ifdef DEBUG_MODE_INFO
1295 : {
1296 : float tmpF = ivas_total_brate / 1000.0f;
1297 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, cpe_id, ENC ) );
1298 : }
1299 : #endif
1300 :
1301 420855 : pop_wmops();
1302 420855 : return error;
1303 : }
1304 :
1305 :
1306 : /*-------------------------------------------------------------------------
1307 : * create_cpe_enc()
1308 : *
1309 : * Create, allocate and initialize IVAS encoder CPE handle
1310 : *-------------------------------------------------------------------------*/
1311 :
1312 2880 : ivas_error create_cpe_enc_fx(
1313 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
1314 : const Word16 cpe_id, /* i : CPE # identifier Q0*/
1315 : const Word32 element_brate /* i : element bitrate Q0*/
1316 : )
1317 : {
1318 : Word16 n;
1319 : Word16 ivas_format, element_mode_init, max_bwidth;
1320 : Word32 input_Fs;
1321 : CPE_ENC_HANDLE hCPE;
1322 : Encoder_State *st;
1323 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1324 : ivas_error error;
1325 :
1326 2880 : error = IVAS_ERR_OK;
1327 :
1328 2880 : hEncoderConfig = st_ivas->hEncoderConfig;
1329 :
1330 2880 : ivas_format = hEncoderConfig->ivas_format;
1331 2880 : move16();
1332 2880 : element_mode_init = hEncoderConfig->element_mode_init; /* Q0 */
1333 2880 : move16();
1334 2880 : input_Fs = hEncoderConfig->input_Fs; /* Q0 */
1335 2880 : move16();
1336 2880 : max_bwidth = hEncoderConfig->max_bwidth; /* Q0 */
1337 2880 : move16();
1338 :
1339 : /*-----------------------------------------------------------------*
1340 : * Allocate CPE handle
1341 : *-----------------------------------------------------------------*/
1342 :
1343 2880 : IF( ( hCPE = (CPE_ENC_HANDLE) malloc( sizeof( CPE_ENC_DATA ) ) ) == NULL )
1344 : {
1345 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
1346 : }
1347 :
1348 : /*-----------------------------------------------------------------*
1349 : * Initialization - general parameters
1350 : *-----------------------------------------------------------------*/
1351 :
1352 2880 : hCPE->cpe_id = cpe_id; /* Q0 */
1353 2880 : move16();
1354 2880 : hCPE->element_brate = element_brate; /* Q0 */
1355 2880 : move16();
1356 2880 : hCPE->last_element_brate = hCPE->element_brate; /* Q0 */
1357 2880 : move16();
1358 2880 : hCPE->element_mode = element_mode_init; /* Q0 */
1359 2880 : move16();
1360 2880 : hCPE->last_element_mode = element_mode_init; /* Q0 */
1361 2880 : move16();
1362 :
1363 2880 : hCPE->hStereoDft = NULL;
1364 2880 : hCPE->hStereoTD = NULL;
1365 2880 : hCPE->hStereoMdct = NULL;
1366 2880 : hCPE->hStereoTCA = NULL;
1367 2880 : hCPE->hStereoICBWE = NULL;
1368 2880 : hCPE->hMetaData = NULL;
1369 :
1370 2880 : hCPE->hStereoCng = NULL;
1371 2880 : hCPE->hFrontVad[0] = NULL;
1372 2880 : hCPE->hFrontVad[1] = NULL;
1373 :
1374 2880 : hCPE->brate_surplus = 0;
1375 2880 : move16();
1376 : /*-----------------------------------------------------------------*
1377 : * Input memory buffer: allocate and initialize
1378 : *-----------------------------------------------------------------*/
1379 :
1380 8640 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1381 : {
1382 5760 : test();
1383 5760 : test();
1384 5760 : test();
1385 5760 : test();
1386 5760 : 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 ) )
1387 : {
1388 358 : IF( ( hCPE->input_mem_fx[n] = (Word16 *) malloc( sizeof( Word16 ) * NS2SA_FX2( input_Fs, STEREO_DFT_OVL_NS ) ) ) == NULL )
1389 : {
1390 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
1391 : }
1392 :
1393 358 : set16_zero_fx( hCPE->input_mem_fx[n], NS2SA_FX2( input_Fs, STEREO_DFT_OVL_NS ) );
1394 : }
1395 : ELSE
1396 : {
1397 5402 : hCPE->input_mem_fx[n] = NULL;
1398 : }
1399 : }
1400 2880 : set16_fx( hCPE->q_input_mem, Q15, CPE_CHANNELS );
1401 :
1402 : /*-----------------------------------------------------------------*
1403 : * stereo classifier: allocate and initialize
1404 : *-----------------------------------------------------------------*/
1405 :
1406 2880 : IF( ( hCPE->hStereoClassif = (STEREO_CLASSIF_HANDLE) malloc( sizeof( STEREO_CLASSIF_DATA ) ) ) == NULL )
1407 : {
1408 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo classifier structure\n" ) );
1409 : }
1410 :
1411 2880 : stereo_classifier_init_fx( hCPE->hStereoClassif );
1412 :
1413 : /*-----------------------------------------------------------------*
1414 : * Metadata: allocate and initialize
1415 : *-----------------------------------------------------------------*/
1416 :
1417 2880 : IF( EQ_16( cpe_id, sub( st_ivas->nCPE, 1 ) ) )
1418 : {
1419 1446 : IF( NE_32( ( error = ivas_initialize_MD_bstr_enc_fx( &( hCPE->hMetaData ), st_ivas ) ), IVAS_ERR_OK ) )
1420 : {
1421 0 : return error;
1422 : }
1423 : }
1424 :
1425 : /*-----------------------------------------------------------------*
1426 : * CoreCoder, 2 instances: allocate and initialize
1427 : *-----------------------------------------------------------------*/
1428 :
1429 8640 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1430 : {
1431 5760 : IF( ( st = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
1432 : {
1433 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
1434 : }
1435 :
1436 5760 : copy_encoder_config_fx( st_ivas, st, 1 );
1437 :
1438 5760 : IF( GT_16( st_ivas->nCPE, 1 ) )
1439 : {
1440 4832 : st->total_brate = hCPE->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() Q0*/
1441 4832 : move32();
1442 : }
1443 : ELSE
1444 : {
1445 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*/
1446 928 : move32();
1447 : }
1448 5760 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
1449 5760 : move16();
1450 5760 : 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 ) )
1451 : {
1452 0 : return error;
1453 : }
1454 :
1455 5760 : IF( st->hFdCngEnc != NULL )
1456 : {
1457 145 : st->hFdCngEnc->hFdCngCom->CngBitrate = L_sub( hCPE->element_brate, 1 ); /* Q0 */
1458 145 : move32();
1459 : }
1460 :
1461 5760 : hCPE->hCoreCoder[n] = st;
1462 : }
1463 :
1464 : /*-----------------------------------------------------------------*
1465 : * LR VAD initialization
1466 : *-----------------------------------------------------------------*/
1467 :
1468 : #ifdef DEBUGGING
1469 : 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 ) )
1470 : #else
1471 2880 : IF( hEncoderConfig->Opt_DTX_ON )
1472 : #endif
1473 : {
1474 88 : test();
1475 88 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1476 : {
1477 31 : IF( ( hCPE->hStereoCng = (STEREO_CNG_ENC_HANDLE) malloc( sizeof( STEREO_CNG_ENC ) ) ) == NULL )
1478 : {
1479 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo Cng for Unified/TD \n" ) );
1480 : }
1481 :
1482 31 : stereo_enc_cng_init_fx( hCPE->hStereoCng );
1483 : }
1484 : }
1485 : ELSE
1486 : {
1487 2792 : hCPE->hStereoCng = NULL;
1488 : }
1489 :
1490 2880 : IF( hEncoderConfig->Opt_DTX_ON && NE_16( element_mode_init, IVAS_CPE_MDCT ) )
1491 : {
1492 93 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1493 : {
1494 62 : IF( ( error = front_vad_create_fx( &( hCPE->hFrontVad[n] ), hEncoderConfig ) ) != IVAS_ERR_OK )
1495 : {
1496 0 : return error;
1497 : }
1498 : }
1499 : }
1500 : ELSE
1501 : {
1502 8547 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1503 : {
1504 5698 : hCPE->hFrontVad[n] = NULL;
1505 : }
1506 : }
1507 :
1508 : /*-----------------------------------------------------------------*
1509 : * DFT stereo initialization
1510 : *-----------------------------------------------------------------*/
1511 :
1512 2880 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1513 : {
1514 99 : IF( ( error = stereo_dft_enc_create_fx( &( hCPE->hStereoDft ), input_Fs, max_bwidth ) ) != IVAS_ERR_OK )
1515 : {
1516 0 : return error;
1517 : }
1518 : }
1519 :
1520 : /*-----------------------------------------------------------------*
1521 : * Temporal inter-channel alignment initialization
1522 : *-----------------------------------------------------------------*/
1523 :
1524 2880 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1525 : {
1526 99 : IF( ( hCPE->hStereoTCA = (STEREO_TCA_ENC_HANDLE) malloc( sizeof( STEREO_TCA_ENC_DATA ) ) ) == NULL )
1527 : {
1528 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
1529 : }
1530 :
1531 99 : stereo_tca_init_enc_fx( hCPE->hStereoTCA, input_Fs );
1532 : }
1533 :
1534 : /*-----------------------------------------------------------------*
1535 : * Stereo IC BWE initialization
1536 : *-----------------------------------------------------------------*/
1537 :
1538 2880 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1539 : {
1540 99 : IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_ENC_HANDLE) malloc( sizeof( STEREO_ICBWE_ENC_DATA ) ) ) == NULL )
1541 : {
1542 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
1543 : }
1544 :
1545 99 : stereo_icBWE_init_enc_fx( hCPE->hStereoICBWE );
1546 : }
1547 :
1548 : /*-----------------------------------------------------------------*
1549 : * TD stereo initialization
1550 : *-----------------------------------------------------------------*/
1551 :
1552 2880 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
1553 : {
1554 0 : IF( ( hCPE->hStereoTD = (STEREO_TD_ENC_DATA_HANDLE) malloc( sizeof( STEREO_TD_ENC_DATA ) ) ) == NULL )
1555 : {
1556 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
1557 : }
1558 0 : stereo_td_init_enc_fx( hCPE->hStereoTD, hCPE->last_element_mode );
1559 : }
1560 :
1561 : /*-----------------------------------------------------------------*
1562 : * MDCT stereo initialization
1563 : *-----------------------------------------------------------------*/
1564 :
1565 2880 : test();
1566 2880 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( st_ivas->nCPE, 1 ) )
1567 : {
1568 365 : IF( ( hCPE->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
1569 : {
1570 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
1571 : }
1572 :
1573 : #ifdef DEBUGGING
1574 : hCPE->hStereoMdct->mdct_stereo_mode_cmdl = st_ivas->hEncoderConfig->mdct_stereo_mode_cmdl;
1575 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
1576 : /*set all other members to defined states */
1577 : hCPE->hStereoMdct->fDualMono = 0;
1578 : hCPE->hStereoMdct->fMSstereo = 0;
1579 :
1580 : if ( hCPE->hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_LR )
1581 : {
1582 : hCPE->hStereoMdct->fDualMono = 1;
1583 : }
1584 : else if ( hCPE->hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_MS )
1585 : {
1586 : hCPE->hStereoMdct->fMSstereo = 1;
1587 : }
1588 : #endif
1589 : #endif
1590 :
1591 365 : initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 1 );
1592 :
1593 365 : test();
1594 365 : test();
1595 365 : hCPE->hStereoMdct->isSBAStereoMode = ( ( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) ) && EQ_16( st_ivas->nchan_transport, 2 ) );
1596 365 : move16();
1597 :
1598 365 : test();
1599 365 : test();
1600 365 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && LE_32( element_brate, MAX_MDCT_ITD_BRATE ) && EQ_16( ivas_format, STEREO_FORMAT ) )
1601 : {
1602 13 : IF( ( error = initMdctItdHandling_fx( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
1603 : {
1604 0 : return error;
1605 : }
1606 : }
1607 : }
1608 :
1609 2880 : st_ivas->hCPE[cpe_id] = hCPE;
1610 :
1611 2880 : return error;
1612 : }
1613 :
1614 :
1615 : /*-------------------------------------------------------------------------
1616 : * destroy_cpe_enc()
1617 : *
1618 : * Destroy and deallocate IVAS encoder CPE handle
1619 : *-------------------------------------------------------------------------*/
1620 :
1621 2880 : void destroy_cpe_enc(
1622 : CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structure */
1623 : )
1624 : {
1625 : Word16 n;
1626 : Encoder_State *st;
1627 :
1628 2880 : IF( hCPE->hStereoClassif != NULL )
1629 : {
1630 2880 : free( hCPE->hStereoClassif );
1631 2880 : hCPE->hStereoClassif = NULL;
1632 : }
1633 :
1634 8640 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1635 : {
1636 5760 : IF( hCPE->input_mem_fx[n] != NULL )
1637 : {
1638 358 : free( hCPE->input_mem_fx[n] );
1639 358 : hCPE->input_mem_fx[n] = NULL;
1640 : }
1641 : }
1642 :
1643 2880 : ivas_destroy_MD_bstr_enc_fx( &( hCPE->hMetaData ) );
1644 :
1645 8640 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1646 : {
1647 5760 : st = hCPE->hCoreCoder[n];
1648 :
1649 5760 : IF( st != NULL )
1650 : {
1651 5760 : destroy_core_enc_fx( st );
1652 5760 : st = NULL;
1653 : }
1654 : }
1655 :
1656 2880 : IF( hCPE->hStereoDft != NULL )
1657 : {
1658 81 : stereo_dft_enc_destroy_fx( &hCPE->hStereoDft );
1659 81 : hCPE->hStereoDft = NULL;
1660 : }
1661 :
1662 2880 : IF( hCPE->hStereoTD != NULL )
1663 : {
1664 1 : free( hCPE->hStereoTD );
1665 1 : hCPE->hStereoTD = NULL;
1666 : }
1667 :
1668 2880 : IF( hCPE->hStereoMdct != NULL )
1669 : {
1670 391 : stereo_mdct_enc_destroy_fx( &hCPE->hStereoMdct );
1671 391 : hCPE->hStereoMdct = NULL;
1672 : }
1673 :
1674 2880 : IF( hCPE->hStereoTCA != NULL )
1675 : {
1676 82 : free( hCPE->hStereoTCA );
1677 82 : hCPE->hStereoTCA = NULL;
1678 : }
1679 :
1680 2880 : IF( hCPE->hStereoICBWE != NULL )
1681 : {
1682 82 : free( hCPE->hStereoICBWE );
1683 82 : hCPE->hStereoICBWE = NULL;
1684 : }
1685 :
1686 2880 : IF( hCPE->hStereoCng != NULL )
1687 : {
1688 57 : free( hCPE->hStereoCng );
1689 57 : hCPE->hStereoCng = NULL;
1690 : }
1691 :
1692 2880 : IF( hCPE->hFrontVad[0] != NULL )
1693 : {
1694 84 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1695 : {
1696 56 : front_vad_destroy_fx( &hCPE->hFrontVad[n] );
1697 56 : hCPE->hFrontVad[n] = NULL;
1698 : }
1699 : }
1700 :
1701 2880 : free( hCPE );
1702 :
1703 2880 : return;
1704 : }
1705 :
1706 :
1707 : /*-------------------------------------------------------------------------
1708 : * stereo_mode_combined_format_enc()
1709 : *
1710 : * Set stereo format in a combined format
1711 : *-------------------------------------------------------------------------*/
1712 :
1713 420855 : static void stereo_mode_combined_format_enc_fx(
1714 : const Encoder_Struct *st_ivas, /* i : encoder main structure */
1715 : CPE_ENC_HANDLE hCPE /* i/o: CPE handle */
1716 : )
1717 : {
1718 : ENCODER_CONFIG_HANDLE hEncoderConfig;
1719 : Word32 element_brate_ref;
1720 :
1721 420855 : hEncoderConfig = st_ivas->hEncoderConfig;
1722 :
1723 420855 : IF( EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
1724 : {
1725 9000 : element_brate_ref = hCPE->element_brate; /* Q0 */
1726 9000 : move32();
1727 :
1728 9000 : test();
1729 9000 : test();
1730 9000 : test();
1731 9000 : test();
1732 9000 : IF( EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) &&
1733 : ( ( EQ_16( hEncoderConfig->nchan_ism, 3 ) && EQ_32( hEncoderConfig->ivas_total_brate, IVAS_96k ) ) ||
1734 : ( EQ_16( hEncoderConfig->nchan_ism, 4 ) && EQ_32( hEncoderConfig->ivas_total_brate, IVAS_128k ) ) ) )
1735 : {
1736 426 : IF( GT_32( L_add( hCPE->element_brate, hCPE->brate_surplus ), IVAS_64k ) )
1737 : {
1738 0 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = 0;
1739 0 : move16();
1740 : }
1741 : ELSE
1742 : {
1743 426 : st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = add( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, 1 ); /* Q0 */
1744 426 : move16();
1745 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 */
1746 426 : move16();
1747 : }
1748 :
1749 426 : IF( LT_16( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, OMASA_STEREO_SW_CNT_MAX ) )
1750 : {
1751 0 : hCPE->element_mode = IVAS_CPE_MDCT;
1752 0 : move16();
1753 0 : hCPE->element_brate = IVAS_64k;
1754 0 : move32();
1755 0 : hCPE->brate_surplus = L_sub( hCPE->brate_surplus, L_sub( hCPE->element_brate, element_brate_ref ) ); /* Q0 */
1756 0 : move32();
1757 : }
1758 :
1759 : /* write OMASA stereo mode signalling */
1760 426 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1761 : {
1762 0 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 1, NBITS_ELEMENT_MODE );
1763 : }
1764 : ELSE
1765 : {
1766 426 : push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 0, NBITS_ELEMENT_MODE );
1767 : }
1768 : }
1769 : }
1770 :
1771 420855 : return;
1772 : }
|