Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <math.h>
34 : #include <assert.h>
35 : #include "options.h"
36 : #include "cnst.h"
37 : #include "rom_enc.h"
38 : #include "ivas_rom_enc.h"
39 : #include "rom_com.h"
40 : #include "prot.h"
41 : #include "ivas_prot.h"
42 : #include "ivas_cnst.h"
43 : #include "ivas_rom_com.h"
44 : #ifdef DEBUGGING
45 : #include "debug.h"
46 : #endif
47 : #ifdef DEBUG_PLOT
48 : #include "deb_out.h"
49 : #endif
50 : #include "wmc_auto.h"
51 :
52 :
53 : /*-------------------------------------------------------------------------
54 : * Local function prototypes
55 : *------------------------------------------------------------------------*/
56 :
57 : static void ivas_param_mc_dmx( PARAM_MC_ENC_HANDLE hParamMC, float *data_f[], float data_dmx[][L_FRAME48k], const int16_t input_frame, const int16_t nchan_input, const int16_t nchan_transport );
58 :
59 : static void ivas_param_mc_param_est_enc( PARAM_MC_ENC_HANDLE hParamMC, float *data_f[], float Cy_sum[][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], float Cx_sum[][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], const int16_t input_frame, const int16_t nchan_input, const int16_t nchan_transport );
60 :
61 : static void ivas_param_mc_parameter_quantizer( const float *x, const int16_t L, const int16_t sz_quantizer, const float *quantizer, int16_t *quant_idx, float *y );
62 :
63 : static void ivas_param_mc_transient_detection( PARAM_MC_ENC_HANDLE hParamMC, TRAN_DET_HANDLE hTranDet, int16_t *bAttackPresent, int16_t *attackIdx );
64 :
65 : static void ivas_param_mc_quantize_iccs( PARAM_MC_ENC_HANDLE hParamMC, float Cy[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], const int16_t freq_idx, const int16_t nchan_input, int16_t *ICC_idx_out );
66 :
67 : static void ivas_param_mc_quantize_ilds( PARAM_MC_ENC_HANDLE hParamMC, float Cy[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], float Cx[PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], const int16_t freq_idx, const int16_t nchan_input, const int16_t nchan_transport, int16_t *ILD_idx_out, float ILD_q[PARAM_MC_SZ_ILD_MAP] );
68 :
69 : static void ivas_param_mc_write_bs( const PARAM_MC_ENC_HANDLE hParamMC, int16_t *ILD_idx, int16_t *ICC_idx, uint16_t bit_buffer[PARAM_MC_MAX_BITS], int16_t *bit_pos );
70 :
71 : static void ivas_param_mc_dec2bin( const int16_t val, const int16_t N, uint16_t bits[PARAM_MC_MAX_BITS] );
72 :
73 : static void ivas_param_mc_encode_parameter( int16_t *idx_in, HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC, HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParameterCodingInfo, const int16_t nbands, const int16_t band_step, const int16_t map_size_wo_lfe, const int16_t map_size, uint16_t bit_buffer[PARAM_MC_MAX_BITS], int16_t *bit_pos );
74 :
75 : static void ivas_param_mc_range_encoder( const int16_t *seq_in, const int16_t num_symbols, const uint16_t *cum_freq, const uint16_t *sym_freq, const uint16_t tot_shift, const int16_t max_nb_bits, uint16_t *bit_buffer, int16_t *bit_pos );
76 :
77 : /*-------------------------------------------------------------------------
78 : * ivas_param_mc_enc_open()
79 : *
80 : * Initialize Parametric MC encoder handle
81 : *------------------------------------------------------------------------*/
82 :
83 279 : ivas_error ivas_param_mc_enc_open(
84 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
85 : )
86 : {
87 : int16_t i;
88 : IVAS_FB_CFG *fb_cfg;
89 : PARAM_MC_ENC_HANDLE hParamMC;
90 : uint16_t config_index;
91 : MC_LS_SETUP mc_input_setup;
92 : int16_t max_bwidth, nchan_inp;
93 : int32_t input_Fs, ivas_total_brate;
94 : ivas_error error;
95 :
96 279 : error = IVAS_ERR_OK;
97 :
98 : /* Sanity Checks */
99 279 : if ( ( hParamMC = (PARAM_MC_ENC_HANDLE) malloc( sizeof( PARAM_MC_ENC_DATA ) ) ) == NULL )
100 : {
101 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Paramtric MC\n" ) );
102 : }
103 :
104 279 : mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
105 279 : max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
106 279 : input_Fs = st_ivas->hEncoderConfig->input_Fs;
107 279 : nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
108 279 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
109 :
110 : /* Preparing Config */
111 279 : hParamMC->lfe_index = LFE_CHANNEL;
112 279 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_input_setup );
113 :
114 : /* get configuration index */
115 279 : config_index = ivas_param_mc_get_configuration_index( mc_input_setup, ivas_total_brate );
116 :
117 : /* set core coder dependent on the number of transport channels */
118 279 : switch ( st_ivas->nchan_transport )
119 : {
120 17 : case 4:
121 : case 3:
122 17 : st_ivas->nCPE = 2;
123 17 : st_ivas->nSCE = 0;
124 17 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
125 17 : break;
126 262 : case 2:
127 262 : st_ivas->nCPE = 1;
128 262 : st_ivas->nSCE = 0;
129 262 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
130 262 : break;
131 : #ifdef DEBUGGING
132 : default:
133 : assert( 0 && "Number of transport channels not supported by ParamMC!\n" );
134 : #endif
135 : }
136 :
137 : /* get dmx factors */
138 279 : hParamMC->dmx_factors = ivas_param_mc_conf[config_index].dmx_fac;
139 :
140 : /* set FB config. */
141 279 : if ( ( error = ivas_fb_set_cfg( &fb_cfg, MC_FORMAT, nchan_inp, 0, 0, input_Fs, 0 ) ) != IVAS_ERR_OK )
142 : {
143 0 : return error;
144 : }
145 :
146 : /* Allocate and initialize FB mixer handle */
147 279 : if ( ( error = ivas_FB_mixer_open( &( hParamMC->hFbMixer ), input_Fs, fb_cfg, 0 ) ) != IVAS_ERR_OK )
148 : {
149 0 : return error;
150 : }
151 :
152 : /* open/init parameter coding */
153 279 : ivas_param_mc_metadata_open( mc_input_setup, ivas_total_brate, &hParamMC->hMetadataPMC );
154 :
155 : /* Band Grouping */
156 279 : if ( hParamMC->hMetadataPMC.num_parameter_bands == 20 )
157 : {
158 3 : mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
159 : }
160 276 : else if ( hParamMC->hMetadataPMC.num_parameter_bands == 14 )
161 : {
162 135 : mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
163 : }
164 141 : else if ( hParamMC->hMetadataPMC.num_parameter_bands == 10 )
165 : {
166 141 : mvs2s( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
167 : }
168 : else
169 : {
170 0 : assert( 0 && "nbands must be 20, 14, or 10!" );
171 : }
172 :
173 : /* set max parameter band for abs cov */
174 279 : i = 0;
175 2514 : while ( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
176 : {
177 2235 : hParamMC->max_param_band_abs_cov = ( i++ );
178 : }
179 :
180 : /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
181 3918 : for ( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
182 : {
183 3639 : hParamMC->band_grouping[i] *= PARAM_MC_CLDFB_TO_MDFT_FAC;
184 : }
185 :
186 : /* set correct coded band width */
187 279 : hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
188 279 : hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
189 279 : ivas_param_mc_set_coded_bands( &hParamMC->hMetadataPMC );
190 :
191 : /* initialize offset for transient detection */
192 279 : hParamMC->transient_detector_delay = ( NSUBBLOCKS_SHIFT + 1 ) + NSUBBLOCKS + 1 - (int16_t) ceilf( (float) NS2SA( input_Fs, DELAY_DIRAC_ENC_CMP_NS ) / (float) NS2SA( input_Fs, 2 * DIRAC_SLOT_NS ) );
193 :
194 : /* Init total/dmx ener factors */
195 279 : set_f( hParamMC->ener_fac, 0.0f, PARAM_MC_MAX_PARAMETER_BANDS );
196 :
197 : /* init previous ILDs */
198 5859 : for ( i = 0; i < PARAM_MC_MAX_PARAMETER_BANDS; i++ )
199 : {
200 5580 : set_zero( hParamMC->prev_ilds[i], PARAM_MC_SZ_ILD_MAP );
201 : }
202 :
203 279 : st_ivas->hParamMC = hParamMC;
204 :
205 279 : return error;
206 : }
207 :
208 :
209 : /*-------------------------------------------------------------------------
210 : * ivas_param_mc_enc_reconfig()
211 : *
212 : * Reconfigure Parametric MC encoder
213 : *------------------------------------------------------------------------*/
214 :
215 65 : ivas_error ivas_param_mc_enc_reconfig(
216 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
217 : )
218 : {
219 : int16_t i;
220 : PARAM_MC_ENC_HANDLE hParamMC;
221 : uint16_t config_index;
222 : MC_LS_SETUP mc_input_setup;
223 : int16_t max_bwidth;
224 : int32_t input_Fs, ivas_total_brate;
225 : ivas_error error;
226 :
227 65 : error = IVAS_ERR_OK;
228 :
229 65 : mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
230 65 : max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
231 65 : input_Fs = st_ivas->hEncoderConfig->input_Fs;
232 65 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
233 65 : hParamMC = st_ivas->hParamMC;
234 :
235 : /* Preparing Config */
236 65 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_input_setup );
237 :
238 : /* get configuration index */
239 65 : config_index = ivas_param_mc_get_configuration_index( mc_input_setup, ivas_total_brate );
240 :
241 : /* set core coder dependent on the number of transport channels */
242 65 : switch ( st_ivas->nchan_transport )
243 : {
244 0 : case 4:
245 : case 3:
246 0 : st_ivas->nCPE = 2;
247 0 : st_ivas->nSCE = 0;
248 0 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
249 0 : break;
250 65 : case 2:
251 65 : st_ivas->nCPE = 1;
252 65 : st_ivas->nSCE = 0;
253 65 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
254 65 : break;
255 : #ifdef DEBUGGING
256 : default:
257 : assert( 0 && "Number of transport channels not supported by ParamMC!\n" );
258 : #endif
259 : }
260 :
261 : /* get dmx factors */
262 65 : hParamMC->dmx_factors = ivas_param_mc_conf[config_index].dmx_fac;
263 :
264 : /* open/init parameter coding */
265 65 : ivas_param_mc_metadata_open( mc_input_setup, ivas_total_brate, &hParamMC->hMetadataPMC );
266 :
267 : /* Band Grouping */
268 65 : if ( hParamMC->hMetadataPMC.num_parameter_bands == 20 )
269 : {
270 0 : mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
271 : }
272 65 : else if ( hParamMC->hMetadataPMC.num_parameter_bands == 14 )
273 : {
274 34 : mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
275 : }
276 31 : else if ( hParamMC->hMetadataPMC.num_parameter_bands == 10 )
277 : {
278 31 : mvs2s( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
279 : }
280 : else
281 : {
282 0 : assert( 0 && "nbands must be 20, 14, or 10!" );
283 : }
284 :
285 : /* set max parameter band for abs cov */
286 65 : i = 0;
287 588 : while ( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
288 : {
289 523 : hParamMC->max_param_band_abs_cov = ( i++ );
290 : }
291 :
292 : /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
293 916 : for ( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
294 : {
295 851 : hParamMC->band_grouping[i] *= PARAM_MC_CLDFB_TO_MDFT_FAC;
296 : }
297 :
298 : /* set correct coded band width */
299 65 : hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
300 65 : hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
301 65 : ivas_param_mc_set_coded_bands( &hParamMC->hMetadataPMC );
302 :
303 : /* initialize offset for transient detection */
304 65 : hParamMC->transient_detector_delay = ( NSUBBLOCKS_SHIFT + 1 ) + NSUBBLOCKS + 1 - (int16_t) ceilf( (float) NS2SA( input_Fs, DELAY_DIRAC_ENC_CMP_NS ) / (float) NS2SA( input_Fs, 2 * DIRAC_SLOT_NS ) );
305 :
306 : /* Init total/dmx ener factors */
307 65 : set_f( hParamMC->ener_fac, 0.0f, PARAM_MC_MAX_PARAMETER_BANDS );
308 :
309 :
310 65 : return error;
311 : }
312 :
313 :
314 : /*-------------------------------------------------------------------------
315 : * ivas_param_mc_enc_close()
316 : *
317 : * Close Parametric MC encoder handle
318 : *------------------------------------------------------------------------*/
319 :
320 1210 : void ivas_param_mc_enc_close(
321 : PARAM_MC_ENC_HANDLE *hParamMC, /* i/o: Parametric MC encoder handle */
322 : const int32_t sampling_rate )
323 : {
324 1210 : if ( hParamMC == NULL || *hParamMC == NULL )
325 : {
326 931 : return;
327 : }
328 :
329 279 : ivas_FB_mixer_close( &( *hParamMC )->hFbMixer, sampling_rate, 0 );
330 :
331 279 : free( ( *hParamMC ) );
332 279 : ( *hParamMC ) = NULL;
333 :
334 279 : return;
335 : }
336 :
337 :
338 : /*-------------------------------------------------------------------------
339 : * ivas_param_mc_enc()
340 : *
341 : * Parametric MC Encoder main encoding function
342 : *------------------------------------------------------------------------*/
343 :
344 10250 : void ivas_param_mc_enc(
345 : Encoder_Struct *st_ivas, /* i/o: IVAS Encoder handle */
346 : BSTR_ENC_HANDLE hMetaData, /* i/o: IVAS Metadata bitstream handle */
347 : float *data_f[], /* i/o: input/transport MC data */
348 : const int16_t input_frame /* i : input frame length */
349 : )
350 : {
351 : int16_t k;
352 : float Cy_sum[PARAM_MC_MAX_PARAMETER_BANDS][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS];
353 : float Cx_sum[PARAM_MC_MAX_PARAMETER_BANDS][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS];
354 : float ILD_q[PARAM_MC_MAX_PARAMETER_BANDS][PARAM_MC_SZ_ILD_MAP];
355 : int16_t ILD_idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
356 : int16_t ICC_idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ICC_MAP];
357 : uint16_t bit_buffer[PARAM_MC_MAX_BITS];
358 : int16_t bit_pos;
359 : int16_t band_step;
360 : float data_dmx[PARAM_MC_MAX_TRANSPORT_CHANS][L_FRAME48k];
361 : int16_t ch;
362 : int16_t band;
363 : PARAM_MC_ENC_HANDLE hParamMC;
364 : int16_t nchan_inp;
365 :
366 10250 : push_wmops( "param_mc_enc" );
367 :
368 : /* initializations */
369 10250 : hParamMC = st_ivas->hParamMC;
370 10250 : bit_pos = 0;
371 10250 : band_step = 1;
372 10250 : nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
373 :
374 215250 : for ( band = 0; band < PARAM_MC_MAX_PARAMETER_BANDS; band++ )
375 : {
376 3485000 : for ( ch = 0; ch < MAX_CICP_CHANNELS; ch++ )
377 : {
378 3280000 : set_zero( Cy_sum[band][ch], MAX_CICP_CHANNELS );
379 : }
380 820000 : for ( ch = 0; ch < PARAM_MC_MAX_TRANSPORT_CHANS; ch++ )
381 : {
382 615000 : set_zero( Cx_sum[band][ch], PARAM_MC_MAX_TRANSPORT_CHANS );
383 : }
384 : }
385 :
386 215250 : for ( band = 0; band < PARAM_MC_MAX_PARAMETER_BANDS; band++ )
387 : {
388 205000 : set_zero( ILD_q[band], PARAM_MC_SZ_ILD_MAP );
389 : }
390 10250 : set_s( ILD_idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
391 10250 : set_s( ICC_idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ICC_MAP );
392 :
393 : /* update parameter frame index */
394 10250 : hParamMC->hMetadataPMC.param_frame_idx = ( hParamMC->hMetadataPMC.param_frame_idx + 1 ) % PARAM_MC_PARAMETER_FRAMES;
395 :
396 : /* DMX generation*/
397 10250 : ivas_param_mc_dmx( hParamMC, data_f, data_dmx, input_frame, nchan_inp, st_ivas->nchan_transport );
398 :
399 : /* Transient Detector */
400 10250 : switch ( st_ivas->nchan_transport )
401 : {
402 10250 : case 2:
403 : case 3:
404 : case 4:
405 : {
406 : int16_t bAttackPresent[PARAM_MC_MAX_TRANSPORT_CHANS];
407 : int16_t attackIdx[PARAM_MC_MAX_TRANSPORT_CHANS];
408 :
409 10250 : set_s( attackIdx, -1, PARAM_MC_MAX_TRANSPORT_CHANS );
410 10250 : set_s( bAttackPresent, 0, PARAM_MC_MAX_TRANSPORT_CHANS );
411 :
412 30860 : for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
413 : {
414 20610 : int16_t cpe_idx = ch / 2;
415 :
416 20610 : RunTransientDetection( data_dmx[ch], input_frame, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet );
417 :
418 20610 : ivas_param_mc_transient_detection( hParamMC, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet, &bAttackPresent[ch], &attackIdx[ch] );
419 : }
420 :
421 : /* if more than one attack, use the earlier */
422 10250 : hParamMC->hMetadataPMC.bAttackPresent = 0;
423 10250 : hParamMC->hMetadataPMC.attackIndex = 16;
424 :
425 30860 : for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
426 : {
427 20610 : hParamMC->hMetadataPMC.bAttackPresent = max( hParamMC->hMetadataPMC.bAttackPresent, bAttackPresent[ch] );
428 : }
429 :
430 10250 : if ( hParamMC->hMetadataPMC.bAttackPresent )
431 : {
432 1509 : for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
433 : {
434 1016 : hParamMC->hMetadataPMC.attackIndex = min( hParamMC->hMetadataPMC.attackIndex, attackIdx[ch] );
435 : }
436 : }
437 : else
438 : {
439 9757 : hParamMC->hMetadataPMC.attackIndex = 0;
440 : }
441 : }
442 10250 : break;
443 : #ifdef DEBUGGING
444 : default:
445 : assert( !"Number of transport channels not valid for ParamMC!" );
446 : #endif
447 : }
448 :
449 : /* Encoding */
450 : /* parameter estimation*/
451 10250 : ivas_param_mc_param_est_enc( hParamMC, data_f, Cy_sum, Cx_sum, input_frame, nchan_inp, st_ivas->nchan_transport );
452 :
453 10250 : band_step = hParamMC->hMetadataPMC.bAttackPresent ? PARAM_MC_TRANSIENT_BAND_STEP : 1;
454 :
455 :
456 : /* ILD parameter quantization */
457 142341 : for ( k = 0; k < hParamMC->hMetadataPMC.nbands_coded; k += band_step )
458 : {
459 132091 : ivas_param_mc_quantize_ilds( hParamMC, Cy_sum[k], Cx_sum[k], k, nchan_inp, st_ivas->nchan_transport, ILD_idx, ILD_q[k] );
460 : }
461 :
462 : /* ICC parameter quantization */
463 142341 : for ( k = 0; k < hParamMC->hMetadataPMC.nbands_coded; k += band_step )
464 : {
465 132091 : ivas_param_mc_quantize_iccs( hParamMC, Cy_sum[k], k, nchan_inp, ICC_idx );
466 : }
467 :
468 : /* time domain DMX generation*/
469 : /* just copy data_dmx generated above, contains already the downmix */
470 30860 : for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
471 : {
472 20610 : mvr2r( data_dmx[ch], data_f[ch], input_frame );
473 : }
474 :
475 : /* we have to run the transient detector on the second channel of the last CPE if we
476 : have an odd number of transport channels */
477 10250 : if ( st_ivas->nchan_transport > 2 )
478 : {
479 220 : for ( ; ch < st_ivas->nCPE * CPE_CHANNELS; ch++ )
480 : {
481 110 : int16_t cpe_idx = ch / 2;
482 :
483 110 : set_zero( data_f[ch], input_frame );
484 :
485 110 : RunTransientDetection( data_f[ch], input_frame, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet );
486 : }
487 : }
488 :
489 : /* write Parametric MC side info bitstream into temporary buffer*/
490 10250 : ivas_param_mc_write_bs( hParamMC, ILD_idx, ICC_idx, bit_buffer, &bit_pos );
491 :
492 : /* push the Parametric MC side info from the temporary buffer into the medatdata bitstream*/
493 10250 : push_next_bits( hMetaData, bit_buffer, bit_pos );
494 :
495 : /* updates */
496 10250 : hParamMC->hMetadataPMC.last_coded_bwidth = hParamMC->hMetadataPMC.coded_bwidth;
497 :
498 10250 : pop_wmops();
499 :
500 10250 : return;
501 : }
502 :
503 :
504 : /*****************************************************************************************/
505 : /* local functions */
506 : /*****************************************************************************************/
507 :
508 : /*-------------------------------------------------------------------------
509 : * ivas_param_mc_dmx()
510 : *
511 : * Computes the time domain down mix signal
512 : *------------------------------------------------------------------------*/
513 :
514 10250 : static void ivas_param_mc_dmx(
515 : PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle */
516 : float *data_f[], /* i : Input frame */
517 : float data_dmx[][L_FRAME48k], /* o : Down mixed frame */
518 : const int16_t input_frame, /* i : Input frame length */
519 : const int16_t nchan_input, /* i : number of input channels */
520 : const int16_t nchan_transport /* i : number of transport channels */
521 : )
522 : {
523 : int16_t i;
524 : const int16_t *idx;
525 : int16_t dmx_ch;
526 : int16_t inp_ch;
527 : const float *p_dmx_fac;
528 :
529 10250 : idx = Param_MC_index;
530 9741450 : for ( i = 0; i < input_frame; i++ )
531 : {
532 9731200 : p_dmx_fac = hParamMC->dmx_factors;
533 29299200 : for ( dmx_ch = 0; dmx_ch < nchan_transport; dmx_ch++ )
534 : {
535 19568000 : float *dmx_sample = &data_dmx[idx[dmx_ch]][i];
536 19568000 : *dmx_sample = 0.0f;
537 140412800 : for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
538 : {
539 120844800 : ( *dmx_sample ) += data_f[idx[inp_ch]][i] * ( *( p_dmx_fac++ ) );
540 : }
541 : }
542 : }
543 :
544 10250 : return;
545 : }
546 :
547 :
548 : /*-------------------------------------------------------------------------
549 : * ivas_param_mc_param_est_enc()
550 : *
551 : * run the CLDFB analysis on the input signal
552 : * estimate the input and down mix covariances
553 : *------------------------------------------------------------------------*/
554 :
555 10250 : static void ivas_param_mc_param_est_enc(
556 : PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle */
557 : float *data_f[], /* i : Input frame in the time domain */
558 : float Cy_sum[][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* o : Covariance matrix for the original frame */
559 : float Cx_sum[][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], /* o : Covariance matrix for the downmixed frame */
560 : const int16_t input_frame, /* i : Input frame length */
561 : const int16_t nchan_input, /* i : number of input channels */
562 : const int16_t nchan_transport /* i : number of transport channels */
563 : )
564 : {
565 : int16_t i, cur_cldfb_band, cur_param_band, ch_idx1, ch_idx2, inp_ch;
566 : int16_t ts;
567 : int16_t l_ts;
568 : int16_t num_time_slots;
569 : int16_t num_parameter_bands;
570 : int16_t brange[2];
571 : int16_t band_step;
572 10250 : const int16_t *map_ls = Param_MC_index; /* Loudspeakers mapping */
573 : int16_t idx_ls;
574 : float *pcm_in[MAX_CICP_CHANNELS];
575 : float slot_frame_f_real[MAX_CICP_CHANNELS][DIRAC_NO_FB_BANDS_MAX]; /* Output of the MDFT FB - real part */
576 : float slot_frame_f_imag[MAX_CICP_CHANNELS][DIRAC_NO_FB_BANDS_MAX]; /* Output of the MDFT FB - imag part */
577 : float *p_slot_frame_f_real[MAX_CICP_CHANNELS]; /* Output of the MDFT FB - real part */
578 : float *p_slot_frame_f_imag[MAX_CICP_CHANNELS]; /* Output of the MDFT FB - imag part */
579 :
580 : float dmx_real[PARAM_MC_MAX_TRANSPORT_CHANS]; /* Downmix channel - Real Part */
581 : float dmx_imag[PARAM_MC_MAX_TRANSPORT_CHANS]; /* Downmix channel - Imag Part */
582 : float a, b, c, d; /* Tmp complex values */
583 : float Cy_sum_imag[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][MAX_CICP_CHANNELS][MAX_CICP_CHANNELS];
584 : float Cx_sum_imag[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS];
585 : float real_part, imag_part;
586 : int16_t start_ts;
587 : const float *p_dmx_fac;
588 :
589 10250 : push_wmops( "param_mc_prm_est" );
590 :
591 : /* initializations */
592 10250 : l_ts = input_frame / PARAM_MC_MDFT_NO_SLOTS;
593 10250 : num_time_slots = PARAM_MC_MDFT_NO_SLOTS;
594 10250 : start_ts = hParamMC->hMetadataPMC.bAttackPresent ? hParamMC->hMetadataPMC.attackIndex : 0;
595 10250 : num_parameter_bands = hParamMC->hMetadataPMC.nbands_coded;
596 10250 : band_step = 1;
597 :
598 112750 : for ( cur_param_band = 0; cur_param_band < PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC; cur_param_band++ )
599 : {
600 1742500 : for ( ch_idx1 = 0; ch_idx1 < MAX_CICP_CHANNELS; ch_idx1++ )
601 : {
602 1640000 : set_zero( Cy_sum_imag[cur_param_band][ch_idx1], MAX_CICP_CHANNELS );
603 : }
604 :
605 410000 : for ( ch_idx1 = 0; ch_idx1 < PARAM_MC_MAX_TRANSPORT_CHANS; ch_idx1++ )
606 : {
607 307500 : set_zero( Cx_sum_imag[cur_param_band][ch_idx1], PARAM_MC_MAX_TRANSPORT_CHANS );
608 : }
609 : }
610 :
611 : /* Copy current frame to memory for delay compensation */
612 73270 : for ( i = 0; i < nchan_input; i++ )
613 : {
614 63020 : idx_ls = map_ls[i];
615 63020 : pcm_in[i] = data_f[idx_ls];
616 63020 : p_slot_frame_f_real[i] = &slot_frame_f_real[i][0];
617 63020 : p_slot_frame_f_imag[i] = &slot_frame_f_imag[i][0];
618 : }
619 :
620 11919 : for ( ts = 0; ts < start_ts; ts++ )
621 : {
622 1669 : ivas_fb_mixer_update_prior_input( hParamMC->hFbMixer, pcm_in, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
623 12537 : for ( i = 0; i < nchan_input; i++ )
624 : {
625 10868 : pcm_in[i] += l_ts;
626 : }
627 : }
628 :
629 90581 : for ( ts = start_ts; ts < num_time_slots; ts++ )
630 : {
631 80331 : ivas_fb_mixer_get_windowed_fr( hParamMC->hFbMixer, pcm_in, p_slot_frame_f_real, p_slot_frame_f_imag, l_ts, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
632 :
633 80331 : ivas_fb_mixer_update_prior_input( hParamMC->hFbMixer, pcm_in, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
634 :
635 573623 : for ( i = 0; i < nchan_input; i++ )
636 : {
637 493292 : pcm_in[i] += l_ts;
638 : }
639 : /* Computing the downmix */
640 693677 : for ( cur_param_band = 0; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band++ )
641 : {
642 613346 : brange[0] = hParamMC->band_grouping[cur_param_band];
643 613346 : brange[1] = hParamMC->band_grouping[cur_param_band + 1];
644 :
645 2219966 : for ( cur_cldfb_band = brange[0]; cur_cldfb_band < brange[1]; cur_cldfb_band++ )
646 : {
647 : /* Cx for DMX */
648 : /* Real Part */
649 1606620 : p_dmx_fac = hParamMC->dmx_factors;
650 :
651 4835620 : for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
652 : {
653 3229000 : dmx_real[ch_idx1] = 0.0f;
654 3229000 : dmx_imag[ch_idx1] = 0.0f;
655 23132520 : for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
656 : {
657 19903520 : dmx_real[ch_idx1] += slot_frame_f_real[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
658 19903520 : dmx_imag[ch_idx1] += slot_frame_f_imag[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
659 19903520 : p_dmx_fac++;
660 : }
661 : }
662 :
663 : /* Cx for transport channels */
664 4835620 : for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
665 : {
666 9734280 : for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
667 : {
668 6505280 : a = dmx_real[ch_idx1];
669 6505280 : b = dmx_imag[ch_idx1];
670 6505280 : c = dmx_real[ch_idx2];
671 6505280 : d = dmx_imag[ch_idx2];
672 :
673 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
674 6505280 : Cx_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
675 6505280 : Cx_sum_imag[cur_param_band][ch_idx1][ch_idx2] += a * d - b * c;
676 : }
677 : }
678 :
679 : /* Cy for input channels */
680 11472460 : for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
681 : {
682 45438040 : for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
683 : {
684 35572200 : a = slot_frame_f_real[ch_idx1][cur_cldfb_band];
685 35572200 : b = slot_frame_f_imag[ch_idx1][cur_cldfb_band];
686 35572200 : c = slot_frame_f_real[ch_idx2][cur_cldfb_band];
687 35572200 : d = slot_frame_f_imag[ch_idx2][cur_cldfb_band];
688 :
689 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
690 35572200 : Cy_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
691 35572200 : Cy_sum_imag[cur_param_band][ch_idx1][ch_idx2] += a * d - b * c;
692 : }
693 : }
694 : }
695 : }
696 :
697 527568 : for ( ; cur_param_band < num_parameter_bands; cur_param_band++ )
698 : {
699 447237 : brange[0] = hParamMC->band_grouping[cur_param_band];
700 447237 : brange[1] = hParamMC->band_grouping[cur_param_band + 1];
701 :
702 8272777 : for ( cur_cldfb_band = brange[0]; cur_cldfb_band < brange[1]; cur_cldfb_band++ )
703 : {
704 : /* Cx for DMX */
705 : /* Real Part */
706 7825540 : p_dmx_fac = hParamMC->dmx_factors;
707 :
708 23555420 : for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
709 : {
710 15729880 : dmx_real[ch_idx1] = 0.0f;
711 15729880 : dmx_imag[ch_idx1] = 0.0f;
712 :
713 112756760 : for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
714 : {
715 97026880 : dmx_real[ch_idx1] += slot_frame_f_real[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
716 97026880 : dmx_imag[ch_idx1] += slot_frame_f_imag[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
717 97026880 : p_dmx_fac++;
718 : }
719 : }
720 :
721 : /* Cx for transport channels */
722 23555420 : for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
723 : {
724 47426040 : for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
725 : {
726 31696160 : a = dmx_real[ch_idx1];
727 31696160 : b = dmx_imag[ch_idx1];
728 31696160 : c = dmx_real[ch_idx2];
729 31696160 : d = dmx_imag[ch_idx2];
730 :
731 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
732 31696160 : Cx_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
733 : }
734 : }
735 :
736 : /* Cy for input channels */
737 55909380 : for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
738 : {
739 221586080 : for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
740 : {
741 173502240 : a = slot_frame_f_real[ch_idx1][cur_cldfb_band];
742 173502240 : b = slot_frame_f_imag[ch_idx1][cur_cldfb_band];
743 173502240 : c = slot_frame_f_real[ch_idx2][cur_cldfb_band];
744 173502240 : d = slot_frame_f_imag[ch_idx2][cur_cldfb_band];
745 :
746 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
747 173502240 : Cy_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
748 : }
749 : }
750 : }
751 : }
752 : }
753 :
754 : /* make sure energy and correlation is zero above the relevant LFE bands for LFE
755 : * avoids wrong energy in case of band combining at transients */
756 10250 : if ( hParamMC->lfe_index >= 0 )
757 : {
758 78220 : for ( cur_param_band = PARAM_MC_MAX_BAND_LFE; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band++ )
759 : {
760 486350 : for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
761 : {
762 418380 : Cy_sum[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
763 418380 : Cy_sum[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
764 418380 : Cy_sum_imag[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
765 418380 : Cy_sum_imag[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
766 : }
767 : }
768 :
769 67260 : for ( ; cur_param_band < num_parameter_bands; cur_param_band++ )
770 : {
771 408230 : for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
772 : {
773 351220 : Cy_sum[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
774 351220 : Cy_sum[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
775 : }
776 : }
777 : }
778 :
779 10250 : if ( !hParamMC->hMetadataPMC.bAttackPresent )
780 : {
781 : const PARAM_MC_ILD_MAPPING *h_ild_mapping;
782 : int16_t ild_attack;
783 9757 : ild_attack = 0;
784 9757 : h_ild_mapping = hParamMC->hMetadataPMC.ild_mapping_conf;
785 : /* create ILDs for to non transmitted parameter bands (only lower half) */
786 74555 : for ( cur_param_band = 0; cur_param_band < hParamMC->hMetadataPMC.num_parameter_bands / 2; cur_param_band++ )
787 : {
788 : float ILD[PARAM_MC_SZ_ILD_MAP];
789 : int16_t k;
790 : int16_t num_ilds_to_code;
791 :
792 64798 : if ( cur_param_band >= PARAM_MC_MAX_BAND_LFE )
793 : {
794 55041 : num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
795 : }
796 : else
797 : {
798 9757 : num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
799 : }
800 64798 : if ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[cur_param_band] )
801 : {
802 : float Nrg[MAX_CICP_CHANNELS];
803 :
804 : /* get ICLDs */
805 231161 : for ( k = 0; k < nchan_input; ++k )
806 : {
807 198792 : Nrg[k] = Cy_sum[cur_param_band][k][k];
808 : }
809 203355 : for ( k = 0; k < num_ilds_to_code; ++k )
810 : {
811 170986 : float ref_ener = 0.0f;
812 : int16_t ref_channel_cnt;
813 : int16_t ref_channel_idx;
814 :
815 378816 : for ( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
816 : {
817 207830 : ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
818 207830 : ref_ener += Cx_sum[cur_param_band][ref_channel_idx][ref_channel_idx];
819 : }
820 170986 : ILD[k] = 10.0f * log10f( ( Nrg[h_ild_mapping->ild_index[k]] + EPSILON ) / ( hParamMC->hMetadataPMC.ild_factors[k] * ref_ener + EPSILON ) );
821 170986 : if ( hParamMC->prev_ilds[cur_param_band][k] - ILD[k] > param_mc_ild_diff_threshold[cur_param_band] )
822 : {
823 306 : ild_attack++;
824 : }
825 : }
826 : }
827 : }
828 : /* check if the ILDs change too much -> go into transient mode... */
829 9757 : if ( ild_attack > PARAM_MC_NUM_ATTACK_ILD_THRESH )
830 : {
831 0 : hParamMC->hMetadataPMC.bAttackPresent = 1;
832 : }
833 : }
834 :
835 :
836 10250 : if ( hParamMC->hMetadataPMC.bAttackPresent )
837 : {
838 : /* combine bands */
839 2337 : for ( cur_param_band = 1; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band += 2 )
840 : {
841 5659 : for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
842 : {
843 11826 : for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
844 : {
845 8011 : Cx_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cx_sum[cur_param_band][ch_idx1][ch_idx2];
846 8011 : Cx_sum_imag[cur_param_band - 1][ch_idx1][ch_idx2] += Cx_sum_imag[cur_param_band][ch_idx1][ch_idx2];
847 : }
848 : }
849 :
850 14012 : for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
851 : {
852 60276 : for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
853 : {
854 48108 : Cy_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cy_sum[cur_param_band][ch_idx1][ch_idx2];
855 48108 : Cy_sum_imag[cur_param_band - 1][ch_idx1][ch_idx2] += Cy_sum_imag[cur_param_band][ch_idx1][ch_idx2];
856 : }
857 : }
858 : }
859 :
860 1788 : for ( ; cur_param_band < num_parameter_bands; cur_param_band += 2 )
861 : {
862 1295 : if ( cur_param_band < num_parameter_bands )
863 : {
864 3989 : for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
865 : {
866 8394 : for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
867 : {
868 5700 : Cx_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cx_sum[cur_param_band][ch_idx1][ch_idx2];
869 : }
870 : }
871 :
872 9905 : for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
873 : {
874 42933 : for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
875 : {
876 34323 : Cy_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cy_sum[cur_param_band][ch_idx1][ch_idx2];
877 : }
878 : }
879 : }
880 : }
881 :
882 493 : band_step = 2;
883 : }
884 :
885 :
886 : /* map complex covariances to real values */
887 86626 : for ( cur_param_band = 0; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band += band_step )
888 : {
889 : /* Cx for transport channels */
890 229941 : for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ch_idx1++ )
891 : {
892 463134 : for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ch_idx2++ )
893 : {
894 309569 : real_part = Cx_sum[cur_param_band][ch_idx1][ch_idx2];
895 309569 : imag_part = Cx_sum_imag[cur_param_band][ch_idx1][ch_idx2];
896 :
897 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
898 309569 : Cx_sum[cur_param_band][ch_idx1][ch_idx2] = sqrtf( real_part * real_part + imag_part * imag_part );
899 : }
900 : }
901 :
902 : /* Cy for transport channels */
903 545608 : for ( ch_idx1 = 0; ch_idx1 < nchan_input; ch_idx1++ )
904 : {
905 2162024 : for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ch_idx2++ )
906 : {
907 1692792 : real_part = Cy_sum[cur_param_band][ch_idx1][ch_idx2];
908 1692792 : imag_part = Cy_sum_imag[cur_param_band][ch_idx1][ch_idx2];
909 1692792 : Cy_sum[cur_param_band][ch_idx1][ch_idx2] = sqrtf( real_part * real_part + imag_part * imag_part );
910 : }
911 : }
912 : }
913 :
914 10250 : if ( Cy_sum[0][LFE_CHANNEL][LFE_CHANNEL] < PARAM_MC_LFE_ON_THRESH )
915 : {
916 9181 : hParamMC->hMetadataPMC.lfe_on = 0;
917 : }
918 : else
919 : {
920 1069 : hParamMC->hMetadataPMC.lfe_on = 1;
921 : }
922 :
923 10250 : pop_wmops();
924 :
925 10250 : return;
926 : }
927 :
928 :
929 : /*-------------------------------------------------------------------------
930 : * ivas_param_mc_quantize_ilds()
931 : *
932 : * Quantize the ILD parameters
933 : *------------------------------------------------------------------------*/
934 :
935 132091 : static void ivas_param_mc_quantize_ilds(
936 : PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle */
937 : float Cy[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* i : Covariance matrix of the input */
938 : float Cx[PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], /* i : Covariance matrix of the dmx */
939 : const int16_t freq_idx, /* i : frequency index being processed */
940 : const int16_t nchan_input, /* i : number of input channels */
941 : const int16_t nchan_transport, /* i : number of transport channels */
942 : int16_t *ILD_idx_out, /* o : ILD indices */
943 : float ILD_q[PARAM_MC_SZ_ILD_MAP] /* o : Quanzited ILD matrix */
944 : )
945 : {
946 : int16_t k;
947 : int16_t Ny;
948 : int16_t num_ilds_to_code;
949 : int16_t ild_map_size;
950 : float Nrg[MAX_CICP_CHANNELS];
951 : float ILD[PARAM_MC_SZ_ILD_MAP];
952 : const PARAM_MC_ILD_MAPPING *h_ild_mapping;
953 : float tot_ener, dmx_ener, ener_fac, delta_fac;
954 : int16_t ILD_idx[PARAM_MC_SZ_ILD_MAP];
955 :
956 132091 : push_wmops( "param_mc_prm_q" );
957 :
958 : /* Initialization */
959 132091 : set_zero( Nrg, MAX_CICP_CHANNELS );
960 132091 : set_zero( ILD, PARAM_MC_SZ_ILD_MAP );
961 :
962 132091 : Ny = nchan_input;
963 :
964 132091 : h_ild_mapping = hParamMC->hMetadataPMC.ild_mapping_conf;
965 132091 : ild_map_size = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
966 132091 : if ( freq_idx >= PARAM_MC_MAX_BAND_LFE )
967 : {
968 121841 : num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
969 : }
970 : else
971 : {
972 10250 : num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
973 : }
974 :
975 : /* Downsampling */
976 132091 : if ( ( hParamMC->hMetadataPMC.bAttackPresent == 0 ) && ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[freq_idx] ) )
977 : {
978 64440 : pop_wmops();
979 :
980 64440 : return;
981 : }
982 :
983 : /* get ICLDs */
984 484191 : for ( k = 0; k < Ny; ++k )
985 : {
986 416540 : Nrg[k] = Cy[k][k];
987 : }
988 :
989 : /* limit ILDs if DMX energy is lower than sum of channel energies */
990 67651 : tot_ener = 0.0f;
991 67651 : dmx_ener = 0.0f;
992 :
993 484191 : for ( k = 0; k < ild_map_size; k++ )
994 : {
995 416540 : if ( k != hParamMC->lfe_index || hParamMC->hMetadataPMC.lfe_on )
996 : {
997 356156 : tot_ener += Nrg[k];
998 : }
999 : }
1000 :
1001 203820 : for ( k = 0; k < nchan_transport; k++ )
1002 : {
1003 136169 : dmx_ener += Cx[k][k];
1004 : }
1005 67651 : ener_fac = 10.0f * log10f( ( tot_ener + EPSILON ) / ( dmx_ener + EPSILON ) );
1006 :
1007 67651 : if ( ener_fac > PARAM_MC_ENER_LIMIT_INTRAFRAME )
1008 : {
1009 : float limit_fac;
1010 18 : limit_fac = powf( 10.0f, ( ( 0.3f * logf( ener_fac - PARAM_MC_ENER_LIMIT_INTRAFRAME + 1.0f ) - ( ener_fac - PARAM_MC_ENER_LIMIT_INTRAFRAME ) ) / 10.0f ) );
1011 18 : v_multc( Nrg, limit_fac, Nrg, num_ilds_to_code );
1012 : }
1013 :
1014 : /* limit ILD jumps in non-tranient frames */
1015 67651 : tot_ener = 0.0f;
1016 67651 : dmx_ener = 0.0f;
1017 :
1018 484191 : for ( k = 0; k < ild_map_size; k++ )
1019 : {
1020 416540 : if ( k != hParamMC->lfe_index || hParamMC->hMetadataPMC.lfe_on )
1021 : {
1022 356156 : tot_ener += Nrg[k];
1023 : }
1024 : }
1025 :
1026 203820 : for ( k = 0; k < nchan_transport; k++ )
1027 : {
1028 136169 : dmx_ener += Cx[k][k];
1029 : }
1030 :
1031 67651 : ener_fac = 10.0f * log10f( ( tot_ener + EPSILON ) / ( dmx_ener + EPSILON ) );
1032 67651 : delta_fac = ener_fac - hParamMC->ener_fac[freq_idx];
1033 :
1034 67651 : if ( !hParamMC->hMetadataPMC.bAttackPresent && ( delta_fac > PARAM_MC_ENER_LIMIT_INTERFRAME ) && ( delta_fac < PARAM_MC_ENER_LIMIT_MAX_DELTA_FAC ) )
1035 : {
1036 : float limit_fac;
1037 424 : limit_fac = powf( 10.0f, ( ( 0.3f * logf( delta_fac - PARAM_MC_ENER_LIMIT_INTERFRAME + 1.0f ) - ( delta_fac - PARAM_MC_ENER_LIMIT_INTERFRAME ) ) / 10.0f ) );
1038 424 : v_multc( Nrg, limit_fac, Nrg, num_ilds_to_code );
1039 424 : ener_fac += 10.0f * log10f( limit_fac );
1040 : }
1041 :
1042 67651 : hParamMC->ener_fac[freq_idx] = ener_fac;
1043 :
1044 : /* update also combined bands ener_fac when in transient frame */
1045 67651 : if ( hParamMC->hMetadataPMC.bAttackPresent && ( ( freq_idx + 1 ) < hParamMC->hMetadataPMC.nbands_coded ) )
1046 : {
1047 3139 : hParamMC->ener_fac[freq_idx + 1] = ener_fac;
1048 : }
1049 :
1050 421124 : for ( k = 0; k < num_ilds_to_code; ++k )
1051 : {
1052 353473 : float ref_ener = 0.0f;
1053 : int16_t ref_channel_cnt;
1054 : int16_t ref_channel_idx;
1055 :
1056 779049 : for ( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
1057 : {
1058 425576 : ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
1059 425576 : ref_ener += Cx[ref_channel_idx][ref_channel_idx];
1060 : }
1061 353473 : ILD[k] = 10.0f * log10f( ( Nrg[h_ild_mapping->ild_index[k]] + EPSILON ) / ( hParamMC->hMetadataPMC.ild_factors[k] * ref_ener + EPSILON ) );
1062 353473 : hParamMC->prev_ilds[freq_idx][k] = ILD[k];
1063 353473 : if ( hParamMC->hMetadataPMC.bAttackPresent && ( ( freq_idx + 1 ) < hParamMC->hMetadataPMC.nbands_coded ) )
1064 : {
1065 17931 : hParamMC->prev_ilds[freq_idx + 1][k] = ILD[k];
1066 : }
1067 : }
1068 :
1069 :
1070 : /* quantize parameters */
1071 67651 : ivas_param_mc_parameter_quantizer( ILD, num_ilds_to_code, hParamMC->hMetadataPMC.ild_coding.quantizer_size, hParamMC->hMetadataPMC.ild_coding.quantizer, ILD_idx, ILD_q );
1072 :
1073 : /* Save current quantized ICLDs */
1074 67651 : mvs2s( ILD_idx, ILD_idx_out + freq_idx * ild_map_size, num_ilds_to_code );
1075 :
1076 67651 : pop_wmops();
1077 :
1078 67651 : return;
1079 : }
1080 :
1081 :
1082 : /*-------------------------------------------------------------------------
1083 : * ivas_param_mc_quantize_iccs()
1084 : *
1085 : * Quantize the ILD parameters
1086 : *------------------------------------------------------------------------*/
1087 :
1088 132091 : static void ivas_param_mc_quantize_iccs(
1089 : PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle */
1090 : float Cy[MAX_CICP_CHANNELS][MAX_CICP_CHANNELS], /* i : Covariance matrix of the input */
1091 : const int16_t freq_idx, /* i : frequency index being processed */
1092 : const int16_t nchan_input, /* i : number of input channels */
1093 : int16_t *ICC_idx_out /* o : quantizer indices */
1094 : )
1095 : {
1096 : int16_t i, k;
1097 : int16_t Ny;
1098 : int16_t num_iccs_to_code;
1099 : int16_t icc_map_size;
1100 : float a;
1101 : int16_t tmp_map[2];
1102 : float Nrg[MAX_CICP_CHANNELS];
1103 : float ICC_vect[PARAM_MC_SZ_ICC_MAP];
1104 : float ICC_vect_q[PARAM_MC_SZ_ICC_MAP];
1105 : int16_t ICC_idx[PARAM_MC_SZ_ICC_MAP];
1106 :
1107 : /* Initialization */
1108 132091 : set_zero( Nrg, MAX_CICP_CHANNELS );
1109 132091 : set_zero( ICC_vect, PARAM_MC_SZ_ICC_MAP );
1110 132091 : set_zero( ICC_vect_q, PARAM_MC_SZ_ICC_MAP );
1111 :
1112 132091 : Ny = nchan_input;
1113 :
1114 : /* Downsampling */
1115 132091 : if ( ( hParamMC->hMetadataPMC.bAttackPresent == 0 ) && ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[freq_idx] ) )
1116 : {
1117 64440 : return;
1118 : }
1119 :
1120 67651 : icc_map_size = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe;
1121 67651 : num_iccs_to_code = icc_map_size;
1122 :
1123 67651 : if ( freq_idx >= PARAM_MC_MAX_BAND_LFE )
1124 : {
1125 62266 : num_iccs_to_code = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_wo_lfe;
1126 : }
1127 :
1128 : /* Get ICC matrix from Cy */
1129 484191 : for ( k = 0; k < Ny; ++k )
1130 : {
1131 416540 : Nrg[k] = Cy[k][k];
1132 416540 : a = 1.0f / ( sqrtf( Nrg[k] ) + EPSILON );
1133 :
1134 1923686 : for ( i = k; i < Ny; ++i )
1135 : {
1136 1507146 : Cy[k][i] = Cy[k][i] * a;
1137 : }
1138 :
1139 1923686 : for ( i = 0; i <= k; i++ )
1140 : {
1141 1507146 : Cy[i][k] = Cy[i][k] * a;
1142 : }
1143 : }
1144 :
1145 : /* set ICCs for zero channels to 1 to avoid artifacts in the decoded signal */
1146 484191 : for ( k = 0; k < Ny; ++k )
1147 : {
1148 416540 : if ( Nrg[k] == 0.0f )
1149 : {
1150 258832 : for ( i = k; i < Ny; ++i )
1151 : {
1152 196566 : Cy[k][i] = 1.0f;
1153 : }
1154 :
1155 311330 : for ( i = 0; i <= k; ++i )
1156 : {
1157 249064 : Cy[i][k] = 1.0f;
1158 : }
1159 : }
1160 : }
1161 :
1162 : /* Reduce set of parameters and quantize them */
1163 354274 : for ( k = 0; k < num_iccs_to_code; ++k )
1164 : {
1165 286623 : tmp_map[0] = hParamMC->hMetadataPMC.icc_mapping_conf->icc_mapping[k][0];
1166 286623 : tmp_map[1] = hParamMC->hMetadataPMC.icc_mapping_conf->icc_mapping[k][1];
1167 286623 : ICC_vect[k] = Cy[tmp_map[0]][tmp_map[1]];
1168 : }
1169 :
1170 : /* Quantization */
1171 67651 : ivas_param_mc_parameter_quantizer( ICC_vect, num_iccs_to_code, hParamMC->hMetadataPMC.icc_coding.quantizer_size, hParamMC->hMetadataPMC.icc_coding.quantizer, ICC_idx, ICC_vect_q );
1172 :
1173 : /* Save current quantized ICCs */
1174 67651 : mvs2s( ICC_idx, ICC_idx_out + freq_idx * icc_map_size, num_iccs_to_code );
1175 :
1176 67651 : return;
1177 : }
1178 :
1179 :
1180 : /*-------------------------------------------------------------------------
1181 : * ivas_param_mc_parameter_quantizer()
1182 : *
1183 : * Parameter Quantization
1184 : *------------------------------------------------------------------------*/
1185 :
1186 135302 : static void ivas_param_mc_parameter_quantizer(
1187 : const float *x, /* i : input sequence */
1188 : const int16_t L, /* i : input length */
1189 : const int16_t sz_quantizer, /* i : quantizer size */
1190 : const float *quantizer, /* i : quantizer table */
1191 : int16_t *quant_idx, /* o : quant indices */
1192 : float *y /* o : output sequence */
1193 : )
1194 : {
1195 : int16_t idx, i;
1196 : int16_t idx_min;
1197 : float tmp_min;
1198 :
1199 135302 : set_f( y, 0.0f, L );
1200 135302 : idx_min = 0;
1201 :
1202 775398 : for ( idx = 0; idx < L; ++idx )
1203 : {
1204 640096 : tmp_min = 1000.0f;
1205 8588648 : for ( i = 0; i < sz_quantizer; ++i )
1206 : {
1207 7948552 : if ( fabsf( x[idx] - quantizer[i] ) < tmp_min )
1208 : {
1209 4184801 : tmp_min = fabsf( x[idx] - quantizer[i] );
1210 4184801 : idx_min = i;
1211 : }
1212 : }
1213 :
1214 640096 : y[idx] = quantizer[idx_min];
1215 640096 : quant_idx[idx] = idx_min;
1216 : }
1217 :
1218 135302 : return;
1219 : }
1220 :
1221 :
1222 : /*-------------------------------------------------------------------------
1223 : * ivas_param_mc_transient_detection()
1224 : *
1225 : * Detect if the current frame has a transient
1226 : *------------------------------------------------------------------------*/
1227 :
1228 20610 : static void ivas_param_mc_transient_detection(
1229 : PARAM_MC_ENC_HANDLE hParamMC, /* i : Parametric MC encoder handle */
1230 : TRAN_DET_HANDLE hTranDet, /* i : Transient detector handle from core coder for a transport channel */
1231 : int16_t *pbIsAttackPresent, /* o : Flag for indicating a found transient */
1232 : int16_t *pAttackIndex /* o : Attack position (0 if no attack) */
1233 : )
1234 : {
1235 : int16_t i;
1236 : int16_t bIsAttackPresent, attackIndex;
1237 : float *pSubblockNrg;
1238 : float *pAccSubblockNrg;
1239 : float attackRatioThreshold;
1240 :
1241 20610 : push_wmops( "param_mc_trn_det" );
1242 :
1243 20610 : attackRatioThreshold = hTranDet->transientDetector.attackRatioThreshold;
1244 20610 : pSubblockNrg = &hTranDet->subblockEnergies.subblockNrg[hParamMC->transient_detector_delay];
1245 20610 : pAccSubblockNrg = &hTranDet->subblockEnergies.accSubblockNrg[hParamMC->transient_detector_delay];
1246 :
1247 20610 : bIsAttackPresent = FALSE;
1248 20610 : attackIndex = 16;
1249 :
1250 : /* Search for the last attack in the subblocks,
1251 : * if we had an attack very late in the last frame,
1252 : * make the current frame also a transient one... */
1253 20610 : if ( ( pSubblockNrg[-1] > pAccSubblockNrg[-1] * attackRatioThreshold ) || ( pSubblockNrg[-2] > pAccSubblockNrg[-2] * attackRatioThreshold ) )
1254 : {
1255 71 : bIsAttackPresent = TRUE;
1256 71 : attackIndex = 0;
1257 : }
1258 :
1259 185490 : for ( i = 0; i < NSUBBLOCKS; i++ )
1260 : {
1261 164880 : if ( pSubblockNrg[i] > pAccSubblockNrg[i] * attackRatioThreshold )
1262 : {
1263 779 : bIsAttackPresent = TRUE;
1264 779 : attackIndex = i;
1265 : }
1266 : }
1267 :
1268 : /* avoid post-echos on click sounds (very short transients) due to TNS aliasing */
1269 20610 : *pAttackIndex = attackIndex;
1270 20610 : *pbIsAttackPresent = bIsAttackPresent;
1271 :
1272 20610 : pop_wmops();
1273 :
1274 20610 : return;
1275 : }
1276 :
1277 :
1278 : /*-------------------------------------------------------------------------
1279 : * ivas_param_mc_entropy_encoder()
1280 : *
1281 : * Write the metadata bitstream
1282 : *------------------------------------------------------------------------*/
1283 :
1284 10250 : static void ivas_param_mc_write_bs(
1285 : const PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder Handle */
1286 : int16_t *ILD_idx, /* i : ILD quantizer indices sequence */
1287 : int16_t *ICC_idx, /* i : ICC quantizer indices sequence */
1288 : uint16_t bit_buffer[PARAM_MC_MAX_BITS], /* o : Output bit buffer */
1289 : int16_t *bit_pos /* o : Number of bits used */
1290 : )
1291 : {
1292 : int16_t i, pos;
1293 : int16_t nbands;
1294 : int16_t band_step;
1295 : int16_t seq_tmp[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
1296 : float seq_tmp_uni[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
1297 : int16_t icc_map_size_wo_lfe;
1298 : int16_t icc_map_size;
1299 : int16_t ild_map_size_wo_lfe;
1300 : int16_t ild_map_size;
1301 :
1302 10250 : push_wmops( "param_mc_prm_enc" );
1303 :
1304 : /* Init */
1305 10250 : set_zero( seq_tmp_uni, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
1306 10250 : set_s( seq_tmp, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
1307 10250 : nbands = hParamMC->hMetadataPMC.nbands_in_param_frame[hParamMC->hMetadataPMC.param_frame_idx];
1308 10250 : icc_map_size_wo_lfe = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_wo_lfe;
1309 10250 : icc_map_size = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe;
1310 10250 : ild_map_size_wo_lfe = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
1311 10250 : ild_map_size = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
1312 :
1313 : /*-----------------------------------------------------------------*
1314 : * Signaling bits
1315 : *-----------------------------------------------------------------*/
1316 :
1317 : /* reserved bit */
1318 10250 : bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.lfe_on;
1319 :
1320 : /* write coded band width */
1321 10250 : i = hParamMC->hMetadataPMC.coded_bwidth;
1322 30750 : for ( pos = 0; pos < 2; pos++ )
1323 : {
1324 20500 : bit_buffer[( *bit_pos )++] = (uint16_t) ( ( i >> pos ) & 1 );
1325 : }
1326 :
1327 : /* write param frame indicator */
1328 10250 : bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.param_frame_idx;
1329 :
1330 : /* write transient frame indicator */
1331 10250 : bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.bAttackPresent;
1332 :
1333 10250 : band_step = 1;
1334 10250 : if ( hParamMC->hMetadataPMC.bAttackPresent )
1335 : {
1336 493 : band_step = PARAM_MC_TRANSIENT_BAND_STEP;
1337 1972 : for ( pos = 2; pos >= 0; --pos )
1338 : {
1339 1479 : bit_buffer[( *bit_pos )++] = (uint16_t) ( ( hParamMC->hMetadataPMC.attackIndex >> pos ) & 1 );
1340 : }
1341 493 : nbands = hParamMC->hMetadataPMC.nbands_coded / band_step + ( ( hParamMC->hMetadataPMC.nbands_coded % band_step ) ? 1 : 0 );
1342 : }
1343 :
1344 10250 : ivas_param_mc_encode_parameter( ICC_idx, &hParamMC->hMetadataPMC, &hParamMC->hMetadataPMC.icc_coding,
1345 : nbands, band_step, icc_map_size_wo_lfe, icc_map_size, bit_buffer, bit_pos );
1346 :
1347 10250 : ivas_param_mc_encode_parameter( ILD_idx, &hParamMC->hMetadataPMC, &hParamMC->hMetadataPMC.ild_coding,
1348 : nbands, band_step, ild_map_size_wo_lfe, ild_map_size, bit_buffer, bit_pos );
1349 10250 : pop_wmops();
1350 :
1351 10250 : return;
1352 : }
1353 :
1354 :
1355 : /*-------------------------------------------------------------------------
1356 : * ivas_param_mc_encode_parameter()
1357 : *
1358 : * (entropy) encode a sequence of parameter indices
1359 : *------------------------------------------------------------------------*/
1360 :
1361 20500 : static void ivas_param_mc_encode_parameter(
1362 : int16_t *quant_idx, /* i : indices sequence to encode */
1363 : HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC, /* i : Parametric MC metadata handle */
1364 : HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParameterCodingInfo, /* i : parameter quantization and coding info */
1365 : const int16_t nbands, /* i : number of parameter bands to encode */
1366 : const int16_t band_step, /* i : parameter band step */
1367 : const int16_t map_size_wo_lfe, /* i : number of parameters per band (w/o LFE) */
1368 : const int16_t map_size, /* i : number of parameters per band */
1369 : uint16_t bit_buffer[PARAM_MC_MAX_BITS], /* o : Output bit buffer */
1370 : int16_t *bit_pos /* o : Number of bits used */
1371 : )
1372 : {
1373 : int16_t sz_seq;
1374 : int16_t idx_prev;
1375 : int16_t idx_offset;
1376 : int16_t bit_cnt_uni;
1377 : int16_t bit_cnt_range;
1378 : int16_t bit_cnt_range_diff;
1379 : int16_t bit_cnt_range_min;
1380 : int16_t bit_pos_tmp;
1381 : int16_t i, j;
1382 : int16_t idx;
1383 : int16_t seq_delta[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
1384 : int16_t seq[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
1385 : uint16_t tmp_bit_buffer[PARAM_MC_MAX_BITS];
1386 : uint16_t tmp_bit_buffer_diff[PARAM_MC_MAX_BITS];
1387 :
1388 : /* Inits */
1389 20500 : sz_seq = nbands * ( map_size_wo_lfe );
1390 :
1391 : /* Computing Delta Sequence */
1392 20500 : idx_prev = hParameterCodingInfo->quantizer_size / 2 + hParameterCodingInfo->quantizer_size % 2 - 1;
1393 20500 : idx_offset = hParameterCodingInfo->quantizer_size - 1;
1394 :
1395 115680 : for ( j = 0; j < map_size_wo_lfe; ++j )
1396 : {
1397 95180 : int16_t coding_band = 0;
1398 :
1399 1321102 : for ( i = 0; i < hMetadataPMC->nbands_coded; i += band_step )
1400 : {
1401 1225922 : if ( hMetadataPMC->bAttackPresent || hMetadataPMC->param_frame_idx == hMetadataPMC->coding_band_mapping[i] )
1402 : {
1403 629260 : idx = quant_idx[i * map_size + j];
1404 629260 : seq[coding_band + j * nbands] = idx;
1405 629260 : seq_delta[coding_band + j * nbands] = idx - idx_prev + idx_offset;
1406 629260 : idx_prev = idx;
1407 629260 : coding_band++;
1408 : }
1409 : }
1410 : }
1411 :
1412 : /* LFE */
1413 20500 : if ( hMetadataPMC->lfe_on )
1414 : {
1415 4276 : for ( i = 0; i < PARAM_MC_MAX_BAND_LFE; i += band_step )
1416 : {
1417 2138 : if ( hMetadataPMC->bAttackPresent || hMetadataPMC->param_frame_idx == hMetadataPMC->coding_band_mapping[i] )
1418 : {
1419 : /* LFE ICC/ILDs are always the last ones in coding band 0 */
1420 : int16_t n_lfe_idx, k;
1421 1172 : n_lfe_idx = map_size - map_size_wo_lfe;
1422 2410 : for ( k = 0; k < n_lfe_idx; k++ )
1423 : {
1424 1238 : idx = quant_idx[( i + 1 ) * map_size - n_lfe_idx + k];
1425 1238 : seq[sz_seq] = idx;
1426 1238 : seq_delta[sz_seq] = idx - idx_prev + idx_offset;
1427 1238 : idx_prev = idx;
1428 1238 : sz_seq++;
1429 : }
1430 : }
1431 : }
1432 : }
1433 :
1434 : #ifdef DEBUG_MODE_PARAM_MC
1435 : dbgwrite( seq, sizeof( int16_t ), sz_seq, 1, "./res/param_mc_quant_param_idx_enc.dat" );
1436 : #endif
1437 :
1438 20500 : bit_cnt_uni = sz_seq * hParameterCodingInfo->uni_bits - 1; /* -1 for the additional diff/direct signaling bit for the range encoder*/
1439 :
1440 : /* code the direct index sequence */
1441 20500 : ivas_param_mc_range_encoder( seq, sz_seq, hParameterCodingInfo->cum_freq, hParameterCodingInfo->sym_freq, PARAM_MC_RANGE_CODER_TOT_SHIFT, bit_cnt_uni, &tmp_bit_buffer[0], &bit_cnt_range );
1442 :
1443 : /* Coding the delta index sequence */
1444 20500 : ivas_param_mc_range_encoder( seq_delta, sz_seq, hParameterCodingInfo->cum_freq_delta, hParameterCodingInfo->sym_freq_delta, PARAM_MC_RANGE_CODER_TOT_SHIFT, bit_cnt_uni, &tmp_bit_buffer_diff[0], &bit_cnt_range_diff );
1445 :
1446 20500 : bit_cnt_range_min = min( bit_cnt_range, bit_cnt_range_diff );
1447 :
1448 : /* uniform fallback */
1449 20500 : if ( bit_cnt_range_min > bit_cnt_uni )
1450 : {
1451 : /* Uniform coding is used */
1452 467 : bit_buffer[( *bit_pos )++] = 0;
1453 467 : bit_pos_tmp = 0;
1454 :
1455 12562 : for ( i = 0; i < sz_seq; ++i )
1456 : {
1457 12095 : ivas_param_mc_dec2bin( seq[i], hParameterCodingInfo->uni_bits, &bit_buffer[*( bit_pos ) + bit_pos_tmp] );
1458 12095 : bit_pos_tmp += hParameterCodingInfo->uni_bits;
1459 : }
1460 467 : *bit_pos = *bit_pos + bit_pos_tmp;
1461 : }
1462 : else
1463 : {
1464 : /* Range Coding is used */
1465 20033 : bit_buffer[( *bit_pos )++] = 1;
1466 20033 : if ( bit_cnt_range_diff < bit_cnt_range )
1467 : {
1468 19778 : bit_buffer[( *bit_pos )++] = 1;
1469 1356582 : for ( i = 0; i < bit_cnt_range_diff; i++ )
1470 : {
1471 1336804 : bit_buffer[( *bit_pos )++] = tmp_bit_buffer_diff[i];
1472 : }
1473 : }
1474 : else
1475 : {
1476 255 : bit_buffer[( *bit_pos )++] = 0;
1477 15737 : for ( i = 0; i < bit_cnt_range; i++ )
1478 : {
1479 15482 : bit_buffer[( *bit_pos )++] = tmp_bit_buffer[i];
1480 : }
1481 : }
1482 : }
1483 :
1484 20500 : return;
1485 : }
1486 :
1487 :
1488 : /*-------------------------------------------------------------------------
1489 : * ivas_param_mc_dec2bin()
1490 : *
1491 : * Decimal to binary routine
1492 : *------------------------------------------------------------------------*/
1493 :
1494 12095 : static void ivas_param_mc_dec2bin(
1495 : const int16_t val, /* i : value to encode */
1496 : const int16_t N, /* i : number of bits for encoding the value */
1497 : uint16_t bits[PARAM_MC_MAX_BITS] ) /* o : encoded bits buffer */
1498 : {
1499 : int16_t idx;
1500 :
1501 12095 : idx = 0;
1502 : /* convert value to bitstream, MSB first */
1503 48380 : for ( idx = 0; idx < N; idx++ )
1504 : {
1505 36285 : bits[idx] = ( val >> ( N - 1 - idx ) ) & 1;
1506 : }
1507 :
1508 12095 : return;
1509 : }
1510 :
1511 :
1512 : /*-------------------------------------------------------------------*
1513 : * ivas_param_mc_range_encoder()
1514 : *
1515 : * Parametric MC Range encoder
1516 : *-------------------------------------------------------------------*/
1517 :
1518 41000 : static void ivas_param_mc_range_encoder(
1519 : const int16_t *seq_in, /* i : input sequence */
1520 : const int16_t num_symbols, /* i : Number of symbole to encode */
1521 : const uint16_t *cum_freq, /* i : cumulated frequencies */
1522 : const uint16_t *sym_freq, /* i : symbol frequencies */
1523 : const uint16_t tot_shift, /* i : max cumulative freq as power of 2 */
1524 : const int16_t max_nb_bits, /* i : Maximum number of bits allowed */
1525 : uint16_t *bit_buffer, /* o : output bit buffer */
1526 : int16_t *bit_pos /* o : number of bits used */
1527 : )
1528 : {
1529 : RangeUniEncState rc_st_enc;
1530 : int16_t rc_tot_bits; /* No. of bits returned by range coder */
1531 : int16_t i;
1532 : uint8_t k, byte;
1533 : uint16_t *bits;
1534 :
1535 : /* Initialize range encoder */
1536 41000 : rc_uni_enc_init( &rc_st_enc );
1537 :
1538 : /* Main loop over the length of the sequence */
1539 1242245 : for ( i = 0; i < num_symbols; ++i )
1540 : {
1541 1211350 : rc_uni_enc_encode_symbol_fastS( &rc_st_enc, (uint16_t) seq_in[i], cum_freq, sym_freq, tot_shift );
1542 :
1543 1211350 : if ( rc_uni_enc_virtual_finish( &rc_st_enc ) > max_nb_bits )
1544 : {
1545 : /* we alread have exceeded the maximum number of bits allowed, i.e. the uniform fallback */
1546 10105 : *bit_pos = MAX_BITS_PER_FRAME;
1547 10105 : return;
1548 : }
1549 : }
1550 :
1551 : /* Finish range encoder */
1552 30895 : rc_tot_bits = rc_uni_enc_finish( &rc_st_enc ); /* No. of bits consumed by range coder */
1553 :
1554 : /* Push range coded bits from byte_buffer to bitstream */
1555 :
1556 : /* 1) Push all complete bytes, one byte at a time */
1557 335845 : for ( i = 0; i < ( rc_tot_bits >> 3 ); ++i )
1558 : {
1559 : /* use rc_st_enc.byte_buffer */
1560 304950 : bits = &bit_buffer[i * 8];
1561 :
1562 304950 : byte = rc_st_enc.byte_buffer[i];
1563 :
1564 304950 : bits[0] = ( byte >> 7 ) & 1;
1565 304950 : bits[1] = ( byte >> 6 ) & 1;
1566 304950 : bits[2] = ( byte >> 5 ) & 1;
1567 304950 : bits[3] = ( byte >> 4 ) & 1;
1568 304950 : bits[4] = ( byte >> 3 ) & 1;
1569 304950 : bits[5] = ( byte >> 2 ) & 1;
1570 304950 : bits[6] = ( byte >> 1 ) & 1;
1571 304950 : bits[7] = byte & 1;
1572 : }
1573 :
1574 : /* 2) Push remaining bits */
1575 30895 : if ( ( rc_tot_bits & 7 ) != 0 )
1576 : {
1577 27230 : uint8_t rem_bits = rc_tot_bits & 7;
1578 :
1579 27230 : bits = &bit_buffer[i * 8];
1580 27230 : byte = rc_st_enc.byte_buffer[i];
1581 :
1582 134870 : for ( k = 0; k < rem_bits; k++ )
1583 : {
1584 107640 : bits[k] = ( byte >> ( 7 - k ) ) & 1;
1585 : }
1586 : }
1587 :
1588 : /* Update output number of bits */
1589 30895 : *bit_pos = rc_tot_bits;
1590 :
1591 30895 : return;
1592 : }
|