Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <assert.h>
6 : #include <stdint.h>
7 : #include "options.h"
8 : #include "cnst.h" /* Common constants */
9 : #include "stat_enc.h"
10 : #include "rom_com.h" /* Common constants */
11 : #include "prot_fx.h" /* Function prototypes */
12 : #include "prot_fx_enc.h" /* Function prototypes */
13 : #include "basop_util.h" /* Function prototypes */
14 :
15 :
16 : /*-----------------------------------------------------------------*
17 : * decision_matrix_enc()
18 : *
19 : * Select operating point (combination of technologies) based on input signal properties and command-line parameters:
20 : *
21 : * 7.20 8.00 9.60 13.20 16.40 24.40 32 48 64 96 128
22 : * Mode 1 1 2 1 2 2 2 2 1 2 2
23 : * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
24 : * NB
25 : * speech ACELP@12k8 ACELP@12k8 ACELP@12k8 ACELP@12k8
26 : * audio LR MDCT LR MDCT TCX LR MDCT
27 : * inactive GSC@12k8 GSC@12k8 TCX GSC@12k8
28 : * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
29 : * WB
30 : * speech ACELP@12k8 ACELP@12k8 ACELP@12k8 ACELP@12k8 ACELP@16k ACELP@16k ACELP@16k TCX ACELP@16k TCX TCX
31 : * +0b WB BWE +0b WB BWE +TD WB BWE +TD WB BWE
32 : * audio GSC@12k8 GSC@12k8 TCX LR MDCT TCX TCX HQ TCX HQ TCX TCX
33 : * +0b WB BWE +0b WB BWE +IGF
34 : * inactive GSC@12k8 GSC@12k8 TCX GSC@12k8 TCX TCX AVQ@16k TCX AVQ@16k TCX TCX
35 : * +0b WB BWE +0b WB BWE +IGF +FD WB BWE
36 : * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
37 : * SWB
38 : * speech ACELP@12k8 ACELP@16k ACELP@16k ACELP@16k TCX ACELP@16k TCX TCX
39 : * +TD SWB BWE +TD SWB BWE +TD SWB BWE +TD SWB BWE +IGF +HR SWB BWE
40 : * audio LR MDCT/GSC TCX TCX HQ TCX HQ TCX TCX
41 : * +FD SWB BWE +IGF +IGF +FD SWB BWE +IGF
42 : * inactive GSC@12k8 TCX TCX AVQ@16k TCX AVQ@16k TCX TCX
43 : * +FD SWB BWE +IGF +IGF +FD SWB BWE +IGF +HR SWB BWE
44 : * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
45 : * FB
46 : * speech ACELP@16k ACELP@16k ACELP@16k TCX ACELP@16k TCX TCX
47 : * +TD FB BWE +TD FB BWE +TD FB BWE +IGF +HR FB BWE
48 : * audio TCX TCX HQ TCX HQ TCX TCX
49 : * +IGF +IGF +FD FB BWE +IGF
50 : * inactive TCX TCX AVQ@16k TCX AVQ@16k TCX TCX
51 : * +IGF +IGF +FD FB BWE +IGF +HR FB BWE
52 : * ---------------------------------------------------------------------------------------------------------------------------------------------------------------
53 : *
54 : * Note: the GSC technology is part of the ACELP core as AUDIO st_fx->coder_type (it is used also at 13.2 and 16.4 kbps for SWB unvoiced noisy speech)
55 : * Note2: FB processing is optional and is activated via "-band FB" option on the encoder command line
56 : * Note3: NB (0-4kHz), WB (0-8kHz), SWB (0-16kHz), FB (0-20kHz)
57 : *
58 : * Signalling of modes (x marks a mode that must be signalled in the bitstream)
59 : *
60 : * 7.20 8.00 9.6 13.2 16.4 24.4 32 48 64
61 : * 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
62 : * GC, 12k8 x x x x x x x x x x x x x
63 : * UC, 12k8 x x x x x x
64 : * VC, 12k8 x x x x x x x x x x x x x
65 : * TC, 12k8 x x x x x x x x x x x x x
66 : * GC, 16k x x x x x x x x x x x x
67 : * TC, 16k x x x x x x x x x x x x
68 : * AC(GSC) x x x x x x x x x x x x x
69 : * 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
70 : *
71 : * GC, 12k8, FS x x x x x x x x x x x x x
72 : * GC, 16k, FS x x x x x x x x x x x
73 : * VC, 12k8, FS x x x x x x x
74 : * TC, 12k8, FS x
75 : * TC, 16k, FS x x x x x x x x x x x
76 : *
77 : * LR MDCT x x x x x x x x x x x
78 : *
79 : *-----------------------------------------------------------------*/
80 :
81 2050 : void decision_matrix_enc_fx(
82 : Encoder_State *st_fx, /* i : encoder state structure */
83 : Word16 *hq_core_type /* o : HQ core type Q0*/
84 : )
85 : {
86 2050 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
87 :
88 : /* initialization */
89 2050 : st_fx->core = -1;
90 2050 : move16();
91 2050 : st_fx->extl = -1;
92 2050 : move16();
93 2050 : st_fx->extl_brate = 0;
94 2050 : move16();
95 2050 : *hq_core_type = -1;
96 2050 : move16();
97 2050 : st_fx->igf = 0;
98 2050 : move16();
99 : /* SID and FRAME_NO_DATA frames */
100 2050 : test();
101 2050 : test();
102 2050 : IF( st_fx->Opt_DTX_ON && ( EQ_32( st_fx->core_brate, SID_2k40 ) || EQ_32( st_fx->core_brate, FRAME_NO_DATA ) ) )
103 : {
104 0 : st_fx->core = ACELP_CORE;
105 0 : move16();
106 :
107 0 : test();
108 0 : IF( GE_32( st_fx->input_Fs, 32000 ) && GE_16( st_fx->bwidth, SWB ) )
109 : {
110 0 : st_fx->extl = SWB_CNG;
111 0 : move16();
112 : }
113 :
114 0 : st_fx->rf_mode = 0;
115 0 : move16();
116 :
117 0 : return;
118 : }
119 :
120 2050 : st_fx->core_brate = L_deposit_l( 0 );
121 :
122 : /* SC-VBR */
123 2050 : IF( st_fx->Opt_SC_VBR )
124 : {
125 : /* SC-VBR */
126 0 : st_fx->core = ACELP_CORE;
127 0 : move16();
128 0 : st_fx->core_brate = ACELP_7k20;
129 0 : move32();
130 0 : st_fx->total_brate = ACELP_7k20;
131 0 : move32();
132 :
133 0 : test();
134 0 : test();
135 0 : test();
136 0 : IF( EQ_16( hSC_VBR->ppp_mode, 1 ) )
137 : {
138 : /* PPP mode */
139 0 : st_fx->core_brate = PPP_NELP_2k80;
140 0 : move16();
141 : }
142 0 : ELSE IF( ( ( EQ_16( st_fx->coder_type, UNVOICED ) || EQ_16( st_fx->coder_type, TRANSITION ) ) && st_fx->sp_aud_decision1 == 0 ) || NE_16( st_fx->bwidth, NB ) )
143 : {
144 0 : test();
145 0 : test();
146 0 : test();
147 0 : test();
148 0 : test();
149 0 : test();
150 0 : test();
151 0 : test();
152 0 : test();
153 0 : test();
154 0 : test();
155 0 : IF( EQ_16( st_fx->coder_type, UNVOICED ) && EQ_16( st_fx->vad_flag, 1 ) &&
156 : ( ( GE_16( st_fx->last_bwidth, SWB ) && EQ_16( st_fx->last_Opt_SC_VBR, 1 ) ) || LT_16( st_fx->last_bwidth, SWB ) ) &&
157 : ( NE_16( st_fx->last_core, HQ_CORE ) || NE_16( st_fx->bwidth, NB ) ) )
158 : {
159 : /* NELP mode */
160 0 : hSC_VBR->nelp_mode = 1;
161 0 : move16();
162 0 : st_fx->core_brate = PPP_NELP_2k80;
163 0 : move32();
164 : }
165 0 : ELSE IF( EQ_16( st_fx->coder_type, TRANSITION ) || ( EQ_16( st_fx->coder_type, UNVOICED ) && NE_16( hSC_VBR->nelp_mode, 1 ) ) ||
166 : ( ( EQ_16( st_fx->coder_type, AUDIO ) || EQ_16( st_fx->coder_type, INACTIVE ) ) && NE_16( st_fx->bwidth, NB ) ) )
167 :
168 : {
169 : /* silence portions */
170 0 : st_fx->core_brate = ACELP_8k00;
171 0 : move32();
172 0 : st_fx->total_brate = ACELP_8k00;
173 0 : move32();
174 : }
175 : }
176 :
177 0 : return;
178 : }
179 :
180 : /*---------------------------------------------------------------------*
181 : * NB
182 : *---------------------------------------------------------------------*/
183 :
184 2050 : ELSE IF( EQ_16( st_fx->bwidth, NB ) )
185 : {
186 0 : st_fx->core = ACELP_CORE;
187 0 : move16();
188 :
189 : #ifdef DEBUGGING
190 : if ( st_fx->total_brate >= HQCORE_NB_MIN_RATE && ( st_fx->force == FORCE_MUSIC || ( st_fx->force == -1 && st_fx->sp_aud_decision1 == 1 ) ) )
191 : {
192 : st_fx->core = HQ_CORE;
193 : }
194 : #else
195 0 : test();
196 0 : IF( GE_32( st_fx->total_brate, HQCORE_NB_MIN_RATE ) && EQ_16( st_fx->sp_aud_decision1, 1 ) )
197 : {
198 0 : st_fx->core = HQ_CORE;
199 0 : move16();
200 : }
201 : #endif
202 : }
203 :
204 : /*---------------------------------------------------------------------*
205 : * WB
206 : *---------------------------------------------------------------------*/
207 :
208 2050 : ELSE IF( EQ_16( st_fx->bwidth, WB ) )
209 : {
210 0 : st_fx->core = ACELP_CORE;
211 0 : move16();
212 :
213 : #ifdef DEBUGGING
214 : if ( ( st_fx->total_brate >= HQCORE_WB_MIN_RATE && ( st_fx->force == FORCE_MUSIC || ( st_fx->force == -1 && st_fx->sp_aud_decision1 == 1 ) ) ) || st_fx->total_brate >= HQ_96k )
215 : #else
216 0 : test();
217 0 : test();
218 0 : IF( ( GE_32( st_fx->total_brate, HQCORE_WB_MIN_RATE ) && EQ_16( st_fx->sp_aud_decision1, 1 ) ) ||
219 : GE_32( st_fx->total_brate, HQ_96k ) )
220 : #endif
221 : {
222 0 : st_fx->core = HQ_CORE;
223 0 : move16();
224 : }
225 : ELSE
226 : {
227 0 : test();
228 0 : test();
229 0 : test();
230 0 : test();
231 0 : IF( EQ_16( st_fx->bwidth, WB ) && LT_32( st_fx->total_brate, ACELP_9k60 ) )
232 : {
233 0 : st_fx->extl = WB_BWE;
234 0 : move16();
235 : }
236 0 : ELSE IF( EQ_16( st_fx->bwidth, WB ) && GE_32( st_fx->total_brate, ACELP_9k60 ) && LE_32( st_fx->total_brate, ACELP_16k40 ) )
237 : {
238 : /* Note: WB BWE is used exceptionally at 13.2 kbps if GSC is selected instead of LR-MDCT */
239 0 : test();
240 0 : test();
241 0 : test();
242 0 : IF( EQ_16( st_fx->sp_aud_decision1, 1 ) || EQ_16( st_fx->coder_type, INACTIVE ) || ( st_fx->sp_aud_decision1 == 0 && EQ_16( st_fx->sp_aud_decision2, 1 ) ) )
243 : {
244 0 : st_fx->extl = WB_BWE;
245 0 : move16();
246 0 : st_fx->extl_brate = WB_BWE_0k35;
247 0 : move32();
248 : }
249 : ELSE
250 : {
251 0 : st_fx->extl = WB_TBE;
252 0 : move16();
253 0 : st_fx->extl_brate = WB_TBE_1k05;
254 0 : move32();
255 : }
256 : }
257 : }
258 : }
259 :
260 : /*---------------------------------------------------------------------*
261 : * SWB and FB
262 : *---------------------------------------------------------------------*/
263 :
264 2050 : ELSE IF( EQ_16( st_fx->bwidth, SWB ) || EQ_16( st_fx->bwidth, FB ) )
265 : {
266 : #ifdef DEBUGGING
267 : if ( ( st_fx->total_brate >= HQCORE_WB_MIN_RATE && ( st_fx->force == FORCE_MUSIC || ( st_fx->force == -1 && st_fx->sp_aud_decision1 == 1 ) ) ) || st_fx->total_brate >= HQ_96k )
268 : #else
269 2050 : test();
270 2050 : test();
271 2050 : IF( ( GE_32( st_fx->total_brate, HQCORE_SWB_MIN_RATE ) && EQ_16( st_fx->sp_aud_decision1, 1 ) ) ||
272 : GE_32( st_fx->total_brate, HQ_96k ) )
273 : #endif
274 : {
275 627 : st_fx->core = HQ_CORE;
276 627 : move16();
277 : }
278 : ELSE
279 : {
280 1423 : st_fx->core = ACELP_CORE;
281 1423 : move16();
282 :
283 1423 : test();
284 1423 : test();
285 1423 : IF( GE_32( st_fx->total_brate, ACELP_13k20 ) && LT_32( st_fx->total_brate, ACELP_48k ) )
286 : {
287 : /* Note: SWB BWE is not used in case of GSC noisy speech */
288 : /* Note: SWB BWE is used exceptionally at 13.2 kbps if GSC is selected instead of LR-MDCT */
289 749 : test();
290 749 : test();
291 749 : test();
292 749 : test();
293 749 : IF( ( EQ_16( st_fx->sp_aud_decision1, 1 ) || EQ_16( st_fx->coder_type, INACTIVE ) || ( st_fx->sp_aud_decision1 == 0 && EQ_16( st_fx->sp_aud_decision2, 1 ) ) ) && st_fx->GSC_noisy_speech == 0 )
294 : {
295 8 : st_fx->extl = SWB_BWE;
296 8 : move16();
297 8 : st_fx->extl_brate = SWB_BWE_1k6;
298 8 : move32();
299 :
300 8 : test();
301 8 : IF( EQ_16( st_fx->bwidth, FB ) && GE_32( st_fx->total_brate, ACELP_24k40 ) )
302 : {
303 0 : st_fx->extl = FB_BWE;
304 0 : move16();
305 0 : st_fx->extl_brate = FB_BWE_1k8;
306 0 : move32();
307 : }
308 : }
309 : ELSE
310 : {
311 741 : st_fx->extl = SWB_TBE;
312 741 : move16();
313 741 : st_fx->extl_brate = SWB_TBE_1k6;
314 741 : move32();
315 741 : if ( GE_32( st_fx->total_brate, ACELP_24k40 ) )
316 : {
317 0 : st_fx->extl_brate = SWB_TBE_2k8;
318 0 : move32();
319 : }
320 :
321 741 : test();
322 741 : IF( EQ_16( st_fx->bwidth, FB ) && GE_32( st_fx->total_brate, ACELP_24k40 ) )
323 : {
324 0 : st_fx->extl = FB_TBE;
325 0 : move16();
326 : {
327 0 : st_fx->extl_brate = FB_TBE_3k0;
328 0 : move32();
329 : }
330 : }
331 : }
332 : }
333 674 : ELSE IF( GE_32( st_fx->total_brate, ACELP_48k ) )
334 : {
335 674 : st_fx->extl = SWB_BWE_HIGHRATE;
336 674 : move16();
337 674 : st_fx->extl_brate = SWB_BWE_16k;
338 674 : move32();
339 :
340 674 : if ( EQ_16( st_fx->bwidth, FB ) )
341 : {
342 0 : st_fx->extl = FB_BWE_HIGHRATE;
343 0 : move32();
344 : }
345 : }
346 : }
347 : }
348 :
349 : /*-----------------------------------------------------------------*
350 : * Set HQ core type
351 : *-----------------------------------------------------------------*/
352 :
353 2050 : IF( EQ_16( st_fx->core, HQ_CORE ) )
354 : {
355 627 : *hq_core_type = NORMAL_HQ_CORE;
356 627 : move16();
357 :
358 627 : test();
359 627 : test();
360 627 : IF( ( EQ_16( st_fx->bwidth, SWB ) || EQ_16( st_fx->bwidth, WB ) ) && LE_32( st_fx->total_brate, LRMDCT_CROSSOVER_POINT ) )
361 : {
362 : /* note that FB (bitrate >= 24400 bps) is always coded with NORMAL_HQ_CORE */
363 301 : *hq_core_type = LOW_RATE_HQ_CORE;
364 301 : move16();
365 : }
366 326 : ELSE IF( EQ_16( st_fx->bwidth, NB ) )
367 : {
368 0 : *hq_core_type = LOW_RATE_HQ_CORE;
369 0 : move16();
370 : }
371 : }
372 :
373 : /* set core bitrate */
374 2050 : st_fx->core_brate = L_sub( st_fx->total_brate, st_fx->extl_brate );
375 :
376 2050 : IF( st_fx->ini_frame == 0 )
377 : {
378 : /* avoid switching in the very first frame */
379 2 : st_fx->last_core = st_fx->core;
380 2 : move16();
381 2 : st_fx->last_core_brate = st_fx->core_brate;
382 2 : move32();
383 2 : st_fx->last_extl = st_fx->extl;
384 2 : move16();
385 : }
386 :
387 : /*-----------------------------------------------------------------*
388 : * set inactive coder_type flag in ACELP core
389 : *-----------------------------------------------------------------*/
390 :
391 2050 : st_fx->inactive_coder_type_flag = 0; /* AVQ by default */
392 2050 : move16();
393 :
394 2050 : if ( LE_32( st_fx->total_brate, MAX_GSC_INACTIVE_BRATE ) )
395 : {
396 1050 : st_fx->inactive_coder_type_flag = 1; /* GSC */
397 1050 : move16();
398 : }
399 :
400 2050 : return;
401 : }
402 :
403 : /*---------------------------------------------------------------------*
404 : * signalling_mode1_tcx20_enc()
405 : *
406 : * write MODE1 TCX20 signalling information into the bitstream
407 : *---------------------------------------------------------------------*/
408 :
409 524 : Word16 signalling_mode1_tcx20_enc_fx(
410 : Encoder_State *st, /* i : encoder state structure */
411 : Word16 push /*Q0*/ )
412 : {
413 : Word16 num_bits;
414 : Word16 nBits, idx, start_idx;
415 : BSTR_ENC_HANDLE hBstr;
416 :
417 524 : hBstr = st->hBstr;
418 524 : assert( st->core == TCX_20_CORE );
419 :
420 524 : num_bits = 0;
421 524 : move16();
422 :
423 : /* Use ACELP signaling for LR MDCT */
424 524 : IF( LE_32( st->total_brate, ACELP_16k40 ) )
425 : {
426 : /* find the section in the ACELP signalling table corresponding to bitrate */
427 524 : idx = 0;
428 524 : move16();
429 18340 : WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) )
430 : {
431 17816 : idx = add( idx, 1 );
432 : }
433 :
434 : /* retrieve the number of bits for signalling */
435 524 : idx = add( idx, 1 );
436 524 : nBits = extract_l( acelp_sig_tbl[idx] ); /*Q0*/
437 :
438 : /* retrieve the signalling index */
439 524 : idx = add( idx, 1 );
440 524 : start_idx = idx;
441 524 : move16();
442 16768 : WHILE( NE_32( acelp_sig_tbl[idx], SIG2IND_fx( LR_MDCT, st->bwidth, 0, 0 ) ) )
443 : {
444 16244 : idx = add( idx, 1 );
445 : }
446 :
447 524 : num_bits = add( num_bits, nBits );
448 524 : IF( push != 0 )
449 : {
450 262 : push_indice( hBstr, IND_ACELP_SIGNALLING, sub( idx, start_idx ), nBits );
451 : }
452 :
453 : /* HQ/TCX core switching flag */
454 524 : num_bits = add( num_bits, 1 );
455 524 : IF( push != 0 )
456 : {
457 262 : push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
458 : }
459 : }
460 : ELSE
461 : {
462 0 : IF( LE_32( st->core_brate, ACELP_64k ) )
463 : {
464 : /* write ACELP/HQ core indication flag */
465 0 : num_bits = add( num_bits, 1 );
466 0 : IF( push != 0 )
467 : {
468 0 : push_indice( hBstr, IND_CORE, 1, 1 );
469 : }
470 : }
471 :
472 : /* HQ/TCX core switching flag */
473 0 : num_bits = add( num_bits, 1 );
474 0 : IF( push != 0 )
475 : {
476 0 : push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
477 : }
478 :
479 0 : num_bits = add( num_bits, 2 );
480 0 : IF( push != 0 )
481 : {
482 : /* write band-width (needed for different I/O sampling rate support) */
483 0 : IF( EQ_16( st->bwidth, NB ) )
484 : {
485 0 : push_indice( hBstr, IND_HQ_BWIDTH, 0, 2 );
486 : }
487 0 : ELSE IF( EQ_16( st->bwidth, WB ) )
488 : {
489 0 : push_indice( hBstr, IND_HQ_BWIDTH, 1, 2 );
490 : }
491 0 : ELSE IF( EQ_16( st->bwidth, SWB ) )
492 : {
493 0 : push_indice( hBstr, IND_HQ_BWIDTH, 2, 2 );
494 : }
495 : ELSE /* st->bwidth == FB */
496 : {
497 0 : push_indice( hBstr, IND_HQ_BWIDTH, 3, 2 );
498 : }
499 : }
500 : }
501 :
502 524 : return num_bits;
503 : }
504 :
505 : /*---------------------------------------------------------------------*
506 : * signalling_enc()
507 : *
508 : * write signalling information into the bit-stream
509 : *---------------------------------------------------------------------*/
510 :
511 1854 : void signalling_enc_fx(
512 : Encoder_State *st_fx /* i : encoder state structure */
513 : )
514 : {
515 : Word16 nBits, idx, start_idx;
516 : Word32 k, total_brate_temp;
517 1854 : SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
518 : BSTR_ENC_HANDLE hBstr;
519 :
520 1854 : hBstr = st_fx->hBstr;
521 :
522 1854 : IF( EQ_16( st_fx->mdct_sw, MODE2 ) )
523 : {
524 :
525 66 : assert( !st_fx->tcxonly );
526 66 : assert( st_fx->core == HQ_CORE );
527 :
528 66 : push_next_indice( hBstr, 1, 1 ); /* TCX */
529 66 : push_next_indice( hBstr, 1, 1 ); /* HQ_CORE */
530 :
531 : /* write ACELP->HQ core switching flag */
532 66 : test();
533 66 : IF( ( st_fx->last_core == ACELP_CORE ) || EQ_16( st_fx->last_core, AMR_WB_CORE ) )
534 : {
535 6 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
536 :
537 : /* write ACELP L_frame info */
538 6 : IF( EQ_16( st_fx->last_L_frame, L_FRAME ) )
539 : {
540 0 : push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
541 : }
542 : ELSE
543 : {
544 6 : push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
545 : }
546 : }
547 : ELSE
548 : {
549 60 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
550 : }
551 :
552 66 : return;
553 : }
554 1788 : IF( ( st_fx->core == ACELP_CORE ) )
555 : {
556 : Word16 ppp_mode, nelp_mode, sig;
557 :
558 1423 : ppp_mode = 0;
559 1423 : nelp_mode = 0;
560 1423 : move16();
561 1423 : move16();
562 1423 : IF( st_fx->Opt_SC_VBR )
563 : {
564 0 : ppp_mode = hSC_VBR->ppp_mode;
565 0 : nelp_mode = hSC_VBR->nelp_mode;
566 0 : move16();
567 0 : move16();
568 : }
569 :
570 1423 : test();
571 1423 : test();
572 1423 : IF( EQ_16( ppp_mode, 1 ) || EQ_16( nelp_mode, 1 ) )
573 : {
574 : /* 1 bit to distinguish between 2.8kbps PPP/NELP frame and SID frame */
575 0 : push_indice( hBstr, IND_CORE, 0, 1 );
576 : /* SC-VBR: 0 - PPP_NB, 1 - PPP_WB, 2 - NELP_NB, 3 - NELP_WB */
577 0 : test();
578 0 : test();
579 0 : test();
580 0 : test();
581 0 : test();
582 0 : test();
583 0 : test();
584 0 : test();
585 0 : IF( EQ_16( st_fx->coder_type, VOICED ) && EQ_16( st_fx->bwidth, NB ) && EQ_16( ppp_mode, 1 ) )
586 : {
587 0 : push_indice( hBstr, IND_PPP_NELP_MODE, 0, 2 );
588 : }
589 0 : ELSE IF( EQ_16( st_fx->coder_type, VOICED ) && NE_16( st_fx->bwidth, NB ) && EQ_16( ppp_mode, 1 ) )
590 : {
591 0 : push_indice( hBstr, IND_PPP_NELP_MODE, 1, 2 );
592 : }
593 0 : ELSE IF( EQ_16( st_fx->coder_type, UNVOICED ) && EQ_16( st_fx->bwidth, NB ) && EQ_16( nelp_mode, 1 ) )
594 : {
595 0 : push_indice( hBstr, IND_PPP_NELP_MODE, 2, 2 );
596 : }
597 0 : ELSE IF( EQ_16( st_fx->coder_type, UNVOICED ) && NE_16( st_fx->bwidth, NB ) && EQ_16( nelp_mode, 1 ) )
598 : {
599 0 : push_indice( hBstr, IND_PPP_NELP_MODE, 3, 2 );
600 : }
601 : }
602 1423 : ELSE IF( NE_32( st_fx->core_brate, SID_2k40 ) && ( st_fx->core_brate != FRAME_NO_DATA ) )
603 : {
604 : /* write the ACELP/HQ core selection bit */
605 1423 : IF( GE_32( st_fx->total_brate, ACELP_24k40 ) )
606 : {
607 674 : push_indice( hBstr, IND_CORE, 0, 1 );
608 : }
609 :
610 : /* find the section in the ACELP signalling table corresponding to bitrate */
611 1423 : idx = 0;
612 1423 : move16();
613 9062 : WHILE( LT_16( idx, MAX_ACELP_SIG ) )
614 : {
615 9062 : IF( LE_32( st_fx->total_brate, brate_tbl[idx] ) )
616 : {
617 1423 : BREAK;
618 : }
619 7639 : idx++;
620 : }
621 1423 : total_brate_temp = brate_tbl[idx]; /*Q0*/
622 1423 : move32();
623 :
624 1423 : idx = 0;
625 1423 : move16();
626 84179 : WHILE( NE_32( acelp_sig_tbl[idx], total_brate_temp ) )
627 : {
628 82756 : idx++;
629 : }
630 :
631 : /* retrieve the number of bits for signalling */
632 1423 : nBits = (Word16) acelp_sig_tbl[++idx]; /*Q0*/
633 1423 : move16();
634 :
635 : /* retrieve the signalling index */
636 1423 : start_idx = ++idx;
637 1423 : move16();
638 1423 : IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) && EQ_16( st_fx->bwidth, SWB ) && LE_32( st_fx->total_brate, ACELP_9k60 ) )
639 : {
640 : /* patch to signal SWB as NB in Stereo */
641 0 : sig = (Word16) SIG2IND( st_fx->coder_type, NB, st_fx->sharpFlag, st_fx->rf_mode ); /*Q0*/
642 : }
643 : ELSE
644 : {
645 1423 : sig = (Word16) SIG2IND( st_fx->coder_type, st_fx->bwidth, st_fx->sharpFlag, st_fx->rf_mode ); /*Q0*/
646 : }
647 :
648 11515 : WHILE( NE_16( (Word16) acelp_sig_tbl[idx], sig ) )
649 : {
650 10092 : idx++;
651 : }
652 :
653 1423 : push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
654 : }
655 :
656 : /* write extension layer flag to distinguish between TBE (0) and BWE (1) */
657 1423 : IF( st_fx->extl_brate > 0 )
658 : {
659 1423 : test();
660 1423 : test();
661 1423 : test();
662 1423 : test();
663 1423 : IF( EQ_16( st_fx->extl, WB_TBE ) || EQ_16( st_fx->extl, SWB_TBE ) || EQ_16( st_fx->extl, FB_TBE ) )
664 : {
665 741 : push_indice( hBstr, IND_BWE_FLAG, 0, 1 );
666 : }
667 682 : ELSE IF( EQ_16( st_fx->extl, WB_BWE ) || EQ_16( st_fx->extl, SWB_BWE ) || EQ_16( st_fx->extl, FB_BWE ) )
668 : {
669 8 : push_indice( hBstr, IND_BWE_FLAG, 1, 1 );
670 : }
671 : }
672 : }
673 : ELSE /* HQ core */
674 : {
675 : /* write ACELP->HQ switching frame flag */
676 365 : test();
677 365 : IF( ( st_fx->last_core == ACELP_CORE ) || EQ_16( st_fx->last_core, AMR_WB_CORE ) )
678 : {
679 19 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
680 : /* write ACELP L_frame info */
681 19 : IF( EQ_16( st_fx->last_L_frame, L_FRAME ) )
682 : {
683 4 : push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
684 : }
685 : ELSE
686 : {
687 15 : push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
688 : }
689 : }
690 : ELSE
691 : {
692 346 : push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
693 : }
694 :
695 : /* HQ/TCX core switching flag */
696 365 : push_indice( hBstr, IND_MDCT_CORE, 0, 1 );
697 :
698 : /* Use ACELP signaling for LR MDCT */
699 365 : IF( LE_32( st_fx->total_brate, ACELP_16k40 ) )
700 : {
701 : /* find the section in the ACELP signalling table corresponding to bitrate */
702 39 : idx = 0;
703 1365 : WHILE( NE_32( acelp_sig_tbl[idx], st_fx->total_brate ) )
704 : {
705 1326 : idx++;
706 : }
707 :
708 : /* retrieve the number of bits for signalling */
709 39 : nBits = extract_l( acelp_sig_tbl[++idx] ); /*Q0*/
710 :
711 : /* retrieve the signalling index */
712 39 : start_idx = ++idx;
713 39 : move16();
714 39 : k = SIG2IND_fx( LR_MDCT, st_fx->bwidth, 0, 0 ); /*Q0*/
715 1248 : WHILE( NE_32( acelp_sig_tbl[idx], k ) )
716 : {
717 1209 : idx++;
718 : }
719 :
720 39 : push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
721 : }
722 : ELSE
723 : {
724 :
725 326 : IF( LE_32( st_fx->core_brate, ACELP_64k ) )
726 : {
727 : /* write ACELP/HQ core indication flag */
728 326 : push_indice( hBstr, IND_CORE, 1, 1 );
729 : }
730 :
731 : /* write band-width (needed for different I/O sampling rate support) */
732 326 : IF( EQ_16( st_fx->bwidth, NB ) )
733 : {
734 0 : push_indice( hBstr, IND_HQ_BWIDTH, 0, 2 );
735 : }
736 326 : ELSE IF( EQ_16( st_fx->bwidth, WB ) )
737 : {
738 0 : push_indice( hBstr, IND_HQ_BWIDTH, 1, 2 );
739 : }
740 326 : ELSE IF( EQ_16( st_fx->bwidth, SWB ) )
741 : {
742 326 : push_indice( hBstr, IND_HQ_BWIDTH, 2, 2 );
743 : }
744 : ELSE /* st_fx->bwidth == FB */
745 : {
746 0 : push_indice( hBstr, IND_HQ_BWIDTH, 3, 2 );
747 : }
748 : }
749 : }
750 :
751 1788 : return;
752 : }
753 :
754 : /*---------------------------------------------------------------------*
755 : * signalling_enc_rf()
756 : *
757 : * write channel-aware signalling information into the bitstream
758 : *---------------------------------------------------------------------*/
759 1133872 : void signaling_enc_rf_fx(
760 : Encoder_State *st /* i : encoder state structure */
761 : )
762 : {
763 : Word16 i;
764 : Word16 sfr;
765 1133872 : RF_ENC_HANDLE hRF = st->hRF;
766 :
767 : /* write partial copy into bitstream */
768 1133872 : IF( EQ_16( st->rf_mode, 1 ) )
769 : {
770 0 : enc_prm_rf_ivas_fx( st, hRF->rf_indx_frametype[st->rf_fec_offset], st->rf_fec_offset );
771 0 : hRF->rf_indx_tbeGainFr[0] = hRF->RF_bwe_gainFr_ind; /*Q0*/
772 0 : move16();
773 : }
774 :
775 : /* Shift the RF indices such that the partial copy associated with
776 : (n-fec_offset)th frame is included in the bitstream in nth frame. */
777 1133872 : IF( hRF != NULL )
778 : {
779 0 : FOR( i = st->rf_fec_offset; i >= 0; i-- )
780 : {
781 : /* rf frame type */
782 0 : hRF->rf_indx_frametype[i + 1] = hRF->rf_indx_frametype[i];
783 0 : move16();
784 : /* rf target bits buffer */
785 0 : hRF->rf_targetbits_buff[i + 1] = hRF->rf_targetbits_buff[i];
786 0 : move16();
787 : /* lsf indx */
788 0 : hRF->rf_indx_lsf[i + 1][0] = hRF->rf_indx_lsf[i][0];
789 0 : hRF->rf_indx_lsf[i + 1][1] = hRF->rf_indx_lsf[i][1];
790 0 : hRF->rf_indx_lsf[i + 1][2] = hRF->rf_indx_lsf[i][2];
791 0 : move16();
792 0 : move16();
793 0 : move16();
794 : /* ES pred energy */
795 0 : hRF->rf_indx_EsPred[i + 1] = hRF->rf_indx_EsPred[i];
796 0 : move16();
797 : /* LTF mode, sfr params: pitch, fcb and gain */
798 0 : FOR( sfr = 0; sfr < st->nb_subfr; sfr++ )
799 : {
800 0 : hRF->rf_indx_ltfMode[i + 1][sfr] = hRF->rf_indx_ltfMode[i][sfr];
801 0 : hRF->rf_indx_pitch[i + 1][sfr] = hRF->rf_indx_pitch[i][sfr];
802 0 : hRF->rf_indx_fcb[i + 1][sfr] = hRF->rf_indx_fcb[i][sfr];
803 0 : hRF->rf_indx_gain[i + 1][sfr] = hRF->rf_indx_gain[i][sfr];
804 0 : move16();
805 0 : move16();
806 0 : move16();
807 0 : move16();
808 : }
809 :
810 : /* shift the nelp indices */
811 0 : hRF->rf_indx_nelp_iG1[i + 1] = hRF->rf_indx_nelp_iG1[i];
812 0 : hRF->rf_indx_nelp_iG2[i + 1][0] = hRF->rf_indx_nelp_iG2[i][0];
813 0 : hRF->rf_indx_nelp_iG2[i + 1][1] = hRF->rf_indx_nelp_iG2[i][1];
814 0 : hRF->rf_indx_nelp_fid[i + 1] = hRF->rf_indx_nelp_fid[i];
815 0 : move16();
816 0 : move16();
817 0 : move16();
818 0 : move16();
819 : /* tbe gain Fr shift */
820 0 : hRF->rf_indx_tbeGainFr[i + 1] = hRF->rf_indx_tbeGainFr[i];
821 0 : hRF->rf_clas[i + 1] = hRF->rf_clas[i];
822 0 : hRF->rf_gain_tcx[i + 1] = hRF->rf_gain_tcx[i];
823 0 : hRF->rf_tcxltp_param[i + 1] = hRF->rf_tcxltp_param[i];
824 0 : move16();
825 0 : move16();
826 0 : move16();
827 0 : move16();
828 : }
829 : }
830 1133872 : return;
831 : }
832 :
833 3100 : void signalling_enc_rf_fx(
834 : Encoder_State *st /* i : encoder state structure */
835 : )
836 : {
837 : Word16 i;
838 : Word16 sfr;
839 3100 : RF_ENC_HANDLE hRF = st->hRF;
840 :
841 : /* write partial copy into bitstream */
842 3100 : IF( EQ_16( st->rf_mode, 1 ) )
843 : {
844 0 : enc_prm_rf_fx( st, hRF->rf_indx_frametype[st->rf_fec_offset], st->rf_fec_offset );
845 0 : hRF->rf_indx_tbeGainFr[0] = hRF->RF_bwe_gainFr_ind; /*Q0*/
846 : }
847 :
848 : /* Shift the RF indices such that the partial copy associated with
849 : (n-fec_offset)th frame is included in the bitstream in nth frame. */
850 :
851 6200 : FOR( i = st->rf_fec_offset; i >= 0; i-- )
852 : {
853 : /* rf frame type */
854 3100 : hRF->rf_indx_frametype[i + 1] = hRF->rf_indx_frametype[i];
855 : /* rf target bits buffer */
856 3100 : hRF->rf_targetbits_buff[i + 1] = hRF->rf_targetbits_buff[i];
857 :
858 : /* lsf indx */
859 3100 : hRF->rf_indx_lsf[i + 1][0] = hRF->rf_indx_lsf[i][0];
860 3100 : hRF->rf_indx_lsf[i + 1][1] = hRF->rf_indx_lsf[i][1];
861 3100 : hRF->rf_indx_lsf[i + 1][2] = hRF->rf_indx_lsf[i][2];
862 :
863 : /* ES pred energy */
864 3100 : hRF->rf_indx_EsPred[i + 1] = hRF->rf_indx_EsPred[i];
865 :
866 : /* LTF mode, sfr params: pitch, fcb and gain */
867 16550 : FOR( sfr = 0; sfr < st->nb_subfr; sfr++ )
868 : {
869 13450 : hRF->rf_indx_ltfMode[i + 1][sfr] = hRF->rf_indx_ltfMode[i][sfr];
870 13450 : hRF->rf_indx_pitch[i + 1][sfr] = hRF->rf_indx_pitch[i][sfr];
871 13450 : hRF->rf_indx_fcb[i + 1][sfr] = hRF->rf_indx_fcb[i][sfr];
872 13450 : hRF->rf_indx_gain[i + 1][sfr] = hRF->rf_indx_gain[i][sfr];
873 : }
874 :
875 : /* shift the nelp indices */
876 3100 : hRF->rf_indx_nelp_iG1[i + 1] = hRF->rf_indx_nelp_iG1[i];
877 3100 : hRF->rf_indx_nelp_iG2[i + 1][0] = hRF->rf_indx_nelp_iG2[i][0];
878 3100 : hRF->rf_indx_nelp_iG2[i + 1][1] = hRF->rf_indx_nelp_iG2[i][1];
879 3100 : hRF->rf_indx_nelp_fid[i + 1] = hRF->rf_indx_nelp_fid[i];
880 :
881 : /* tbe gain Fr shift */
882 3100 : hRF->rf_indx_tbeGainFr[i + 1] = hRF->rf_indx_tbeGainFr[i];
883 3100 : hRF->rf_clas[i + 1] = hRF->rf_clas[i];
884 3100 : hRF->rf_gain_tcx[i + 1] = hRF->rf_gain_tcx[i];
885 3100 : hRF->rf_tcxltp_param[i + 1] = hRF->rf_tcxltp_param[i];
886 : }
887 :
888 3100 : return;
889 : }
|