Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <math.h>
34 : #include <assert.h>
35 : #include "options.h"
36 : #include "cnst.h"
37 : #include "rom_enc.h"
38 : #include "rom_com.h"
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #include "ivas_cnst.h"
42 : #include "ivas_rom_com.h"
43 : #include "ivas_rom_enc.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 : * Local function prototypes
54 : *------------------------------------------------------------------------*/
55 :
56 : static void ivas_mc_paramupmix_dmx( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, float *data_f[], const int16_t input_frame );
57 :
58 : static void ivas_mc_paramupmix_param_est_enc( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, float *input_frame_t[], const int16_t input_frame, float alphas[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS], float betas[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS] );
59 :
60 : static void get_huff_table( const PAR_TYPE par_type, HUFF_TAB *df0, HUFF_TAB *df );
61 :
62 : static void write_huff_bits( const int32_t value, const uint16_t length, uint16_t bit_buffer[MC_PARAMUPMIX_MAX_BITS], int16_t *bit_pos );
63 :
64 : static void huffman_encode( const int32_t *vqPrev, const int32_t *vq, const PAR_TYPE parType, const int16_t nq, uint16_t bit_buffer[MC_PARAMUPMIX_MAX_BITS], int16_t *bit_pos );
65 :
66 : static void put_ec_data( MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, const int16_t ch, const float pars[IVAS_MAX_NUM_BANDS], const float alphas[IVAS_MAX_NUM_BANDS], const PAR_TYPE parType, uint16_t bit_buffer[MC_PARAMUPMIX_MAX_BITS], int16_t *bit_pos );
67 :
68 : static void quantize_alpha( const float *alpha, int16_t *pnq, int32_t aq[IVAS_MAX_NUM_BANDS], float *adeq );
69 :
70 : static void quantize_pars( const float *v, const int16_t nq, const float *data, int32_t vq[IVAS_MAX_NUM_BANDS], float *vdeq );
71 :
72 : /*-------------------------------------------------------------------------
73 : * ivas_mc_paramupmix_enc()
74 : *
75 : * MC ParamUpmix Encoder main encoding function
76 : *------------------------------------------------------------------------*/
77 :
78 37887 : void ivas_mc_paramupmix_enc(
79 : Encoder_Struct *st_ivas, /* i/o: IVAS Encoder handle */
80 : BSTR_ENC_HANDLE hBStr, /* i/o: IVAS Metadata bitstream handle */
81 : float *data_f[], /* i/o: input/transport MC data */
82 : const int16_t input_frame /* i : input frame length */
83 : )
84 : {
85 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix;
86 : int16_t i;
87 : float alphas[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS];
88 : float betas[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS];
89 : uint16_t bit_buffer[MC_PARAMUPMIX_MAX_BITS];
90 : int16_t bit_pos;
91 :
92 37887 : push_wmops( "mc_paramupmix_enc" );
93 :
94 37887 : hMCParamUpmix = st_ivas->hMCParamUpmix;
95 37887 : bit_pos = 0;
96 :
97 : /* Parameter estimation */
98 37887 : ivas_mc_paramupmix_param_est_enc( hMCParamUpmix, data_f, input_frame, alphas, betas );
99 :
100 189435 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
101 : {
102 151548 : put_ec_data( hMCParamUpmix, i, alphas[i], NULL, ALPHA, bit_buffer, &bit_pos );
103 151548 : put_ec_data( hMCParamUpmix, i, betas[i], alphas[i], BETA, bit_buffer, &bit_pos );
104 : }
105 :
106 : /* push the PARAM UPMIX MC side info from the temporary buffer into the medatdata bitstream*/
107 37887 : push_next_bits( hBStr, bit_buffer, bit_pos );
108 :
109 : /* DMX generation*/
110 37887 : ivas_mc_paramupmix_dmx( hMCParamUpmix, data_f, input_frame );
111 :
112 37887 : pop_wmops();
113 :
114 37887 : return;
115 : }
116 :
117 :
118 : /*-------------------------------------------------------------------------
119 : * ivas_mc_paramupmix_enc_open()
120 : *
121 : * Initialize MC ParamUpmix encoder handle
122 : *------------------------------------------------------------------------*/
123 :
124 669 : ivas_error ivas_mc_paramupmix_enc_open(
125 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
126 : )
127 : {
128 : IVAS_FB_CFG *fb_cfg;
129 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix;
130 : int32_t input_Fs;
131 : int32_t input_frame;
132 : int16_t i, k, b, j;
133 : ivas_error error;
134 :
135 669 : error = IVAS_ERR_OK;
136 669 : input_Fs = st_ivas->hEncoderConfig->input_Fs;
137 669 : input_frame = (int32_t) st_ivas->hEncoderConfig->input_Fs / FRAMES_PER_SEC;
138 :
139 : /* Sanity Checks */
140 669 : if ( ( hMCParamUpmix = (MC_PARAMUPMIX_ENC_HANDLE) malloc( sizeof( MC_PARAMUPMIX_ENC_DATA ) ) ) == NULL )
141 : {
142 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MC_MODE_PARAMUPMIX\n" ) );
143 : }
144 3345 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
145 : {
146 8028 : for ( k = 0; k < MC_PARAMUPMIX_NCH; k++ )
147 : {
148 5352 : if ( ( hMCParamUpmix->midside[i][k] = (float *) malloc( sizeof( float ) * input_frame ) ) == NULL )
149 : {
150 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MC_MODE_PARAMUPMIX\n" ) );
151 : }
152 5352 : set_zero( hMCParamUpmix->midside[i][k], (int16_t) input_frame );
153 : }
154 : }
155 669 : hMCParamUpmix->first_frame = 1;
156 :
157 : /* MC_LS_SETUP_5_1_2 is the only current configuration */
158 669 : st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
159 :
160 : /* set core coder dependent on the number of transport channels */
161 669 : switch ( st_ivas->nchan_transport )
162 : {
163 669 : case 8:
164 669 : st_ivas->nCPE = 4;
165 669 : st_ivas->nSCE = 0;
166 669 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
167 669 : break;
168 : #ifdef DEBUGGING
169 : default:
170 : assert( 0 && "Number of transport channels not supported by MC PARAM UPMIX MODE!\n" );
171 : #endif
172 : }
173 :
174 : /* Transient Detector handle */
175 6021 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
176 : {
177 5352 : if ( ( error = ivas_transient_det_open( &( hMCParamUpmix->hTranDet[i] ), input_Fs ) ) != IVAS_ERR_OK )
178 : {
179 0 : return error;
180 : }
181 : }
182 :
183 : /* set FB config. */
184 : /* need to set num output channels to a value > 0 to get pFb != NULL */
185 669 : if ( ( error = ivas_fb_set_cfg( &fb_cfg, MC_FORMAT, MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH, MC_PARAMUPMIX_COMBINATIONS, 0, input_Fs, 0 ) ) != IVAS_ERR_OK )
186 : {
187 0 : return error;
188 : }
189 :
190 669 : fb_cfg->remix_order = mc_paramupmix_fb_remix_order;
191 : /* override latency, could be moved to ivas_fb_set_cfg */
192 : /* assuming parameters are calculated at end of frame, compensate for MCT delay and half of decoder fb */
193 : /* still 1.5ms off, since MCT delay is not large enough */
194 : /* param at end of frame */
195 669 : fb_cfg->prior_input_length = (int16_t) ( NS2SA( input_Fs, 12000000L ) + NS2SA( input_Fs, DELAY_FB_4_NS / 2 ) - input_frame / 2 - NS2SA( input_Fs, DELAY_FB_1_NS / 2 ) );
196 669 : fb_cfg->prior_input_length = (int16_t) max( fb_cfg->prior_input_length, input_frame / MAX_PARAM_SPATIAL_SUBFRAMES );
197 :
198 : /* Allocate and initialize FB mixer handle */
199 669 : if ( ( error = ivas_FB_mixer_open( &( hMCParamUpmix->hFbMixer ), input_Fs, fb_cfg, 0 ) ) != IVAS_ERR_OK )
200 : {
201 0 : return error;
202 : }
203 :
204 3345 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
205 : {
206 : /* Covariance handle */
207 2676 : if ( ( error = ivas_spar_covar_enc_open( &( hMCParamUpmix->hCovEnc[i] ), hMCParamUpmix->hFbMixer->pFb, input_Fs, MC_PARAMUPMIX_NCH + 1, COV_SMOOTH_MC, st_ivas->hEncoderConfig->ivas_total_brate ) ) != IVAS_ERR_OK )
208 : {
209 0 : return error;
210 : }
211 : }
212 :
213 3345 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
214 : {
215 2676 : if ( ( hMCParamUpmix->cov_real[b] = (float ***) malloc( MC_PARAMUPMIX_NCH * sizeof( float ** ) ) ) == NULL )
216 : {
217 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
218 : }
219 8028 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
220 : {
221 5352 : if ( ( hMCParamUpmix->cov_real[b][i] = (float **) malloc( MC_PARAMUPMIX_NCH * sizeof( float * ) ) ) == NULL )
222 : {
223 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
224 : }
225 16056 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
226 : {
227 10704 : if ( ( hMCParamUpmix->cov_real[b][i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
228 : {
229 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov real matrix" );
230 : }
231 : }
232 : }
233 :
234 2676 : if ( ( hMCParamUpmix->cov_dtx_real[b] = (float ***) malloc( MC_PARAMUPMIX_NCH * sizeof( float ** ) ) ) == NULL )
235 : {
236 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
237 : }
238 8028 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
239 : {
240 5352 : if ( ( hMCParamUpmix->cov_dtx_real[b][i] = (float **) malloc( MC_PARAMUPMIX_NCH * sizeof( float * ) ) ) == NULL )
241 : {
242 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
243 : }
244 16056 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
245 : {
246 10704 : if ( ( hMCParamUpmix->cov_dtx_real[b][i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
247 : {
248 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR cov dtx real matrix" );
249 : }
250 : }
251 : }
252 : }
253 :
254 669 : st_ivas->hMCParamUpmix = hMCParamUpmix;
255 :
256 669 : return error;
257 : }
258 :
259 :
260 : /*-------------------------------------------------------------------------
261 : * ivas_mc_paramupmix_enc_close()
262 : *
263 : * Close MC Param-Upmix encoder handle
264 : *------------------------------------------------------------------------*/
265 :
266 20642 : void ivas_mc_paramupmix_enc_close(
267 : MC_PARAMUPMIX_ENC_HANDLE *hMCParamUpmix, /* i/o: MC Param-Upmix encoder handle */
268 : const int32_t input_Fs /* i : input sampling rate */
269 : )
270 : {
271 : int16_t i, k;
272 : int16_t b, j;
273 :
274 20642 : if ( hMCParamUpmix == NULL || *hMCParamUpmix == NULL )
275 : {
276 19973 : return;
277 : }
278 :
279 3345 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
280 : {
281 2676 : if ( ( *hMCParamUpmix )->cov_real[b] != NULL )
282 : {
283 8028 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
284 : {
285 5352 : if ( ( *hMCParamUpmix )->cov_real[b][i] != NULL )
286 : {
287 16056 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
288 : {
289 10704 : if ( ( *hMCParamUpmix )->cov_real[b][i][j] != NULL )
290 : {
291 10704 : free( ( *hMCParamUpmix )->cov_real[b][i][j] );
292 : }
293 : }
294 5352 : free( ( *hMCParamUpmix )->cov_real[b][i] );
295 : }
296 : }
297 2676 : free( ( *hMCParamUpmix )->cov_real[b] );
298 : }
299 :
300 2676 : if ( ( *hMCParamUpmix )->cov_dtx_real[b] != NULL )
301 : {
302 8028 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
303 : {
304 5352 : if ( ( *hMCParamUpmix )->cov_dtx_real[b][i] != NULL )
305 : {
306 16056 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
307 : {
308 10704 : if ( ( *hMCParamUpmix )->cov_dtx_real[b][i][j] != NULL )
309 : {
310 10704 : free( ( *hMCParamUpmix )->cov_dtx_real[b][i][j] );
311 : }
312 : }
313 5352 : free( ( *hMCParamUpmix )->cov_dtx_real[b][i] );
314 : }
315 : }
316 2676 : free( ( *hMCParamUpmix )->cov_dtx_real[b] );
317 : }
318 : }
319 :
320 3345 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
321 : {
322 8028 : for ( k = 0; k < MC_PARAMUPMIX_NCH; k++ )
323 : {
324 5352 : free( ( *hMCParamUpmix )->midside[i][k] );
325 : }
326 : }
327 :
328 6021 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
329 : {
330 5352 : ivas_transient_det_close( &( *hMCParamUpmix )->hTranDet[i] );
331 : }
332 :
333 669 : if ( ( *hMCParamUpmix )->hFbMixer != NULL )
334 : {
335 669 : ivas_FB_mixer_close( &( *hMCParamUpmix )->hFbMixer, input_Fs, 0 );
336 : }
337 :
338 3345 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
339 : {
340 : /* Covariance handle */
341 2676 : if ( ( *hMCParamUpmix )->hCovEnc[i] != NULL )
342 : {
343 2676 : ivas_spar_covar_enc_close( &( *hMCParamUpmix )->hCovEnc[i], ( MC_PARAMUPMIX_NCH + 1 ) );
344 : }
345 : }
346 :
347 669 : free( *hMCParamUpmix );
348 669 : *hMCParamUpmix = NULL;
349 :
350 669 : return;
351 : }
352 :
353 :
354 : /*****************************************************************************************/
355 : /* local functions */
356 : /*****************************************************************************************/
357 :
358 303096 : static void get_huff_table(
359 : const PAR_TYPE par_type,
360 : HUFF_TAB *df0,
361 : HUFF_TAB *df )
362 : {
363 303096 : switch ( par_type )
364 : {
365 151548 : case ALPHA:
366 151548 : df0->value = huff_alpha_table.df0.value;
367 151548 : df0->length = huff_alpha_table.df0.length;
368 151548 : df->value = huff_alpha_table.df.value;
369 151548 : df->length = huff_alpha_table.df.length;
370 151548 : break;
371 151548 : case BETA:
372 151548 : df0->value = huff_beta_table.df0.value;
373 151548 : df0->length = huff_beta_table.df0.length;
374 151548 : df->value = huff_beta_table.df.value;
375 151548 : df->length = huff_beta_table.df.length;
376 151548 : break;
377 : }
378 :
379 303096 : return;
380 : }
381 :
382 :
383 3637152 : static void write_huff_bits(
384 : const int32_t value,
385 : const uint16_t length,
386 : uint16_t bit_buffer[MC_PARAMUPMIX_MAX_BITS],
387 : int16_t *bit_pos )
388 : {
389 : int16_t k;
390 :
391 11991937 : for ( k = length - 1; k >= 0; k-- )
392 : {
393 8354785 : bit_buffer[( *bit_pos )++] = (uint16_t) ( ( value >> k ) & 1 );
394 : }
395 :
396 3637152 : return;
397 : }
398 :
399 :
400 303096 : static void huffman_encode(
401 : const int32_t *vqPrev,
402 : const int32_t *vq,
403 : const PAR_TYPE parType,
404 : const int16_t nq,
405 : uint16_t bit_buffer[MC_PARAMUPMIX_MAX_BITS],
406 : int16_t *bit_pos )
407 : {
408 : int16_t iv;
409 : int32_t icode;
410 : int16_t offset;
411 : HUFF_TAB df0, df;
412 :
413 303096 : get_huff_table( parType, &df0, &df );
414 :
415 303096 : offset = nq - 1; /* range [-(nquant - 1), nquant - 1] */
416 :
417 3940248 : for ( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
418 : {
419 3637152 : if ( iv == 0 )
420 : {
421 303096 : icode = vq[iv];
422 : }
423 : else
424 : {
425 3334056 : icode = vq[iv] - vq[iv - 1] + offset;
426 : }
427 :
428 3637152 : icode = vq[iv] - vqPrev[iv] + offset;
429 : }
430 :
431 : /* Write the bitstream */
432 303096 : bit_buffer[( *bit_pos )++] = (uint16_t) 0 & 1;
433 3940248 : for ( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
434 : {
435 3637152 : if ( iv == 0 )
436 : {
437 303096 : icode = vq[iv];
438 303096 : write_huff_bits( df0.value[icode], df0.length[icode], bit_buffer, bit_pos );
439 : }
440 : else
441 : {
442 3334056 : icode = vq[iv] - vq[iv - 1] + offset;
443 3334056 : write_huff_bits( df.value[icode], df.length[icode], bit_buffer, bit_pos );
444 : }
445 : }
446 :
447 303096 : return;
448 : }
449 :
450 :
451 303096 : static void quantize_pars(
452 : const float *v,
453 : const int16_t nq,
454 : const float *data,
455 : int32_t vq[IVAS_MAX_NUM_BANDS],
456 : float *vdeq )
457 : {
458 : int16_t iv, iq, iq0, iq1;
459 :
460 3940248 : for ( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
461 : {
462 3637152 : iq0 = 0;
463 3637152 : iq1 = nq - 1;
464 :
465 21822912 : while ( iq1 - iq0 > 1 )
466 : {
467 18185760 : iq = ( iq0 + iq1 ) / 2;
468 18185760 : if ( v[iv] < data[iq] )
469 : {
470 8578834 : iq1 = iq;
471 : }
472 : else
473 : {
474 9606926 : iq0 = iq;
475 : }
476 : }
477 :
478 3637152 : if ( fabs( v[iv] - data[iq0] ) < fabs( v[iv] - data[iq1] ) )
479 : {
480 1847022 : vq[iv] = iq0;
481 1847022 : vdeq[iv] = data[iq0];
482 : }
483 : else
484 : {
485 1790130 : vq[iv] = iq1;
486 1790130 : vdeq[iv] = data[iq1];
487 : }
488 : }
489 :
490 303096 : return;
491 : }
492 :
493 :
494 303096 : static void quantize_alpha(
495 : const float *alpha,
496 : int16_t *pnq,
497 : int32_t aq[IVAS_MAX_NUM_BANDS],
498 : float *adeq )
499 : {
500 : int16_t nq;
501 : const float *data;
502 :
503 303096 : nq = ivas_mc_paramupmix_alpha_quant_table.nquant;
504 303096 : data = ivas_mc_paramupmix_alpha_quant_table.data;
505 :
506 303096 : quantize_pars( alpha, nq, data, aq, adeq );
507 303096 : *pnq = nq;
508 :
509 303096 : return;
510 : }
511 :
512 :
513 151548 : static void quantize_beta(
514 : const float *beta,
515 : const int32_t aq[IVAS_MAX_NUM_BANDS],
516 : int16_t *pnq,
517 : int32_t bq[IVAS_MAX_NUM_BANDS],
518 : float *bdeq )
519 : {
520 : int16_t iv, iq, iq0, iq1;
521 151548 : const ACPL_QUANT_TABLE *tables = ivas_mc_paramupmix_beta_quant_table;
522 : ACPL_QUANT_TABLE quant_table;
523 :
524 1970124 : for ( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
525 : {
526 1818576 : quant_table = tables[ivas_param_upmx_mx_qmap[aq[iv]]];
527 :
528 1818576 : iq0 = 0;
529 1818576 : iq1 = quant_table.nquant - 1;
530 :
531 7274304 : while ( iq1 - iq0 > 1 )
532 : {
533 5455728 : iq = ( iq0 + iq1 ) / 2;
534 5455728 : if ( beta[iv] < quant_table.data[iq] )
535 : {
536 3703317 : iq1 = iq;
537 : }
538 : else
539 : {
540 1752411 : iq0 = iq;
541 : }
542 : }
543 :
544 1818576 : if ( fabs( beta[iv] - quant_table.data[iq0] ) < fabs( beta[iv] - quant_table.data[iq1] ) )
545 : {
546 971284 : bq[iv] = iq0;
547 971284 : bdeq[iv] = quant_table.data[iq0];
548 : }
549 : else
550 : {
551 847292 : bq[iv] = iq1;
552 847292 : bdeq[iv] = quant_table.data[iq1];
553 : }
554 : }
555 :
556 151548 : *pnq = ivas_mc_paramupmix_beta_quant_table[0].nquant;
557 :
558 151548 : return;
559 : }
560 :
561 :
562 303096 : static void put_ec_data(
563 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix,
564 : const int16_t ch,
565 : const float pars[IVAS_MAX_NUM_BANDS],
566 : const float alphas[IVAS_MAX_NUM_BANDS],
567 : const PAR_TYPE parType,
568 : uint16_t bit_buffer[MC_PARAMUPMIX_MAX_BITS],
569 : int16_t *bit_pos )
570 : {
571 : int16_t nq;
572 : int32_t alphaQuant[IVAS_MAX_NUM_BANDS];
573 : int32_t betaQuant[IVAS_MAX_NUM_BANDS];
574 : float alphaDequant[IVAS_MAX_NUM_BANDS];
575 : float betaDequant[IVAS_MAX_NUM_BANDS];
576 :
577 303096 : if ( parType == ALPHA )
578 : {
579 151548 : quantize_alpha( pars, &nq, alphaQuant, alphaDequant );
580 : }
581 : else
582 : {
583 151548 : quantize_alpha( alphas, &nq, alphaQuant, alphaDequant );
584 151548 : quantize_beta( pars, alphaQuant, &nq, betaQuant, betaDequant );
585 : }
586 :
587 303096 : if ( hMCParamUpmix->first_frame )
588 : {
589 5352 : mvl2l( &( alphaQuant[0] ), &( hMCParamUpmix->alpha_quant_prev[ch][0] ), IVAS_MAX_NUM_BANDS );
590 5352 : if ( parType == BETA )
591 : {
592 2676 : mvl2l( &( betaQuant[0] ), &( hMCParamUpmix->beta_quant_prev[ch][0] ), IVAS_MAX_NUM_BANDS );
593 2676 : if ( ch == ( MC_PARAMUPMIX_COMBINATIONS - 1 ) )
594 : {
595 669 : hMCParamUpmix->first_frame = 0;
596 : }
597 : }
598 : }
599 :
600 : /* Always one parameter set per frame for transient frames. Original PS framing is used internally. */
601 303096 : if ( parType == ALPHA )
602 : {
603 151548 : huffman_encode( hMCParamUpmix->alpha_quant_prev[ch], alphaQuant, ALPHA, nq, bit_buffer, bit_pos );
604 : }
605 : else
606 : {
607 151548 : huffman_encode( hMCParamUpmix->beta_quant_prev[ch], betaQuant, BETA, nq, bit_buffer, bit_pos );
608 : }
609 :
610 303096 : if ( parType == ALPHA )
611 : {
612 151548 : mvl2l( alphaQuant, hMCParamUpmix->alpha_quant_prev[ch], IVAS_MAX_NUM_BANDS );
613 : }
614 : else
615 : {
616 151548 : mvl2l( betaQuant, hMCParamUpmix->beta_quant_prev[ch], IVAS_MAX_NUM_BANDS );
617 : }
618 :
619 303096 : return;
620 : }
621 :
622 :
623 : /*-------------------------------------------------------------------------
624 : * ivas_mc_paramupmix_dmx()
625 : *
626 : * Computes the time domain down mix signal
627 : *------------------------------------------------------------------------*/
628 :
629 37887 : static void ivas_mc_paramupmix_dmx(
630 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, /* i/o: MC ParamUpmix encoder handle */
631 : float *data_f[], /* i/o: Input, downmix out */
632 : const int16_t input_frame /* i : Input frame length */
633 : )
634 : {
635 : int16_t i, l;
636 37887 : const int16_t chan1s[4] = { 4, 5, 8, 9 };
637 37887 : const int16_t chan2s[4] = { 6, 7, 10, 11 };
638 37887 : const int16_t chanOut[4] = { 4, 5, 6, 7 };
639 37887 : const int16_t chanZero[4] = { 8, 9, 10, 11 };
640 :
641 : /* boxes = { 0 1 2 3 [4 6] [5 7] [8 10] [9 11] }; */
642 : /* 9+11 -> 7 */
643 : /* 8+10 -> 6 */
644 : /* 5+7 -> 5 */
645 : /* 4+6 -> 4 */
646 189435 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
647 : {
648 145445628 : for ( l = 0; l < input_frame; l++ )
649 : {
650 : /* mid */
651 145294080 : hMCParamUpmix->midside[i][0][l] = ( data_f[chan1s[i]][l] + data_f[chan2s[i]][l] ) * 0.5f;
652 : /* side */
653 145294080 : hMCParamUpmix->midside[i][1][l] = ( data_f[chan1s[i]][l] - data_f[chan2s[i]][l] ) * 0.5f;
654 145294080 : data_f[chanOut[i]][l] = hMCParamUpmix->midside[i][0][l];
655 : }
656 : }
657 :
658 189435 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
659 : {
660 151548 : set_zero( data_f[chanZero[i]], input_frame );
661 : }
662 :
663 37887 : return;
664 : }
665 :
666 :
667 : /*-------------------------------------------------------------------------
668 : * ivas_mc_paramupmix_param_est_enc()
669 : *
670 : * run the CLDFB analysis on the input signal
671 : * estimate the input and down mix covariances
672 : *------------------------------------------------------------------------*/
673 :
674 37887 : static void ivas_mc_paramupmix_param_est_enc(
675 : MC_PARAMUPMIX_ENC_HANDLE hMCParamUpmix, /* i/o: MC Param-Upmix encoder handle */
676 : float *data_f[], /* i : Input frame in the time domain */
677 : const int16_t input_frame, /* i : Input frame length */
678 : float alphas[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS],
679 : float betas[MC_PARAMUPMIX_COMBINATIONS][IVAS_MAX_NUM_BANDS] )
680 : {
681 : float *pcm_in[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
682 : float fr_realbuffer[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH][L_FRAME48k];
683 : float fr_imagbuffer[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH][L_FRAME48k];
684 : float FR_Real_Mid[L_FRAME48k], FR_Imag_Mid[L_FRAME48k];
685 : float *p_fr_realbuffer[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
686 : float *p_fr_imagbuffer[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
687 : float *pp_in_fr_real[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
688 : float *pp_in_fr_imag[MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH];
689 : float *cov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
690 : float *cov_dtx_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
691 : float rxx, rxy, ryy, cmat, rxxest, drxx, wetaux;
692 : int16_t l_ts;
693 : int16_t b, i, j, ts, bnd;
694 : int16_t maxbands;
695 : int16_t transient_det[MC_PARAMUPMIX_COMBINATIONS][2];
696 : int16_t transient_det_l[2], transient_det_r[2];
697 37887 : const int16_t chan1s[MC_PARAMUPMIX_COMBINATIONS] = { 4, 5, 8, 9 };
698 37887 : const int16_t chan2s[MC_PARAMUPMIX_COMBINATIONS] = { 6, 7, 10, 11 };
699 37887 : const int16_t HOA_md_ind[IVAS_SPAR_MAX_CH] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
700 :
701 189435 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
702 : {
703 151548 : pcm_in[2 * i] = data_f[chan1s[i]];
704 151548 : pcm_in[2 * i + 1] = data_f[chan2s[i]];
705 : }
706 :
707 : /*-----------------------------------------------------------------------------------------*
708 : * Transient detector
709 : *-----------------------------------------------------------------------------------------*/
710 :
711 189435 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
712 : {
713 151548 : ivas_transient_det_process( hMCParamUpmix->hTranDet[2 * i], pcm_in[2 * i], input_frame, transient_det_l );
714 151548 : ivas_transient_det_process( hMCParamUpmix->hTranDet[2 * i + 1], pcm_in[2 * i + 1], input_frame, transient_det_r );
715 151548 : transient_det[i][0] = transient_det_l[0] || transient_det_r[0];
716 151548 : transient_det[i][1] = transient_det_l[0] || transient_det_r[0];
717 : /* should probably be transient_det_l[1] || transient_det_r[1] , but choosing 0 reproduces the before merge state */
718 : }
719 :
720 340983 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
721 : {
722 303096 : p_fr_realbuffer[i] = fr_realbuffer[i];
723 303096 : p_fr_imagbuffer[i] = fr_imagbuffer[i];
724 : }
725 :
726 : /* prepare Parameter MDFT analysis */
727 340983 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
728 : {
729 303096 : pp_in_fr_real[i] = p_fr_realbuffer[i];
730 303096 : pp_in_fr_imag[i] = p_fr_imagbuffer[i];
731 : }
732 :
733 37887 : l_ts = input_frame / MAX_PARAM_SPATIAL_SUBFRAMES;
734 189435 : for ( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
735 : {
736 151548 : ivas_fb_mixer_get_windowed_fr( hMCParamUpmix->hFbMixer, pcm_in, pp_in_fr_real, pp_in_fr_imag, l_ts, l_ts, hMCParamUpmix->hFbMixer->fb_cfg->num_in_chans );
737 :
738 151548 : ivas_fb_mixer_update_prior_input( hMCParamUpmix->hFbMixer, pcm_in, l_ts, hMCParamUpmix->hFbMixer->fb_cfg->num_in_chans );
739 :
740 1363932 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
741 : {
742 1212384 : pcm_in[i] += l_ts;
743 1212384 : pp_in_fr_real[i] += l_ts;
744 1212384 : pp_in_fr_imag[i] += l_ts;
745 : }
746 : }
747 :
748 : /*-----------------------------------------------------------------------------------------*
749 : * Covariance process
750 : *-----------------------------------------------------------------------------------------*/
751 :
752 189435 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
753 : {
754 151548 : pp_in_fr_real[0] = p_fr_realbuffer[2 * b];
755 151548 : pp_in_fr_imag[0] = p_fr_imagbuffer[2 * b];
756 151548 : pp_in_fr_real[1] = FR_Real_Mid;
757 151548 : pp_in_fr_imag[1] = FR_Imag_Mid;
758 :
759 151548 : v_add( pp_in_fr_real[0], p_fr_realbuffer[2 * b + 1], pp_in_fr_real[1], L_FRAME48k );
760 151548 : v_add( pp_in_fr_imag[0], p_fr_imagbuffer[2 * b + 1], pp_in_fr_imag[1], L_FRAME48k );
761 :
762 454644 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
763 : {
764 909288 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
765 : {
766 606192 : cov_real[i][j] = hMCParamUpmix->cov_real[b][i][j];
767 606192 : cov_dtx_real[i][j] = hMCParamUpmix->cov_dtx_real[b][i][j];
768 : }
769 : }
770 :
771 151548 : ivas_enc_cov_handler_process( hMCParamUpmix->hCovEnc[b], pp_in_fr_real, pp_in_fr_imag, cov_real, cov_dtx_real, hMCParamUpmix->hFbMixer->pFb, 0, hMCParamUpmix->hFbMixer->pFb->filterbank_num_bands, MC_PARAMUPMIX_NCH, 0 /*dtx_vad*/, transient_det[b], HOA_md_ind, NULL, NULL, NULL, 0, 0 );
772 : }
773 :
774 37887 : maxbands = hMCParamUpmix->hFbMixer->pFb->filterbank_num_bands;
775 189435 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
776 : {
777 1969724 : for ( bnd = 0; bnd < maxbands; bnd++ )
778 : {
779 1818176 : rxy = hMCParamUpmix->cov_real[b][1][0][bnd];
780 1818176 : ryy = hMCParamUpmix->cov_real[b][1][1][bnd];
781 1818176 : cmat = rxy / ( ryy + EPSILON );
782 1818176 : alphas[b][bnd] = 2.0f * cmat - 1.0f;
783 :
784 1818176 : rxx = hMCParamUpmix->cov_real[b][0][0][bnd];
785 1818176 : rxxest = cmat * cmat * ryy;
786 1818176 : drxx = rxx - rxxest;
787 1818176 : drxx = max( drxx, 0.0f );
788 1818176 : wetaux = sqrtf( drxx / ( ryy + EPSILON ) );
789 1818176 : betas[b][bnd] = 2.0f * wetaux;
790 : }
791 : }
792 :
793 37887 : if ( maxbands < IVAS_MAX_NUM_BANDS )
794 : {
795 250 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
796 : {
797 600 : for ( bnd = maxbands; bnd < IVAS_MAX_NUM_BANDS; bnd++ )
798 : {
799 400 : alphas[b][bnd] = 0.0f;
800 400 : betas[b][bnd] = 0.0f;
801 : }
802 : }
803 : }
804 :
805 37887 : return;
806 : }
|