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