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 790 : 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 790 : push_wmops( "mc_paramupmix_enc" );
93 :
94 790 : hMCParamUpmix = st_ivas->hMCParamUpmix;
95 790 : bit_pos = 0;
96 :
97 : /* Parameter estimation */
98 790 : ivas_mc_paramupmix_param_est_enc( hMCParamUpmix, data_f, input_frame, alphas, betas );
99 :
100 3950 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
101 : {
102 3160 : put_ec_data( hMCParamUpmix, i, alphas[i], NULL, ALPHA, bit_buffer, &bit_pos );
103 3160 : 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 790 : push_next_bits( hBStr, bit_buffer, bit_pos );
108 :
109 : /* DMX generation*/
110 790 : ivas_mc_paramupmix_dmx( hMCParamUpmix, data_f, input_frame );
111 :
112 790 : pop_wmops();
113 :
114 790 : return;
115 : }
116 :
117 :
118 : /*-------------------------------------------------------------------------
119 : * ivas_mc_paramupmix_enc_open()
120 : *
121 : * Initialize MC ParamUpmix encoder handle
122 : *------------------------------------------------------------------------*/
123 :
124 12 : 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 12 : error = IVAS_ERR_OK;
136 12 : input_Fs = st_ivas->hEncoderConfig->input_Fs;
137 12 : input_frame = (int32_t) st_ivas->hEncoderConfig->input_Fs / FRAMES_PER_SEC;
138 :
139 : /* Sanity Checks */
140 12 : 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 60 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
145 : {
146 144 : for ( k = 0; k < MC_PARAMUPMIX_NCH; k++ )
147 : {
148 96 : 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 96 : set_zero( hMCParamUpmix->midside[i][k], (int16_t) input_frame );
153 : }
154 : }
155 12 : hMCParamUpmix->first_frame = 1;
156 :
157 : /* MC_LS_SETUP_5_1_2 is the only current configuration */
158 12 : st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
159 :
160 : /* set core coder dependent on the number of transport channels */
161 12 : switch ( st_ivas->nchan_transport )
162 : {
163 12 : case 8:
164 12 : st_ivas->nCPE = 4;
165 12 : st_ivas->nSCE = 0;
166 12 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
167 12 : 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 108 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
176 : {
177 96 : 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 12 : 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 12 : 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 12 : 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 12 : 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 12 : if ( ( error = ivas_FB_mixer_open( &( hMCParamUpmix->hFbMixer ), input_Fs, fb_cfg, 0 ) ) != IVAS_ERR_OK )
200 : {
201 0 : return error;
202 : }
203 :
204 60 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
205 : {
206 : /* Covariance handle */
207 48 : 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 60 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
214 : {
215 48 : 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 144 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
220 : {
221 96 : 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 288 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
226 : {
227 192 : 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 48 : 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 144 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
239 : {
240 96 : 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 288 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
245 : {
246 192 : 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 12 : st_ivas->hMCParamUpmix = hMCParamUpmix;
255 :
256 12 : return error;
257 : }
258 :
259 :
260 : /*-------------------------------------------------------------------------
261 : * ivas_mc_paramupmix_enc_close()
262 : *
263 : * Close MC Param-Upmix encoder handle
264 : *------------------------------------------------------------------------*/
265 :
266 1537 : 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 1537 : if ( hMCParamUpmix == NULL || *hMCParamUpmix == NULL )
275 : {
276 1525 : return;
277 : }
278 :
279 60 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
280 : {
281 48 : if ( ( *hMCParamUpmix )->cov_real[b] != NULL )
282 : {
283 144 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
284 : {
285 96 : if ( ( *hMCParamUpmix )->cov_real[b][i] != NULL )
286 : {
287 288 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
288 : {
289 192 : if ( ( *hMCParamUpmix )->cov_real[b][i][j] != NULL )
290 : {
291 192 : free( ( *hMCParamUpmix )->cov_real[b][i][j] );
292 : }
293 : }
294 96 : free( ( *hMCParamUpmix )->cov_real[b][i] );
295 : }
296 : }
297 48 : free( ( *hMCParamUpmix )->cov_real[b] );
298 : }
299 :
300 48 : if ( ( *hMCParamUpmix )->cov_dtx_real[b] != NULL )
301 : {
302 144 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
303 : {
304 96 : if ( ( *hMCParamUpmix )->cov_dtx_real[b][i] != NULL )
305 : {
306 288 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
307 : {
308 192 : if ( ( *hMCParamUpmix )->cov_dtx_real[b][i][j] != NULL )
309 : {
310 192 : free( ( *hMCParamUpmix )->cov_dtx_real[b][i][j] );
311 : }
312 : }
313 96 : free( ( *hMCParamUpmix )->cov_dtx_real[b][i] );
314 : }
315 : }
316 48 : free( ( *hMCParamUpmix )->cov_dtx_real[b] );
317 : }
318 : }
319 :
320 60 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
321 : {
322 144 : for ( k = 0; k < MC_PARAMUPMIX_NCH; k++ )
323 : {
324 96 : free( ( *hMCParamUpmix )->midside[i][k] );
325 : }
326 : }
327 :
328 108 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
329 : {
330 96 : ivas_transient_det_close( &( *hMCParamUpmix )->hTranDet[i] );
331 : }
332 :
333 12 : if ( ( *hMCParamUpmix )->hFbMixer != NULL )
334 : {
335 12 : ivas_FB_mixer_close( &( *hMCParamUpmix )->hFbMixer, input_Fs, 0 );
336 : }
337 :
338 60 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
339 : {
340 : /* Covariance handle */
341 48 : if ( ( *hMCParamUpmix )->hCovEnc[i] != NULL )
342 : {
343 48 : ivas_spar_covar_enc_close( &( *hMCParamUpmix )->hCovEnc[i], ( MC_PARAMUPMIX_NCH + 1 ) );
344 : }
345 : }
346 :
347 12 : free( *hMCParamUpmix );
348 12 : *hMCParamUpmix = NULL;
349 :
350 12 : return;
351 : }
352 :
353 :
354 : /*****************************************************************************************/
355 : /* local functions */
356 : /*****************************************************************************************/
357 :
358 6320 : static void get_huff_table(
359 : const PAR_TYPE par_type,
360 : HUFF_TAB *df0,
361 : HUFF_TAB *df )
362 : {
363 6320 : switch ( par_type )
364 : {
365 3160 : case ALPHA:
366 3160 : df0->value = huff_alpha_table.df0.value;
367 3160 : df0->length = huff_alpha_table.df0.length;
368 3160 : df->value = huff_alpha_table.df.value;
369 3160 : df->length = huff_alpha_table.df.length;
370 3160 : break;
371 3160 : case BETA:
372 3160 : df0->value = huff_beta_table.df0.value;
373 3160 : df0->length = huff_beta_table.df0.length;
374 3160 : df->value = huff_beta_table.df.value;
375 3160 : df->length = huff_beta_table.df.length;
376 3160 : break;
377 : }
378 :
379 6320 : return;
380 : }
381 :
382 :
383 75840 : 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 235303 : for ( k = length - 1; k >= 0; k-- )
392 : {
393 159463 : bit_buffer[( *bit_pos )++] = (uint16_t) ( ( value >> k ) & 1 );
394 : }
395 :
396 75840 : return;
397 : }
398 :
399 :
400 6320 : 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 6320 : get_huff_table( parType, &df0, &df );
414 :
415 6320 : offset = nq - 1; /* range [-(nquant - 1), nquant - 1] */
416 :
417 82160 : for ( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
418 : {
419 75840 : if ( iv == 0 )
420 : {
421 6320 : icode = vq[iv];
422 : }
423 : else
424 : {
425 69520 : icode = vq[iv] - vq[iv - 1] + offset;
426 : }
427 :
428 75840 : icode = vq[iv] - vqPrev[iv] + offset;
429 : }
430 :
431 : /* Write the bitstream */
432 6320 : bit_buffer[( *bit_pos )++] = (uint16_t) 0 & 1;
433 82160 : for ( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
434 : {
435 75840 : if ( iv == 0 )
436 : {
437 6320 : icode = vq[iv];
438 6320 : write_huff_bits( df0.value[icode], df0.length[icode], bit_buffer, bit_pos );
439 : }
440 : else
441 : {
442 69520 : icode = vq[iv] - vq[iv - 1] + offset;
443 69520 : write_huff_bits( df.value[icode], df.length[icode], bit_buffer, bit_pos );
444 : }
445 : }
446 :
447 6320 : return;
448 : }
449 :
450 :
451 6320 : 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 82160 : for ( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
461 : {
462 75840 : iq0 = 0;
463 75840 : iq1 = nq - 1;
464 :
465 455040 : while ( iq1 - iq0 > 1 )
466 : {
467 379200 : iq = ( iq0 + iq1 ) / 2;
468 379200 : if ( v[iv] < data[iq] )
469 : {
470 206988 : iq1 = iq;
471 : }
472 : else
473 : {
474 172212 : iq0 = iq;
475 : }
476 : }
477 :
478 75840 : if ( fabs( v[iv] - data[iq0] ) < fabs( v[iv] - data[iq1] ) )
479 : {
480 36896 : vq[iv] = iq0;
481 36896 : vdeq[iv] = data[iq0];
482 : }
483 : else
484 : {
485 38944 : vq[iv] = iq1;
486 38944 : vdeq[iv] = data[iq1];
487 : }
488 : }
489 :
490 6320 : return;
491 : }
492 :
493 :
494 6320 : 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 6320 : nq = ivas_mc_paramupmix_alpha_quant_table.nquant;
504 6320 : data = ivas_mc_paramupmix_alpha_quant_table.data;
505 :
506 6320 : quantize_pars( alpha, nq, data, aq, adeq );
507 6320 : *pnq = nq;
508 :
509 6320 : return;
510 : }
511 :
512 :
513 3160 : 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 3160 : const ACPL_QUANT_TABLE *tables = ivas_mc_paramupmix_beta_quant_table;
522 : ACPL_QUANT_TABLE quant_table;
523 :
524 41080 : for ( iv = 0; iv < IVAS_MAX_NUM_BANDS; iv++ )
525 : {
526 37920 : quant_table = tables[ivas_param_upmx_mx_qmap[aq[iv]]];
527 :
528 37920 : iq0 = 0;
529 37920 : iq1 = quant_table.nquant - 1;
530 :
531 151680 : while ( iq1 - iq0 > 1 )
532 : {
533 113760 : iq = ( iq0 + iq1 ) / 2;
534 113760 : if ( beta[iv] < quant_table.data[iq] )
535 : {
536 88473 : iq1 = iq;
537 : }
538 : else
539 : {
540 25287 : iq0 = iq;
541 : }
542 : }
543 :
544 37920 : if ( fabs( beta[iv] - quant_table.data[iq0] ) < fabs( beta[iv] - quant_table.data[iq1] ) )
545 : {
546 21060 : bq[iv] = iq0;
547 21060 : bdeq[iv] = quant_table.data[iq0];
548 : }
549 : else
550 : {
551 16860 : bq[iv] = iq1;
552 16860 : bdeq[iv] = quant_table.data[iq1];
553 : }
554 : }
555 :
556 3160 : *pnq = ivas_mc_paramupmix_beta_quant_table[0].nquant;
557 :
558 3160 : return;
559 : }
560 :
561 :
562 6320 : 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 6320 : if ( parType == ALPHA )
578 : {
579 3160 : quantize_alpha( pars, &nq, alphaQuant, alphaDequant );
580 : }
581 : else
582 : {
583 3160 : quantize_alpha( alphas, &nq, alphaQuant, alphaDequant );
584 3160 : quantize_beta( pars, alphaQuant, &nq, betaQuant, betaDequant );
585 : }
586 :
587 6320 : if ( hMCParamUpmix->first_frame )
588 : {
589 96 : mvl2l( &( alphaQuant[0] ), &( hMCParamUpmix->alpha_quant_prev[ch][0] ), IVAS_MAX_NUM_BANDS );
590 96 : if ( parType == BETA )
591 : {
592 48 : mvl2l( &( betaQuant[0] ), &( hMCParamUpmix->beta_quant_prev[ch][0] ), IVAS_MAX_NUM_BANDS );
593 48 : if ( ch == ( MC_PARAMUPMIX_COMBINATIONS - 1 ) )
594 : {
595 12 : 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 6320 : if ( parType == ALPHA )
602 : {
603 3160 : huffman_encode( hMCParamUpmix->alpha_quant_prev[ch], alphaQuant, ALPHA, nq, bit_buffer, bit_pos );
604 : }
605 : else
606 : {
607 3160 : huffman_encode( hMCParamUpmix->beta_quant_prev[ch], betaQuant, BETA, nq, bit_buffer, bit_pos );
608 : }
609 :
610 6320 : if ( parType == ALPHA )
611 : {
612 3160 : mvl2l( alphaQuant, hMCParamUpmix->alpha_quant_prev[ch], IVAS_MAX_NUM_BANDS );
613 : }
614 : else
615 : {
616 3160 : mvl2l( betaQuant, hMCParamUpmix->beta_quant_prev[ch], IVAS_MAX_NUM_BANDS );
617 : }
618 :
619 6320 : return;
620 : }
621 :
622 :
623 : /*-------------------------------------------------------------------------
624 : * ivas_mc_paramupmix_dmx()
625 : *
626 : * Computes the time domain down mix signal
627 : *------------------------------------------------------------------------*/
628 :
629 790 : 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 790 : const int16_t chan1s[4] = { 4, 5, 8, 9 };
637 790 : const int16_t chan2s[4] = { 6, 7, 10, 11 };
638 790 : const int16_t chanOut[4] = { 4, 5, 6, 7 };
639 790 : 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 3950 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
647 : {
648 3036760 : for ( l = 0; l < input_frame; l++ )
649 : {
650 : /* mid */
651 3033600 : hMCParamUpmix->midside[i][0][l] = ( data_f[chan1s[i]][l] + data_f[chan2s[i]][l] ) * 0.5f;
652 : /* side */
653 3033600 : hMCParamUpmix->midside[i][1][l] = ( data_f[chan1s[i]][l] - data_f[chan2s[i]][l] ) * 0.5f;
654 3033600 : data_f[chanOut[i]][l] = hMCParamUpmix->midside[i][0][l];
655 : }
656 : }
657 :
658 3950 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
659 : {
660 3160 : set_zero( data_f[chanZero[i]], input_frame );
661 : }
662 :
663 790 : 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 790 : 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 790 : const int16_t chan1s[MC_PARAMUPMIX_COMBINATIONS] = { 4, 5, 8, 9 };
698 790 : const int16_t chan2s[MC_PARAMUPMIX_COMBINATIONS] = { 6, 7, 10, 11 };
699 790 : const int16_t HOA_md_ind[IVAS_SPAR_MAX_CH] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
700 :
701 3950 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
702 : {
703 3160 : pcm_in[2 * i] = data_f[chan1s[i]];
704 3160 : pcm_in[2 * i + 1] = data_f[chan2s[i]];
705 : }
706 :
707 : /*-----------------------------------------------------------------------------------------*
708 : * Transient detector
709 : *-----------------------------------------------------------------------------------------*/
710 :
711 3950 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS; i++ )
712 : {
713 3160 : ivas_transient_det_process( hMCParamUpmix->hTranDet[2 * i], pcm_in[2 * i], input_frame, transient_det_l );
714 3160 : ivas_transient_det_process( hMCParamUpmix->hTranDet[2 * i + 1], pcm_in[2 * i + 1], input_frame, transient_det_r );
715 3160 : transient_det[i][0] = transient_det_l[0] || transient_det_r[0];
716 3160 : 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 7110 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
721 : {
722 6320 : p_fr_realbuffer[i] = fr_realbuffer[i];
723 6320 : p_fr_imagbuffer[i] = fr_imagbuffer[i];
724 : }
725 :
726 : /* prepare Parameter MDFT analysis */
727 7110 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
728 : {
729 6320 : pp_in_fr_real[i] = p_fr_realbuffer[i];
730 6320 : pp_in_fr_imag[i] = p_fr_imagbuffer[i];
731 : }
732 :
733 790 : l_ts = input_frame / MAX_PARAM_SPATIAL_SUBFRAMES;
734 3950 : for ( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
735 : {
736 3160 : 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 3160 : ivas_fb_mixer_update_prior_input( hMCParamUpmix->hFbMixer, pcm_in, l_ts, hMCParamUpmix->hFbMixer->fb_cfg->num_in_chans );
739 :
740 28440 : for ( i = 0; i < MC_PARAMUPMIX_COMBINATIONS * MC_PARAMUPMIX_NCH; i++ )
741 : {
742 25280 : pcm_in[i] += l_ts;
743 25280 : pp_in_fr_real[i] += l_ts;
744 25280 : pp_in_fr_imag[i] += l_ts;
745 : }
746 : }
747 :
748 : /*-----------------------------------------------------------------------------------------*
749 : * Covariance process
750 : *-----------------------------------------------------------------------------------------*/
751 :
752 3950 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
753 : {
754 3160 : pp_in_fr_real[0] = p_fr_realbuffer[2 * b];
755 3160 : pp_in_fr_imag[0] = p_fr_imagbuffer[2 * b];
756 3160 : pp_in_fr_real[1] = FR_Real_Mid;
757 3160 : pp_in_fr_imag[1] = FR_Imag_Mid;
758 :
759 3160 : v_add( pp_in_fr_real[0], p_fr_realbuffer[2 * b + 1], pp_in_fr_real[1], L_FRAME48k );
760 3160 : v_add( pp_in_fr_imag[0], p_fr_imagbuffer[2 * b + 1], pp_in_fr_imag[1], L_FRAME48k );
761 :
762 9480 : for ( i = 0; i < MC_PARAMUPMIX_NCH; i++ )
763 : {
764 18960 : for ( j = 0; j < MC_PARAMUPMIX_NCH; j++ )
765 : {
766 12640 : cov_real[i][j] = hMCParamUpmix->cov_real[b][i][j];
767 12640 : cov_dtx_real[i][j] = hMCParamUpmix->cov_dtx_real[b][i][j];
768 : }
769 : }
770 :
771 3160 : 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 790 : maxbands = hMCParamUpmix->hFbMixer->pFb->filterbank_num_bands;
775 3950 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
776 : {
777 41080 : for ( bnd = 0; bnd < maxbands; bnd++ )
778 : {
779 37920 : rxy = hMCParamUpmix->cov_real[b][1][0][bnd];
780 37920 : ryy = hMCParamUpmix->cov_real[b][1][1][bnd];
781 37920 : cmat = rxy / ( ryy + EPSILON );
782 37920 : alphas[b][bnd] = 2.0f * cmat - 1.0f;
783 :
784 37920 : rxx = hMCParamUpmix->cov_real[b][0][0][bnd];
785 37920 : rxxest = cmat * cmat * ryy;
786 37920 : drxx = rxx - rxxest;
787 37920 : drxx = max( drxx, 0.0f );
788 37920 : wetaux = sqrtf( drxx / ( ryy + EPSILON ) );
789 37920 : betas[b][bnd] = 2.0f * wetaux;
790 : }
791 : }
792 :
793 790 : if ( maxbands < IVAS_MAX_NUM_BANDS )
794 : {
795 0 : for ( b = 0; b < MC_PARAMUPMIX_COMBINATIONS; b++ )
796 : {
797 0 : for ( bnd = maxbands; bnd < IVAS_MAX_NUM_BANDS; bnd++ )
798 : {
799 0 : alphas[b][bnd] = 0.0f;
800 0 : betas[b][bnd] = 0.0f;
801 : }
802 : }
803 : }
804 :
805 790 : return;
806 : }
|