Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 60213 : 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 60213 : st->core = -1;
122 60213 : st->extl = -1;
123 60213 : st->extl_brate = 0;
124 60213 : *hq_core_type = -1;
125 60213 : st->igf = 0;
126 :
127 : /* SID and FRAME_NO_DATA frames */
128 60213 : 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 58185 : st->core_brate = 0;
143 :
144 : /* SC-VBR */
145 58185 : 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 55795 : 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 51597 : else if ( st->bwidth == WB )
205 : {
206 11943 : 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 11943 : 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 10408 : if ( st->bwidth == WB && st->total_brate < ACELP_9k60 )
219 : {
220 4898 : st->extl = WB_BWE;
221 : }
222 5510 : 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 2095 : 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 1844 : st->extl = WB_TBE;
233 1844 : st->extl_brate = WB_TBE_1k05;
234 : }
235 : }
236 : }
237 : }
238 :
239 : /*---------------------------------------------------------------------*
240 : * SWB and FB
241 : *---------------------------------------------------------------------*/
242 :
243 39654 : 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 39654 : if ( ( st->total_brate >= HQCORE_SWB_MIN_RATE && st->sp_aud_decision1 == 1 ) || st->total_brate >= HQ_96k )
249 : #endif
250 : {
251 13075 : st->core = HQ_CORE;
252 : }
253 : else
254 : {
255 26579 : st->core = ACELP_CORE;
256 :
257 26579 : 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 16738 : 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 15358 : st->extl = SWB_TBE;
275 15358 : st->extl_brate = SWB_TBE_1k6;
276 :
277 15358 : if ( st->total_brate >= ACELP_24k40 )
278 : {
279 2861 : st->extl_brate = SWB_TBE_2k8;
280 : }
281 :
282 15358 : if ( st->bwidth == FB && st->total_brate >= ACELP_24k40 )
283 : {
284 922 : st->extl = FB_TBE;
285 922 : st->extl_brate = FB_TBE_3k0;
286 : }
287 : }
288 : }
289 9841 : else if ( st->total_brate >= ACELP_48k )
290 : {
291 9841 : st->extl = SWB_BWE_HIGHRATE;
292 9841 : st->extl_brate = SWB_BWE_16k;
293 :
294 9841 : if ( st->bwidth == FB )
295 : {
296 939 : st->extl = FB_BWE_HIGHRATE;
297 : }
298 : }
299 : }
300 : }
301 :
302 : /*-----------------------------------------------------------------*
303 : * Set HQ core type
304 : *-----------------------------------------------------------------*/
305 :
306 55795 : if ( st->core == HQ_CORE )
307 : {
308 15682 : *hq_core_type = NORMAL_HQ_CORE;
309 :
310 15682 : 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 7063 : *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 55795 : st->core_brate = st->total_brate - st->extl_brate;
323 :
324 55795 : if ( st->ini_frame == 0 )
325 : {
326 : /* avoid switching in the very first frame */
327 109 : st->last_core = st->core;
328 109 : st->last_core_brate = st->core_brate;
329 109 : st->last_extl = st->extl;
330 : }
331 :
332 : /*-----------------------------------------------------------------*
333 : * set inactive coder_type flag in ACELP core
334 : *-----------------------------------------------------------------*/
335 :
336 55795 : st->inactive_coder_type_flag = 0; /* AVQ by default */
337 55795 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
338 : {
339 32131 : st->inactive_coder_type_flag = 1; /* GSC */
340 : }
341 :
342 55795 : return;
343 : }
344 :
345 :
346 : /*---------------------------------------------------------------------*
347 : * signaling_mode1_tcx20_enc()
348 : *
349 : * write MODE1 TCX20 signaling information into the bitstream
350 : *---------------------------------------------------------------------*/
351 :
352 12578 : 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 12578 : BSTR_ENC_HANDLE hBstr = st->hBstr;
360 :
361 12578 : assert( st->core == TCX_20_CORE );
362 :
363 12578 : num_bits = 0;
364 :
365 : /* Use ACELP signaling for LR MDCT */
366 12578 : if ( st->total_brate <= ACELP_16k40 )
367 : {
368 : /* find the section in the ACELP signaling table corresponding to bitrate */
369 12326 : idx = 0;
370 431410 : while ( acelp_sig_tbl[idx] != st->total_brate )
371 : {
372 419084 : idx++;
373 : }
374 :
375 : /* retrieve the number of bits for signaling */
376 12326 : nBits = (int16_t) acelp_sig_tbl[++idx];
377 :
378 : /* retrieve the signaling index */
379 12326 : start_idx = ++idx;
380 391458 : while ( acelp_sig_tbl[idx] != SIG2IND( LR_MDCT, st->bwidth, 0, 0 ) )
381 : {
382 379132 : idx++;
383 : }
384 :
385 12326 : num_bits += nBits;
386 12326 : if ( push )
387 : {
388 6163 : push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
389 : }
390 :
391 : /* HQ/TCX core switching flag */
392 12326 : ++num_bits;
393 12326 : if ( push )
394 : {
395 6163 : 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 12578 : return num_bits;
441 : }
442 :
443 :
444 : /*---------------------------------------------------------------------*
445 : * signaling_enc()
446 : *
447 : * write signaling information into the bitstream
448 : *---------------------------------------------------------------------*/
449 :
450 55468 : 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 55468 : BSTR_ENC_HANDLE hBstr = st->hBstr;
458 :
459 55468 : if ( st->mdct_sw == MODE2 )
460 : {
461 :
462 1187 : assert( !st->tcxonly );
463 1187 : assert( st->core == HQ_CORE );
464 :
465 1187 : push_next_indice( hBstr, 1, 1 ); /* TCX */
466 1187 : push_next_indice( hBstr, 1, 1 ); /* HQ_CORE */
467 :
468 : /* write ACELP->HQ core switching flag */
469 1187 : if ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE )
470 : {
471 95 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
472 :
473 : /* write ACELP L_frame info */
474 95 : if ( st->last_L_frame == L_FRAME )
475 : {
476 0 : push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
477 : }
478 : else
479 : {
480 95 : push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
481 : }
482 : }
483 : else
484 : {
485 1092 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
486 : }
487 :
488 1187 : return;
489 : }
490 :
491 54281 : if ( st->core == ACELP_CORE )
492 : {
493 : int16_t ppp_mode, nelp_mode;
494 :
495 44888 : 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 42141 : ppp_mode = 0;
503 42141 : nelp_mode = 0;
504 : }
505 :
506 44888 : 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 44133 : else if ( st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA )
530 : {
531 : /* write the ACELP/HQ core selection bit */
532 42105 : if ( st->total_brate >= ACELP_24k40 )
533 : {
534 16117 : push_indice( hBstr, IND_CORE, 0, 1 );
535 : }
536 :
537 : /* find the section in the ACELP signaling table corresponding to bitrate */
538 42105 : idx = 0;
539 218923 : while ( idx < MAX_ACELP_SIG )
540 : {
541 218923 : if ( st->total_brate <= brate_tbl[idx] )
542 : {
543 42105 : break;
544 : }
545 176818 : idx++;
546 : }
547 42105 : total_brate_temp = brate_tbl[idx];
548 :
549 42105 : idx = 0;
550 1997190 : while ( acelp_sig_tbl[idx] != total_brate_temp )
551 : {
552 1955085 : idx++;
553 : }
554 :
555 : /* retrieve the number of bits for signaling */
556 42105 : nBits = (int16_t) acelp_sig_tbl[++idx];
557 :
558 : /* retrieve the signaling index */
559 42105 : start_idx = ++idx;
560 42105 : 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 42105 : sig = SIG2IND( st->coder_type, st->bwidth, st->sharpFlag, st->rf_mode );
568 : }
569 :
570 320758 : while ( acelp_sig_tbl[idx] != sig )
571 : {
572 278653 : idx++;
573 : }
574 :
575 42105 : 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 44888 : if ( st->extl_brate > 0 )
580 : {
581 28674 : if ( st->extl == WB_TBE || st->extl == SWB_TBE || st->extl == FB_TBE )
582 : {
583 17202 : push_indice( hBstr, IND_BWE_FLAG, 0, 1 );
584 : }
585 11472 : 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 9393 : if ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE )
595 : {
596 282 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
597 :
598 : /* write ACELP L_frame info */
599 282 : if ( st->last_L_frame == L_FRAME )
600 : {
601 78 : 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 9111 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
611 : }
612 :
613 : /* HQ/TCX core switching flag */
614 9393 : push_indice( hBstr, IND_MDCT_CORE, 0, 1 );
615 :
616 : /* Use ACELP signaling for LR MDCT */
617 9393 : if ( st->total_brate <= ACELP_16k40 )
618 : {
619 : /* find the section in the ACELP signaling table corresponding to bitrate */
620 1972 : idx = 0;
621 59364 : while ( acelp_sig_tbl[idx] != st->total_brate )
622 : {
623 57392 : idx++;
624 : }
625 :
626 : /* retrieve the number of bits for signaling */
627 1972 : nBits = (int16_t) acelp_sig_tbl[++idx];
628 :
629 : /* retrieve the signaling index */
630 1972 : start_idx = ++idx;
631 58045 : while ( acelp_sig_tbl[idx] != SIG2IND( LR_MDCT, st->bwidth, 0, 0 ) )
632 : {
633 56073 : idx++;
634 : }
635 :
636 1972 : 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 54281 : return;
667 : }
668 :
669 : /*---------------------------------------------------------------------*
670 : * signaling_enc_rf()
671 : *
672 : * write channel-aware signaling information into the bitstream
673 : *---------------------------------------------------------------------*/
674 :
675 105468 : void signaling_enc_rf(
676 : Encoder_State *st /* i/o: encoder state structure */
677 : )
678 : {
679 : int16_t i, sfr;
680 105468 : RF_ENC_HANDLE hRF = st->hRF;
681 :
682 : /* write partial copy into bitstream */
683 105468 : 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 105468 : 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 217736 : for ( i = st->rf_fec_offset; i >= 0; i-- )
694 : {
695 : /* RF frame type */
696 112268 : hRF->rf_indx_frametype[i + 1] = hRF->rf_indx_frametype[i];
697 :
698 : /* RF target bits buffer */
699 112268 : hRF->rf_targetbits_buff[i + 1] = hRF->rf_targetbits_buff[i];
700 :
701 : /* lsf indx */
702 112268 : hRF->rf_indx_lsf[i + 1][0] = hRF->rf_indx_lsf[i][0];
703 112268 : hRF->rf_indx_lsf[i + 1][1] = hRF->rf_indx_lsf[i][1];
704 112268 : hRF->rf_indx_lsf[i + 1][2] = hRF->rf_indx_lsf[i][2];
705 :
706 : /* ES pred energy */
707 112268 : hRF->rf_indx_EsPred[i + 1] = hRF->rf_indx_EsPred[i];
708 :
709 : /* LTF mode, sfr params: pitch, fcb and gain */
710 595690 : for ( sfr = 0; sfr < st->nb_subfr; sfr++ )
711 : {
712 483422 : hRF->rf_indx_ltfMode[i + 1][sfr] = hRF->rf_indx_ltfMode[i][sfr];
713 483422 : hRF->rf_indx_pitch[i + 1][sfr] = hRF->rf_indx_pitch[i][sfr];
714 483422 : hRF->rf_indx_fcb[i + 1][sfr] = hRF->rf_indx_fcb[i][sfr];
715 483422 : hRF->rf_indx_gain[i + 1][sfr] = hRF->rf_indx_gain[i][sfr];
716 : }
717 :
718 : /* shift the nelp indices */
719 112268 : hRF->rf_indx_nelp_iG1[i + 1] = hRF->rf_indx_nelp_iG1[i];
720 112268 : hRF->rf_indx_nelp_iG2[i + 1][0] = hRF->rf_indx_nelp_iG2[i][0];
721 112268 : hRF->rf_indx_nelp_iG2[i + 1][1] = hRF->rf_indx_nelp_iG2[i][1];
722 112268 : hRF->rf_indx_nelp_fid[i + 1] = hRF->rf_indx_nelp_fid[i];
723 :
724 : /* tbe gain Fr shift */
725 112268 : hRF->rf_indx_tbeGainFr[i + 1] = hRF->rf_indx_tbeGainFr[i];
726 112268 : hRF->rf_clas[i + 1] = hRF->rf_clas[i];
727 112268 : hRF->rf_gain_tcx[i + 1] = hRF->rf_gain_tcx[i];
728 112268 : hRF->rf_tcxltp_param[i + 1] = hRF->rf_tcxltp_param[i];
729 : }
730 : }
731 :
732 105468 : return;
733 : }
|