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 <stdint.h>
34 : #include "options.h"
35 : #include "cnst.h"
36 : #include "ivas_cnst.h"
37 : #include "rom_com.h"
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include "ivas_rom_com.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 : #include "wmc_auto.h"
45 :
46 :
47 : /*-----------------------------------------------------------------*
48 : * ivas_decision_matrix_enc()
49 : *
50 : * Core technology selection in the IVAS codec
51 : *
52 : * CPE
53 : * bitrate 13.2 16.4 24.4 32 48 >48(*)
54 : * ----------------------------------------------------------------------------------------
55 : * WB
56 : * speech ACELP ACELP ACELP ACELP ACELP TCX
57 : * music GSC/TCX GSC/TCX TCX/HQ TCX/HQ TCX/HQ TCX
58 : * inactive ACELP-I/TCX ACELP-I/TCX ACELP-I/TCX ACELP-I/TCX ACELP-I/TCX TCX
59 : *
60 : * SWB
61 : * speech ACELP ACELP ACELP ACELP ACELP TCX
62 : * music GSC/TCX GSD/TCX TCX/HQ TCX/HQ TCX/HQ TCX
63 : * inactive ACELP-I/TCX ACELP-I/TCX ACELP-I/TCX ACELP-I/TCX ACELP-I/TCX TCX
64 : *
65 : * FB
66 : * speech ACELP ACELP TCX
67 : * music TCX/HQ TCX/HQ TCX
68 : * inactive ACELP-I/TCX ACELP-I/TCX TCX
69 : *
70 : * ACELP-I: GSC Inactive mode used for total_brate <= 28 kbps (total_brate), AVQ Inactive mode used otherwise
71 : * (*): TCX core is selected when total_brate > 48kbps, not CPE bitrate
72 : * Note: in MDCT stereo mode, TCX core is selected for all content
73 : * -------------------------------------------------------------------------------------------------------------------------------------------------------------*/
74 :
75 1129814 : void ivas_decision_matrix_enc(
76 : Encoder_State *st, /* i : encoder state structure */
77 : const int32_t element_brate, /* i : element bitrate */
78 : const float fft_buff[], /* i : FFT buffer */
79 : const float enerBuffer[], /* i : energy buffer */
80 : const int16_t last_element_mode /* i : last element mode */
81 : )
82 : {
83 : int32_t icbwe_brate;
84 : /* init */
85 1129814 : icbwe_brate = 0;
86 :
87 : /* initialization */
88 1129814 : st->core = -1;
89 1129814 : st->extl = -1;
90 1129814 : st->extl_brate = 0;
91 1129814 : if ( st->element_mode != IVAS_CPE_MDCT )
92 : {
93 415044 : st->igf = 0;
94 : }
95 :
96 : /* SID and FRAME_NO_DATA frames */
97 1129814 : if ( st->Opt_DTX_ON && ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA ) )
98 : {
99 24299 : st->core = ACELP_CORE;
100 :
101 24299 : if ( st->input_Fs >= 32000 && st->bwidth >= SWB )
102 : {
103 17460 : st->extl = SWB_CNG;
104 : }
105 :
106 24299 : st->rf_mode = 0;
107 :
108 24299 : return;
109 : }
110 :
111 : /*---------------------------------------------------------------------*
112 : * Select the core
113 : *---------------------------------------------------------------------*/
114 :
115 1105515 : if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
116 : {
117 : /* ISM low-rate mode */
118 7534 : st->core = ACELP_CORE;
119 7534 : st->coder_type = INACTIVE;
120 : }
121 1097981 : else if ( st->total_brate > MAX_ACELP_BRATE )
122 : {
123 : /* highest bitrates */
124 641045 : st->core = TCX_20_CORE;
125 : }
126 456936 : else if ( st->element_mode == IVAS_CPE_MDCT )
127 : {
128 : /* in MDCT stereo set TCX core */
129 164930 : st->core = TCX_20_CORE;
130 : }
131 292006 : else if ( st->coder_type == INACTIVE )
132 : {
133 : /* inactive frames */
134 17338 : if ( st->cng_type == FD_CNG && st->total_brate >= STEREO_TCX_MIN_RATE )
135 : {
136 10574 : st->core = TCX_20_CORE;
137 : }
138 : else
139 : {
140 6764 : st->core = ACELP_CORE;
141 : }
142 : }
143 274668 : else if ( st->sp_aud_decision1 == 0 && st->sp_aud_decision2 == 0 )
144 : {
145 : /* speech */
146 132486 : st->core = ACELP_CORE;
147 : }
148 142182 : else if ( st->sp_aud_decision1 == 1 && st->sp_aud_decision2 == 0 )
149 : {
150 : /* music w. GSC core */
151 5820 : st->core = ACELP_CORE;
152 : }
153 : else /* sp_aud_decision1 == 1 && *sp_aud_decision2 == 1 */
154 : {
155 : /* music w. TCX or HQ core */
156 136362 : st->core = TCX_20_CORE;
157 :
158 136362 : if ( st->element_mode == IVAS_CPE_TD || st->sp_aud_decision0 == 0 )
159 : {
160 45556 : st->core = TCX_20_CORE;
161 : }
162 : else
163 : {
164 : /* select TCX core or HQ core using bits_frame_nominal to match the TCX configuration bitrate */
165 90806 : st->core = mdct_classifier( st, fft_buff, enerBuffer, st->bits_frame_nominal * FRAMES_PER_SEC );
166 : }
167 : }
168 :
169 : /* do not allow TD stereo ACELP core -> DFT stereo TCX core switching as it is on the WC complexity path */
170 1105515 : if ( ( ( st->last_core == ACELP_CORE && last_element_mode == IVAS_CPE_TD && st->element_mode == IVAS_CPE_DFT ) || ( st->tdm_LRTD_flag == 1 && st->total_brate <= IVAS_16k4 ) ) && st->core == TCX_20_CORE && st->total_brate <= MAX_ACELP_BRATE ) /* Override TCX in case of LRTD && primary channel has low bitrate*/
171 : {
172 356 : st->core = ACELP_CORE;
173 : }
174 :
175 : /* sanity check: highest bitrates in ISM */
176 1105515 : if ( st->is_ism_format && st->tcxonly && st->total_brate > MAX_ACELP_BRATE_ISM )
177 : {
178 101016 : st->core = TCX_20_CORE;
179 : }
180 :
181 : #ifdef DEBUGGING
182 : if ( st->idchan == 0 )
183 : {
184 : if ( st->force == FORCE_SPEECH && st->element_mode != IVAS_CPE_MDCT && st->total_brate <= MAX_ACELP_BRATE )
185 : {
186 : st->core = ACELP_CORE;
187 : }
188 : else if ( st->force == FORCE_MUSIC && st->core == ACELP_CORE )
189 : {
190 : st->core = TCX_20_CORE;
191 : }
192 : else if ( st->force == FORCE_ACELP && st->element_mode != IVAS_CPE_MDCT && st->total_brate <= MAX_ACELP_BRATE )
193 : {
194 : st->core = ACELP_CORE;
195 : if ( st->coder_type == AUDIO )
196 : {
197 : st->coder_type = GENERIC;
198 : }
199 : }
200 : else if ( st->force == FORCE_GSC && element_brate < IVAS_24k4 )
201 : {
202 : st->core = ACELP_CORE;
203 : }
204 : else if ( st->force == FORCE_TCX )
205 : {
206 : st->core = TCX_20_CORE;
207 : }
208 : else if ( st->force == FORCE_HQ && st->element_mode != IVAS_CPE_MDCT && element_brate >= IVAS_24k4 )
209 : {
210 : st->core = HQ_CORE;
211 : }
212 : }
213 : #endif
214 :
215 : /* TCX not available at low bitrates -> replace it by GSC */
216 1105515 : if ( st->core == TCX_20_CORE && st->total_brate < STEREO_TCX_MIN_RATE )
217 : {
218 4213 : st->core = ACELP_CORE;
219 :
220 : /* In TD stereo below 24.4 kbps we cannot overwrite the `coder_type` when it is set to TRANSITION, */
221 : /* as it is used for TD stereo bit allocation. To ensure consistent bit allocation, it must remain unchanged on the decoder side. */
222 4213 : if ( st->idchan == 0 && !( element_brate < IVAS_24k4 && st->coder_type == TRANSITION && st->element_mode == IVAS_CPE_TD ) )
223 : {
224 4213 : st->coder_type = AUDIO;
225 : }
226 :
227 4213 : st->sp_aud_decision2 = 0;
228 :
229 4213 : if ( st->low_rate_mode )
230 : {
231 0 : st->coder_type = INACTIVE;
232 : }
233 : }
234 :
235 : /* sanity check to avoid too low ACELP bitrate in case of "limitation to avoid too high bitrate in one active TCX channel" at element_brate = 32000 */
236 1105515 : if ( st->is_ism_format && st->flag_ACELP16k && !st->low_rate_mode && st->core == ACELP_CORE && st->total_brate < ACELP_16k_LOW_LIMIT + FB_TBE_1k8 )
237 : {
238 0 : st->core = TCX_20_CORE;
239 : }
240 :
241 : /*---------------------------------------------------------------------*
242 : * Select ACELP and GSC extension layer
243 : *---------------------------------------------------------------------*/
244 :
245 1105515 : if ( st->core == ACELP_CORE )
246 : {
247 : /* WB */
248 156388 : if ( st->bwidth == WB )
249 : {
250 28814 : if ( st->total_brate < MIN_BRATE_WB_BWE || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) )
251 : {
252 8382 : st->extl = WB_BWE;
253 : }
254 20432 : else if ( st->total_brate >= MIN_BRATE_WB_BWE && !st->flag_ACELP16k )
255 : {
256 11341 : if ( ( st->sp_aud_decision1 == 1 && st->sp_aud_decision2 == 0 ) || st->coder_type == INACTIVE )
257 : {
258 4100 : st->extl = WB_BWE;
259 4100 : st->extl_brate = WB_BWE_0k35;
260 : }
261 : else
262 : {
263 7241 : st->extl = WB_TBE;
264 :
265 7241 : if ( st->total_brate < MIN_BRATE_WB_TBE_1k05 || ( st->element_mode == IVAS_CPE_TD && st->total_brate < MIN_TDM_BRATE_WB_TBE_1k05 ) )
266 : {
267 2457 : st->extl_brate = WB_TBE_0k35;
268 : }
269 : else
270 : {
271 4784 : st->extl_brate = WB_TBE_1k05;
272 : }
273 : }
274 : }
275 : }
276 :
277 : /* SWB and FB */
278 127574 : else if ( st->bwidth == SWB || st->bwidth == FB )
279 : {
280 127574 : if ( st->total_brate >= MIN_BRATE_SWB_BWE /*&& (*coder_type != AUDIO || st->total_brate >= (MIN_BWE_PRI_BRATE+600))*/
281 287 : || ( st->total_brate >= MIN_MIN_BRATE_LRTD_SWB_BWE && st->element_mode == IVAS_CPE_TD && st->bwidth == SWB && st->tdm_LRTD_flag ) || ( element_brate < IVAS_16k4 && st->total_brate >= MIN_MIN_BRATE_LRTD_SWB_BWE && st->element_mode == IVAS_CPE_TD && st->bwidth == SWB ) )
282 : {
283 127574 : if ( ( ( ( st->sp_aud_decision1 == 1 && st->sp_aud_decision2 == 0 ) || st->coder_type == INACTIVE ) && !st->GSC_noisy_speech ) /* Note: SWB BWE is not used for GSC noisy speech */ )
284 : {
285 11916 : st->extl = SWB_BWE;
286 11916 : st->extl_brate = SWB_BWE_1k6;
287 :
288 11916 : if ( st->bwidth == FB )
289 : {
290 4771 : st->extl = FB_BWE;
291 4771 : st->extl_brate = FB_BWE_1k8;
292 : }
293 : }
294 : else
295 : {
296 115658 : st->extl = SWB_TBE;
297 115658 : st->extl_brate = SWB_TBE_1k6;
298 :
299 115658 : if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
300 : {
301 25234 : st->extl_brate = SWB_TBE_2k8;
302 : }
303 90424 : else if ( st->tdm_LRTD_flag == 1 && st->element_mode == IVAS_CPE_TD )
304 : {
305 5674 : if ( st->element_brate < IVAS_24k4 )
306 : {
307 898 : st->extl_brate = SWB_TBE_1k10;
308 : }
309 : else
310 : {
311 4776 : st->extl_brate = SWB_TBE_1k75;
312 : }
313 : }
314 84750 : else if ( st->total_brate < MIN_BRATE_SWB_TBE_1k60 )
315 : {
316 32489 : st->extl_brate = SWB_TBE_0k95;
317 : }
318 :
319 115658 : if ( st->bwidth == FB )
320 : {
321 43877 : st->extl = FB_TBE;
322 43877 : st->extl_brate = FB_TBE_1k8;
323 :
324 43877 : if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
325 : {
326 20219 : st->extl_brate = FB_TBE_3k0;
327 : }
328 : }
329 : }
330 : }
331 : else
332 : {
333 0 : st->extl = WB_BWE;
334 0 : st->extl_brate = 0;
335 : }
336 :
337 : /* set IC-BWE bitrate */
338 127574 : if ( st->element_mode == IVAS_CPE_TD && ( st->idchan == 0 ) && !st->tdm_LRTD_flag )
339 : {
340 34 : icbwe_brate = STEREO_BITS_ICBWE * FRAMES_PER_SEC;
341 34 : if ( st->flag_ACELP16k == 0 )
342 : {
343 12 : icbwe_brate = ( STEREO_BITS_ICBWE - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
344 : }
345 : }
346 127540 : else if ( st->element_mode == IVAS_CPE_DFT )
347 : {
348 26719 : icbwe_brate = STEREO_BITS_ICBWE_DFT * FRAMES_PER_SEC;
349 26719 : if ( st->flag_ACELP16k == 0 )
350 : {
351 13610 : icbwe_brate = ( STEREO_BITS_ICBWE_DFT - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
352 : }
353 : }
354 :
355 127574 : if ( st->element_mode >= IVAS_CPE_DFT && st->core == ACELP_CORE && ( st->extl == SWB_TBE || st->extl == FB_TBE ) && !( st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) )
356 : {
357 24111 : icbwe_brate += STEREO_ICBWE_MSFLAG_BITS * FRAMES_PER_SEC;
358 : }
359 : }
360 : }
361 :
362 : /* set core bitrate */
363 1105515 : st->core_brate = st->total_brate - st->extl_brate - icbwe_brate;
364 :
365 1105515 : if ( st->ini_frame == 0 )
366 : {
367 : /* avoid core switching in the very first frame */
368 8724 : st->last_core = st->core;
369 8724 : st->last_core_brate = st->core_brate;
370 8724 : st->last_extl = st->extl;
371 : }
372 :
373 : /*-----------------------------------------------------------------*
374 : * set inactive coder_type flag in ACELP core
375 : *-----------------------------------------------------------------*/
376 :
377 1105515 : st->inactive_coder_type_flag = 0; /* AVQ by default */
378 1105515 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
379 : {
380 305066 : st->inactive_coder_type_flag = 1; /* GSC */
381 : }
382 :
383 1105515 : return;
384 : }
385 :
386 :
387 : /*---------------------------------------------------------------------*
388 : * ivas_signaling_enc()
389 : *
390 : * write signaling information of SCE/CPE mode into the bitstream
391 : *---------------------------------------------------------------------*/
392 :
393 791076 : void ivas_signaling_enc(
394 : Encoder_State *st, /* i/o: encoder state structure */
395 : const int16_t MCT_flag, /* i : hMCT handle allocated (1) or not (0) */
396 : const int32_t element_brate, /* i : element bitrate */
397 : const int16_t tdm_SM_or_LRTD_Pri, /* i : channel combination scheme flag in TD stereo OR LRTD primary channel */
398 : const int16_t tdm_Pitch_reuse_flag /* i : primary channel pitch reuse flag in TD stereo */
399 : )
400 : {
401 : int16_t ind;
402 791076 : BSTR_ENC_HANDLE hBstr = st->hBstr;
403 :
404 791076 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
405 : {
406 : /* minimal signaling for the secondary channel, most of the parameters are deduced from the primary channel */
407 3791 : signaling_enc_secondary( st, tdm_SM_or_LRTD_Pri, tdm_Pitch_reuse_flag );
408 : }
409 787285 : else if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
410 : {
411 : /* ISM Low-rate mode -> do nothing -> always WB, ACELP core, IC coder_type */
412 : }
413 779751 : else if ( ( st->element_mode == IVAS_CPE_MDCT && st->idchan == 1 ) || st->core_brate <= SID_2k40 )
414 : {
415 : /* do nothing */
416 : }
417 : else
418 : {
419 571872 : if ( st->idchan == 0 )
420 : {
421 : /*--------------------------------------------------------------------------
422 : * Write element mode info
423 : *--------------------------------------------------------------------------*/
424 :
425 571872 : if ( ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD ) && !MCT_flag ) /* note: in MCT, the MDCT stereo is used exclusively */
426 : {
427 53119 : ind = st->element_mode - IVAS_CPE_DFT;
428 53119 : push_indice( hBstr, IND_SMODE, ind, NBITS_ELEMENT_MODE );
429 : }
430 :
431 : /*--------------------------------------------------------------------------
432 : * Write band-width info
433 : *--------------------------------------------------------------------------*/
434 :
435 571872 : if ( st->element_mode == IVAS_SCE && element_brate < MIN_BRATE_SWB_SCE )
436 : {
437 : /* only WB is supported */
438 : }
439 565922 : else if ( ( element_brate < MIN_BRATE_FB_STEREO && !st->is_ism_format ) ||
440 27651 : ( element_brate < MIN_BRATE_FB_ISM && st->is_ism_format ) )
441 : {
442 : /* WB and SWB are supported */
443 97772 : ind = st->bwidth - WB;
444 97772 : push_indice( hBstr, IND_BWIDTH, ind, 1 );
445 : }
446 : else
447 : {
448 : /* WB, SWB and FB are supported */
449 468150 : push_indice( hBstr, IND_BWIDTH, st->bwidth, NBITS_BWIDTH );
450 : }
451 :
452 571872 : if ( st->element_mode != IVAS_CPE_MDCT )
453 : {
454 : /*--------------------------------------------------------------------------
455 : * Write ACELP/MDCT core selection bit
456 : *--------------------------------------------------------------------------*/
457 :
458 388292 : if ( st->element_mode == IVAS_CPE_TD || st->total_brate >= STEREO_TCX_MIN_RATE )
459 : {
460 380109 : if ( st->core == ACELP_CORE )
461 : {
462 136880 : push_indice( hBstr, IND_CORE, 0, 1 );
463 : }
464 : else
465 : {
466 243229 : push_indice( hBstr, IND_CORE, 1, 1 );
467 : }
468 : }
469 :
470 : /*--------------------------------------------------------------------------
471 : * Write core dependent signaling
472 : *--------------------------------------------------------------------------*/
473 :
474 388292 : if ( st->core == ACELP_CORE )
475 : {
476 : /* write coder type */
477 145063 : push_indice( hBstr, IND_ACELP_SIGNALLING, st->coder_type, 3 );
478 :
479 145063 : if ( element_brate >= FRMT_SHP_MIN_BRATE_IVAS )
480 : {
481 : /* write sharpening flag */
482 75972 : push_indice( hBstr, IND_SHARP_FLAG, st->sharpFlag, 1 );
483 : }
484 :
485 : /* write extension layer flag to distinguish between TBE (0) and BWE (1) */
486 145063 : if ( st->extl_brate > 0 )
487 : {
488 135972 : if ( st->extl == WB_TBE || st->extl == SWB_TBE || st->extl == FB_TBE )
489 : {
490 120017 : push_indice( hBstr, IND_BWE_FLAG, 0, 1 );
491 : }
492 15955 : else if ( st->extl == WB_BWE || st->extl == SWB_BWE || st->extl == FB_BWE )
493 : {
494 15955 : push_indice( hBstr, IND_BWE_FLAG, 1, 1 );
495 : }
496 : }
497 : }
498 : else /* MDCT core */
499 : {
500 : /* write TCX/HQ selection flag */
501 243229 : if ( st->core == HQ_CORE )
502 : {
503 8017 : push_indice( hBstr, IND_MDCT_CORE, 0, 1 );
504 : }
505 : else /* TCX20/TCX10 core */
506 : {
507 235212 : push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
508 : }
509 :
510 243229 : if ( st->core == HQ_CORE )
511 : {
512 8017 : if ( st->last_core == ACELP_CORE )
513 : {
514 198 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
515 : }
516 : else
517 : {
518 7819 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
519 : }
520 : }
521 : }
522 : } /* element_mode != IVAS_CPE_MDCT */
523 : }
524 : }
525 :
526 791076 : return;
527 : }
|