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 <math.h>
34 : #include <assert.h>
35 : #include "options.h"
36 : #include "cnst.h"
37 : #include "rom_enc.h"
38 : #include "ivas_rom_enc.h"
39 : #include "rom_com.h"
40 : #include "prot_fx.h"
41 : #include "ivas_prot_fx.h"
42 : #include "ivas_cnst.h"
43 : #include "ivas_rom_com.h"
44 : #include "wmc_auto.h"
45 :
46 : #include "prot_fx_enc.h"
47 :
48 : /*-------------------------------------------------------------------------
49 : * Local function prototypes
50 : *------------------------------------------------------------------------*/
51 :
52 : static void ivas_param_mc_write_bs_fx( const PARAM_MC_ENC_HANDLE hParamMC, Word16 *ILD_idx, Word16 *ICC_idx, UWord16 bit_buffer[PARAM_MC_MAX_BITS], Word16 *bit_pos );
53 :
54 : static void ivas_param_mc_dec2bin_fx( const Word16 val, const Word16 N, UWord16 bits[PARAM_MC_MAX_BITS] );
55 :
56 : static void ivas_param_mc_encode_parameter_fx( Word16 *idx_in, HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC, HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParameterCodingInfo, const Word16 nbands, const Word16 band_step, const Word16 map_size_wo_lfe, const Word16 map_size, UWord16 bit_buffer[PARAM_MC_MAX_BITS], Word16 *bit_pos );
57 :
58 : static void ivas_param_mc_range_encoder_fx( const Word16 *seq_in, const Word16 num_symbols, const UWord16 *cum_freq, const UWord16 *sym_freq, const UWord16 tot_shift, const Word16 max_nb_bits, UWord16 *bit_buffer, Word16 *bit_pos );
59 :
60 :
61 : #define ATTACKTHRESHOLD_E 4
62 : static void ivas_param_mc_quantize_ilds_fx( PARAM_MC_ENC_HANDLE hParamMC, Word32 Cy_fx[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], Word16 Cy_e[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], Word32 Cx_fx[PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], Word16 Cx_e[PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], const Word16 freq_idx, const Word16 nchan_input, const Word16 nchan_transport, Word16 *ILD_idx_out, Word16 ILD_q[PARAM_MC_SZ_ILD_MAP] );
63 :
64 : static void ivas_param_mc_parameter_quantizer_fx( const Word32 *x, const Word16 *x_e, const Word16 L, const Word16 sz_quantizer, const Word16 *quantizer_fx, const Word16 Q_quant, Word16 *quant_idx, Word16 *y );
65 :
66 : static void ivas_param_mc_quantize_iccs_fx( PARAM_MC_ENC_HANDLE hParamMC, Word32 Cy_fx[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], Word16 Cy_e[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], const Word16 freq_idx, const Word16 nchan_input, Word16 *ICC_idx_out );
67 :
68 : static void ivas_param_mc_dmx_fx( PARAM_MC_ENC_HANDLE hParamMC, Word32 *data_f_fx[], Word32 data_dmx_fx[][L_FRAME48k], const Word16 input_frame, const Word16 nchan_input, const Word16 nchan_transport );
69 :
70 : static void ivas_param_mc_transient_detection_fx( PARAM_MC_ENC_HANDLE hParamMC, TRAN_DET_HANDLE hTranDet, Word16 *bAttackPresent, Word16 *attackIdx );
71 :
72 : static void ivas_param_mc_param_est_enc_fx( PARAM_MC_ENC_HANDLE hParamMC, Word32 *data_f[], Word32 Cy_sum_fx[][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], Word16 Cy_sum_e[][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], Word32 Cx_sum_fx[][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], Word16 Cx_sum_e[][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], const Word16 input_frame, const Word16 nchan_input, const Word16 nchan_transport );
73 :
74 :
75 : /*-------------------------------------------------------------------------
76 : * ivas_param_mc_enc_open()
77 : *
78 : * Initialize Parametric MC encoder handle
79 : *------------------------------------------------------------------------*/
80 :
81 258 : ivas_error ivas_param_mc_enc_open_fx(
82 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
83 : )
84 : {
85 : Word16 i;
86 : IVAS_FB_CFG *fb_cfg;
87 : PARAM_MC_ENC_HANDLE hParamMC;
88 : UWord16 config_index;
89 : MC_LS_SETUP mc_input_setup;
90 : Word16 max_bwidth, nchan_inp;
91 : Word16 tmp1, tmp2;
92 : Word32 input_Fs, ivas_total_brate;
93 : ivas_error error;
94 :
95 258 : error = IVAS_ERR_OK;
96 258 : move16();
97 :
98 : /* Sanity Checks */
99 258 : IF( ( hParamMC = (PARAM_MC_ENC_HANDLE) malloc( sizeof( PARAM_MC_ENC_DATA ) ) ) == NULL )
100 : {
101 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Paramtric MC\n" ) );
102 : }
103 :
104 258 : mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
105 258 : move32();
106 258 : max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
107 258 : move16();
108 258 : input_Fs = st_ivas->hEncoderConfig->input_Fs;
109 258 : move32();
110 258 : nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
111 258 : move16();
112 258 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
113 258 : move32();
114 :
115 : /* Preparing Config */
116 258 : hParamMC->lfe_index = LFE_CHANNEL;
117 258 : move16();
118 258 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels_fx( ivas_total_brate, mc_input_setup );
119 258 : move16();
120 :
121 : /* get configuration index */
122 258 : config_index = ivas_param_mc_get_configuration_index_fx( mc_input_setup, ivas_total_brate );
123 :
124 : /* set core coder dependent on the number of transport channels */
125 258 : SWITCH( st_ivas->nchan_transport )
126 : {
127 17 : case 4:
128 : case 3:
129 17 : st_ivas->nCPE = 2;
130 17 : move16();
131 17 : st_ivas->nSCE = 0;
132 17 : move16();
133 17 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
134 17 : move16();
135 17 : BREAK;
136 241 : case 2:
137 241 : st_ivas->nCPE = 1;
138 241 : move16();
139 241 : st_ivas->nSCE = 0;
140 241 : move16();
141 241 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
142 241 : move16();
143 241 : BREAK;
144 : }
145 :
146 : /* get dmx factors */
147 258 : hParamMC->dmx_factors_fx = ivas_param_mc_conf[config_index].dmx_fac_fx; // Q31
148 :
149 : /* set FB config. */
150 258 : IF( NE_32( ( error = ivas_fb_set_cfg( &fb_cfg, MC_FORMAT, nchan_inp, 0, 0, input_Fs, 0 ) ), IVAS_ERR_OK ) )
151 : {
152 0 : return error;
153 : }
154 :
155 : /* Allocate and initialize FB mixer handle */
156 258 : IF( NE_32( ( error = ivas_FB_mixer_open_fx( &( hParamMC->hFbMixer ), input_Fs, fb_cfg, 0 ) ), IVAS_ERR_OK ) )
157 : {
158 0 : return error;
159 : }
160 :
161 : /* open/init parameter coding */
162 258 : ivas_param_mc_metadata_open_fx( mc_input_setup, ivas_total_brate, &hParamMC->hMetadataPMC );
163 :
164 : /* Band Grouping */
165 258 : IF( EQ_16( hParamMC->hMetadataPMC.num_parameter_bands, 20 ) )
166 : {
167 3 : Copy( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
168 : }
169 255 : ELSE IF( EQ_16( hParamMC->hMetadataPMC.num_parameter_bands, 14 ) )
170 : {
171 119 : Copy( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
172 : }
173 136 : ELSE IF( EQ_16( hParamMC->hMetadataPMC.num_parameter_bands, 10 ) )
174 : {
175 136 : Copy( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
176 : }
177 : ELSE
178 : {
179 0 : assert( 0 && "nbands must be 20, 14, or 10!" );
180 : }
181 :
182 : /* set max parameter band for abs cov */
183 258 : i = 0;
184 258 : move16();
185 2314 : WHILE( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
186 : {
187 2056 : hParamMC->max_param_band_abs_cov = i;
188 2056 : move16();
189 2056 : i = add( i, 1 );
190 : }
191 :
192 : /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
193 3602 : FOR( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
194 : {
195 3344 : hParamMC->band_grouping[i] = i_mult( hParamMC->band_grouping[i], PARAM_MC_CLDFB_TO_MDFT_FAC );
196 3344 : move16();
197 : }
198 :
199 : /* set correct coded band width */
200 258 : hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
201 258 : move16();
202 258 : hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
203 258 : move16();
204 258 : ivas_param_mc_set_coded_bands_fx( &hParamMC->hMetadataPMC );
205 :
206 : /* initialize offset for transient detection */
207 258 : tmp1 = NS2SA_FX2( input_Fs, DELAY_DIRAC_ENC_CMP_NS );
208 258 : move16();
209 258 : tmp2 = NS2SA_FX2( input_Fs, 2 * DIRAC_SLOT_NS );
210 258 : move16();
211 258 : tmp1 = idiv1616( sub( add( tmp1, tmp2 ), 1 ), tmp2 );
212 258 : hParamMC->transient_detector_delay = sub( ( NSUBBLOCKS_SHIFT + 1 ) + NSUBBLOCKS + 1, tmp1 );
213 258 : move16();
214 :
215 : /* Init total/dmx ener factors */
216 258 : set32_fx( hParamMC->ener_fac_fx, 0, PARAM_MC_MAX_PARAMETER_BANDS ); // Q21
217 :
218 : /* init previous ILDs */
219 5418 : FOR( i = 0; i < PARAM_MC_MAX_PARAMETER_BANDS; i++ )
220 : {
221 5160 : set32_fx( hParamMC->prev_ilds_fx[i], 0, PARAM_MC_SZ_ILD_MAP ); // Q21
222 : }
223 :
224 258 : st_ivas->hParamMC = hParamMC;
225 :
226 258 : return error;
227 : }
228 :
229 :
230 : /*-------------------------------------------------------------------------
231 : * ivas_param_mc_enc_reconfig()
232 : *
233 : * Reconfigure Parametric MC encoder
234 : *------------------------------------------------------------------------*/
235 :
236 60 : ivas_error ivas_param_mc_enc_reconfig_fx(
237 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
238 : )
239 : {
240 : Word16 i;
241 : PARAM_MC_ENC_HANDLE hParamMC;
242 : UWord16 config_index;
243 : MC_LS_SETUP mc_input_setup;
244 : Word16 max_bwidth;
245 : Word16 tmp1, tmp2;
246 : Word32 input_Fs, ivas_total_brate;
247 : ivas_error error;
248 :
249 60 : error = IVAS_ERR_OK;
250 60 : move16();
251 :
252 60 : mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
253 60 : move32();
254 60 : max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
255 60 : move16();
256 60 : input_Fs = st_ivas->hEncoderConfig->input_Fs;
257 60 : move32();
258 60 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
259 60 : move32();
260 60 : hParamMC = st_ivas->hParamMC;
261 :
262 : /* Preparing Config */
263 60 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels_fx( ivas_total_brate, mc_input_setup );
264 60 : move16();
265 :
266 : /* get configuration index */
267 60 : config_index = ivas_param_mc_get_configuration_index_fx( mc_input_setup, ivas_total_brate );
268 :
269 : /* set core coder dependent on the number of transport channels */
270 60 : SWITCH( st_ivas->nchan_transport )
271 : {
272 0 : case 4:
273 : case 3:
274 0 : st_ivas->nCPE = 2;
275 0 : move16();
276 0 : st_ivas->nSCE = 0;
277 0 : move16();
278 0 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
279 0 : move16();
280 0 : BREAK;
281 60 : case 2:
282 60 : st_ivas->nCPE = 1;
283 60 : move16();
284 60 : st_ivas->nSCE = 0;
285 60 : move16();
286 60 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
287 60 : move16();
288 60 : BREAK;
289 : }
290 :
291 : /* get dmx factors */
292 60 : hParamMC->dmx_factors_fx = ivas_param_mc_conf[config_index].dmx_fac_fx; // Q31
293 :
294 : /* open/init parameter coding */
295 60 : ivas_param_mc_metadata_open_fx( mc_input_setup, ivas_total_brate, &hParamMC->hMetadataPMC );
296 :
297 : /* Band Grouping */
298 60 : IF( EQ_16( hParamMC->hMetadataPMC.num_parameter_bands, 20 ) )
299 : {
300 0 : Copy( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
301 : }
302 60 : ELSE IF( EQ_16( hParamMC->hMetadataPMC.num_parameter_bands, 14 ) )
303 : {
304 30 : Copy( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
305 : }
306 30 : ELSE IF( EQ_16( hParamMC->hMetadataPMC.num_parameter_bands, 10 ) )
307 : {
308 30 : Copy( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
309 : }
310 : ELSE
311 : {
312 0 : assert( 0 && "nbands must be 20, 14, or 10!" );
313 : }
314 :
315 : /* set max parameter band for abs cov */
316 60 : i = 0;
317 60 : move16();
318 540 : WHILE( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
319 : {
320 480 : hParamMC->max_param_band_abs_cov = i;
321 480 : move16();
322 480 : i = add( i, 1 );
323 : }
324 :
325 : /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
326 840 : FOR( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
327 : {
328 780 : hParamMC->band_grouping[i] = i_mult( hParamMC->band_grouping[i], PARAM_MC_CLDFB_TO_MDFT_FAC );
329 780 : move16();
330 : }
331 :
332 : /* set correct coded band width */
333 60 : hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
334 60 : move16();
335 60 : hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
336 60 : move16();
337 60 : ivas_param_mc_set_coded_bands_fx( &hParamMC->hMetadataPMC );
338 :
339 : /* initialize offset for transient detection */
340 60 : tmp1 = NS2SA_FX2( input_Fs, DELAY_DIRAC_ENC_CMP_NS );
341 60 : move16();
342 60 : tmp2 = NS2SA_FX2( input_Fs, 2 * DIRAC_SLOT_NS );
343 60 : move16();
344 60 : tmp1 = idiv1616( sub( add( tmp1, tmp2 ), 1 ), tmp2 );
345 60 : hParamMC->transient_detector_delay = sub( ( NSUBBLOCKS_SHIFT + 1 ) + NSUBBLOCKS + 1, tmp1 );
346 60 : move16();
347 :
348 : /* Init total/dmx ener factors */
349 60 : set32_fx( hParamMC->ener_fac_fx, 0, PARAM_MC_MAX_PARAMETER_BANDS ); // Q21
350 :
351 :
352 60 : return error;
353 : }
354 :
355 :
356 : /*-------------------------------------------------------------------------
357 : * ivas_param_mc_enc_close()
358 : *
359 : * Close Parametric MC encoder handle
360 : *------------------------------------------------------------------------*/
361 :
362 1164 : void ivas_param_mc_enc_close_fx(
363 : PARAM_MC_ENC_HANDLE *hParamMC, /* i/o: Parametric MC encoder handle */
364 : const Word32 sampling_rate )
365 : {
366 1164 : test();
367 1164 : IF( hParamMC == NULL || *hParamMC == NULL )
368 : {
369 906 : return;
370 : }
371 :
372 258 : ivas_FB_mixer_close_fx( &( *hParamMC )->hFbMixer, sampling_rate, 0 );
373 :
374 258 : free( ( *hParamMC ) );
375 258 : ( *hParamMC ) = NULL;
376 :
377 258 : return;
378 : }
379 :
380 :
381 : /*-------------------------------------------------------------------------
382 : * ivas_param_mc_enc()
383 : *
384 : * Parametric MC Encoder main encoding function
385 : *------------------------------------------------------------------------*/
386 :
387 9960 : void ivas_param_mc_enc_fx(
388 : Encoder_Struct *st_ivas, /* i/o: IVAS Encoder handle */
389 : BSTR_ENC_HANDLE hMetaData, /* i/o: IVAS Metadata bitstream handle */
390 : Word32 *data_f_fx[], /* i/o: input/transport MC data Q11 */
391 : const Word16 input_frame /* i : input frame length */
392 : )
393 : {
394 : Word16 k;
395 : Word16 ILD_idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
396 : Word16 ICC_idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ICC_MAP];
397 : UWord16 bit_buffer[PARAM_MC_MAX_BITS];
398 : Word16 bit_pos;
399 : Word16 band_step;
400 : Word16 data_f_fx16[L_FRAME48k];
401 : Word32 data_dmx_fx[PARAM_MC_MAX_TRANSPORT_CHANS][L_FRAME48k];
402 : Word16 data_dmx_fx16[PARAM_MC_MAX_TRANSPORT_CHANS][L_FRAME48k];
403 : Word32 Cy_sum_fx[PARAM_MC_MAX_PARAMETER_BANDS][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS];
404 : Word16 Cy_sum_e[PARAM_MC_MAX_PARAMETER_BANDS][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS];
405 : Word32 Cx_sum_fx[PARAM_MC_MAX_PARAMETER_BANDS][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS];
406 : Word16 Cx_sum_e[PARAM_MC_MAX_PARAMETER_BANDS][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS];
407 : Word16 ILD_q_fx[PARAM_MC_MAX_PARAMETER_BANDS][PARAM_MC_SZ_ILD_MAP];
408 : Word16 ch;
409 : Word16 band;
410 : PARAM_MC_ENC_HANDLE hParamMC;
411 : Word16 nchan_inp;
412 :
413 9960 : push_wmops( "param_mc_enc" );
414 :
415 : /* initializations */
416 9960 : hParamMC = st_ivas->hParamMC;
417 9960 : bit_pos = 0;
418 9960 : move16();
419 9960 : band_step = 1;
420 9960 : move16();
421 9960 : nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
422 9960 : move16();
423 :
424 209160 : FOR( band = 0; band < PARAM_MC_MAX_PARAMETER_BANDS; band++ )
425 : {
426 3386400 : FOR( ch = 0; ch < MAX_CICP_CHANNELS; ch++ )
427 : {
428 3187200 : set32_fx( Cy_sum_fx[band][ch], 0, MAX_CICP_CHANNELS );
429 3187200 : set16_fx( Cy_sum_e[band][ch], 0, MAX_CICP_CHANNELS );
430 : }
431 796800 : FOR( ch = 0; ch < PARAM_MC_MAX_TRANSPORT_CHANS; ch++ )
432 : {
433 597600 : set32_fx( Cx_sum_fx[band][ch], 0, PARAM_MC_MAX_TRANSPORT_CHANS );
434 597600 : set16_fx( Cx_sum_e[band][ch], 0, PARAM_MC_MAX_TRANSPORT_CHANS );
435 : }
436 : }
437 :
438 9960 : set16_fx( ILD_idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
439 9960 : set16_fx( ICC_idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ICC_MAP );
440 :
441 : /* update parameter frame index */
442 9960 : hParamMC->hMetadataPMC.param_frame_idx = add( hParamMC->hMetadataPMC.param_frame_idx, 1 ) % PARAM_MC_PARAMETER_FRAMES;
443 9960 : move16();
444 :
445 : /* DMX generation*/
446 9960 : ivas_param_mc_dmx_fx( hParamMC, data_f_fx, data_dmx_fx, input_frame, nchan_inp, st_ivas->nchan_transport );
447 :
448 : /* Transient Detector */
449 9960 : SWITCH( st_ivas->nchan_transport )
450 : {
451 9960 : case 3:
452 : case 2:
453 : case 4:
454 : {
455 : Word16 q_data_dmx_fx16;
456 : Word16 bAttackPresent[PARAM_MC_MAX_TRANSPORT_CHANS];
457 : Word16 attackIdx[PARAM_MC_MAX_TRANSPORT_CHANS];
458 :
459 9960 : set16_fx( attackIdx, -1, PARAM_MC_MAX_TRANSPORT_CHANS );
460 9960 : set16_fx( bAttackPresent, 0, PARAM_MC_MAX_TRANSPORT_CHANS );
461 :
462 29990 : FOR( ch = 0; ch < st_ivas->nchan_transport; ch++ )
463 : {
464 20030 : Word16 cpe_idx = shr( ch, 1 );
465 :
466 20030 : q_data_dmx_fx16 = sub( L_norm_arr( data_dmx_fx[ch], input_frame ), 16 );
467 20030 : Copy_Scale_sig_32_16( data_dmx_fx[ch], data_dmx_fx16[ch], input_frame, q_data_dmx_fx16 ); // Q11 -> Q(q_data_dmx_fx16 + 11)
468 20030 : q_data_dmx_fx16 = add( q_data_dmx_fx16, Q11 );
469 :
470 20030 : RunTransientDetection_ivas_fx( data_dmx_fx16[ch], input_frame, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet, q_data_dmx_fx16 );
471 :
472 20030 : ivas_param_mc_transient_detection_fx( hParamMC, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet, &bAttackPresent[ch], &attackIdx[ch] );
473 : }
474 :
475 : /* if more than one attack, use the earlier */
476 9960 : hParamMC->hMetadataPMC.bAttackPresent = 0;
477 9960 : move16();
478 9960 : hParamMC->hMetadataPMC.attackIndex = 16;
479 9960 : move16();
480 :
481 29990 : FOR( ch = 0; ch < st_ivas->nchan_transport; ch++ )
482 : {
483 20030 : hParamMC->hMetadataPMC.bAttackPresent = s_max( hParamMC->hMetadataPMC.bAttackPresent, bAttackPresent[ch] );
484 20030 : move16();
485 : }
486 :
487 9960 : IF( hParamMC->hMetadataPMC.bAttackPresent )
488 : {
489 1440 : FOR( ch = 0; ch < st_ivas->nchan_transport; ch++ )
490 : {
491 970 : hParamMC->hMetadataPMC.attackIndex = s_min( hParamMC->hMetadataPMC.attackIndex, attackIdx[ch] );
492 970 : move16();
493 : }
494 : }
495 : ELSE
496 : {
497 9490 : hParamMC->hMetadataPMC.attackIndex = 0;
498 9490 : move16();
499 : }
500 : }
501 9960 : BREAK;
502 : }
503 :
504 : /* Encoding */
505 : /* parameter estimation*/
506 9960 : ivas_param_mc_param_est_enc_fx( hParamMC, data_f_fx, Cy_sum_fx, Cy_sum_e, Cx_sum_fx, Cx_sum_e, input_frame, nchan_inp, st_ivas->nchan_transport );
507 :
508 9960 : IF( hParamMC->hMetadataPMC.bAttackPresent )
509 : {
510 470 : band_step = PARAM_MC_TRANSIENT_BAND_STEP;
511 470 : move16();
512 : }
513 : ELSE
514 : {
515 9490 : band_step = 1;
516 9490 : move16();
517 : }
518 :
519 :
520 : /* ILD parameter quantization */
521 138827 : FOR( k = 0; k < hParamMC->hMetadataPMC.nbands_coded; k += band_step )
522 : {
523 128867 : ivas_param_mc_quantize_ilds_fx( hParamMC, Cy_sum_fx[k], Cy_sum_e[k], Cx_sum_fx[k], Cx_sum_e[k], k, nchan_inp, st_ivas->nchan_transport, ILD_idx, ILD_q_fx[k] );
524 : }
525 :
526 : /* ICC parameter quantization */
527 138827 : FOR( k = 0; k < hParamMC->hMetadataPMC.nbands_coded; k += band_step )
528 : {
529 128867 : ivas_param_mc_quantize_iccs_fx( hParamMC, Cy_sum_fx[k], Cy_sum_e[k], k, nchan_inp, ICC_idx );
530 : }
531 :
532 : /* time domain DMX generation*/
533 : /* just copy data_dmx generated above, contains already the downmix */
534 29990 : FOR( ch = 0; ch < st_ivas->nchan_transport; ch++ )
535 : {
536 20030 : Copy32( data_dmx_fx[ch], data_f_fx[ch], input_frame ); // q_data_dmx_fx16
537 : }
538 :
539 : /* we have to run the transient detector on the second channel of the last CPE if we
540 : have an odd number of transport channels */
541 9960 : IF( GT_16( st_ivas->nchan_transport, 2 ) )
542 : {
543 220 : FOR( ; ch < st_ivas->nCPE * CPE_CHANNELS; ch++ )
544 : {
545 110 : Word16 cpe_idx = shr( ch, 1 );
546 :
547 110 : set32_fx( data_f_fx[ch], 0, input_frame ); // Q11
548 110 : set16_fx( data_f_fx16, 0, input_frame ); // Q11
549 :
550 110 : RunTransientDetection_ivas_fx( data_f_fx16, input_frame, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet, 0 );
551 : }
552 : }
553 :
554 : /* write Parametric MC side info bitstream into temporary buffer*/
555 9960 : ivas_param_mc_write_bs_fx( hParamMC, ILD_idx, ICC_idx, bit_buffer, &bit_pos );
556 :
557 : /* push the Parametric MC side info from the temporary buffer into the medatdata bitstream*/
558 9960 : push_next_bits( hMetaData, bit_buffer, bit_pos );
559 :
560 : /* updates */
561 9960 : hParamMC->hMetadataPMC.last_coded_bwidth = hParamMC->hMetadataPMC.coded_bwidth;
562 9960 : move16();
563 :
564 9960 : pop_wmops();
565 :
566 9960 : return;
567 : }
568 :
569 :
570 : /*****************************************************************************************/
571 : /* local functions */
572 : /*****************************************************************************************/
573 :
574 : /*-------------------------------------------------------------------------
575 : * ivas_param_mc_dmx()
576 : *
577 : * Computes the time domain down mix signal
578 : *------------------------------------------------------------------------*/
579 :
580 9960 : static void ivas_param_mc_dmx_fx(
581 : PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle */
582 : Word32 *data_f_fx[], /* i : Input frame Q_x */
583 : Word32 data_dmx_fx[][L_FRAME48k], /* o : Down mixed frame Q_x - 11 */
584 : const Word16 input_frame, /* i : Input frame length */
585 : const Word16 nchan_input, /* i : number of input channels */
586 : const Word16 nchan_transport /* i : number of transport channels */
587 : )
588 : {
589 : Word16 i;
590 : const Word16 *idx;
591 : Word16 dmx_ch;
592 : Word16 inp_ch;
593 : const Word32 *p_dmx_fac_fx;
594 :
595 9960 : idx = Param_MC_index;
596 9462760 : FOR( i = 0; i < input_frame; i++ )
597 : {
598 9452800 : p_dmx_fac_fx = hParamMC->dmx_factors_fx;
599 28464000 : FOR( dmx_ch = 0; dmx_ch < nchan_transport; dmx_ch++ )
600 : {
601 19011200 : Word32 *dmx_sample_fx = &data_dmx_fx[idx[dmx_ch]][i];
602 19011200 : *dmx_sample_fx = 0;
603 19011200 : move16();
604 136515200 : FOR( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
605 : {
606 117504000 : ( *dmx_sample_fx ) = Madd_32_32( ( *dmx_sample_fx ), data_f_fx[idx[inp_ch]][i], ( *( p_dmx_fac_fx++ ) ) ); // Q_x - 11
607 117504000 : move16();
608 : }
609 : }
610 : }
611 :
612 9960 : return;
613 : }
614 :
615 :
616 : /*-------------------------------------------------------------------------
617 : * ivas_param_mc_param_est_enc()
618 : *
619 : * run the CLDFB analysis on the input signal
620 : * estimate the input and down mix covariances
621 : *------------------------------------------------------------------------*/
622 :
623 9960 : static void ivas_param_mc_param_est_enc_fx(
624 : PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle */
625 : Word32 *data_f_fx[], /* i : Input frame in the time domain Q11 */
626 : Word32 Cy_sum_fx[][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* o : Covariance matrix for the original frame Cy_sum_e*/
627 : Word16 Cy_sum_e[][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* o : Covariance matrix for the original frame */
628 : Word32 Cx_sum_fx[][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], /* o : Covariance matrix for the downmixed frame Cx_sum_e*/
629 : Word16 Cx_sum_e[][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], /* o : Covariance matrix for the downmixed frame */
630 : const Word16 input_frame, /* i : Input frame length */
631 : const Word16 nchan_input, /* i : number of input channels */
632 : const Word16 nchan_transport /* i : number of transport channels */
633 : )
634 : {
635 : Word16 i, cur_cldfb_band, cur_param_band, ch_idx1, ch_idx2, inp_ch;
636 : Word16 ts;
637 : Word16 l_ts;
638 : Word16 num_time_slots;
639 : Word16 num_parameter_bands;
640 : Word16 brange[2];
641 : Word16 band_step;
642 9960 : const Word16 *map_ls = Param_MC_index; /* Loudspeakers mapping */
643 : Word16 idx_ls;
644 : Word16 start_ts;
645 :
646 : Word32 *pcm_in_fx[MAX_CICP_CHANNELS];
647 : Word32 slot_frame_f_real_fx[MAX_CICP_CHANNELS][DIRAC_NO_FB_BANDS_MAX]; /* Output of the MDFT FB - real part */
648 : Word32 slot_frame_f_imag_fx[MAX_CICP_CHANNELS][DIRAC_NO_FB_BANDS_MAX]; /* Output of the MDFT FB - imag part */
649 : Word32 *p_slot_frame_f_real_fx[MAX_CICP_CHANNELS]; /* Output of the MDFT FB - real part */
650 : Word32 *p_slot_frame_f_imag_fx[MAX_CICP_CHANNELS]; /* Output of the MDFT FB - imag part */
651 :
652 : Word32 dmx_real_fx[PARAM_MC_MAX_TRANSPORT_CHANS]; /* Downmix channel - Real Part */
653 : Word16 dmx_real_e[PARAM_MC_MAX_TRANSPORT_CHANS]; /* Downmix channel - Real Part */
654 : Word32 dmx_imag_fx[PARAM_MC_MAX_TRANSPORT_CHANS]; /* Downmix channel - Imag Part */
655 : Word16 dmx_imag_e[PARAM_MC_MAX_TRANSPORT_CHANS]; /* Downmix channel - Imag Part */
656 : Word32 a_fx, b_fx, c_fx, d_fx; /* Tmp complex values */
657 : Word16 a_e, b_e, c_e, d_e; /* Tmp complex values */
658 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
659 : Word64 Cy_sum_real_64[PARAM_MC_MAX_PARAMETER_BANDS][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS];
660 : Word64 Cy_sum_imag_64[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS];
661 : #else
662 : Word32 Cy_sum_imag_fx[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS];
663 : Word16 Cy_sum_imag_e[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS];
664 : #endif
665 : Word32 Cx_sum_imag_fx[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS];
666 : Word16 Cx_sum_imag_e[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS];
667 : Word32 real_part_fx, imag_part_fx;
668 : Word16 real_part_e, imag_part_e;
669 : const Word32 *p_dmx_fac_fx;
670 : Word32 L_tmp;
671 : Word16 tmp_e;
672 :
673 9960 : push_wmops( "param_mc_prm_est" );
674 :
675 : /* initializations */
676 9960 : l_ts = extract_l( Mpy_32_16_1( input_frame, INV_PARAM_MC_MDFT_NO_SLOTS_FX ) );
677 9960 : num_time_slots = PARAM_MC_MDFT_NO_SLOTS;
678 9960 : move16();
679 9960 : IF( hParamMC->hMetadataPMC.bAttackPresent )
680 : {
681 470 : start_ts = hParamMC->hMetadataPMC.attackIndex;
682 470 : move16();
683 : }
684 : ELSE
685 : {
686 9490 : start_ts = 0;
687 9490 : move16();
688 : }
689 9960 : num_parameter_bands = hParamMC->hMetadataPMC.nbands_coded;
690 9960 : move16();
691 9960 : band_step = 1;
692 9960 : move16();
693 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
694 209160 : FOR( cur_param_band = 0; cur_param_band < PARAM_MC_MAX_PARAMETER_BANDS; cur_param_band++ )
695 : {
696 3386400 : FOR( ch_idx1 = 0; ch_idx1 < MAX_CICP_CHANNELS; ch_idx1++ )
697 : {
698 3187200 : set64_fx( Cy_sum_real_64[cur_param_band][ch_idx1], 0, MAX_CICP_CHANNELS );
699 : }
700 : }
701 : #endif
702 :
703 109560 : FOR( cur_param_band = 0; cur_param_band < PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC; cur_param_band++ )
704 : {
705 1693200 : FOR( ch_idx1 = 0; ch_idx1 < MAX_CICP_CHANNELS; ch_idx1++ )
706 : {
707 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
708 1593600 : set64_fx( Cy_sum_imag_64[cur_param_band][ch_idx1], 0, MAX_CICP_CHANNELS );
709 : #else
710 : set32_fx( Cy_sum_fx[cur_param_band][ch_idx1], 0, MAX_CICP_CHANNELS );
711 : set16_fx( Cy_sum_e[cur_param_band][ch_idx1], 0, MAX_CICP_CHANNELS );
712 : #endif
713 : }
714 :
715 398400 : FOR( ch_idx1 = 0; ch_idx1 < PARAM_MC_MAX_TRANSPORT_CHANS; ch_idx1++ )
716 : {
717 298800 : set32_fx( Cx_sum_imag_fx[cur_param_band][ch_idx1], 0, PARAM_MC_MAX_TRANSPORT_CHANS );
718 298800 : set16_fx( Cx_sum_imag_e[cur_param_band][ch_idx1], 0, PARAM_MC_MAX_TRANSPORT_CHANS );
719 : }
720 : }
721 :
722 : /* Copy current frame to memory for delay compensation */
723 71240 : FOR( i = 0; i < nchan_input; i++ )
724 : {
725 61280 : idx_ls = map_ls[i];
726 61280 : move16();
727 61280 : pcm_in_fx[i] = data_f_fx[idx_ls];
728 61280 : p_slot_frame_f_real_fx[i] = &slot_frame_f_real_fx[i][0];
729 61280 : p_slot_frame_f_imag_fx[i] = &slot_frame_f_imag_fx[i][0];
730 : }
731 :
732 11545 : FOR( ts = 0; ts < start_ts; ts++ )
733 : {
734 1585 : ivas_fb_mixer_update_prior_input_fx( hParamMC->hFbMixer, pcm_in_fx, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
735 11949 : FOR( i = 0; i < nchan_input; i++ )
736 : {
737 10364 : pcm_in_fx[i] += l_ts;
738 : }
739 : }
740 :
741 : #if defined( IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_BE ) || defined( IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_NONBE )
742 9960 : Word16 gb = find_guarded_bits_fx( l_ts );
743 9960 : Word16 add20gb = add( 20, gb );
744 : #endif
745 :
746 88055 : FOR( ts = start_ts; ts < num_time_slots; ts++ )
747 : {
748 : #if !defined( IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_BE ) && !defined( IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_NONBE )
749 : Word16 gb = find_guarded_bits_fx( l_ts );
750 : #endif
751 78095 : ivas_fb_mixer_get_windowed_fr_fx( hParamMC->hFbMixer, pcm_in_fx, p_slot_frame_f_real_fx, p_slot_frame_f_imag_fx, l_ts, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans, gb );
752 78095 : ivas_fb_mixer_update_prior_input_fx( hParamMC->hFbMixer, pcm_in_fx, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
753 :
754 : /* slot_frame_f buffer Q = 11 - gb : exponent = 20 + gb */
755 :
756 557971 : FOR( i = 0; i < nchan_input; i++ )
757 : {
758 479876 : pcm_in_fx[i] += l_ts;
759 479876 : move32();
760 : }
761 : /* Computing the downmix */
762 674617 : FOR( cur_param_band = 0; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band++ )
763 : {
764 596522 : brange[0] = hParamMC->band_grouping[cur_param_band];
765 596522 : move16();
766 596522 : brange[1] = hParamMC->band_grouping[cur_param_band + 1];
767 596522 : move16();
768 :
769 2158422 : FOR( cur_cldfb_band = brange[0]; cur_cldfb_band < brange[1]; cur_cldfb_band++ )
770 : {
771 : /* Cx for DMX */
772 : /* Real Part */
773 1561900 : p_dmx_fac_fx = hParamMC->dmx_factors_fx;
774 :
775 4701460 : FOR( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
776 : {
777 : #ifndef IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_BE
778 : dmx_real_fx[ch_idx1] = 0;
779 : move32();
780 : dmx_real_e[ch_idx1] = 0;
781 : move16();
782 : dmx_imag_fx[ch_idx1] = 0;
783 : move32();
784 : dmx_imag_e[ch_idx1] = 0;
785 : move16();
786 : FOR( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
787 : {
788 : L_tmp = Mpy_32_32( slot_frame_f_real_fx[inp_ch][cur_cldfb_band], ( *p_dmx_fac_fx ) );
789 : dmx_real_fx[ch_idx1] = BASOP_Util_Add_Mant32Exp( dmx_real_fx[ch_idx1], dmx_real_e[ch_idx1], L_tmp, add( 20, gb ), &dmx_real_e[ch_idx1] );
790 : move32();
791 : L_tmp = Mpy_32_32( slot_frame_f_imag_fx[inp_ch][cur_cldfb_band], ( *p_dmx_fac_fx ) );
792 : dmx_imag_fx[ch_idx1] = BASOP_Util_Add_Mant32Exp( dmx_imag_fx[ch_idx1], dmx_imag_e[ch_idx1], L_tmp, add( 20, gb ), &dmx_imag_e[ch_idx1] );
793 : move32();
794 : p_dmx_fac_fx++;
795 : }
796 : #else
797 3139560 : Word32 real_fx = L_add( 0, 0 );
798 3139560 : Word16 real_e = add( 0, 0 );
799 3139560 : Word32 imag_fx = L_add( 0, 0 );
800 3139560 : Word16 imag_e = add( 0, 0 );
801 22506440 : FOR( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
802 : {
803 19366880 : L_tmp = Mpy_32_32( slot_frame_f_real_fx[inp_ch][cur_cldfb_band], ( *p_dmx_fac_fx ) );
804 19366880 : real_fx = BASOP_Util_Add_Mant32Exp( real_fx, real_e, L_tmp, add20gb, &real_e );
805 19366880 : L_tmp = Mpy_32_32( slot_frame_f_imag_fx[inp_ch][cur_cldfb_band], ( *p_dmx_fac_fx ) );
806 19366880 : imag_fx = BASOP_Util_Add_Mant32Exp( imag_fx, imag_e, L_tmp, add20gb, &imag_e );
807 19366880 : p_dmx_fac_fx++;
808 : }
809 3139560 : dmx_real_fx[ch_idx1] = real_fx;
810 3139560 : dmx_real_e[ch_idx1] = real_e;
811 3139560 : dmx_imag_fx[ch_idx1] = imag_fx;
812 3139560 : dmx_imag_e[ch_idx1] = imag_e;
813 3139560 : move32();
814 3139560 : move16();
815 3139560 : move32();
816 3139560 : move16();
817 : #endif
818 : }
819 :
820 : /* Cx for transport channels */
821 4701460 : FOR( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
822 : {
823 9465960 : FOR( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
824 : {
825 6326400 : a_fx = dmx_real_fx[ch_idx1];
826 6326400 : move32();
827 6326400 : a_e = dmx_real_e[ch_idx1];
828 6326400 : move16();
829 6326400 : b_fx = dmx_imag_fx[ch_idx1];
830 6326400 : move32();
831 6326400 : b_e = dmx_imag_e[ch_idx1];
832 6326400 : move16();
833 6326400 : c_fx = dmx_real_fx[ch_idx2];
834 6326400 : move32();
835 6326400 : c_e = dmx_real_e[ch_idx2];
836 6326400 : move16();
837 6326400 : d_fx = dmx_imag_fx[ch_idx2];
838 6326400 : move32();
839 6326400 : d_e = dmx_imag_e[ch_idx2];
840 6326400 : move16();
841 :
842 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
843 6326400 : L_tmp = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, c_fx ), add( a_e, c_e ), Mpy_32_32( b_fx, d_fx ), add( b_e, d_e ), &tmp_e );
844 12652800 : Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cx_sum_e[cur_param_band][ch_idx1][ch_idx2],
845 6326400 : L_tmp, tmp_e, &Cx_sum_e[cur_param_band][ch_idx1][ch_idx2] );
846 6326400 : move32();
847 6326400 : L_tmp = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, d_fx ), add( a_e, d_e ), L_negate( Mpy_32_32( b_fx, c_fx ) ), add( b_e, c_e ), &tmp_e );
848 12652800 : Cx_sum_imag_fx[cur_param_band][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cx_sum_imag_fx[cur_param_band][ch_idx1][ch_idx2], Cx_sum_imag_e[cur_param_band][ch_idx1][ch_idx2],
849 6326400 : L_tmp, tmp_e, &Cx_sum_imag_e[cur_param_band][ch_idx1][ch_idx2] );
850 6326400 : move32();
851 : }
852 : }
853 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
854 11159420 : FOR( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
855 : {
856 9597520 : a_fx = slot_frame_f_real_fx[ch_idx1][cur_cldfb_band];
857 9597520 : b_fx = slot_frame_f_imag_fx[ch_idx1][cur_cldfb_band];
858 9597520 : move32();
859 44230600 : FOR( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
860 : {
861 : Word16 norm;
862 34633080 : c_fx = slot_frame_f_real_fx[ch_idx2][cur_cldfb_band];
863 34633080 : d_fx = slot_frame_f_imag_fx[ch_idx2][cur_cldfb_band];
864 34633080 : move32();
865 : // Conjugated complex multiplication (a-ib)(c+id) = ac+bd + i(ad-bc)
866 34633080 : Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2] = W_add( Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2],
867 : W_add( W_mult0_32_32( a_fx, c_fx ), W_mult0_32_32( b_fx, d_fx ) ) );
868 34633080 : move64();
869 34633080 : Cy_sum_imag_64[cur_param_band][ch_idx1][ch_idx2] = W_add( Cy_sum_imag_64[cur_param_band][ch_idx1][ch_idx2],
870 : W_sub( W_mult0_32_32( a_fx, d_fx ), W_mult0_32_32( b_fx, c_fx ) ) );
871 34633080 : move64();
872 :
873 : // convert the 64 bit fixpoint back into the 48 bit float format
874 34633080 : norm = W_norm( Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2] );
875 34633080 : Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2] = W_extract_h( W_shl( Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2], norm ) );
876 34633080 : Cy_sum_e[cur_param_band][ch_idx1][ch_idx2] = sub( sub( 62, gb ), norm );
877 : }
878 : }
879 : #else
880 : /* Cy for input channels */
881 : FOR( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
882 : {
883 : #ifdef IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_NONBE
884 : a_e = norm_l( slot_frame_f_real_fx[ch_idx1][cur_cldfb_band] );
885 : a_fx = L_shl( slot_frame_f_real_fx[ch_idx1][cur_cldfb_band], a_e );
886 : a_e = sub( add20gb, a_e );
887 : if ( a_fx == 0 )
888 : {
889 : a_e = 0;
890 : move16();
891 : }
892 : b_e = norm_l( slot_frame_f_imag_fx[ch_idx1][cur_cldfb_band] );
893 : b_fx = L_shl( slot_frame_f_imag_fx[ch_idx1][cur_cldfb_band], b_e );
894 : b_e = sub( add20gb, b_e );
895 : if ( b_fx == 0 )
896 : {
897 : b_e = 0;
898 : move16();
899 : }
900 : #endif
901 : FOR( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
902 : {
903 : #ifndef IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_NONBE
904 : a_fx = BASOP_Util_Add_Mant32Exp( slot_frame_f_real_fx[ch_idx1][cur_cldfb_band], add( 20, gb ), 0, 0, &a_e );
905 : b_fx = BASOP_Util_Add_Mant32Exp( slot_frame_f_imag_fx[ch_idx1][cur_cldfb_band], add( 20, gb ), 0, 0, &b_e );
906 : c_fx = BASOP_Util_Add_Mant32Exp( slot_frame_f_real_fx[ch_idx2][cur_cldfb_band], add( 20, gb ), 0, 0, &c_e );
907 : d_fx = BASOP_Util_Add_Mant32Exp( slot_frame_f_imag_fx[ch_idx2][cur_cldfb_band], add( 20, gb ), 0, 0, &d_e );
908 : #else
909 : c_e = norm_l( slot_frame_f_real_fx[ch_idx2][cur_cldfb_band] );
910 : c_fx = L_shl( slot_frame_f_real_fx[ch_idx2][cur_cldfb_band], c_e );
911 : c_e = sub( add20gb, c_e );
912 : if ( c_fx == 0 )
913 : {
914 : c_e = 0;
915 : move16();
916 : }
917 : d_e = norm_l( slot_frame_f_imag_fx[ch_idx2][cur_cldfb_band] );
918 : d_fx = L_shl( slot_frame_f_imag_fx[ch_idx2][cur_cldfb_band], d_e );
919 : d_e = sub( add20gb, d_e );
920 : if ( d_fx == 0 )
921 : {
922 : d_e = 0;
923 : move16();
924 : }
925 : #endif
926 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
927 : L_tmp = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, c_fx ), add( a_e, c_e ), Mpy_32_32( b_fx, d_fx ), add( b_e, d_e ), &tmp_e );
928 : Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cy_sum_e[cur_param_band][ch_idx1][ch_idx2],
929 : L_tmp, tmp_e, &Cy_sum_e[cur_param_band][ch_idx1][ch_idx2] );
930 : move32();
931 : L_tmp = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, d_fx ), add( a_e, d_e ), L_negate( Mpy_32_32( b_fx, c_fx ) ), add( b_e, c_e ), &tmp_e );
932 : Cy_sum_imag_fx[cur_param_band][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cy_sum_imag_fx[cur_param_band][ch_idx1][ch_idx2], Cy_sum_imag_e[cur_param_band][ch_idx1][ch_idx2],
933 : L_tmp, tmp_e, &Cy_sum_imag_e[cur_param_band][ch_idx1][ch_idx2] );
934 : move32();
935 : }
936 : }
937 : #endif
938 : }
939 : }
940 :
941 516302 : FOR( ; cur_param_band < num_parameter_bands; cur_param_band++ )
942 : {
943 438207 : brange[0] = hParamMC->band_grouping[cur_param_band];
944 438207 : move16();
945 438207 : brange[1] = hParamMC->band_grouping[cur_param_band + 1];
946 438207 : move16();
947 :
948 8145427 : FOR( cur_cldfb_band = brange[0]; cur_cldfb_band < brange[1]; cur_cldfb_band++ )
949 : {
950 : /* Cx for DMX */
951 : /* Real Part */
952 7707220 : p_dmx_fac_fx = hParamMC->dmx_factors_fx; // Q31
953 :
954 23200460 : FOR( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
955 : {
956 : #ifndef IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_BE
957 : dmx_real_fx[ch_idx1] = 0;
958 : move32();
959 : dmx_real_e[ch_idx1] = 0;
960 : move16();
961 : dmx_imag_fx[ch_idx1] = 0;
962 : move32();
963 : dmx_imag_e[ch_idx1] = 0;
964 : move16();
965 :
966 : FOR( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
967 : {
968 : L_tmp = Mpy_32_32( slot_frame_f_real_fx[inp_ch][cur_cldfb_band], ( *p_dmx_fac_fx ) );
969 : dmx_real_fx[ch_idx1] = BASOP_Util_Add_Mant32Exp( dmx_real_fx[ch_idx1], dmx_real_e[ch_idx1], L_tmp, add( 20, gb ), &dmx_real_e[ch_idx1] );
970 : move32();
971 : L_tmp = Mpy_32_32( slot_frame_f_imag_fx[inp_ch][cur_cldfb_band], ( *p_dmx_fac_fx ) );
972 : dmx_imag_fx[ch_idx1] = BASOP_Util_Add_Mant32Exp( dmx_imag_fx[ch_idx1], dmx_imag_e[ch_idx1], L_tmp, add( 20, gb ), &dmx_imag_e[ch_idx1] );
973 : move32();
974 : p_dmx_fac_fx++;
975 : }
976 : #else
977 15493240 : Word32 real_fx = L_add( 0, 0 );
978 15493240 : Word16 real_e = 0;
979 15493240 : move16();
980 15493240 : Word32 imag_fx = L_add( 0, 0 );
981 15493240 : Word16 imag_e = 0;
982 15493240 : move16();
983 :
984 111100280 : FOR( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
985 : {
986 95607040 : L_tmp = Mpy_32_32( slot_frame_f_real_fx[inp_ch][cur_cldfb_band], ( *p_dmx_fac_fx ) );
987 95607040 : real_fx = BASOP_Util_Add_Mant32Exp( real_fx, real_e, L_tmp, add20gb, &real_e );
988 95607040 : L_tmp = Mpy_32_32( slot_frame_f_imag_fx[inp_ch][cur_cldfb_band], ( *p_dmx_fac_fx ) );
989 95607040 : imag_fx = BASOP_Util_Add_Mant32Exp( imag_fx, imag_e, L_tmp, add20gb, &imag_e );
990 95607040 : p_dmx_fac_fx++;
991 : }
992 15493240 : dmx_real_fx[ch_idx1] = real_fx;
993 15493240 : move32();
994 15493240 : dmx_real_e[ch_idx1] = real_e;
995 15493240 : move16();
996 15493240 : dmx_imag_fx[ch_idx1] = imag_fx;
997 15493240 : move32();
998 15493240 : dmx_imag_e[ch_idx1] = imag_e;
999 15493240 : move16();
1000 : #endif
1001 : }
1002 :
1003 : /* Cx for transport channels */
1004 23200460 : FOR( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
1005 : {
1006 : #ifdef IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_BE
1007 15493240 : a_fx = dmx_real_fx[ch_idx1];
1008 15493240 : move32();
1009 15493240 : a_e = dmx_real_e[ch_idx1];
1010 15493240 : move16();
1011 15493240 : b_fx = dmx_imag_fx[ch_idx1];
1012 15493240 : move32();
1013 15493240 : b_e = dmx_imag_e[ch_idx1];
1014 15493240 : move16();
1015 : #endif
1016 46716120 : FOR( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
1017 : {
1018 : #ifndef IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_BE
1019 : a_fx = dmx_real_fx[ch_idx1];
1020 : move32();
1021 : a_e = dmx_real_e[ch_idx1];
1022 : move16();
1023 : b_fx = dmx_imag_fx[ch_idx1];
1024 : move32();
1025 : b_e = dmx_imag_e[ch_idx1];
1026 : move16();
1027 : c_fx = dmx_real_fx[ch_idx2];
1028 : move32();
1029 : c_e = dmx_real_e[ch_idx2];
1030 : move16();
1031 : d_fx = dmx_imag_fx[ch_idx2];
1032 : move32();
1033 : d_e = dmx_imag_e[ch_idx2];
1034 : move16();
1035 :
1036 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
1037 : L_tmp = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, c_fx ), add( a_e, c_e ), Mpy_32_32( b_fx, d_fx ), add( b_e, d_e ), &tmp_e );
1038 : Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cx_sum_e[cur_param_band][ch_idx1][ch_idx2], L_tmp, tmp_e,
1039 : &Cx_sum_e[cur_param_band][ch_idx1][ch_idx2] );
1040 : #else
1041 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
1042 31222880 : L_tmp = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, dmx_real_fx[ch_idx2] ), add( a_e, dmx_real_e[ch_idx2] ), Mpy_32_32( b_fx, dmx_imag_fx[ch_idx2] ), add( b_e, dmx_imag_e[ch_idx2] ), &tmp_e );
1043 62445760 : Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cx_sum_e[cur_param_band][ch_idx1][ch_idx2], L_tmp, tmp_e,
1044 31222880 : &Cx_sum_e[cur_param_band][ch_idx1][ch_idx2] );
1045 : #endif
1046 31222880 : move32();
1047 : }
1048 : }
1049 :
1050 : /* Cy for input channels */
1051 55081140 : FOR( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
1052 : {
1053 : #ifdef IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_NONBE
1054 : a_e = norm_l( slot_frame_f_real_fx[ch_idx1][cur_cldfb_band] );
1055 : a_fx = L_shl( slot_frame_f_real_fx[ch_idx1][cur_cldfb_band], a_e );
1056 : a_e = sub( add20gb, a_e );
1057 : if ( a_fx == 0 )
1058 : {
1059 : a_e = 0;
1060 : move16();
1061 : }
1062 : b_e = norm_l( slot_frame_f_imag_fx[ch_idx1][cur_cldfb_band] );
1063 : b_fx = L_shl( slot_frame_f_imag_fx[ch_idx1][cur_cldfb_band], b_e );
1064 : b_e = sub( add20gb, b_e );
1065 : if ( b_fx == 0 )
1066 : {
1067 : b_e = 0;
1068 : move16();
1069 : }
1070 : #endif
1071 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
1072 47373920 : a_fx = slot_frame_f_real_fx[ch_idx1][cur_cldfb_band];
1073 47373920 : b_fx = slot_frame_f_imag_fx[ch_idx1][cur_cldfb_band];
1074 47373920 : move32();
1075 47373920 : move32();
1076 218391440 : FOR( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
1077 : {
1078 : Word16 norm;
1079 171017520 : c_fx = slot_frame_f_real_fx[ch_idx2][cur_cldfb_band];
1080 171017520 : d_fx = slot_frame_f_imag_fx[ch_idx2][cur_cldfb_band];
1081 171017520 : move32();
1082 171017520 : move32();
1083 : // Conjugated complex multiplication (a-ib)(c+id) = ac+bd + i(ad-bc)
1084 171017520 : Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2] = W_add( Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2],
1085 : W_add( W_mult0_32_32( a_fx, c_fx ), W_mult0_32_32( b_fx, d_fx ) ) );
1086 171017520 : move64();
1087 : // convert the 64 bit fixpoint back into the 48 bit float format
1088 171017520 : norm = W_norm( Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2] );
1089 171017520 : Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2] = W_extract_h( W_shl( Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2], norm ) );
1090 171017520 : Cy_sum_e[cur_param_band][ch_idx1][ch_idx2] = sub( sub( 62, gb ), norm );
1091 : }
1092 : #else
1093 : FOR( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
1094 : {
1095 : #ifndef IMPROVE_HIGH_COMPLEXITY_PARAM_MC_PRM_EST_NONBE
1096 : a_fx = BASOP_Util_Add_Mant32Exp( slot_frame_f_real_fx[ch_idx1][cur_cldfb_band], add( 20, gb ), 0, 0, &a_e );
1097 : b_fx = BASOP_Util_Add_Mant32Exp( slot_frame_f_imag_fx[ch_idx1][cur_cldfb_band], add( 20, gb ), 0, 0, &b_e );
1098 : c_fx = BASOP_Util_Add_Mant32Exp( slot_frame_f_real_fx[ch_idx2][cur_cldfb_band], add( 20, gb ), 0, 0, &c_e );
1099 : d_fx = BASOP_Util_Add_Mant32Exp( slot_frame_f_imag_fx[ch_idx2][cur_cldfb_band], add( 20, gb ), 0, 0, &d_e );
1100 : #else
1101 : c_e = norm_l( slot_frame_f_real_fx[ch_idx2][cur_cldfb_band] );
1102 : c_fx = L_shl( slot_frame_f_real_fx[ch_idx2][cur_cldfb_band], c_e );
1103 : c_e = sub( add20gb, c_e );
1104 : if ( c_fx == 0 )
1105 : {
1106 : c_e = 0;
1107 : move16();
1108 : }
1109 : d_e = norm_l( slot_frame_f_imag_fx[ch_idx2][cur_cldfb_band] );
1110 : d_fx = L_shl( slot_frame_f_imag_fx[ch_idx2][cur_cldfb_band], d_e );
1111 : d_e = sub( add20gb, d_e );
1112 : if ( d_fx == 0 )
1113 : {
1114 : d_e = 0;
1115 : move16();
1116 : }
1117 : #endif
1118 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
1119 : L_tmp = BASOP_Util_Add_Mant32Exp( Mpy_32_32( a_fx, c_fx ), add( a_e, c_e ), Mpy_32_32( b_fx, d_fx ), add( b_e, d_e ), &tmp_e );
1120 : Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cy_sum_e[cur_param_band][ch_idx1][ch_idx2], L_tmp, tmp_e,
1121 : &Cy_sum_e[cur_param_band][ch_idx1][ch_idx2] );
1122 : move32();
1123 : }
1124 : #endif
1125 : }
1126 : }
1127 : }
1128 : }
1129 :
1130 : /* make sure energy and correlation is zero above the relevant LFE bands for LFE
1131 : * avoids wrong energy in case of band combining at transients */
1132 9960 : IF( hParamMC->lfe_index >= 0 )
1133 : {
1134 76040 : FOR( cur_param_band = PARAM_MC_MAX_BAND_LFE; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band++ )
1135 : {
1136 473120 : FOR( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
1137 : {
1138 407040 : Cy_sum_fx[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1139 407040 : move32();
1140 407040 : Cy_sum_e[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1141 407040 : move16();
1142 407040 : Cy_sum_fx[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1143 407040 : move32();
1144 407040 : Cy_sum_e[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1145 407040 : move16();
1146 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
1147 407040 : Cy_sum_real_64[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1148 407040 : move64();
1149 407040 : Cy_sum_real_64[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1150 407040 : move64();
1151 407040 : Cy_sum_imag_64[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1152 407040 : move64();
1153 407040 : Cy_sum_imag_64[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1154 407040 : move64();
1155 : #else
1156 : Cy_sum_imag_fx[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1157 : move32();
1158 : Cy_sum_imag_e[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1159 : move16();
1160 : Cy_sum_imag_fx[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1161 : move32();
1162 : Cy_sum_imag_e[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1163 : move16();
1164 : #endif
1165 : }
1166 : }
1167 :
1168 65800 : FOR( ; cur_param_band < num_parameter_bands; cur_param_band++ )
1169 : {
1170 400040 : FOR( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
1171 : {
1172 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
1173 344200 : Cy_sum_real_64[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1174 344200 : move64();
1175 344200 : Cy_sum_real_64[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1176 344200 : move64();
1177 : #endif
1178 344200 : Cy_sum_fx[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1179 344200 : move32();
1180 344200 : Cy_sum_e[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0;
1181 344200 : move16();
1182 344200 : Cy_sum_fx[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1183 344200 : move32();
1184 344200 : Cy_sum_e[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0;
1185 344200 : move16();
1186 : }
1187 : }
1188 : }
1189 :
1190 9960 : IF( !hParamMC->hMetadataPMC.bAttackPresent )
1191 : {
1192 : const PARAM_MC_ILD_MAPPING *h_ild_mapping;
1193 : Word16 ild_attack;
1194 9490 : ild_attack = 0;
1195 9490 : move16();
1196 9490 : h_ild_mapping = hParamMC->hMetadataPMC.ild_mapping_conf;
1197 : /* create ILDs for to non transmitted parameter bands (only lower half) */
1198 72545 : FOR( cur_param_band = 0; cur_param_band < hParamMC->hMetadataPMC.num_parameter_bands / 2; cur_param_band++ )
1199 : {
1200 : Word32 ILD_fx[PARAM_MC_SZ_ILD_MAP];
1201 : Word16 k;
1202 : Word16 num_ilds_to_code;
1203 :
1204 63055 : IF( GE_16( cur_param_band, PARAM_MC_MAX_BAND_LFE ) )
1205 : {
1206 53565 : num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
1207 53565 : move16();
1208 : }
1209 : ELSE
1210 : {
1211 9490 : num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
1212 9490 : move16();
1213 : }
1214 63055 : IF( NE_16( hParamMC->hMetadataPMC.param_frame_idx, hParamMC->hMetadataPMC.coding_band_mapping[cur_param_band] ) )
1215 : {
1216 : Word32 Nrg_fx[MAX_CICP_CHANNELS];
1217 : Word16 Nrg_e[MAX_CICP_CHANNELS];
1218 :
1219 : /* get ICLDs */
1220 224693 : FOR( k = 0; k < nchan_input; ++k )
1221 : {
1222 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
1223 : Word16 norm;
1224 : // convert the 64 bit fixpoint back into the 48 bit float format
1225 193248 : norm = W_norm( Cy_sum_real_64[cur_param_band][k][k] );
1226 193248 : Nrg_fx[k] = W_extract_h( W_shl( Cy_sum_real_64[cur_param_band][k][k], norm ) );
1227 193248 : Nrg_e[k] = sub( sub( 62, gb ), norm );
1228 : #else
1229 : Nrg_fx[k] = Cy_sum_fx[cur_param_band][k][k];
1230 : move32();
1231 : Nrg_e[k] = Cy_sum_e[cur_param_band][k][k];
1232 : move16();
1233 : #endif
1234 : }
1235 197669 : FOR( k = 0; k < num_ilds_to_code; ++k )
1236 : {
1237 166224 : Word32 ref_ener_fx = 0;
1238 166224 : move32();
1239 166224 : Word16 ref_ener_e = 0;
1240 166224 : move16();
1241 : Word16 ref_channel_cnt;
1242 : Word16 ref_channel_idx;
1243 :
1244 368226 : FOR( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
1245 : {
1246 202002 : ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
1247 202002 : move16();
1248 202002 : ref_ener_fx = BASOP_Util_Add_Mant32Exp( ref_ener_fx, ref_ener_e, Cx_sum_fx[cur_param_band][ref_channel_idx][ref_channel_idx], // ref_ener_e
1249 202002 : Cx_sum_e[cur_param_band][ref_channel_idx][ref_channel_idx], &ref_ener_e );
1250 : }
1251 166224 : L_tmp = Mpy_32_16_1( ref_ener_fx, hParamMC->hMetadataPMC.ild_factors_fx[k] );
1252 166224 : L_tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( Nrg_fx[h_ild_mapping->ild_index[k]], L_add( L_tmp, EPSILLON_FX ), &tmp_e ) );
1253 :
1254 166224 : tmp_e = add( sub( Nrg_e[h_ild_mapping->ild_index[k]], ref_ener_e ), tmp_e );
1255 :
1256 : /*1342177280 = 10 in Q27*/
1257 166224 : ILD_fx[k] = Mpy_32_32( 1342177280, BASOP_Util_Log10( L_tmp, tmp_e ) ); // Q25 + Q27 - Q31 = Q21
1258 166224 : move32();
1259 :
1260 166224 : if ( GT_32( L_sub( hParamMC->prev_ilds_fx[cur_param_band][k], ILD_fx[k] ), param_mc_ild_diff_threshold_fx[cur_param_band] ) )
1261 : {
1262 301 : ild_attack = add( ild_attack, 1 );
1263 : }
1264 : }
1265 : }
1266 : }
1267 : /* check if the ILDs change too much -> go into transient mode... */
1268 9490 : if ( GT_16( ild_attack, PARAM_MC_NUM_ATTACK_ILD_THRESH ) )
1269 : {
1270 0 : hParamMC->hMetadataPMC.bAttackPresent = 1;
1271 0 : move16();
1272 : }
1273 : }
1274 :
1275 :
1276 9960 : IF( hParamMC->hMetadataPMC.bAttackPresent )
1277 : {
1278 : /* combine bands */
1279 2229 : FOR( cur_param_band = 1; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band += 2 )
1280 : {
1281 5404 : FOR( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
1282 : {
1283 11316 : FOR( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
1284 : {
1285 15342 : Cx_sum_fx[cur_param_band - 1][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cx_sum_fx[cur_param_band - 1][ch_idx1][ch_idx2], Cx_sum_e[cur_param_band - 1][ch_idx1][ch_idx2],
1286 7671 : Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cx_sum_e[cur_param_band][ch_idx1][ch_idx2],
1287 7671 : &Cx_sum_e[cur_param_band - 1][ch_idx1][ch_idx2] );
1288 7671 : move32();
1289 15342 : Cx_sum_imag_fx[cur_param_band - 1][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cx_sum_imag_fx[cur_param_band - 1][ch_idx1][ch_idx2], Cx_sum_imag_e[cur_param_band - 1][ch_idx1][ch_idx2],
1290 7671 : Cx_sum_imag_fx[cur_param_band][ch_idx1][ch_idx2], Cx_sum_imag_e[cur_param_band][ch_idx1][ch_idx2],
1291 7671 : &Cx_sum_imag_e[cur_param_band - 1][ch_idx1][ch_idx2] );
1292 7671 : move32();
1293 : }
1294 : }
1295 :
1296 13417 : FOR( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
1297 : {
1298 57981 : FOR( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
1299 : {
1300 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
1301 46323 : Cy_sum_real_64[cur_param_band - 1][ch_idx1][ch_idx2] = W_add( Cy_sum_real_64[cur_param_band - 1][ch_idx1][ch_idx2], Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2] );
1302 46323 : move64();
1303 46323 : Cy_sum_imag_64[cur_param_band - 1][ch_idx1][ch_idx2] = W_add( Cy_sum_imag_64[cur_param_band - 1][ch_idx1][ch_idx2], Cy_sum_imag_64[cur_param_band][ch_idx1][ch_idx2] );
1304 46323 : move64();
1305 :
1306 : #else
1307 : Cy_sum_fx[cur_param_band - 1][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cy_sum_fx[cur_param_band - 1][ch_idx1][ch_idx2], Cy_sum_e[cur_param_band - 1][ch_idx1][ch_idx2],
1308 : Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cy_sum_e[cur_param_band][ch_idx1][ch_idx2],
1309 : &Cy_sum_e[cur_param_band - 1][ch_idx1][ch_idx2] );
1310 : move32();
1311 : Cy_sum_imag_fx[cur_param_band - 1][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cy_sum_imag_fx[cur_param_band - 1][ch_idx1][ch_idx2], Cy_sum_imag_e[cur_param_band - 1][ch_idx1][ch_idx2],
1312 : Cy_sum_imag_fx[cur_param_band][ch_idx1][ch_idx2], Cy_sum_imag_e[cur_param_band][ch_idx1][ch_idx2],
1313 : &Cy_sum_imag_e[cur_param_band - 1][ch_idx1][ch_idx2] );
1314 : move32();
1315 : #endif
1316 : }
1317 : }
1318 : }
1319 :
1320 1724 : FOR( ; cur_param_band < num_parameter_bands; cur_param_band += 2 )
1321 : {
1322 1254 : IF( LT_16( cur_param_band, num_parameter_bands ) )
1323 : {
1324 3866 : FOR( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
1325 : {
1326 8148 : FOR( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
1327 : {
1328 11072 : Cx_sum_fx[cur_param_band - 1][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cx_sum_fx[cur_param_band - 1][ch_idx1][ch_idx2], Cx_sum_e[cur_param_band - 1][ch_idx1][ch_idx2],
1329 5536 : Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cx_sum_e[cur_param_band][ch_idx1][ch_idx2],
1330 5536 : &Cx_sum_e[cur_param_band - 1][ch_idx1][ch_idx2] );
1331 5536 : move32();
1332 : }
1333 : }
1334 :
1335 9618 : FOR( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
1336 : {
1337 41826 : FOR( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
1338 : {
1339 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
1340 33462 : Cy_sum_real_64[cur_param_band - 1][ch_idx1][ch_idx2] = W_add( Cy_sum_real_64[cur_param_band - 1][ch_idx1][ch_idx2], Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2] );
1341 : #else
1342 : Cy_sum_fx[cur_param_band - 1][ch_idx1][ch_idx2] = BASOP_Util_Add_Mant32Exp( Cy_sum_fx[cur_param_band - 1][ch_idx1][ch_idx2], Cy_sum_e[cur_param_band - 1][ch_idx1][ch_idx2],
1343 : Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2], Cy_sum_e[cur_param_band][ch_idx1][ch_idx2],
1344 : &Cy_sum_e[cur_param_band - 1][ch_idx1][ch_idx2] );
1345 : move32();
1346 : #endif
1347 : }
1348 : }
1349 : }
1350 : }
1351 :
1352 470 : band_step = 2;
1353 470 : move16();
1354 : }
1355 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
1356 : {
1357 : // convert the 64 bit fixpoint back into the 48 bit float format
1358 209160 : FOR( cur_param_band = 0; cur_param_band < PARAM_MC_MAX_PARAMETER_BANDS; cur_param_band++ )
1359 : {
1360 3386400 : FOR( ch_idx1 = 0; ch_idx1 < MAX_CICP_CHANNELS; ch_idx1++ )
1361 : {
1362 54182400 : FOR( ch_idx2 = 0; ch_idx2 < MAX_CICP_CHANNELS; ch_idx2++ )
1363 : {
1364 : Word16 norm;
1365 50995200 : norm = W_norm( Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2] );
1366 50995200 : Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2] = W_extract_h( W_shl( Cy_sum_real_64[cur_param_band][ch_idx1][ch_idx2], norm ) );
1367 50995200 : Cy_sum_e[cur_param_band][ch_idx1][ch_idx2] = sub( sub( 62, gb ), norm );
1368 : }
1369 : }
1370 : }
1371 : }
1372 : #endif
1373 :
1374 : /* map complex covariances to real values */
1375 84241 : FOR( cur_param_band = 0; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band += band_step )
1376 : {
1377 : /* Cx for transport channels */
1378 223656 : FOR( ch_idx1 = 0; ch_idx1 < nchan_transport; ch_idx1++ )
1379 : {
1380 450564 : FOR( ch_idx2 = 0; ch_idx2 < nchan_transport; ch_idx2++ )
1381 : {
1382 301189 : real_part_fx = Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2];
1383 301189 : move32();
1384 301189 : real_part_e = Cx_sum_e[cur_param_band][ch_idx1][ch_idx2];
1385 301189 : move16();
1386 301189 : imag_part_fx = Cx_sum_imag_fx[cur_param_band][ch_idx1][ch_idx2];
1387 301189 : move32();
1388 301189 : imag_part_e = Cx_sum_imag_e[cur_param_band][ch_idx1][ch_idx2];
1389 301189 : move16();
1390 :
1391 301189 : real_part_fx = Mpy_32_32( real_part_fx, real_part_fx );
1392 301189 : imag_part_fx = Mpy_32_32( imag_part_fx, imag_part_fx );
1393 301189 : L_tmp = BASOP_Util_Add_Mant32Exp( real_part_fx, shl( real_part_e, 1 ), imag_part_fx, shl( imag_part_e, 1 ), &tmp_e );
1394 :
1395 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
1396 301189 : L_tmp = Sqrt32( L_tmp, &tmp_e );
1397 301189 : Cx_sum_fx[cur_param_band][ch_idx1][ch_idx2] = L_tmp;
1398 301189 : move32();
1399 301189 : Cx_sum_e[cur_param_band][ch_idx1][ch_idx2] = tmp_e;
1400 301189 : move16();
1401 : }
1402 : }
1403 :
1404 : /* Cy for transport channels */
1405 530943 : FOR( ch_idx1 = 0; ch_idx1 < nchan_input; ch_idx1++ )
1406 : {
1407 2105459 : FOR( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ch_idx2++ )
1408 : {
1409 : #ifdef MERGE_REQUEST_1378_SPEEDUP_ivas_mc_param_enc_fx_NONBE
1410 : Word16 norm;
1411 : // convert the 64 bit fixpoint back into the 48 bit float format
1412 1648797 : norm = W_norm( Cy_sum_imag_64[cur_param_band][ch_idx1][ch_idx2] );
1413 1648797 : imag_part_fx = W_extract_h( W_shl( Cy_sum_imag_64[cur_param_band][ch_idx1][ch_idx2], norm ) );
1414 1648797 : imag_part_e = sub( sub( 62, gb ), norm );
1415 : #else
1416 : imag_part_fx = Cy_sum_imag_fx[cur_param_band][ch_idx1][ch_idx2];
1417 : move32();
1418 : imag_part_e = Cy_sum_imag_e[cur_param_band][ch_idx1][ch_idx2];
1419 : move16();
1420 : #endif
1421 1648797 : real_part_fx = Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2];
1422 1648797 : move32();
1423 1648797 : real_part_e = Cy_sum_e[cur_param_band][ch_idx1][ch_idx2];
1424 1648797 : move16();
1425 1648797 : real_part_fx = Mpy_32_32( real_part_fx, real_part_fx );
1426 1648797 : imag_part_fx = Mpy_32_32( imag_part_fx, imag_part_fx );
1427 :
1428 1648797 : L_tmp = BASOP_Util_Add_Mant32Exp( real_part_fx, shl( real_part_e, 1 ), imag_part_fx, shl( imag_part_e, 1 ), &tmp_e );
1429 1648797 : L_tmp = Sqrt32( L_tmp, &tmp_e );
1430 :
1431 1648797 : Cy_sum_fx[cur_param_band][ch_idx1][ch_idx2] = L_tmp;
1432 1648797 : move32();
1433 1648797 : Cy_sum_e[cur_param_band][ch_idx1][ch_idx2] = tmp_e;
1434 1648797 : move16();
1435 : }
1436 : }
1437 : }
1438 :
1439 9960 : IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( Cy_sum_fx[0][LFE_CHANNEL][LFE_CHANNEL], Cy_sum_e[0][LFE_CHANNEL][LFE_CHANNEL], PARAM_MC_LFE_ON_THRESH_FX, 31 ), -1 ) )
1440 : {
1441 8904 : hParamMC->hMetadataPMC.lfe_on = 0;
1442 8904 : move16();
1443 : }
1444 : ELSE
1445 : {
1446 1056 : hParamMC->hMetadataPMC.lfe_on = 1;
1447 1056 : move16();
1448 : }
1449 :
1450 9960 : pop_wmops();
1451 :
1452 9960 : return;
1453 : }
1454 :
1455 : /*-------------------------------------------------------------------------
1456 : * ivas_param_mc_quantize_ilds()
1457 : *
1458 : * Quantize the ILD parameters
1459 : *------------------------------------------------------------------------*/
1460 :
1461 128867 : static void ivas_param_mc_quantize_ilds_fx(
1462 : PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle */
1463 : Word32 Cy_fx[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* i : Covariance matrix of the input */
1464 : Word16 Cy_e[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* i : Covariance matrix of the input */
1465 : Word32 Cx_fx[PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], /* i : Covariance matrix of the dmx */
1466 : Word16 Cx_e[PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], /* i : Covariance matrix of the dmx */
1467 : const Word16 freq_idx, /* i : frequency index being processed */
1468 : const Word16 nchan_input, /* i : number of input channels */
1469 : const Word16 nchan_transport, /* i : number of transport channels */
1470 : Word16 *ILD_idx_out, /* o : ILD indices */
1471 : Word16 ILD_q[PARAM_MC_SZ_ILD_MAP] /* o : Quanzited ILD matrix */
1472 : )
1473 : {
1474 : Word16 i, k;
1475 : Word16 Ny;
1476 : Word16 num_ilds_to_code;
1477 : Word16 ild_map_size;
1478 : Word32 Nrg_fx[MAX_CICP_CHANNELS];
1479 : Word16 Nrg_e[MAX_CICP_CHANNELS];
1480 : Word32 ILD_fx[PARAM_MC_SZ_ILD_MAP];
1481 : Word16 ILD_e[PARAM_MC_SZ_ILD_MAP];
1482 : const PARAM_MC_ILD_MAPPING *h_ild_mapping;
1483 : Word32 tot_ener_fx, dmx_ener_fx, ener_fac_fx, delta_fac_fx;
1484 : Word16 tot_ener_e, dmx_ener_e;
1485 : Word16 ILD_idx[PARAM_MC_SZ_ILD_MAP];
1486 : Word32 L_tmp;
1487 : Word16 tmp_e;
1488 :
1489 128867 : push_wmops( "param_mc_prm_q" );
1490 :
1491 : /* Initialization */
1492 128867 : set32_fx( Nrg_fx, 0, MAX_CICP_CHANNELS );
1493 128867 : set32_fx( ILD_fx, 0, PARAM_MC_SZ_ILD_MAP );
1494 :
1495 128867 : Ny = nchan_input;
1496 128867 : move16();
1497 :
1498 128867 : h_ild_mapping = hParamMC->hMetadataPMC.ild_mapping_conf;
1499 128867 : ild_map_size = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
1500 128867 : move16();
1501 128867 : IF( GE_16( freq_idx, PARAM_MC_MAX_BAND_LFE ) )
1502 : {
1503 118907 : num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
1504 118907 : move16();
1505 : }
1506 : ELSE
1507 : {
1508 9960 : num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
1509 9960 : move16();
1510 : }
1511 :
1512 : /* Downsampling */
1513 128867 : test();
1514 128867 : IF( ( hParamMC->hMetadataPMC.bAttackPresent == 0 ) && NE_16( hParamMC->hMetadataPMC.param_frame_idx, hParamMC->hMetadataPMC.coding_band_mapping[freq_idx] ) )
1515 : {
1516 62884 : pop_wmops();
1517 :
1518 62884 : return;
1519 : }
1520 :
1521 : /* get ICLDs */
1522 472515 : FOR( k = 0; k < Ny; ++k )
1523 : {
1524 406532 : Nrg_fx[k] = Cy_fx[k][k];
1525 406532 : move32();
1526 406532 : Nrg_e[k] = Cy_e[k][k];
1527 406532 : move16();
1528 : }
1529 :
1530 : /* limit ILDs if DMX energy is lower than sum of channel energies */
1531 65983 : tot_ener_fx = 0;
1532 65983 : move32();
1533 65983 : tot_ener_e = 0;
1534 65983 : move16();
1535 65983 : dmx_ener_fx = 0;
1536 65983 : move32();
1537 65983 : dmx_ener_e = 0;
1538 65983 : move16();
1539 :
1540 472515 : FOR( k = 0; k < ild_map_size; k++ )
1541 : {
1542 406532 : test();
1543 406532 : IF( NE_16( k, hParamMC->lfe_index ) || hParamMC->hMetadataPMC.lfe_on )
1544 : {
1545 347750 : tot_ener_fx = BASOP_Util_Add_Mant32Exp( tot_ener_fx, tot_ener_e, Nrg_fx[k], Nrg_e[k], &tot_ener_e );
1546 : }
1547 : }
1548 :
1549 198816 : FOR( k = 0; k < nchan_transport; k++ )
1550 : {
1551 132833 : dmx_ener_fx = BASOP_Util_Add_Mant32Exp( dmx_ener_fx, dmx_ener_e, Cx_fx[k][k], Cx_e[k][k], &dmx_ener_e );
1552 : }
1553 : /*ener_fac = 10.0f * log10f( ( tot_ener + EPSILON ) / ( dmx_ener + EPSILON ) )*/
1554 65983 : IF( tot_ener_fx == 0 )
1555 : {
1556 0 : tot_ener_fx = 9223; // 1e-15(EPSILON) in Q63
1557 0 : tot_ener_e = -32;
1558 0 : move32();
1559 0 : move16();
1560 : }
1561 65983 : IF( dmx_ener_fx == 0 )
1562 : {
1563 0 : dmx_ener_fx = 9223; // 1e-15(EPSILON) in Q63
1564 0 : dmx_ener_e = -32;
1565 0 : move32();
1566 0 : move16();
1567 : }
1568 :
1569 65983 : L_tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( tot_ener_fx, dmx_ener_fx, &tmp_e ) );
1570 65983 : tmp_e = add( sub( tot_ener_e, dmx_ener_e ), tmp_e );
1571 65983 : ener_fac_fx = BASOP_Util_Log10( L_tmp, tmp_e ); // Q25
1572 : /*10 in Q21 = 1342177280*/
1573 65983 : ener_fac_fx = Mpy_32_32( 1342177280, ener_fac_fx ); // Q25 + Q27 - Q31 = Q21;
1574 :
1575 65983 : IF( GT_32( ener_fac_fx, PARAM_MC_ENER_LIMIT_INTRAFRAME_FX_Q21 ) )
1576 : {
1577 15 : L_tmp = L_sub( ener_fac_fx, PARAM_MC_ENER_LIMIT_INTRAFRAME_FX_Q21 - ONE_IN_Q21 ); // Q21
1578 15 : L_tmp = BASOP_Util_Loge( L_tmp, 31 - Q21 ); // Q25
1579 : /*0.3 in Q31 = 644245094*/
1580 15 : L_tmp = L_sub( L_shr( Mpy_32_32( 644245094, L_tmp ), 4 ), L_sub( ener_fac_fx, PARAM_MC_ENER_LIMIT_INTRAFRAME_FX_Q21 ) );
1581 : /*0.1 in Q31 = 214748365*/
1582 15 : L_tmp = Mpy_32_32( L_tmp, 214748365 );
1583 15 : L_tmp = BASOP_util_Pow2( Mpy_32_32( L_tmp, LOG2_10_Q29 ), 31 - 19, &tmp_e );
1584 :
1585 : /*v_multc( Nrg, limit_fac, Nrg, num_ilds_to_code );*/
1586 105 : FOR( i = 0; i < num_ilds_to_code; i++ )
1587 : {
1588 90 : Nrg_fx[i] = Mpy_32_32( Nrg_fx[i], L_tmp );
1589 90 : move32();
1590 90 : Nrg_e[i] = add( tmp_e, Nrg_e[i] );
1591 90 : move16();
1592 90 : Nrg_fx[i] = BASOP_Util_Add_Mant32Exp( Nrg_fx[i], Nrg_e[i], 0, 0, &Nrg_e[i] );
1593 90 : move32();
1594 : }
1595 : }
1596 :
1597 : /* limit ILD jumps in non-tranient frames */
1598 65983 : tot_ener_fx = 0;
1599 65983 : move32();
1600 65983 : dmx_ener_fx = 0;
1601 65983 : move32();
1602 :
1603 472515 : FOR( k = 0; k < ild_map_size; k++ )
1604 : {
1605 406532 : test();
1606 406532 : IF( NE_16( k, hParamMC->lfe_index ) || hParamMC->hMetadataPMC.lfe_on )
1607 : {
1608 347750 : tot_ener_fx = BASOP_Util_Add_Mant32Exp( tot_ener_fx, tot_ener_e, Nrg_fx[k], Nrg_e[k], &tot_ener_e );
1609 : }
1610 : }
1611 :
1612 198816 : FOR( k = 0; k < nchan_transport; k++ )
1613 : {
1614 132833 : dmx_ener_fx = BASOP_Util_Add_Mant32Exp( dmx_ener_fx, dmx_ener_e, Cx_fx[k][k], Cx_e[k][k], &dmx_ener_e );
1615 : }
1616 :
1617 65983 : tot_ener_fx = BASOP_Util_Add_Mant32Exp( tot_ener_fx, tot_ener_e, EPSILON_FX, 0, &tot_ener_e );
1618 65983 : dmx_ener_fx = BASOP_Util_Add_Mant32Exp( dmx_ener_fx, dmx_ener_e, EPSILON_FX, 0, &dmx_ener_e );
1619 65983 : L_tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( tot_ener_fx, dmx_ener_fx, &tmp_e ) );
1620 65983 : tmp_e = add( sub( tot_ener_e, dmx_ener_e ), tmp_e );
1621 65983 : ener_fac_fx = BASOP_Util_Log10( L_tmp, tmp_e ); // Q25
1622 : /*10 in Q21 = 1342177280*/
1623 65983 : ener_fac_fx = Mpy_32_32( 1342177280, ener_fac_fx ); // Q25 + Q27 - Q31 = Q21;
1624 65983 : delta_fac_fx = L_sub( ener_fac_fx, hParamMC->ener_fac_fx[freq_idx] );
1625 :
1626 65983 : test();
1627 65983 : test();
1628 65983 : IF( !hParamMC->hMetadataPMC.bAttackPresent && GT_32( delta_fac_fx, PARAM_MC_ENER_LIMIT_INTERFRAME_FX_Q21 ) && LT_32( delta_fac_fx, PARAM_MC_ENER_LIMIT_MAX_DELTA_FAC_FX_Q21 ) )
1629 : {
1630 401 : L_tmp = L_sub( delta_fac_fx, PARAM_MC_ENER_LIMIT_INTERFRAME_FX_Q21 - ONE_IN_Q21 ); // Q21
1631 401 : L_tmp = BASOP_Util_Loge( L_tmp, 31 - Q21 ); // Q25
1632 : /*0.3 in Q31 = 644245094*/
1633 401 : L_tmp = L_sub( L_shr( Mpy_32_32( 644245094, L_tmp ), 4 ), L_sub( delta_fac_fx, PARAM_MC_ENER_LIMIT_INTERFRAME_FX_Q21 ) );
1634 : /*0.1 in Q31 = 214748365*/
1635 401 : L_tmp = Mpy_32_32( L_tmp, 214748365 );
1636 401 : L_tmp = BASOP_util_Pow2( Mpy_32_32( L_tmp, LOG2_10_Q29 ), 31 - 19, &tmp_e );
1637 :
1638 2512 : FOR( i = 0; i < num_ilds_to_code; i++ )
1639 : {
1640 2111 : Nrg_fx[i] = Mpy_32_32( Nrg_fx[i], L_tmp );
1641 2111 : move32();
1642 2111 : Nrg_e[i] = add( tmp_e, Nrg_e[i] );
1643 2111 : move16();
1644 2111 : Nrg_fx[i] = BASOP_Util_Add_Mant32Exp( Nrg_fx[i], Nrg_e[i], 0, 0, &Nrg_e[i] );
1645 2111 : move32();
1646 : }
1647 :
1648 : /*10 in Q21 = 1342177280*/
1649 401 : ener_fac_fx = L_add( ener_fac_fx, Mpy_32_32( 1342177280, BASOP_Util_Log10( L_tmp, tmp_e ) ) );
1650 : }
1651 :
1652 65983 : hParamMC->ener_fac_fx[freq_idx] = ener_fac_fx; // Q21
1653 65983 : move32();
1654 :
1655 : /* update also combined bands ener_fac when in transient frame */
1656 65983 : test();
1657 65983 : if ( hParamMC->hMetadataPMC.bAttackPresent && LT_16( add( freq_idx, 1 ), hParamMC->hMetadataPMC.nbands_coded ) )
1658 : {
1659 3013 : hParamMC->ener_fac_fx[freq_idx + 1] = ener_fac_fx; // Q21
1660 3013 : move32();
1661 : }
1662 :
1663 410968 : FOR( k = 0; k < num_ilds_to_code; ++k )
1664 : {
1665 344985 : Word32 ref_ener_fx = 0;
1666 344985 : move32();
1667 344985 : Word16 ref_ener_e = 0;
1668 344985 : move16();
1669 : Word16 ref_channel_cnt;
1670 : Word16 ref_channel_idx;
1671 :
1672 760257 : FOR( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
1673 : {
1674 415272 : ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
1675 415272 : move16();
1676 415272 : ref_ener_fx = BASOP_Util_Add_Mant32Exp( ref_ener_fx, ref_ener_e, Cx_fx[ref_channel_idx][ref_channel_idx], Cx_e[ref_channel_idx][ref_channel_idx], &ref_ener_e );
1677 : }
1678 344985 : ref_ener_fx = Mpy_32_16_1( ref_ener_fx, hParamMC->hMetadataPMC.ild_factors_fx[k] );
1679 344985 : L_tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( Nrg_fx[h_ild_mapping->ild_index[k]], L_add( ref_ener_fx, EPSILLON_FX ), &tmp_e ) );
1680 344985 : tmp_e = add( sub( Nrg_e[h_ild_mapping->ild_index[k]], ref_ener_e ), tmp_e );
1681 : /*10 in Q21 = 1342177280*/
1682 344985 : ILD_fx[k] = Mpy_32_32( 1342177280, BASOP_Util_Log10( L_tmp, tmp_e ) ); // Q21
1683 344985 : move32();
1684 344985 : ILD_e[k] = 31 - 21;
1685 344985 : move16();
1686 344985 : hParamMC->prev_ilds_fx[freq_idx][k] = ILD_fx[k];
1687 344985 : move32();
1688 344985 : test();
1689 344985 : IF( hParamMC->hMetadataPMC.bAttackPresent && LT_16( add( freq_idx, 1 ), hParamMC->hMetadataPMC.nbands_coded ) )
1690 : {
1691 17278 : hParamMC->prev_ilds_fx[freq_idx + 1][k] = ILD_fx[k];
1692 17278 : move32();
1693 : }
1694 : }
1695 :
1696 :
1697 : /* quantize parameters */
1698 65983 : ivas_param_mc_parameter_quantizer_fx( ILD_fx, ILD_e, num_ilds_to_code, hParamMC->hMetadataPMC.ild_coding.quantizer_size, hParamMC->hMetadataPMC.ild_coding.quantizer_fx, Q8, ILD_idx, ILD_q );
1699 :
1700 : /* Save current quantized ICLDs */
1701 65983 : Copy( ILD_idx, ILD_idx_out + freq_idx * ild_map_size, num_ilds_to_code );
1702 :
1703 65983 : pop_wmops();
1704 :
1705 65983 : return;
1706 : }
1707 :
1708 :
1709 : /*-------------------------------------------------------------------------
1710 : * ivas_param_mc_quantize_iccs()
1711 : *
1712 : * Quantize the ILD parameters
1713 : *------------------------------------------------------------------------*/
1714 :
1715 128867 : static void ivas_param_mc_quantize_iccs_fx(
1716 : PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle */
1717 : Word32 Cy_fx[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* i : Covariance matrix of the input */
1718 : Word16 Cy_e[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* i : Covariance matrix of the input */
1719 : const Word16 freq_idx, /* i : frequency index being processed */
1720 : const Word16 nchan_input, /* i : number of input channels */
1721 : Word16 *ICC_idx_out /* o : quantizer indices */
1722 : )
1723 : {
1724 : Word16 i, k;
1725 : Word16 Ny;
1726 : Word16 num_iccs_to_code;
1727 : Word16 icc_map_size;
1728 : Word16 tmp_map[2];
1729 : Word16 ICC_idx[PARAM_MC_SZ_ICC_MAP];
1730 :
1731 : /* Initialization */
1732 : Word32 a_fx;
1733 : Word32 Nrg_fx[MAX_CICP_CHANNELS];
1734 : Word16 Nrg_e;
1735 : Word32 ICC_vect_fx[PARAM_MC_SZ_ICC_MAP];
1736 : Word16 ICC_vect_e[PARAM_MC_SZ_ICC_MAP];
1737 : Word16 ICC_vect_q_fx[PARAM_MC_SZ_ICC_MAP];
1738 :
1739 128867 : set32_fx( Nrg_fx, 0, MAX_CICP_CHANNELS );
1740 128867 : set32_fx( ICC_vect_fx, 0, PARAM_MC_SZ_ICC_MAP );
1741 128867 : set16_fx( ICC_vect_q_fx, 0, PARAM_MC_SZ_ICC_MAP );
1742 :
1743 :
1744 128867 : Ny = nchan_input;
1745 128867 : move16();
1746 :
1747 : /* Downsampling */
1748 128867 : test();
1749 128867 : IF( ( hParamMC->hMetadataPMC.bAttackPresent == 0 ) && NE_16( hParamMC->hMetadataPMC.param_frame_idx, hParamMC->hMetadataPMC.coding_band_mapping[freq_idx] ) )
1750 : {
1751 62884 : return;
1752 : }
1753 :
1754 65983 : icc_map_size = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe;
1755 65983 : move16();
1756 65983 : num_iccs_to_code = icc_map_size;
1757 65983 : move16();
1758 :
1759 65983 : if ( GE_16( freq_idx, PARAM_MC_MAX_BAND_LFE ) )
1760 : {
1761 60746 : num_iccs_to_code = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_wo_lfe;
1762 60746 : move16();
1763 : }
1764 :
1765 : /* Get ICC matrix from Cy */
1766 472515 : FOR( k = 0; k < Ny; ++k )
1767 : {
1768 406532 : Nrg_fx[k] = Cy_fx[k][k];
1769 406532 : move32();
1770 406532 : Nrg_e = Cy_e[k][k];
1771 406532 : move16();
1772 406532 : a_fx = ISqrt32( L_add( Nrg_fx[k], EPSILLON_FX ), &Nrg_e );
1773 :
1774 1878650 : FOR( i = k; i < Ny; ++i )
1775 : {
1776 1472118 : Cy_fx[k][i] = Mpy_32_32( Cy_fx[k][i], a_fx );
1777 1472118 : move32();
1778 1472118 : Cy_fx[k][i] = BASOP_Util_Add_Mant32Exp( Cy_fx[k][i], add( Cy_e[k][i], Nrg_e ), 0, 0, &Cy_e[k][i] );
1779 1472118 : move32();
1780 : }
1781 :
1782 1878650 : FOR( i = 0; i <= k; i++ )
1783 : {
1784 1472118 : Cy_fx[i][k] = Mpy_32_32( Cy_fx[i][k], a_fx );
1785 1472118 : move32();
1786 1472118 : Cy_fx[i][k] = BASOP_Util_Add_Mant32Exp( Cy_fx[i][k], add( Cy_e[i][k], Nrg_e ), 0, 0, &Cy_e[i][k] );
1787 1472118 : move32();
1788 : }
1789 : }
1790 :
1791 : /* set ICCs for zero channels to 1 to avoid artifacts in the decoded signal */
1792 472515 : FOR( k = 0; k < Ny; ++k )
1793 : {
1794 406532 : IF( Nrg_fx[k] == 0 )
1795 : {
1796 252840 : FOR( i = k; i < Ny; ++i )
1797 : {
1798 192072 : Cy_fx[k][i] = ONE_IN_Q31;
1799 192072 : move32();
1800 192072 : Cy_e[k][i] = 0;
1801 192072 : move16();
1802 : }
1803 :
1804 303840 : FOR( i = 0; i <= k; ++i )
1805 : {
1806 243072 : Cy_fx[i][k] = ONE_IN_Q31;
1807 243072 : move32();
1808 243072 : Cy_e[i][k] = 0;
1809 243072 : move16();
1810 : }
1811 : }
1812 : }
1813 :
1814 : /* Reduce set of parameters and quantize them */
1815 345786 : FOR( k = 0; k < num_iccs_to_code; ++k )
1816 : {
1817 279803 : tmp_map[0] = hParamMC->hMetadataPMC.icc_mapping_conf->icc_mapping[k][0];
1818 279803 : move16();
1819 279803 : tmp_map[1] = hParamMC->hMetadataPMC.icc_mapping_conf->icc_mapping[k][1];
1820 279803 : move16();
1821 279803 : ICC_vect_fx[k] = Cy_fx[tmp_map[0]][tmp_map[1]];
1822 279803 : move32();
1823 279803 : ICC_vect_e[k] = Cy_e[tmp_map[0]][tmp_map[1]];
1824 279803 : move16();
1825 : }
1826 :
1827 : /* Quantization */
1828 65983 : ivas_param_mc_parameter_quantizer_fx( ICC_vect_fx, ICC_vect_e, num_iccs_to_code, hParamMC->hMetadataPMC.icc_coding.quantizer_size, hParamMC->hMetadataPMC.icc_coding.quantizer_fx, Q15, ICC_idx, ICC_vect_q_fx );
1829 :
1830 : /* Save current quantized ICCs */
1831 65983 : Copy( ICC_idx, ICC_idx_out + freq_idx * icc_map_size, num_iccs_to_code );
1832 :
1833 65983 : return;
1834 : }
1835 :
1836 :
1837 : /*-------------------------------------------------------------------------
1838 : * ivas_param_mc_parameter_quantizer()
1839 : *
1840 : * Parameter Quantization
1841 : *------------------------------------------------------------------------*/
1842 :
1843 131966 : static void ivas_param_mc_parameter_quantizer_fx(
1844 : const Word32 *x, /* i : input sequence */
1845 : const Word16 *x_e, /* i : input sequence */
1846 : const Word16 L, /* i : input length */
1847 : const Word16 sz_quantizer, /* i : quantizer size */
1848 : const Word16 *quantizer_fx, /* i : quantizer table Q_quant */
1849 : const Word16 Q_quant, /* i : quantizer table */
1850 : Word16 *quant_idx, /* o : quant indices */
1851 : Word16 *y_fx /* o : output sequence Q_quant */
1852 : )
1853 : {
1854 : Word16 idx, i;
1855 : Word16 idx_min;
1856 : Word32 tmp_min_fx;
1857 : Word16 tmp_min_e;
1858 : Word32 tmp_val;
1859 : Word16 tmp_e;
1860 :
1861 131966 : set16_fx( y_fx, 0, L );
1862 131966 : idx_min = 0;
1863 131966 : move16();
1864 :
1865 756754 : FOR( idx = 0; idx < L; ++idx )
1866 : {
1867 624788 : tmp_min_fx = 1000;
1868 624788 : move32();
1869 624788 : tmp_min_e = 31;
1870 624788 : move16();
1871 :
1872 8382972 : FOR( i = 0; i < sz_quantizer; ++i )
1873 : {
1874 7758184 : tmp_val = BASOP_Util_Add_Mant32Exp( x[idx], x_e[idx], L_negate( L_deposit_h( quantizer_fx[i] ) ), sub( 15, Q_quant ), &tmp_e );
1875 7758184 : tmp_val = L_abs( tmp_val );
1876 7758184 : IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( tmp_val, tmp_e, tmp_min_fx, tmp_min_e ), -1 ) )
1877 : {
1878 4084448 : tmp_min_fx = tmp_val;
1879 4084448 : move32();
1880 4084448 : tmp_min_e = tmp_e;
1881 4084448 : move16();
1882 4084448 : idx_min = i;
1883 4084448 : move16();
1884 : }
1885 : }
1886 :
1887 624788 : y_fx[idx] = quantizer_fx[idx_min];
1888 624788 : move32();
1889 624788 : quant_idx[idx] = idx_min;
1890 624788 : move16();
1891 : }
1892 :
1893 131966 : return;
1894 : }
1895 :
1896 :
1897 : /*-------------------------------------------------------------------------
1898 : * ivas_param_mc_transient_detection()
1899 : *
1900 : * Detect if the current frame has a transient
1901 : *------------------------------------------------------------------------*/
1902 :
1903 20030 : static void ivas_param_mc_transient_detection_fx(
1904 : PARAM_MC_ENC_HANDLE hParamMC, /* i : Parametric MC encoder handle */
1905 : TRAN_DET_HANDLE hTranDet, /* i : Transient detector handle from core coder for a transport channel */
1906 : Word16 *pbIsAttackPresent, /* o : Flag for indicating a found transient */
1907 : Word16 *pAttackIndex /* o : Attack position (0 if no attack) */
1908 : )
1909 : {
1910 : Word16 i;
1911 : Word16 bIsAttackPresent, attackIndex;
1912 : Word32 *pSubblockNrg_fx;
1913 : Word32 *pAccSubblockNrg_fx;
1914 : Word16 attackRatioThreshold_fx;
1915 :
1916 20030 : push_wmops( "param_mc_trn_det" );
1917 :
1918 20030 : attackRatioThreshold_fx = hTranDet->transientDetector.attackRatioThreshold;
1919 20030 : pSubblockNrg_fx = &hTranDet->subblockEnergies.subblockNrg[hParamMC->transient_detector_delay]; // Q(-1)
1920 20030 : pAccSubblockNrg_fx = &hTranDet->subblockEnergies.accSubblockNrg[hParamMC->transient_detector_delay]; // Q(-1)
1921 :
1922 20030 : bIsAttackPresent = FALSE;
1923 20030 : move16();
1924 20030 : attackIndex = 16;
1925 20030 : move16();
1926 :
1927 : /* Search for the last attack in the subblocks,
1928 : * if we had an attack very late in the last frame,
1929 : * make the current frame also a transient one... */
1930 20030 : test();
1931 20030 : IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( pSubblockNrg_fx[-1], 32, Mpy_32_16_1( pAccSubblockNrg_fx[-1], attackRatioThreshold_fx ), ( 32 + ATTACKTHRESHOLD_E ) ), 1 ) ||
1932 : EQ_16( BASOP_Util_Cmp_Mant32Exp( pSubblockNrg_fx[-2], 32, Mpy_32_16_1( pAccSubblockNrg_fx[-2], attackRatioThreshold_fx ), ( 32 + ATTACKTHRESHOLD_E ) ), 1 ) )
1933 : {
1934 69 : bIsAttackPresent = TRUE;
1935 69 : move16();
1936 69 : attackIndex = 0;
1937 69 : move16();
1938 : }
1939 :
1940 180270 : FOR( i = 0; i < NSUBBLOCKS; i++ ){
1941 160240 : IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( pSubblockNrg_fx[i], 32, Mpy_32_16_1( pAccSubblockNrg_fx[i], attackRatioThreshold_fx ), ( 32 + ATTACKTHRESHOLD_E ) ), 1 ) ){
1942 754 : bIsAttackPresent = TRUE;
1943 754 : move16();
1944 754 : attackIndex = i;
1945 754 : move16();
1946 : }
1947 : }
1948 :
1949 : /* avoid post-echos on click sounds (very short transients) due to TNS aliasing */
1950 20030 : *pAttackIndex = attackIndex;
1951 20030 : move16();
1952 20030 : *pbIsAttackPresent = bIsAttackPresent;
1953 20030 : move16();
1954 :
1955 20030 : pop_wmops();
1956 :
1957 20030 : return;
1958 : }
1959 :
1960 : /*-------------------------------------------------------------------------
1961 : * ivas_param_mc_entropy_encoder()
1962 : *
1963 : * Write the metadata bitstream
1964 : *------------------------------------------------------------------------*/
1965 :
1966 9960 : static void ivas_param_mc_write_bs_fx(
1967 : const PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder Handle */
1968 : Word16 *ILD_idx, /* i : ILD quantizer indices sequence */
1969 : Word16 *ICC_idx, /* i : ICC quantizer indices sequence */
1970 : UWord16 bit_buffer[PARAM_MC_MAX_BITS], /* o : Output bit buffer */
1971 : Word16 *bit_pos /* o : Number of bits used */
1972 : )
1973 : {
1974 : Word16 i, pos;
1975 : Word16 nbands;
1976 : Word16 band_step;
1977 : /*buffers are not getting used in the function*/
1978 : // Word16 seq_tmp[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
1979 : // float seq_tmp_uni[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
1980 : Word16 icc_map_size_wo_lfe;
1981 : Word16 icc_map_size;
1982 : Word16 ild_map_size_wo_lfe;
1983 : Word16 ild_map_size;
1984 :
1985 9960 : push_wmops( "param_mc_prm_enc" );
1986 :
1987 : /* Init */
1988 : /*buffers are not getting used in the function */
1989 : // set_zero( seq_tmp_uni, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
1990 : // set_s( seq_tmp, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
1991 9960 : nbands = hParamMC->hMetadataPMC.nbands_in_param_frame[hParamMC->hMetadataPMC.param_frame_idx];
1992 9960 : move16();
1993 9960 : icc_map_size_wo_lfe = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_wo_lfe;
1994 9960 : move16();
1995 9960 : icc_map_size = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe;
1996 9960 : move16();
1997 9960 : ild_map_size_wo_lfe = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
1998 9960 : move16();
1999 9960 : ild_map_size = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
2000 9960 : move16();
2001 :
2002 : /*-----------------------------------------------------------------*
2003 : * Signaling bits
2004 : *-----------------------------------------------------------------*/
2005 :
2006 : /* reserved bit */
2007 9960 : bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.lfe_on;
2008 9960 : move16();
2009 :
2010 : /* write coded band width */
2011 9960 : i = hParamMC->hMetadataPMC.coded_bwidth;
2012 9960 : move16();
2013 29880 : FOR( pos = 0; pos < 2; pos++ )
2014 : {
2015 19920 : bit_buffer[( *bit_pos )++] = (UWord16) s_and( shr( i, pos ), 1 );
2016 19920 : move16();
2017 : }
2018 :
2019 : /* write param frame indicator */
2020 9960 : bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.param_frame_idx;
2021 9960 : move16();
2022 :
2023 : /* write transient frame indicator */
2024 9960 : bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.bAttackPresent;
2025 9960 : move16();
2026 :
2027 9960 : band_step = 1;
2028 9960 : move16();
2029 9960 : IF( hParamMC->hMetadataPMC.bAttackPresent )
2030 : {
2031 470 : band_step = PARAM_MC_TRANSIENT_BAND_STEP;
2032 470 : move16();
2033 1880 : FOR( pos = 2; pos >= 0; --pos )
2034 : {
2035 1410 : bit_buffer[( *bit_pos )++] = (UWord16) s_and( shr( hParamMC->hMetadataPMC.attackIndex, pos ), 1 );
2036 1410 : move16();
2037 : }
2038 470 : IF( ( hParamMC->hMetadataPMC.nbands_coded % band_step ) )
2039 : {
2040 42 : nbands = add( idiv1616( hParamMC->hMetadataPMC.nbands_coded, band_step ), 1 );
2041 : }
2042 : ELSE
2043 : {
2044 428 : nbands = add( idiv1616( hParamMC->hMetadataPMC.nbands_coded, band_step ), 0 );
2045 : }
2046 : }
2047 :
2048 9960 : ivas_param_mc_encode_parameter_fx( ICC_idx, &hParamMC->hMetadataPMC, &hParamMC->hMetadataPMC.icc_coding,
2049 : nbands, band_step, icc_map_size_wo_lfe, icc_map_size, bit_buffer, bit_pos );
2050 :
2051 9960 : ivas_param_mc_encode_parameter_fx( ILD_idx, &hParamMC->hMetadataPMC, &hParamMC->hMetadataPMC.ild_coding,
2052 : nbands, band_step, ild_map_size_wo_lfe, ild_map_size, bit_buffer, bit_pos );
2053 9960 : pop_wmops();
2054 :
2055 9960 : return;
2056 : }
2057 :
2058 :
2059 : /*-------------------------------------------------------------------------
2060 : * ivas_param_mc_encode_parameter_fx()
2061 : *
2062 : * (entropy) encode a sequence of parameter indices
2063 : *------------------------------------------------------------------------*/
2064 :
2065 19920 : static void ivas_param_mc_encode_parameter_fx(
2066 : Word16 *quant_idx, /* i : indices sequence to encode */
2067 : HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC, /* i : Parametric MC metadata handle */
2068 : HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParameterCodingInfo, /* i : parameter quantization and coding info */
2069 : const Word16 nbands, /* i : number of parameter bands to encode */
2070 : const Word16 band_step, /* i : parameter band step */
2071 : const Word16 map_size_wo_lfe, /* i : number of parameters per band (w/o LFE) */
2072 : const Word16 map_size, /* i : number of parameters per band */
2073 : UWord16 bit_buffer[PARAM_MC_MAX_BITS], /* o : Output bit buffer */
2074 : Word16 *bit_pos /* o : Number of bits used */
2075 : )
2076 : {
2077 : Word16 sz_seq;
2078 : Word16 idx_prev;
2079 : Word16 idx_offset;
2080 : Word16 bit_cnt_uni;
2081 : Word16 bit_cnt_range;
2082 : Word16 bit_cnt_range_diff;
2083 : Word16 bit_cnt_range_min;
2084 : Word16 bit_pos_tmp;
2085 : Word16 i, j;
2086 : Word16 idx;
2087 : Word16 seq_delta[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2088 : Word16 seq[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2089 : UWord16 tmp_bit_buffer[PARAM_MC_MAX_BITS];
2090 : UWord16 tmp_bit_buffer_diff[PARAM_MC_MAX_BITS];
2091 :
2092 : /* Inits */
2093 19920 : sz_seq = i_mult( nbands, ( map_size_wo_lfe ) );
2094 :
2095 : /* Computing Delta Sequence */
2096 19920 : idx_prev = sub( add( shr( hParameterCodingInfo->quantizer_size, 1 ), hParameterCodingInfo->quantizer_size % 2 ), 1 );
2097 19920 : idx_offset = sub( hParameterCodingInfo->quantizer_size, 1 );
2098 :
2099 112490 : FOR( j = 0; j < map_size_wo_lfe; ++j )
2100 : {
2101 92570 : Word16 coding_band = 0;
2102 92570 : move16();
2103 :
2104 1289476 : FOR( i = 0; i < hMetadataPMC->nbands_coded; i += band_step )
2105 : {
2106 1196906 : test();
2107 1196906 : IF( hMetadataPMC->bAttackPresent || EQ_16( hMetadataPMC->param_frame_idx, hMetadataPMC->coding_band_mapping[i] ) )
2108 : {
2109 614248 : idx = quant_idx[i * map_size + j];
2110 614248 : move16();
2111 614248 : seq[coding_band + j * nbands] = idx;
2112 614248 : move16();
2113 614248 : seq_delta[coding_band + j * nbands] = add( sub( idx, idx_prev ), idx_offset );
2114 614248 : move16();
2115 614248 : idx_prev = idx;
2116 614248 : move16();
2117 614248 : coding_band = add( coding_band, 1 );
2118 : }
2119 : }
2120 : }
2121 :
2122 : /* LFE */
2123 19920 : IF( hMetadataPMC->lfe_on )
2124 : {
2125 4224 : FOR( i = 0; i < PARAM_MC_MAX_BAND_LFE; i += band_step )
2126 : {
2127 2112 : test();
2128 2112 : IF( hMetadataPMC->bAttackPresent || EQ_16( hMetadataPMC->param_frame_idx, hMetadataPMC->coding_band_mapping[i] ) )
2129 : {
2130 : /* LFE ICC/ILDs are always the last ones in coding band 0 */
2131 : Word16 n_lfe_idx, k;
2132 1158 : n_lfe_idx = sub( map_size, map_size_wo_lfe );
2133 2382 : FOR( k = 0; k < n_lfe_idx; k++ )
2134 : {
2135 1224 : idx = quant_idx[( i + 1 ) * map_size - n_lfe_idx + k];
2136 1224 : move16();
2137 1224 : seq[sz_seq] = idx;
2138 1224 : move16();
2139 1224 : seq_delta[sz_seq] = add( sub( idx, idx_prev ), idx_offset );
2140 1224 : move16();
2141 1224 : idx_prev = idx;
2142 1224 : move16();
2143 1224 : sz_seq = add( sz_seq, 1 );
2144 : }
2145 : }
2146 : }
2147 : }
2148 :
2149 :
2150 19920 : bit_cnt_uni = sub( i_mult( sz_seq, hParameterCodingInfo->uni_bits ), 1 ); /* -1 for the additional diff/direct signaling bit for the range encoder*/
2151 :
2152 : /* code the direct index sequence */
2153 19920 : ivas_param_mc_range_encoder_fx( seq, sz_seq, hParameterCodingInfo->cum_freq, hParameterCodingInfo->sym_freq, PARAM_MC_RANGE_CODER_TOT_SHIFT, bit_cnt_uni, &tmp_bit_buffer[0], &bit_cnt_range );
2154 :
2155 : /* Coding the delta index sequence */
2156 19920 : ivas_param_mc_range_encoder_fx( seq_delta, sz_seq, hParameterCodingInfo->cum_freq_delta, hParameterCodingInfo->sym_freq_delta, PARAM_MC_RANGE_CODER_TOT_SHIFT, bit_cnt_uni, &tmp_bit_buffer_diff[0], &bit_cnt_range_diff );
2157 :
2158 19920 : bit_cnt_range_min = s_min( bit_cnt_range, bit_cnt_range_diff );
2159 :
2160 : /* uniform fallback */
2161 19920 : IF( GT_16( bit_cnt_range_min, bit_cnt_uni ) )
2162 : {
2163 : /* Uniform coding is used */
2164 460 : bit_buffer[( *bit_pos )++] = 0;
2165 460 : move16();
2166 460 : bit_pos_tmp = 0;
2167 460 : move16();
2168 :
2169 12537 : FOR( i = 0; i < sz_seq; ++i )
2170 : {
2171 12077 : ivas_param_mc_dec2bin_fx( seq[i], hParameterCodingInfo->uni_bits, &bit_buffer[*( bit_pos ) + bit_pos_tmp] );
2172 12077 : bit_pos_tmp = add( bit_pos_tmp, hParameterCodingInfo->uni_bits );
2173 : }
2174 460 : *bit_pos = add( *bit_pos, bit_pos_tmp );
2175 : }
2176 : ELSE
2177 : {
2178 : /* Range Coding is used */
2179 19460 : bit_buffer[( *bit_pos )++] = 1;
2180 19460 : move16();
2181 19460 : IF( bit_cnt_range_diff < bit_cnt_range )
2182 : {
2183 19214 : bit_buffer[( *bit_pos )++] = 1;
2184 19214 : move16();
2185 1324048 : FOR( i = 0; i < bit_cnt_range_diff; i++ )
2186 : {
2187 1304834 : bit_buffer[( *bit_pos )++] = tmp_bit_buffer_diff[i];
2188 1304834 : move16();
2189 : }
2190 : }
2191 : ELSE
2192 : {
2193 246 : bit_buffer[( *bit_pos )++] = 0;
2194 246 : move16();
2195 14815 : FOR( i = 0; i < bit_cnt_range; i++ )
2196 : {
2197 14569 : bit_buffer[( *bit_pos )++] = tmp_bit_buffer[i];
2198 14569 : move16();
2199 : }
2200 : }
2201 : }
2202 :
2203 19920 : return;
2204 : }
2205 :
2206 :
2207 : /*-------------------------------------------------------------------------
2208 : * ivas_param_mc_dec2bin()
2209 : *
2210 : * Decimal to binary routine
2211 : *------------------------------------------------------------------------*/
2212 :
2213 12077 : static void ivas_param_mc_dec2bin_fx(
2214 : const Word16 val, /* i : value to encode */
2215 : const Word16 N, /* i : number of bits for encoding the value */
2216 : UWord16 bits[PARAM_MC_MAX_BITS] ) /* o : encoded bits buffer */
2217 : {
2218 : Word16 idx;
2219 :
2220 12077 : idx = 0;
2221 12077 : move16();
2222 : /* convert value to bitstream, MSB first */
2223 48308 : FOR( idx = 0; idx < N; idx++ )
2224 : {
2225 36231 : bits[idx] = (UWord16) s_and( shr( val, sub( sub( N, 1 ), idx ) ), 1 );
2226 36231 : move16();
2227 : }
2228 :
2229 12077 : return;
2230 : }
2231 :
2232 :
2233 : /*-------------------------------------------------------------------*
2234 : * ivas_param_mc_range_encoder()
2235 : *
2236 : * Parametric MC Range encoder
2237 : *-------------------------------------------------------------------*/
2238 :
2239 39840 : static void ivas_param_mc_range_encoder_fx(
2240 : const Word16 *seq_in, /* i : input sequence */
2241 : const Word16 num_symbols, /* i : Number of symbole to encode */
2242 : const UWord16 *cum_freq, /* i : cumulated frequencies */
2243 : const UWord16 *sym_freq, /* i : symbol frequencies */
2244 : const UWord16 tot_shift, /* i : max cumulative freq as power of 2 */
2245 : const Word16 max_nb_bits, /* i : Maximum number of bits allowed */
2246 : UWord16 *bit_buffer, /* o : output bit buffer */
2247 : Word16 *bit_pos /* o : number of bits used */
2248 : )
2249 : {
2250 : RangeUniEncState rc_st_enc;
2251 : Word16 rc_tot_bits; /* No. of bits returned by range coder */
2252 : Word16 i;
2253 : UWord8 k, byte;
2254 : UWord16 *bits;
2255 :
2256 : /* Initialize range encoder */
2257 39840 : rc_uni_enc_init_fx( &rc_st_enc );
2258 :
2259 : /* Main loop over the length of the sequence */
2260 1212542 : FOR( i = 0; i < num_symbols; ++i )
2261 : {
2262 1182531 : rc_uni_enc_encode_symbol_fastS_fx( &rc_st_enc, (UWord16) seq_in[i], cum_freq, sym_freq, tot_shift );
2263 :
2264 1182531 : IF( GT_16( rc_uni_enc_virtual_finish_fx( &rc_st_enc ), max_nb_bits ) )
2265 : {
2266 : /* we alread have exceeded the maximum number of bits allowed, i.e. the uniform fallback */
2267 9829 : *bit_pos = MAX_BITS_PER_FRAME;
2268 9829 : return;
2269 : }
2270 : }
2271 :
2272 : /* Finish range encoder */
2273 30011 : rc_tot_bits = rc_uni_enc_finish_fx( &rc_st_enc ); /* No. of bits consumed by range coder */
2274 :
2275 : /* Push range coded bits from byte_buffer to bitstream */
2276 :
2277 : /* 1) Push all complete bytes, one byte at a time */
2278 327739 : FOR( i = 0; i < ( rc_tot_bits >> 3 ); ++i )
2279 : {
2280 : /* use rc_st_enc.byte_buffer */
2281 297728 : bits = &bit_buffer[i << 3];
2282 :
2283 297728 : byte = rc_st_enc.byte_buffer[i];
2284 297728 : move16();
2285 :
2286 297728 : bits[0] = (UWord16) s_and( shr( (Word16) byte, 7 ), 1 );
2287 297728 : move16();
2288 297728 : bits[1] = (UWord16) s_and( shr( (Word16) byte, 6 ), 1 );
2289 297728 : move16();
2290 297728 : bits[2] = (UWord16) s_and( shr( (Word16) byte, 5 ), 1 );
2291 297728 : move16();
2292 297728 : bits[3] = (UWord16) s_and( shr( (Word16) byte, 4 ), 1 );
2293 297728 : move16();
2294 297728 : bits[4] = (UWord16) s_and( shr( (Word16) byte, 3 ), 1 );
2295 297728 : move16();
2296 297728 : bits[5] = (UWord16) s_and( shr( (Word16) byte, 2 ), 1 );
2297 297728 : move16();
2298 297728 : bits[6] = (UWord16) s_and( shr( (Word16) byte, 1 ), 1 );
2299 297728 : move16();
2300 297728 : bits[7] = (UWord16) s_and( (Word16) byte, 1 );
2301 297728 : move16();
2302 : }
2303 :
2304 : /* 2) Push remaining bits */
2305 30011 : IF( s_and( rc_tot_bits, 7 ) != 0 )
2306 : {
2307 26435 : UWord8 rem_bits = (UWord8) s_and( rc_tot_bits, 7 );
2308 :
2309 26435 : bits = &bit_buffer[( i << 3 )];
2310 26435 : byte = rc_st_enc.byte_buffer[i];
2311 26435 : move16();
2312 :
2313 131217 : FOR( k = 0; k < rem_bits; k++ )
2314 : {
2315 104782 : bits[k] = (UWord16) s_and( shr( (Word16) byte, sub( 7, k ) ), 1 );
2316 104782 : move16();
2317 : }
2318 : }
2319 :
2320 : /* Update output number of bits */
2321 30011 : *bit_pos = rc_tot_bits;
2322 30011 : move16();
2323 :
2324 30011 : return;
2325 : }
|