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 7130 : 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 7130 : st->core = -1;
122 7130 : st->extl = -1;
123 7130 : st->extl_brate = 0;
124 7130 : *hq_core_type = -1;
125 7130 : st->igf = 0;
126 :
127 : /* SID and FRAME_NO_DATA frames */
128 7130 : if ( st->Opt_DTX_ON && ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA ) )
129 : {
130 0 : st->core = ACELP_CORE;
131 :
132 0 : if ( st->input_Fs >= 32000 && st->bwidth >= SWB )
133 : {
134 0 : st->extl = SWB_CNG;
135 : }
136 :
137 0 : st->rf_mode = 0;
138 :
139 0 : return;
140 : }
141 :
142 7130 : st->core_brate = 0;
143 :
144 : /* SC-VBR */
145 7130 : if ( st->Opt_SC_VBR )
146 : {
147 : /* SC-VBR */
148 200 : st->core = ACELP_CORE;
149 200 : st->core_brate = ACELP_7k20;
150 200 : st->total_brate = ACELP_7k20;
151 :
152 200 : if ( st->hSC_VBR->ppp_mode == 1 )
153 : {
154 : /* PPP mode */
155 95 : st->core_brate = PPP_NELP_2k80;
156 : }
157 105 : else if ( ( ( st->coder_type == UNVOICED || st->coder_type == TRANSITION ) && !st->sp_aud_decision1 ) || st->bwidth != NB )
158 : {
159 79 : 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 20 : st->hSC_VBR->nelp_mode = 1;
163 20 : st->core_brate = PPP_NELP_2k80;
164 : }
165 59 : 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 19 : st->core_brate = ACELP_8k00;
169 19 : st->total_brate = ACELP_8k00;
170 : }
171 : }
172 :
173 : /* set inactive coder_type flag in ACELP core to GSC */
174 200 : st->inactive_coder_type_flag = 1;
175 :
176 200 : return;
177 : }
178 :
179 : /*---------------------------------------------------------------------*
180 : * NB
181 : *---------------------------------------------------------------------*/
182 :
183 6930 : else if ( st->bwidth == NB )
184 : {
185 380 : 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 380 : if ( st->total_brate >= HQCORE_NB_MIN_RATE && st->sp_aud_decision1 == 1 )
194 : {
195 8 : st->core = HQ_CORE;
196 : }
197 : #endif
198 : }
199 :
200 : /*---------------------------------------------------------------------*
201 : * WB
202 : *---------------------------------------------------------------------*/
203 :
204 6550 : else if ( st->bwidth == WB )
205 : {
206 1830 : 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 1830 : if ( ( st->total_brate >= HQCORE_WB_MIN_RATE && st->sp_aud_decision1 == 1 ) || st->total_brate >= HQ_96k )
212 : #endif
213 : {
214 0 : st->core = HQ_CORE;
215 : }
216 : else
217 : {
218 1830 : if ( st->bwidth == WB && st->total_brate < ACELP_9k60 )
219 : {
220 660 : st->extl = WB_BWE;
221 : }
222 1170 : 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 390 : if ( st->sp_aud_decision1 == 1 || st->coder_type == INACTIVE || ( st->sp_aud_decision1 == 0 && st->sp_aud_decision2 == 1 ) )
226 : {
227 0 : st->extl = WB_BWE;
228 0 : st->extl_brate = WB_BWE_0k35;
229 : }
230 : else
231 : {
232 390 : st->extl = WB_TBE;
233 390 : st->extl_brate = WB_TBE_1k05;
234 : }
235 : }
236 : }
237 : }
238 :
239 : /*---------------------------------------------------------------------*
240 : * SWB and FB
241 : *---------------------------------------------------------------------*/
242 :
243 4720 : 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 4720 : if ( ( st->total_brate >= HQCORE_SWB_MIN_RATE && st->sp_aud_decision1 == 1 ) || st->total_brate >= HQ_96k )
249 : #endif
250 : {
251 968 : st->core = HQ_CORE;
252 : }
253 : else
254 : {
255 3752 : st->core = ACELP_CORE;
256 :
257 3752 : 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 2443 : 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 6 : st->extl = SWB_BWE;
264 6 : st->extl_brate = SWB_BWE_1k6;
265 :
266 6 : 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 2437 : st->extl = SWB_TBE;
275 2437 : st->extl_brate = SWB_TBE_1k6;
276 :
277 2437 : if ( st->total_brate >= ACELP_24k40 )
278 : {
279 700 : st->extl_brate = SWB_TBE_2k8;
280 : }
281 :
282 2437 : if ( st->bwidth == FB && st->total_brate >= ACELP_24k40 )
283 : {
284 250 : st->extl = FB_TBE;
285 250 : st->extl_brate = FB_TBE_3k0;
286 : }
287 : }
288 : }
289 1309 : else if ( st->total_brate >= ACELP_48k )
290 : {
291 1309 : st->extl = SWB_BWE_HIGHRATE;
292 1309 : st->extl_brate = SWB_BWE_16k;
293 :
294 1309 : if ( st->bwidth == FB )
295 : {
296 270 : st->extl = FB_BWE_HIGHRATE;
297 : }
298 : }
299 : }
300 : }
301 :
302 : /*-----------------------------------------------------------------*
303 : * Set HQ core type
304 : *-----------------------------------------------------------------*/
305 :
306 6930 : if ( st->core == HQ_CORE )
307 : {
308 976 : *hq_core_type = NORMAL_HQ_CORE;
309 :
310 976 : 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 637 : *hq_core_type = LOW_RATE_HQ_CORE;
314 : }
315 339 : else if ( st->bwidth == NB )
316 : {
317 8 : *hq_core_type = LOW_RATE_HQ_CORE;
318 : }
319 : }
320 :
321 : /* set core bitrate */
322 6930 : st->core_brate = st->total_brate - st->extl_brate;
323 :
324 6930 : if ( st->ini_frame == 0 )
325 : {
326 : /* avoid switching in the very first frame */
327 79 : st->last_core = st->core;
328 79 : st->last_core_brate = st->core_brate;
329 79 : st->last_extl = st->extl;
330 : }
331 :
332 : /*-----------------------------------------------------------------*
333 : * set inactive coder_type flag in ACELP core
334 : *-----------------------------------------------------------------*/
335 :
336 6930 : st->inactive_coder_type_flag = 0; /* AVQ by default */
337 6930 : if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
338 : {
339 3810 : st->inactive_coder_type_flag = 1; /* GSC */
340 : }
341 :
342 6930 : return;
343 : }
344 :
345 :
346 : /*---------------------------------------------------------------------*
347 : * signaling_mode1_tcx20_enc()
348 : *
349 : * write MODE1 TCX20 signaling information into the bitstream
350 : *---------------------------------------------------------------------*/
351 :
352 1166 : 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 1166 : BSTR_ENC_HANDLE hBstr = st->hBstr;
360 :
361 1166 : assert( st->core == TCX_20_CORE );
362 :
363 1166 : num_bits = 0;
364 :
365 : /* Use ACELP signaling for LR MDCT */
366 1166 : if ( st->total_brate <= ACELP_16k40 )
367 : {
368 : /* find the section in the ACELP signaling table corresponding to bitrate */
369 1166 : idx = 0;
370 40810 : while ( acelp_sig_tbl[idx] != st->total_brate )
371 : {
372 39644 : idx++;
373 : }
374 :
375 : /* retrieve the number of bits for signaling */
376 1166 : nBits = (int16_t) acelp_sig_tbl[++idx];
377 :
378 : /* retrieve the signaling index */
379 1166 : start_idx = ++idx;
380 37312 : while ( acelp_sig_tbl[idx] != SIG2IND( LR_MDCT, st->bwidth, 0, 0 ) )
381 : {
382 36146 : idx++;
383 : }
384 :
385 1166 : num_bits += nBits;
386 1166 : if ( push )
387 : {
388 583 : push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
389 : }
390 :
391 : /* HQ/TCX core switching flag */
392 1166 : ++num_bits;
393 1166 : if ( push )
394 : {
395 583 : push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
396 : }
397 : }
398 : else
399 : {
400 0 : if ( st->core_brate <= ACELP_64k )
401 : {
402 : /* write ACELP/HQ core indication flag */
403 0 : ++num_bits;
404 0 : if ( push )
405 : {
406 0 : push_indice( hBstr, IND_CORE, 1, 1 );
407 : }
408 : }
409 :
410 : /* HQ/TCX core switching flag */
411 0 : ++num_bits;
412 0 : if ( push )
413 : {
414 0 : push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
415 : }
416 :
417 0 : num_bits += 2;
418 0 : if ( push )
419 : {
420 : /* write band-width (needed for different I/O sampling rate support) */
421 0 : if ( st->bwidth == NB )
422 : {
423 0 : push_indice( hBstr, IND_HQ_BWIDTH, 0, 2 );
424 : }
425 0 : else if ( st->bwidth == WB )
426 : {
427 0 : push_indice( hBstr, IND_HQ_BWIDTH, 1, 2 );
428 : }
429 0 : else if ( st->bwidth == SWB )
430 : {
431 0 : push_indice( hBstr, IND_HQ_BWIDTH, 2, 2 );
432 : }
433 : else /* st->bwidth == FB */
434 : {
435 0 : push_indice( hBstr, IND_HQ_BWIDTH, 3, 2 );
436 : }
437 : }
438 : }
439 :
440 1166 : return num_bits;
441 : }
442 :
443 :
444 : /*---------------------------------------------------------------------*
445 : * signaling_enc()
446 : *
447 : * write signaling information into the bitstream
448 : *---------------------------------------------------------------------*/
449 :
450 6934 : 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 6934 : BSTR_ENC_HANDLE hBstr = st->hBstr;
458 :
459 6934 : if ( st->mdct_sw == MODE2 )
460 : {
461 :
462 315 : assert( !st->tcxonly );
463 315 : assert( st->core == HQ_CORE );
464 :
465 315 : push_next_indice( hBstr, 1, 1 ); /* TCX */
466 315 : push_next_indice( hBstr, 1, 1 ); /* HQ_CORE */
467 :
468 : /* write ACELP->HQ core switching flag */
469 315 : if ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE )
470 : {
471 29 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
472 :
473 : /* write ACELP L_frame info */
474 29 : if ( st->last_L_frame == L_FRAME )
475 : {
476 0 : push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
477 : }
478 : else
479 : {
480 29 : push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
481 : }
482 : }
483 : else
484 : {
485 286 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
486 : }
487 :
488 315 : return;
489 : }
490 :
491 6619 : if ( st->core == ACELP_CORE )
492 : {
493 : int16_t ppp_mode, nelp_mode;
494 :
495 6226 : if ( st->Opt_SC_VBR )
496 : {
497 272 : ppp_mode = st->hSC_VBR->ppp_mode;
498 272 : nelp_mode = st->hSC_VBR->nelp_mode;
499 : }
500 : else
501 : {
502 5954 : ppp_mode = 0;
503 5954 : nelp_mode = 0;
504 : }
505 :
506 6226 : if ( ppp_mode == 1 || nelp_mode == 1 )
507 : {
508 : /* 1 bit to distinguish between 2.8kbps PPP/NELP frame and SID frame */
509 115 : push_indice( hBstr, IND_CORE, 0, 1 );
510 :
511 : /* SC-VBR: 0 - PPP_NB, 1 - PPP_WB, 2 - NELP_NB, 3 - NELP_WB */
512 115 : if ( st->coder_type == VOICED && st->bwidth == NB && ppp_mode == 1 )
513 : {
514 22 : push_indice( hBstr, IND_PPP_NELP_MODE, 0, 2 );
515 : }
516 93 : else if ( st->coder_type == VOICED && st->bwidth != NB && ppp_mode == 1 )
517 : {
518 73 : push_indice( hBstr, IND_PPP_NELP_MODE, 1, 2 );
519 : }
520 20 : else if ( st->coder_type == UNVOICED && st->bwidth == NB && nelp_mode == 1 )
521 : {
522 8 : push_indice( hBstr, IND_PPP_NELP_MODE, 2, 2 );
523 : }
524 12 : else if ( st->coder_type == UNVOICED && st->bwidth != NB && nelp_mode == 1 )
525 : {
526 12 : push_indice( hBstr, IND_PPP_NELP_MODE, 3, 2 );
527 : }
528 : }
529 6111 : else if ( st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA )
530 : {
531 : /* write the ACELP/HQ core selection bit */
532 6111 : if ( st->total_brate >= ACELP_24k40 )
533 : {
534 2789 : push_indice( hBstr, IND_CORE, 0, 1 );
535 : }
536 :
537 : /* find the section in the ACELP signaling table corresponding to bitrate */
538 6111 : idx = 0;
539 33463 : while ( idx < MAX_ACELP_SIG )
540 : {
541 33463 : if ( st->total_brate <= brate_tbl[idx] )
542 : {
543 6111 : break;
544 : }
545 27352 : idx++;
546 : }
547 6111 : total_brate_temp = brate_tbl[idx];
548 :
549 6111 : idx = 0;
550 308949 : while ( acelp_sig_tbl[idx] != total_brate_temp )
551 : {
552 302838 : idx++;
553 : }
554 :
555 : /* retrieve the number of bits for signaling */
556 6111 : nBits = (int16_t) acelp_sig_tbl[++idx];
557 :
558 : /* retrieve the signaling index */
559 6111 : start_idx = ++idx;
560 6111 : 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 6111 : sig = SIG2IND( st->coder_type, st->bwidth, st->sharpFlag, st->rf_mode );
568 : }
569 :
570 40999 : while ( acelp_sig_tbl[idx] != sig )
571 : {
572 34888 : idx++;
573 : }
574 :
575 6111 : 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 6226 : if ( st->extl_brate > 0 )
580 : {
581 4142 : if ( st->extl == WB_TBE || st->extl == SWB_TBE || st->extl == FB_TBE )
582 : {
583 2827 : push_indice( hBstr, IND_BWE_FLAG, 0, 1 );
584 : }
585 1315 : else if ( st->extl == WB_BWE || st->extl == SWB_BWE || st->extl == FB_BWE )
586 : {
587 6 : push_indice( hBstr, IND_BWE_FLAG, 1, 1 );
588 : }
589 : }
590 : }
591 : else /* HQ core */
592 : {
593 : /* write ACELP->HQ core switching flag */
594 393 : if ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE )
595 : {
596 19 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
597 :
598 : /* write ACELP L_frame info */
599 19 : if ( st->last_L_frame == L_FRAME )
600 : {
601 7 : push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
602 : }
603 : else
604 : {
605 12 : push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
606 : }
607 : }
608 : else
609 : {
610 374 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
611 : }
612 :
613 : /* HQ/TCX core switching flag */
614 393 : push_indice( hBstr, IND_MDCT_CORE, 0, 1 );
615 :
616 : /* Use ACELP signaling for LR MDCT */
617 393 : if ( st->total_brate <= ACELP_16k40 )
618 : {
619 : /* find the section in the ACELP signaling table corresponding to bitrate */
620 62 : idx = 0;
621 2000 : while ( acelp_sig_tbl[idx] != st->total_brate )
622 : {
623 1938 : idx++;
624 : }
625 :
626 : /* retrieve the number of bits for signaling */
627 62 : nBits = (int16_t) acelp_sig_tbl[++idx];
628 :
629 : /* retrieve the signaling index */
630 62 : start_idx = ++idx;
631 1878 : while ( acelp_sig_tbl[idx] != SIG2IND( LR_MDCT, st->bwidth, 0, 0 ) )
632 : {
633 1816 : idx++;
634 : }
635 :
636 62 : push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
637 : }
638 : else
639 : {
640 331 : if ( st->core_brate <= ACELP_64k )
641 : {
642 : /* write ACELP/HQ core indication flag */
643 331 : push_indice( hBstr, IND_CORE, 1, 1 );
644 : }
645 :
646 : /* write band-width (needed for different I/O sampling rate support) */
647 331 : if ( st->bwidth == NB )
648 : {
649 0 : push_indice( hBstr, IND_HQ_BWIDTH, 0, 2 );
650 : }
651 331 : else if ( st->bwidth == WB )
652 : {
653 0 : push_indice( hBstr, IND_HQ_BWIDTH, 1, 2 );
654 : }
655 331 : else if ( st->bwidth == SWB )
656 : {
657 331 : push_indice( hBstr, IND_HQ_BWIDTH, 2, 2 );
658 : }
659 : else /* st->bwidth == FB */
660 : {
661 0 : push_indice( hBstr, IND_HQ_BWIDTH, 3, 2 );
662 : }
663 : }
664 : }
665 :
666 6619 : return;
667 : }
668 :
669 : /*---------------------------------------------------------------------*
670 : * signaling_enc_rf()
671 : *
672 : * write channel-aware signaling information into the bitstream
673 : *---------------------------------------------------------------------*/
674 :
675 16460 : void signaling_enc_rf(
676 : Encoder_State *st /* i/o: encoder state structure */
677 : )
678 : {
679 : int16_t i, sfr;
680 16460 : RF_ENC_HANDLE hRF = st->hRF;
681 :
682 : /* write partial copy into bitstream */
683 16460 : 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 16460 : 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 39720 : for ( i = st->rf_fec_offset; i >= 0; i-- )
694 : {
695 : /* RF frame type */
696 23260 : hRF->rf_indx_frametype[i + 1] = hRF->rf_indx_frametype[i];
697 :
698 : /* RF target bits buffer */
699 23260 : hRF->rf_targetbits_buff[i + 1] = hRF->rf_targetbits_buff[i];
700 :
701 : /* lsf indx */
702 23260 : hRF->rf_indx_lsf[i + 1][0] = hRF->rf_indx_lsf[i][0];
703 23260 : hRF->rf_indx_lsf[i + 1][1] = hRF->rf_indx_lsf[i][1];
704 23260 : hRF->rf_indx_lsf[i + 1][2] = hRF->rf_indx_lsf[i][2];
705 :
706 : /* ES pred energy */
707 23260 : hRF->rf_indx_EsPred[i + 1] = hRF->rf_indx_EsPred[i];
708 :
709 : /* LTF mode, sfr params: pitch, fcb and gain */
710 121640 : for ( sfr = 0; sfr < st->nb_subfr; sfr++ )
711 : {
712 98380 : hRF->rf_indx_ltfMode[i + 1][sfr] = hRF->rf_indx_ltfMode[i][sfr];
713 98380 : hRF->rf_indx_pitch[i + 1][sfr] = hRF->rf_indx_pitch[i][sfr];
714 98380 : hRF->rf_indx_fcb[i + 1][sfr] = hRF->rf_indx_fcb[i][sfr];
715 98380 : hRF->rf_indx_gain[i + 1][sfr] = hRF->rf_indx_gain[i][sfr];
716 : }
717 :
718 : /* shift the nelp indices */
719 23260 : hRF->rf_indx_nelp_iG1[i + 1] = hRF->rf_indx_nelp_iG1[i];
720 23260 : hRF->rf_indx_nelp_iG2[i + 1][0] = hRF->rf_indx_nelp_iG2[i][0];
721 23260 : hRF->rf_indx_nelp_iG2[i + 1][1] = hRF->rf_indx_nelp_iG2[i][1];
722 23260 : hRF->rf_indx_nelp_fid[i + 1] = hRF->rf_indx_nelp_fid[i];
723 :
724 : /* tbe gain Fr shift */
725 23260 : hRF->rf_indx_tbeGainFr[i + 1] = hRF->rf_indx_tbeGainFr[i];
726 23260 : hRF->rf_clas[i + 1] = hRF->rf_clas[i];
727 23260 : hRF->rf_gain_tcx[i + 1] = hRF->rf_gain_tcx[i];
728 23260 : hRF->rf_tcxltp_param[i + 1] = hRF->rf_tcxltp_param[i];
729 : }
730 : }
731 :
732 16460 : return;
733 : }
|