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 <stdint.h>
34 : #include <assert.h>
35 : #include "options.h"
36 : #include "ivas_prot_fx.h"
37 : #include "prot_fx.h"
38 : #include "ivas_rom_com.h"
39 : #include "ivas_stat_com.h"
40 : #include "math.h"
41 : #include "wmc_auto.h"
42 : #include "prot_fx_enc.h"
43 : #ifdef DEBUGGING
44 : #include "debug.h"
45 : #endif
46 :
47 :
48 : /*-------------------------------------------------------------------*
49 : * Local function prototypes
50 : *--------------------------------------------------------------------*/
51 :
52 : static ivas_error ivas_spar_enc_process_fx( Encoder_Struct *st_ivas, const ENCODER_CONFIG_HANDLE hEncoderConfig, BSTR_ENC_HANDLE hMetaData, const Word16 front_vad_flag, Word32 *data_fx[], const Word16 q_data );
53 :
54 : /*-------------------------------------------------------------------------
55 : * ivas_spar_enc_open()
56 : *
57 : * Allocate and initialize SPAR encoder handle and sub-handles
58 : *------------------------------------------------------------------------*/
59 1637 : ivas_error ivas_spar_enc_open_fx(
60 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder handle */
61 : const Word16 spar_reconfig_flag /* i : SPAR reconfiguration flag */
62 : )
63 : {
64 : SPAR_ENC_HANDLE hSpar;
65 : ENCODER_CONFIG_HANDLE hEncoderConfig;
66 : IVAS_FB_CFG *fb_cfg;
67 : Word16 nchan_inp, nchan_transport, sba_order_internal;
68 : Word16 nchan_fb_in;
69 : Word16 table_idx, active_w_mixing;
70 : Word32 input_Fs, ivas_total_brate;
71 : ivas_error error;
72 :
73 1637 : hEncoderConfig = st_ivas->hEncoderConfig;
74 1637 : error = IVAS_ERR_OK;
75 1637 : move32();
76 1637 : hSpar = st_ivas->hSpar;
77 :
78 1637 : IF( !spar_reconfig_flag )
79 : {
80 : /* SPAR encoder handle */
81 282 : IF( ( hSpar = (SPAR_ENC_HANDLE) malloc( sizeof( SPAR_ENC_DATA ) ) ) == NULL )
82 : {
83 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR encoder" );
84 : }
85 : }
86 :
87 1637 : hSpar->spar_reconfig_flag = 0;
88 1637 : move16();
89 1637 : input_Fs = hEncoderConfig->input_Fs;
90 1637 : move32();
91 1637 : sba_order_internal = extract_l( L_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ) );
92 1637 : nchan_inp = ivas_sba_get_nchan_metadata_fx( sba_order_internal, hEncoderConfig->ivas_total_brate );
93 1637 : assert( nchan_inp <= hEncoderConfig->nchan_inp );
94 1637 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
95 1637 : move32();
96 1637 : nchan_fb_in = 0;
97 1637 : move16();
98 1637 : IF( EQ_16( st_ivas->sba_analysis_order, 1 ) )
99 : {
100 1378 : nchan_fb_in = FOA_CHANNELS;
101 1378 : move16();
102 : }
103 259 : ELSE IF( EQ_16( st_ivas->sba_analysis_order, 2 ) )
104 : {
105 73 : nchan_fb_in = 9;
106 73 : move16();
107 : }
108 186 : ELSE IF( EQ_16( st_ivas->sba_analysis_order, 3 ) )
109 : {
110 186 : nchan_fb_in = 11;
111 186 : move16();
112 : }
113 : ELSE
114 : {
115 0 : assert( 0 && "sba_order must be 1,2, or 3!" );
116 : }
117 :
118 1637 : nchan_transport = ivas_get_sba_num_TCs_fx( hEncoderConfig->ivas_total_brate, sba_order_internal );
119 :
120 1637 : table_idx = ivas_get_spar_table_idx_fx( ivas_total_brate, sba_order_internal, SPAR_CONFIG_BW, NULL, NULL );
121 :
122 : /* MD handle */
123 1637 : IF( ( error = ivas_spar_md_enc_open_fx( &( hSpar->hMdEnc ), hEncoderConfig, sba_order_internal ) ) != IVAS_ERR_OK )
124 : {
125 0 : return error;
126 : }
127 :
128 : /* set FB config. */
129 1637 : active_w_mixing = ivas_spar_br_table_consts[table_idx].active_w;
130 1637 : move16();
131 1637 : IF( NE_32( ( error = ivas_fb_set_cfg( &fb_cfg, SBA_FORMAT, nchan_inp, nchan_transport, active_w_mixing, input_Fs, nchan_fb_in ) ), IVAS_ERR_OK ) )
132 : {
133 0 : return error;
134 : }
135 1637 : fb_cfg->remix_order = remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order];
136 :
137 : /* FB mixer handle */
138 1637 : IF( NE_32( ( error = ivas_FB_mixer_open_fx( &( hSpar->hFbMixer ), input_Fs, fb_cfg, spar_reconfig_flag ) ), IVAS_ERR_OK ) )
139 : {
140 0 : return error;
141 : }
142 :
143 : /* Covariance handle */
144 1637 : IF( NE_32( ( error = ivas_spar_covar_enc_open_fx( &( hSpar->hCovEnc ), hSpar->hFbMixer->pFb, input_Fs, nchan_inp, COV_SMOOTH_SPAR, hEncoderConfig->ivas_total_brate ) ), IVAS_ERR_OK ) )
145 : {
146 0 : return error;
147 : }
148 1637 : IF( !spar_reconfig_flag )
149 : {
150 : /* Transient Detector handle */
151 282 : IF( NE_32( ( error = ivas_transient_det_open_fx( &( hSpar->hTranDet ), input_Fs ) ), IVAS_ERR_OK ) )
152 : {
153 0 : return error;
154 : }
155 : }
156 :
157 : /* initialization */
158 1637 : hSpar->hMdEnc->table_idx = -1;
159 1637 : move16();
160 : /* AGC handle */
161 1637 : hSpar->AGC_Enable = ivas_agc_enc_get_flag( nchan_transport );
162 1637 : move16();
163 1637 : hSpar->hAgcEnc = NULL;
164 1637 : IF( hSpar->AGC_Enable )
165 : {
166 567 : IF( NE_32( ( error = ivas_spar_agc_enc_open_fx( &hSpar->hAgcEnc, input_Fs, nchan_inp ) ), IVAS_ERR_OK ) )
167 : {
168 0 : return error;
169 : }
170 : }
171 :
172 : /* PCA handle */
173 1637 : hSpar->hPCA = NULL;
174 1637 : IF( hEncoderConfig->Opt_PCA_ON )
175 : {
176 4 : IF( ( hSpar->hPCA = (PCA_ENC_STATE *) malloc( sizeof( PCA_ENC_STATE ) ) ) == NULL )
177 : {
178 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR PCA encoder" );
179 : }
180 4 : ivas_pca_enc_init_fx( hSpar->hPCA );
181 : }
182 :
183 : /* initialization */
184 1637 : hSpar->hMdEnc->table_idx = -1;
185 1637 : move16();
186 : /*-----------------------------------------------------------------*
187 : * Configuration - set SPAR high-level parameters
188 : *-----------------------------------------------------------------*/
189 :
190 1637 : ivas_spar_config_fx( hEncoderConfig->ivas_total_brate, s_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ), &st_ivas->nchan_transport, &st_ivas->nSCE, &st_ivas->nCPE, &hSpar->core_nominal_brate, -1 );
191 :
192 1637 : IF( EQ_16( st_ivas->nchan_transport, 1 ) )
193 : {
194 567 : hEncoderConfig->element_mode_init = IVAS_SCE;
195 : }
196 : ELSE
197 : {
198 1070 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
199 : }
200 1637 : move16();
201 : /*-----------------------------------------------------------------*
202 : * Allocate and initialize Front-VAD handle
203 : *-----------------------------------------------------------------*/
204 :
205 1637 : IF( !spar_reconfig_flag )
206 : {
207 282 : hSpar->front_vad_flag = 0;
208 282 : move16();
209 282 : hSpar->front_vad_dtx_flag = 0;
210 282 : move16();
211 282 : hSpar->force_front_vad = 0;
212 282 : move16();
213 282 : IF( hEncoderConfig->Opt_DTX_ON )
214 : {
215 69 : IF( NE_32( ( error = front_vad_create_fx( &( hSpar->hFrontVad ), hEncoderConfig ) ), IVAS_ERR_OK ) )
216 : {
217 0 : return error;
218 : }
219 :
220 69 : IF( ( hSpar->hCoreCoderVAD = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
221 : {
222 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
223 : }
224 :
225 69 : copy_encoder_config_fx( st_ivas, hSpar->hCoreCoderVAD, 1 );
226 :
227 69 : hSpar->hCoreCoderVAD->total_brate = hEncoderConfig->ivas_total_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
228 69 : hSpar->hCoreCoderVAD->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
229 69 : move32();
230 69 : move32();
231 69 : IF( NE_32( ( error = init_encoder_ivas_fx( hSpar->hCoreCoderVAD, st_ivas, 0, hEncoderConfig->var_SID_rate_flag, hEncoderConfig->interval_SID, 1, st_ivas->ism_mode, hSpar->hCoreCoderVAD->total_brate ) ), IVAS_ERR_OK ) )
232 : {
233 0 : return error;
234 : }
235 : }
236 : ELSE
237 : {
238 213 : hSpar->hCoreCoderVAD = NULL;
239 213 : hSpar->hFrontVad = NULL;
240 : }
241 : }
242 :
243 : /*-----------------------------------------------------------------*
244 : * Final assignment
245 : *-----------------------------------------------------------------*/
246 :
247 1637 : st_ivas->hSpar = hSpar;
248 :
249 1637 : return error;
250 : }
251 : /*-------------------------------------------------------------------------
252 : * ivas_spar_enc_close()
253 : *
254 : * Deallocate SPAR encoder handle
255 : *------------------------------------------------------------------------*/
256 :
257 1983 : void ivas_spar_enc_close_fx(
258 : SPAR_ENC_HANDLE *hSpar, /* i/o: SPAR encoder handle */
259 : const Word32 input_Fs, /* i : input sampling rate */
260 : const Word16 nchan_inp, /* i : number of input channels */
261 : const Word16 spar_reconfig_flag /* i : SPAR reconfiguration flag */
262 : )
263 : {
264 : Word16 num_chans;
265 :
266 1983 : test();
267 1983 : IF( hSpar == NULL || *hSpar == NULL )
268 : {
269 346 : return;
270 : }
271 :
272 1637 : IF( !spar_reconfig_flag )
273 : {
274 : /* core-coder-VAD handle */
275 282 : IF( ( *hSpar )->hCoreCoderVAD != NULL )
276 : {
277 69 : destroy_core_enc_fx( ( *hSpar )->hCoreCoderVAD );
278 69 : ( *hSpar )->hCoreCoderVAD = NULL;
279 : }
280 :
281 : /* front-VAD handle */
282 282 : IF( ( *hSpar )->hFrontVad != NULL )
283 : {
284 69 : front_vad_destroy_fx( &( *hSpar )->hFrontVad );
285 69 : ( *hSpar )->hFrontVad = NULL;
286 : }
287 : }
288 :
289 1637 : num_chans = ( *hSpar )->hFbMixer->fb_cfg->num_in_chans;
290 1637 : move16();
291 1637 : assert( num_chans <= nchan_inp );
292 :
293 : /* MD handle */
294 1637 : ivas_spar_md_enc_close_fx( &( *hSpar )->hMdEnc );
295 :
296 : /* Covar. State handle */
297 1637 : ivas_spar_covar_enc_close_fx( &( *hSpar )->hCovEnc, num_chans );
298 :
299 : /* FB mixer handle */
300 1637 : ivas_FB_mixer_close_fx( &( *hSpar )->hFbMixer, input_Fs, spar_reconfig_flag );
301 :
302 : /* AGC */
303 1637 : ivas_spar_agc_enc_close_fx( &( *hSpar )->hAgcEnc );
304 :
305 : /* PCA */
306 1637 : IF( ( *hSpar )->hPCA != NULL )
307 : {
308 4 : free( ( *hSpar )->hPCA );
309 4 : ( *hSpar )->hPCA = NULL;
310 : }
311 :
312 1637 : IF( !spar_reconfig_flag )
313 : {
314 : /* Trans Det handle */
315 282 : ivas_transient_det_close_fx( &( *hSpar )->hTranDet );
316 282 : free( ( *hSpar ) );
317 282 : ( *hSpar ) = NULL;
318 : }
319 :
320 1637 : return;
321 : }
322 :
323 :
324 : /*-------------------------------------------------------------------*
325 : * ivas_spar_enc()
326 : *
327 : * Principal IVAS SPAR encoder routine
328 : *-------------------------------------------------------------------*/
329 :
330 160500 : ivas_error ivas_spar_enc_fx(
331 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
332 : Word32 *data_fx[], /* i/o: input/transport audio channel Q11 */
333 : const Word16 input_frame, /* i : input frame length */
334 : Word16 *nb_bits_metadata, /* i : number of MD bits written */
335 : BSTR_ENC_HANDLE hMetaData /* o : MetaData handle */
336 : )
337 : {
338 : ENCODER_CONFIG_HANDLE hEncoderConfig;
339 : Encoder_State *st0; /* used for bitstream handling */
340 : ivas_error error;
341 160500 : error = IVAS_ERR_OK;
342 160500 : move32();
343 160500 : hEncoderConfig = st_ivas->hEncoderConfig;
344 :
345 160500 : IF( st_ivas->nSCE > 0 )
346 : {
347 44251 : st0 = st_ivas->hSCE[0]->hCoreCoder[0];
348 : }
349 : ELSE
350 : {
351 116249 : st0 = st_ivas->hCPE[0]->hCoreCoder[0];
352 : }
353 :
354 160500 : test();
355 160500 : test();
356 : /* Write SBA signaling bits */
357 160500 : IF( EQ_32( hEncoderConfig->ivas_format, SBA_FORMAT ) || ( EQ_32( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) ) )
358 : {
359 : /* Write SBA planar flag */
360 145580 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
361 :
362 : /* Write SBA order */
363 145580 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
364 : }
365 : ELSE /* ism_mode == ISM_MODE_NONE */
366 : {
367 14920 : IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
368 : {
369 : /* Write SBA planar flag */
370 2881 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
371 :
372 : /* hack to indicate OSBA bitstream at VLBR */
373 2881 : push_indice( st0->hBstr, IND_SMODE, 0, SBA_ORDER_BITS );
374 :
375 : /* additionally code the real SBA order */
376 2881 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
377 : }
378 : ELSE
379 : {
380 : /* Write SBA planar flag */
381 12039 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
382 : /* Write SBA order */
383 12039 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
384 : }
385 : }
386 :
387 160500 : IF( EQ_32( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
388 : {
389 : /* write the number of objects in ISM_SBA format*/
390 37000 : push_indice( hMetaData, IND_ISM_NUM_OBJECTS, hEncoderConfig->nchan_ism - 1, NO_BITS_MASA_ISM_NO_OBJ );
391 : }
392 : /* front VAD */
393 160500 : IF( NE_32( ( error = front_vad_spar_fx( st_ivas->hSpar, data_fx[0], hEncoderConfig, input_frame ) ), IVAS_ERR_OK ) )
394 : {
395 0 : return error;
396 : }
397 :
398 160500 : IF( hEncoderConfig->sba_planar )
399 : {
400 10250 : ivas_sba_zero_vert_comp_fx( data_fx, st_ivas->sba_analysis_order, hEncoderConfig->sba_planar, input_frame );
401 : }
402 :
403 : /* SPAR encoder */
404 160500 : IF( NE_32( ( error = ivas_spar_enc_process_fx( st_ivas, hEncoderConfig, hMetaData, st_ivas->hSpar->front_vad_flag, data_fx, st_ivas->q_data_fx ) ), IVAS_ERR_OK ) )
405 : {
406 0 : return error;
407 : }
408 :
409 160500 : *nb_bits_metadata = hMetaData->nb_bits_tot;
410 160500 : move16();
411 :
412 160500 : test();
413 : /* Force IVAS front pre-proc decision for higher bitrates */
414 160500 : if ( GT_32( hEncoderConfig->ivas_total_brate, SBA_DTX_BITRATE_THRESHOLD ) || hEncoderConfig->Opt_DTX_ON == 0 )
415 : {
416 142710 : st_ivas->hSpar->front_vad_flag = 0;
417 142710 : move16();
418 : }
419 :
420 160500 : return error;
421 : }
422 :
423 :
424 : /*-------------------------------------------------------------------*
425 : * ivas_spar_cov_md_process()
426 : *
427 : * Process call for SPAR covariance and MD encoder
428 : *-------------------------------------------------------------------*/
429 :
430 160500 : static ivas_error ivas_spar_cov_md_process_fx(
431 : const ENCODER_CONFIG_HANDLE hEncoderConfig,
432 : SPAR_ENC_HANDLE hSpar,
433 : const IVAS_QMETADATA_HANDLE hQMetaData,
434 : BSTR_ENC_HANDLE hMetaData,
435 : const Word16 nchan_inp,
436 : const Word16 sba_order,
437 : Word32 *ppIn_FR_real_fx[IVAS_SPAR_MAX_CH], // q_ppIn_FR
438 : Word32 *ppIn_FR_imag_fx[IVAS_SPAR_MAX_CH], // q_ppIn_FR
439 : const Word16 transient_det[2],
440 : const Word16 dtx_vad,
441 : const Word16 nchan_transport,
442 : Word16 *dyn_active_w_flag,
443 : Word16 q_ppIn_FR )
444 : {
445 : Word16 i, j, i_ts, b, table_idx;
446 : Word16 active_w_vlbr;
447 : /* note: the actual dimensions of matrixes correspond to num_channels = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate ); */
448 : Word32 *cov_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
449 : Word32 *cov_dtx_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
450 : Word16 *q_cov_real[IVAS_SPAR_MAX_CH];
451 : Word16 *q_cov_dtx_real[IVAS_SPAR_MAX_CH];
452 : Word32 cov_real_buf_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
453 : Word32 cov_dtx_real_buf_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
454 : ivas_error error;
455 :
456 160500 : error = IVAS_ERR_OK;
457 160500 : move32();
458 :
459 160500 : IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
460 : {
461 13553 : active_w_vlbr = 1;
462 13553 : move16();
463 : }
464 : ELSE
465 : {
466 146947 : active_w_vlbr = 0;
467 146947 : move16();
468 : }
469 :
470 : /*-----------------------------------------------------------------------------------------*
471 : * Set SPAR bitrates
472 : *-----------------------------------------------------------------------------------------*/
473 :
474 160500 : table_idx = ivas_get_spar_table_idx_fx( hEncoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
475 :
476 160500 : IF( NE_16( hSpar->hMdEnc->table_idx, table_idx ) )
477 : {
478 1955 : hSpar->hMdEnc->table_idx = table_idx;
479 1955 : move16();
480 1955 : test();
481 1955 : IF( NE_32( hEncoderConfig->ivas_total_brate, hEncoderConfig->last_ivas_total_brate ) && !hSpar->spar_reconfig_flag )
482 : {
483 318 : IF( NE_32( ( error = ivas_spar_md_enc_init_fx( hSpar->hMdEnc, hEncoderConfig, sba_order ) ), IVAS_ERR_OK ) )
484 : {
485 0 : return error;
486 : }
487 : }
488 : ELSE
489 : {
490 1637 : Word16 num_bands = SPAR_DIRAC_SPLIT_START_BAND;
491 1637 : move16();
492 1637 : if ( hSpar->hMdEnc->spar_hoa_md_flag != 0 )
493 : {
494 259 : num_bands = IVAS_MAX_NUM_BANDS;
495 259 : move16();
496 : }
497 1637 : ivas_spar_set_bitrate_config_fx( &hSpar->hMdEnc->spar_md_cfg, table_idx, num_bands, hSpar->hMdEnc->spar_hoa_dirac2spar_md_flag, 1, hEncoderConfig->Opt_PCA_ON, hSpar->AGC_Enable );
498 : }
499 : }
500 :
501 : /*-----------------------------------------------------------------------------------------*
502 : * Covariance process
503 : *-----------------------------------------------------------------------------------------*/
504 904700 : FOR( i = 0; i < nchan_inp; i++ )
505 : {
506 4688660 : FOR( j = 0; j < nchan_inp; j++ )
507 : {
508 3944460 : cov_real_fx[i][j] = cov_real_buf_fx[i][j];
509 3944460 : cov_dtx_real_fx[i][j] = cov_dtx_real_buf_fx[i][j];
510 4368460 : FOR( b = hSpar->hFbMixer->pFb->filterbank_num_bands; b < IVAS_MAX_NUM_BANDS; b++ )
511 : {
512 424000 : cov_real_fx[i][j][b] = 0;
513 424000 : move32();
514 424000 : cov_dtx_real_fx[i][j][b] = 0;
515 424000 : move32();
516 : }
517 : }
518 744200 : IF( ( q_cov_real[i] = (Word16 *) malloc( sizeof( Word16 ) * nchan_inp ) ) == NULL )
519 : {
520 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
521 : }
522 744200 : set16_fx( q_cov_real[i], Q31, nchan_inp );
523 744200 : IF( ( q_cov_dtx_real[i] = (Word16 *) malloc( sizeof( Word16 ) * nchan_inp ) ) == NULL )
524 : {
525 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
526 : }
527 744200 : set16_fx( q_cov_dtx_real[i], Q31, nchan_inp );
528 : }
529 :
530 160500 : ivas_enc_cov_handler_process_fx( hSpar->hCovEnc, ppIn_FR_real_fx, ppIn_FR_imag_fx, q_ppIn_FR, cov_real_fx, q_cov_real, cov_dtx_real_fx, q_cov_dtx_real, hSpar->hFbMixer->pFb, 0, hSpar->hFbMixer->pFb->filterbank_num_bands, nchan_inp, dtx_vad, transient_det, hSpar->hMdEnc->HOA_md_ind,
531 160500 : &hSpar->hMdEnc->spar_md.res_ind, remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order], dyn_active_w_flag, nchan_transport, 1 );
532 :
533 904700 : FOR( i = 0; i < nchan_inp; i++ )
534 : {
535 744200 : free( q_cov_real[i] );
536 744200 : q_cov_real[i] = NULL;
537 744200 : free( q_cov_dtx_real[i] );
538 744200 : q_cov_dtx_real[i] = NULL;
539 : }
540 160500 : test();
541 160500 : IF( GT_16( nchan_transport, 1 ) && LE_16( nchan_transport, ( FOA_CHANNELS - 1 ) ) )
542 : {
543 75079 : push_next_indice( hMetaData, *dyn_active_w_flag, 1 );
544 75079 : test();
545 75079 : IF( EQ_16( *dyn_active_w_flag, 1 ) && EQ_16( nchan_transport, 2 ) )
546 : {
547 0 : push_next_indice( hMetaData, sub( hSpar->hMdEnc->spar_md.res_ind, nchan_transport ), 1 );
548 : }
549 75079 : IF( EQ_16( *dyn_active_w_flag, 1 ) )
550 : {
551 0 : hSpar->front_vad_flag = 1;
552 0 : move16();
553 : }
554 : ELSE
555 : {
556 75079 : hSpar->front_vad_flag = hSpar->front_vad_flag;
557 75079 : move16();
558 : }
559 : }
560 : ELSE
561 : {
562 85421 : IF( EQ_16( nchan_transport, FOA_CHANNELS ) )
563 : {
564 41170 : push_next_indice( hMetaData, 0, 1 );
565 : }
566 : }
567 :
568 : /*-----------------------------------------------------------------------------------------*
569 : * MetaData encoder
570 : *-----------------------------------------------------------------------------------------*/
571 :
572 160500 : IF( hSpar->hMdEnc->spar_hoa_md_flag == 0 )
573 : {
574 138460 : IF( NE_32( ( error = ivas_spar_md_enc_process_fx( hSpar->hMdEnc, hEncoderConfig, cov_real_fx, hSpar->hCovEnc->pCov_state->q_cov_real_per_band, cov_dtx_real_fx, hSpar->hCovEnc->pCov_dtx_state->q_cov_real_per_band, hMetaData, dtx_vad, nchan_inp, sba_order, hSpar->hFbMixer->prior_mixer_fx, &hSpar->hFbMixer->q_prior_mixer_fx, *dyn_active_w_flag, hQMetaData->dirac_mono_flag, hSpar->hFbMixer->fb_cfg->num_out_chans ) ), IVAS_ERR_OK ) )
575 : {
576 0 : return error;
577 : }
578 : }
579 :
580 160500 : IF( hSpar->hMdEnc->spar_hoa_dirac2spar_md_flag )
581 : {
582 : Word16 order;
583 : Word32 azi_dirac_fx[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
584 : Word32 ele_dirac_fx[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
585 : Word32 diffuseness_fx[IVAS_MAX_NUM_BANDS];
586 : Word32 Wscale_d_fx[IVAS_MAX_NUM_BANDS];
587 : Word16 d_start_band, d_end_band;
588 : Word16 dirac_band_idx;
589 :
590 150560 : d_start_band = hSpar->enc_param_start_band;
591 150560 : move16();
592 150560 : d_end_band = IVAS_MAX_NUM_BANDS;
593 150560 : move16();
594 :
595 803200 : FOR( b = d_start_band; b < d_end_band; b++ )
596 : {
597 652640 : dirac_band_idx = sub( hSpar->dirac_to_spar_md_bands[b], d_start_band );
598 3100564 : FOR( i_ts = 0; i_ts < hQMetaData->q_direction->cfg.nblocks; i_ts++ )
599 : {
600 2447924 : azi_dirac_fx[b][i_ts] = hQMetaData->q_direction->band_data[dirac_band_idx].azimuth_fx[i_ts];
601 2447924 : move32();
602 2447924 : ele_dirac_fx[b][i_ts] = hQMetaData->q_direction->band_data[dirac_band_idx].elevation_fx[i_ts];
603 2447924 : move32();
604 : }
605 652640 : diffuseness_fx[b] = L_sub( ONE_IN_Q30, hQMetaData->q_direction->band_data[dirac_band_idx].energy_ratio_fx[0] );
606 652640 : move32();
607 : }
608 150560 : test();
609 150560 : IF( GE_16( d_start_band, 6 ) && EQ_16( dtx_vad, 1 ) )
610 : {
611 142655 : Copy32( hSpar->hMdEnc->spar_md.band_coeffs[d_start_band - 1].P_quant_re_fx, hSpar->hMdEnc->spar_md.band_coeffs[d_start_band - 1].P_re_fx, IVAS_SPAR_MAX_CH - 1 );
612 : }
613 :
614 803200 : FOR( b = d_start_band; b < d_end_band; b++ )
615 : {
616 652640 : Word16 tmp_e, Wscale_d_e = 0;
617 652640 : move16();
618 652640 : Wscale_d_fx[b] = ONE_IN_Q31;
619 652640 : move32();
620 2891120 : FOR( i = 1; i < nchan_inp; i++ )
621 : {
622 2238480 : Word32 tmp = 0;
623 2238480 : move32();
624 2238480 : tmp_e = 0;
625 2238480 : move16();
626 2238480 : IF( cov_real_fx[0][0][b] != 0 )
627 : {
628 2128880 : tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( cov_real_fx[i][i][b], cov_real_fx[0][0][b], &tmp_e ) );
629 2128880 : tmp_e = add( tmp_e, sub( sub( Q31, hSpar->hCovEnc->pCov_state->q_cov_real_per_band[i][i][b] ), sub( Q31, hSpar->hCovEnc->pCov_state->q_cov_real_per_band[0][0][b] ) ) );
630 : }
631 2238480 : Wscale_d_fx[b] = BASOP_Util_Add_Mant32Exp( tmp, tmp_e, Wscale_d_fx[b], Wscale_d_e, &Wscale_d_e );
632 2238480 : move32();
633 : }
634 652640 : Wscale_d_fx[b] = L_deposit_h( BASOP_Util_Divide3232_Scale( Wscale_d_fx[b], L_add( 1, sba_order ), &tmp_e ) );
635 652640 : move32();
636 652640 : tmp_e = add( tmp_e, sub( Wscale_d_e, Q31 ) );
637 652640 : Wscale_d_fx[b] = Sqrt32( Wscale_d_fx[b], &tmp_e );
638 652640 : move32();
639 652640 : Wscale_d_fx[b] = L_shl_sat( Wscale_d_fx[b], sub( tmp_e, Q2 ) ); // saturating to Q29, as final value is limited to 2 below
640 652640 : move32();
641 652640 : Wscale_d_fx[b] = L_min( ONE_IN_Q30, L_max( Wscale_d_fx[b], ONE_IN_Q29 ) ); // Q29
642 652640 : move32();
643 : }
644 :
645 150560 : IF( hSpar->hMdEnc->spar_hoa_md_flag == 0 )
646 : {
647 138460 : order = sba_order;
648 138460 : move16();
649 : }
650 : ELSE
651 : {
652 12100 : order = 1;
653 12100 : move16();
654 : }
655 150560 : ivas_get_spar_md_from_dirac_enc_fx( azi_dirac_fx, ele_dirac_fx, diffuseness_fx, 1, hSpar->hMdEnc->mixer_mat_fx, &hSpar->hMdEnc->q_mixer_mat_fx, &hSpar->hMdEnc->spar_md, &hSpar->hMdEnc->spar_md_cfg, d_start_band, d_end_band, order, dtx_vad, Wscale_d_fx, hQMetaData->useLowerRes, active_w_vlbr, *dyn_active_w_flag );
656 : }
657 :
658 160500 : IF( hSpar->hMdEnc->spar_hoa_md_flag )
659 : {
660 22040 : error = ivas_spar_md_enc_process_fx( hSpar->hMdEnc, hEncoderConfig, cov_real_fx, hSpar->hCovEnc->pCov_state->q_cov_real_per_band, cov_dtx_real_fx, hSpar->hCovEnc->pCov_dtx_state->q_cov_real_per_band, hMetaData, dtx_vad, nchan_inp, sba_order, hSpar->hFbMixer->prior_mixer_fx, &hSpar->hFbMixer->q_prior_mixer_fx, *dyn_active_w_flag, hQMetaData->dirac_mono_flag, hSpar->hFbMixer->fb_cfg->num_out_chans );
661 : }
662 :
663 160500 : return error;
664 : }
665 :
666 :
667 : /*-----------------------------------------------------------------------------------------*
668 : * Function ivas_spar_enc_process()
669 : *
670 : * Process call for SPAR encoder
671 : *-----------------------------------------------------------------------------------------*/
672 :
673 160500 : static ivas_error ivas_spar_enc_process_fx(
674 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
675 : const ENCODER_CONFIG_HANDLE hEncoderConfig, /* i : configuration structure */
676 : BSTR_ENC_HANDLE hMetaData, /* i/o: MetaData handle */
677 : const Word16 front_vad_flag, /* i : front-VAD decision */
678 : Word32 *data_fx[], /* i/o: input/transport audio channels Q(q_data) */
679 : const Word16 q_data )
680 : {
681 : Word32 pcm_tmp_fx[DIRAC_MAX_ANA_CHANS][L_FRAME48k * 2];
682 : Word32 *p_pcm_tmp_fx[DIRAC_MAX_ANA_CHANS];
683 : Word16 q_pcm_fx[DIRAC_MAX_ANA_CHANS];
684 : Word16 q_p_pcm_tmp_fx[DIRAC_MAX_ANA_CHANS];
685 : Word16 i, j, input_frame, dtx_vad;
686 : Word16 transient_det[2];
687 : Word16 hodirac_flag;
688 : Word32 ivas_total_brate, input_Fs;
689 : Word16 nchan_inp, sba_order, nchan_transport;
690 : Word16 in_out_mixer_map[IVAS_MAX_FB_MIXER_OUT_CH][IVAS_MAX_SPAR_FB_MIXER_IN_CH];
691 : ivas_error error;
692 : const Word16 *order;
693 160500 : SPAR_ENC_HANDLE hSpar = st_ivas->hSpar;
694 160500 : IVAS_QMETADATA_HANDLE hQMetaData = st_ivas->hQMetaData;
695 : Word16 ts, l_ts, num_del_samples, b, i_ts;
696 :
697 : Word32 *ppIn_FR_real_fx[IVAS_SPAR_MAX_CH], *ppIn_FR_imag_fx[IVAS_SPAR_MAX_CH];
698 : Word32 wyzx_del_buf[FOA_CHANNELS][IVAS_FB_1MS_48K_SAMP];
699 : Word16 dyn_active_w_flag;
700 : Word16 nchan_fb_in;
701 :
702 160500 : push_wmops( "ivas_spar_enc_process" );
703 :
704 : /*-----------------------------------------------------------------------------------------*
705 : * Initialization
706 : *-----------------------------------------------------------------------------------------*/
707 :
708 160500 : error = IVAS_ERR_OK;
709 160500 : move32();
710 :
711 160500 : input_Fs = hEncoderConfig->input_Fs;
712 160500 : move32();
713 160500 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
714 160500 : move32();
715 160500 : num_del_samples = hSpar->hFbMixer->fb_cfg->fb_latency;
716 160500 : move16();
717 :
718 160500 : input_frame = extract_l( Mpy_32_32_r( input_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) );
719 :
720 160500 : sba_order = s_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
721 160500 : nchan_inp = ivas_sba_get_nchan_metadata_fx( sba_order, hEncoderConfig->ivas_total_brate );
722 160500 : assert( nchan_inp <= hEncoderConfig->nchan_inp );
723 :
724 160500 : nchan_fb_in = hSpar->hFbMixer->fb_cfg->nchan_fb_in;
725 160500 : move16();
726 160500 : nchan_transport = st_ivas->nchan_transport;
727 160500 : move16();
728 :
729 276960 : FOR( i = FOA_CHANNELS + 1; i < nchan_fb_in; i++ )
730 : {
731 116460 : Copy32( data_fx[HOA_keep_ind[i]], data_fx[i], input_frame );
732 : }
733 :
734 : /*-----------------------------------------------------------------------------------------*
735 : * Transient detector
736 : *-----------------------------------------------------------------------------------------*/
737 :
738 160500 : ivas_transient_det_process_fx( hSpar->hTranDet, data_fx[0], input_frame, transient_det );
739 :
740 : #ifdef DEBUGGING
741 : {
742 : float a[2];
743 : a[0] = transient_det[0];
744 : a[1] = transient_det[1];
745 : dbgwrite_txt( a, 2, "fixed.txt", NULL );
746 : }
747 : #endif
748 160500 : if ( LT_32( ivas_total_brate, IVAS_24k4 ) )
749 : {
750 13553 : transient_det[1] = 0;
751 13553 : move16();
752 : }
753 :
754 : /* store previous input samples for W in local buffer */
755 160500 : assert( num_del_samples <= IVAS_FB_1MS_48K_SAMP );
756 160500 : IF( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 )
757 : {
758 : /* fill delay (1 ms) buffer for all Transport channels */
759 581245 : FOR( i = 0; i < FOA_CHANNELS; i++ )
760 : {
761 464996 : Word16 idx = hSpar->hFbMixer->fb_cfg->remix_order[i];
762 464996 : move16();
763 464996 : Copy32( &hSpar->hFbMixer->ppFilterbank_prior_input_fx[idx][hSpar->hFbMixer->fb_cfg->prior_input_length - num_del_samples], wyzx_del_buf[idx], num_del_samples );
764 : }
765 : }
766 :
767 : /*-----------------------------------------------------------------------------------------*
768 : * FB mixer ingest
769 : *-----------------------------------------------------------------------------------------*/
770 941000 : FOR( i = 0; i < nchan_fb_in; i++ )
771 : {
772 780500 : p_pcm_tmp_fx[i] = pcm_tmp_fx[i];
773 : }
774 :
775 : /* run Filter Bank overlapping MDFT analysis first, then we can use the temporary buffer for Parameter MDFT analysis*/
776 160500 : ivas_fb_mixer_pcm_ingest_fx( hSpar->hFbMixer, data_fx, p_pcm_tmp_fx, input_frame, hSpar->hMdEnc->HOA_md_ind, q_data, q_p_pcm_tmp_fx );
777 904700 : FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_in_chans; i++ )
778 : {
779 : /* changing q format to q_data*/
780 744200 : scale_sig32( p_pcm_tmp_fx[i], shl( input_frame, 1 ), sub( q_data, q_p_pcm_tmp_fx[i] ) ); // q_data
781 : }
782 :
783 : /* prepare Parameter MDFT analysis */
784 160500 : Word16 shift = Q31;
785 160500 : move16();
786 941000 : FOR( i = 0; i < nchan_fb_in; i++ )
787 : {
788 780500 : ppIn_FR_real_fx[i] = p_pcm_tmp_fx[i];
789 780500 : ppIn_FR_imag_fx[i] = p_pcm_tmp_fx[i] + input_frame;
790 780500 : p_pcm_tmp_fx[i] = data_fx[i];
791 780500 : shift = s_min( shift, L_norm_arr( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length ) );
792 780500 : shift = s_min( shift, L_norm_arr( data_fx[i], input_frame ) );
793 : }
794 :
795 160500 : l_ts = shr( input_frame, MAX_PARAM_SPATIAL_SUBFRAMES_LOG2 );
796 :
797 160500 : Word16 gb = find_guarded_bits_fx( l_ts );
798 802500 : FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
799 : {
800 3764000 : FOR( i = 0; i < nchan_fb_in; i++ )
801 : {
802 3122000 : scale_sig32( data_fx[i], input_frame, shift ); // Q:q_data+shift
803 3122000 : scale_sig32( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length, shift ); // Q:q_data+shift
804 : }
805 642000 : ivas_fb_mixer_get_windowed_fr_fx( hSpar->hFbMixer, p_pcm_tmp_fx, ppIn_FR_real_fx, ppIn_FR_imag_fx, l_ts, l_ts, nchan_fb_in, gb );
806 3764000 : FOR( i = 0; i < nchan_fb_in; i++ )
807 : {
808 3122000 : scale_sig32( data_fx[i], input_frame, negate( shift ) ); // Q:q_data
809 3122000 : scale_sig32( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length, negate( shift ) ); // Q:q_data
810 : }
811 642000 : ivas_fb_mixer_update_prior_input_fx( hSpar->hFbMixer, p_pcm_tmp_fx, l_ts, nchan_fb_in );
812 :
813 3764000 : FOR( i = 0; i < nchan_fb_in; i++ )
814 : {
815 3122000 : p_pcm_tmp_fx[i] += l_ts;
816 3122000 : ppIn_FR_real_fx[i] += l_ts;
817 3122000 : ppIn_FR_imag_fx[i] += l_ts;
818 3122000 : q_p_pcm_tmp_fx[i] = sub( q_data, gb ); // updating the q for p_pcm
819 3122000 : move16();
820 : }
821 : }
822 :
823 : /* turn pointers back to the local buffer, needed for the following processing */
824 941000 : FOR( i = 0; i < nchan_fb_in; i++ )
825 : {
826 780500 : ppIn_FR_real_fx[i] = pcm_tmp_fx[i];
827 780500 : ppIn_FR_imag_fx[i] = pcm_tmp_fx[i] + input_frame;
828 780500 : p_pcm_tmp_fx[i] = pcm_tmp_fx[i];
829 : }
830 160500 : IF( EQ_16( hEncoderConfig->Opt_DTX_ON, 1 ) )
831 : {
832 18000 : dtx_vad = front_vad_flag;
833 18000 : move16();
834 : }
835 : ELSE
836 : {
837 142500 : dtx_vad = 1;
838 142500 : move16();
839 : }
840 :
841 : /*-----------------------------------------------------------------------------------------*
842 : * DirAC encoding
843 : *-----------------------------------------------------------------------------------------*/
844 :
845 160500 : hodirac_flag = ivas_get_hodirac_flag_fx( ivas_total_brate, st_ivas->sba_analysis_order );
846 :
847 : /*Finding max possible q*/
848 : Word16 pp_fr_q;
849 160500 : pp_fr_q = sub( add( q_data, shift ), gb );
850 160500 : Word16 re_q = MAX_16, im_q = MAX_16, FR_q;
851 160500 : move16();
852 160500 : move16();
853 941000 : FOR( i = 0; i < nchan_fb_in; i++ )
854 : {
855 780500 : re_q = s_min( re_q, L_norm_arr( ppIn_FR_real_fx[i], input_frame ) );
856 780500 : im_q = s_min( im_q, L_norm_arr( ppIn_FR_imag_fx[i], input_frame ) );
857 : }
858 160500 : FR_q = s_min( re_q, im_q );
859 941000 : FOR( i = 0; i < nchan_fb_in; i++ )
860 : {
861 780500 : scale_sig32( ppIn_FR_real_fx[i], input_frame, FR_q ); // Q(FR_q+pp_fr_q)
862 780500 : scale_sig32( ppIn_FR_imag_fx[i], input_frame, FR_q ); // Q(FR_q+pp_fr_q)
863 : }
864 160500 : pp_fr_q = add( FR_q, pp_fr_q );
865 :
866 160500 : IF( NE_32( ( error = ivas_dirac_enc_fx( st_ivas->hDirAC, hQMetaData, hMetaData, data_fx, ppIn_FR_real_fx, ppIn_FR_imag_fx, pp_fr_q, input_frame, dtx_vad, hEncoderConfig->ivas_format, nchan_transport, hodirac_flag, shift ) ), IVAS_ERR_OK ) )
867 : {
868 0 : return error;
869 : }
870 :
871 : /* Set Energy Ratio to 0.0 if the mono flag has been set */
872 160500 : IF( hQMetaData->dirac_mono_flag )
873 : {
874 256 : FOR( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
875 : {
876 192 : Word16 loop_count = 1;
877 192 : move16();
878 192 : IF( EQ_16( dtx_vad, 1 ) )
879 : {
880 192 : loop_count = hQMetaData->q_direction[0].cfg.nblocks;
881 192 : move16();
882 : }
883 960 : FOR( i_ts = 0; i_ts < loop_count; i_ts++ )
884 : {
885 768 : hQMetaData->q_direction[0].band_data[b].energy_ratio_fx[i_ts] = 0;
886 768 : move32();
887 : }
888 : }
889 : }
890 :
891 : /*-----------------------------------------------------------------------------------------*
892 : * Covariance and MD processing
893 : *-----------------------------------------------------------------------------------------*/
894 :
895 160500 : IF( NE_32( ( error = ivas_spar_cov_md_process_fx( hEncoderConfig, st_ivas->hSpar, st_ivas->hQMetaData, hMetaData, nchan_inp, sba_order, ppIn_FR_real_fx, ppIn_FR_imag_fx, transient_det, dtx_vad, nchan_transport, &dyn_active_w_flag, pp_fr_q ) ), IVAS_ERR_OK ) )
896 : {
897 0 : return error;
898 : }
899 :
900 160500 : set16_fx( q_p_pcm_tmp_fx, pp_fr_q, nchan_fb_in );
901 :
902 : /*-----------------------------------------------------------------------------------------*
903 : * FB mixer
904 : *-----------------------------------------------------------------------------------------*/
905 :
906 160500 : ivas_fb_mixer_get_in_out_mapping_fx( hSpar->hFbMixer->fb_cfg, in_out_mixer_map );
907 :
908 : #ifdef DEBUG_SPAR_DIRAC_WRITE_OUT_PRED_PARS
909 : {
910 : static FILE *fid = 0;
911 : int16_t band = 9;
912 : if ( !fid )
913 : {
914 : fid = fopen( "pred_coeffs_enc.txt", "wt" );
915 : }
916 : fprintf( fid, "%.6f\n", hSpar->hMdEnc->mixer_mat[1][0][band] );
917 : }
918 : #endif
919 :
920 160500 : ivas_fb_mixer_process_fx( hSpar->hFbMixer, hSpar->hMdEnc->mixer_mat_fx, &hSpar->hMdEnc->q_mixer_mat_fx, p_pcm_tmp_fx, q_p_pcm_tmp_fx, input_frame, in_out_mixer_map );
921 :
922 437079 : FOR( Word32 ch = ( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 ); ch < hSpar->hFbMixer->fb_cfg->num_out_chans; ch++ )
923 : {
924 : /* changing q format to q_data*/
925 276579 : scale_sig32( p_pcm_tmp_fx[ch], input_frame, sub( q_data, q_p_pcm_tmp_fx[ch] ) ); // q_data
926 276579 : q_p_pcm_tmp_fx[ch] = q_data;
927 276579 : move16();
928 : }
929 160500 : IF( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 )
930 : {
931 : /*cross fade between new active W channels and old passive W channel*/
932 116249 : IF( EQ_16( dyn_active_w_flag, 1 ) )
933 : {
934 0 : IF( NE_16( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag, dyn_active_w_flag ) )
935 : {
936 : Word32 new_w[L_FRAME48k];
937 : /* delayed W */
938 0 : Copy32( wyzx_del_buf[0], p_pcm_tmp_fx[0], num_del_samples );
939 0 : Copy32( data_fx[0], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
940 :
941 0 : Copy32( wyzx_del_buf[hSpar->hMdEnc->spar_md.res_ind], new_w, num_del_samples );
942 0 : Copy32( data_fx[hSpar->hMdEnc->spar_md.res_ind], &new_w[num_del_samples], sub( input_frame, num_del_samples ) );
943 :
944 0 : test();
945 0 : test();
946 0 : IF( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag == 0 && EQ_16( dyn_active_w_flag, 1 ) )
947 : {
948 0 : ivas_fb_mixer_cross_fading_fx( hSpar->hFbMixer, p_pcm_tmp_fx, p_pcm_tmp_fx[0], new_w, 0, input_frame, 0 );
949 : }
950 0 : ELSE IF( EQ_16( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag, 1 ) && dyn_active_w_flag == 0 )
951 : {
952 0 : ivas_fb_mixer_cross_fading_fx( hSpar->hFbMixer, p_pcm_tmp_fx, new_w, p_pcm_tmp_fx[0], 0, input_frame, 0 );
953 : }
954 : }
955 : ELSE
956 : {
957 0 : Copy32( wyzx_del_buf[hSpar->hMdEnc->spar_md.res_ind], p_pcm_tmp_fx[0], num_del_samples );
958 0 : Copy32( data_fx[hSpar->hMdEnc->spar_md.res_ind], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
959 : }
960 : }
961 : ELSE
962 : {
963 : /* delayed W */
964 116249 : Copy32( wyzx_del_buf[0], p_pcm_tmp_fx[0], num_del_samples );
965 116249 : Copy32( data_fx[0], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
966 : }
967 :
968 348577 : FOR( i = 1; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
969 : {
970 232328 : Word16 idx = hSpar->hFbMixer->fb_cfg->remix_order[i];
971 232328 : move16();
972 :
973 : /* delayed, reorderd and accumulated with (negative) prediction from W */
974 232328 : v_add_fx( wyzx_del_buf[idx], p_pcm_tmp_fx[i], p_pcm_tmp_fx[i], num_del_samples );
975 232328 : v_add_fx( data_fx[idx], p_pcm_tmp_fx[i] + num_del_samples, p_pcm_tmp_fx[i] + num_del_samples, sub( input_frame, num_del_samples ) );
976 : }
977 : }
978 :
979 : /*-----------------------------------------------------------------------------------------*
980 : * PCA encoder
981 : *-----------------------------------------------------------------------------------------*/
982 :
983 160500 : IF( hSpar->hPCA != NULL )
984 : {
985 1750 : ivas_pca_enc_fx( hEncoderConfig, hSpar->hPCA, hMetaData, p_pcm_tmp_fx, input_frame, FOA_CHANNELS );
986 : }
987 : ELSE
988 : {
989 158750 : test();
990 158750 : IF( EQ_32( ivas_total_brate, PCA_BRATE ) && EQ_16( sba_order, SBA_FOA_ORDER ) )
991 : {
992 : /* write PCA bypass bit */
993 9870 : push_next_indice( hMetaData, PCA_MODE_INACTIVE, 1 );
994 : }
995 : }
996 :
997 : /*-----------------------------------------------------------------------------------------*
998 : * AGC
999 : *-----------------------------------------------------------------------------------------*/
1000 :
1001 160500 : IF( EQ_16( dtx_vad, 1 ) )
1002 : {
1003 158895 : IF( hSpar->AGC_Enable != 0 )
1004 : {
1005 86818 : FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
1006 : {
1007 43409 : q_pcm_fx[i] = q_data;
1008 43409 : move16();
1009 : }
1010 :
1011 43409 : ivas_agc_enc_process_fx( hSpar->hAgcEnc, hMetaData, p_pcm_tmp_fx, p_pcm_tmp_fx, hSpar->hFbMixer->fb_cfg->num_out_chans, hEncoderConfig, q_pcm_fx );
1012 :
1013 86818 : FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
1014 : {
1015 43409 : scale_sig32( p_pcm_tmp_fx[i], input_frame, sub( q_data, q_pcm_fx[i] ) ); // q_data
1016 43409 : q_pcm_fx[i] = q_data;
1017 43409 : move16();
1018 : }
1019 : }
1020 : }
1021 :
1022 :
1023 : /*-----------------------------------------------------------------------------------------*
1024 : * Re-order the dmx back to ACN/SN3D format
1025 : *-----------------------------------------------------------------------------------------*/
1026 :
1027 160500 : order = remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order];
1028 160500 : move16();
1029 :
1030 553328 : FOR( j = 0; j < nchan_transport; j++ )
1031 : {
1032 392828 : IF( LT_16( st_ivas->nchan_transport, 3 ) )
1033 : {
1034 126931 : Copy32( p_pcm_tmp_fx[j], data_fx[j], input_frame );
1035 : }
1036 : ELSE
1037 : {
1038 265897 : Copy32( p_pcm_tmp_fx[j], data_fx[order[j]], input_frame );
1039 : }
1040 : }
1041 409672 : FOR( ; j < IVAS_SPAR_MAX_DMX_CHS; j++ )
1042 : {
1043 249172 : set32_fx( data_fx[order[j]], 0, input_frame );
1044 : }
1045 :
1046 160500 : hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag = dyn_active_w_flag;
1047 160500 : move16();
1048 :
1049 160500 : pop_wmops();
1050 :
1051 160500 : return error;
1052 : }
|