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 "options.h"
34 : #include <stdlib.h>
35 : #include <assert.h>
36 : #include <math.h>
37 : #include "ivas_cnst.h"
38 : #include "ivas_prot.h"
39 : #include "prot.h"
40 : #include "ivas_rom_com.h"
41 : #include "ivas_rom_enc.h"
42 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 : #include "wmc_auto.h"
46 :
47 :
48 : /*-------------------------------------------------------------------------
49 : * Local function prototypes
50 : *------------------------------------------------------------------------*/
51 :
52 : static void ivas_osba_render_ism_to_sba( float *data_in_f[], float data_out_f[][L_FRAME48k], const int16_t input_frame, const int16_t nchan_sba, const int16_t nchan_ism, ISM_METADATA_HANDLE hIsmMeta[], float prev_gains[][MAX_INPUT_CHANNELS], const float interpolator[L_FRAME48k] );
53 :
54 : /*-------------------------------------------------------------------*
55 : * ivas_merge_sba_transports()
56 : *
57 : * Merge SBA transport channels
58 : *-------------------------------------------------------------------*/
59 :
60 14920 : static void ivas_merge_sba_transports(
61 : float data_in_f1[][L_FRAME48k],
62 : float *data_in_f2[],
63 : float *data_out_f[],
64 : const int16_t input_frame,
65 : const int16_t sba_analysis_order )
66 : {
67 : int16_t i, j, nchan_sba;
68 :
69 14920 : nchan_sba = ( sba_analysis_order + 1 ) * ( sba_analysis_order + 1 );
70 :
71 74600 : for ( i = 0; i < nchan_sba; i++ )
72 : {
73 51950880 : for ( j = 0; j < input_frame; j++ )
74 : {
75 51891200 : data_out_f[i][j] = 0.5f * ( data_in_f1[i][j] + data_in_f2[i][j] );
76 : }
77 : }
78 :
79 14920 : return;
80 : }
81 :
82 : /*--------------------------------------------------------------------------*
83 : * ivas_osba_enc_open()
84 : *
85 : * Allocate and initialize OMASA handle
86 : *--------------------------------------------------------------------------*/
87 :
88 37 : ivas_error ivas_osba_enc_open(
89 : Encoder_Struct *st_ivas /* i/o: IVAS encoder handle */
90 : )
91 : {
92 : int16_t i;
93 : OSBA_ENC_HANDLE hOSba;
94 : int16_t input_frame;
95 : ivas_error error;
96 : int16_t len;
97 37 : error = IVAS_ERR_OK;
98 :
99 37 : if ( ( hOSba = (OSBA_ENC_HANDLE) malloc( sizeof( OSBA_ENC_DATA ) ) ) == NULL )
100 : {
101 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for OMASA encoder\n" ) );
102 : }
103 :
104 185 : for ( i = 0; i < MAX_NUM_OBJECTS; i++ )
105 : {
106 148 : set_f( hOSba->prev_object_dm_gains[i], INV_SQRT_2, MAX_INPUT_CHANNELS );
107 : }
108 :
109 37 : len = NS2SA( st_ivas->hEncoderConfig->input_Fs, IVAS_FB_ENC_DELAY_NS );
110 :
111 147 : for ( i = 0; i < st_ivas->hEncoderConfig->nchan_ism; i++ )
112 : {
113 110 : if ( ( hOSba->input_data_mem[i] = (float *) malloc( len * sizeof( float ) ) ) == NULL )
114 : {
115 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for OSBA input buffers" );
116 : }
117 110 : set_f( hOSba->input_data_mem[i], 0.0f, len );
118 : }
119 :
120 75 : for ( ; i < MAX_NUM_OBJECTS; i++ )
121 : {
122 38 : hOSba->input_data_mem[i] = NULL;
123 : }
124 :
125 37 : input_frame = (int16_t) ( st_ivas->hEncoderConfig->input_Fs / FRAMES_PER_SEC );
126 31717 : for ( i = 0; i < input_frame; i++ )
127 : {
128 31680 : hOSba->interpolator[i] = ( (float) i ) / ( (float) input_frame );
129 : }
130 :
131 37 : st_ivas->hOSba = hOSba;
132 :
133 37 : return error;
134 : }
135 :
136 :
137 : /*--------------------------------------------------------------------------*
138 : * ivas_omasa_enc_close()
139 : *
140 : * Close OMASA handle
141 : *--------------------------------------------------------------------------*/
142 :
143 627 : void ivas_osba_enc_close(
144 : OSBA_ENC_HANDLE *hOSba /* i/o: encoder OSBA handle */
145 : )
146 : {
147 627 : if ( hOSba == NULL || *hOSba == NULL )
148 : {
149 590 : return;
150 : }
151 :
152 185 : for ( int16_t n = 0; n < MAX_NUM_OBJECTS; n++ )
153 : {
154 148 : if ( ( *hOSba )->input_data_mem[n] != NULL )
155 : {
156 110 : free( ( *hOSba )->input_data_mem[n] );
157 110 : ( *hOSba )->input_data_mem[n] = NULL;
158 : }
159 : }
160 :
161 37 : free( *hOSba );
162 37 : ( *hOSba ) = NULL;
163 :
164 37 : return;
165 : }
166 :
167 :
168 : /*--------------------------------------------------------------------------*
169 : * ivas_osba_enc_reconfig()
170 : *
171 : * oSBA encoder reconfiguration
172 : *--------------------------------------------------------------------------*/
173 :
174 37000 : ivas_error ivas_osba_enc_reconfig(
175 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
176 : )
177 : {
178 :
179 : int16_t n, nSCE_old, nCPE_old, nchan_transport_old;
180 : ISM_MODE old_ism_mode;
181 : int32_t ivas_total_brate;
182 : ivas_error error;
183 : ENCODER_CONFIG_HANDLE hEncoderConfig;
184 :
185 :
186 37000 : error = IVAS_ERR_OK;
187 37000 : hEncoderConfig = st_ivas->hEncoderConfig;
188 37000 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
189 : int16_t nchan_transport;
190 :
191 37000 : if ( ivas_total_brate != hEncoderConfig->last_ivas_total_brate )
192 : {
193 653 : DIRAC_ENC_HANDLE hDirAC = st_ivas->hDirAC;
194 : SPAR_ENC_HANDLE hSpar;
195 : int16_t analysis_order_old;
196 : int16_t spar_reconfig_flag;
197 : int16_t nbands_old;
198 : int16_t ndir_old;
199 :
200 653 : spar_reconfig_flag = 0;
201 653 : old_ism_mode = st_ivas->ism_mode;
202 653 : st_ivas->ism_mode = ivas_osba_ism_mode_select( ivas_total_brate, st_ivas->hEncoderConfig->nchan_ism );
203 653 : nchan_transport_old = st_ivas->nchan_transport;
204 653 : nCPE_old = st_ivas->nCPE;
205 653 : nSCE_old = st_ivas->nSCE;
206 :
207 653 : st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, hEncoderConfig->sba_order );
208 653 : analysis_order_old = ivas_sba_get_analysis_order( hEncoderConfig->last_ivas_total_brate, hEncoderConfig->sba_order );
209 653 : nbands_old = st_ivas->hQMetaData->q_direction->cfg.nbands;
210 653 : ndir_old = st_ivas->hQMetaData->no_directions;
211 :
212 653 : if ( ( analysis_order_old != st_ivas->sba_analysis_order ) || ( old_ism_mode != st_ivas->ism_mode ) )
213 : {
214 : int16_t i, n_old;
215 : float **old_mem_hp20_in;
216 :
217 396 : n_old = st_ivas->hEncoderConfig->nchan_ism + ( analysis_order_old + 1 ) * ( analysis_order_old + 1 );
218 396 : n = st_ivas->hEncoderConfig->nchan_ism + ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
219 :
220 396 : if ( n > n_old )
221 : {
222 : /* save old mem_hp_20 pointer */
223 92 : old_mem_hp20_in = st_ivas->mem_hp20_in;
224 92 : st_ivas->mem_hp20_in = NULL;
225 :
226 92 : if ( ( st_ivas->mem_hp20_in = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
227 : {
228 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
229 : }
230 :
231 762 : for ( i = 0; i < n_old; i++ )
232 : {
233 670 : st_ivas->mem_hp20_in[i] = old_mem_hp20_in[i];
234 670 : old_mem_hp20_in[i] = NULL;
235 : }
236 : /* create additional hp20 memories */
237 909 : for ( ; i < n; i++ )
238 : {
239 817 : if ( ( st_ivas->mem_hp20_in[i] = (float *) malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
240 : {
241 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
242 : }
243 :
244 817 : set_f( st_ivas->mem_hp20_in[i], 0.0f, L_HP20_MEM );
245 : }
246 :
247 92 : free( old_mem_hp20_in );
248 92 : old_mem_hp20_in = NULL;
249 : }
250 304 : else if ( n < n_old )
251 : {
252 : /* save old mem_hp_20 pointer */
253 87 : old_mem_hp20_in = st_ivas->mem_hp20_in;
254 87 : st_ivas->mem_hp20_in = NULL;
255 :
256 87 : if ( ( st_ivas->mem_hp20_in = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
257 : {
258 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
259 : }
260 :
261 720 : for ( i = 0; i < n; i++ )
262 : {
263 633 : st_ivas->mem_hp20_in[i] = old_mem_hp20_in[i];
264 633 : old_mem_hp20_in[i] = NULL;
265 : }
266 : /* remove superfluous hp20 memories */
267 858 : for ( ; i < n_old; i++ )
268 : {
269 771 : free( old_mem_hp20_in[i] );
270 771 : old_mem_hp20_in[i] = NULL;
271 : }
272 :
273 87 : free( old_mem_hp20_in );
274 87 : old_mem_hp20_in = NULL;
275 : }
276 : }
277 :
278 653 : ivas_spar_config( ivas_total_brate, min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ), &st_ivas->nchan_transport, &st_ivas->nSCE, &st_ivas->nCPE, &st_ivas->hSpar->core_nominal_brate, -1 );
279 :
280 653 : hSpar = st_ivas->hSpar;
281 :
282 653 : if ( st_ivas->nchan_transport == 1 )
283 : {
284 228 : hEncoderConfig->element_mode_init = IVAS_SCE;
285 : }
286 : else
287 : {
288 425 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
289 : }
290 653 : if ( nchan_transport_old != st_ivas->nchan_transport || ( ivas_total_brate < IVAS_512k && hEncoderConfig->last_ivas_total_brate >= IVAS_512k ) || ( ivas_total_brate >= IVAS_512k && hEncoderConfig->last_ivas_total_brate < IVAS_512k ) )
291 : {
292 : /* FB mixer handle */
293 540 : if ( hDirAC->hFbMixer != NULL )
294 : {
295 0 : ivas_FB_mixer_close( &( hDirAC->hFbMixer ), hEncoderConfig->input_Fs, 0 );
296 0 : hDirAC->hFbMixer = NULL;
297 : }
298 540 : spar_reconfig_flag = 1;
299 540 : ivas_spar_enc_close( &( st_ivas->hSpar ), hEncoderConfig->input_Fs, hEncoderConfig->nchan_inp, spar_reconfig_flag );
300 :
301 540 : if ( ( error = ivas_spar_enc_open( st_ivas, spar_reconfig_flag ) ) != IVAS_ERR_OK )
302 : {
303 0 : return error;
304 : }
305 : }
306 653 : st_ivas->hSpar->spar_reconfig_flag = spar_reconfig_flag;
307 653 : if ( ( error = ivas_dirac_enc_reconfigure( st_ivas ) ) != IVAS_ERR_OK )
308 : {
309 0 : return error;
310 : }
311 653 : if ( st_ivas->hQMetaData->q_direction->cfg.nbands != nbands_old || st_ivas->hQMetaData->no_directions != ndir_old )
312 : {
313 : int16_t dir, j, i;
314 334 : IVAS_QDIRECTION *q_direction = st_ivas->hQMetaData->q_direction;
315 730 : for ( dir = 0; dir < st_ivas->hQMetaData->no_directions; dir++ )
316 : {
317 2447 : for ( j = 0; j < q_direction[dir].cfg.nbands; j++ )
318 : {
319 10255 : for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
320 : {
321 8204 : q_direction[dir].band_data[j].energy_ratio_index[i] = 0;
322 8204 : q_direction[dir].band_data[j].energy_ratio_index_mod[i] = 0;
323 : }
324 : }
325 : }
326 : }
327 653 : hSpar->enc_param_start_band = hDirAC->hConfig->enc_param_start_band;
328 :
329 : /*-----------------------------------------------------------------*
330 : * Allocate, initialize, and configure SCE/CPE/MCT handles
331 : *-----------------------------------------------------------------*/
332 :
333 653 : nchan_transport = st_ivas->nchan_transport;
334 653 : if ( old_ism_mode == ISM_MODE_NONE && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
335 : {
336 : {
337 186 : nchan_transport = st_ivas->nchan_transport + st_ivas->hEncoderConfig->nchan_ism;
338 186 : st_ivas->nCPE = ( nchan_transport + 1 ) >> 1;
339 : }
340 : }
341 467 : else if ( old_ism_mode == ISM_SBA_MODE_DISC && st_ivas->ism_mode == ISM_MODE_NONE )
342 : {
343 :
344 180 : nchan_transport_old += st_ivas->hEncoderConfig->nchan_ism;
345 180 : nchan_transport = st_ivas->nchan_transport;
346 : }
347 287 : else if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
348 : {
349 69 : nchan_transport_old += st_ivas->hEncoderConfig->nchan_ism;
350 69 : nchan_transport = st_ivas->nchan_transport + st_ivas->hEncoderConfig->nchan_ism;
351 69 : st_ivas->nCPE = ( nchan_transport + 1 ) >> 1;
352 : }
353 :
354 653 : if ( ( error = ivas_corecoder_enc_reconfig( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, ivas_total_brate / st_ivas->nchan_transport, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS, MC_MODE_NONE ) ) != IVAS_ERR_OK )
355 : {
356 0 : return error;
357 : }
358 : }
359 :
360 37000 : return error;
361 : }
362 :
363 :
364 : /*--------------------------------------------------------------------------*
365 : * ivas_osba_enc()
366 : *
367 : * Main OSBA encoding function
368 : *--------------------------------------------------------------------------*/
369 :
370 37000 : void ivas_osba_enc(
371 : OSBA_ENC_HANDLE hOSba, /* i/o: OSBA encoder handle */
372 : ISM_METADATA_HANDLE hIsmMeta[], /* i/o: ISM metadata handle */
373 : float *data_in_f[], /* i/o: Input / transport audio signals */
374 : const int16_t input_frame, /* i : Input frame size */
375 : const int16_t nchan_ism, /* i : Number of objects for parameter analysis */
376 : const ISM_MODE ism_mode, /* i : ISM mode */
377 : const int16_t sba_analysis_order, /* i : SBA order evaluated in DirAC/SPAR encoder */
378 : const int32_t input_Fs, /* i : input sampling rate */
379 : const int16_t sba_planar /* i : planar SBA flag */
380 : )
381 : {
382 : float data_out_f[MAX_INPUT_CHANNELS][L_FRAME48k];
383 : int16_t n, delay_s;
384 37000 : delay_s = NS2SA( input_Fs, IVAS_FB_ENC_DELAY_NS );
385 :
386 37000 : if ( ism_mode == ISM_MODE_NONE )
387 : {
388 : /*keep the delay buffer up to date*/
389 55560 : for ( n = 0; n < nchan_ism; n++ )
390 : {
391 40640 : mvr2r( &data_in_f[n][input_frame - delay_s], hOSba->input_data_mem[n], delay_s );
392 : }
393 :
394 : /* Convert ISM to SBA */
395 14920 : ivas_osba_render_ism_to_sba( data_in_f, data_out_f, input_frame, sba_analysis_order, nchan_ism, hIsmMeta, hOSba->prev_object_dm_gains, hOSba->interpolator );
396 :
397 14920 : if ( sba_planar )
398 : {
399 0 : ivas_sba_zero_vert_comp( &( data_in_f[nchan_ism] ), sba_analysis_order, sba_planar, input_frame );
400 : }
401 :
402 : /* Merge SBA signals */
403 14920 : ivas_merge_sba_transports( data_out_f, &( data_in_f[nchan_ism] ), data_in_f, input_frame, sba_analysis_order );
404 : }
405 : else
406 : {
407 : int16_t azimuth, elevation;
408 : /* delay ISM input channels to match the SBA encoder delay */
409 91440 : for ( n = 0; n < nchan_ism; n++ )
410 : {
411 69360 : delay_signal( data_in_f[n], input_frame, hOSba->input_data_mem[n], delay_s );
412 :
413 :
414 : /*keep prev gain upto date for bitrate switching*/
415 69360 : azimuth = (int16_t) floorf( hIsmMeta[n]->azimuth + 0.5f );
416 69360 : elevation = (int16_t) floorf( hIsmMeta[n]->elevation + 0.5f );
417 69360 : ivas_dirac_dec_get_response( azimuth, elevation, hOSba->prev_object_dm_gains[n], sba_analysis_order );
418 : }
419 : }
420 :
421 : /* Set the number of objects */
422 37000 : hOSba->nchan_ism = nchan_ism;
423 :
424 37000 : return;
425 : }
426 :
427 :
428 : /*--------------------------------------------------------------------------*
429 : * Local functions
430 : *--------------------------------------------------------------------------*/
431 :
432 : /* Render ISMs to SBA */
433 14920 : static void ivas_osba_render_ism_to_sba(
434 : float *data_in_f[],
435 : float data_out_f[][L_FRAME48k],
436 : const int16_t input_frame,
437 : const int16_t sba_analysis_order,
438 : const int16_t nchan_ism,
439 : ISM_METADATA_HANDLE hIsmMeta[],
440 : float prev_gains[][MAX_INPUT_CHANNELS],
441 : const float interpolator[L_FRAME48k] )
442 : {
443 : int16_t i, j, k;
444 : int16_t azimuth, elevation;
445 : float gains[MAX_INPUT_CHANNELS];
446 : float g1, g2;
447 :
448 : int16_t nchan_sba;
449 :
450 14920 : nchan_sba = ( sba_analysis_order + 1 ) * ( sba_analysis_order + 1 );
451 :
452 74600 : for ( i = 0; i < nchan_sba; i++ )
453 : {
454 59680 : set_zero( data_out_f[i], input_frame );
455 : }
456 :
457 55560 : for ( i = 0; i < nchan_ism; i++ )
458 : {
459 40640 : azimuth = (int16_t) floorf( hIsmMeta[i]->azimuth + 0.5f );
460 40640 : elevation = (int16_t) floorf( hIsmMeta[i]->elevation + 0.5f );
461 :
462 40640 : ivas_dirac_dec_get_response( azimuth, elevation, gains, sba_analysis_order );
463 :
464 : /* Render using the sh gains */
465 203200 : for ( j = 0; j < nchan_sba; j++ )
466 : {
467 162560 : if ( fabsf( gains[j] ) > 0.0 || fabsf( prev_gains[i][j] ) > 0.0f )
468 : {
469 145480862 : for ( k = 0; k < input_frame; k++ )
470 : {
471 145319360 : g1 = interpolator[k];
472 145319360 : g2 = 1.0f - g1;
473 145319360 : data_out_f[j][k] += ( g1 * gains[j] + g2 * prev_gains[i][j] ) * data_in_f[i][k];
474 : }
475 : }
476 162560 : prev_gains[i][j] = gains[j];
477 : }
478 : }
479 :
480 : /* Gain with loudness-matching gains */
481 :
482 14920 : return;
483 : }
|