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 "rom_com.h"
39 : #include "prot_fx.h"
40 : #include "ivas_prot_fx.h"
41 : #include "basop_util.h"
42 : #include "ivas_cnst.h"
43 : #include "ivas_rom_com.h"
44 : #include "ivas_rom_enc.h"
45 : #include "wmc_auto.h"
46 : #ifdef DEBUGGING
47 : #include "debug.h"
48 : #endif
49 :
50 : /*-------------------------------------------------------------------------
51 : * Local function prototypes
52 : *------------------------------------------------------------------------*/
53 :
54 : static void ivas_mc_paramupmix_dmx_fx( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, Word32 *data_f[], const Word16 input_frame );
55 :
56 : static ivas_error ivas_mc_paramupmix_param_est_enc_fx( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, Word32 *input_frame_t_fx[], Word16 Q_input_frame_t, const Word16 input_frame, Word32 alphas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], Word16 *exp_alphas, Word32 betas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], Word16 *exp_betas );
57 :
58 : static void get_huff_table_fx( const PAR_TYPE par_type, HUFF_TAB *df0, HUFF_TAB *df );
59 :
60 : static void write_huff_bits_fx( const Word32 value, const UWord16 length, UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS], Word16 *bit_pos );
61 :
62 : static void huffman_encode_fx( const Word32 *vqPrev, const Word32 *vq, const PAR_TYPE parType, const Word16 nq, UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS], Word16 *bit_pos );
63 :
64 : static void put_ec_data_fx( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, const Word16 ch, const Word32 pars_fx[IVAS_MAX_NUM_BANDS], Word16 exp_paras, const Word32 alphas_fx[IVAS_MAX_NUM_BANDS], Word16 exp_alphas, const PAR_TYPE parType, UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS], Word16 *bit_pos );
65 :
66 : static void quantize_alpha_fx( const Word32 *alpha_fx, Word16 exp_alpha, Word16 *pnq, Word32 aq[IVAS_MAX_NUM_BANDS], Word32 *adeq_fx, Word16 *exp_adeq );
67 :
68 :
69 : static void quantize_pars_fx( const Word32 *v_fx, Word16 exp_v, const Word16 nq, const Word32 *data_fx, Word32 vq[IVAS_MAX_NUM_BANDS], Word32 *vdeq_fx, Word16 *exp_vdeq );
70 :
71 : /*-------------------------------------------------------------------------
72 : * ivas_mc_paramupmix_enc()
73 : *
74 : * MC ParamUpmix Encoder main encoding function
75 : *------------------------------------------------------------------------*/
76 :
77 790 : void ivas_mc_paramupmix_enc_fx(
78 : Encoder_Struct *st_ivas, /* i/o: IVAS Encoder handle */
79 : BSTR_ENC_HANDLE hBStr, /* i/o: IVAS Metadata bitstream handle */
80 : Word32 *data_fx[], // st_ivas->q_data_fx
81 : const Word16 input_frame /* i : input frame length */
82 : )
83 : {
84 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix;
85 : Word16 i;
86 : UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS];
87 : Word16 bit_pos;
88 :
89 790 : push_wmops( "mc_paramupmix_enc" );
90 :
91 790 : hMCParamUpmix = st_ivas->hMCParamUpmix;
92 790 : bit_pos = 0;
93 :
94 : /* Parameter estimation */
95 : Word32 alphas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS]; // exp_alphas
96 : Word32 betas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS]; // exp_betas
97 790 : Word16 exp_alphas = 31, exp_betas = 31;
98 790 : move16();
99 790 : move16();
100 :
101 790 : ivas_mc_paramupmix_param_est_enc_fx( hMCParamUpmix, data_fx, st_ivas->q_data_fx, input_frame, alphas_fx, &exp_alphas, betas_fx, &exp_betas );
102 :
103 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
104 : {
105 3160 : put_ec_data_fx( hMCParamUpmix, i, alphas_fx[i], exp_alphas, NULL, 0, ALPHA, bit_buffer, &bit_pos );
106 3160 : put_ec_data_fx( hMCParamUpmix, i, betas_fx[i], exp_betas, alphas_fx[i], exp_alphas, BETA, bit_buffer, &bit_pos );
107 : }
108 :
109 : /* push the PARAM UPMIX MC side info from the temporary buffer into the medatdata bitstream*/
110 790 : push_next_bits( hBStr, bit_buffer, bit_pos );
111 :
112 : /* DMX generation*/
113 790 : ivas_mc_paramupmix_dmx_fx( hMCParamUpmix, data_fx, input_frame );
114 :
115 790 : pop_wmops();
116 :
117 790 : return;
118 : }
119 :
120 :
121 : /*-------------------------------------------------------------------------
122 : * ivas_mc_paramupmix_enc_open()
123 : *
124 : * Initialize MC ParamUpmix encoder handle
125 : *------------------------------------------------------------------------*/
126 12 : ivas_error ivas_mc_paramupmix_enc_open_fx(
127 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
128 : )
129 : {
130 : IVAS_FB_CFG *fb_cfg;
131 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix;
132 : Word32 input_Fs;
133 : Word32 input_frame;
134 : Word16 i, k, b, j;
135 : ivas_error error;
136 :
137 12 : error = IVAS_ERR_OK;
138 12 : move16();
139 12 : input_Fs = st_ivas->hEncoderConfig->input_Fs;
140 12 : move16();
141 12 : input_frame = (Word32) extract_l( Mpy_32_32( st_ivas->hEncoderConfig->input_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) );
142 :
143 : /* Sanity Checks */
144 12 : IF( ( hMCParamUpmix = (MC_PARAMUPMIX_ENC_HANDLE) malloc( sizeof( MC_PARAMUPMIX_ENC_DATA ) ) ) == NULL )
145 : {
146 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MC_MODE_PARAMUPMIX\n" ) );
147 : }
148 60 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
149 : {
150 144 : FOR( k = 0; k < MC_PARAMUPMIX_NCH; k++ )
151 : {
152 96 : IF( ( hMCParamUpmix->midside_fx[i][k] = (Word32 *) malloc( sizeof( Word32 ) * input_frame ) ) == NULL )
153 : {
154 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MC_MODE_PARAMUPMIX\n" ) );
155 : }
156 96 : set_zero_fx( hMCParamUpmix->midside_fx[i][k], (Word16) input_frame );
157 : }
158 : }
159 12 : hMCParamUpmix->first_frame = 1;
160 12 : move16();
161 :
162 : /* MC_LS_SETUP_5_1_2 is the only current configuration */
163 12 : st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
164 12 : move16();
165 :
166 : /* set core coder dependent on the number of transport channels */
167 12 : SWITCH( st_ivas->nchan_transport )
168 : {
169 12 : case 8:
170 12 : st_ivas->nCPE = 4;
171 12 : move16();
172 12 : st_ivas->nSCE = 0;
173 12 : move16();
174 12 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
175 12 : move16();
176 12 : break;
177 : }
178 :
179 : /* Transient Detector handle */
180 108 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
181 : {
182 96 : IF( NE_32( ( error = ivas_transient_det_open_fx( &( hMCParamUpmix->hTranDet[i] ), input_Fs ) ), IVAS_ERR_OK ) )
183 : {
184 0 : return error;
185 : }
186 : }
187 :
188 : /* set FB config. */
189 : /* need to set num output channels to a value > 0 to get pFb != NULL */
190 12 : IF( NE_32( ( error = ivas_fb_set_cfg( &fb_cfg, MC_FORMAT, MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH, MC_PARAMUPMIX_COMBINATIONS, 0, input_Fs, 0 ) ), IVAS_ERR_OK ) )
191 : {
192 0 : return error;
193 : }
194 :
195 12 : fb_cfg->remix_order = mc_paramupmix_fb_remix_order;
196 12 : move16();
197 : /* override latency, could be moved to ivas_fb_set_cfg */
198 : /* assuming parameters are calculated at end of frame, compensate for MCT delay and half of decoder fb */
199 : /* still 1.5ms off, since MCT delay is not large enough */
200 : /* param at end of frame */
201 12 : fb_cfg->prior_input_length = (Word16) ( NS2SA_FX2( input_Fs, 12000000L ) + NS2SA_FX2( input_Fs, DELAY_FB_4_NS / 2 ) - input_frame / 2 - NS2SA_FX2( input_Fs, DELAY_FB_1_NS / 2 ) );
202 12 : fb_cfg->prior_input_length = s_max( fb_cfg->prior_input_length, (Word16) input_frame / MAX_PARAM_SPATIAL_SUBFRAMES );
203 :
204 : /* Allocate and initialize FB mixer handle */
205 12 : IF( NE_32( ( error = ivas_FB_mixer_open_fx( &( hMCParamUpmix->hFbMixer ), input_Fs, fb_cfg, 0 ) ), IVAS_ERR_OK ) )
206 : {
207 0 : return error;
208 : }
209 :
210 60 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
211 : {
212 : /* Covariance handle */
213 48 : IF( NE_32( ( error = ivas_spar_covar_enc_open_fx( &( hMCParamUpmix->hCovEnc[i] ), hMCParamUpmix->hFbMixer->pFb, input_Fs, MC_PARAMUPMIX_NCH + 1, COV_SMOOTH_MC, st_ivas->hEncoderConfig->ivas_total_brate ) ), IVAS_ERR_OK ) )
214 : {
215 0 : return error;
216 : }
217 : }
218 :
219 60 : FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
220 : {
221 48 : IF( ( hMCParamUpmix->cov_real_fx[b] = (Word32 ***) malloc( MC_PARAMUPMIX_NCH * sizeof( Word32 ** ) ) ) == NULL )
222 : {
223 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
224 : }
225 144 : FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
226 : {
227 96 : IF( ( hMCParamUpmix->cov_real_fx[b][i] = (Word32 **) malloc( MC_PARAMUPMIX_NCH * sizeof( Word32 * ) ) ) == NULL )
228 : {
229 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
230 : }
231 288 : FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
232 : {
233 192 : IF( ( hMCParamUpmix->cov_real_fx[b][i][j] = (Word32 *) malloc( IVAS_MAX_NUM_BANDS * sizeof( Word32 ) ) ) == NULL )
234 : {
235 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
236 : }
237 : }
238 : }
239 :
240 48 : IF( ( hMCParamUpmix->cov_dtx_real_fx[b] = (Word32 ***) malloc( MC_PARAMUPMIX_NCH * sizeof( Word32 ** ) ) ) == NULL )
241 : {
242 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
243 : }
244 144 : FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
245 : {
246 96 : IF( ( hMCParamUpmix->cov_dtx_real_fx[b][i] = (Word32 **) malloc( MC_PARAMUPMIX_NCH * sizeof( Word32 * ) ) ) == NULL )
247 : {
248 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
249 : }
250 288 : FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
251 : {
252 192 : IF( ( hMCParamUpmix->cov_dtx_real_fx[b][i][j] = (Word32 *) malloc( IVAS_MAX_NUM_BANDS * sizeof( Word32 ) ) ) == NULL )
253 : {
254 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
255 : }
256 : }
257 : }
258 : }
259 :
260 12 : st_ivas->hMCParamUpmix = hMCParamUpmix;
261 :
262 12 : return error;
263 : }
264 :
265 : /*-------------------------------------------------------------------------
266 : * ivas_mc_paramupmix_enc_close()
267 : *
268 : * Close MC Param-Upmix encoder handle
269 : *------------------------------------------------------------------------*/
270 1538 : void ivas_mc_paramupmix_enc_close_fx(
271 : MC_PARAMUPMIX_ENC_HANDLE *hMCParamUpmix, /* i/o: MC Param-Upmix encoder handle */
272 : const Word32 input_Fs /* i : input sampling rate */
273 : )
274 : {
275 : Word16 i, k;
276 : Word16 b, j;
277 :
278 1538 : test();
279 1538 : IF( hMCParamUpmix == NULL || *hMCParamUpmix == NULL )
280 : {
281 1526 : return;
282 : }
283 :
284 60 : FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
285 : {
286 48 : IF( ( *hMCParamUpmix )->cov_real_fx[b] != NULL )
287 : {
288 144 : FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
289 : {
290 96 : IF( ( *hMCParamUpmix )->cov_real_fx[b][i] != NULL )
291 : {
292 288 : FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
293 : {
294 192 : IF( ( *hMCParamUpmix )->cov_real_fx[b][i][j] != NULL )
295 : {
296 192 : free( ( *hMCParamUpmix )->cov_real_fx[b][i][j] );
297 : }
298 : }
299 96 : free( ( *hMCParamUpmix )->cov_real_fx[b][i] );
300 : }
301 : }
302 48 : free( ( *hMCParamUpmix )->cov_real_fx[b] );
303 : }
304 :
305 48 : IF( ( *hMCParamUpmix )->cov_dtx_real_fx[b] != NULL )
306 : {
307 144 : FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
308 : {
309 96 : IF( ( *hMCParamUpmix )->cov_dtx_real_fx[b][i] != NULL )
310 : {
311 288 : FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
312 : {
313 192 : IF( ( *hMCParamUpmix )->cov_dtx_real_fx[b][i][j] != NULL )
314 : {
315 192 : free( ( *hMCParamUpmix )->cov_dtx_real_fx[b][i][j] );
316 : }
317 : }
318 96 : free( ( *hMCParamUpmix )->cov_dtx_real_fx[b][i] );
319 : }
320 : }
321 48 : free( ( *hMCParamUpmix )->cov_dtx_real_fx[b] );
322 : }
323 : }
324 :
325 60 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
326 : {
327 144 : FOR( k = 0; k < MC_PARAMUPMIX_NCH; k++ )
328 : {
329 96 : free( ( *hMCParamUpmix )->midside_fx[i][k] );
330 : }
331 : }
332 :
333 108 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
334 : {
335 96 : ivas_transient_det_close_fx( &( *hMCParamUpmix )->hTranDet[i] );
336 : }
337 :
338 12 : IF( ( *hMCParamUpmix )->hFbMixer != NULL )
339 : {
340 12 : ivas_FB_mixer_close_fx( &( *hMCParamUpmix )->hFbMixer, input_Fs, 0 );
341 : }
342 :
343 60 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
344 : {
345 : /* Covariance handle */
346 48 : IF( ( *hMCParamUpmix )->hCovEnc[i] != NULL )
347 : {
348 48 : ivas_spar_covar_enc_close_fx( &( *hMCParamUpmix )->hCovEnc[i], ( MC_PARAMUPMIX_NCH + 1 ) );
349 : }
350 : }
351 :
352 12 : free( *hMCParamUpmix );
353 12 : *hMCParamUpmix = NULL;
354 :
355 12 : return;
356 : }
357 :
358 : /*****************************************************************************************/
359 : /* local functions */
360 : /*****************************************************************************************/
361 :
362 6320 : static void get_huff_table_fx(
363 : const PAR_TYPE par_type,
364 : HUFF_TAB *df0,
365 : HUFF_TAB *df )
366 : {
367 6320 : SWITCH( par_type )
368 : {
369 3160 : case ALPHA:
370 3160 : df0->value = huff_alpha_table.df0.value;
371 3160 : move16();
372 3160 : df0->length = huff_alpha_table.df0.length;
373 3160 : move16();
374 3160 : df->value = huff_alpha_table.df.value;
375 3160 : move16();
376 3160 : df->length = huff_alpha_table.df.length;
377 3160 : move16();
378 3160 : break;
379 3160 : case BETA:
380 3160 : df0->value = huff_beta_table.df0.value;
381 3160 : move16();
382 3160 : df0->length = huff_beta_table.df0.length;
383 3160 : move16();
384 3160 : df->value = huff_beta_table.df.value;
385 3160 : move16();
386 3160 : df->length = huff_beta_table.df.length;
387 3160 : move16();
388 3160 : break;
389 : }
390 :
391 6320 : return;
392 : }
393 :
394 :
395 75840 : static void write_huff_bits_fx(
396 : const Word32 value,
397 : const UWord16 length,
398 : UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS],
399 : Word16 *bit_pos )
400 : {
401 : Word16 k;
402 :
403 235311 : FOR( k = length - 1; k >= 0; k-- )
404 : {
405 159471 : bit_buffer[( *bit_pos )++] = (UWord16) ( ( L_shr( value, k ) ) & 1 );
406 : }
407 :
408 75840 : return;
409 : }
410 :
411 6320 : static void huffman_encode_fx(
412 : const Word32 *vqPrev,
413 : const Word32 *vq,
414 : const PAR_TYPE parType,
415 : const Word16 nq,
416 : UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS],
417 : Word16 *bit_pos )
418 : {
419 : Word16 iv;
420 : Word32 icode;
421 : Word16 offset;
422 : HUFF_TAB df0, df;
423 :
424 6320 : get_huff_table_fx( parType, &df0, &df );
425 :
426 6320 : offset = sub( nq, 1 ); /* range [-(nquant - 1), nquant - 1] */
427 :
428 82160 : FOR( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
429 : {
430 75840 : IF( iv == 0 )
431 : {
432 6320 : icode = vq[iv];
433 : }
434 : ELSE
435 : {
436 69520 : icode = L_add( L_sub( vq[iv], vq[iv - 1] ), offset );
437 : }
438 75840 : move16();
439 :
440 75840 : icode = L_add( L_sub( vq[iv], vqPrev[iv] ), offset );
441 : }
442 :
443 : /* Write the bitstream */
444 6320 : bit_buffer[( *bit_pos )++] = (UWord16) 0 & 1;
445 6320 : move16();
446 82160 : FOR( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
447 : {
448 75840 : IF( iv == 0 )
449 : {
450 6320 : icode = vq[iv];
451 6320 : move32();
452 6320 : write_huff_bits_fx( df0.value[icode], df0.length[icode], bit_buffer, bit_pos );
453 : }
454 : ELSE
455 : {
456 69520 : icode = L_add( L_sub( vq[iv], vq[iv - 1] ), offset );
457 69520 : write_huff_bits_fx( df.value[icode], df.length[icode], bit_buffer, bit_pos );
458 : }
459 : }
460 :
461 6320 : return;
462 : }
463 :
464 6320 : static void quantize_pars_fx(
465 : const Word32 *v_fx, // exp_v
466 : Word16 exp_v,
467 : const Word16 nq,
468 : const Word32 *data_fx, // Q28
469 : Word32 vq[IVAS_MAX_NUM_BANDS],
470 : Word32 *vdeq_fx, // exp_vdeq
471 : Word16 *exp_vdeq )
472 : {
473 : Word16 iv, iq, iq0, iq1;
474 :
475 82160 : FOR( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
476 : {
477 75840 : iq0 = 0;
478 75840 : iq1 = sub( nq, 1 );
479 :
480 455040 : WHILE( ( iq1 - iq0 ) > 1 )
481 : {
482 379200 : iq = shr( add( iq0, iq1 ), 1 );
483 379200 : Word16 cmp_1 = BASOP_Util_Cmp_Mant32Exp( v_fx[iv], exp_v, data_fx[iq], 31 - Q28 );
484 379200 : IF( EQ_16( cmp_1, negate( 1 ) ) )
485 : {
486 207008 : iq1 = iq;
487 : }
488 : ELSE
489 : {
490 172192 : iq0 = iq;
491 : }
492 : }
493 :
494 75840 : Word16 exp_var1 = 0;
495 75840 : move16();
496 75840 : Word32 var1 = BASOP_Util_Add_Mant32Exp( v_fx[iv], exp_v, L_negate( data_fx[iq0] ), 31 - Q28, &exp_var1 );
497 75840 : var1 = L_abs( var1 );
498 :
499 75840 : Word16 exp_var2 = 0;
500 75840 : move16();
501 75840 : Word32 var2 = BASOP_Util_Add_Mant32Exp( v_fx[iv], exp_v, L_negate( data_fx[iq1] ), 31 - Q28, &exp_var2 );
502 75840 : var2 = L_abs( var2 );
503 :
504 75840 : Word16 cmp_2 = BASOP_Util_Cmp_Mant32Exp( var1, exp_var1, var2, exp_var2 );
505 :
506 75840 : IF( EQ_16( cmp_2, negate( 1 ) ) )
507 : {
508 36866 : vq[iv] = iq0;
509 36866 : vdeq_fx[iv] = data_fx[iq0];
510 : }
511 : ELSE
512 : {
513 38974 : vq[iv] = iq1;
514 38974 : vdeq_fx[iv] = data_fx[iq1];
515 : }
516 75840 : move16();
517 75840 : move32();
518 : }
519 :
520 6320 : *exp_vdeq = sub( 31, Q28 );
521 6320 : move16();
522 :
523 6320 : return;
524 : }
525 :
526 6320 : static void quantize_alpha_fx(
527 : const Word32 *alpha_fx, // 31 - exp_alpha
528 : Word16 exp_alpha,
529 : Word16 *pnq,
530 : Word32 aq[IVAS_MAX_NUM_BANDS],
531 : Word32 *adeq_fx, // 31 - exp_adeq
532 : Word16 *exp_adeq )
533 : {
534 : Word16 nq;
535 : const Word32 *data_fx;
536 :
537 6320 : nq = ivas_mc_paramupmix_alpha_quant_table_fx.nquant; // Q0
538 6320 : move16();
539 6320 : data_fx = ivas_mc_paramupmix_alpha_quant_table_fx.data; // Q28
540 :
541 6320 : quantize_pars_fx( alpha_fx, exp_alpha, nq, data_fx, aq, adeq_fx, exp_adeq );
542 :
543 6320 : *pnq = nq;
544 6320 : move16();
545 :
546 6320 : return;
547 : }
548 :
549 3160 : static void quantize_beta_fx(
550 : const Word32 *beta_fx,
551 : Word16 exp_beta,
552 : const Word32 aq[IVAS_MAX_NUM_BANDS],
553 : Word16 *pnq,
554 : Word32 bq[IVAS_MAX_NUM_BANDS],
555 : Word32 *bdeq_fx,
556 : Word16 *exp_bdeq )
557 : {
558 : Word16 iv, iq, iq0, iq1;
559 :
560 3160 : const ACPL_QUANT_TABLE_FX *tables_fx = ivas_mc_paramupmix_beta_quant_table_fx; // Q28
561 :
562 : ACPL_QUANT_TABLE_FX quant_table_fx;
563 :
564 41080 : FOR( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
565 : {
566 37920 : quant_table_fx = tables_fx[ivas_param_upmx_mx_qmap[aq[iv]]]; // Q28
567 :
568 37920 : iq0 = 0;
569 37920 : move16();
570 37920 : iq1 = sub( quant_table_fx.nquant, 1 );
571 :
572 151680 : WHILE( ( ( iq1 - iq0 ) > 1 ) )
573 : {
574 113760 : iq = shr( add( iq0, iq1 ), 1 );
575 :
576 113760 : Word16 cmp_1 = BASOP_Util_Cmp_Mant32Exp( beta_fx[iv], exp_beta, quant_table_fx.data[iq], 31 - Q28 );
577 :
578 113760 : IF( EQ_16( cmp_1, negate( 1 ) ) )
579 : {
580 88478 : iq1 = iq;
581 : }
582 : ELSE
583 : {
584 25282 : iq0 = iq;
585 : }
586 113760 : move16();
587 : }
588 :
589 37920 : Word16 exp_var1 = 0;
590 37920 : move16();
591 37920 : Word32 var1 = BASOP_Util_Add_Mant32Exp( beta_fx[iv], exp_beta, L_negate( quant_table_fx.data[iq0] ), 31 - Q28, &exp_var1 );
592 37920 : var1 = L_abs( var1 );
593 :
594 37920 : Word16 exp_var2 = 0;
595 37920 : move16();
596 37920 : Word32 var2 = BASOP_Util_Add_Mant32Exp( beta_fx[iv], exp_beta, L_negate( quant_table_fx.data[iq1] ), 31 - Q28, &exp_var2 );
597 37920 : var2 = L_abs( var2 );
598 :
599 37920 : Word16 cmp_2 = BASOP_Util_Cmp_Mant32Exp( var1, exp_var1, var2, exp_var2 );
600 :
601 37920 : IF( EQ_16( cmp_2, negate( 1 ) ) )
602 : {
603 21049 : bq[iv] = iq0;
604 21049 : bdeq_fx[iv] = quant_table_fx.data[iq0]; // Q28
605 : }
606 : ELSE
607 : {
608 16871 : bq[iv] = iq1;
609 16871 : bdeq_fx[iv] = quant_table_fx.data[iq1]; // Q28
610 : }
611 37920 : move16();
612 37920 : move32();
613 : }
614 3160 : *exp_bdeq = sub( 31, Q28 );
615 :
616 3160 : *pnq = ivas_mc_paramupmix_beta_quant_table_fx[0].nquant;
617 3160 : move16();
618 :
619 3160 : return;
620 : }
621 :
622 6320 : static void put_ec_data_fx(
623 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix,
624 : const Word16 ch,
625 : const Word32 pars_fx[IVAS_MAX_NUM_BANDS], // Q(31 - exp_paras)
626 : Word16 exp_paras,
627 : const Word32 alphas_fx[IVAS_MAX_NUM_BANDS], // Q(31 - exp_alphas)
628 : Word16 exp_alphas,
629 : const PAR_TYPE parType,
630 : UWord16 bit_buffer[MC_PARAMUPMIX_MAX_BITS],
631 : Word16 *bit_pos )
632 : {
633 : Word16 nq;
634 : Word32 alphaQuant[IVAS_MAX_NUM_BANDS];
635 : Word32 betaQuant[IVAS_MAX_NUM_BANDS];
636 : Word32 alphaDequant_fx[IVAS_MAX_NUM_BANDS]; // 31 - exp_alphaDequant
637 : Word32 betaDequant_fx[IVAS_MAX_NUM_BANDS]; // 31 - exp_betaDequant
638 6320 : Word16 exp_alphaDequant = 31, exp_betaDequant = 31;
639 :
640 :
641 6320 : IF( EQ_16( parType, ALPHA ) )
642 : {
643 3160 : quantize_alpha_fx( pars_fx, exp_paras, &nq, alphaQuant, alphaDequant_fx, &exp_alphaDequant );
644 : }
645 : ELSE
646 : {
647 3160 : quantize_alpha_fx( alphas_fx, exp_alphas, &nq, alphaQuant, alphaDequant_fx, &exp_alphaDequant );
648 3160 : quantize_beta_fx( pars_fx, exp_paras, alphaQuant, &nq, betaQuant, betaDequant_fx, &exp_betaDequant );
649 : }
650 :
651 6320 : IF( hMCParamUpmix->first_frame )
652 : {
653 96 : Copy32( &( alphaQuant[0] ), &( hMCParamUpmix->alpha_quant_prev[ch][0] ), IVAS_MAX_NUM_BANDS );
654 96 : IF( EQ_16( parType, BETA ) )
655 : {
656 48 : Copy32( &( betaQuant[0] ), &( hMCParamUpmix->beta_quant_prev[ch][0] ), IVAS_MAX_NUM_BANDS );
657 48 : if ( EQ_16( ch, ( MC_PARAMUPMIX_COMBINATIONS - 1 ) ) )
658 : {
659 12 : hMCParamUpmix->first_frame = 0;
660 12 : move16();
661 : }
662 : }
663 : }
664 :
665 : /* Always one parameter set per frame for transient frames. Original PS framing is used internally. */
666 6320 : IF( EQ_16( parType, ALPHA ) )
667 : {
668 3160 : huffman_encode_fx( hMCParamUpmix->alpha_quant_prev[ch], alphaQuant, ALPHA, nq, bit_buffer, bit_pos );
669 : }
670 : ELSE
671 : {
672 3160 : huffman_encode_fx( hMCParamUpmix->beta_quant_prev[ch], betaQuant, BETA, nq, bit_buffer, bit_pos );
673 : }
674 :
675 6320 : IF( EQ_16( parType, ALPHA ) )
676 : {
677 3160 : Copy32( alphaQuant, hMCParamUpmix->alpha_quant_prev[ch], IVAS_MAX_NUM_BANDS );
678 : }
679 : ELSE
680 : {
681 3160 : Copy32( betaQuant, hMCParamUpmix->beta_quant_prev[ch], IVAS_MAX_NUM_BANDS );
682 : }
683 :
684 6320 : return;
685 : }
686 :
687 : /*-------------------------------------------------------------------------
688 : * ivas_mc_paramupmix_dmx()
689 : *
690 : * Computes the time domain down mix signal
691 : *------------------------------------------------------------------------*/
692 :
693 790 : static void ivas_mc_paramupmix_dmx_fx(
694 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, /* i/o: MC ParamUpmix encoder handle */
695 : Word32 *data_fx[],
696 : /* i/o: Input, downmix out */ // st_ivas->q_data_fx
697 : const Word16 input_frame /* i : Input frame length */
698 : )
699 : {
700 : Word16 i, l;
701 790 : const Word16 chan1s[4] = { 4, 5, 8, 9 };
702 790 : const Word16 chan2s[4] = { 6, 7, 10, 11 };
703 790 : const Word16 chanOut[4] = { 4, 5, 6, 7 };
704 790 : const Word16 chanZero[4] = { 8, 9, 10, 11 };
705 790 : move16();
706 790 : move16();
707 790 : move16();
708 790 : move16();
709 790 : move16();
710 790 : move16();
711 790 : move16();
712 790 : move16();
713 790 : move16();
714 790 : move16();
715 790 : move16();
716 790 : move16();
717 790 : move16();
718 790 : move16();
719 790 : move16();
720 790 : move16();
721 :
722 : /* boxes = { 0 1 2 3 [4 6] [5 7] [8 10] [9 11] }; */
723 : /* 9+11 -> 7 */
724 : /* 8+10 -> 6 */
725 : /* 5+7 -> 5 */
726 : /* 4+6 -> 4 */
727 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
728 : {
729 3036760 : FOR( l = 0; l < input_frame; l++ )
730 : {
731 : /* mid */
732 3033600 : hMCParamUpmix->midside_fx[i][0][l] = L_shr( L_add( data_fx[chan1s[i]][l], data_fx[chan2s[i]][l] ), 1 ); // st_ivas->q_data_fx
733 3033600 : move32();
734 : /* side */
735 3033600 : hMCParamUpmix->midside_fx[i][1][l] = L_shr( L_sub( data_fx[chan1s[i]][l], data_fx[chan2s[i]][l] ), 1 ); // st_ivas->q_data_fx
736 3033600 : move32();
737 3033600 : data_fx[chanOut[i]][l] = hMCParamUpmix->midside_fx[i][0][l];
738 3033600 : move32();
739 : }
740 : }
741 :
742 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
743 : {
744 3160 : set_zero_fx( data_fx[chanZero[i]], input_frame );
745 : }
746 :
747 790 : return;
748 : }
749 :
750 :
751 : /*-------------------------------------------------------------------------
752 : * ivas_mc_paramupmix_param_est_enc()
753 : *
754 : * run the CLDFB analysis on the input signal
755 : * estimate the input and down mix covariances
756 : *------------------------------------------------------------------------*/
757 :
758 790 : static ivas_error ivas_mc_paramupmix_param_est_enc_fx(
759 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, /* i/o: MC Param-Upmix encoder handle */
760 : Word32 *data_f_fx[], // Q_data_f
761 : Word16 Q_data_f,
762 : const Word16 input_frame, /* i : Input frame length */
763 : Word32 alphas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], // 31 - exp_alphas
764 : Word16 *exp_alphas,
765 : Word32 betas_fx[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], // 31 - exp_betas
766 : Word16 *exp_betas )
767 : {
768 :
769 : Word32 *pcm_in_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
770 : Word32 FR_Real_Mid_fx[L_FRAME48k], FR_Imag_Mid_fx[L_FRAME48k];
771 : Word32 fr_realbuffer_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH][L_FRAME48k];
772 : Word32 fr_imagbuffer_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH][L_FRAME48k];
773 : Word32 *p_fr_realbuffer_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
774 : Word32 *p_fr_imagbuffer_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
775 : Word32 *cov_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH]; // q_cov_real
776 : Word32 *cov_dtx_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH]; // q_cov_dtx_real
777 : Word16 *q_cov_real[IVAS_SPAR_MAX_CH];
778 : Word16 *q_cov_dtx_real[IVAS_SPAR_MAX_CH];
779 : Word32 *pp_in_fr_real_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH], *pp_in_fr_imag_fx[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH]; // q_ppIn_FR
780 : Word16 q_ppIn_FR;
781 :
782 : Word32 rxy_fx, ryy_fx;
783 : Word32 rxx_fx;
784 : Word32 rxxest_fx, drxx_fx;
785 : Word16 exp_drxx;
786 : Word16 wetaux_fx, exp_wetaux;
787 : Word16 cmat_fx, exp_cmat;
788 :
789 : Word16 l_ts;
790 : Word16 b, i, j, ts, bnd;
791 : Word16 maxbands;
792 : Word16 transient_det[MC_PARAMUPMIX_COMBINATIONS][2];
793 : Word16 transient_det_l[2], transient_det_r[2];
794 790 : const Word16 chan1s[MC_PARAMUPMIX_COMBINATIONS] = { 4, 5, 8, 9 };
795 790 : const Word16 chan2s[MC_PARAMUPMIX_COMBINATIONS] = { 6, 7, 10, 11 };
796 790 : const Word16 HOA_md_ind[IVAS_SPAR_MAX_CH] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
797 790 : move16();
798 790 : move16();
799 790 : move16();
800 790 : move16();
801 790 : move16();
802 790 : move16();
803 790 : move16();
804 790 : move16();
805 790 : move16();
806 790 : move16();
807 790 : move16();
808 790 : move16();
809 790 : move16();
810 790 : move16();
811 790 : move16();
812 790 : move16();
813 790 : move16();
814 790 : move16();
815 790 : move16();
816 :
817 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
818 : {
819 3160 : pcm_in_fx[2 * i] = data_f_fx[chan1s[i]];
820 3160 : pcm_in_fx[2 * i + 1] = data_f_fx[chan2s[i]];
821 :
822 3160 : Scale_sig32( pcm_in_fx[2 * i], input_frame, sub( Q14, Q_data_f ) ); // Q14
823 3160 : Scale_sig32( pcm_in_fx[2 * i + 1], input_frame, sub( Q14, Q_data_f ) ); // Q14
824 : }
825 :
826 : /*-----------------------------------------------------------------------------------------*
827 : * Transient detector
828 : *-----------------------------------------------------------------------------------------*/
829 :
830 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
831 : {
832 3160 : ivas_transient_det_process_fx( hMCParamUpmix->hTranDet[2 * i], pcm_in_fx[2 * i], input_frame, transient_det_l );
833 3160 : ivas_transient_det_process_fx( hMCParamUpmix->hTranDet[2 * i + 1], pcm_in_fx[2 * i + 1], input_frame, transient_det_r );
834 3160 : test();
835 3160 : transient_det[i][0] = transient_det_l[0] || transient_det_r[0];
836 3160 : move16();
837 3160 : test();
838 3160 : transient_det[i][1] = transient_det_l[0] || transient_det_r[0];
839 3160 : move16();
840 : /* should probably be transient_det_l[1] || transient_det_r[1] , but choosing 0 reproduces the before merge state */
841 : }
842 :
843 7110 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
844 : {
845 6320 : p_fr_realbuffer_fx[i] = fr_realbuffer_fx[i];
846 6320 : p_fr_imagbuffer_fx[i] = fr_imagbuffer_fx[i];
847 : }
848 :
849 : /* prepare Parameter MDFT analysis */
850 7110 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
851 : {
852 6320 : pp_in_fr_real_fx[i] = p_fr_realbuffer_fx[i];
853 6320 : pp_in_fr_imag_fx[i] = p_fr_imagbuffer_fx[i];
854 : }
855 :
856 : // l_ts = input_frame / MAX_PARAM_SPATIAL_SUBFRAMES;
857 790 : l_ts = shr( input_frame, MAX_PARAM_SPATIAL_SUBFRAMES_LOG2 );
858 790 : move16();
859 :
860 790 : Word16 gb = find_guarded_bits_fx( l_ts );
861 :
862 3950 : FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
863 : {
864 3160 : ivas_fb_mixer_get_windowed_fr_fx( hMCParamUpmix->hFbMixer, pcm_in_fx, pp_in_fr_real_fx, pp_in_fr_imag_fx, l_ts, l_ts, hMCParamUpmix->hFbMixer->fb_cfg->num_in_chans, gb );
865 :
866 3160 : ivas_fb_mixer_update_prior_input_fx( hMCParamUpmix->hFbMixer, pcm_in_fx, l_ts, hMCParamUpmix->hFbMixer->fb_cfg->num_in_chans );
867 :
868 28440 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
869 : {
870 25280 : pcm_in_fx[i] += l_ts;
871 25280 : pp_in_fr_real_fx[i] += l_ts;
872 25280 : pp_in_fr_imag_fx[i] += l_ts;
873 : }
874 : }
875 :
876 3950 : FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
877 : {
878 28440 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
879 : {
880 25280 : pcm_in_fx[i] -= l_ts;
881 25280 : pp_in_fr_real_fx[i] -= l_ts;
882 25280 : pp_in_fr_imag_fx[i] -= l_ts;
883 : }
884 : }
885 :
886 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
887 : {
888 3160 : Scale_sig32( pcm_in_fx[2 * i], input_frame, sub( Q_data_f, Q14 ) );
889 3160 : Scale_sig32( pcm_in_fx[2 * i + 1], input_frame, sub( Q_data_f, Q14 ) );
890 : }
891 :
892 : /*-----------------------------------------------------------------------------------------*
893 : * Covariance process
894 : *-----------------------------------------------------------------------------------------*/
895 :
896 3950 : FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
897 : {
898 3160 : pp_in_fr_real_fx[0] = p_fr_realbuffer_fx[2 * b]; // q_ppIn_FR
899 3160 : pp_in_fr_imag_fx[0] = p_fr_imagbuffer_fx[2 * b]; // q_ppIn_FR
900 3160 : pp_in_fr_real_fx[1] = FR_Real_Mid_fx; // q_ppIn_FR
901 3160 : pp_in_fr_imag_fx[1] = FR_Imag_Mid_fx; // q_ppIn_FR
902 :
903 3160 : v_add_fx( pp_in_fr_real_fx[0], p_fr_realbuffer_fx[2 * b + 1], pp_in_fr_real_fx[1], L_FRAME48k );
904 3160 : v_add_fx( pp_in_fr_imag_fx[0], p_fr_imagbuffer_fx[2 * b + 1], pp_in_fr_imag_fx[1], L_FRAME48k );
905 :
906 9480 : FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
907 : {
908 18960 : FOR( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
909 : {
910 12640 : cov_real_fx[i][j] = hMCParamUpmix->cov_real_fx[b][i][j]; // q_cov_real
911 12640 : move32();
912 12640 : cov_dtx_real_fx[i][j] = hMCParamUpmix->cov_dtx_real_fx[b][i][j]; // q_cov_dtx_real
913 12640 : move32();
914 : }
915 6320 : IF( ( q_cov_real[i] = (Word16 *) malloc( sizeof( Word16 ) * MC_PARAMUPMIX_NCH ) ) == NULL )
916 : {
917 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
918 : }
919 6320 : set_s( q_cov_real[i], Q31, MC_PARAMUPMIX_NCH );
920 6320 : IF( ( q_cov_dtx_real[i] = (Word16 *) malloc( sizeof( Word16 ) * MC_PARAMUPMIX_NCH ) ) == NULL )
921 : {
922 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
923 : }
924 6320 : set_s( q_cov_dtx_real[i], Q31, MC_PARAMUPMIX_NCH );
925 : }
926 :
927 3160 : q_ppIn_FR = sub( Q14, gb );
928 3160 : ivas_enc_cov_handler_process_fx( hMCParamUpmix->hCovEnc[b], pp_in_fr_real_fx, pp_in_fr_imag_fx, q_ppIn_FR, cov_real_fx, q_cov_real, cov_dtx_real_fx, q_cov_dtx_real, hMCParamUpmix->hFbMixer->pFb, 0, hMCParamUpmix->hFbMixer->pFb->filterbank_num_bands, MC_PARAMUPMIX_NCH, 0 /*dtx_vad*/, transient_det[b], HOA_md_ind, NULL, NULL, NULL, 0, 0 );
929 :
930 9480 : FOR( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
931 : {
932 6320 : free( q_cov_real[i] );
933 6320 : q_cov_real[i] = NULL;
934 6320 : free( q_cov_dtx_real[i] );
935 6320 : q_cov_dtx_real[i] = NULL;
936 : }
937 : }
938 :
939 790 : maxbands = hMCParamUpmix->hFbMixer->pFb->filterbank_num_bands;
940 790 : move16();
941 : Word16 exp_alpha_buff[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS];
942 : Word16 exp_beta_buff[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS];
943 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
944 : {
945 3160 : set16_fx( &exp_alpha_buff[i][0], 0, IVAS_MAX_NUM_BANDS );
946 3160 : set16_fx( &exp_beta_buff[i][0], 0, IVAS_MAX_NUM_BANDS );
947 : }
948 :
949 3950 : FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
950 : {
951 41080 : FOR( bnd = 0; bnd < maxbands; bnd++ )
952 : {
953 37920 : rxy_fx = hMCParamUpmix->cov_real_fx[b][1][0][bnd];
954 37920 : move32();
955 37920 : ryy_fx = hMCParamUpmix->cov_real_fx[b][1][1][bnd];
956 37920 : move32();
957 :
958 37920 : exp_cmat = 0;
959 37920 : move16();
960 37920 : cmat_fx = BASOP_Util_Divide3232_Scale( rxy_fx, L_add( ryy_fx, EPSILON_FX ), &exp_cmat );
961 37920 : exp_cmat = sub( add( exp_cmat, sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[1][0][bnd] ) ) ), sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[1][1][bnd] ) ) );
962 :
963 37920 : Word16 alpha_fx = 0, exp_alpha_var = 0;
964 37920 : move16();
965 37920 : move16();
966 37920 : exp_alpha_var = BASOP_Util_Add_MantExp( cmat_fx, add( exp_cmat, 1 ), negate( ONE_IN_Q14 ), 1, &alpha_fx );
967 :
968 37920 : rxx_fx = hMCParamUpmix->cov_real_fx[b][0][0][bnd];
969 37920 : move32();
970 37920 : Word32 tmp_2 = L_mult( cmat_fx, cmat_fx ); // exp_cmat * 2
971 37920 : rxxest_fx = Mpy_32_32( tmp_2, ryy_fx );
972 :
973 37920 : exp_drxx = 0;
974 37920 : move16();
975 37920 : drxx_fx = BASOP_Util_Add_Mant32Exp( rxx_fx, sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[0][0][bnd] ) ), L_negate( rxxest_fx ),
976 37920 : add( add( exp_cmat, exp_cmat ), sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[1][1][bnd] ) ) ), &exp_drxx );
977 37920 : drxx_fx = L_max( drxx_fx, 0 );
978 :
979 37920 : exp_wetaux = 0;
980 37920 : move16();
981 37920 : wetaux_fx = BASOP_Util_Divide3232_Scale( drxx_fx, L_add( ryy_fx, EPSILON_FX ), &exp_wetaux );
982 37920 : exp_wetaux = sub( add( exp_wetaux, exp_drxx ), sub( 31, ( hMCParamUpmix->hCovEnc[b]->pCov_state->q_cov_real_per_band[1][1][bnd] ) ) );
983 :
984 37920 : Word16 sqrt_wetaux_fx = 0, exp_sqrt_wetaux = 0;
985 37920 : move16();
986 37920 : move16();
987 :
988 37920 : IF( wetaux_fx != 0 )
989 : {
990 37865 : Word16 tmp_4 = 0, exp_tmp4 = 0;
991 37865 : move16();
992 37865 : move16();
993 37865 : BASOP_Util_Sqrt_InvSqrt_MantExp( wetaux_fx, exp_wetaux, &sqrt_wetaux_fx, &exp_sqrt_wetaux, &tmp_4, &exp_tmp4 );
994 : }
995 : ELSE
996 : {
997 55 : exp_sqrt_wetaux = -1;
998 55 : move16();
999 : }
1000 :
1001 37920 : Word16 exp_betas_var = add( exp_sqrt_wetaux, 1 );
1002 :
1003 37920 : alphas_fx[b][bnd] = L_deposit_h( alpha_fx );
1004 37920 : move16();
1005 37920 : betas_fx[b][bnd] = L_deposit_h( sqrt_wetaux_fx );
1006 37920 : move16();
1007 37920 : exp_alpha_buff[b][bnd] = exp_alpha_var;
1008 37920 : move16();
1009 37920 : exp_beta_buff[b][bnd] = exp_betas_var;
1010 37920 : move16();
1011 : }
1012 : }
1013 :
1014 790 : Word16 max_exp_alpha_buff = 0, max_exp_beta_buff = 0;
1015 790 : move16();
1016 790 : move16();
1017 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
1018 : {
1019 41080 : FOR( j = 0; j < maxbands; j++ )
1020 : {
1021 37920 : max_exp_alpha_buff = s_max( max_exp_alpha_buff, exp_alpha_buff[i][j] );
1022 37920 : max_exp_beta_buff = s_max( max_exp_beta_buff, exp_beta_buff[i][j] );
1023 : }
1024 : }
1025 3950 : FOR( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
1026 : {
1027 41080 : FOR( j = 0; j < maxbands; j++ )
1028 : {
1029 37920 : alphas_fx[i][j] = L_shr( alphas_fx[i][j], sub( max_exp_alpha_buff, exp_alpha_buff[i][j] ) );
1030 37920 : move16();
1031 37920 : betas_fx[i][j] = L_shr( betas_fx[i][j], sub( max_exp_beta_buff, exp_beta_buff[i][j] ) );
1032 37920 : move16();
1033 : }
1034 : }
1035 790 : *exp_alphas = max_exp_alpha_buff;
1036 790 : *exp_betas = max_exp_beta_buff;
1037 790 : move16();
1038 790 : move16();
1039 :
1040 790 : IF( LT_16( maxbands, IVAS_MAX_NUM_BANDS ) )
1041 : {
1042 0 : FOR( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
1043 : {
1044 0 : FOR( bnd = maxbands; bnd < IVAS_MAX_NUM_BANDS; bnd++ )
1045 : {
1046 0 : alphas_fx[b][bnd] = 0;
1047 0 : move16();
1048 0 : betas_fx[b][bnd] = 0;
1049 0 : move16();
1050 : }
1051 : }
1052 : }
1053 :
1054 :
1055 790 : return IVAS_ERR_OK;
1056 : }
|