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 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include "prot.h"
44 : #include "stat_enc.h"
45 : #include "stat_dec.h"
46 : #include "rom_com.h"
47 : #include "wmc_auto.h"
48 :
49 :
50 : /*-----------------------------------------------------------------*
51 : * decision_matrix_enc()
52 : *
53 : * Select operating point (combination of technologies) based on input signal properties and command-line parameters:
54 : *
55 : * 7.20 8.00 9.60 13.20 16.40 24.40 32 48 64 96 128
56 : * Mode 1 1 2 1 2 2 1 2 1 2 2
57 : * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
58 : * NB
59 : * speech ACELP@12k8 ACELP@12k8 ACELP@12k8 ACELP@12k8
60 : * audio LR MDCT LR MDCT TCX LR MDCT
61 : * inactive GSC@12k8 GSC@12k8 TCX GSC@12k8
62 : * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
63 : * WB
64 : * speech ACELP@12k8 ACELP@12k8 ACELP@12k8 ACELP@12k8 ACELP@16k ACELP@16k ACELP@16k TCX ACELP@16k TCX TCX
65 : * +0b WB BWE +0b WB BWE +TD WB BWE +TD WB BWE
66 : * audio GSC@12k8 GSC@12k8 TCX LR MDCT TCX TCX HQ TCX HQ TCX TCX
67 : * +0b WB BWE +0b WB BWE +IGF
68 : * inactive GSC@12k8 GSC@12k8 TCX GSC@12k8 TCX TCX AVQ@16k TCX AVQ@16k TCX TCX
69 : * +0b WB BWE +0b WB BWE +IGF +FD WB BWE
70 : * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
71 : * SWB
72 : * speech ACELP@12k8 ACELP@16k ACELP@16k ACELP@16k TCX ACELP@16k TCX TCX
73 : * +TD SWB BWE +TD SWB BWE +TD SWB BWE +TD SWB BWE +IGF +HR SWB BWE
74 : * audio LR MDCT/GSC TCX TCX HQ TCX HQ TCX TCX
75 : * +FD SWB BWE +IGF +IGF +FD SWB BWE +IGF
76 : * inactive GSC@12k8 TCX TCX AVQ@16k TCX AVQ@16k TCX TCX
77 : * +FD SWB BWE +IGF +IGF +FD SWB BWE +IGF +HR SWB BWE
78 : * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
79 : * FB
80 : * speech ACELP@16k ACELP@16k ACELP@16k TCX ACELP@16k TCX TCX
81 : * +TD FB BWE +TD FB BWE +TD FB BWE +IGF +HR FB BWE
82 : * audio TCX TCX HQ TCX HQ TCX TCX
83 : * +IGF +IGF +FD FB BWE +IGF
84 : * inactive TCX TCX AVQ@16k TCX AVQ@16k TCX TCX
85 : * +IGF +IGF +FD FB BWE +IGF +HR FB BWE
86 : * -----------------------------------------------------------------------------------------------------------------------------------------------------------------
87 : *
88 : * Note: the GSC technology is part of the ACELP core as AUDIO coder_type (it is used also at 13.2 for SWB unvoiced noisy speech)
89 : * Note2: FB processing is optional and is activated via "-band FB" option on the encoder command line
90 : * Note3: NB (0-4kHz), WB (0-8kHz), SWB (0-16kHz), FB (0-20kHz)
91 : *
92 : * Signalling of modes (x marks a mode that must be signalled in the bitstream)
93 : *
94 : * 7.20 8.00 9.6 13.2 16.4 24.4 32 48 64
95 : * NB WB SWB FB NB WB SWB FB NB WB SWB FB NB WB SWB FB NB WB SWB FB NB WB SWB FB NB WB SWB FB NB WB SWB FB NB WB SWB FB
96 : * GC, 12k8 x x x x x x x x x x x x x
97 : * UC, 12k8 x x x x x x
98 : * VC, 12k8 x x x x x x x x x x x x x
99 : * TC, 12k8 x x x x x x x x x x x x x
100 : * GC, 16k x x x x x x x x x x x x
101 : * TC, 16k x x x x x x x x x x x x
102 : * AC(GSC) x x x x x x x x x x x x x
103 : * IC x x x x x x x x x x x x x x x x x x x x x x x x x
104 : *
105 : * GC, 12k8, FS x x x x x x x x x x x x x
106 : * GC, 16k, FS x x x x x x x x x x x
107 : * VC, 12k8, FS x x x x x x x
108 : * TC, 12k8, FS x
109 : * TC, 16k, FS x x x x x x x x x x x
110 : *
111 : * LR MDCT x x x x x x x x x x x
112 : *
113 : *-----------------------------------------------------------------*/
114 :
115 58713 : void decision_matrix_enc(
116 : Encoder_State *st, /* i : encoder state structure */
117 : int16_t *hq_core_type /* o : HQ core type */
118 : )
119 : {
120 : /* initialization */
121 58713 : st->core = -1;
122 58713 : st->extl = -1;
123 58713 : st->extl_brate = 0;
124 58713 : *hq_core_type = -1;
125 58713 : st->igf = 0;
126 :
127 : /* SID and FRAME_NO_DATA frames */
128 58713 : if ( st->Opt_DTX_ON && ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA ) )
129 : {
130 2028 : st->core = ACELP_CORE;
131 :
132 2028 : if ( st->input_Fs >= 32000 && st->bwidth >= SWB )
133 : {
134 0 : st->extl = SWB_CNG;
135 : }
136 :
137 2028 : st->rf_mode = 0;
138 :
139 2028 : return;
140 : }
141 :
142 56685 : st->core_brate = 0;
143 :
144 : /* SC-VBR */
145 56685 : if ( st->Opt_SC_VBR )
146 : {
147 : /* SC-VBR */
148 2390 : st->core = ACELP_CORE;
149 2390 : st->core_brate = ACELP_7k20;
150 2390 : st->total_brate = ACELP_7k20;
151 :
152 2390 : if ( st->hSC_VBR->ppp_mode == 1 )
153 : {
154 : /* PPP mode */
155 467 : st->core_brate = PPP_NELP_2k80;
156 : }
157 1923 : else if ( ( ( st->coder_type == UNVOICED || st->coder_type == TRANSITION ) && !st->sp_aud_decision1 ) || st->bwidth != NB )
158 : {
159 1262 : if ( st->coder_type == UNVOICED && st->vad_flag == 1 && ( ( st->last_bwidth >= SWB && st->last_Opt_SC_VBR ) || st->last_bwidth < SWB ) && ( st->last_core != HQ_CORE || st->bwidth != NB ) )
160 : {
161 : /* NELP mode */
162 288 : st->hSC_VBR->nelp_mode = 1;
163 288 : st->core_brate = PPP_NELP_2k80;
164 : }
165 974 : else if ( st->coder_type == TRANSITION || ( st->coder_type == UNVOICED && st->hSC_VBR->nelp_mode != 1 ) || ( ( st->coder_type == AUDIO || st->coder_type == INACTIVE ) && st->bwidth != NB ) )
166 : {
167 : /* silence portions */
168 495 : st->core_brate = ACELP_8k00;
169 495 : st->total_brate = ACELP_8k00;
170 : }
171 : }
172 :
173 : /* set inactive coder_type flag in ACELP core to GSC */
174 2390 : st->inactive_coder_type_flag = 1;
175 :
176 2390 : return;
177 : }
178 :
179 : /*---------------------------------------------------------------------*
180 : * NB
181 : *---------------------------------------------------------------------*/
182 :
183 54295 : else if ( st->bwidth == NB )
184 : {
185 4198 : st->core = ACELP_CORE;
186 :
187 : #ifdef DEBUGGING
188 : if ( st->total_brate >= HQCORE_NB_MIN_RATE && ( st->force == FORCE_MUSIC || ( st->force == -1 && st->sp_aud_decision1 == 1 ) ) )
189 : {
190 : st->core = HQ_CORE;
191 : }
192 : #else
193 4198 : if ( st->total_brate >= HQCORE_NB_MIN_RATE && st->sp_aud_decision1 == 1 )
194 : {
195 1072 : st->core = HQ_CORE;
196 : }
197 : #endif
198 : }
199 :
200 : /*---------------------------------------------------------------------*
201 : * WB
202 : *---------------------------------------------------------------------*/
203 :
204 50097 : else if ( st->bwidth == WB )
205 : {
206 11693 : st->core = ACELP_CORE;
207 :
208 : #ifdef DEBUGGING
209 : if ( ( st->total_brate >= HQCORE_WB_MIN_RATE && ( st->force == FORCE_MUSIC || ( st->force == -1 && st->sp_aud_decision1 == 1 ) ) ) || st->total_brate >= HQ_96k )
210 : #else
211 11693 : if ( ( st->total_brate >= HQCORE_WB_MIN_RATE && st->sp_aud_decision1 == 1 ) || st->total_brate >= HQ_96k )
212 : #endif
213 : {
214 1535 : st->core = HQ_CORE;
215 : }
216 : else
217 : {
218 10158 : if ( st->bwidth == WB && st->total_brate < ACELP_9k60 )
219 : {
220 4798 : st->extl = WB_BWE;
221 : }
222 5360 : else if ( st->bwidth == WB && st->total_brate >= ACELP_9k60 && st->total_brate <= ACELP_16k40 )
223 : {
224 : /* Note: WB BWE is used exceptionally at 13.2 kbps if GSC is selected instead of LR-MDCT */
225 2045 : if ( st->sp_aud_decision1 == 1 || st->coder_type == INACTIVE || ( st->sp_aud_decision1 == 0 && st->sp_aud_decision2 == 1 ) )
226 : {
227 251 : st->extl = WB_BWE;
228 251 : st->extl_brate = WB_BWE_0k35;
229 : }
230 : else
231 : {
232 1794 : st->extl = WB_TBE;
233 1794 : st->extl_brate = WB_TBE_1k05;
234 : }
235 : }
236 : }
237 : }
238 :
239 : /*---------------------------------------------------------------------*
240 : * SWB and FB
241 : *---------------------------------------------------------------------*/
242 :
243 38404 : else if ( st->bwidth == SWB || st->bwidth == FB )
244 : {
245 : #ifdef DEBUGGING
246 : if ( ( st->total_brate >= HQCORE_WB_MIN_RATE && ( st->force == FORCE_MUSIC || ( st->force == -1 && st->sp_aud_decision1 == 1 ) ) ) || st->total_brate >= HQ_96k )
247 : #else
248 38404 : if ( ( st->total_brate >= HQCORE_SWB_MIN_RATE && st->sp_aud_decision1 == 1 ) || st->total_brate >= HQ_96k )
249 : #endif
250 : {
251 12747 : st->core = HQ_CORE;
252 : }
253 : else
254 : {
255 25657 : st->core = ACELP_CORE;
256 :
257 25657 : if ( st->total_brate >= ACELP_13k20 && st->total_brate < ACELP_48k )
258 : {
259 : /* Note: SWB BWE is not used in case of GSC noisy speech */
260 : /* Note: SWB BWE is used exceptionally at 13.2 kbps if GSC is selected instead of LR-MDCT */
261 15916 : if ( ( st->sp_aud_decision1 == 1 || st->coder_type == INACTIVE || ( st->sp_aud_decision1 == 0 && st->sp_aud_decision2 == 1 ) ) && !st->GSC_noisy_speech )
262 : {
263 1380 : st->extl = SWB_BWE;
264 1380 : st->extl_brate = SWB_BWE_1k6;
265 :
266 1380 : if ( st->bwidth == FB && st->total_brate >= ACELP_24k40 )
267 : {
268 0 : st->extl = FB_BWE;
269 0 : st->extl_brate = FB_BWE_1k8;
270 : }
271 : }
272 : else
273 : {
274 14536 : st->extl = SWB_TBE;
275 14536 : st->extl_brate = SWB_TBE_1k6;
276 :
277 14536 : if ( st->total_brate >= ACELP_24k40 )
278 : {
279 2761 : st->extl_brate = SWB_TBE_2k8;
280 : }
281 :
282 14536 : if ( st->bwidth == FB && st->total_brate >= ACELP_24k40 )
283 : {
284 872 : st->extl = FB_TBE;
285 872 : st->extl_brate = FB_TBE_3k0;
286 : }
287 : }
288 : }
289 9741 : else if ( st->total_brate >= ACELP_48k )
290 : {
291 9741 : st->extl = SWB_BWE_HIGHRATE;
292 9741 : st->extl_brate = SWB_BWE_16k;
293 :
294 9741 : if ( st->bwidth == FB )
295 : {
296 889 : st->extl = FB_BWE_HIGHRATE;
297 : }
298 : }
299 : }
300 : }
301 :
302 : /*-----------------------------------------------------------------*
303 : * Set HQ core type
304 : *-----------------------------------------------------------------*/
305 :
306 54295 : if ( st->core == HQ_CORE )
307 : {
308 15354 : *hq_core_type = NORMAL_HQ_CORE;
309 :
310 15354 : if ( ( st->bwidth == SWB || st->bwidth == WB ) && st->total_brate <= LRMDCT_CROSSOVER_POINT )
311 : {
312 : /* note that FB (bitrate >= 24400 bps) is always coded with NORMAL_HQ_CORE */
313 6735 : *hq_core_type = LOW_RATE_HQ_CORE;
314 : }
315 8619 : else if ( st->bwidth == NB )
316 : {
317 1072 : *hq_core_type = LOW_RATE_HQ_CORE;
318 : }
319 : }
320 :
321 : /* set core bitrate */
322 54295 : st->core_brate = st->total_brate - st->extl_brate;
323 :
324 54295 : if ( st->ini_frame == 0 )
325 : {
326 : /* avoid switching in the very first frame */
327 98 : st->last_core = st->core;
328 98 : st->last_core_brate = st->core_brate;
329 98 : st->last_extl = st->extl;
330 : }
331 :
332 : /*-----------------------------------------------------------------*
333 : * set inactive coder_type flag in ACELP core
334 : *-----------------------------------------------------------------*/
335 :
336 54295 : st->inactive_coder_type_flag = 0; /* AVQ by default */
337 54295 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
338 : {
339 30931 : st->inactive_coder_type_flag = 1; /* GSC */
340 : }
341 :
342 54295 : return;
343 : }
344 :
345 :
346 : /*---------------------------------------------------------------------*
347 : * signaling_mode1_tcx20_enc()
348 : *
349 : * write MODE1 TCX20 signaling information into the bitstream
350 : *---------------------------------------------------------------------*/
351 :
352 11962 : int16_t signaling_mode1_tcx20_enc(
353 : Encoder_State *st, /* i/o: encoder state structure */
354 : const int16_t push /* i : flag to push indice */
355 : )
356 : {
357 : int16_t num_bits;
358 : int16_t nBits, idx, start_idx;
359 11962 : BSTR_ENC_HANDLE hBstr = st->hBstr;
360 :
361 11962 : assert( st->core == TCX_20_CORE );
362 :
363 11962 : num_bits = 0;
364 :
365 : /* Use ACELP signaling for LR MDCT */
366 11962 : if ( st->total_brate <= ACELP_16k40 )
367 : {
368 : /* find the section in the ACELP signaling table corresponding to bitrate */
369 11710 : idx = 0;
370 409850 : while ( acelp_sig_tbl[idx] != st->total_brate )
371 : {
372 398140 : idx++;
373 : }
374 :
375 : /* retrieve the number of bits for signaling */
376 11710 : nBits = (int16_t) acelp_sig_tbl[++idx];
377 :
378 : /* retrieve the signaling index */
379 11710 : start_idx = ++idx;
380 371746 : while ( acelp_sig_tbl[idx] != SIG2IND( LR_MDCT, st->bwidth, 0, 0 ) )
381 : {
382 360036 : idx++;
383 : }
384 :
385 11710 : num_bits += nBits;
386 11710 : if ( push )
387 : {
388 5855 : push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
389 : }
390 :
391 : /* HQ/TCX core switching flag */
392 11710 : ++num_bits;
393 11710 : if ( push )
394 : {
395 5855 : push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
396 : }
397 : }
398 : else
399 : {
400 252 : if ( st->core_brate <= ACELP_64k )
401 : {
402 : /* write ACELP/HQ core indication flag */
403 252 : ++num_bits;
404 252 : if ( push )
405 : {
406 126 : push_indice( hBstr, IND_CORE, 1, 1 );
407 : }
408 : }
409 :
410 : /* HQ/TCX core switching flag */
411 252 : ++num_bits;
412 252 : if ( push )
413 : {
414 126 : push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
415 : }
416 :
417 252 : num_bits += 2;
418 252 : if ( push )
419 : {
420 : /* write band-width (needed for different I/O sampling rate support) */
421 126 : if ( st->bwidth == NB )
422 : {
423 0 : push_indice( hBstr, IND_HQ_BWIDTH, 0, 2 );
424 : }
425 126 : else if ( st->bwidth == WB )
426 : {
427 0 : push_indice( hBstr, IND_HQ_BWIDTH, 1, 2 );
428 : }
429 126 : else if ( st->bwidth == SWB )
430 : {
431 103 : push_indice( hBstr, IND_HQ_BWIDTH, 2, 2 );
432 : }
433 : else /* st->bwidth == FB */
434 : {
435 23 : push_indice( hBstr, IND_HQ_BWIDTH, 3, 2 );
436 : }
437 : }
438 : }
439 :
440 11962 : return num_bits;
441 : }
442 :
443 :
444 : /*---------------------------------------------------------------------*
445 : * signaling_enc()
446 : *
447 : * write signaling information into the bitstream
448 : *---------------------------------------------------------------------*/
449 :
450 54174 : void signaling_enc(
451 : Encoder_State *st /* i : encoder state structure */
452 : )
453 : {
454 : int16_t nBits, idx, start_idx;
455 : int32_t total_brate_temp;
456 : int16_t sig;
457 54174 : BSTR_ENC_HANDLE hBstr = st->hBstr;
458 :
459 54174 : if ( st->mdct_sw == MODE2 )
460 : {
461 :
462 1085 : assert( !st->tcxonly );
463 1085 : assert( st->core == HQ_CORE );
464 :
465 1085 : push_next_indice( hBstr, 1, 1 ); /* TCX */
466 1085 : push_next_indice( hBstr, 1, 1 ); /* HQ_CORE */
467 :
468 : /* write ACELP->HQ core switching flag */
469 1085 : if ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE )
470 : {
471 84 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
472 :
473 : /* write ACELP L_frame info */
474 84 : if ( st->last_L_frame == L_FRAME )
475 : {
476 0 : push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
477 : }
478 : else
479 : {
480 84 : push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
481 : }
482 : }
483 : else
484 : {
485 1001 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
486 : }
487 :
488 1085 : return;
489 : }
490 :
491 53089 : if ( st->core == ACELP_CORE )
492 : {
493 : int16_t ppp_mode, nelp_mode;
494 :
495 43716 : if ( st->Opt_SC_VBR )
496 : {
497 2747 : ppp_mode = st->hSC_VBR->ppp_mode;
498 2747 : nelp_mode = st->hSC_VBR->nelp_mode;
499 : }
500 : else
501 : {
502 40969 : ppp_mode = 0;
503 40969 : nelp_mode = 0;
504 : }
505 :
506 43716 : if ( ppp_mode == 1 || nelp_mode == 1 )
507 : {
508 : /* 1 bit to distinguish between 2.8kbps PPP/NELP frame and SID frame */
509 755 : push_indice( hBstr, IND_CORE, 0, 1 );
510 :
511 : /* SC-VBR: 0 - PPP_NB, 1 - PPP_WB, 2 - NELP_NB, 3 - NELP_WB */
512 755 : if ( st->coder_type == VOICED && st->bwidth == NB && ppp_mode == 1 )
513 : {
514 198 : push_indice( hBstr, IND_PPP_NELP_MODE, 0, 2 );
515 : }
516 557 : else if ( st->coder_type == VOICED && st->bwidth != NB && ppp_mode == 1 )
517 : {
518 269 : push_indice( hBstr, IND_PPP_NELP_MODE, 1, 2 );
519 : }
520 288 : else if ( st->coder_type == UNVOICED && st->bwidth == NB && nelp_mode == 1 )
521 : {
522 160 : push_indice( hBstr, IND_PPP_NELP_MODE, 2, 2 );
523 : }
524 128 : else if ( st->coder_type == UNVOICED && st->bwidth != NB && nelp_mode == 1 )
525 : {
526 128 : push_indice( hBstr, IND_PPP_NELP_MODE, 3, 2 );
527 : }
528 : }
529 42961 : else if ( st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA )
530 : {
531 : /* write the ACELP/HQ core selection bit */
532 40933 : if ( st->total_brate >= ACELP_24k40 )
533 : {
534 15817 : push_indice( hBstr, IND_CORE, 0, 1 );
535 : }
536 :
537 : /* find the section in the ACELP signaling table corresponding to bitrate */
538 40933 : idx = 0;
539 213285 : while ( idx < MAX_ACELP_SIG )
540 : {
541 213285 : if ( st->total_brate <= brate_tbl[idx] )
542 : {
543 40933 : break;
544 : }
545 172352 : idx++;
546 : }
547 40933 : total_brate_temp = brate_tbl[idx];
548 :
549 40933 : idx = 0;
550 1945970 : while ( acelp_sig_tbl[idx] != total_brate_temp )
551 : {
552 1905037 : idx++;
553 : }
554 :
555 : /* retrieve the number of bits for signaling */
556 40933 : nBits = (int16_t) acelp_sig_tbl[++idx];
557 :
558 : /* retrieve the signaling index */
559 40933 : start_idx = ++idx;
560 40933 : if ( st->element_mode == IVAS_CPE_TD && st->bwidth == SWB && st->total_brate <= ACELP_9k60 )
561 : {
562 : /* patch to signal SWB as NB in Stereo */
563 0 : sig = SIG2IND( st->coder_type, NB, st->sharpFlag, st->rf_mode );
564 : }
565 : else
566 : {
567 40933 : sig = SIG2IND( st->coder_type, st->bwidth, st->sharpFlag, st->rf_mode );
568 : }
569 :
570 310020 : while ( acelp_sig_tbl[idx] != sig )
571 : {
572 269087 : idx++;
573 : }
574 :
575 40933 : push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
576 : }
577 :
578 : /* write extension layer flag to distinguish between TBE (0) and BWE (1) */
579 43716 : if ( st->extl_brate > 0 )
580 : {
581 27702 : if ( st->extl == WB_TBE || st->extl == SWB_TBE || st->extl == FB_TBE )
582 : {
583 16330 : push_indice( hBstr, IND_BWE_FLAG, 0, 1 );
584 : }
585 11372 : else if ( st->extl == WB_BWE || st->extl == SWB_BWE || st->extl == FB_BWE )
586 : {
587 1631 : push_indice( hBstr, IND_BWE_FLAG, 1, 1 );
588 : }
589 : }
590 : }
591 : else /* HQ core */
592 : {
593 : /* write ACELP->HQ core switching flag */
594 9373 : if ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE )
595 : {
596 280 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
597 :
598 : /* write ACELP L_frame info */
599 280 : if ( st->last_L_frame == L_FRAME )
600 : {
601 76 : push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
602 : }
603 : else
604 : {
605 204 : push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
606 : }
607 : }
608 : else
609 : {
610 9093 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
611 : }
612 :
613 : /* HQ/TCX core switching flag */
614 9373 : push_indice( hBstr, IND_MDCT_CORE, 0, 1 );
615 :
616 : /* Use ACELP signaling for LR MDCT */
617 9373 : if ( st->total_brate <= ACELP_16k40 )
618 : {
619 : /* find the section in the ACELP signaling table corresponding to bitrate */
620 1952 : idx = 0;
621 58664 : while ( acelp_sig_tbl[idx] != st->total_brate )
622 : {
623 56712 : idx++;
624 : }
625 :
626 : /* retrieve the number of bits for signaling */
627 1952 : nBits = (int16_t) acelp_sig_tbl[++idx];
628 :
629 : /* retrieve the signaling index */
630 1952 : start_idx = ++idx;
631 57405 : while ( acelp_sig_tbl[idx] != SIG2IND( LR_MDCT, st->bwidth, 0, 0 ) )
632 : {
633 55453 : idx++;
634 : }
635 :
636 1952 : push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
637 : }
638 : else
639 : {
640 7421 : if ( st->core_brate <= ACELP_64k )
641 : {
642 : /* write ACELP/HQ core indication flag */
643 7421 : push_indice( hBstr, IND_CORE, 1, 1 );
644 : }
645 :
646 : /* write band-width (needed for different I/O sampling rate support) */
647 7421 : if ( st->bwidth == NB )
648 : {
649 0 : push_indice( hBstr, IND_HQ_BWIDTH, 0, 2 );
650 : }
651 7421 : else if ( st->bwidth == WB )
652 : {
653 1408 : push_indice( hBstr, IND_HQ_BWIDTH, 1, 2 );
654 : }
655 6013 : else if ( st->bwidth == SWB )
656 : {
657 5377 : push_indice( hBstr, IND_HQ_BWIDTH, 2, 2 );
658 : }
659 : else /* st->bwidth == FB */
660 : {
661 636 : push_indice( hBstr, IND_HQ_BWIDTH, 3, 2 );
662 : }
663 : }
664 : }
665 :
666 53089 : return;
667 : }
668 :
669 : /*---------------------------------------------------------------------*
670 : * signaling_enc_rf()
671 : *
672 : * write channel-aware signaling information into the bitstream
673 : *---------------------------------------------------------------------*/
674 :
675 17645546 : void signaling_enc_rf(
676 : Encoder_State *st /* i/o: encoder state structure */
677 : )
678 : {
679 : int16_t i, sfr;
680 17645546 : RF_ENC_HANDLE hRF = st->hRF;
681 :
682 : /* write partial copy into bitstream */
683 17645546 : if ( st->rf_mode == 1 )
684 : {
685 1600 : enc_prm_rf( st, hRF->rf_indx_frametype[st->rf_fec_offset], st->rf_fec_offset );
686 1600 : hRF->rf_indx_tbeGainFr[0] = hRF->RF_bwe_gainFr_ind;
687 : }
688 :
689 17645546 : if ( hRF != NULL )
690 : {
691 : /* Shift the RF indices such that the partial copy associated with
692 : (n-fec_offset)th frame is included in the bitstream in nth frame. */
693 210936 : for ( i = st->rf_fec_offset; i >= 0; i-- )
694 : {
695 : /* RF frame type */
696 108868 : hRF->rf_indx_frametype[i + 1] = hRF->rf_indx_frametype[i];
697 :
698 : /* RF target bits buffer */
699 108868 : hRF->rf_targetbits_buff[i + 1] = hRF->rf_targetbits_buff[i];
700 :
701 : /* lsf indx */
702 108868 : hRF->rf_indx_lsf[i + 1][0] = hRF->rf_indx_lsf[i][0];
703 108868 : hRF->rf_indx_lsf[i + 1][1] = hRF->rf_indx_lsf[i][1];
704 108868 : hRF->rf_indx_lsf[i + 1][2] = hRF->rf_indx_lsf[i][2];
705 :
706 : /* ES pred energy */
707 108868 : hRF->rf_indx_EsPred[i + 1] = hRF->rf_indx_EsPred[i];
708 :
709 : /* LTF mode, sfr params: pitch, fcb and gain */
710 577140 : for ( sfr = 0; sfr < st->nb_subfr; sfr++ )
711 : {
712 468272 : hRF->rf_indx_ltfMode[i + 1][sfr] = hRF->rf_indx_ltfMode[i][sfr];
713 468272 : hRF->rf_indx_pitch[i + 1][sfr] = hRF->rf_indx_pitch[i][sfr];
714 468272 : hRF->rf_indx_fcb[i + 1][sfr] = hRF->rf_indx_fcb[i][sfr];
715 468272 : hRF->rf_indx_gain[i + 1][sfr] = hRF->rf_indx_gain[i][sfr];
716 : }
717 :
718 : /* shift the nelp indices */
719 108868 : hRF->rf_indx_nelp_iG1[i + 1] = hRF->rf_indx_nelp_iG1[i];
720 108868 : hRF->rf_indx_nelp_iG2[i + 1][0] = hRF->rf_indx_nelp_iG2[i][0];
721 108868 : hRF->rf_indx_nelp_iG2[i + 1][1] = hRF->rf_indx_nelp_iG2[i][1];
722 108868 : hRF->rf_indx_nelp_fid[i + 1] = hRF->rf_indx_nelp_fid[i];
723 :
724 : /* tbe gain Fr shift */
725 108868 : hRF->rf_indx_tbeGainFr[i + 1] = hRF->rf_indx_tbeGainFr[i];
726 108868 : hRF->rf_clas[i + 1] = hRF->rf_clas[i];
727 108868 : hRF->rf_gain_tcx[i + 1] = hRF->rf_gain_tcx[i];
728 108868 : hRF->rf_tcxltp_param[i + 1] = hRF->rf_tcxltp_param[i];
729 : }
730 : }
731 :
732 17645546 : return;
733 : }
|