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 "ivas_cnst.h"
36 : #include "ivas_prot.h"
37 : #include "prot.h"
38 : #include "ivas_rom_com.h"
39 : #include <math.h>
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 :
44 : /*---------------------------------------------------------------
45 : * Local constants
46 : *---------------------------------------------------------------*/
47 :
48 : #define GAMMA_ISM_LOW_IMP 0.8f
49 : #define GAMMA_ISM_MEDIUM_IMP 1.2f
50 : #define GAMMA_ISM_HIGH_IMP 1.4f
51 :
52 : #define GAMMA_ISM_LOW_IMP2 0.9f
53 : #define GAMMA_ISM_MEDIUM_IMP2 1.2f
54 : #define GAMMA_ISM_HIGH_IMP2 1.35f
55 :
56 : #define GAMMA_ISM_LOW_IMP3 0.85f
57 : #define GAMMA_ISM_MEDIUM_IMP3 1.15f
58 : #define GAMMA_ISM_HIGH_IMP3 1.3f
59 :
60 : #define GAMMA_ISM_LOW_IMP4 0.8f
61 : #define GAMMA_ISM_MEDIUM_IMP4 1.0f
62 : #define GAMMA_ISM_HIGH_IMP4 1.2f
63 :
64 :
65 : /*---------------------------------------------------------------
66 : * ivas_omasa_ism_mode_select()
67 : *
68 : * selects the ISM mode base on IVAS total bit-rate and
69 : * the number of objects in the combined ISM MASA format mode
70 : * ---------------------------------------------------------------*/
71 :
72 : /*! r : ISM format mode */
73 1759204 : ISM_MODE ivas_omasa_ism_mode_select(
74 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
75 : const int16_t nchan_ism /* i : number of input ISM's */
76 : )
77 : {
78 1759204 : ISM_MODE ism_mode = ISM_MODE_NONE;
79 :
80 1759204 : switch ( nchan_ism )
81 : {
82 127484 : case 1:
83 127484 : if ( ivas_total_brate >= IVAS_24k4 )
84 : {
85 110523 : ism_mode = ISM_MASA_MODE_DISC;
86 : }
87 : else
88 : {
89 16961 : ism_mode = ISM_MODE_NONE;
90 : }
91 127484 : break;
92 220860 : case 2:
93 220860 : if ( ivas_total_brate >= IVAS_48k )
94 : {
95 120540 : ism_mode = ISM_MASA_MODE_DISC;
96 : }
97 100320 : else if ( ivas_total_brate >= IVAS_32k )
98 : {
99 78840 : ism_mode = ISM_MASA_MODE_PARAM_ONE_OBJ;
100 : }
101 : else
102 : {
103 21480 : ism_mode = ISM_MODE_NONE;
104 : }
105 220860 : break;
106 593782 : case 3:
107 593782 : if ( ivas_total_brate >= IVAS_96k )
108 : {
109 170282 : ism_mode = ISM_MASA_MODE_DISC;
110 : }
111 423500 : else if ( ivas_total_brate >= IVAS_64k )
112 : {
113 169948 : ism_mode = ISM_MASA_MODE_PARAM_ONE_OBJ;
114 : }
115 253552 : else if ( ivas_total_brate >= IVAS_32k )
116 : {
117 212764 : ism_mode = ISM_MASA_MODE_MASA_ONE_OBJ;
118 : }
119 : else
120 : {
121 40788 : ism_mode = ISM_MODE_NONE;
122 : }
123 593782 : break;
124 817078 : case 4:
125 817078 : if ( ivas_total_brate >= IVAS_128k )
126 : {
127 266073 : ism_mode = ISM_MASA_MODE_DISC;
128 : }
129 551005 : else if ( ivas_total_brate >= IVAS_64k )
130 : {
131 228723 : ism_mode = ISM_MASA_MODE_PARAM_ONE_OBJ;
132 : }
133 322282 : else if ( ivas_total_brate >= IVAS_32k )
134 : {
135 268908 : ism_mode = ISM_MASA_MODE_MASA_ONE_OBJ;
136 : }
137 : else
138 : {
139 53374 : ism_mode = ISM_MODE_NONE;
140 : }
141 817078 : break;
142 : }
143 :
144 1759204 : return ism_mode;
145 : }
146 :
147 :
148 : /*---------------------------------------------------------------
149 : * ivas_set_omasa_TC()
150 : *
151 : * set number of transport channels in OMASA format
152 : * ---------------------------------------------------------------*/
153 :
154 186952 : void ivas_set_omasa_TC(
155 : const ISM_MODE ism_mode, /* i : ISM mode */
156 : const int16_t nchan_ism, /* i : number of input ISMs */
157 : int16_t *nSCE, /* o : number of SCEs */
158 : int16_t *nCPE /* o : number of CPEs */
159 : )
160 : {
161 186952 : switch ( ism_mode )
162 : {
163 84015 : case ISM_MASA_MODE_MASA_ONE_OBJ:
164 : case ISM_MASA_MODE_PARAM_ONE_OBJ:
165 84015 : *nCPE = 1;
166 84015 : *nSCE = 1;
167 84015 : break;
168 51633 : case ISM_MASA_MODE_DISC:
169 51633 : *nCPE = 1;
170 51633 : *nSCE = nchan_ism;
171 51633 : break;
172 51304 : case ISM_MODE_NONE:
173 51304 : *nCPE = 1;
174 51304 : *nSCE = 0;
175 51304 : break;
176 0 : default:
177 0 : break;
178 : }
179 :
180 186952 : return;
181 : }
182 :
183 :
184 : /*---------------------------------------------------------------
185 : * ivas_interformat_brate()
186 : *
187 : * Bit-budget distribution in case of combined-format coding
188 : * ---------------------------------------------------------------*/
189 :
190 : /*! r: adjusted bitrate */
191 1535956 : int32_t ivas_interformat_brate(
192 : const ISM_MODE ism_mode, /* i : ISM mode */
193 : const int16_t nchan_ism, /* i : number of ISM channels */
194 : const int32_t element_brate, /* i : element bitrate */
195 : const int16_t ism_imp, /* i : ISM importance flag */
196 : const int16_t limit_flag /* i : flag to limit the bitrate increase */
197 : )
198 : {
199 : int32_t element_brate_out;
200 : int16_t nBits, limit_low, limit_high;
201 :
202 1535956 : nBits = (int16_t) ( element_brate / FRAMES_PER_SEC );
203 :
204 1535956 : if ( ism_imp == ISM_INACTIVE_IMP )
205 : {
206 62869 : nBits = BITS_ISM_INACTIVE;
207 : }
208 : else
209 : {
210 1473087 : if ( ism_mode == ISM_MASA_MODE_DISC && ( ( nchan_ism == 4 && element_brate == 24000 ) || ( nchan_ism == 3 && element_brate <= 24000 ) || ( nchan_ism == 2 && element_brate <= 11000 ) ) ) /* for border case in DISC mode */
211 : {
212 162487 : if ( limit_flag == 1 && ( ( nchan_ism == 4 && element_brate == 24000 ) || ( nchan_ism == 3 && element_brate == 20000 ) || ( nchan_ism == 2 && element_brate <= 11000 ) ) )
213 : {
214 134732 : return element_brate;
215 : }
216 :
217 27755 : if ( ism_imp == ISM_LOW_IMP )
218 : {
219 4212 : nBits = (int16_t) ( nBits * GAMMA_ISM_LOW_IMP4 );
220 : }
221 23543 : else if ( ism_imp == ISM_MEDIUM_IMP )
222 : {
223 5645 : nBits = (int16_t) ( nBits * GAMMA_ISM_MEDIUM_IMP4 );
224 5645 : if ( limit_flag == -1 )
225 : {
226 0 : nBits = (int16_t) ( nBits * GAMMA_ISM_HIGH_IMP4 );
227 : }
228 : }
229 : else /* ISM_HIGH_IMP */
230 : {
231 17898 : nBits = (int16_t) ( nBits * GAMMA_ISM_HIGH_IMP4 );
232 17898 : if ( limit_flag == -1 )
233 : {
234 48 : nBits = (int16_t) ( nBits * GAMMA_ISM_HIGH_IMP4 );
235 : }
236 : }
237 : }
238 1310600 : else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ ||
239 783513 : ( ism_mode == ISM_MASA_MODE_DISC && element_brate == 9600 ) /* this condition corresponds to the ivas_total_brate = 24400 and 1 object */
240 : )
241 : {
242 291171 : if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && element_brate == IVAS_13k2 )
243 : {
244 47962 : if ( ism_imp == ISM_LOW_IMP )
245 : {
246 4603 : nBits = (int16_t) ( nBits * GAMMA_ISM_LOW_IMP3 );
247 : }
248 43359 : else if ( ism_imp == ISM_MEDIUM_IMP )
249 : {
250 11106 : nBits = (int16_t) ( nBits * GAMMA_ISM_MEDIUM_IMP3 );
251 : }
252 : else /* ISM_HIGH_IMP */
253 : {
254 32253 : nBits = (int16_t) ( nBits * GAMMA_ISM_HIGH_IMP4 );
255 : }
256 : }
257 : else
258 : {
259 243209 : if ( ism_imp == ISM_LOW_IMP )
260 : {
261 26916 : nBits = (int16_t) ( nBits * GAMMA_ISM_LOW_IMP3 );
262 : }
263 216293 : else if ( ism_imp == ISM_MEDIUM_IMP )
264 : {
265 72029 : nBits = (int16_t) ( nBits * GAMMA_ISM_MEDIUM_IMP3 );
266 : }
267 : else /* ISM_HIGH_IMP */
268 : {
269 144264 : nBits = (int16_t) ( nBits * GAMMA_ISM_HIGH_IMP3 );
270 : }
271 : }
272 : }
273 1019429 : else if ( ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ && element_brate == 16000 )
274 : {
275 129308 : if ( ism_imp == ISM_LOW_IMP )
276 : {
277 17862 : nBits = (int16_t) ( nBits * GAMMA_ISM_LOW_IMP );
278 : }
279 111446 : else if ( ism_imp == ISM_MEDIUM_IMP )
280 : {
281 38447 : nBits = (int16_t) ( nBits * GAMMA_ISM_MEDIUM_IMP );
282 : }
283 : else /* ISM_HIGH_IMP */
284 : {
285 72999 : nBits = (int16_t) ( nBits * GAMMA_ISM_HIGH_IMP3 );
286 : }
287 : }
288 : else
289 : {
290 890121 : if ( ism_imp == ISM_LOW_IMP )
291 : {
292 80516 : nBits = (int16_t) ( nBits * GAMMA_ISM_LOW_IMP );
293 : }
294 809605 : else if ( ism_imp == ISM_MEDIUM_IMP )
295 : {
296 180301 : nBits = (int16_t) ( nBits * GAMMA_ISM_MEDIUM_IMP );
297 : }
298 : else /* ISM_HIGH_IMP */
299 : {
300 629304 : nBits = (int16_t) ( nBits * GAMMA_ISM_HIGH_IMP );
301 : }
302 : }
303 : }
304 :
305 1401224 : limit_low = MIN_BRATE_SWB_BWE / FRAMES_PER_SEC;
306 1401224 : if ( ism_imp == ISM_INACTIVE_IMP )
307 : {
308 62869 : limit_low = BITS_ISM_INACTIVE;
309 : }
310 1338355 : else if ( element_brate >= SCE_CORE_16k_LOW_LIMIT )
311 : {
312 944449 : limit_low = SCE_CORE_16k_LOW_LIMIT / FRAMES_PER_SEC;
313 : }
314 :
315 1401224 : limit_high = IVAS_512k / FRAMES_PER_SEC;
316 1401224 : if ( element_brate < SCE_CORE_16k_LOW_LIMIT )
317 : {
318 407215 : limit_high = ACELP_12k8_HIGH_LIMIT / FRAMES_PER_SEC;
319 : }
320 :
321 1401224 : nBits = check_bounds_s( nBits, limit_low, limit_high );
322 :
323 1401224 : element_brate_out = nBits * FRAMES_PER_SEC;
324 :
325 1401224 : return element_brate_out;
326 : }
327 :
328 :
329 : /*---------------------------------------------------------------
330 : * ivas_combined_format_brate_sanity()
331 : *
332 : * Sanity check in combined format coding
333 : * ---------------------------------------------------------------*/
334 :
335 49296 : void ivas_combined_format_brate_sanity(
336 : const int32_t element_brate, /* i : element bitrate */
337 : const int16_t core, /* i : core */
338 : const int32_t total_brate, /* i : total bitrate */
339 : int32_t *core_brate, /* i/o: core bitrate */
340 : int16_t *inactive_coder_type_flag, /* o : inactive coder_type flag */
341 : int16_t *diff_nBits /* o : number of differential bits */
342 : )
343 : {
344 : int16_t limit_high, nBits;
345 : int32_t brate_diff;
346 :
347 49296 : brate_diff = total_brate - *core_brate;
348 :
349 : /* sanity check: at lowest IVAS bit-rates and one ISM channel coded by
350 : low-rate core-coder mode, it can happen that the CPE (MASA) bit-budget
351 : for ACELP core-coding @12.8 kHz is too high */
352 :
353 49296 : if ( element_brate < ACELP_12k8_HIGH_LIMIT )
354 : {
355 22632 : limit_high = ACELP_12k8_HIGH_LIMIT / FRAMES_PER_SEC;
356 22632 : nBits = (int16_t) ( *core_brate / FRAMES_PER_SEC );
357 :
358 22632 : *diff_nBits = nBits - limit_high;
359 22632 : if ( *diff_nBits > 0 )
360 : {
361 4880 : if ( core == TCX_20_CORE || core == TCX_10_CORE )
362 : {
363 4028 : *diff_nBits = 0;
364 : }
365 : else /* ACELP core */
366 : {
367 852 : *core_brate -= ( *diff_nBits * FRAMES_PER_SEC );
368 : }
369 : }
370 : }
371 :
372 : /*-----------------------------------------------------------------*
373 : * set inactive coder_type flag in ACELP core
374 : *-----------------------------------------------------------------*/
375 :
376 49296 : if ( core == ACELP_CORE )
377 : {
378 16897 : *inactive_coder_type_flag = 0; /* AVQ by default */
379 16897 : if ( *core_brate + brate_diff <= MAX_GSC_INACTIVE_BRATE )
380 : {
381 11867 : *inactive_coder_type_flag = 1; /* GSC */
382 : }
383 : }
384 :
385 49296 : return;
386 : }
387 :
388 :
389 : /*---------------------------------------------------------------
390 : * bits_index_ism_ratio()
391 : *
392 : *
393 : * ---------------------------------------------------------------*/
394 :
395 : /*!r : number of bits for ISM ratio index */
396 161782 : int16_t bits_index_ism_ratio(
397 : const int16_t nchan_ism /* i : number of objects */
398 : )
399 : {
400 : int16_t bits_index;
401 :
402 161782 : bits_index = 0;
403 161782 : if ( nchan_ism == 2 )
404 : {
405 21328 : bits_index = 3;
406 : }
407 140454 : else if ( nchan_ism == 3 )
408 : {
409 55368 : bits_index = 6;
410 : }
411 85086 : else if ( nchan_ism == 4 )
412 : {
413 85086 : bits_index = 7;
414 : }
415 : else
416 : {
417 0 : assert( ( nchan_ism >= 2 && nchan_ism <= 4 ) && "Wrong number of objects for MASA_ISM." );
418 : }
419 :
420 161782 : return bits_index;
421 : }
422 :
423 :
424 : /*---------------------------------------------------------------
425 : * calculate_nbits_meta()
426 : *
427 : *
428 : * ---------------------------------------------------------------*/
429 :
430 234484 : void calculate_nbits_meta(
431 : const int16_t nchan_ism,
432 : float q_energy_ratio_ism[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS][MAX_NUM_OBJECTS],
433 : float masa_to_total_energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],
434 : const int16_t numSf,
435 : const int16_t numCodingBands,
436 : int16_t *bits_ism,
437 : const int16_t idx_sep_obj,
438 : const int16_t ism_imp )
439 : {
440 : int16_t sf, band, obj;
441 : float priority[MAX_NUM_OBJECTS], max_p;
442 :
443 234484 : if ( nchan_ism > 1 )
444 : {
445 234484 : set_f( priority, 0.0f, nchan_ism );
446 805133 : for ( sf = 0; sf < numSf; sf++ )
447 : {
448 4000072 : for ( band = 0; band < numCodingBands; band++ )
449 : {
450 15399493 : for ( obj = 0; obj < nchan_ism; obj++ )
451 : {
452 11970070 : priority[obj] = max( priority[obj], ( q_energy_ratio_ism[sf][band][obj] * ( 1 - masa_to_total_energy_ratio[sf][band] ) ) );
453 : }
454 : }
455 : }
456 : }
457 : else
458 : {
459 0 : priority[0] = 1;
460 : }
461 :
462 : /* decide parameters for ISM metadata quantization */
463 234484 : maximum( priority, nchan_ism, &max_p );
464 1012458 : for ( obj = 0; obj < nchan_ism; obj++ )
465 : {
466 777974 : if ( obj == idx_sep_obj )
467 : {
468 234484 : if ( ism_imp == 3 )
469 : {
470 138076 : priority[obj] = 1;
471 : }
472 96408 : else if ( ism_imp == 2 )
473 : {
474 65704 : priority[obj] = ( 1 + max_p ) * 0.5f;
475 : }
476 : else
477 : {
478 30704 : priority[obj] = max_p;
479 : }
480 : }
481 777974 : bits_ism[obj] = bits_direction_masa[0] - (int16_t) ( ( 1 - ( (int16_t) ( priority[obj] * 1000.0f ) ) * 0.001f ) * 6 );
482 : }
483 :
484 234484 : return;
485 : }
486 :
487 :
488 : /*---------------------------------------------------------------
489 : * ivas_get_stereo_panning_gains()
490 : *
491 : *
492 : *---------------------------------------------------------------*/
493 :
494 3011455 : void ivas_get_stereo_panning_gains(
495 : const float aziDeg,
496 : const float eleDeg,
497 : float panningGains[2] )
498 : {
499 : float aziRad, eleRad;
500 : float y, mappedX, aziRadMapped, A, A2, A3;
501 3011455 : const float LsAngleRad = 30.0f * PI_OVER_180;
502 : /* Convert azi and ele to an azi value of the cone of confusion */
503 3011455 : aziRad = aziDeg * PI_OVER_180;
504 3011455 : eleRad = eleDeg * PI_OVER_180;
505 3011455 : y = ( sinf( aziRad ) * cosf( eleRad ) );
506 3011455 : mappedX = sqrtf( max( 0.0f, 1.0f - ( y * y ) ) );
507 3011455 : aziRadMapped = atan2f( y, mappedX );
508 :
509 3011455 : if ( aziRadMapped >= LsAngleRad )
510 : { /* Left side */
511 635519 : panningGains[0] = 1.0f;
512 635519 : panningGains[1] = 0.0f;
513 : }
514 2375936 : else if ( aziRadMapped <= -LsAngleRad )
515 : { /* Right side */
516 645586 : panningGains[0] = 0.0f;
517 645586 : panningGains[1] = 1.0f;
518 : }
519 : else /* Tangent panning law */
520 : {
521 1730350 : A = tanf( aziRadMapped ) / tanf( LsAngleRad );
522 1730350 : A2 = ( A - 1.0f ) / max( 0.001f, A + 1.0f );
523 1730350 : A3 = 1.0f / ( A2 * A2 + 1.0f );
524 1730350 : panningGains[0] = sqrtf( A3 );
525 1730350 : panningGains[1] = sqrtf( 1.0f - A3 );
526 : }
527 :
528 3011455 : return;
529 : }
530 :
531 :
532 : /*---------------------------------------------------------------
533 : * calculate_brate_limit_flag()
534 : *
535 : *
536 : *---------------------------------------------------------------*/
537 :
538 : /*! r: limitation flag */
539 407620 : int16_t calculate_brate_limit_flag(
540 : const int16_t ism_imp[], /* i : ISM importance flags */
541 : const int16_t nchan_ism /* i : number of objects */
542 : )
543 : {
544 : int16_t n;
545 : int16_t brate_limit_flag;
546 : int16_t nzeros;
547 :
548 407620 : brate_limit_flag = 0;
549 407620 : nzeros = 0;
550 1461405 : for ( n = 0; n < nchan_ism; n++ )
551 : {
552 1053785 : brate_limit_flag += ism_imp[n];
553 1053785 : if ( ism_imp[n] == 0 )
554 : {
555 48595 : nzeros++;
556 : }
557 : }
558 :
559 407620 : if ( brate_limit_flag >= (int16_t) ( nchan_ism * 2.5f ) )
560 : {
561 334154 : brate_limit_flag = 1;
562 : }
563 : else
564 : {
565 73466 : if ( nzeros / (float) nchan_ism >= 0.5f )
566 : {
567 16709 : brate_limit_flag = -1; /* there is no limitation, on the contrary */
568 : }
569 : }
570 :
571 407620 : return brate_limit_flag;
572 : }
|