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 1558 : 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 1558 : hEncoderConfig = st_ivas->hEncoderConfig;
74 1558 : error = IVAS_ERR_OK;
75 1558 : move32();
76 1558 : hSpar = st_ivas->hSpar;
77 :
78 1558 : IF( !spar_reconfig_flag )
79 : {
80 : /* SPAR encoder handle */
81 279 : 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 1558 : hSpar->spar_reconfig_flag = 0;
88 1558 : move16();
89 1558 : input_Fs = hEncoderConfig->input_Fs;
90 1558 : move32();
91 1558 : sba_order_internal = extract_l( L_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ) );
92 1558 : nchan_inp = ivas_sba_get_nchan_metadata_fx( sba_order_internal, hEncoderConfig->ivas_total_brate );
93 1558 : assert( nchan_inp <= hEncoderConfig->nchan_inp );
94 1558 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
95 1558 : move32();
96 1558 : nchan_fb_in = 0;
97 1558 : move16();
98 1558 : IF( EQ_16( st_ivas->sba_analysis_order, 1 ) )
99 : {
100 1299 : nchan_fb_in = FOA_CHANNELS;
101 1299 : 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 1558 : nchan_transport = ivas_get_sba_num_TCs_fx( hEncoderConfig->ivas_total_brate, sba_order_internal );
119 :
120 1558 : table_idx = ivas_get_spar_table_idx_fx( ivas_total_brate, sba_order_internal, SPAR_CONFIG_BW, NULL, NULL );
121 :
122 : /* MD handle */
123 1558 : 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 1558 : active_w_mixing = ivas_spar_br_table_consts[table_idx].active_w;
130 1558 : move16();
131 1558 : 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 1558 : fb_cfg->remix_order = remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order];
136 :
137 : /* FB mixer handle */
138 1558 : 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 1558 : 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 1558 : IF( !spar_reconfig_flag )
149 : {
150 : /* Transient Detector handle */
151 279 : 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 1558 : hSpar->hMdEnc->table_idx = -1;
159 1558 : move16();
160 : /* AGC handle */
161 1558 : hSpar->AGC_Enable = ivas_agc_enc_get_flag( nchan_transport );
162 1558 : move16();
163 1558 : hSpar->hAgcEnc = NULL;
164 1558 : IF( hSpar->AGC_Enable )
165 : {
166 540 : 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 1558 : hSpar->hPCA = NULL;
174 1558 : 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 1558 : hSpar->hMdEnc->table_idx = -1;
185 1558 : move16();
186 : /*-----------------------------------------------------------------*
187 : * Configuration - set SPAR high-level parameters
188 : *-----------------------------------------------------------------*/
189 :
190 1558 : 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 1558 : IF( EQ_16( st_ivas->nchan_transport, 1 ) )
193 : {
194 540 : hEncoderConfig->element_mode_init = IVAS_SCE;
195 : }
196 : ELSE
197 : {
198 1018 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
199 : }
200 1558 : move16();
201 : /*-----------------------------------------------------------------*
202 : * Allocate and initialize Front-VAD handle
203 : *-----------------------------------------------------------------*/
204 :
205 1558 : IF( !spar_reconfig_flag )
206 : {
207 279 : hSpar->front_vad_flag = 0;
208 279 : move16();
209 279 : hSpar->front_vad_dtx_flag = 0;
210 279 : move16();
211 279 : hSpar->force_front_vad = 0;
212 279 : move16();
213 279 : 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 210 : hSpar->hCoreCoderVAD = NULL;
239 210 : hSpar->hFrontVad = NULL;
240 : }
241 : }
242 :
243 : /*-----------------------------------------------------------------*
244 : * Final assignment
245 : *-----------------------------------------------------------------*/
246 :
247 1558 : st_ivas->hSpar = hSpar;
248 :
249 1558 : return error;
250 : }
251 : /*-------------------------------------------------------------------------
252 : * ivas_spar_enc_close()
253 : *
254 : * Deallocate SPAR encoder handle
255 : *------------------------------------------------------------------------*/
256 :
257 1903 : 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 1903 : test();
267 1903 : IF( hSpar == NULL || *hSpar == NULL )
268 : {
269 345 : return;
270 : }
271 :
272 1558 : IF( !spar_reconfig_flag )
273 : {
274 : /* core-coder-VAD handle */
275 279 : 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 279 : IF( ( *hSpar )->hFrontVad != NULL )
283 : {
284 69 : front_vad_destroy_fx( &( *hSpar )->hFrontVad );
285 69 : ( *hSpar )->hFrontVad = NULL;
286 : }
287 : }
288 :
289 1558 : num_chans = ( *hSpar )->hFbMixer->fb_cfg->num_in_chans;
290 1558 : move16();
291 1558 : assert( num_chans <= nchan_inp );
292 :
293 : /* MD handle */
294 1558 : ivas_spar_md_enc_close_fx( &( *hSpar )->hMdEnc );
295 :
296 : /* Covar. State handle */
297 1558 : ivas_spar_covar_enc_close_fx( &( *hSpar )->hCovEnc, num_chans );
298 :
299 : /* FB mixer handle */
300 1558 : ivas_FB_mixer_close_fx( &( *hSpar )->hFbMixer, input_Fs, spar_reconfig_flag );
301 :
302 : /* AGC */
303 1558 : ivas_spar_agc_enc_close_fx( &( *hSpar )->hAgcEnc );
304 :
305 : /* PCA */
306 1558 : IF( ( *hSpar )->hPCA != NULL )
307 : {
308 4 : free( ( *hSpar )->hPCA );
309 4 : ( *hSpar )->hPCA = NULL;
310 : }
311 :
312 1558 : IF( !spar_reconfig_flag )
313 : {
314 : /* Trans Det handle */
315 279 : ivas_transient_det_close_fx( &( *hSpar )->hTranDet );
316 279 : free( ( *hSpar ) );
317 279 : ( *hSpar ) = NULL;
318 : }
319 :
320 1558 : return;
321 : }
322 :
323 :
324 : /*-------------------------------------------------------------------*
325 : * ivas_spar_enc()
326 : *
327 : * Principal IVAS SPAR encoder routine
328 : *-------------------------------------------------------------------*/
329 :
330 157500 : 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 157500 : error = IVAS_ERR_OK;
342 157500 : move32();
343 157500 : hEncoderConfig = st_ivas->hEncoderConfig;
344 :
345 157500 : IF( st_ivas->nSCE > 0 )
346 : {
347 43891 : st0 = st_ivas->hSCE[0]->hCoreCoder[0];
348 : }
349 : ELSE
350 : {
351 113609 : st0 = st_ivas->hCPE[0]->hCoreCoder[0];
352 : }
353 :
354 157500 : test();
355 157500 : test();
356 : /* Write SBA signaling bits */
357 157500 : 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 138810 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
361 :
362 : /* Write SBA order */
363 138810 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
364 : }
365 : ELSE /* ism_mode == ISM_MODE_NONE */
366 : {
367 18690 : IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
368 : {
369 : /* Write SBA planar flag */
370 2651 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_planar, SBA_PLANAR_BITS );
371 :
372 : /* hack to indicate OSBA bitstream at VLBR */
373 2651 : push_indice( st0->hBstr, IND_SMODE, 0, SBA_ORDER_BITS );
374 : }
375 : ELSE
376 : {
377 : /* Write SBA order */
378 16039 : push_indice( st0->hBstr, IND_SMODE, hEncoderConfig->sba_order, SBA_ORDER_BITS );
379 : }
380 : }
381 :
382 157500 : IF( EQ_32( hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) )
383 : {
384 : /* write the number of objects in ISM_SBA format*/
385 35000 : push_indice( hMetaData, IND_ISM_NUM_OBJECTS, hEncoderConfig->nchan_ism - 1, NO_BITS_MASA_ISM_NO_OBJ );
386 : }
387 : /* front VAD */
388 157500 : IF( NE_32( ( error = front_vad_spar_fx( st_ivas->hSpar, data_fx[0], hEncoderConfig, input_frame ) ), IVAS_ERR_OK ) )
389 : {
390 0 : return error;
391 : }
392 :
393 157500 : IF( hEncoderConfig->sba_planar )
394 : {
395 10250 : ivas_sba_zero_vert_comp_fx( data_fx, st_ivas->sba_analysis_order, hEncoderConfig->sba_planar, input_frame );
396 : }
397 :
398 : /* SPAR encoder */
399 157500 : 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 ) )
400 : {
401 0 : return error;
402 : }
403 :
404 157500 : *nb_bits_metadata = hMetaData->nb_bits_tot;
405 157500 : move16();
406 :
407 157500 : test();
408 : /* Force IVAS front pre-proc decision for higher bitrates */
409 157500 : if ( GT_32( hEncoderConfig->ivas_total_brate, SBA_DTX_BITRATE_THRESHOLD ) || hEncoderConfig->Opt_DTX_ON == 0 )
410 : {
411 139710 : st_ivas->hSpar->front_vad_flag = 0;
412 139710 : move16();
413 : }
414 :
415 157500 : return error;
416 : }
417 :
418 :
419 : /*-------------------------------------------------------------------*
420 : * ivas_spar_cov_md_process()
421 : *
422 : * Process call for SPAR covariance and MD encoder
423 : *-------------------------------------------------------------------*/
424 :
425 157500 : static ivas_error ivas_spar_cov_md_process_fx(
426 : const ENCODER_CONFIG_HANDLE hEncoderConfig,
427 : SPAR_ENC_HANDLE hSpar,
428 : const IVAS_QMETADATA_HANDLE hQMetaData,
429 : BSTR_ENC_HANDLE hMetaData,
430 : const Word16 nchan_inp,
431 : const Word16 sba_order,
432 : Word32 *ppIn_FR_real_fx[IVAS_SPAR_MAX_CH], // q_ppIn_FR
433 : Word32 *ppIn_FR_imag_fx[IVAS_SPAR_MAX_CH], // q_ppIn_FR
434 : const Word16 transient_det[2],
435 : const Word16 dtx_vad,
436 : const Word16 nchan_transport,
437 : Word16 *dyn_active_w_flag,
438 : Word16 q_ppIn_FR )
439 : {
440 : Word16 i, j, i_ts, b, table_idx;
441 : Word16 active_w_vlbr;
442 : /* note: the actual dimensions of matrixes correspond to num_channels = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate ); */
443 : Word32 *cov_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
444 : Word32 *cov_dtx_real_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
445 : Word16 *q_cov_real[IVAS_SPAR_MAX_CH];
446 : Word16 *q_cov_dtx_real[IVAS_SPAR_MAX_CH];
447 : Word32 cov_real_buf_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
448 : Word32 cov_dtx_real_buf_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
449 : ivas_error error;
450 :
451 157500 : error = IVAS_ERR_OK;
452 157500 : move32();
453 :
454 157500 : IF( LT_32( hEncoderConfig->ivas_total_brate, IVAS_24k4 ) )
455 : {
456 13323 : active_w_vlbr = 1;
457 13323 : move16();
458 : }
459 : ELSE
460 : {
461 144177 : active_w_vlbr = 0;
462 144177 : move16();
463 : }
464 :
465 : /*-----------------------------------------------------------------------------------------*
466 : * Set SPAR bitrates
467 : *-----------------------------------------------------------------------------------------*/
468 :
469 157500 : table_idx = ivas_get_spar_table_idx_fx( hEncoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
470 :
471 157500 : IF( NE_16( hSpar->hMdEnc->table_idx, table_idx ) )
472 : {
473 1861 : hSpar->hMdEnc->table_idx = table_idx;
474 1861 : move16();
475 1861 : test();
476 1861 : IF( NE_32( hEncoderConfig->ivas_total_brate, hEncoderConfig->last_ivas_total_brate ) && !hSpar->spar_reconfig_flag )
477 : {
478 303 : IF( NE_32( ( error = ivas_spar_md_enc_init_fx( hSpar->hMdEnc, hEncoderConfig, sba_order ) ), IVAS_ERR_OK ) )
479 : {
480 0 : return error;
481 : }
482 : }
483 : ELSE
484 : {
485 1558 : Word16 num_bands = SPAR_DIRAC_SPLIT_START_BAND;
486 1558 : move16();
487 1558 : if ( hSpar->hMdEnc->spar_hoa_md_flag != 0 )
488 : {
489 259 : num_bands = IVAS_MAX_NUM_BANDS;
490 259 : move16();
491 : }
492 1558 : 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 );
493 : }
494 : }
495 :
496 : /*-----------------------------------------------------------------------------------------*
497 : * Covariance process
498 : *-----------------------------------------------------------------------------------------*/
499 889700 : FOR( i = 0; i < nchan_inp; i++ )
500 : {
501 4628660 : FOR( j = 0; j < nchan_inp; j++ )
502 : {
503 3896460 : cov_real_fx[i][j] = cov_real_buf_fx[i][j];
504 3896460 : cov_dtx_real_fx[i][j] = cov_dtx_real_buf_fx[i][j];
505 4320460 : FOR( b = hSpar->hFbMixer->pFb->filterbank_num_bands; b < IVAS_MAX_NUM_BANDS; b++ )
506 : {
507 424000 : cov_real_fx[i][j][b] = 0;
508 424000 : move32();
509 424000 : cov_dtx_real_fx[i][j][b] = 0;
510 424000 : move32();
511 : }
512 : }
513 732200 : IF( ( q_cov_real[i] = (Word16 *) malloc( sizeof( Word16 ) * nchan_inp ) ) == NULL )
514 : {
515 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder Fixed" );
516 : }
517 732200 : set16_fx( q_cov_real[i], Q31, nchan_inp );
518 732200 : IF( ( q_cov_dtx_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 732200 : set16_fx( q_cov_dtx_real[i], Q31, nchan_inp );
523 : }
524 :
525 157500 : 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,
526 157500 : &hSpar->hMdEnc->spar_md.res_ind, remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order], dyn_active_w_flag, nchan_transport, 1 );
527 :
528 889700 : FOR( i = 0; i < nchan_inp; i++ )
529 : {
530 732200 : free( q_cov_real[i] );
531 732200 : q_cov_real[i] = NULL;
532 732200 : free( q_cov_dtx_real[i] );
533 732200 : q_cov_dtx_real[i] = NULL;
534 : }
535 157500 : test();
536 157500 : IF( GT_16( nchan_transport, 1 ) && LE_16( nchan_transport, ( FOA_CHANNELS - 1 ) ) )
537 : {
538 72649 : push_next_indice( hMetaData, *dyn_active_w_flag, 1 );
539 72649 : test();
540 72649 : IF( EQ_16( *dyn_active_w_flag, 1 ) && EQ_16( nchan_transport, 2 ) )
541 : {
542 0 : push_next_indice( hMetaData, sub( hSpar->hMdEnc->spar_md.res_ind, nchan_transport ), 1 );
543 : }
544 72649 : IF( EQ_16( *dyn_active_w_flag, 1 ) )
545 : {
546 0 : hSpar->front_vad_flag = 1;
547 0 : move16();
548 : }
549 : ELSE
550 : {
551 72649 : hSpar->front_vad_flag = hSpar->front_vad_flag;
552 72649 : move16();
553 : }
554 : }
555 : ELSE
556 : {
557 84851 : IF( EQ_16( nchan_transport, FOA_CHANNELS ) )
558 : {
559 40960 : push_next_indice( hMetaData, 0, 1 );
560 : }
561 : }
562 :
563 : /*-----------------------------------------------------------------------------------------*
564 : * MetaData encoder
565 : *-----------------------------------------------------------------------------------------*/
566 :
567 157500 : IF( hSpar->hMdEnc->spar_hoa_md_flag == 0 )
568 : {
569 135460 : 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 ) )
570 : {
571 0 : return error;
572 : }
573 : }
574 :
575 157500 : IF( hSpar->hMdEnc->spar_hoa_dirac2spar_md_flag )
576 : {
577 : Word16 order;
578 : Word32 azi_dirac_fx[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
579 : Word32 ele_dirac_fx[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
580 : Word32 diffuseness_fx[IVAS_MAX_NUM_BANDS];
581 : Word32 Wscale_d_fx[IVAS_MAX_NUM_BANDS];
582 : Word16 d_start_band, d_end_band;
583 : Word16 dirac_band_idx;
584 :
585 147560 : d_start_band = hSpar->enc_param_start_band;
586 147560 : move16();
587 147560 : d_end_band = IVAS_MAX_NUM_BANDS;
588 147560 : move16();
589 :
590 788200 : FOR( b = d_start_band; b < d_end_band; b++ )
591 : {
592 640640 : dirac_band_idx = sub( hSpar->dirac_to_spar_md_bands[b], d_start_band );
593 3043324 : FOR( i_ts = 0; i_ts < hQMetaData->q_direction->cfg.nblocks; i_ts++ )
594 : {
595 2402684 : azi_dirac_fx[b][i_ts] = hQMetaData->q_direction->band_data[dirac_band_idx].azimuth_fx[i_ts];
596 2402684 : move32();
597 2402684 : ele_dirac_fx[b][i_ts] = hQMetaData->q_direction->band_data[dirac_band_idx].elevation_fx[i_ts];
598 2402684 : move32();
599 : }
600 640640 : diffuseness_fx[b] = L_sub( ONE_IN_Q30, hQMetaData->q_direction->band_data[dirac_band_idx].energy_ratio_fx[0] );
601 640640 : move32();
602 : }
603 147560 : test();
604 147560 : IF( GE_16( d_start_band, 6 ) && EQ_16( dtx_vad, 1 ) )
605 : {
606 137897 : 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 );
607 : }
608 :
609 788200 : FOR( b = d_start_band; b < d_end_band; b++ )
610 : {
611 640640 : Word16 tmp_e, Wscale_d_e = 0;
612 640640 : move16();
613 640640 : Wscale_d_fx[b] = ONE_IN_Q31;
614 640640 : move32();
615 2843120 : FOR( i = 1; i < nchan_inp; i++ )
616 : {
617 2202480 : Word32 tmp = 0;
618 2202480 : move32();
619 2202480 : tmp_e = 0;
620 2202480 : move16();
621 2202480 : IF( cov_real_fx[0][0][b] != 0 )
622 : {
623 2096960 : tmp = L_deposit_h( BASOP_Util_Divide3232_Scale( cov_real_fx[i][i][b], cov_real_fx[0][0][b], &tmp_e ) );
624 2096960 : 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] ) ) );
625 : }
626 2202480 : Wscale_d_fx[b] = BASOP_Util_Add_Mant32Exp( tmp, tmp_e, Wscale_d_fx[b], Wscale_d_e, &Wscale_d_e );
627 2202480 : move32();
628 : }
629 640640 : Wscale_d_fx[b] = L_deposit_h( BASOP_Util_Divide3232_Scale( Wscale_d_fx[b], L_add( 1, sba_order ), &tmp_e ) );
630 640640 : move32();
631 640640 : tmp_e = add( tmp_e, sub( Wscale_d_e, Q31 ) );
632 640640 : Wscale_d_fx[b] = Sqrt32( Wscale_d_fx[b], &tmp_e );
633 640640 : move32();
634 640640 : 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
635 640640 : move32();
636 640640 : Wscale_d_fx[b] = L_min( ONE_IN_Q30, L_max( Wscale_d_fx[b], ONE_IN_Q29 ) ); // Q29
637 640640 : move32();
638 : }
639 :
640 147560 : IF( hSpar->hMdEnc->spar_hoa_md_flag == 0 )
641 : {
642 135460 : order = sba_order;
643 135460 : move16();
644 : }
645 : ELSE
646 : {
647 12100 : order = 1;
648 12100 : move16();
649 : }
650 147560 : 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 );
651 : }
652 :
653 157500 : IF( hSpar->hMdEnc->spar_hoa_md_flag )
654 : {
655 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 );
656 : }
657 :
658 157500 : return error;
659 : }
660 :
661 :
662 : /*-----------------------------------------------------------------------------------------*
663 : * Function ivas_spar_enc_process()
664 : *
665 : * Process call for SPAR encoder
666 : *-----------------------------------------------------------------------------------------*/
667 :
668 157500 : static ivas_error ivas_spar_enc_process_fx(
669 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
670 : const ENCODER_CONFIG_HANDLE hEncoderConfig, /* i : configuration structure */
671 : BSTR_ENC_HANDLE hMetaData, /* i/o: MetaData handle */
672 : const Word16 front_vad_flag, /* i : front-VAD decision */
673 : Word32 *data_fx[], /* i/o: input/transport audio channels Q(q_data) */
674 : const Word16 q_data )
675 : {
676 : Word32 pcm_tmp_fx[DIRAC_MAX_ANA_CHANS][L_FRAME48k * 2];
677 : Word32 *p_pcm_tmp_fx[DIRAC_MAX_ANA_CHANS];
678 : Word16 q_pcm_fx[DIRAC_MAX_ANA_CHANS];
679 : Word16 q_p_pcm_tmp_fx[DIRAC_MAX_ANA_CHANS];
680 : Word16 i, j, input_frame, dtx_vad;
681 : Word16 transient_det[2];
682 : Word16 hodirac_flag;
683 : Word32 ivas_total_brate, input_Fs;
684 : Word16 nchan_inp, sba_order, nchan_transport;
685 : Word16 in_out_mixer_map[IVAS_MAX_FB_MIXER_OUT_CH][IVAS_MAX_SPAR_FB_MIXER_IN_CH];
686 : ivas_error error;
687 : const Word16 *order;
688 157500 : SPAR_ENC_HANDLE hSpar = st_ivas->hSpar;
689 157500 : IVAS_QMETADATA_HANDLE hQMetaData = st_ivas->hQMetaData;
690 : Word16 ts, l_ts, num_del_samples, b, i_ts;
691 :
692 : Word32 *ppIn_FR_real_fx[IVAS_SPAR_MAX_CH], *ppIn_FR_imag_fx[IVAS_SPAR_MAX_CH];
693 : Word32 wyzx_del_buf[FOA_CHANNELS][IVAS_FB_1MS_48K_SAMP];
694 : Word16 dyn_active_w_flag;
695 : Word16 nchan_fb_in;
696 :
697 157500 : push_wmops( "ivas_spar_enc_process" );
698 :
699 : /*-----------------------------------------------------------------------------------------*
700 : * Initialization
701 : *-----------------------------------------------------------------------------------------*/
702 :
703 157500 : error = IVAS_ERR_OK;
704 157500 : move32();
705 :
706 157500 : input_Fs = hEncoderConfig->input_Fs;
707 157500 : move32();
708 157500 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
709 157500 : move32();
710 157500 : num_del_samples = hSpar->hFbMixer->fb_cfg->fb_latency;
711 157500 : move16();
712 :
713 : Word32 res_dec, res_frac;
714 157500 : iDiv_and_mod_32( input_Fs, FRAMES_PER_SEC, &res_dec, &res_frac, 0 );
715 157500 : input_frame = extract_l( res_dec );
716 :
717 157500 : sba_order = s_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
718 157500 : nchan_inp = ivas_sba_get_nchan_metadata_fx( sba_order, hEncoderConfig->ivas_total_brate );
719 157500 : assert( nchan_inp <= hEncoderConfig->nchan_inp );
720 :
721 157500 : nchan_fb_in = hSpar->hFbMixer->fb_cfg->nchan_fb_in;
722 157500 : move16();
723 157500 : nchan_transport = st_ivas->nchan_transport;
724 157500 : move16();
725 :
726 273960 : FOR( i = FOA_CHANNELS + 1; i < nchan_fb_in; i++ )
727 : {
728 116460 : Copy32( data_fx[HOA_keep_ind[i]], data_fx[i], input_frame );
729 : }
730 : /*-----------------------------------------------------------------------------------------*
731 : * Transient detector
732 : *-----------------------------------------------------------------------------------------*/
733 157500 : ivas_transient_det_process_fx( hSpar->hTranDet, data_fx[0], input_frame, transient_det );
734 :
735 : #ifdef DEBUGGING
736 : {
737 : float a[2];
738 : a[0] = transient_det[0];
739 : a[1] = transient_det[1];
740 : dbgwrite_txt( a, 2, "fixed.txt", NULL );
741 : }
742 : #endif
743 :
744 157500 : if ( LT_32( ivas_total_brate, IVAS_24k4 ) )
745 : {
746 13323 : transient_det[1] = 0;
747 13323 : move16();
748 : }
749 :
750 : /* store previous input samples for W in local buffer */
751 157500 : assert( num_del_samples <= IVAS_FB_1MS_48K_SAMP );
752 157500 : IF( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 )
753 : {
754 : /* fill delay (1 ms) buffer for all Transport channels */
755 568045 : FOR( i = 0; i < FOA_CHANNELS; i++ )
756 : {
757 454436 : Word16 idx = hSpar->hFbMixer->fb_cfg->remix_order[i];
758 454436 : move16();
759 454436 : 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 );
760 : }
761 : }
762 :
763 : /*-----------------------------------------------------------------------------------------*
764 : * FB mixer ingest
765 : *-----------------------------------------------------------------------------------------*/
766 926000 : FOR( i = 0; i < nchan_fb_in; i++ )
767 : {
768 768500 : p_pcm_tmp_fx[i] = pcm_tmp_fx[i];
769 : }
770 :
771 : /* run Filter Bank overlapping MDFT analysis first, then we can use the temporary buffer for Parameter MDFT analysis*/
772 157500 : 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 );
773 889700 : FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_in_chans; i++ )
774 : {
775 : /* changing q format to q_data*/
776 732200 : scale_sig32( p_pcm_tmp_fx[i], shl( input_frame, 1 ), sub( q_data, q_p_pcm_tmp_fx[i] ) ); // q_data
777 : }
778 :
779 : /* prepare Parameter MDFT analysis */
780 157500 : Word16 shift = Q31;
781 157500 : move16();
782 926000 : FOR( i = 0; i < nchan_fb_in; i++ )
783 : {
784 768500 : ppIn_FR_real_fx[i] = p_pcm_tmp_fx[i];
785 768500 : ppIn_FR_imag_fx[i] = p_pcm_tmp_fx[i] + input_frame;
786 768500 : p_pcm_tmp_fx[i] = data_fx[i];
787 768500 : shift = s_min( shift, L_norm_arr( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length ) );
788 768500 : shift = s_min( shift, L_norm_arr( data_fx[i], input_frame ) );
789 : }
790 :
791 157500 : l_ts = idiv1616( input_frame, MAX_PARAM_SPATIAL_SUBFRAMES );
792 :
793 157500 : Word16 gb = find_guarded_bits_fx( l_ts );
794 787500 : FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
795 : {
796 3704000 : FOR( i = 0; i < nchan_fb_in; i++ )
797 : {
798 3074000 : scale_sig32( data_fx[i], input_frame, shift ); // Q:q_data+shift
799 3074000 : scale_sig32( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length, shift ); // Q:q_data+shift
800 : }
801 630000 : 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 );
802 3704000 : FOR( i = 0; i < nchan_fb_in; i++ )
803 : {
804 3074000 : scale_sig32( data_fx[i], input_frame, negate( shift ) ); // Q:q_data
805 3074000 : scale_sig32( hSpar->hFbMixer->ppFilterbank_prior_input_fx[i], hSpar->hFbMixer->fb_cfg->prior_input_length, negate( shift ) ); // Q:q_data
806 : }
807 630000 : ivas_fb_mixer_update_prior_input_fx( hSpar->hFbMixer, p_pcm_tmp_fx, l_ts, nchan_fb_in );
808 :
809 3704000 : FOR( i = 0; i < nchan_fb_in; i++ )
810 : {
811 3074000 : p_pcm_tmp_fx[i] += l_ts;
812 3074000 : ppIn_FR_real_fx[i] += l_ts;
813 3074000 : ppIn_FR_imag_fx[i] += l_ts;
814 3074000 : q_p_pcm_tmp_fx[i] = sub( q_data, gb ); // updating the q for p_pcm
815 3074000 : move16();
816 : }
817 : }
818 :
819 : /* turn pointers back to the local buffer, needed for the following processing */
820 926000 : FOR( i = 0; i < nchan_fb_in; i++ )
821 : {
822 768500 : ppIn_FR_real_fx[i] = pcm_tmp_fx[i];
823 768500 : ppIn_FR_imag_fx[i] = pcm_tmp_fx[i] + input_frame;
824 768500 : p_pcm_tmp_fx[i] = pcm_tmp_fx[i];
825 : }
826 157500 : IF( EQ_16( hEncoderConfig->Opt_DTX_ON, 1 ) )
827 : {
828 18000 : dtx_vad = front_vad_flag;
829 18000 : move16();
830 : }
831 : ELSE
832 : {
833 139500 : dtx_vad = 1;
834 139500 : move16();
835 : }
836 :
837 : /*-----------------------------------------------------------------------------------------*
838 : * DirAC encoding
839 : *-----------------------------------------------------------------------------------------*/
840 :
841 157500 : hodirac_flag = ivas_get_hodirac_flag_fx( ivas_total_brate, st_ivas->sba_analysis_order );
842 :
843 : /*Finding max possible q*/
844 : Word16 pp_fr_q;
845 157500 : pp_fr_q = sub( add( q_data, shift ), gb );
846 157500 : Word16 re_q = MAX_16, im_q = MAX_16, FR_q;
847 157500 : move16();
848 157500 : move16();
849 926000 : FOR( i = 0; i < nchan_fb_in; i++ )
850 : {
851 768500 : re_q = s_min( re_q, L_norm_arr( ppIn_FR_real_fx[i], input_frame ) );
852 768500 : im_q = s_min( im_q, L_norm_arr( ppIn_FR_imag_fx[i], input_frame ) );
853 : }
854 157500 : FR_q = s_min( re_q, im_q );
855 926000 : FOR( i = 0; i < nchan_fb_in; i++ )
856 : {
857 768500 : scale_sig32( ppIn_FR_real_fx[i], input_frame, FR_q ); // Q(FR_q+pp_fr_q)
858 768500 : scale_sig32( ppIn_FR_imag_fx[i], input_frame, FR_q ); // Q(FR_q+pp_fr_q)
859 : }
860 157500 : pp_fr_q = add( FR_q, pp_fr_q );
861 :
862 157500 : 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, hodirac_flag, shift ) ), IVAS_ERR_OK ) )
863 : {
864 0 : return error;
865 : }
866 :
867 : /* Set Energy Ratio to 0.0 if the mono flag has been set */
868 157500 : IF( hQMetaData->dirac_mono_flag )
869 : {
870 0 : FOR( b = hQMetaData->q_direction->cfg.start_band; b < hQMetaData->q_direction->cfg.nbands; b++ )
871 : {
872 0 : Word16 loop_count = 1;
873 0 : move16();
874 0 : IF( EQ_16( dtx_vad, 1 ) )
875 : {
876 0 : loop_count = hQMetaData->q_direction[0].cfg.nblocks;
877 0 : move16();
878 : }
879 0 : FOR( i_ts = 0; i_ts < loop_count; i_ts++ )
880 : {
881 0 : hQMetaData->q_direction[0].band_data[b].energy_ratio_fx[i_ts] = 0;
882 0 : move32();
883 : }
884 : }
885 : }
886 :
887 : /*-----------------------------------------------------------------------------------------*
888 : * Covariance and MD processing
889 : *-----------------------------------------------------------------------------------------*/
890 157500 : 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 ) )
891 : {
892 0 : return error;
893 : }
894 :
895 157500 : set16_fx( q_p_pcm_tmp_fx, pp_fr_q, nchan_fb_in );
896 : /*-----------------------------------------------------------------------------------------*
897 : * FB mixer
898 : *-----------------------------------------------------------------------------------------*/
899 :
900 157500 : ivas_fb_mixer_get_in_out_mapping_fx( hSpar->hFbMixer->fb_cfg, in_out_mixer_map );
901 :
902 : #ifdef DEBUG_SPAR_DIRAC_WRITE_OUT_PRED_PARS
903 : {
904 : static FILE *fid = 0;
905 : int16_t band = 9;
906 : if ( !fid )
907 : {
908 : fid = fopen( "pred_coeffs_enc.txt", "wt" );
909 : }
910 : fprintf( fid, "%.6f\n", hSpar->hMdEnc->mixer_mat[1][0][band] );
911 : }
912 : #endif
913 :
914 157500 : 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 );
915 :
916 429399 : FOR( Word32 ch = ( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 ); ch < hSpar->hFbMixer->fb_cfg->num_out_chans; ch++ )
917 : {
918 : /* changing q format to q_data*/
919 271899 : scale_sig32( p_pcm_tmp_fx[ch], input_frame, sub( q_data, q_p_pcm_tmp_fx[ch] ) ); // q_data
920 271899 : q_p_pcm_tmp_fx[ch] = q_data;
921 271899 : move16();
922 : }
923 157500 : IF( hSpar->hFbMixer->fb_cfg->active_w_mixing == 0 )
924 : {
925 : /*cross fade between new active W channels and old passive W channel*/
926 113609 : IF( EQ_16( dyn_active_w_flag, 1 ) )
927 : {
928 0 : IF( NE_16( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag, dyn_active_w_flag ) )
929 : {
930 : Word32 new_w[L_FRAME48k];
931 : /* delayed W */
932 0 : Copy32( wyzx_del_buf[0], p_pcm_tmp_fx[0], num_del_samples );
933 0 : Copy32( data_fx[0], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
934 :
935 0 : Copy32( wyzx_del_buf[hSpar->hMdEnc->spar_md.res_ind], new_w, num_del_samples );
936 0 : Copy32( data_fx[hSpar->hMdEnc->spar_md.res_ind], &new_w[num_del_samples], sub( input_frame, num_del_samples ) );
937 :
938 0 : test();
939 0 : test();
940 0 : IF( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag == 0 && EQ_16( dyn_active_w_flag, 1 ) )
941 : {
942 0 : ivas_fb_mixer_cross_fading_fx( hSpar->hFbMixer, p_pcm_tmp_fx, p_pcm_tmp_fx[0], new_w, 0, input_frame, 0 );
943 : }
944 0 : ELSE IF( EQ_16( hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag, 1 ) && dyn_active_w_flag == 0 )
945 : {
946 0 : ivas_fb_mixer_cross_fading_fx( hSpar->hFbMixer, p_pcm_tmp_fx, new_w, p_pcm_tmp_fx[0], 0, input_frame, 0 );
947 : }
948 : }
949 : ELSE
950 : {
951 0 : Copy32( wyzx_del_buf[hSpar->hMdEnc->spar_md.res_ind], p_pcm_tmp_fx[0], num_del_samples );
952 0 : Copy32( data_fx[hSpar->hMdEnc->spar_md.res_ind], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
953 : }
954 : }
955 : ELSE
956 : {
957 : /* delayed W */
958 113609 : Copy32( wyzx_del_buf[0], p_pcm_tmp_fx[0], num_del_samples );
959 113609 : Copy32( data_fx[0], p_pcm_tmp_fx[0] + num_del_samples, sub( input_frame, num_del_samples ) );
960 : }
961 :
962 341617 : FOR( i = 1; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
963 : {
964 228008 : Word16 idx = hSpar->hFbMixer->fb_cfg->remix_order[i];
965 228008 : move16();
966 :
967 : /* delayed, reorderd and accumulated with (negative) prediction from W */
968 228008 : v_add_fx( wyzx_del_buf[idx], p_pcm_tmp_fx[i], p_pcm_tmp_fx[i], num_del_samples );
969 228008 : 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 ) );
970 : }
971 : }
972 : /*-----------------------------------------------------------------------------------------*
973 : * PCA encoder
974 : *-----------------------------------------------------------------------------------------*/
975 :
976 157500 : IF( hSpar->hPCA != NULL )
977 : {
978 1750 : ivas_pca_enc_fx( hEncoderConfig, hSpar->hPCA, hMetaData, p_pcm_tmp_fx, input_frame, FOA_CHANNELS );
979 : }
980 : ELSE
981 : {
982 155750 : test();
983 155750 : IF( EQ_32( ivas_total_brate, PCA_BRATE ) && EQ_16( sba_order, SBA_FOA_ORDER ) )
984 : {
985 : /* write PCA bypass bit */
986 9830 : push_next_indice( hMetaData, PCA_MODE_INACTIVE, 1 );
987 : }
988 : }
989 :
990 :
991 : /*-----------------------------------------------------------------------------------------*
992 : * AGC
993 : *-----------------------------------------------------------------------------------------*/
994 :
995 157500 : IF( EQ_16( dtx_vad, 1 ) )
996 : {
997 154137 : IF( hSpar->AGC_Enable != 0 )
998 : {
999 84036 : FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
1000 : {
1001 42018 : q_pcm_fx[i] = q_data;
1002 42018 : move16();
1003 : }
1004 :
1005 42018 : 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 );
1006 :
1007 84036 : FOR( i = 0; i < hSpar->hFbMixer->fb_cfg->num_out_chans; i++ )
1008 : {
1009 42018 : scale_sig32( p_pcm_tmp_fx[i], input_frame, sub( q_data, q_pcm_fx[i] ) ); // q_data
1010 42018 : q_pcm_fx[i] = q_data;
1011 42018 : move16();
1012 : }
1013 : }
1014 : }
1015 :
1016 :
1017 : /*-----------------------------------------------------------------------------------------*
1018 : * Re-order the dmx back to ACN/SN3D format
1019 : *-----------------------------------------------------------------------------------------*/
1020 :
1021 157500 : order = remix_order_set[hSpar->hMdEnc->spar_md_cfg.remix_unmix_order];
1022 157500 : move16();
1023 :
1024 543008 : FOR( j = 0; j < nchan_transport; j++ )
1025 : {
1026 385508 : IF( LT_16( st_ivas->nchan_transport, 3 ) )
1027 : {
1028 124231 : Copy32( p_pcm_tmp_fx[j], data_fx[j], input_frame );
1029 : }
1030 : ELSE
1031 : {
1032 261277 : Copy32( p_pcm_tmp_fx[j], data_fx[order[j]], input_frame );
1033 : }
1034 : }
1035 401992 : FOR( ; j < IVAS_SPAR_MAX_DMX_CHS; j++ )
1036 : {
1037 244492 : set32_fx( data_fx[order[j]], 0, input_frame );
1038 : }
1039 :
1040 157500 : hSpar->hMdEnc->spar_md.prior_dyn_active_w_flag = dyn_active_w_flag;
1041 157500 : move16();
1042 :
1043 157500 : pop_wmops();
1044 :
1045 157500 : return error;
1046 : }
|